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.testUtils.TestEmailReciever.TestMail;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
27 public class ManagedTest {
28 private final String registerService = "/register";
30 private static TestEmailReciever ter;
31 private static Process gigi;
32 private static String url = "localhost:4443";
34 public static String getServerName() {
37 static Properties testProps = new Properties();
39 public static void connectToServer() {
41 testProps.load(new FileInputStream("config/test.properties"));
42 String type = testProps.getProperty("type");
43 if (type.equals("local")) {
44 url = testProps.getProperty("server");
45 String[] parts = testProps.getProperty("mail").split(":", 2);
46 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
47 Integer.parseInt(parts[1])));
50 url = "localhost:" + testProps.getProperty("serverPort");
51 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
52 DataOutputStream toGigi = new DataOutputStream(
53 gigi.getOutputStream());
54 System.out.println("... starting server");
55 Properties mainProps = new Properties();
56 mainProps.load(new FileInputStream("config/gigi.properties"));
57 mainProps.setProperty("host", "127.0.0.1");
58 mainProps.setProperty("port", testProps.getProperty("serverPort"));
59 mainProps.setProperty("emailProvider",
60 "org.cacert.gigi.email.TestEmailProvider");
61 mainProps.setProperty("emailProvider.port", "8473");
63 byte[] cacerts = Files
64 .readAllBytes(Paths.get("config/cacerts.jks"));
65 byte[] keystore = Files.readAllBytes(Paths
66 .get("config/keystore.pkcs12"));
68 DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
69 "changeit".getBytes(), mainProps, cacerts, keystore);
71 // TODO wait for ready
74 } catch (InterruptedException e) {
77 final BufferedReader br = new BufferedReader(new InputStreamReader(
78 gigi.getErrorStream()));
80 while ((line = br.readLine()) != null
81 && !line.contains("Server:main: Started")) {
82 System.err.println(line);
89 while ((line = br.readLine()) != null) {
90 System.err.println(line);
92 } catch (IOException e) {
97 System.err.println(line);
99 throw new Error("Server startup failed");
101 ter = new TestEmailReciever(
102 new InetSocketAddress("localhost", 8473));
103 } catch (IOException e) {
109 public static void tearDownServer() {
110 String type = testProps.getProperty("type");
111 if (type.equals("local")) {
118 public void removeMails() {
122 public TestMail waitForMail() {
124 return ter.recieve();
125 } catch (InterruptedException e) {
129 public static TestEmailReciever getMailReciever() {
132 public String runRegister(String param) throws IOException {
133 HttpURLConnection uc = (HttpURLConnection) new URL("https://"
134 + getServerName() + registerService).openConnection();
135 uc.setDoOutput(true);
136 uc.getOutputStream().write(param.getBytes());
137 String d = IOUtils.readURL(uc);
140 public String fetchStartErrorMessage(String query) throws IOException {
141 String d = runRegister(query);
142 String formFail = "<div class='formError'>";
143 int idx = d.indexOf(formFail);
144 assertNotEquals(-1, idx);
145 String startError = d.substring(idx + formFail.length(), idx + 100)
150 public void registerUser(String firstName, String lastName, String email,
153 String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
154 + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
155 + "&email=" + URLEncoder.encode(email, "UTF-8")
156 + "&pword1=" + URLEncoder.encode(password, "UTF-8")
157 + "&pword2=" + URLEncoder.encode(password, "UTF-8")
158 + "&day=1&month=1&year=1910&cca_agree=1";
159 String data = fetchStartErrorMessage(query);
160 assertTrue(data, data.startsWith("</div>"));
161 } catch (UnsupportedEncodingException e) {
163 } catch (IOException e) {
167 public void createVerifiedUser(String firstName, String lastName,
168 String email, String password) {
169 registerUser(firstName, lastName, email, password);
171 TestMail tm = ter.recieve();
172 String verifyLink = tm.extractLink();
173 String[] parts = verifyLink.split("\\?");
174 URL u = new URL("https://" + getServerName() + "/verify" + parts[1]);
175 u.openStream().close();;
176 } catch (InterruptedException e) {
178 } catch (IOException e) {