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