1 package org.cacert.gigi.pages.wot;
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.MalformedURLException;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.junit.Before;
18 import org.junit.Test;
20 import static org.junit.Assert.*;
22 public class TestAssurance extends ManagedTest {
23 private String assurerM;
24 private String assureeM;
27 private String cookie;
29 public void setup() throws IOException {
30 assurerM = createUniqueName() + "@cacert-test.org";
31 assureeM = createUniqueName() + "@cacert-test.org";
32 assurer = createAssuranceUser("a", "b", assurerM, "xvXV.1");
33 assuree = createAssuranceUser("a", "c", assureeM, "xvXV.1");
34 cookie = login(assurerM, "xvXV.1");
38 public void testAssureSearch() throws IOException {
39 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
40 + "&day=1&month=1&year=1910");
41 assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
45 public void testAssureSearchEmail() throws IOException {
46 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8")
47 + "&day=1&month=1&year=1910");
51 public void testAssureSearchDob() throws IOException {
52 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
53 + "&day=2&month=1&year=1910");
55 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
56 + "&day=1&month=2&year=1910");
58 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
59 + "&day=1&month=1&year=1911");
62 private String search(String query) throws MalformedURLException,
63 IOException, UnsupportedEncodingException {
64 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
65 URLConnection uc = u.openConnection();
67 uc.addRequestProperty("Cookie", cookie);
68 uc.getOutputStream().write((query).getBytes());
69 uc.getOutputStream().flush();
71 String loc = uc.getHeaderField("Location");
75 public void testAssureForm() throws IOException {
76 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
77 assertTrue(error, error.startsWith("</div>"));
81 public void testAssureFormRace() throws IOException, SQLException {
82 URLConnection uc = buildupAssureFormConnection();
83 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
84 "UPDATE `users` SET email='changed' WHERE id=?");
85 ps.setInt(1, assuree);
88 .write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10")
90 uc.getOutputStream().flush();
91 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
92 assertTrue(error, !error.startsWith("</div>"));
95 public void testAssureFormFuture() throws IOException {
96 SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
97 int year = Integer.parseInt(sdf.format(new Date(System
98 .currentTimeMillis()))) + 2;
99 String error = getError("date="
101 + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
102 assertTrue(error, !error.startsWith("</div>"));
105 public void testAssureFormNoLoc() throws IOException {
106 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
107 assertTrue(error, !error.startsWith("</div>"));
108 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
109 assertTrue(error, !error.startsWith("</div>"));
113 public void testAssureFormInvalDate() throws IOException {
114 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
115 assertTrue(error, !error.startsWith("</div>"));
116 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
117 assertTrue(error, !error.startsWith("</div>"));
120 public void testAssureFormBoxes() throws IOException {
121 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
122 assertTrue(error, !error.startsWith("</div>"));
123 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
124 assertTrue(error, !error.startsWith("</div>"));
125 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
126 assertTrue(error, !error.startsWith("</div>"));
127 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
128 assertTrue(error, !error.startsWith("</div>"));
130 private String getError(String query) throws MalformedURLException,
132 URLConnection uc = buildupAssureFormConnection();
133 uc.getOutputStream().write((query).getBytes());
134 uc.getOutputStream().flush();
135 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
138 private URLConnection buildupAssureFormConnection()
139 throws MalformedURLException, IOException {
140 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/"
142 URLConnection uc = u.openConnection();
143 uc.addRequestProperty("Cookie", cookie);
144 uc.getInputStream();// request form
145 uc = u.openConnection();
146 uc.addRequestProperty("Cookie", cookie);
147 uc.setDoOutput(true);