1 package org.cacert.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.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;
21 public class TestDNS extends PingTest {
24 public void dnsSanity() throws IOException, NamingException {
26 String token = RandomToken.generateToken(16);
27 String value = RandomToken.generateToken(16);
29 updateService(token, value, "dns");
30 assertEquals(value, readDNS(token));
35 public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
36 testEmailAndDNS(0, 0, true, true);
40 public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
41 testEmailAndDNS(1, 0, false, true);
45 public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
46 testEmailAndDNS(2, 0, false, true);
50 public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
51 testEmailAndDNS(0, 1, true, false);
55 public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
56 testEmailAndDNS(2, 1, false, false);
59 public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
61 String test = getTestProps().getProperty("domain.dnstest");
64 Matcher m = initailizeDomainForm();
65 updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
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);
76 TestMail mail = getMailReciever().receive();
77 if (emailVariant == 0) {
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());
90 private String readDNS(String token) throws NamingException {
91 String test = getTestProps().getProperty("domain.dnstest");
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);