]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
Merge "Suggestions to enhance the SQL call pattern."
[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.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;
26
27 public class TestHTTP extends PingTest {
28
29     @Test
30     public void httpSanity() throws IOException, NamingException {
31
32         String token = RandomToken.generateToken(16);
33         String value = RandomToken.generateToken(16);
34
35         TestDNS.updateService(token, value, "http");
36         assertEquals(value, readHTTP(token));
37
38     }
39
40     @Test
41     public void httpAndMailSuccess() throws Exception {
42         testEmailAndHTTP(0, 0, true, true);
43     }
44
45     @Test
46     public void httpFailKeyAndMailSuccess() throws Exception {
47         testEmailAndHTTP(1, 0, false, true);
48     }
49
50     @Test
51     public void httpFailValAndMailFail() throws Exception {
52         testEmailAndHTTP(2, 1, false, false);
53     }
54
55     public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
56
57         String test = getTestProps().getProperty("domain.http");
58         assumeNotNull(test);
59
60         Matcher m = initailizeDomainForm();
61         updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
62
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);
71
72         TestMail mail = getMailReciever().receive();
73         if (emailVariant == 0) {
74             mail.verify();
75         }
76         waitForPings(2);
77
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());
83
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) {
90                     dpc = conf;
91                     break;
92                 }
93             }
94             if (dpc == null) {
95                 fail("Http config not found");
96             }
97             String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
98             assertThat(res, containsString("only allowed after"));
99         }
100     }
101
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();
107
108     }
109 }