1 package org.cacert.gigi.pages.wot;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.io.UnsupportedEncodingException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.sql.PreparedStatement;
13 import java.sql.SQLException;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.regex.Pattern;
18 import org.cacert.gigi.database.DatabaseConnection;
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;
34 private String cookie;
37 public void setup() throws IOException {
38 assurerM = createUniqueName() + "@cacert-test.org";
39 assureeM = createUniqueName() + "@cacert-test.org";
40 assurer = createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
41 assuree = createAssuranceUser("a", "c", assureeM, TEST_PASSWORD);
42 cookie = login(assurerM, TEST_PASSWORD);
47 public void testAssureSearch() throws IOException {
48 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
49 assertTrue(loc, loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
53 public void testAssureSearchEmail() throws IOException {
54 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
55 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
59 public void testAssureSearchDob() throws IOException {
60 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
61 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
62 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
63 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
64 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
65 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
68 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
69 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
70 URLConnection uc = u.openConnection();
72 uc.addRequestProperty("Cookie", cookie);
73 uc.getOutputStream().write(("search&" + query).getBytes());
74 uc.getOutputStream().flush();
76 return IOUtils.readURL(uc);
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");
86 public void testAssureFormNoCSRF() throws IOException {
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());
95 public void testAssureFormWrongCSRF() throws IOException {
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());
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);
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>"));
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>"));
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>"));
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>"));
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>"));
152 public void testAssureListingValid() throws IOException {
153 String uniqueLoc = createUniqueName();
154 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
156 String cookie = login(assureeM, TEST_PASSWORD);
157 URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
158 url.setRequestProperty("Cookie", cookie);
159 String resp = IOUtils.readURL(url);
160 resp = resp.split(Pattern.quote("</table>"))[0];
161 assertTrue(resp.contains(uniqueLoc));
165 public void testAssurerListingValid() throws IOException {
166 String uniqueLoc = createUniqueName();
167 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
169 String cookie = login(assurerM, TEST_PASSWORD);
170 URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
171 url.setRequestProperty("Cookie", cookie);
172 String resp = IOUtils.readURL(url);
173 resp = resp.split(Pattern.quote("</table>"))[1];
174 assertTrue(resp.contains(uniqueLoc));
177 private String getError(String query) throws MalformedURLException, IOException {
178 URLConnection uc = buildupAssureFormConnection(true);
179 uc.getOutputStream().write((query).getBytes());
180 uc.getOutputStream().flush();
181 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
185 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
186 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
187 URLConnection uc = u.openConnection();
188 uc.addRequestProperty("Cookie", cookie);
189 uc.setDoOutput(true);
190 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes());
192 String csrf = getCSRF(uc);
193 uc = u.openConnection();
194 uc.addRequestProperty("Cookie", cookie);
195 uc.setDoOutput(true);
197 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());