]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
769767cd76f41b1ff329fb17cf150d429aa0fa1e
[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.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.testUtils.IOUtils;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.junit.Before;
18 import org.junit.Test;
19
20 import static org.junit.Assert.*;
21
22 public class TestAssurance extends ManagedTest {
23         private String assurerM;
24         private String assureeM;
25         private int assurer;
26         private int assuree;
27         private String cookie;
28         @Before
29         public void setup() throws IOException {
30                 assurerM = createUniqueName() + "@cacert-test.org";
31                 assureeM = createUniqueName() + "@cacert-test.org";
32                 assurer = createAssuranceUser("a", "b", assurerM, "xvXV.1");
33                 assuree = createAssuranceUser("a", "c", assureeM, "xvXV.1");
34                 cookie = login(assurerM, "xvXV.1");
35
36         }
37         @Test
38         public void testAssureSearch() throws IOException {
39                 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
40                                 + "&day=1&month=1&year=1910");
41                 assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
42         }
43
44         @Test
45         public void testAssureSearchEmail() throws IOException {
46                 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8")
47                                 + "&day=1&month=1&year=1910");
48                 assertNull(loc);
49         }
50         @Test
51         public void testAssureSearchDob() throws IOException {
52                 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
53                                 + "&day=2&month=1&year=1910");
54                 assertNull(loc);
55                 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
56                                 + "&day=1&month=2&year=1910");
57                 assertNull(loc);
58                 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8")
59                                 + "&day=1&month=1&year=1911");
60                 assertNull(loc);
61         }
62         private String search(String query) throws MalformedURLException,
63                         IOException, UnsupportedEncodingException {
64                 URL u = new URL("https://" + getServerName() + AssurePage.PATH);
65                 URLConnection uc = u.openConnection();
66                 uc.setDoOutput(true);
67                 uc.addRequestProperty("Cookie", cookie);
68                 uc.getOutputStream().write((query).getBytes());
69                 uc.getOutputStream().flush();
70
71                 String loc = uc.getHeaderField("Location");
72                 return loc;
73         }
74         @Test
75         public void testAssureForm() throws IOException {
76                 String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
77                 assertTrue(error, error.startsWith("</div>"));
78         }
79
80         @Test
81         public void testAssureFormRace() throws IOException, SQLException {
82                 URLConnection uc = buildupAssureFormConnection();
83                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
84                                 "UPDATE `users` SET email='changed' WHERE id=?");
85                 ps.setInt(1, assuree);
86                 ps.execute();
87                 uc.getOutputStream()
88                                 .write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10")
89                                                 .getBytes());
90                 uc.getOutputStream().flush();
91                 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
92                 assertTrue(error, !error.startsWith("</div>"));
93         }
94         @Test
95         public void testAssureFormFuture() throws IOException {
96                 SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
97                 int year = Integer.parseInt(sdf.format(new Date(System
98                                 .currentTimeMillis()))) + 2;
99                 String error = getError("date="
100                                 + year
101                                 + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
102                 assertTrue(error, !error.startsWith("</div>"));
103         }
104         @Test
105         public void testAssureFormNoLoc() throws IOException {
106                 String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
107                 assertTrue(error, !error.startsWith("</div>"));
108                 error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
109                 assertTrue(error, !error.startsWith("</div>"));
110         }
111
112         @Test
113         public void testAssureFormInvalDate() throws IOException {
114                 String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
115                 assertTrue(error, !error.startsWith("</div>"));
116                 error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
117                 assertTrue(error, !error.startsWith("</div>"));
118         }
119         @Test
120         public void testAssureFormBoxes() throws IOException {
121                 String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
122                 assertTrue(error, !error.startsWith("</div>"));
123                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
124                 assertTrue(error, !error.startsWith("</div>"));
125                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
126                 assertTrue(error, !error.startsWith("</div>"));
127                 error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
128                 assertTrue(error, !error.startsWith("</div>"));
129         }
130         private String getError(String query) throws MalformedURLException,
131                         IOException {
132                 URLConnection uc = buildupAssureFormConnection();
133                 uc.getOutputStream().write((query).getBytes());
134                 uc.getOutputStream().flush();
135                 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
136                 return error;
137         }
138         private URLConnection buildupAssureFormConnection()
139                         throws MalformedURLException, IOException {
140                 URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/"
141                                 + assuree);
142                 URLConnection uc = u.openConnection();
143                 uc.addRequestProperty("Cookie", cookie);
144                 String csrf = getCSRF(uc);
145                 uc = u.openConnection();
146                 uc.addRequestProperty("Cookie", cookie);
147                 uc.setDoOutput(true);
148                 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());
149                 return uc;
150         }
151
152 }