]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ManagedTest.java
Extend test to mbox verifications
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
1 package org.cacert.gigi.testUtils;
2
3 import static org.junit.Assert.assertNotEquals;
4 import static org.junit.Assert.assertTrue;
5
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;
14 import java.net.URL;
15 import java.net.URLEncoder;
16 import java.nio.file.Files;
17 import java.nio.file.Paths;
18 import java.util.Properties;
19
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;
26
27 public class ManagedTest {
28         private final String registerService = "/register";
29
30         private static TestEmailReciever ter;
31         private static Process gigi;
32         private static String url = "localhost:4443";
33
34         public static String getServerName() {
35                 return url;
36         }
37         static Properties testProps = new Properties();
38         @BeforeClass
39         public static void connectToServer() {
40                 try {
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])));
48                                 return;
49                         }
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");
62
63                         byte[] cacerts = Files
64                                         .readAllBytes(Paths.get("config/cacerts.jks"));
65                         byte[] keystore = Files.readAllBytes(Paths
66                                         .get("config/keystore.pkcs12"));
67
68                         DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
69                                         "changeit".getBytes(), mainProps, cacerts, keystore);
70                         toGigi.flush();
71                         // TODO wait for ready
72                         try {
73                                 Thread.sleep(3000);
74                         } catch (InterruptedException e) {
75                                 e.printStackTrace();
76                         }
77                         final BufferedReader br = new BufferedReader(new InputStreamReader(
78                                         gigi.getErrorStream()));
79                         String line;
80                         while ((line = br.readLine()) != null
81                                         && !line.contains("Server:main: Started")) {
82                                 System.err.println(line);
83                         }
84                         new Thread() {
85                                 @Override
86                                 public void run() {
87                                         String line;
88                                         try {
89                                                 while ((line = br.readLine()) != null) {
90                                                         System.err.println(line);
91                                                 }
92                                         } catch (IOException e) {
93                                                 e.printStackTrace();
94                                         }
95                                 }
96                         }.start();
97                         System.err.println(line);
98                         if (line == null) {
99                                 throw new Error("Server startup failed");
100                         }
101                         ter = new TestEmailReciever(
102                                         new InetSocketAddress("localhost", 8473));
103                 } catch (IOException e) {
104                         throw new Error(e);
105                 }
106
107         }
108         @AfterClass
109         public static void tearDownServer() {
110                 String type = testProps.getProperty("type");
111                 if (type.equals("local")) {
112                         return;
113                 }
114                 gigi.destroy();
115         }
116
117         @After
118         public void removeMails() {
119                 ter.reset();
120         }
121
122         public TestMail waitForMail() {
123                 try {
124                         return ter.recieve();
125                 } catch (InterruptedException e) {
126                         throw new Error(e);
127                 }
128         }
129         public static TestEmailReciever getMailReciever() {
130                 return ter;
131         }
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);
138                 return d;
139         }
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)
146                                 .trim();
147                 return startError;
148         }
149
150         public void registerUser(String firstName, String lastName, String email,
151                         String password) {
152                 try {
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) {
162                         throw new Error(e);
163                 } catch (IOException e) {
164                         throw new Error(e);
165                 }
166         }
167         public void createVerifiedUser(String firstName, String lastName,
168                         String email, String password) {
169                 registerUser(firstName, lastName, email, password);
170                 try {
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) {
177                         throw new Error(e);
178                 } catch (IOException e) {
179                         throw new Error(e);
180                 }
181         }
182 }