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