]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ManagedTest.java
Create test method to create assurer (stub) + add id return.
[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.PreparedStatement;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.util.Properties;
22
23 import org.cacert.gigi.DevelLauncher;
24 import org.cacert.gigi.IOUtils;
25 import org.cacert.gigi.InitTruststore;
26 import org.cacert.gigi.database.DatabaseConnection;
27 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
28 import org.cacert.gigi.util.DatabaseManager;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32
33 public class ManagedTest {
34         private final String registerService = "/register";
35
36         private static TestEmailReciever ter;
37         private static Process gigi;
38         private static String url = "localhost:4443";
39
40         public static String getServerName() {
41                 return url;
42         }
43         static Properties testProps = new Properties();
44         static {
45                 InitTruststore.run();
46                 HttpURLConnection.setFollowRedirects(false);
47         }
48
49         @BeforeClass
50         public static void connectToServer() {
51                 try {
52                         testProps.load(new FileInputStream("config/test.properties"));
53                         if (!DatabaseConnection.isInited()) {
54                                 DatabaseConnection.init(testProps);
55                         }
56                         System.out.println("... purging Database");
57                         DatabaseManager.run(new String[]{
58                                         testProps.getProperty("sql.driver"),
59                                         testProps.getProperty("sql.url"),
60                                         testProps.getProperty("sql.user"),
61                                         testProps.getProperty("sql.password")});
62
63                         String type = testProps.getProperty("type");
64                         if (type.equals("local")) {
65                                 url = testProps.getProperty("server");
66                                 String[] parts = testProps.getProperty("mail").split(":", 2);
67                                 ter = new TestEmailReciever(new InetSocketAddress(parts[0],
68                                                 Integer.parseInt(parts[1])));
69                                 return;
70                         }
71                         url = "localhost:" + testProps.getProperty("serverPort");
72                         gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
73                         DataOutputStream toGigi = new DataOutputStream(
74                                         gigi.getOutputStream());
75                         System.out.println("... starting server");
76                         Properties mainProps = new Properties();
77                         mainProps.setProperty("host", "127.0.0.1");
78                         mainProps.setProperty("port", testProps.getProperty("serverPort"));
79                         mainProps.setProperty("emailProvider",
80                                         "org.cacert.gigi.email.TestEmailProvider");
81                         mainProps.setProperty("emailProvider.port", "8473");
82                         mainProps.setProperty("sql.driver",
83                                         testProps.getProperty("sql.driver"));
84                         mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
85                         mainProps
86                                         .setProperty("sql.user", testProps.getProperty("sql.user"));
87                         mainProps.setProperty("sql.password",
88                                         testProps.getProperty("sql.password"));
89
90                         byte[] cacerts = Files
91                                         .readAllBytes(Paths.get("config/cacerts.jks"));
92                         byte[] keystore = Files.readAllBytes(Paths
93                                         .get("config/keystore.pkcs12"));
94
95                         DevelLauncher.writeGigiConfig(toGigi, new byte[]{},
96                                         "changeit".getBytes(), mainProps, cacerts, keystore);
97                         toGigi.flush();
98                         // TODO wait for ready
99                         try {
100                                 Thread.sleep(3000);
101                         } catch (InterruptedException e) {
102                                 e.printStackTrace();
103                         }
104                         final BufferedReader br = new BufferedReader(new InputStreamReader(
105                                         gigi.getErrorStream()));
106                         String line;
107                         while ((line = br.readLine()) != null
108                                         && !line.contains("Server:main: Started")) {
109                                 System.err.println(line);
110                         }
111                         new Thread() {
112                                 @Override
113                                 public void run() {
114                                         String line;
115                                         try {
116                                                 while ((line = br.readLine()) != null) {
117                                                         System.err.println(line);
118                                                 }
119                                         } catch (IOException e) {
120                                                 e.printStackTrace();
121                                         }
122                                 }
123                         }.start();
124                         System.err.println(line);
125                         if (line == null) {
126                                 throw new Error("Server startup failed");
127                         }
128                         ter = new TestEmailReciever(
129                                         new InetSocketAddress("localhost", 8473));
130                 } catch (IOException e) {
131                         throw new Error(e);
132                 } catch (ClassNotFoundException e1) {
133                         e1.printStackTrace();
134                 } catch (SQLException e1) {
135                         e1.printStackTrace();
136                 }
137
138         }
139         @AfterClass
140         public static void tearDownServer() {
141                 String type = testProps.getProperty("type");
142                 if (type.equals("local")) {
143                         return;
144                 }
145                 gigi.destroy();
146         }
147
148         @After
149         public void removeMails() {
150                 ter.reset();
151         }
152
153         public TestMail waitForMail() {
154                 try {
155                         return ter.recieve();
156                 } catch (InterruptedException e) {
157                         throw new Error(e);
158                 }
159         }
160         public static TestEmailReciever getMailReciever() {
161                 return ter;
162         }
163         public String runRegister(String param) throws IOException {
164                 HttpURLConnection uc = (HttpURLConnection) new URL("https://"
165                                 + getServerName() + registerService).openConnection();
166                 uc.setDoOutput(true);
167                 uc.getOutputStream().write(param.getBytes());
168                 String d = IOUtils.readURL(uc);
169                 return d;
170         }
171         public String fetchStartErrorMessage(String query) throws IOException {
172                 String d = runRegister(query);
173                 String formFail = "<div class='formError'>";
174                 int idx = d.indexOf(formFail);
175                 assertNotEquals(-1, idx);
176                 String startError = d.substring(idx + formFail.length(), idx + 100)
177                                 .trim();
178                 return startError;
179         }
180
181         public void registerUser(String firstName, String lastName, String email,
182                         String password) {
183                 try {
184                         String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
185                                         + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
186                                         + "&email=" + URLEncoder.encode(email, "UTF-8")
187                                         + "&pword1=" + URLEncoder.encode(password, "UTF-8")
188                                         + "&pword2=" + URLEncoder.encode(password, "UTF-8")
189                                         + "&day=1&month=1&year=1910&cca_agree=1";
190                         String data = fetchStartErrorMessage(query);
191                         assertTrue(data, data.startsWith("</div>"));
192                 } catch (UnsupportedEncodingException e) {
193                         throw new Error(e);
194                 } catch (IOException e) {
195                         throw new Error(e);
196                 }
197         }
198         public int createVerifiedUser(String firstName, String lastName,
199                         String email, String password) {
200                 registerUser(firstName, lastName, email, password);
201                 try {
202                         TestMail tm = ter.recieve();
203                         String verifyLink = tm.extractLink();
204                         String[] parts = verifyLink.split("\\?");
205                         URL u = new URL("https://" + getServerName() + "/verify?"
206                                         + parts[1]);
207                         u.openStream().close();;
208                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
209                                         "SELECT id FROM users where email=?");
210                         ps.setString(1, email);
211                         ResultSet rs = ps.executeQuery();
212                         if (rs.next()) {
213                                 return rs.getInt(1);
214                         }
215                         throw new Error();
216                 } catch (InterruptedException e) {
217                         throw new Error(e);
218                 } catch (IOException e) {
219                         throw new Error(e);
220                 } catch (SQLException e) {
221                         throw new Error(e);
222                 }
223         }
224         public int createAssuranceUser(String firstName, String lastName,
225                         String email, String password) {
226                 int uid = createVerifiedUser(firstName, lastName, email, password);
227                 // TODO make him pass CATS and be assured for 100 points
228                 return uid;
229         }
230         static int count = 0;
231         public String createUniqueName() {
232                 return "test" + System.currentTimeMillis() + "a" + (count++);
233         }
234 }