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.IOException;
8 import java.io.InputStreamReader;
10 import java.net.URLEncoder;
11 import java.sql.SQLException;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 import javax.naming.NamingException;
17 import org.cacert.gigi.GigiApiException;
18 import org.cacert.gigi.dbObjects.Domain;
19 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
20 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
21 import org.cacert.gigi.pages.account.domain.DomainOverview;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.PingTest;
24 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
25 import org.cacert.gigi.util.RandomToken;
26 import org.junit.Test;
28 public class TestHTTP extends PingTest {
31 public void httpSanity() throws IOException, NamingException {
33 String token = RandomToken.generateToken(16);
34 String value = RandomToken.generateToken(16);
36 TestDNS.updateService(token, value, "http");
37 assertEquals(value, readHTTP(token));
42 public void httpAndMailSuccess() throws Exception {
43 testEmailAndHTTP(0, 0, true, true);
47 public void httpFailKeyAndMailSuccess() throws Exception {
48 testEmailAndHTTP(1, 0, false, true);
52 public void httpFailValAndMailFail() throws Exception {
53 testEmailAndHTTP(2, 1, false, false);
56 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
58 String test = getTestProps().getProperty("domain.http");
61 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
62 Matcher m = initailizeDomainForm(u);
63 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
65 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
66 "&emailType=y&email=2&HTTPType=y" + //
67 "&ssl-type-0=direct&ssl-port-0=" + //
68 "&ssl-type-1=direct&ssl-port-1=" + //
69 "&ssl-type-2=direct&ssl-port-2=" + //
70 "&ssl-type-3=direct&ssl-port-3=" + //
71 "&adddomain&csrf=" + csrf;
72 URL u2 = sendDomainForm(u, content);
74 TestMail mail = getMailReciever().receive();
75 if (emailVariant == 0) {
80 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
81 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
82 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
83 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
84 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
86 if (successHTTP) { // give it a second try
87 int id = Integer.parseInt(u2.toString().replaceFirst("^.*/([0-9]+)$", "$1"));
88 Domain d = Domain.getById(id);
89 DomainPingConfiguration dpc = null;
90 for (DomainPingConfiguration conf : d.getConfiguredPings()) {
91 if (conf.getType() == PingType.HTTP) {
97 fail("Http config not found");
99 String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
100 assertThat(res, containsString("only allowed after"));
104 private String readHTTP(String token) throws IOException {
105 String httpDom = getTestProps().getProperty("domain.http");
106 assumeNotNull(httpDom);
107 URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
108 return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();