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;
11 import java.net.URLConnection;
12 import java.net.URLEncoder;
13 import java.sql.PreparedStatement;
14 import java.sql.SQLException;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.regex.Pattern;
19 import org.cacert.gigi.database.DatabaseConnection;
20 import org.cacert.gigi.testUtils.IOUtils;
21 import org.cacert.gigi.testUtils.ManagedTest;
22 import org.junit.Before;
23 import org.junit.Test;
25 public class TestAssurance extends ManagedTest {
27 private String assurerM;
29 private String assureeM;
35 private String cookie;
38 public void setup() throws IOException {
39 assurerM = createUniqueName() + "@cacert-test.org";
40 assureeM = createUniqueName() + "@cacert-test.org";
41 assurer = createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
42 assuree = createAssuranceUser("a", "c", assureeM, TEST_PASSWORD);
43 cookie = login(assurerM, TEST_PASSWORD);
48 public void testAssureSearch() throws IOException {
49 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
50 assertTrue(loc, loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
54 public void testAssureSearchEmail() throws IOException {
55 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
56 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
60 public void testAssureSearchDob() throws IOException {
61 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
62 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
63 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
64 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
65 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
66 assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
69 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
70 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
71 URLConnection uc = u.openConnection();
73 uc.addRequestProperty("Cookie", cookie);
74 uc.getOutputStream().write(("search&" + query).getBytes());
75 uc.getOutputStream().flush();
77 return IOUtils.readURL(uc);
81 public void testAssureForm() throws IOException {
82 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
87 public void testAssureFormNoCSRF() throws IOException {
89 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
90 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes());
91 uc.getOutputStream().flush();
92 assertEquals(500, uc.getResponseCode());
96 public void testAssureFormWrongCSRF() throws IOException {
98 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
99 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10&csrf=aragc").getBytes());
100 uc.getOutputStream().flush();
101 assertEquals(500, uc.getResponseCode());
105 public void testAssureFormRace() throws IOException, SQLException {
106 URLConnection uc = buildupAssureFormConnection(true);
107 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `users` SET email='changed' WHERE id=?");
108 ps.setInt(1, assuree);
110 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes());
111 uc.getOutputStream().flush();
112 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
113 assertTrue(error, !error.startsWith("</div>"));
117 public void testAssureFormFuture() throws IOException {
118 SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
119 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
120 String error = getError("date=" + year + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
121 assertTrue(error, !error.startsWith("</div>"));
125 public void testAssureFormNoLoc() throws IOException {
126 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
127 assertTrue(error, !error.startsWith("</div>"));
128 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
129 assertTrue(error, !error.startsWith("</div>"));
133 public void testAssureFormInvalDate() throws IOException {
134 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
135 assertTrue(error, !error.startsWith("</div>"));
136 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
137 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>"));
153 public void testAssureListingValid() throws IOException {
154 String uniqueLoc = createUniqueName();
155 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
157 String cookie = login(assureeM, TEST_PASSWORD);
158 URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
159 url.setRequestProperty("Cookie", cookie);
160 String resp = IOUtils.readURL(url);
161 resp = resp.split(Pattern.quote("</table>"))[0];
162 assertThat(resp, containsString(uniqueLoc));
166 public void testAssurerListingValid() throws IOException {
167 String uniqueLoc = createUniqueName();
168 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
170 String cookie = login(assurerM, TEST_PASSWORD);
171 URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
172 url.setRequestProperty("Cookie", cookie);
173 String resp = IOUtils.readURL(url);
174 resp = resp.split(Pattern.quote("</table>"))[1];
175 assertThat(resp, containsString(uniqueLoc));
178 private String getError(String query) throws MalformedURLException, IOException {
179 URLConnection uc = buildupAssureFormConnection(true);
180 uc.getOutputStream().write((query).getBytes());
181 uc.getOutputStream().flush();
182 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
186 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
187 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
188 URLConnection uc = u.openConnection();
189 uc.addRequestProperty("Cookie", cookie);
190 uc.setDoOutput(true);
191 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes());
193 String csrf = getCSRF(uc);
194 uc = u.openConnection();
195 uc.addRequestProperty("Cookie", cookie);
196 uc.setDoOutput(true);
198 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());