]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/PingTest.java
6c57d76dcd80d30173f6b00f4c2ff133e7a1e18c
[gigi.git] / tests / org / cacert / gigi / testUtils / PingTest.java
1 package org.cacert.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
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;
15
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.pages.account.DomainOverview;
18 import org.junit.Before;
19
20 public abstract class PingTest extends ClientTest {
21
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());
26     }
27
28     protected void waitForPings(int count) throws SQLException, InterruptedException {
29         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
30         while (true) {
31             ResultSet rs = ps.executeQuery();
32             rs.next();
33             if (rs.getInt(1) >= count) {
34                 break;
35             }
36             Thread.sleep(200);
37         }
38     }
39
40     protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException {
41         URLConnection openConnection = u.openConnection();
42         openConnection.setRequestProperty("Cookie", cookie);
43         openConnection.setDoOutput(true);
44         openConnection.getOutputStream().write(content.getBytes());
45         openConnection.getHeaderField("Location");
46
47         String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
48         Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
49         Matcher m1 = dlink.matcher(newcontent);
50         m1.find();
51         URL u2 = new URL(u.toString() + m1.group(1));
52         return u2;
53     }
54
55     protected Matcher initailizeDomainForm(URL u) throws IOException, Error {
56         URLConnection openConnection = u.openConnection();
57         openConnection.setRequestProperty("Cookie", cookie);
58         String content1 = IOUtils.readURL(openConnection);
59         csrf = getCSRF(1, content1);
60
61         Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)");
62         Matcher m = p.matcher(content1);
63         m.find();
64         return m;
65     }
66
67     private static boolean first = true;
68
69     @Before
70     public void purgeDbAfterTest() throws SQLException, IOException {
71         if (first) {
72             first = false;
73             return;
74         }
75         purgeDatabase();
76     }
77
78 }