]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
upd: move external keywords to own class
[gigi.git] / tests / org / cacert / gigi / ping / TestHTTP.java
1 package org.cacert.gigi.ping;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5 import static org.junit.Assume.*;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.net.ServerSocket;
13 import java.net.Socket;
14 import java.net.URL;
15 import java.net.URLEncoder;
16 import java.sql.SQLException;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import javax.naming.NamingException;
21
22 import org.cacert.gigi.GigiApiException;
23 import org.cacert.gigi.dbObjects.Domain;
24 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
25 import org.cacert.gigi.dbObjects.DomainPingType;
26 import org.cacert.gigi.testUtils.IOUtils;
27 import org.cacert.gigi.testUtils.PingTest;
28 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
29 import org.cacert.gigi.util.RandomToken;
30 import org.cacert.gigi.util.SystemKeywords;
31 import org.junit.Test;
32
33 public class TestHTTP extends PingTest {
34
35     @Test
36     public void httpSanity() throws IOException, NamingException {
37
38         String token = RandomToken.generateToken(16);
39         String value = RandomToken.generateToken(16);
40
41         TestDNS.updateService(token, value, "http");
42         assertEquals(value, readHTTP(token));
43
44     }
45
46     @Test
47     public void httpAndMailSuccess() throws Exception {
48         testEmailAndHTTP(0, 0, true, true);
49     }
50
51     @Test
52     public void httpFailKeyAndMailSuccess() throws Exception {
53         testEmailAndHTTP(1, 0, false, true);
54     }
55
56     @Test
57     public void httpFailValAndMailFail() throws Exception {
58         testEmailAndHTTP(2, 1, false, false);
59     }
60
61     public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
62
63         String test = getTestProps().getProperty("domain.http");
64         assumeNotNull(test);
65
66         Matcher m = initailizeDomainForm();
67         updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
68
69         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
70                 "&emailType=y&email=2&HTTPType=y" + //
71                 "&ssl-type-0=direct&ssl-port-0=" + //
72                 "&ssl-type-1=direct&ssl-port-1=" + //
73                 "&ssl-type-2=direct&ssl-port-2=" + //
74                 "&ssl-type-3=direct&ssl-port-3=" + //
75                 "&adddomain&csrf=" + csrf;
76         String p2 = sendDomainForm(content);
77
78         TestMail mail = getMailReceiver().receive();
79         if (emailVariant == 0) {
80             mail.verify();
81         }
82         waitForPings(2);
83
84         String newcontent = IOUtils.readURL(get(p2));
85         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
86         assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
87         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
88         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
89
90         if (successHTTP) { // give it a second try
91             int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
92             Domain d = Domain.getById(id);
93             DomainPingConfiguration dpc = null;
94             for (DomainPingConfiguration conf : d.getConfiguredPings()) {
95                 if (conf.getType() == DomainPingType.HTTP) {
96                     dpc = conf;
97                     break;
98                 }
99             }
100             if (dpc == null) {
101                 fail("Http config not found");
102                 return;
103             }
104             String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
105             assertThat(res, containsString("only allowed after"));
106         }
107     }
108
109     private String readHTTP(String token) throws IOException {
110         String httpDom = getTestProps().getProperty("domain.http");
111         assumeNotNull(httpDom);
112         URL u = new URL("http://" + httpDom + "/" + SystemKeywords.HTTP_CHALLENGE_PREFIX + token + ".txt");
113         return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
114
115     }
116
117     @Test
118     public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
119         try (ServerSocket s = openServer()) {
120             testHttpRedirect(s, true);
121         }
122     }
123
124     @Test
125     public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
126         try (ServerSocket s = openServer()) {
127             testHttpRedirect(s, false);
128         }
129     }
130
131     private ServerSocket openServer() {
132         String localHTTP = getTestProps().getProperty("domain.localHTTP");
133         assumeNotNull(localHTTP);
134         try {
135             return new ServerSocket(Integer.parseInt(localHTTP));
136         } catch (IOException e) {
137             throw new Error("Requires a free port " + localHTTP);
138         }
139     }
140
141     public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
142         String test = getTestProps().getProperty("domain.local");
143         assumeNotNull(test);
144
145         Matcher m = initailizeDomainForm();
146
147         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
148                 "&emailType=y&email=2&HTTPType=y" + //
149                 "&ssl-type-0=direct&ssl-port-0=" + //
150                 "&ssl-type-1=direct&ssl-port-1=" + //
151                 "&ssl-type-2=direct&ssl-port-2=" + //
152                 "&ssl-type-3=direct&ssl-port-3=" + //
153                 "&adddomain&csrf=" + csrf;
154         String p2 = sendDomainForm(content);
155         try (Socket s0 = s.accept()) {
156             BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
157             String fst = br.readLine();
158             assertEquals("GET /" + SystemKeywords.HTTP_CHALLENGE_PREFIX + m.group(1) + ".txt HTTP/1.1", fst);
159             while ( !"".equals(br.readLine())) {
160             }
161             String res = m.group(2);
162             PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
163             if ( !doRedirect) {
164                 out.println("HTTP/1.1 200 OK");
165                 out.println("Content-length: " + res.length());
166                 out.println();
167                 out.print(res);
168             } else {
169                 out.println("HTTP/1.1 302 Moved");
170                 out.println("Location: /token");
171                 out.println();
172             }
173             out.flush();
174         }
175         waitForPings(2);
176
177         TestMail mail = getMailReceiver().receive();
178         mail.verify();
179
180         String newcontent = IOUtils.readURL(get(p2));
181         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
182         pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
183         Matcher m0 = pat.matcher(newcontent);
184         assertTrue(newcontent, m0.find());
185         if (doRedirect) {
186             assertEquals("failed", m0.group(1));
187             assertThat(m0.group(3), containsString("code 302"));
188         } else {
189             assertEquals("success", m0.group(1));
190         }
191         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
192         assertTrue(newcontent, pat.matcher(newcontent).find());
193
194     }
195 }