]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestDNS.java
Move email/certs/mail to their own packages
[gigi.git] / tests / org / cacert / gigi / ping / TestDNS.java
1 package org.cacert.gigi.ping;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.URL;
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.pages.account.domain.DomainOverview;
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.cacert.gigi.testUtils.PingTest;
17 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
18 import org.cacert.gigi.util.DNSUtil;
19 import org.cacert.gigi.util.RandomToken;
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
64         URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
65         Matcher m = initailizeDomainForm(u);
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         URL u2 = sendDomainForm(u, content);
76
77         TestMail mail = getMailReciever().recieve();
78         if (emailVariant == 0) {
79             String link = mail.extractLink();
80             new URL(link).openConnection().getHeaderField("");
81         }
82
83         waitForPings(2);
84
85         String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
86         Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
87         assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
88         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
89         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
90     }
91
92     private String readDNS(String token) throws NamingException {
93         String test = getTestProps().getProperty("domain.dnstest");
94         String targetDomain = token + "._cacert._auth." + test;
95         String[] data = DNSUtil.getTXTEntries(targetDomain, getTestProps().getProperty("domain.testns"));
96         assertEquals(1, data.length);
97         return data[0];
98
99     }
100 }