1 package org.cacert.gigi.pages.wot;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.sql.SQLException;
13 import java.text.SimpleDateFormat;
14 import java.util.Calendar;
15 import java.util.Date;
16 import java.util.regex.Pattern;
18 import org.cacert.gigi.pages.account.MyDetails;
19 import org.cacert.gigi.testUtils.IOUtils;
20 import org.cacert.gigi.testUtils.ManagedTest;
21 import org.junit.Before;
22 import org.junit.Test;
24 public class TestAssurance extends ManagedTest {
26 private String assurerM;
28 private String assureeM;
30 private String cookie;
33 public void setup() throws IOException {
35 assurerM = createUniqueName() + "@cacert-test.org";
36 assureeM = createUniqueName() + "@cacert-test.org";
38 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
39 createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
41 cookie = login(assurerM, TEST_PASSWORD);
45 public void testAssureSearch() throws IOException {
46 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
47 assertTrue(loc, loc.contains("type=\"checkbox\" name=\"tos_agree\""));
51 public void testAssureSearchEmail() throws IOException {
52 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
53 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"tos_agree\""));
57 public void testAssureSearchDob() throws IOException {
58 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
59 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"tos_agree\""));
60 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
61 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"tos_agree\""));
62 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
63 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"tos_agree\""));
66 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
67 URLConnection uc = get(cookie, AssurePage.PATH);
69 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
70 uc.getOutputStream().flush();
72 return IOUtils.readURL(uc);
76 public void testAssureForm() throws IOException {
77 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
82 public void testAssureFormContanisData() throws IOException {
83 URLConnection uc = buildupAssureFormConnection(true);
84 uc.getOutputStream().write(("date=2000-01-01&location=testcase&rules=1&tos_agree=1&assertion=1&points=10").getBytes("UTF-8"));
85 uc.getOutputStream().flush();
86 String data = IOUtils.readURL(uc);
87 assertThat(data, containsString("2000-01-01"));
88 assertThat(data, containsString("testcase"));
92 public void testAssureFormNoCSRF() throws IOException {
94 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
95 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10").getBytes("UTF-8"));
96 uc.getOutputStream().flush();
97 assertEquals(500, uc.getResponseCode());
101 public void testAssureFormWrongCSRF() throws IOException {
103 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
104 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
105 uc.getOutputStream().flush();
106 assertEquals(500, uc.getResponseCode());
110 public void testAssureFormRaceName() throws IOException, SQLException {
111 testAssureFormRace(true, false);
115 public void testAssureFormRaceDoB() throws IOException, SQLException {
116 testAssureFormRace(false, false);
120 public void testAssureFormRaceNameBlind() throws IOException, SQLException {
121 testAssureFormRace(true, true);
125 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
126 testAssureFormRace(false, true);
129 public void testAssureFormRace(boolean name, boolean succeed) throws IOException, SQLException {
130 URLConnection uc = buildupAssureFormConnection(true);
132 String assureeCookie = login(assureeM, TEST_PASSWORD);
133 String newName = "lname=" + (name && !succeed ? "a" : "c") + "&fname=a&mname=&suffix=";
134 String newDob = "day=1&month=1&year=" + ( !name && !succeed ? 1911 : 1910);
136 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newName + "&" + newDob + "&processDetails", 0));
138 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10").getBytes("UTF-8"));
139 uc.getOutputStream().flush();
140 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
144 assertTrue(error, !error.startsWith("</div>"));
145 assertThat(error, containsString("changed his personal details"));
150 public void testAssureFormFuture() throws IOException {
151 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
152 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
153 String error = getError("date=" + year + "-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
154 assertTrue(error, !error.startsWith("</div>"));
158 public void testAssureFormFutureOK() throws IOException {
159 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
160 Calendar c = Calendar.getInstance();
161 c.setTimeInMillis(System.currentTimeMillis());
162 c.add(Calendar.HOUR_OF_DAY, 12);
164 String error = getError("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
169 public void testAssureFormNoLoc() throws IOException {
170 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
171 assertTrue(error, !error.startsWith("</div>"));
172 error = getError("date=2000-01-01&location=&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
173 assertTrue(error, !error.startsWith("</div>"));
177 public void testAssureFormInvalDate() throws IOException {
178 String error = getError("date=20000101&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
179 assertTrue(error, !error.startsWith("</div>"));
180 error = getError("date=&location=testcase&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
181 assertTrue(error, !error.startsWith("</div>"));
185 public void testAssureFormBoxes() throws IOException {
186 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&tos_agree=1&assertion=1&points=10");
187 assertTrue(error, !error.startsWith("</div>"));
188 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&tos_agree=1&assertion=1&points=10");
189 assertTrue(error, !error.startsWith("</div>"));
190 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=a&assertion=1&points=10");
191 assertTrue(error, !error.startsWith("</div>"));
192 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&tos_agree=1&assertion=z&points=10");
193 assertTrue(error, !error.startsWith("</div>"));
197 public void testAssureListingValid() throws IOException {
198 String uniqueLoc = createUniqueName();
199 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
201 String cookie = login(assureeM, TEST_PASSWORD);
202 URLConnection url = get(cookie, MyPoints.PATH);
203 String resp = IOUtils.readURL(url);
204 resp = resp.split(Pattern.quote("</table>"))[0];
205 assertThat(resp, containsString(uniqueLoc));
209 public void testAssurerListingValid() throws IOException {
210 String uniqueLoc = createUniqueName();
211 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&tos_agree=1&assertion=1&points=10");
213 String cookie = login(assurerM, TEST_PASSWORD);
214 URLConnection url = get(cookie, MyPoints.PATH);
215 String resp = IOUtils.readURL(url);
216 resp = resp.split(Pattern.quote("</table>"))[1];
217 assertThat(resp, containsString(uniqueLoc));
220 private String getError(String query) throws MalformedURLException, IOException {
221 URLConnection uc = buildupAssureFormConnection(true);
222 uc.getOutputStream().write((query).getBytes("UTF-8"));
223 uc.getOutputStream().flush();
224 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
228 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
229 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
232 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
233 URLConnection uc = get(cookie, AssurePage.PATH);
234 uc.setDoOutput(true);
235 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
237 String csrf = getCSRF(uc);
238 uc = get(cookie, AssurePage.PATH);
239 uc.setDoOutput(true);
241 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));