]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
Format code according do BenBE's formatter.
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestAssurance.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.HttpURLConnection;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.net.URLEncoder;
10 import java.sql.PreparedStatement;
11 import java.sql.SQLException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.testUtils.IOUtils;
17 import org.cacert.gigi.testUtils.ManagedTest;
18 import org.junit.Before;
19 import org.junit.Test;
20
21 import static org.junit.Assert.*;
22
23 public class TestAssurance extends ManagedTest {
24
25     private String assurerM;
26
27     private String assureeM;
28
29     private int assurer;
30
31     private int assuree;
32
33     private String cookie;
34
35     @Before
36     public void setup() throws IOException {
37         assurerM = createUniqueName() + "@cacert-test.org";
38         assureeM = createUniqueName() + "@cacert-test.org";
39         assurer = createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
40         assuree = createAssuranceUser("a", "c", assureeM, TEST_PASSWORD);
41         cookie = login(assurerM, TEST_PASSWORD);
42
43     }
44
45     @Test
46     public void testAssureSearch() throws IOException {
47         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
48         assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
49     }
50
51     @Test
52     public void testAssureSearchEmail() throws IOException {
53         String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
54         assertNull(loc);
55     }
56
57     @Test
58     public void testAssureSearchDob() throws IOException {
59         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
60         assertNull(loc);
61         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
62         assertNull(loc);
63         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
64         assertNull(loc);
65     }
66
67     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
68         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
69         URLConnection uc = u.openConnection();
70         uc.setDoOutput(true);
71         uc.addRequestProperty("Cookie", cookie);
72         uc.getOutputStream().write((query).getBytes());
73         uc.getOutputStream().flush();
74
75         String loc = uc.getHeaderField("Location");
76         return loc;
77     }
78
79     @Test
80     public void testAssureForm() throws IOException {
81         String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
82         assertTrue(error, error.startsWith("</div>"));
83     }
84
85     @Test
86     public void testAssureFormNoCSRF() throws IOException {
87         // override csrf
88         HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
89         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes());
90         uc.getOutputStream().flush();
91         assertEquals(500, uc.getResponseCode());
92     }
93
94     @Test
95     public void testAssureFormWrongCSRF() throws IOException {
96         // override csrf
97         HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
98         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10&csrf=aragc").getBytes());
99         uc.getOutputStream().flush();
100         assertEquals(500, uc.getResponseCode());
101     }
102
103     @Test
104     public void testAssureFormRace() throws IOException, SQLException {
105         URLConnection uc = buildupAssureFormConnection(true);
106         PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `users` SET email='changed' WHERE id=?");
107         ps.setInt(1, assuree);
108         ps.execute();
109         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes());
110         uc.getOutputStream().flush();
111         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
112         assertTrue(error, !error.startsWith("</div>"));
113     }
114
115     @Test
116     public void testAssureFormFuture() throws IOException {
117         SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
118         int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
119         String error = getError("date=" + year + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
120         assertTrue(error, !error.startsWith("</div>"));
121     }
122
123     @Test
124     public void testAssureFormNoLoc() throws IOException {
125         String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
126         assertTrue(error, !error.startsWith("</div>"));
127         error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
128         assertTrue(error, !error.startsWith("</div>"));
129     }
130
131     @Test
132     public void testAssureFormInvalDate() throws IOException {
133         String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
134         assertTrue(error, !error.startsWith("</div>"));
135         error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
136         assertTrue(error, !error.startsWith("</div>"));
137     }
138
139     @Test
140     public void testAssureFormBoxes() throws IOException {
141         String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
142         assertTrue(error, !error.startsWith("</div>"));
143         error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
144         assertTrue(error, !error.startsWith("</div>"));
145         error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
146         assertTrue(error, !error.startsWith("</div>"));
147         error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
148         assertTrue(error, !error.startsWith("</div>"));
149     }
150
151     private String getError(String query) throws MalformedURLException, IOException {
152         URLConnection uc = buildupAssureFormConnection(true);
153         uc.getOutputStream().write((query).getBytes());
154         uc.getOutputStream().flush();
155         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
156         return error;
157     }
158
159     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
160         URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/" + assuree);
161         URLConnection uc = u.openConnection();
162         uc.addRequestProperty("Cookie", cookie);
163         String csrf = getCSRF(uc);
164         uc = u.openConnection();
165         uc.addRequestProperty("Cookie", cookie);
166         uc.setDoOutput(true);
167         if (doCSRF) {
168             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());
169         }
170         return uc;
171     }
172
173 }