1 package club.wpia.gigi.ping;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
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;
12 import javax.naming.NamingException;
14 import org.junit.Test;
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;
23 public class TestDNS extends PingTest {
26 public void dnsSanity() throws IOException, NamingException {
28 String token = RandomToken.generateToken(16);
29 String value = RandomToken.generateToken(16);
31 updateService(token, value, "dns");
32 assertEquals(value, readDNS(token));
37 public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
38 testEmailAndDNS(0, 0, true, true);
42 public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
43 testEmailAndDNS(1, 0, false, true);
47 public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
48 testEmailAndDNS(2, 0, false, true);
52 public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
53 testEmailAndDNS(0, 1, true, false);
57 public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
58 testEmailAndDNS(2, 1, false, false);
61 public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
63 String test = getTestProps().getProperty("domain.dnstest");
66 Matcher m = initailizeDomainForm();
67 updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
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);
78 TestMail mail = getMailReceiver().receive();
79 if (emailVariant == 0) {
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());
92 private String readDNS(String token) throws NamingException {
93 String test = getTestProps().getProperty("domain.dnstest");
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);