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.TestEmailReciever.TestMail;
19 import org.cacert.gigi.util.DNSUtil;
20 import org.cacert.gigi.util.RandomToken;
21 import org.junit.Assert;
22 import org.junit.Test;
24 public class TestDNS extends PingTest {
27 public void dnsSanity() throws IOException, NamingException {
29 String token = RandomToken.generateToken(16);
30 String value = RandomToken.generateToken(16);
32 updateService(token, value, "dns");
33 assertEquals(value, readDNS(token));
38 public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
39 testEmailAndDNS(0, 0, true, true);
43 public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
44 testEmailAndDNS(1, 0, false, true);
48 public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
49 testEmailAndDNS(2, 0, false, true);
53 public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
54 testEmailAndDNS(0, 1, true, false);
58 public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
59 testEmailAndDNS(2, 1, false, false);
62 public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
64 String test = getTestProps().getProperty("domain.dnstest");
67 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
68 Matcher m = initailizeDomainForm(u);
69 updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
71 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
72 "&emailType=y&email=2&DNSType=y" + //
73 "&ssl-type-0=direct&ssl-port-0=" + //
74 "&ssl-type-1=direct&ssl-port-1=" + //
75 "&ssl-type-2=direct&ssl-port-2=" + //
76 "&ssl-type-3=direct&ssl-port-3=" + //
77 "&adddomain&csrf=" + csrf;
78 URL u2 = sendDomainForm(u, content);
80 TestMail mail = getMailReciever().recieve();
81 if (emailVariant == 0) {
82 Assert.assertNotNull(mail);
88 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
89 Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
90 assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
91 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
92 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
95 private String readDNS(String token) throws NamingException {
96 String test = getTestProps().getProperty("domain.dnstest");
98 String targetDomain = token + "._cacert._auth." + test;
99 String testns = getTestProps().getProperty("domain.testns");
100 assumeNotNull(testns);
101 String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
102 assertEquals(1, data.length);