1 package org.cacert.gigi.ping;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5 import static org.junit.Assume.*;
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;
15 import java.net.URLEncoder;
16 import java.sql.SQLException;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import javax.naming.NamingException;
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;
32 public class TestHTTP extends PingTest {
35 public void httpSanity() throws IOException, NamingException {
37 String token = RandomToken.generateToken(16);
38 String value = RandomToken.generateToken(16);
40 TestDNS.updateService(token, value, "http");
41 assertEquals(value, readHTTP(token));
46 public void httpAndMailSuccess() throws Exception {
47 testEmailAndHTTP(0, 0, true, true);
51 public void httpFailKeyAndMailSuccess() throws Exception {
52 testEmailAndHTTP(1, 0, false, true);
56 public void httpFailValAndMailFail() throws Exception {
57 testEmailAndHTTP(2, 1, false, false);
60 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
62 String test = getTestProps().getProperty("domain.http");
65 Matcher m = initailizeDomainForm();
66 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
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);
77 TestMail mail = getMailReceiver().receive();
78 if (emailVariant == 0) {
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());
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) {
100 fail("Http config not found");
103 String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
104 assertThat(res, containsString("only allowed after"));
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();
117 public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
118 try (ServerSocket s = openServer()) {
119 testHttpRedirect(s, true);
124 public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
125 try (ServerSocket s = openServer()) {
126 testHttpRedirect(s, false);
130 private ServerSocket openServer() {
131 String localHTTP = getTestProps().getProperty("domain.localHTTP");
132 assumeNotNull(localHTTP);
134 return new ServerSocket(Integer.parseInt(localHTTP));
135 } catch (IOException e) {
136 throw new Error("Requires a free port " + localHTTP);
140 public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
141 String test = getTestProps().getProperty("domain.local");
144 Matcher m = initailizeDomainForm();
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 ( !br.readLine().equals("")) {
160 String res = m.group(2);
161 PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
163 out.println("HTTP/1.1 200 OK");
164 out.println("Content-length: " + res.length());
168 out.println("HTTP/1.1 302 Moved");
169 out.println("Location: /token");
176 TestMail mail = getMailReceiver().receive();
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());
185 assertEquals("failed", m0.group(1));
186 assertThat(m0.group(3), containsString("code 302"));
188 assertEquals("success", m0.group(1));
190 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
191 assertTrue(newcontent, pat.matcher(newcontent).find());