1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
8 import java.net.URLEncoder;
9 import java.sql.SQLException;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
13 import javax.naming.NamingException;
15 import org.cacert.gigi.pages.account.domain.DomainOverview;
16 import org.cacert.gigi.testUtils.IOUtils;
17 import org.cacert.gigi.testUtils.PingTest;
18 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
19 import org.cacert.gigi.util.RandomToken;
20 import org.junit.Test;
22 public class TestHTTP extends PingTest {
25 public void httpSanity() throws IOException, NamingException {
27 String token = RandomToken.generateToken(16);
28 String value = RandomToken.generateToken(16);
30 TestDNS.updateService(token, value, "http");
31 assertEquals(value, readHTTP(token));
36 public void httpAndMailSuccess() throws IOException, InterruptedException, SQLException {
37 testEmailAndHTTP(0, 0, true, true);
41 public void httpFailKeyAndMailSuccess() throws IOException, InterruptedException, SQLException {
42 testEmailAndHTTP(1, 0, false, true);
46 public void httpFailValAndMailFail() throws IOException, InterruptedException, SQLException {
47 testEmailAndHTTP(2, 1, false, false);
50 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException {
52 String test = getTestProps().getProperty("domain.http");
54 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
55 Matcher m = initailizeDomainForm(u);
56 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
58 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
59 "&emailType=y&email=2&HTTPType=y" + //
60 "&ssl-type-0=direct&ssl-port-0=" + //
61 "&ssl-type-1=direct&ssl-port-1=" + //
62 "&ssl-type-2=direct&ssl-port-2=" + //
63 "&ssl-type-3=direct&ssl-port-3=" + //
64 "&adddomain&csrf=" + csrf;
65 URL u2 = sendDomainForm(u, content);
67 TestMail mail = getMailReciever().recieve();
68 if (emailVariant == 0) {
69 String link = mail.extractLink();
70 new URL(link).openConnection().getHeaderField("");
74 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
75 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
76 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
77 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
78 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
81 private String readHTTP(String token) throws IOException {
82 URL u = new URL("http://" + getTestProps().getProperty("domain.http") + "/cacert-" + token + ".txt");
83 return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();