]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestHTTP.java
UPD: Assume ping-env tests
[gigi.git] / tests / org / cacert / gigi / ping / TestHTTP.java
1 package org.cacert.gigi.ping;
2
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
5
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.URL;
9 import java.net.URLEncoder;
10 import java.sql.SQLException;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import javax.naming.NamingException;
15
16 import org.cacert.gigi.pages.account.domain.DomainOverview;
17 import org.cacert.gigi.testUtils.IOUtils;
18 import org.cacert.gigi.testUtils.PingTest;
19 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
20 import org.cacert.gigi.util.RandomToken;
21 import org.junit.Test;
22
23 public class TestHTTP extends PingTest {
24
25     @Test
26     public void httpSanity() throws IOException, NamingException {
27
28         String token = RandomToken.generateToken(16);
29         String value = RandomToken.generateToken(16);
30
31         TestDNS.updateService(token, value, "http");
32         assertEquals(value, readHTTP(token));
33
34     }
35
36     @Test
37     public void httpAndMailSuccess() throws IOException, InterruptedException, SQLException {
38         testEmailAndHTTP(0, 0, true, true);
39     }
40
41     @Test
42     public void httpFailKeyAndMailSuccess() throws IOException, InterruptedException, SQLException {
43         testEmailAndHTTP(1, 0, false, true);
44     }
45
46     @Test
47     public void httpFailValAndMailFail() throws IOException, InterruptedException, SQLException {
48         testEmailAndHTTP(2, 1, false, false);
49     }
50
51     public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException {
52
53         String test = getTestProps().getProperty("domain.http");
54         assumeNotNull(test);
55
56         URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
57         Matcher m = initailizeDomainForm(u);
58         updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
59
60         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
61                 "&emailType=y&email=2&HTTPType=y" + //
62                 "&ssl-type-0=direct&ssl-port-0=" + //
63                 "&ssl-type-1=direct&ssl-port-1=" + //
64                 "&ssl-type-2=direct&ssl-port-2=" + //
65                 "&ssl-type-3=direct&ssl-port-3=" + //
66                 "&adddomain&csrf=" + csrf;
67         URL u2 = sendDomainForm(u, content);
68
69         TestMail mail = getMailReciever().recieve();
70         if (emailVariant == 0) {
71             String link = mail.extractLink();
72             new URL(link).openConnection().getHeaderField("");
73         }
74         waitForPings(2);
75
76         String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
77         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
78         assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
79         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
80         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
81     }
82
83     private String readHTTP(String token) throws IOException {
84         String httpDom = getTestProps().getProperty("domain.http");
85         assumeNotNull(httpDom);
86         URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
87         return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();
88
89     }
90 }