]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestDNS.java
UPD: Assume ping-env tests
[gigi.git] / tests / org / cacert / gigi / ping / TestDNS.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.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.DNSUtil;
20 import org.cacert.gigi.util.RandomToken;
21 import org.junit.Test;
22
23 public class TestDNS extends PingTest {
24
25     @Test
26     public void dnsSanity() throws IOException, NamingException {
27
28         String token = RandomToken.generateToken(16);
29         String value = RandomToken.generateToken(16);
30
31         updateService(token, value, "dns");
32         assertEquals(value, readDNS(token));
33
34     }
35
36     @Test
37     public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
38         testEmailAndDNS(0, 0, true, true);
39     }
40
41     @Test
42     public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
43         testEmailAndDNS(1, 0, false, true);
44     }
45
46     @Test
47     public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
48         testEmailAndDNS(2, 0, false, true);
49     }
50
51     @Test
52     public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
53         testEmailAndDNS(0, 1, true, false);
54     }
55
56     @Test
57     public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
58         testEmailAndDNS(2, 1, false, false);
59     }
60
61     public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
62
63         String test = getTestProps().getProperty("domain.dnstest");
64         assumeNotNull(test);
65
66         URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
67         Matcher m = initailizeDomainForm(u);
68         updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
69
70         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
71                 "&emailType=y&email=2&DNSType=y" + //
72                 "&ssl-type-0=direct&ssl-port-0=" + //
73                 "&ssl-type-1=direct&ssl-port-1=" + //
74                 "&ssl-type-2=direct&ssl-port-2=" + //
75                 "&ssl-type-3=direct&ssl-port-3=" + //
76                 "&adddomain&csrf=" + csrf;
77         URL u2 = sendDomainForm(u, content);
78
79         TestMail mail = getMailReciever().recieve();
80         if (emailVariant == 0) {
81             String link = mail.extractLink();
82             new URL(link).openConnection().getHeaderField("");
83         }
84
85         waitForPings(2);
86
87         String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
88         Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
89         assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
90         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
91         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
92     }
93
94     private String readDNS(String token) throws NamingException {
95         String test = getTestProps().getProperty("domain.dnstest");
96         assumeNotNull(test);
97         String targetDomain = token + "._cacert._auth." + test;
98         String testns = getTestProps().getProperty("domain.testns");
99         assumeNotNull(testns);
100         String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
101         assertEquals(1, data.length);
102         return data[0];
103
104     }
105 }