]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
6ee6f6e821cecc958eb2a338ea9503fd3437dd25
[gigi.git] / tests / org / cacert / gigi / ping / TestHTTP.java
1 package org.cacert.gigi.ping;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5 import static org.junit.Assume.*;
6
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9 import java.net.URL;
10 import java.net.URLEncoder;
11 import java.sql.SQLException;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import javax.naming.NamingException;
16
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.Test;
27
28 public class TestHTTP extends PingTest {
29
30     @Test
31     public void httpSanity() throws IOException, NamingException {
32
33         String token = RandomToken.generateToken(16);
34         String value = RandomToken.generateToken(16);
35
36         TestDNS.updateService(token, value, "http");
37         assertEquals(value, readHTTP(token));
38
39     }
40
41     @Test
42     public void httpAndMailSuccess() throws Exception {
43         testEmailAndHTTP(0, 0, true, true);
44     }
45
46     @Test
47     public void httpFailKeyAndMailSuccess() throws Exception {
48         testEmailAndHTTP(1, 0, false, true);
49     }
50
51     @Test
52     public void httpFailValAndMailFail() throws Exception {
53         testEmailAndHTTP(2, 1, false, false);
54     }
55
56     public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
57
58         String test = getTestProps().getProperty("domain.http");
59         assumeNotNull(test);
60
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");
64
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);
73
74         TestMail mail = getMailReciever().recieve();
75         if (emailVariant == 0) {
76             mail.verify();
77         }
78         waitForPings(2);
79
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());
85
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) {
92                     dpc = conf;
93                     break;
94                 }
95             }
96             if (dpc == null) {
97                 fail("Http config not found");
98             }
99             String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
100             assertThat(res, containsString("only allowed after"));
101         }
102     }
103
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();
109
110     }
111 }