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.DomainPingType;
21 import org.cacert.gigi.testUtils.IOUtils;
22 import org.cacert.gigi.testUtils.PingTest;
23 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
24 import org.cacert.gigi.util.RandomToken;
25 import org.junit.Test;
27 public class TestHTTP extends PingTest {
30 public void httpSanity() throws IOException, NamingException {
32 String token = RandomToken.generateToken(16);
33 String value = RandomToken.generateToken(16);
35 TestDNS.updateService(token, value, "http");
36 assertEquals(value, readHTTP(token));
41 public void httpAndMailSuccess() throws Exception {
42 testEmailAndHTTP(0, 0, true, true);
46 public void httpFailKeyAndMailSuccess() throws Exception {
47 testEmailAndHTTP(1, 0, false, true);
51 public void httpFailValAndMailFail() throws Exception {
52 testEmailAndHTTP(2, 1, false, false);
55 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
57 String test = getTestProps().getProperty("domain.http");
60 Matcher m = initailizeDomainForm();
61 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
63 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
64 "&emailType=y&email=2&HTTPType=y" + //
65 "&ssl-type-0=direct&ssl-port-0=" + //
66 "&ssl-type-1=direct&ssl-port-1=" + //
67 "&ssl-type-2=direct&ssl-port-2=" + //
68 "&ssl-type-3=direct&ssl-port-3=" + //
69 "&adddomain&csrf=" + csrf;
70 String p2 = sendDomainForm(content);
72 TestMail mail = getMailReciever().receive();
73 if (emailVariant == 0) {
78 String newcontent = IOUtils.readURL(get(p2));
79 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
80 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
81 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
82 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
84 if (successHTTP) { // give it a second try
85 int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
86 Domain d = Domain.getById(id);
87 DomainPingConfiguration dpc = null;
88 for (DomainPingConfiguration conf : d.getConfiguredPings()) {
89 if (conf.getType() == DomainPingType.HTTP) {
95 fail("Http config not found");
97 String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
98 assertThat(res, containsString("only allowed after"));
102 private String readHTTP(String token) throws IOException {
103 String httpDom = getTestProps().getProperty("domain.http");
104 assumeNotNull(httpDom);
105 URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
106 return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();