]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/PingTest.java
upd: move external keywords to own class
[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.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.pages.account.domain.DomainOverview;
18 import org.cacert.gigi.util.SystemKeywords;
19 import org.junit.After;
20
21 /**
22  * Base class for test suites that check extensively if the domain-ping
23  * functionality wroks as expected.
24  */
25 public abstract class PingTest extends ClientTest {
26
27     protected String csrf;
28
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());
34     }
35
36     protected void waitForPings(int count) throws SQLException, InterruptedException {
37         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT COUNT(*) FROM `domainPinglog`")) {
38             long start = System.currentTimeMillis();
39             while (System.currentTimeMillis() - start < 10000) {
40                 GigiResultSet rs = ps.executeQuery();
41                 rs.next();
42                 if (rs.getInt(1) >= count) {
43                     break;
44                 }
45                 Thread.sleep(200);
46             }
47         }
48     }
49
50     protected String sendDomainForm(String content) throws IOException, MalformedURLException {
51         URLConnection openConnection = get(DomainOverview.PATH);
52         openConnection.setDoOutput(true);
53         openConnection.getOutputStream().write(content.getBytes("UTF-8"));
54         openConnection.getHeaderField("Location");
55         int code = ((HttpURLConnection) openConnection).getResponseCode();
56         if (code != 302) {
57             throw new Error("Code was: " + code + "\ncontent was: " + fetchStartErrorMessage(IOUtils.readURL(openConnection)));
58         }
59
60         String newcontent = IOUtils.readURL(get(DomainOverview.PATH));
61         Pattern dlink = Pattern.compile(DomainOverview.PATH + "/([0-9]+)'>");
62         Matcher m1 = dlink.matcher(newcontent);
63         if ( !m1.find()) {
64             throw new Error(newcontent);
65         }
66         return DomainOverview.PATH + "/" + m1.group(1);
67     }
68
69     protected Matcher initailizeDomainForm() throws IOException, Error {
70         String content1 = IOUtils.readURL(get(DomainOverview.PATH));
71         csrf = getCSRF(1, content1);
72
73         Pattern p = Pattern.compile("([A-Za-z0-9]+)." + SystemKeywords.DNS_PREFIX + "._auth IN TXT ([A-Za-z0-9]+)");
74         Matcher m = p.matcher(content1);
75         m.find();
76         return m;
77     }
78
79     @After
80     public void purgeDbAfterTest() throws SQLException, IOException {
81         purgeDatabase();
82     }
83
84 }