X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FPingTest.java;h=0d15f2e86c3cf5807c3a969d647b83e061103c89;hb=78aea4e2c6a8e99ba546c4189d7071d57c1aaf3b;hp=9007ec25811b5d4044bf3220a6ad95fecc35a480;hpb=b1092da65fd373d945343e01dd8975ec3b84db0a;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/PingTest.java b/tests/org/cacert/gigi/testUtils/PingTest.java index 9007ec25..0d15f2e8 100644 --- a/tests/org/cacert/gigi/testUtils/PingTest.java +++ b/tests/org/cacert/gigi/testUtils/PingTest.java @@ -1,62 +1,71 @@ package org.cacert.gigi.testUtils; 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.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.pages.account.domain.DomainOverview; -import org.junit.Before; +import org.junit.After; +/** + * Base class for test suites that check extensively if the domain-ping + * functionality wroks as expected. + */ public abstract class PingTest extends ClientTest { + protected String csrf; + protected static void updateService(String token, String value, String action) throws IOException, MalformedURLException { String manage = getTestProps().getProperty("domain.manage"); + assumeNotNull(manage); String url = manage + "t1=" + token + "&t2=" + value + "&action=" + action; assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode()); } protected void waitForPings(int count) throws SQLException, InterruptedException { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog"); - long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start < 10000) { - ResultSet rs = ps.executeQuery(); - rs.next(); - if (rs.getInt(1) >= count) { - break; + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT COUNT(*) FROM `domainPinglog`")) { + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < 10000) { + GigiResultSet rs = ps.executeQuery(); + rs.next(); + if (rs.getInt(1) >= count) { + break; + } + Thread.sleep(200); } - Thread.sleep(200); } } - protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException { - URLConnection openConnection = u.openConnection(); - openConnection.setRequestProperty("Cookie", cookie); + protected String sendDomainForm(String content) throws IOException, MalformedURLException { + URLConnection openConnection = get(DomainOverview.PATH); openConnection.setDoOutput(true); - openConnection.getOutputStream().write(content.getBytes()); + openConnection.getOutputStream().write(content.getBytes("UTF-8")); openConnection.getHeaderField("Location"); + if (((HttpURLConnection) openConnection).getResponseCode() != 302) { + throw new Error(IOUtils.readURL(openConnection)); + } - String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie)); + String newcontent = IOUtils.readURL(get(DomainOverview.PATH)); Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>"); Matcher m1 = dlink.matcher(newcontent); - m1.find(); - URL u2 = new URL(u.toString() + m1.group(1)); - return u2; + if ( !m1.find()) { + throw new Error(newcontent); + } + return DomainOverview.PATH + m1.group(1); } - protected Matcher initailizeDomainForm(URL u) throws IOException, Error { - URLConnection openConnection = u.openConnection(); - openConnection.setRequestProperty("Cookie", cookie); - String content1 = IOUtils.readURL(openConnection); + protected Matcher initailizeDomainForm() throws IOException, Error { + String content1 = IOUtils.readURL(get(DomainOverview.PATH)); csrf = getCSRF(1, content1); Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)"); @@ -65,14 +74,8 @@ public abstract class PingTest extends ClientTest { return m; } - private static boolean first = true; - - @Before + @After public void purgeDbAfterTest() throws SQLException, IOException { - if (first) { - first = false; - return; - } purgeDatabase(); }