]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/PingTest.java
[test-config] FIX: the ssl-pinger+ add various tests for that.
[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         long start = System.currentTimeMillis();
31         while (System.currentTimeMillis() - start < 10000) {
32             ResultSet rs = ps.executeQuery();
33             rs.next();
34             if (rs.getInt(1) >= count) {
35                 break;
36             }
37             Thread.sleep(200);
38         }
39     }
40
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");
47
48         String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
49         Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
50         Matcher m1 = dlink.matcher(newcontent);
51         m1.find();
52         URL u2 = new URL(u.toString() + m1.group(1));
53         return u2;
54     }
55
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);
61
62         Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)");
63         Matcher m = p.matcher(content1);
64         m.find();
65         return m;
66     }
67
68     private static boolean first = true;
69
70     @Before
71     public void purgeDbAfterTest() throws SQLException, IOException {
72         if (first) {
73             first = false;
74             return;
75         }
76         purgeDatabase();
77     }
78
79 }