1 package org.cacert.gigi.testUtils;
3 import java.io.BufferedReader;
4 import java.io.DataOutputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.net.InetSocketAddress;
9 import java.nio.file.Files;
10 import java.nio.file.Paths;
11 import java.util.Properties;
13 import org.cacert.gigi.DevelLauncher;
14 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
15 import org.junit.After;
16 import org.junit.AfterClass;
17 import org.junit.BeforeClass;
19 public class ManagedTest {
20 private static TestEmailReciever ter;
21 private static Process gigi;
22 private static String url = "localhost:4443";
24 public static String getServerName() {
27 static Properties testProps = new Properties();
29 public static void connectToServer() {
31 testProps.load(new FileInputStream("config/test.properties"));
32 String type = testProps.getProperty("type");
33 if (type.equals("local")) {
34 url = testProps.getProperty("server");
35 String[] parts = testProps.getProperty("mail").split(":", 2);
36 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
37 Integer.parseInt(parts[1])));
40 url = "localhost:" + testProps.getProperty("serverPort");
41 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
42 DataOutputStream toGigi = new DataOutputStream(
43 gigi.getOutputStream());
44 System.out.println("... starting server");
45 Properties mainProps = new Properties();
46 mainProps.load(new FileInputStream("config/gigi.properties"));
47 mainProps.setProperty("host", "127.0.0.1");
48 mainProps.setProperty("port", testProps.getProperty("serverPort"));
49 mainProps.setProperty("emailProvider",
50 "org.cacert.gigi.email.TestEmailProvider");
51 mainProps.setProperty("emailProvider.port", "8473");
53 byte[] cacerts = Files
54 .readAllBytes(Paths.get("config/cacerts.jks"));
55 byte[] keystore = Files.readAllBytes(Paths
56 .get("config/keystore.pkcs12"));
58 DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
59 "changeit".getBytes(), mainProps, cacerts, keystore);
61 // TODO wait for ready
64 } catch (InterruptedException e) {
67 final BufferedReader br = new BufferedReader(new InputStreamReader(
68 gigi.getErrorStream()));
70 while ((line = br.readLine()) != null
71 && !line.contains("Server:main: Started")) {
72 System.err.println(line);
79 while ((line = br.readLine()) != null) {
80 System.err.println(line);
82 } catch (IOException e) {
87 System.err.println(line);
89 throw new Error("Server startup failed");
91 ter = new TestEmailReciever(
92 new InetSocketAddress("localhost", 8473));
93 } catch (IOException e) {
99 public static void tearDownServer() {
100 String type = testProps.getProperty("type");
101 if (type.equals("local")) {
108 public void removeMails() {
112 public TestMail waitForMail() {
114 return ter.recieve();
115 } catch (InterruptedException e) {