]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
fix: Avoid NPE when handling the HTTP response in tests
[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                 return;
102             }
103             String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
104             assertThat(res, containsString("only allowed after"));
105         }
106     }
107
108     private String readHTTP(String token) throws IOException {
109         String httpDom = getTestProps().getProperty("domain.http");
110         assumeNotNull(httpDom);
111         URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
112         return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
113
114     }
115
116     @Test
117     public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
118         try (ServerSocket s = openServer()) {
119             testHttpRedirect(s, true);
120         }
121     }
122
123     @Test
124     public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
125         try (ServerSocket s = openServer()) {
126             testHttpRedirect(s, false);
127         }
128     }
129
130     private ServerSocket openServer() {
131         String localHTTP = getTestProps().getProperty("domain.localHTTP");
132         assumeNotNull(localHTTP);
133         try {
134             return new ServerSocket(Integer.parseInt(localHTTP));
135         } catch (IOException e) {
136             throw new Error("Requires a free port " + localHTTP);
137         }
138     }
139
140     public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
141         String test = getTestProps().getProperty("domain.local");
142         assumeNotNull(test);
143
144         Matcher m = initailizeDomainForm();
145
146         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
147                 "&emailType=y&email=2&HTTPType=y" + //
148                 "&ssl-type-0=direct&ssl-port-0=" + //
149                 "&ssl-type-1=direct&ssl-port-1=" + //
150                 "&ssl-type-2=direct&ssl-port-2=" + //
151                 "&ssl-type-3=direct&ssl-port-3=" + //
152                 "&adddomain&csrf=" + csrf;
153         String p2 = sendDomainForm(content);
154         try (Socket s0 = s.accept()) {
155             BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
156             String fst = br.readLine();
157             assertEquals("GET /cacert-" + m.group(1) + ".txt HTTP/1.1", fst);
158             while ( !"".equals(br.readLine())) {
159             }
160             String res = m.group(2);
161             PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
162             if ( !doRedirect) {
163                 out.println("HTTP/1.1 200 OK");
164                 out.println("Content-length: " + res.length());
165                 out.println();
166                 out.print(res);
167             } else {
168                 out.println("HTTP/1.1 302 Moved");
169                 out.println("Location: /token");
170                 out.println();
171             }
172             out.flush();
173         }
174         waitForPings(2);
175
176         TestMail mail = getMailReceiver().receive();
177         mail.verify();
178
179         String newcontent = IOUtils.readURL(get(p2));
180         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
181         pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
182         Matcher m0 = pat.matcher(newcontent);
183         assertTrue(newcontent, m0.find());
184         if (doRedirect) {
185             assertEquals("failed", m0.group(1));
186             assertThat(m0.group(3), containsString("code 302"));
187         } else {
188             assertEquals("success", m0.group(1));
189         }
190         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
191         assertTrue(newcontent, pat.matcher(newcontent).find());
192
193     }
194 }