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.endsWith(AssurePage.PATH + "/" + assuree));
53 public void testAssureSearchEmail() throws IOException {
54 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
59 public void testAssureSearchDob() throws IOException {
60 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
62 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
64 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
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((query).getBytes());
74 uc.getOutputStream().flush();
76 String loc = uc.getHeaderField("Location");
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 assertTrue(resp.contains(uniqueLoc));
167 public void testAssurerListingValid() throws IOException {
168 String uniqueLoc = createUniqueName();
169 String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
171 String cookie = login(assurerM, TEST_PASSWORD);
172 URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
173 url.setRequestProperty("Cookie", cookie);
174 String resp = IOUtils.readURL(url);
175 resp = resp.split(Pattern.quote("</table>"))[1];
176 assertTrue(resp.contains(uniqueLoc));
179 private String getError(String query) throws MalformedURLException, IOException {
180 URLConnection uc = buildupAssureFormConnection(true);
181 uc.getOutputStream().write((query).getBytes());
182 uc.getOutputStream().flush();
183 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
187 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
188 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/" + assuree);
189 URLConnection uc = u.openConnection();
190 uc.addRequestProperty("Cookie", cookie);
191 String csrf = getCSRF(uc);
192 uc = u.openConnection();
193 uc.addRequestProperty("Cookie", cookie);
194 uc.setDoOutput(true);
196 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());