1 package org.cacert.gigi.testUtils;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.sql.SQLException;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.database.GigiPreparedStatement;
17 import org.cacert.gigi.database.GigiResultSet;
18 import org.cacert.gigi.pages.account.domain.DomainOverview;
19 import org.junit.After;
22 * Base class for test suites that check extensively if the domain-ping
23 * functionality wroks as expected.
25 public abstract class PingTest extends ClientTest {
27 protected String csrf;
29 protected static void updateService(String token, String value, String action) throws IOException, MalformedURLException {
30 String manage = getTestProps().getProperty("domain.manage");
31 assumeNotNull(manage);
32 String url = manage + "t1=" + token + "&t2=" + value + "&action=" + action;
33 assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
36 protected void waitForPings(int count) throws SQLException, InterruptedException {
37 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM `domainPinglog`");
38 long start = System.currentTimeMillis();
39 while (System.currentTimeMillis() - start < 10000) {
40 GigiResultSet rs = ps.executeQuery();
42 if (rs.getInt(1) >= count) {
49 protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException {
50 URLConnection openConnection = u.openConnection();
51 openConnection.setRequestProperty("Cookie", cookie);
52 openConnection.setDoOutput(true);
53 openConnection.getOutputStream().write(content.getBytes("UTF-8"));
54 openConnection.getHeaderField("Location");
56 String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
57 Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
58 Matcher m1 = dlink.matcher(newcontent);
60 throw new Error(newcontent);
62 URL u2 = new URL(u.toString() + m1.group(1));
66 protected Matcher initailizeDomainForm(URL u) throws IOException, Error {
67 URLConnection openConnection = u.openConnection();
68 openConnection.setRequestProperty("Cookie", cookie);
69 String content1 = IOUtils.readURL(openConnection);
70 csrf = getCSRF(1, content1);
72 Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)");
73 Matcher m = p.matcher(content1);
79 public void purgeDbAfterTest() throws SQLException, IOException {