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.UnsupportedEncodingException;
12 import java.net.HttpURLConnection;
13 import java.net.InetSocketAddress;
15 import java.net.URLEncoder;
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
18 import java.sql.SQLException;
19 import java.util.Properties;
21 import org.cacert.gigi.DevelLauncher;
22 import org.cacert.gigi.IOUtils;
23 import org.cacert.gigi.InitTruststore;
24 import org.cacert.gigi.database.DatabaseConnection;
25 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
26 import org.cacert.gigi.util.DatabaseManager;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
31 public class ManagedTest {
32 private final String registerService = "/register";
34 private static TestEmailReciever ter;
35 private static Process gigi;
36 private static String url = "localhost:4443";
38 public static String getServerName() {
41 static Properties testProps = new Properties();
44 HttpURLConnection.setFollowRedirects(false);
48 public static void connectToServer() {
50 testProps.load(new FileInputStream("config/test.properties"));
51 if (!DatabaseConnection.isInited()) {
52 DatabaseConnection.init(testProps);
54 System.out.println("... purging Database");
55 DatabaseManager.run(new String[]{
56 testProps.getProperty("sql.driver"),
57 testProps.getProperty("sql.url"),
58 testProps.getProperty("sql.user"),
59 testProps.getProperty("sql.password")});
61 String type = testProps.getProperty("type");
62 if (type.equals("local")) {
63 url = testProps.getProperty("server");
64 String[] parts = testProps.getProperty("mail").split(":", 2);
65 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
66 Integer.parseInt(parts[1])));
69 url = "localhost:" + testProps.getProperty("serverPort");
70 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
71 DataOutputStream toGigi = new DataOutputStream(
72 gigi.getOutputStream());
73 System.out.println("... starting server");
74 Properties mainProps = new Properties();
75 mainProps.setProperty("host", "127.0.0.1");
76 mainProps.setProperty("port", testProps.getProperty("serverPort"));
77 mainProps.setProperty("emailProvider",
78 "org.cacert.gigi.email.TestEmailProvider");
79 mainProps.setProperty("emailProvider.port", "8473");
80 mainProps.setProperty("sql.driver",
81 testProps.getProperty("sql.driver"));
82 mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
84 .setProperty("sql.user", testProps.getProperty("sql.user"));
85 mainProps.setProperty("sql.password",
86 testProps.getProperty("sql.password"));
88 byte[] cacerts = Files
89 .readAllBytes(Paths.get("config/cacerts.jks"));
90 byte[] keystore = Files.readAllBytes(Paths
91 .get("config/keystore.pkcs12"));
93 DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
94 "changeit".getBytes(), mainProps, cacerts, keystore);
96 // TODO wait for ready
99 } catch (InterruptedException e) {
102 final BufferedReader br = new BufferedReader(new InputStreamReader(
103 gigi.getErrorStream()));
105 while ((line = br.readLine()) != null
106 && !line.contains("Server:main: Started")) {
107 System.err.println(line);
114 while ((line = br.readLine()) != null) {
115 System.err.println(line);
117 } catch (IOException e) {
122 System.err.println(line);
124 throw new Error("Server startup failed");
126 ter = new TestEmailReciever(
127 new InetSocketAddress("localhost", 8473));
128 } catch (IOException e) {
130 } catch (ClassNotFoundException e1) {
131 e1.printStackTrace();
132 } catch (SQLException e1) {
133 e1.printStackTrace();
138 public static void tearDownServer() {
139 String type = testProps.getProperty("type");
140 if (type.equals("local")) {
147 public void removeMails() {
151 public TestMail waitForMail() {
153 return ter.recieve();
154 } catch (InterruptedException e) {
158 public static TestEmailReciever getMailReciever() {
161 public String runRegister(String param) throws IOException {
162 HttpURLConnection uc = (HttpURLConnection) new URL("https://"
163 + getServerName() + registerService).openConnection();
164 uc.setDoOutput(true);
165 uc.getOutputStream().write(param.getBytes());
166 String d = IOUtils.readURL(uc);
169 public String fetchStartErrorMessage(String query) throws IOException {
170 String d = runRegister(query);
171 String formFail = "<div class='formError'>";
172 int idx = d.indexOf(formFail);
173 assertNotEquals(-1, idx);
174 String startError = d.substring(idx + formFail.length(), idx + 100)
179 public void registerUser(String firstName, String lastName, String email,
182 String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
183 + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
184 + "&email=" + URLEncoder.encode(email, "UTF-8")
185 + "&pword1=" + URLEncoder.encode(password, "UTF-8")
186 + "&pword2=" + URLEncoder.encode(password, "UTF-8")
187 + "&day=1&month=1&year=1910&cca_agree=1";
188 String data = fetchStartErrorMessage(query);
189 assertTrue(data, data.startsWith("</div>"));
190 } catch (UnsupportedEncodingException e) {
192 } catch (IOException e) {
196 public void createVerifiedUser(String firstName, String lastName,
197 String email, String password) {
198 registerUser(firstName, lastName, email, password);
200 TestMail tm = ter.recieve();
201 String verifyLink = tm.extractLink();
202 String[] parts = verifyLink.split("\\?");
203 URL u = new URL("https://" + getServerName() + "/verify?"
205 u.openStream().close();;
206 } catch (InterruptedException e) {
208 } catch (IOException e) {