]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/ping/TestDNS.java
upd: enforce that test cases receive all mails explicitly
[gigi.git] / tests / club / wpia / gigi / ping / TestDNS.java
1 package club.wpia.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.junit.Test;
15
16 import club.wpia.gigi.testUtils.IOUtils;
17 import club.wpia.gigi.testUtils.PingTest;
18 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
19 import club.wpia.gigi.util.DNSUtil;
20 import club.wpia.gigi.util.RandomToken;
21 import club.wpia.gigi.util.SystemKeywords;
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         Matcher m = initailizeDomainForm();
67         updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
68
69         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
70                 "&emailType=y&email=2&DNSType=y" + //
71                 "&ssl-type-0=direct&ssl-port-0=" + //
72                 "&ssl-type-1=direct&ssl-port-1=" + //
73                 "&ssl-type-2=direct&ssl-port-2=" + //
74                 "&ssl-type-3=direct&ssl-port-3=" + //
75                 "&adddomain&csrf=" + csrf;
76         String p2 = sendDomainForm(content);
77
78         TestMail mail = getMailReceiver().receive("postmaster@" + test);
79         if (emailVariant == 0) {
80             mail.verify();
81         }
82
83         waitForPings(2);
84
85         String newcontent = IOUtils.readURL(get(p2));
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         assumeNotNull(test);
95         String targetDomain = token + "." + SystemKeywords.DNS_PREFIX + "._auth." + test;
96         String testns = getTestProps().getProperty("domain.testns");
97         assumeNotNull(testns);
98         String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
99         assertEquals(1, data.length);
100         return data[0];
101
102     }
103 }