X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2Fping%2FTestDNS.java;h=606efa29073f5c0e3afcf9a7d75c8b4827ae4314;hp=e6c53f161ef4af5dddaad95fa1c17b6c8c81bd6e;hb=b1092da65fd373d945343e01dd8975ec3b84db0a;hpb=5dcfbec6497e6b11aff12b7c1a1e187f3b6b30fb diff --git a/tests/org/cacert/gigi/ping/TestDNS.java b/tests/org/cacert/gigi/ping/TestDNS.java index e6c53f16..606efa29 100644 --- a/tests/org/cacert/gigi/ping/TestDNS.java +++ b/tests/org/cacert/gigi/ping/TestDNS.java @@ -3,58 +3,67 @@ package org.cacert.gigi.ping; import static org.junit.Assert.*; 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.pages.account.domain.DomainOverview; import org.cacert.gigi.testUtils.IOUtils; -import org.cacert.gigi.testUtils.ManagedTest; +import org.cacert.gigi.testUtils.PingTest; import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; import org.cacert.gigi.util.DNSUtil; import org.cacert.gigi.util.RandomToken; 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 testEmailAndDNS() throws IOException, InterruptedException, SQLException, NamingException { - String email = createUniqueName() + "@example.org"; - int uid = createVerifiedUser("a", "b", email, TEST_PASSWORD); - String cookie = login(email, TEST_PASSWORD); + public void emailAndDNSSuccess() throws IOException, InterruptedException, SQLException, NamingException { + testEmailAndDNS(0, 0, true, true); + } + + @Test + public void dnsFail() throws IOException, InterruptedException, SQLException, NamingException { + testEmailAndDNS(1, 0, false, true); + } + + @Test + 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 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); - 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), m.group(2)); + URL u = new URL("https://" + getServerName() + DomainOverview.PATH); + Matcher m = initailizeDomainForm(u); + 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" + // @@ -63,47 +72,29 @@ public class TestDNS extends ManagedTest { "&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)); + URL u2 = sendDomainForm(u, content); 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); + if (emailVariant == 0) { + String link = mail.extractLink(); + new URL(link).openConnection().getHeaderField(""); } - newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie)); + waitForPings(2); + + String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie)); Pattern pat = Pattern.compile("dns\\s*success"); - assertTrue(newcontent, pat.matcher(newcontent).find()); + assertTrue(newcontent, !successDNS ^ pat.matcher(newcontent).find()); pat = Pattern.compile("email\\s*success"); - assertTrue(newcontent, pat.matcher(newcontent).find()); + 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 targetDomain = token + "._cacert._auth." + test; String[] data = DNSUtil.getTXTEntries(targetDomain, getTestProps().getProperty("domain.testns")); assertEquals(1, data.length); return data[0]; + } }