]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ManagedTest.java
Implement purging of Database before testcases.
[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.sql.SQLException;
19 import java.util.Properties;
20
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;
30
31 public class ManagedTest {
32         private final String registerService = "/register";
33
34         private static TestEmailReciever ter;
35         private static Process gigi;
36         private static String url = "localhost:4443";
37
38         public static String getServerName() {
39                 return url;
40         }
41         static Properties testProps = new Properties();
42         static {
43                 InitTruststore.run();
44                 HttpURLConnection.setFollowRedirects(false);
45         }
46
47         @BeforeClass
48         public static void connectToServer() {
49                 try {
50                         testProps.load(new FileInputStream("config/test.properties"));
51                         if (!DatabaseConnection.isInited()) {
52                                 DatabaseConnection.init(testProps);
53                         }
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")});
60
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])));
67                                 return;
68                         }
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"));
83                         mainProps
84                                         .setProperty("sql.user", testProps.getProperty("sql.user"));
85                         mainProps.setProperty("sql.password",
86                                         testProps.getProperty("sql.password"));
87
88                         byte[] cacerts = Files
89                                         .readAllBytes(Paths.get("config/cacerts.jks"));
90                         byte[] keystore = Files.readAllBytes(Paths
91                                         .get("config/keystore.pkcs12"));
92
93                         DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
94                                         "changeit".getBytes(), mainProps, cacerts, keystore);
95                         toGigi.flush();
96                         // TODO wait for ready
97                         try {
98                                 Thread.sleep(3000);
99                         } catch (InterruptedException e) {
100                                 e.printStackTrace();
101                         }
102                         final BufferedReader br = new BufferedReader(new InputStreamReader(
103                                         gigi.getErrorStream()));
104                         String line;
105                         while ((line = br.readLine()) != null
106                                         && !line.contains("Server:main: Started")) {
107                                 System.err.println(line);
108                         }
109                         new Thread() {
110                                 @Override
111                                 public void run() {
112                                         String line;
113                                         try {
114                                                 while ((line = br.readLine()) != null) {
115                                                         System.err.println(line);
116                                                 }
117                                         } catch (IOException e) {
118                                                 e.printStackTrace();
119                                         }
120                                 }
121                         }.start();
122                         System.err.println(line);
123                         if (line == null) {
124                                 throw new Error("Server startup failed");
125                         }
126                         ter = new TestEmailReciever(
127                                         new InetSocketAddress("localhost", 8473));
128                 } catch (IOException e) {
129                         throw new Error(e);
130                 } catch (ClassNotFoundException e1) {
131                         e1.printStackTrace();
132                 } catch (SQLException e1) {
133                         e1.printStackTrace();
134                 }
135
136         }
137         @AfterClass
138         public static void tearDownServer() {
139                 String type = testProps.getProperty("type");
140                 if (type.equals("local")) {
141                         return;
142                 }
143                 gigi.destroy();
144         }
145
146         @After
147         public void removeMails() {
148                 ter.reset();
149         }
150
151         public TestMail waitForMail() {
152                 try {
153                         return ter.recieve();
154                 } catch (InterruptedException e) {
155                         throw new Error(e);
156                 }
157         }
158         public static TestEmailReciever getMailReciever() {
159                 return ter;
160         }
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);
167                 return d;
168         }
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)
175                                 .trim();
176                 return startError;
177         }
178
179         public void registerUser(String firstName, String lastName, String email,
180                         String password) {
181                 try {
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) {
191                         throw new Error(e);
192                 } catch (IOException e) {
193                         throw new Error(e);
194                 }
195         }
196         public void createVerifiedUser(String firstName, String lastName,
197                         String email, String password) {
198                 registerUser(firstName, lastName, email, password);
199                 try {
200                         TestMail tm = ter.recieve();
201                         String verifyLink = tm.extractLink();
202                         String[] parts = verifyLink.split("\\?");
203                         URL u = new URL("https://" + getServerName() + "/verify?"
204                                         + parts[1]);
205                         u.openStream().close();;
206                 } catch (InterruptedException e) {
207                         throw new Error(e);
208                 } catch (IOException e) {
209                         throw new Error(e);
210                 }
211         }
212 }