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