]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/PingTest.java
50d5d7fc14696a97692c39e8fb85b35ec8feb9ed
[gigi.git] / tests / org / cacert / gigi / testUtils / PingTest.java
1 package org.cacert.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
5
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.sql.SQLException;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
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;
20
21 public abstract class PingTest extends ClientTest {
22
23     protected static void updateService(String token, String value, String action) throws IOException, MalformedURLException {
24         String manage = getTestProps().getProperty("domain.manage");
25         assumeNotNull(manage);
26         String url = manage + "t1=" + token + "&t2=" + value + "&action=" + action;
27         assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
28     }
29
30     protected void waitForPings(int count) throws SQLException, InterruptedException {
31         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT COUNT(*) FROM domainPinglog");
32         long start = System.currentTimeMillis();
33         while (System.currentTimeMillis() - start < 10000) {
34             GigiResultSet rs = ps.executeQuery();
35             rs.next();
36             if (rs.getInt(1) >= count) {
37                 break;
38             }
39             Thread.sleep(200);
40         }
41     }
42
43     protected URL sendDomainForm(URL u, String content) throws IOException, MalformedURLException {
44         URLConnection openConnection = u.openConnection();
45         openConnection.setRequestProperty("Cookie", cookie);
46         openConnection.setDoOutput(true);
47         openConnection.getOutputStream().write(content.getBytes());
48         openConnection.getHeaderField("Location");
49
50         String newcontent = IOUtils.readURL(cookie(u.openConnection(), cookie));
51         Pattern dlink = Pattern.compile(DomainOverview.PATH + "([0-9]+)'>");
52         Matcher m1 = dlink.matcher(newcontent);
53         if ( !m1.find()) {
54             throw new Error(newcontent);
55         }
56         URL u2 = new URL(u.toString() + m1.group(1));
57         return u2;
58     }
59
60     protected Matcher initailizeDomainForm(URL u) throws IOException, Error {
61         URLConnection openConnection = u.openConnection();
62         openConnection.setRequestProperty("Cookie", cookie);
63         String content1 = IOUtils.readURL(openConnection);
64         csrf = getCSRF(1, content1);
65
66         Pattern p = Pattern.compile("([A-Za-z0-9]+)._cacert._auth IN TXT ([A-Za-z0-9]+)");
67         Matcher m = p.matcher(content1);
68         m.find();
69         return m;
70     }
71
72     @After
73     public void purgeDbAfterTest() throws SQLException, IOException {
74         purgeDatabase();
75     }
76
77 }