]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
Add points parameter to tamper with.
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestAssurance.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.net.URLConnection;
8 import java.net.URLEncoder;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11
12 import org.cacert.gigi.IOUtils;
13 import org.cacert.gigi.testUtils.ManagedTest;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import static org.junit.Assert.*;
18
19 public class TestAssurance extends ManagedTest {
20         private String assurerM;
21         private String assureeM;
22         private int assurer;
23         private int assuree;
24         private String cookie;
25         @Before
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");
32
33         }
34         @Test
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));
39         }
40
41         @Test
42         public void testAssureSearchEmail() throws IOException {
43                 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8")
44                                 + "&day=1&month=1&year=1910");
45                 assertNull(loc);
46         }
47         @Test
48         public void testAssureSearchDob() throws IOException {
49                 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
50                                 + "&day=2&month=1&year=1910");
51                 assertNull(loc);
52                 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
53                                 + "&day=1&month=2&year=1910");
54                 assertNull(loc);
55                 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
56                                 + "&day=1&month=1&year=1911");
57                 assertNull(loc);
58         }
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();
63                 uc.setDoOutput(true);
64                 uc.addRequestProperty("Cookie", cookie);
65                 uc.getOutputStream().write((query).getBytes());
66                 uc.getOutputStream().flush();
67
68                 String loc = uc.getHeaderField("Location");
69                 return loc;
70         }
71         @Test
72         public void testAssureForm() throws IOException {
73                 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
74                 assertTrue(error, error.startsWith("</div>"));
75         }
76         @Test
77         public void testAssureFormFuture() 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="
82                                 + year
83                                 + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
84                 assertTrue(error, !error.startsWith("</div>"));
85         }
86         @Test
87         public void testAssureFormNoLoc() throws IOException {
88                 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
89                 assertTrue(error, !error.startsWith("</div>"));
90                 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
91                 assertTrue(error, !error.startsWith("</div>"));
92         }
93
94         @Test
95         public void testAssureFormInvalDate() throws IOException {
96                 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
97                 assertTrue(error, !error.startsWith("</div>"));
98                 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
99                 assertTrue(error, !error.startsWith("</div>"));
100         }
101         @Test
102         public void testAssureFormBoxes() throws IOException {
103                 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
104                 assertTrue(error, !error.startsWith("</div>"));
105                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
106                 assertTrue(error, !error.startsWith("</div>"));
107                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
108                 assertTrue(error, !error.startsWith("</div>"));
109                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
110                 assertTrue(error, !error.startsWith("</div>"));
111         }
112         private String getError(String query) throws MalformedURLException,
113                         IOException {
114                 URLConnection uc = buildupAssureFormConnection();
115                 uc.getOutputStream().write((query).getBytes());
116                 uc.getOutputStream().flush();
117                 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
118                 return error;
119         }
120         private URLConnection buildupAssureFormConnection()
121                         throws MalformedURLException, IOException {
122                 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/"
123                                 + assuree);
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);
130                 return uc;
131         }
132
133 }