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.util.Properties;
20 import org.cacert.gigi.DevelLauncher;
21 import org.cacert.gigi.IOUtils;
22 import org.cacert.gigi.InitTruststore;
23 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
28 public class ManagedTest {
29 private final String registerService = "/register";
31 private static TestEmailReciever ter;
32 private static Process gigi;
33 private static String url = "localhost:4443";
35 public static String getServerName() {
38 static Properties testProps = new Properties();
41 HttpURLConnection.setFollowRedirects(false);
45 public static void connectToServer() {
47 testProps.load(new FileInputStream("config/test.properties"));
48 String type = testProps.getProperty("type");
49 if (type.equals("local")) {
50 url = testProps.getProperty("server");
51 String[] parts = testProps.getProperty("mail").split(":", 2);
52 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
53 Integer.parseInt(parts[1])));
56 url = "localhost:" + testProps.getProperty("serverPort");
57 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
58 DataOutputStream toGigi = new DataOutputStream(
59 gigi.getOutputStream());
60 System.out.println("... starting server");
61 Properties mainProps = new Properties();
62 mainProps.load(new FileInputStream("config/gigi.properties"));
63 mainProps.setProperty("host", "127.0.0.1");
64 mainProps.setProperty("port", testProps.getProperty("serverPort"));
65 mainProps.setProperty("emailProvider",
66 "org.cacert.gigi.email.TestEmailProvider");
67 mainProps.setProperty("emailProvider.port", "8473");
69 byte[] cacerts = Files
70 .readAllBytes(Paths.get("config/cacerts.jks"));
71 byte[] keystore = Files.readAllBytes(Paths
72 .get("config/keystore.pkcs12"));
74 DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
75 "changeit".getBytes(), mainProps, cacerts, keystore);
77 // TODO wait for ready
80 } catch (InterruptedException e) {
83 final BufferedReader br = new BufferedReader(new InputStreamReader(
84 gigi.getErrorStream()));
86 while ((line = br.readLine()) != null
87 && !line.contains("Server:main: Started")) {
88 System.err.println(line);
95 while ((line = br.readLine()) != null) {
96 System.err.println(line);
98 } catch (IOException e) {
103 System.err.println(line);
105 throw new Error("Server startup failed");
107 ter = new TestEmailReciever(
108 new InetSocketAddress("localhost", 8473));
109 } catch (IOException e) {
115 public static void tearDownServer() {
116 String type = testProps.getProperty("type");
117 if (type.equals("local")) {
124 public void removeMails() {
128 public TestMail waitForMail() {
130 return ter.recieve();
131 } catch (InterruptedException e) {
135 public static TestEmailReciever getMailReciever() {
138 public String runRegister(String param) throws IOException {
139 HttpURLConnection uc = (HttpURLConnection) new URL("https://"
140 + getServerName() + registerService).openConnection();
141 uc.setDoOutput(true);
142 uc.getOutputStream().write(param.getBytes());
143 String d = IOUtils.readURL(uc);
146 public String fetchStartErrorMessage(String query) throws IOException {
147 String d = runRegister(query);
148 String formFail = "<div class='formError'>";
149 int idx = d.indexOf(formFail);
150 assertNotEquals(-1, idx);
151 String startError = d.substring(idx + formFail.length(), idx + 100)
156 public void registerUser(String firstName, String lastName, String email,
159 String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
160 + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
161 + "&email=" + URLEncoder.encode(email, "UTF-8")
162 + "&pword1=" + URLEncoder.encode(password, "UTF-8")
163 + "&pword2=" + URLEncoder.encode(password, "UTF-8")
164 + "&day=1&month=1&year=1910&cca_agree=1";
165 String data = fetchStartErrorMessage(query);
166 assertTrue(data, data.startsWith("</div>"));
167 } catch (UnsupportedEncodingException e) {
169 } catch (IOException e) {
173 public void createVerifiedUser(String firstName, String lastName,
174 String email, String password) {
175 registerUser(firstName, lastName, email, password);
177 TestMail tm = ter.recieve();
178 String verifyLink = tm.extractLink();
179 String[] parts = verifyLink.split("\\?");
180 URL u = new URL("https://" + getServerName() + "/verify?"
182 u.openStream().close();;
183 } catch (InterruptedException e) {
185 } catch (IOException e) {