1 package org.cacert.gigi.testUtils;
3 import static org.junit.Assert.assertNotEquals;
4 import static org.junit.Assert.assertTrue;
6 import java.io.BufferedReader;
7 import java.io.DataOutputStream;
8 import java.io.FileInputStream;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.io.UnsupportedEncodingException;
13 import java.net.HttpURLConnection;
14 import java.net.InetSocketAddress;
16 import java.net.URLConnection;
17 import java.net.URLEncoder;
18 import java.nio.file.Files;
19 import java.nio.file.Paths;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.Properties;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import org.cacert.gigi.DevelLauncher;
28 import org.cacert.gigi.database.DatabaseConnection;
29 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
30 import org.cacert.gigi.util.DatabaseManager;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
35 public class ManagedTest {
36 private final String registerService = "/register";
38 private static TestEmailReciever ter;
39 private static Process gigi;
40 private static String url = "localhost:4443";
42 public static String getServerName() {
46 static Properties testProps = new Properties();
49 HttpURLConnection.setFollowRedirects(false);
53 public static void connectToServer() {
55 testProps.load(new FileInputStream("config/test.properties"));
56 if (!DatabaseConnection.isInited()) {
57 DatabaseConnection.init(testProps);
59 System.out.println("... purging Database");
60 DatabaseManager.run(new String[] { testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"),
61 testProps.getProperty("sql.user"), testProps.getProperty("sql.password") });
63 String type = testProps.getProperty("type");
64 if (type.equals("local")) {
65 url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
66 String[] parts = testProps.getProperty("mail").split(":", 2);
67 ter = new TestEmailReciever(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
70 url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
71 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
72 DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
73 System.out.println("... starting server");
74 Properties mainProps = new Properties();
75 mainProps.setProperty("host", "127.0.0.1");
76 mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
77 mainProps.setProperty("name.www", testProps.getProperty("name.www"));
78 mainProps.setProperty("name.static", testProps.getProperty("name.static"));
80 mainProps.setProperty("port", testProps.getProperty("serverPort"));
81 mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
82 mainProps.setProperty("emailProvider.port", "8473");
83 mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
84 mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
85 mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
86 mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
88 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
89 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
91 DevelLauncher.writeGigiConfig(toGigi, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts,
95 final BufferedReader br = new BufferedReader(new InputStreamReader(gigi.getErrorStream()));
97 while ((line = br.readLine()) != null && !line.contains("Server:main: Started")) {
104 while ((line = br.readLine()) != null) {
105 System.err.println(line);
107 } catch (IOException e) {
113 throw new Error("Server startup failed");
115 ter = new TestEmailReciever(new InetSocketAddress("localhost", 8473));
116 } catch (IOException e) {
118 } catch (ClassNotFoundException e1) {
119 e1.printStackTrace();
120 } catch (SQLException e1) {
121 e1.printStackTrace();
127 public static void tearDownServer() {
128 String type = testProps.getProperty("type");
130 if (type.equals("local")) {
137 public void removeMails() {
141 public TestMail waitForMail() {
143 return ter.recieve();
144 } catch (InterruptedException e) {
149 public static TestEmailReciever getMailReciever() {
153 public String runRegister(String param) throws IOException {
154 URL regist = new URL("https://" + getServerName() + registerService);
155 HttpURLConnection uc = (HttpURLConnection) regist.openConnection();
156 HttpURLConnection csrfConn = (HttpURLConnection) regist.openConnection();
158 String headerField = csrfConn.getHeaderField("Set-Cookie");
159 headerField = headerField.substring(0, headerField.indexOf(';'));
161 String csrf = getCSRF(csrfConn);
162 uc.addRequestProperty("Cookie", headerField);
163 uc.setDoOutput(true);
164 uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes());
165 String d = IOUtils.readURL(uc);
169 public String fetchStartErrorMessage(String d) throws IOException {
170 String formFail = "<div class='formError'>";
171 int idx = d.indexOf(formFail);
172 assertNotEquals(-1, idx);
173 String startError = d.substring(idx + formFail.length(), idx + 100).trim();
177 public void registerUser(String firstName, String lastName, String email, String password) {
179 String query = "fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname="
180 + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1="
181 + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8")
182 + "&day=1&month=1&year=1910&cca_agree=1";
183 String data = fetchStartErrorMessage(runRegister(query));
184 assertTrue(data, data.startsWith("</div>"));
185 } catch (UnsupportedEncodingException e) {
187 } catch (IOException e) {
192 public int createVerifiedUser(String firstName, String lastName, String email, String password) {
193 registerUser(firstName, lastName, email, password);
195 TestMail tm = ter.recieve();
196 String verifyLink = tm.extractLink();
197 String[] parts = verifyLink.split("\\?");
198 URL u = new URL("https://" + getServerName() + "/verify?" + parts[1]);
199 u.openStream().close();
201 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM users where email=?");
202 ps.setString(1, email);
203 ResultSet rs = ps.executeQuery();
208 } catch (InterruptedException e) {
210 } catch (IOException e) {
212 } catch (SQLException e) {
218 * Creates a new user with 100 Assurance points given by an (invalid)
229 * @return a new userid.
231 public int createAssuranceUser(String firstName, String lastName, String email, String password) {
232 int uid = createVerifiedUser(firstName, lastName, email, password);
234 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
235 "INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?");
239 ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'");
244 } catch (SQLException e) {
250 static int count = 0;
252 public String createUniqueName() {
253 return "test" + System.currentTimeMillis() + "a" + (count++);
256 public String login(String email, String pw) throws IOException {
257 URL u = new URL("https://" + getServerName() + "/login");
258 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
259 huc.setDoOutput(true);
260 OutputStream os = huc.getOutputStream();
261 String data = "username=" + URLEncoder.encode(email, "UTF-8") + "&password=" + URLEncoder.encode(pw, "UTF-8");
262 os.write(data.getBytes());
264 String headerField = huc.getHeaderField("Set-Cookie");
265 headerField = headerField.substring(0, headerField.indexOf(';'));
269 public String getCSRF(URLConnection u) throws IOException {
270 String content = IOUtils.readURL(u);
271 Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
272 Matcher m = p.matcher(content);
274 throw new Error("No CSRF Token");