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