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.TestEmailReciever.TestMail;
25 import org.cacert.gigi.util.RandomToken;
26 import org.junit.Assert;
27 import org.junit.Test;
29 public class TestHTTP extends PingTest {
32 public void httpSanity() throws IOException, NamingException {
34 String token = RandomToken.generateToken(16);
35 String value = RandomToken.generateToken(16);
37 TestDNS.updateService(token, value, "http");
38 assertEquals(value, readHTTP(token));
43 public void httpAndMailSuccess() throws Exception {
44 testEmailAndHTTP(0, 0, true, true);
48 public void httpFailKeyAndMailSuccess() throws Exception {
49 testEmailAndHTTP(1, 0, false, true);
53 public void httpFailValAndMailFail() throws Exception {
54 testEmailAndHTTP(2, 1, false, false);
57 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
59 String test = getTestProps().getProperty("domain.http");
62 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
63 Matcher m = initailizeDomainForm(u);
64 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
66 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
67 "&emailType=y&email=2&HTTPType=y" + //
68 "&ssl-type-0=direct&ssl-port-0=" + //
69 "&ssl-type-1=direct&ssl-port-1=" + //
70 "&ssl-type-2=direct&ssl-port-2=" + //
71 "&ssl-type-3=direct&ssl-port-3=" + //
72 "&adddomain&csrf=" + csrf;
73 URL u2 = sendDomainForm(u, content);
75 TestMail mail = getMailReciever().recieve();
76 if (emailVariant == 0) {
77 Assert.assertNotNull(mail);
82 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
83 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
84 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
85 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
86 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
88 if (successHTTP) { // give it a second try
89 int id = Integer.parseInt(u2.toString().replaceFirst("^.*/([0-9]+)$", "$1"));
90 Domain d = Domain.getById(id);
91 DomainPingConfiguration dpc = null;
92 for (DomainPingConfiguration conf : d.getConfiguredPings()) {
93 if (conf.getType() == PingType.HTTP) {
99 fail("Http config not found");
101 String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
102 assertThat(res, containsString("only allowed after"));
106 private String readHTTP(String token) throws IOException {
107 String httpDom = getTestProps().getProperty("domain.http");
108 assumeNotNull(httpDom);
109 URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
110 return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();