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.text.SimpleDateFormat;
10 import java.util.Date;
12 import org.cacert.gigi.IOUtils;
13 import org.cacert.gigi.testUtils.ManagedTest;
14 import org.junit.Before;
15 import org.junit.Test;
17 import static org.junit.Assert.*;
19 public class TestAssurance extends ManagedTest {
20 private String assurerM;
21 private String assureeM;
24 private String cookie;
26 public void setup() throws IOException {
27 assurerM = createUniqueName() + "@cacert-test.org";
28 assureeM = createUniqueName() + "@cacert-test.org";
29 assurer = createAssuranceUser("a", "b", assurerM, "xvXV.1");
30 assuree = createAssuranceUser("a", "c", assureeM, "xvXV.1");
31 cookie = login(assurerM, "xvXV.1");
35 public void testAssureSearch() throws IOException {
36 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
37 + "&day=1&month=1&year=1910");
38 assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
42 public void testAssureSearchEmail() throws IOException {
43 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8")
44 + "&day=1&month=1&year=1910");
48 public void testAssureSearchDob() throws IOException {
49 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
50 + "&day=2&month=1&year=1910");
52 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
53 + "&day=1&month=2&year=1910");
55 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
56 + "&day=1&month=1&year=1911");
59 private String search(String query) throws MalformedURLException,
60 IOException, UnsupportedEncodingException {
61 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
62 URLConnection uc = u.openConnection();
64 uc.addRequestProperty("Cookie", cookie);
65 uc.getOutputStream().write((query).getBytes());
66 uc.getOutputStream().flush();
68 String loc = uc.getHeaderField("Location");
72 public void testAssureForm() throws IOException {
73 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1");
74 assertTrue(error, error.startsWith("</div>"));
77 public void testAssureFormFufure() throws IOException {
78 SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
79 int year = Integer.parseInt(sdf.format(new Date(System
80 .currentTimeMillis()))) + 2;
81 String error = getError("date="
83 + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1");
84 assertTrue(error, !error.startsWith("</div>"));
87 public void testAssureFormNoLoc() throws IOException {
88 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1");
89 assertTrue(error, !error.startsWith("</div>"));
90 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1");
91 assertTrue(error, !error.startsWith("</div>"));
95 public void testAssureFormInvalDate() throws IOException {
96 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1");
97 assertTrue(error, !error.startsWith("</div>"));
98 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1");
99 assertTrue(error, !error.startsWith("</div>"));
102 public void testAssureFormBoxes() throws IOException {
103 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1");
104 assertTrue(error, !error.startsWith("</div>"));
105 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1");
106 assertTrue(error, !error.startsWith("</div>"));
107 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1");
108 assertTrue(error, !error.startsWith("</div>"));
109 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z");
110 assertTrue(error, !error.startsWith("</div>"));
112 private String getError(String query) throws MalformedURLException,
114 URLConnection uc = buildupAssureFormConnection();
115 uc.getOutputStream().write((query).getBytes());
116 uc.getOutputStream().flush();
117 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
120 private URLConnection buildupAssureFormConnection()
121 throws MalformedURLException, IOException {
122 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/"
124 URLConnection uc = u.openConnection();
125 uc.addRequestProperty("Cookie", cookie);
126 uc.getInputStream();// request form
127 uc = u.openConnection();
128 uc.addRequestProperty("Cookie", cookie);
129 uc.setDoOutput(true);