1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
9 import java.net.URLConnection;
10 import java.net.URLEncoder;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import javax.naming.NamingException;
19 import org.cacert.gigi.database.DatabaseConnection;
20 import org.cacert.gigi.pages.account.DomainOverview;
21 import org.cacert.gigi.testUtils.IOUtils;
22 import org.cacert.gigi.testUtils.ManagedTest;
23 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
24 import org.cacert.gigi.util.DNSUtil;
25 import org.cacert.gigi.util.RandomToken;
26 import org.junit.After;
27 import org.junit.Test;
29 public class TestDNS extends ManagedTest {
32 public void dnsSanity() throws IOException, NamingException {
34 String token = RandomToken.generateToken(16);
35 String value = RandomToken.generateToken(16);
37 String reRead = updateDNS(token, value);
38 assertEquals(value, reRead);
43 public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
44 testEmailAndDNS(0, 0, true, true);
48 public void purgeDbAfterTest() throws SQLException, IOException {
53 public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
54 testEmailAndDNS(1, 0, false, true);
58 public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
59 testEmailAndDNS(2, 0, false, true);
63 public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
64 testEmailAndDNS(0, 1, true, false);
68 public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
69 testEmailAndDNS(2, 1, false, false);
72 public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
73 String email = createUniqueName() + "@example.org";
74 createVerifiedUser("a", "b", email, TEST_PASSWORD);
75 String cookie = login(email, TEST_PASSWORD);
77 String test = getTestProps().getProperty("domain.dnstest");
78 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
79 URLConnection openConnection = u.openConnection();
80 openConnection.setRequestProperty("Cookie", cookie);
81 String content1 = IOUtils.readURL(openConnection);
82 String csrf = getCSRF(1, content1);
84 Pattern p = Pattern.compile("cacert-([A-Za-z0-9]+) IN TXT ([A-Za-z0-9]+)");
85 Matcher m = p.matcher(content1);
87 updateDNS(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""));
89 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
90 "&emailType=y&email=2&DNSType=y" + //
91 "&ssl-type-0=direct&ssl-port-0=" + //
92 "&ssl-type-1=direct&ssl-port-1=" + //
93 "&ssl-type-2=direct&ssl-port-2=" + //
94 "&ssl-type-3=direct&ssl-port-3=" + //
95 "&adddomain&csrf=" + csrf;
96 openConnection = u.openConnection();
97 openConnection.setRequestProperty("Cookie", cookie);
98 openConnection.setDoOutput(true);
99 openConnection.getOutputStream().write(content.getBytes());
100 openConnection.getHeaderField("Location");
102 String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
103 Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
104 Matcher m1 = dlink.matcher(newcontent);
106 URL u2 = new URL(u.toString() + m1.group(1));
108 TestMail mail = getMailReciever().recieve();
109 if (emailVariant == 0) {
110 String link = mail.extractLink();
111 new URL(link).openConnection().getHeaderField("");
114 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
116 ResultSet rs = ps.executeQuery();
118 if (rs.getInt(1) >= 2) {
124 newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
125 Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
126 assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
127 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
128 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
131 private String updateDNS(String token, String value) throws IOException, MalformedURLException, NamingException {
132 String test = getTestProps().getProperty("domain.dnstest");
133 String targetDomain = "cacert-" + token + "." + test;
134 String manage = getTestProps().getProperty("domain.dnsmanage");
135 String url = manage + "t1=" + token + "&t2=" + value;
136 assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
137 String[] data = DNSUtil.getTXTEntries(targetDomain, getTestProps().getProperty("domain.testns"));
138 assertEquals(1, data.length);