]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
Fix: some nullpointer references (coverity).
[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.Assert;
27 import org.junit.Test;
28
29 public class TestHTTP extends PingTest {
30
31     @Test
32     public void httpSanity() throws IOException, NamingException {
33
34         String token = RandomToken.generateToken(16);
35         String value = RandomToken.generateToken(16);
36
37         TestDNS.updateService(token, value, "http");
38         assertEquals(value, readHTTP(token));
39
40     }
41
42     @Test
43     public void httpAndMailSuccess() throws Exception {
44         testEmailAndHTTP(0, 0, true, true);
45     }
46
47     @Test
48     public void httpFailKeyAndMailSuccess() throws Exception {
49         testEmailAndHTTP(1, 0, false, true);
50     }
51
52     @Test
53     public void httpFailValAndMailFail() throws Exception {
54         testEmailAndHTTP(2, 1, false, false);
55     }
56
57     public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
58
59         String test = getTestProps().getProperty("domain.http");
60         assumeNotNull(test);
61
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");
65
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);
74
75         TestMail mail = getMailReciever().recieve();
76         if (emailVariant == 0) {
77             Assert.assertNotNull(mail);
78             mail.verify();
79         }
80         waitForPings(2);
81
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());
87
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) {
94                     dpc = conf;
95                     break;
96                 }
97             }
98             if (dpc == null) {
99                 fail("Http config not found");
100             }
101             String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
102             assertThat(res, containsString("only allowed after"));
103         }
104     }
105
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();
111
112     }
113 }