1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.IOException;
8 import java.net.URLEncoder;
9 import java.sql.SQLException;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
13 import javax.naming.NamingException;
15 import org.cacert.gigi.pages.account.domain.DomainOverview;
16 import org.cacert.gigi.testUtils.IOUtils;
17 import org.cacert.gigi.testUtils.PingTest;
18 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
19 import org.cacert.gigi.util.DNSUtil;
20 import org.cacert.gigi.util.RandomToken;
21 import org.junit.Test;
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 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
67 Matcher m = initailizeDomainForm(u);
68 updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
70 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
71 "&emailType=y&email=2&DNSType=y" + //
72 "&ssl-type-0=direct&ssl-port-0=" + //
73 "&ssl-type-1=direct&ssl-port-1=" + //
74 "&ssl-type-2=direct&ssl-port-2=" + //
75 "&ssl-type-3=direct&ssl-port-3=" + //
76 "&adddomain&csrf=" + csrf;
77 URL u2 = sendDomainForm(u, content);
79 TestMail mail = getMailReciever().receive();
80 if (emailVariant == 0) {
86 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
87 Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
88 assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
89 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
90 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
93 private String readDNS(String token) throws NamingException {
94 String test = getTestProps().getProperty("domain.dnstest");
96 String targetDomain = token + "._cacert._auth." + test;
97 String testns = getTestProps().getProperty("domain.testns");
98 assumeNotNull(testns);
99 String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
100 assertEquals(1, data.length);