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.cacert.gigi.util.SystemKeywords;
31 import org.junit.Test;
33 public class TestHTTP extends PingTest {
36 public void httpSanity() throws IOException, NamingException {
38 String token = RandomToken.generateToken(16);
39 String value = RandomToken.generateToken(16);
41 TestDNS.updateService(token, value, "http");
42 assertEquals(value, readHTTP(token));
47 public void httpAndMailSuccess() throws Exception {
48 testEmailAndHTTP(0, 0, true, true);
52 public void httpFailKeyAndMailSuccess() throws Exception {
53 testEmailAndHTTP(1, 0, false, true);
57 public void httpFailValAndMailFail() throws Exception {
58 testEmailAndHTTP(2, 1, false, false);
61 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
63 String test = getTestProps().getProperty("domain.http");
66 Matcher m = initailizeDomainForm();
67 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
69 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
70 "&emailType=y&email=2&HTTPType=y" + //
71 "&ssl-type-0=direct&ssl-port-0=" + //
72 "&ssl-type-1=direct&ssl-port-1=" + //
73 "&ssl-type-2=direct&ssl-port-2=" + //
74 "&ssl-type-3=direct&ssl-port-3=" + //
75 "&adddomain&csrf=" + csrf;
76 String p2 = sendDomainForm(content);
78 TestMail mail = getMailReceiver().receive();
79 if (emailVariant == 0) {
84 String newcontent = IOUtils.readURL(get(p2));
85 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
86 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
87 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
88 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
90 if (successHTTP) { // give it a second try
91 int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
92 Domain d = Domain.getById(id);
93 DomainPingConfiguration dpc = null;
94 for (DomainPingConfiguration conf : d.getConfiguredPings()) {
95 if (conf.getType() == DomainPingType.HTTP) {
101 fail("Http config not found");
104 String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
105 assertThat(res, containsString("only allowed after"));
109 private String readHTTP(String token) throws IOException {
110 String httpDom = getTestProps().getProperty("domain.http");
111 assumeNotNull(httpDom);
112 URL u = new URL("http://" + httpDom + "/" + SystemKeywords.HTTP_CHALLENGE_PREFIX + token + ".txt");
113 return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
118 public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
119 try (ServerSocket s = openServer()) {
120 testHttpRedirect(s, true);
125 public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
126 try (ServerSocket s = openServer()) {
127 testHttpRedirect(s, false);
131 private ServerSocket openServer() {
132 String localHTTP = getTestProps().getProperty("domain.localHTTP");
133 assumeNotNull(localHTTP);
135 return new ServerSocket(Integer.parseInt(localHTTP));
136 } catch (IOException e) {
137 throw new Error("Requires a free port " + localHTTP);
141 public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
142 String test = getTestProps().getProperty("domain.local");
145 Matcher m = initailizeDomainForm();
147 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
148 "&emailType=y&email=2&HTTPType=y" + //
149 "&ssl-type-0=direct&ssl-port-0=" + //
150 "&ssl-type-1=direct&ssl-port-1=" + //
151 "&ssl-type-2=direct&ssl-port-2=" + //
152 "&ssl-type-3=direct&ssl-port-3=" + //
153 "&adddomain&csrf=" + csrf;
154 String p2 = sendDomainForm(content);
155 try (Socket s0 = s.accept()) {
156 BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
157 String fst = br.readLine();
158 assertEquals("GET /" + SystemKeywords.HTTP_CHALLENGE_PREFIX + m.group(1) + ".txt HTTP/1.1", fst);
159 while ( !"".equals(br.readLine())) {
161 String res = m.group(2);
162 PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
164 out.println("HTTP/1.1 200 OK");
165 out.println("Content-length: " + res.length());
169 out.println("HTTP/1.1 302 Moved");
170 out.println("Location: /token");
177 TestMail mail = getMailReceiver().receive();
180 String newcontent = IOUtils.readURL(get(p2));
181 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
182 pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
183 Matcher m0 = pat.matcher(newcontent);
184 assertTrue(newcontent, m0.find());
186 assertEquals("failed", m0.group(1));
187 assertThat(m0.group(3), containsString("code 302"));
189 assertEquals("success", m0.group(1));
191 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
192 assertTrue(newcontent, pat.matcher(newcontent).find());