1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
5 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.pages.account.DomainOverview;
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.cacert.gigi.testUtils.PingTest;
17 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
18 import org.cacert.gigi.util.DNSUtil;
19 import org.cacert.gigi.util.RandomToken;
20 import org.junit.Test;
22 public class TestDNS extends PingTest {
25 public void dnsSanity() throws IOException, NamingException {
27 String token = RandomToken.generateToken(16);
28 String value = RandomToken.generateToken(16);
30 updateService(token, value, "dns");
31 assertEquals(value, readDNS(token));
36 public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
37 testEmailAndDNS(0, 0, true, true);
41 public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
42 testEmailAndDNS(1, 0, false, true);
46 public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
47 testEmailAndDNS(2, 0, false, true);
51 public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
52 testEmailAndDNS(0, 1, true, false);
56 public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
57 testEmailAndDNS(2, 1, false, false);
60 public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
62 String test = getTestProps().getProperty("domain.dnstest");
64 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
65 Matcher m = initailizeDomainForm(u);
66 updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
68 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
69 "&emailType=y&email=2&DNSType=y" + //
70 "&ssl-type-0=direct&ssl-port-0=" + //
71 "&ssl-type-1=direct&ssl-port-1=" + //
72 "&ssl-type-2=direct&ssl-port-2=" + //
73 "&ssl-type-3=direct&ssl-port-3=" + //
74 "&adddomain&csrf=" + csrf;
75 URL u2 = sendDomainForm(u, content);
77 TestMail mail = getMailReciever().recieve();
78 if (emailVariant == 0) {
79 String link = mail.extractLink();
80 new URL(link).openConnection().getHeaderField("");
85 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
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");
94 String targetDomain = token + "._cacert._auth." + test;
95 String[] data = DNSUtil.getTXTEntries(targetDomain, getTestProps().getProperty("domain.testns"));
96 assertEquals(1, data.length);