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.sql.Timestamp;
14 import java.text.SimpleDateFormat;
15 import java.util.Calendar;
16 import java.util.Date;
17 import java.util.regex.Pattern;
19 import org.cacert.gigi.database.GigiPreparedStatement;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.pages.account.MyDetails;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.ManagedTest;
24 import org.cacert.gigi.util.DayDate;
25 import org.cacert.gigi.util.Notary;
26 import org.hamcrest.Matcher;
27 import org.junit.Before;
28 import org.junit.Test;
30 public class TestAssurance extends ManagedTest {
32 private String assurerM;
34 private String assureeM;
36 private int assureeName;
38 private String cookie;
41 public void setup() throws IOException {
43 assurerM = createUniqueName() + "@cacert-test.org";
44 assureeM = createUniqueName() + "@cacert-test.org";
46 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
47 int assureeId = createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
48 assureeName = User.getById(assureeId).getPreferredName().getId();
50 cookie = login(assurerM, TEST_PASSWORD);
53 private Matcher<String> isAssuranceForm() {
54 return containsString("<select name=\"assuranceType\">");
58 public void testAssureSearch() throws IOException {
59 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
60 assertThat(loc, isAssuranceForm());
64 public void testAssureSearchEmail() throws IOException {
65 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
66 assertThat(loc, not(isAssuranceForm()));
70 public void testAssureSearchDobInvalid() throws IOException {
71 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=mal");
72 assertThat(loc, not(isAssuranceForm()));
76 public void testAssureSearchDob() throws IOException {
77 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
78 assertThat(loc, not(isAssuranceForm()));
79 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
80 assertThat(loc, not(isAssuranceForm()));
81 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
82 assertThat(loc, not(isAssuranceForm()));
85 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
86 URLConnection uc = get(cookie, AssurePage.PATH);
88 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
89 uc.getOutputStream().flush();
91 return IOUtils.readURL(uc);
95 public void testAssureForm() throws IOException {
96 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
100 public void testAssureFormContanisData() throws IOException {
101 URLConnection uc = buildupAssureFormConnection(true);
102 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&rules=1&assertion=1&points=10").getBytes("UTF-8"));
103 uc.getOutputStream().flush();
104 String data = IOUtils.readURL(uc);
105 assertThat(data, containsString(validVerificationDateString()));
106 assertThat(data, containsString("testcase"));
110 public void testAssureFormNoCSRF() throws IOException {
112 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
113 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
114 uc.getOutputStream().flush();
115 assertEquals(500, uc.getResponseCode());
119 public void testAssureFormWrongCSRF() throws IOException {
121 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
122 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
123 uc.getOutputStream().flush();
124 assertEquals(500, uc.getResponseCode());
128 public void testAssureFormRaceDoB() throws IOException, SQLException {
129 testAssureFormRace(false);
133 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
134 testAssureFormRace(true);
137 public void testAssureFormRace(boolean succeed) throws IOException, SQLException {
138 URLConnection uc = buildupAssureFormConnection(true);
140 String assureeCookie = login(assureeM, TEST_PASSWORD);
141 String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
143 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
145 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
146 uc.getOutputStream().flush();
147 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
151 assertTrue(error, !error.startsWith("</div>"));
152 assertThat(error, containsString("changed his personal details"));
157 public void testAssureFormFuture() throws IOException {
158 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
159 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
160 executeFails("date=" + year + "-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
164 public void testAssureFormFutureOK() throws IOException {
165 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
166 Calendar c = Calendar.getInstance();
167 c.setTimeInMillis(System.currentTimeMillis());
168 c.add(Calendar.HOUR_OF_DAY, 12);
170 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
174 public void testAssureFormPastInRange() throws IOException {
175 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
179 public void testAssureFormPastOnLimit() throws IOException {
180 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
181 Calendar c = Calendar.getInstance();
182 c.setTimeInMillis(System.currentTimeMillis());
183 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
184 c.add(Calendar.DAY_OF_MONTH, 1);
186 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
190 public void testAssureFormPastOutOfRange() throws IOException {
191 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
192 Calendar c = Calendar.getInstance();
193 c.setTimeInMillis(System.currentTimeMillis());
194 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
196 executeFails("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
200 public void testAssureFormNoLoc() throws IOException {
201 executeFails("date=" + validVerificationDateString() + "&location=a&certify=1&rules=1&assertion=1&points=10");
202 executeFails("date=" + validVerificationDateString() + "&location=&certify=1&rules=1&assertion=1&points=10");
206 public void testAssureFormInvalDate() throws IOException {
207 executeFails("date=20000101&location=testcase&certify=1&rules=1&assertion=1&points=10");
208 executeFails("date=&location=testcase&certify=1&rules=1&assertion=1&points=10");
212 public void testAssureFormBoxes() throws IOException {
213 executeFails("date=" + validVerificationDateString() + "&location=testcase&certify=0&rules=1&assertion=1&points=10");
214 executeFails("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=&assertion=1&points=10");
215 executeFails("date=" + validVerificationDateString() + "&location=testcase&certify=1&rules=1&assertion=z&points=10");
219 public void testAssureListingValid() throws IOException {
220 String uniqueLoc = createUniqueName();
221 execute("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
223 String cookie = login(assureeM, TEST_PASSWORD);
224 URLConnection url = get(cookie, MyPoints.PATH);
225 String resp = IOUtils.readURL(url);
226 resp = resp.split(Pattern.quote("</table>"))[0];
227 assertThat(resp, containsString(uniqueLoc));
231 public void testAssurerListingValid() throws IOException {
232 String uniqueLoc = createUniqueName();
233 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
234 String cookie = login(assurerM, TEST_PASSWORD);
235 URLConnection url = get(cookie, MyPoints.PATH);
236 String resp = IOUtils.readURL(url);
237 resp = resp.split(Pattern.quote("</table>"))[1];
238 assertThat(resp, containsString(uniqueLoc));
241 private void executeFails(String query) throws MalformedURLException, IOException {
242 assertThat(execute(query), hasError());
246 private void executeSuccess(String query) throws MalformedURLException, IOException {
247 assertThat(execute(query), hasNoError());
251 private String execute(String query) throws MalformedURLException, IOException {
252 URLConnection uc = buildupAssureFormConnection(true);
253 uc.getOutputStream().write(("assuredName=" + assureeName + "&" + query).getBytes("UTF-8"));
254 uc.getOutputStream().flush();
255 return IOUtils.readURL(uc);
258 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
259 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
262 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
263 URLConnection uc = get(cookie, AssurePage.PATH);
264 uc.setDoOutput(true);
265 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
267 String csrf = getCSRF(uc);
268 uc = get(cookie, AssurePage.PATH);
269 uc.setDoOutput(true);
271 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
277 public void testMultipleAssurance() throws IOException {
279 User users[] = User.findByEmail(assurerM);
280 int agentID = users[0].getId();
282 users = User.findByEmail(assureeM);
283 int applicantID = users[0].getId();
285 // enter first entry 200 days in the past
286 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
287 ps.setInt(1, agentID);
288 ps.setInt(2, applicantID);
290 ps.setString(4, "test-location");
291 ps.setString(5, "2010-01-01");
292 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
296 // enter second entry
297 String uniqueLoc = createUniqueName();
298 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
300 // enter third entry on the same day
301 URLConnection uc = get(cookie, AssurePage.PATH);
302 uc.setDoOutput(true);
303 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
304 assertThat(IOUtils.readURL(uc), hasError());