1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
9 import java.net.URLEncoder;
10 import java.sql.SQLException;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
14 import javax.naming.NamingException;
16 import org.cacert.gigi.pages.account.domain.DomainOverview;
17 import org.cacert.gigi.testUtils.IOUtils;
18 import org.cacert.gigi.testUtils.PingTest;
19 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
20 import org.cacert.gigi.util.RandomToken;
21 import org.junit.Test;
23 public class TestHTTP extends PingTest {
26 public void httpSanity() throws IOException, NamingException {
28 String token = RandomToken.generateToken(16);
29 String value = RandomToken.generateToken(16);
31 TestDNS.updateService(token, value, "http");
32 assertEquals(value, readHTTP(token));
37 public void httpAndMailSuccess() throws IOException, InterruptedException, SQLException {
38 testEmailAndHTTP(0, 0, true, true);
42 public void httpFailKeyAndMailSuccess() throws IOException, InterruptedException, SQLException {
43 testEmailAndHTTP(1, 0, false, true);
47 public void httpFailValAndMailFail() throws IOException, InterruptedException, SQLException {
48 testEmailAndHTTP(2, 1, false, false);
51 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException {
53 String test = getTestProps().getProperty("domain.http");
56 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
57 Matcher m = initailizeDomainForm(u);
58 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
60 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
61 "&emailType=y&email=2&HTTPType=y" + //
62 "&ssl-type-0=direct&ssl-port-0=" + //
63 "&ssl-type-1=direct&ssl-port-1=" + //
64 "&ssl-type-2=direct&ssl-port-2=" + //
65 "&ssl-type-3=direct&ssl-port-3=" + //
66 "&adddomain&csrf=" + csrf;
67 URL u2 = sendDomainForm(u, content);
69 TestMail mail = getMailReciever().recieve();
70 if (emailVariant == 0) {
71 String link = mail.extractLink();
72 new URL(link).openConnection().getHeaderField("");
76 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
77 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
78 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
79 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
80 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
83 private String readHTTP(String token) throws IOException {
84 String httpDom = getTestProps().getProperty("domain.http");
85 assumeNotNull(httpDom);
86 URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
87 return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();