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