]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
Add: simple test for reping rate limiting.
[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             String link = mail.extractLink();
77             new URL(link).openConnection().getHeaderField("");
78         }
79         waitForPings(2);
80
81         String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
82         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
83         assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
84         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
85         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
86
87         if (successHTTP) { // give it a second try
88             int id = Integer.parseInt(u2.toString().replaceFirst("^.*/([0-9]+)$", "$1"));
89             Domain d = Domain.getById(id);
90             DomainPingConfiguration dpc = null;
91             for (DomainPingConfiguration conf : d.getConfiguredPings()) {
92                 if (conf.getType() == PingType.HTTP) {
93                     dpc = conf;
94                     break;
95                 }
96             }
97             if (dpc == null) {
98                 fail("Http config not found");
99             }
100             String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
101             assertThat(res, containsString("only allowed after"));
102         }
103     }
104
105     private String readHTTP(String token) throws IOException {
106         String httpDom = getTestProps().getProperty("domain.http");
107         assumeNotNull(httpDom);
108         URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
109         return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();
110
111     }
112 }