1 package org.cacert.gigi.testUtils;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
9 import java.net.URLConnection;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.pages.account.domain.DomainOverview;
18 import org.junit.Before;
20 public abstract class PingTest extends ClientTest {
22 protected static void updateService(String token, String value, String action) throws IOException, MalformedURLException {
23 String manage = getTestProps().getProperty("domain.manage");
24 String url = manage + "t1=" + token + "&t2=" + value + "&action=" + action;
25 assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
28 protected void waitForPings(int count) throws SQLException, InterruptedException {
29 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
30 long start = System.currentTimeMillis();
31 while (System.currentTimeMillis() - start < 10000) {
32 ResultSet rs = ps.executeQuery();
34 if (rs.getInt(1) >= count) {
41 protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException {
42 URLConnection openConnection = u.openConnection();
43 openConnection.setRequestProperty("Cookie", cookie);
44 openConnection.setDoOutput(true);
45 openConnection.getOutputStream().write(content.getBytes());
46 openConnection.getHeaderField("Location");
48 String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
49 Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
50 Matcher m1 = dlink.matcher(newcontent);
52 URL u2 = new URL(u.toString() + m1.group(1));
56 protected Matcher initailizeDomainForm(URL u) throws IOException, Error {
57 URLConnection openConnection = u.openConnection();
58 openConnection.setRequestProperty("Cookie", cookie);
59 String content1 = IOUtils.readURL(openConnection);
60 csrf = getCSRF(1, content1);
62 Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)");
63 Matcher m = p.matcher(content1);
68 private static boolean first = true;
71 public void purgeDbAfterTest() throws SQLException, IOException {