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