1 package org.cacert.gigi.pages.wot;
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.HttpURLConnection;
6 import java.net.MalformedURLException;
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;
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;
21 import static org.junit.Assert.*;
23 public class TestAssurance extends ManagedTest {
24 private String assurerM;
25 private String assureeM;
28 private String cookie;
30 public void setup() throws IOException {
31 assurerM = createUniqueName() + "@cacert-test.org";
32 assureeM = createUniqueName() + "@cacert-test.org";
33 assurer = createAssuranceUser("a", "b", assurerM, "xvXV.1");
34 assuree = createAssuranceUser("a", "c", assureeM, "xvXV.1");
35 cookie = login(assurerM, "xvXV.1");
39 public void testAssureSearch() throws IOException {
40 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
41 + "&day=1&month=1&year=1910");
42 assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
46 public void testAssureSearchEmail() throws IOException {
47 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8")
48 + "&day=1&month=1&year=1910");
52 public void testAssureSearchDob() throws IOException {
53 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
54 + "&day=2&month=1&year=1910");
56 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
57 + "&day=1&month=2&year=1910");
59 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
60 + "&day=1&month=1&year=1911");
63 private String search(String query) throws MalformedURLException,
64 IOException, UnsupportedEncodingException {
65 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
66 URLConnection uc = u.openConnection();
68 uc.addRequestProperty("Cookie", cookie);
69 uc.getOutputStream().write((query).getBytes());
70 uc.getOutputStream().flush();
72 String loc = uc.getHeaderField("Location");
76 public void testAssureForm() throws IOException {
77 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
78 assertTrue(error, error.startsWith("</div>"));
82 public void testAssureFormNoCSRF() throws IOException {
84 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
86 .write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10")
88 uc.getOutputStream().flush();
89 assertEquals(500, uc.getResponseCode());
92 public void testAssureFormWrongCSRF() throws IOException {
94 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
96 .write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10&csrf=aragc")
98 uc.getOutputStream().flush();
99 assertEquals(500, uc.getResponseCode());
102 public void testAssureFormRace() throws IOException, SQLException {
103 URLConnection uc = buildupAssureFormConnection(true);
104 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
105 "UPDATE `users` SET email='changed' WHERE id=?");
106 ps.setInt(1, assuree);
109 .write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10")
111 uc.getOutputStream().flush();
112 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
113 assertTrue(error, !error.startsWith("</div>"));
116 public void testAssureFormFuture() throws IOException {
117 SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
118 int year = Integer.parseInt(sdf.format(new Date(System
119 .currentTimeMillis()))) + 2;
120 String error = getError("date="
122 + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
123 assertTrue(error, !error.startsWith("</div>"));
126 public void testAssureFormNoLoc() throws IOException {
127 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
128 assertTrue(error, !error.startsWith("</div>"));
129 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
130 assertTrue(error, !error.startsWith("</div>"));
134 public void testAssureFormInvalDate() throws IOException {
135 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
136 assertTrue(error, !error.startsWith("</div>"));
137 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
138 assertTrue(error, !error.startsWith("</div>"));
141 public void testAssureFormBoxes() throws IOException {
142 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
143 assertTrue(error, !error.startsWith("</div>"));
144 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
145 assertTrue(error, !error.startsWith("</div>"));
146 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
147 assertTrue(error, !error.startsWith("</div>"));
148 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
149 assertTrue(error, !error.startsWith("</div>"));
151 private String getError(String query) throws MalformedURLException,
153 URLConnection uc = buildupAssureFormConnection(true);
154 uc.getOutputStream().write((query).getBytes());
155 uc.getOutputStream().flush();
156 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
159 private URLConnection buildupAssureFormConnection(boolean doCSRF)
160 throws MalformedURLException, IOException {
161 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/"
163 URLConnection uc = u.openConnection();
164 uc.addRequestProperty("Cookie", cookie);
165 String csrf = getCSRF(uc);
166 uc = u.openConnection();
167 uc.addRequestProperty("Cookie", cookie);
168 uc.setDoOutput(true);
170 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());