]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/ping/TestDNS.java
More DNS-ping and email ping failtests.
[gigi.git] / tests / org / cacert / gigi / ping / TestDNS.java
1 package org.cacert.gigi.ping;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
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;
16
17 import javax.naming.NamingException;
18
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;
28
29 public class TestDNS extends ManagedTest {
30
31     @Test
32     public void dnsSanity() throws IOException, NamingException {
33
34         String token = RandomToken.generateToken(16);
35         String value = RandomToken.generateToken(16);
36
37         String reRead = updateDNS(token, value);
38         assertEquals(value, reRead);
39
40     }
41
42     @Test
43     public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
44         testEmailAndDNS(0, 0, true, true);
45     }
46
47     @After
48     public void purgeDbAfterTest() throws SQLException, IOException {
49         purgeDatabase();
50     }
51
52     @Test
53     public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
54         testEmailAndDNS(1, 0, false, true);
55     }
56
57     @Test
58     public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
59         testEmailAndDNS(2, 0, false, true);
60     }
61
62     @Test
63     public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
64         testEmailAndDNS(0, 1, true, false);
65     }
66
67     @Test
68     public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
69         testEmailAndDNS(2, 1, false, false);
70     }
71
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);
76
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);
83
84         Pattern p = Pattern.compile("cacert-([A-Za-z0-9]+) IN TXT ([A-Za-z0-9]+)");
85         Matcher m = p.matcher(content1);
86         m.find();
87         updateDNS(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""));
88
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");
101
102         String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
103         Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
104         Matcher m1 = dlink.matcher(newcontent);
105         m1.find();
106         URL u2 = new URL(u.toString() + m1.group(1));
107
108         TestMail mail = getMailReciever().recieve();
109         if (emailVariant == 0) {
110             String link = mail.extractLink();
111             new URL(link).openConnection().getHeaderField("");
112         }
113
114         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
115         while (true) {
116             ResultSet rs = ps.executeQuery();
117             rs.next();
118             if (rs.getInt(1) >= 2) {
119                 break;
120             }
121             Thread.sleep(200);
122         }
123
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());
129     }
130
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);
139         return data[0];
140     }
141 }