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");
102 String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
103 assertThat(res, containsString("only allowed after"));
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();
116 public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
117 try (ServerSocket s = openServer()) {
118 testHttpRedirect(s, true);
123 public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
124 try (ServerSocket s = openServer()) {
125 testHttpRedirect(s, false);
129 private ServerSocket openServer() {
130 String localHTTP = getTestProps().getProperty("domain.localHTTP");
131 assumeNotNull(localHTTP);
133 return new ServerSocket(Integer.parseInt(localHTTP));
134 } catch (IOException e) {
135 throw new Error("Requires a free port " + localHTTP);
139 public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
140 String test = getTestProps().getProperty("domain.local");
143 Matcher m = initailizeDomainForm();
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("")) {
159 String res = m.group(2);
160 PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
162 out.println("HTTP/1.1 200 OK");
163 out.println("Content-length: " + res.length());
167 out.println("HTTP/1.1 302 Moved");
168 out.println("Location: /token");
175 TestMail mail = getMailReceiver().receive();
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());
184 assertEquals("failed", m0.group(1));
185 assertThat(m0.group(3), containsString("code 302"));
187 assertEquals("success", m0.group(1));
189 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
190 assertTrue(newcontent, pat.matcher(newcontent).find());