package org.cacert.gigi.ping;
import static org.junit.Assert.*;
+import static org.junit.Assume.*;
import java.io.IOException;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
import java.net.URLEncoder;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.naming.NamingException;
-import org.cacert.gigi.database.DatabaseConnection;
-import org.cacert.gigi.pages.account.DomainOverview;
import org.cacert.gigi.testUtils.IOUtils;
-import org.cacert.gigi.testUtils.ManagedTest;
-import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
+import org.cacert.gigi.testUtils.PingTest;
+import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
import org.cacert.gigi.util.DNSUtil;
import org.cacert.gigi.util.RandomToken;
-import org.junit.After;
import org.junit.Test;
-public class TestDNS extends ManagedTest {
+public class TestDNS extends PingTest {
@Test
- public void testDNSSanity() throws IOException, NamingException {
+ public void dnsSanity() throws IOException, NamingException {
String token = RandomToken.generateToken(16);
String value = RandomToken.generateToken(16);
- String reRead = updateDNS(token, value);
- assertEquals(value, reRead);
+ updateService(token, value, "dns");
+ assertEquals(value, readDNS(token));
}
@Test
- public void testEmailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
+ public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException {
testEmailAndDNS(0, 0, true, true);
}
- @After
- public void test() throws SQLException, IOException {
- purgeDatabase();
+ @Test
+ public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException {
+ testEmailAndDNS(1, 0, false, true);
}
@Test
- public void testEmailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
- testEmailAndDNS(1, 0, false, true);
- purgeDatabase();
+ public void dnsContentFail() throws IOException, InterruptedException, SQLException, NamingException {
testEmailAndDNS(2, 0, false, true);
}
+ @Test
+ public void emailFail() throws IOException, InterruptedException, SQLException, NamingException {
+ testEmailAndDNS(0, 1, true, false);
+ }
+
+ @Test
+ public void emailAndDNSFail() throws IOException, InterruptedException, SQLException, NamingException {
+ testEmailAndDNS(2, 1, false, false);
+ }
+
public void testEmailAndDNS(int dnsVariant, int emailVariant, boolean successDNS, boolean successMail) throws IOException, InterruptedException, SQLException, NamingException {
- String email = createUniqueName() + "@example.org";
- createVerifiedUser("a", "b", email, TEST_PASSWORD);
- String cookie = login(email, TEST_PASSWORD);
String test = getTestProps().getProperty("domain.dnstest");
- URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
- URLConnection openConnection = u.openConnection();
- openConnection.setRequestProperty("Cookie", cookie);
- String content1 = IOUtils.readURL(openConnection);
- String csrf = getCSRF(1, content1);
+ assumeNotNull(test);
- Pattern p = Pattern.compile("cacert-([A-Za-z0-9]+) IN TXT ([A-Za-z0-9]+)");
- Matcher m = p.matcher(content1);
- m.find();
- updateDNS(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""));
+ Matcher m = initailizeDomainForm();
+ updateService(m.group(1) + (dnsVariant == 1 ? "a" : ""), m.group(2) + (dnsVariant == 2 ? "a" : ""), "dns");
String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
"&emailType=y&email=2&DNSType=y" + //
"&ssl-type-2=direct&ssl-port-2=" + //
"&ssl-type-3=direct&ssl-port-3=" + //
"&adddomain&csrf=" + csrf;
- openConnection = u.openConnection();
- openConnection.setRequestProperty("Cookie", cookie);
- openConnection.setDoOutput(true);
- openConnection.getOutputStream().write(content.getBytes());
- openConnection.getHeaderField("Location");
-
- String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
- Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
- Matcher m1 = dlink.matcher(newcontent);
- m1.find();
- URL u2 = new URL(u.toString() + m1.group(1));
-
- TestMail mail = getMailReciever().recieve();
- String link = mail.extractLink();
- new URL(link).openConnection().getHeaderField("");
-
- PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
- while (true) {
- ResultSet rs = ps.executeQuery();
- rs.next();
- if (rs.getInt(1) >= 2) {
- break;
- }
- Thread.sleep(200);
+ String p2 = sendDomainForm(content);
+
+ TestMail mail = getMailReciever().receive();
+ if (emailVariant == 0) {
+ mail.verify();
}
- newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
+ waitForPings(2);
+
+ String newcontent = IOUtils.readURL(get(p2));
Pattern pat = Pattern.compile("<td>dns</td>\\s*<td>success</td>");
assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find());
pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
}
- private String updateDNS(String token, String value) throws IOException, MalformedURLException, NamingException {
+ private String readDNS(String token) throws NamingException {
String test = getTestProps().getProperty("domain.dnstest");
- String targetDomain = "cacert-" + token + "." + test;
- String manage = getTestProps().getProperty("domain.dnsmanage");
- String url = manage + "t1=" + token + "&t2=" + value;
- assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
- String[] data = DNSUtil.getTXTEntries(targetDomain, getTestProps().getProperty("domain.testns"));
+ assumeNotNull(test);
+ String targetDomain = token + "._cacert._auth." + test;
+ String testns = getTestProps().getProperty("domain.testns");
+ assumeNotNull(testns);
+ String[] data = DNSUtil.getTXTEntries(targetDomain, testns);
assertEquals(1, data.length);
return data[0];
+
}
}