]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestDNS.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[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.URLEncoder;
8 import java.sql.SQLException;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12 import javax.naming.NamingException;
13
14 import org.cacert.gigi.testUtils.IOUtils;
15 import org.cacert.gigi.testUtils.PingTest;
16 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
17 import org.cacert.gigi.util.DNSUtil;
18 import org.cacert.gigi.util.RandomToken;
19 import org.cacert.gigi.util.SystemKeywords;
20 import org.junit.Test;
21
22 public class TestDNS extends PingTest {
23
24     @Test
25     public void dnsSanity() throws IOException, NamingException {
26
27         String token = RandomToken.generateToken(16);
28         String value = RandomToken.generateToken(16);
29
30         updateService(token, value, "dns");
31         assertEquals(value, readDNS(token));
32
33     }
34
35     @Test
36     public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
37         testEmailAndDNS(0, 0, true, true);
38     }
39
40     @Test
41     public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
42         testEmailAndDNS(1, 0, false, true);
43     }
44
45     @Test
46     public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
47         testEmailAndDNS(2, 0, false, true);
48     }
49
50     @Test
51     public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
52         testEmailAndDNS(0, 1, true, false);
53     }
54
55     @Test
56     public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
57         testEmailAndDNS(2, 1, false, false);
58     }
59
60     public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
61
62         String test = getTestProps().getProperty("domain.dnstest");
63         assumeNotNull(test);
64
65         Matcher m = initailizeDomainForm();
66         updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
67
68         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
69                 "&emailType=y&email=2&DNSType=y" + //
70                 "&ssl-type-0=direct&ssl-port-0=" + //
71                 "&ssl-type-1=direct&ssl-port-1=" + //
72                 "&ssl-type-2=direct&ssl-port-2=" + //
73                 "&ssl-type-3=direct&ssl-port-3=" + //
74                 "&adddomain&csrf=" + csrf;
75         String p2 = sendDomainForm(content);
76
77         TestMail mail = getMailReceiver().receive();
78         if (emailVariant == 0) {
79             mail.verify();
80         }
81
82         waitForPings(2);
83
84         String newcontent = IOUtils.readURL(get(p2));
85         Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
86         assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
87         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
88         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
89     }
90
91     private String readDNS(String token) throws NamingException {
92         String test = getTestProps().getProperty("domain.dnstest");
93         assumeNotNull(test);
94         String targetDomain = token + "." + SystemKeywords.DNS_PREFIX + "._auth." + test;
95         String testns = getTestProps().getProperty("domain.testns");
96         assumeNotNull(testns);
97         String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
98         assertEquals(1, data.length);
99         return data[0];
100
101     }
102 }