1 package club.wpia.gigi.testUtils;
3 import static org.junit.Assert.*;
5 import java.io.BufferedReader;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8 import java.io.InputStreamReader;
9 import java.io.OutputStream;
10 import java.io.UnsupportedEncodingException;
11 import java.net.HttpURLConnection;
12 import java.net.InetSocketAddress;
13 import java.net.MalformedURLException;
14 import java.net.Socket;
16 import java.net.URLConnection;
17 import java.net.URLEncoder;
18 import java.nio.file.Files;
19 import java.nio.file.Paths;
20 import java.security.KeyManagementException;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.Principal;
23 import java.security.PrivateKey;
24 import java.security.cert.X509Certificate;
25 import java.sql.SQLException;
26 import java.util.Locale;
27 import java.util.Properties;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.KeyManager;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.X509KeyManager;
36 import org.hamcrest.CoreMatchers;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
41 import club.wpia.gigi.DevelLauncher;
42 import club.wpia.gigi.GigiApiException;
43 import club.wpia.gigi.database.GigiPreparedStatement;
44 import club.wpia.gigi.database.GigiResultSet;
45 import club.wpia.gigi.dbObjects.EmailAddress;
46 import club.wpia.gigi.dbObjects.Group;
47 import club.wpia.gigi.dbObjects.Job;
48 import club.wpia.gigi.dbObjects.ObjectCache;
49 import club.wpia.gigi.dbObjects.User;
50 import club.wpia.gigi.pages.account.MyDetails;
51 import club.wpia.gigi.pages.main.RegisterPage;
52 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
53 import club.wpia.gigi.util.SimpleSigner;
56 * Base class for test suites who require a launched Gigi instance. The instance
57 * is cleared once per test suite.
59 public class ManagedTest extends ConfiguredTest {
62 System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
65 private static TestEmailReceiver ter;
67 private static Process gigi;
69 private static String url = "localhost:4443";
71 private static String acceptLanguage = null;
73 public static void setAcceptLanguage(String acceptLanguage) {
74 ManagedTest.acceptLanguage = acceptLanguage;
77 public static String getServerName() {
78 return url.replaceFirst(":443$", "");
81 public static String getSecureServerName() {
82 return getServerName().replaceAll("^www\\.", "secure.");
87 HttpURLConnection.setFollowRedirects(false);
91 public static void initEnvironmentHook() {
95 private static boolean inited = false;
97 public static Properties initEnvironment() {
99 Properties mainProps = ConfiguredTest.initEnvironment();
105 String type = testProps.getProperty("type");
106 generateMainProps(mainProps);
107 if (type.equals("local")) {
108 url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
109 String[] parts = testProps.getProperty("mail").split(":", 2);
110 ter = new TestEmailReceiver(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
112 if (testProps.getProperty("withSigner", "false").equals("true")) {
113 SimpleSigner.runSigner();
117 url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
118 gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
119 DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
120 System.out.println("... starting server");
122 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
123 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
125 DevelLauncher.writeGigiConfig(toGigi, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore);
128 final BufferedReader br = new BufferedReader(new InputStreamReader(gigi.getErrorStream(), "UTF-8"));
130 while ((line = br.readLine()) != null && !line.contains("System successfully started.")) {
131 System.err.println(line);
139 while ((line = br.readLine()) != null) {
140 System.err.println(line);
142 } catch (IOException e) {
148 throw new Error("Server startup failed");
150 ter = new TestEmailReceiver(new InetSocketAddress("localhost", 8473));
152 SimpleSigner.runSigner();
154 } catch (IOException e) {
156 } catch (SQLException e1) {
158 } catch (InterruptedException e) {
164 protected static void await(Job j) throws InterruptedException {
169 public static void purgeDatabase() throws SQLException, IOException {
174 public static void clearCaches() throws IOException {
175 ObjectCache.clearAllCaches();
176 // String type = testProps.getProperty("type");
177 URL u = new URL("https://" + getServerName() + "/manage");
178 u.openConnection().getHeaderField("Location");
181 private static void generateMainProps(Properties mainProps) {
182 mainProps.setProperty("testrunner", "true");
183 mainProps.setProperty("host", "127.0.0.1");
185 mainProps.setProperty("emailProvider", "club.wpia.gigi.email.TestEmailProvider");
186 mainProps.setProperty("emailProvider.port", "8473");
187 mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
188 mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
189 mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
190 mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
191 mainProps.setProperty("testing", "true");
195 public static void tearDownServer() {
196 String type = testProps.getProperty("type");
198 if (type.equals("local")) {
199 if (testProps.getProperty("withSigner", "false").equals("true")) {
201 SimpleSigner.stopSigner();
202 } catch (InterruptedException e) {
211 SimpleSigner.stopSigner();
212 } catch (InterruptedException e) {
218 public final String uniq = createUniqueName();
221 public void removeMails() {
226 public void clearAcceptLanguage() {
227 ManagedTest.setAcceptLanguage(null);
231 public MailReceiver getMailReceiver() {
235 public static String runRegister(String param) throws IOException {
236 URL regist = new URL("https://" + getServerName() + RegisterPage.PATH);
237 HttpURLConnection uc = (HttpURLConnection) regist.openConnection();
238 HttpURLConnection csrfConn = (HttpURLConnection) regist.openConnection();
239 if (acceptLanguage != null) {
240 csrfConn.setRequestProperty("Accept-Language", acceptLanguage);
241 uc.setRequestProperty("Accept-Language", acceptLanguage);
244 String headerField = csrfConn.getHeaderField("Set-Cookie");
245 headerField = stripCookie(headerField);
247 String csrf = getCSRF(csrfConn);
248 uc.addRequestProperty("Cookie", headerField);
249 uc.setDoOutput(true);
250 uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes("UTF-8"));
251 if (uc.getResponseCode() == 302) {
254 String d = IOUtils.readURL(uc);
258 public static org.hamcrest.Matcher<String> hasError() {
259 return CoreMatchers.containsString("<div class='alert alert-danger error-msgs'>");
262 public static org.hamcrest.Matcher<String> hasNoError() {
263 return CoreMatchers.not(hasError());
266 public static String fetchStartErrorMessage(String d) throws IOException {
267 String formFail = "<div class='alert alert-danger error-msgs'>";
268 int idx = d.indexOf(formFail);
272 String startError = d.substring(idx + formFail.length(), idx + formFail.length() + 150).trim();
276 public static void registerUser(String firstName, String lastName, String email, String password) {
278 String query = "name-type=western&fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname=" + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1=" + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8") + "&day=1&month=1&year=1910&tos_agree=1";
279 String data = fetchStartErrorMessage(runRegister(query));
281 } catch (UnsupportedEncodingException e) {
283 } catch (IOException e) {
288 public static int createVerifiedUser(String firstName, String lastName, String email, String password) {
289 registerUser(firstName, lastName, email, password);
291 ter.receive().verify();
293 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `users` WHERE `email`=?")) {
294 ps.setString(1, email);
296 GigiResultSet rs = ps.executeQuery();
303 } catch (IOException e) {
308 public static void grant(User u, Group g) throws IOException, GigiApiException {
309 u.grantGroup(getSupporter(), g);
314 * Creates a new user with 100 Verification Points given by an (invalid)
325 * @return a new userid.
327 public static int createVerificationUser(String firstName, String lastName, String email, String password) {
328 int uid = createVerifiedUser(firstName, lastName, email, password);
335 protected static String stripCookie(String headerField) {
336 return headerField.substring(0, headerField.indexOf(';'));
339 public static final String SECURE_REFERENCE = MyDetails.PATH;
341 public static boolean isLoggedin(String cookie) throws IOException {
342 URL u = new URL("https://" + getServerName() + SECURE_REFERENCE);
343 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
344 huc.addRequestProperty("Cookie", cookie);
345 return huc.getResponseCode() == 200;
348 public static String login(String email, String pw) throws IOException {
349 URL u = new URL("https://" + getServerName() + "/login");
350 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
352 String csrf = getCSRF(huc);
353 String headerField = stripCookie(huc.getHeaderField("Set-Cookie"));
355 huc = (HttpURLConnection) u.openConnection();
356 cookie(huc, headerField);
357 huc.setDoOutput(true);
358 OutputStream os = huc.getOutputStream();
359 String data = "username=" + URLEncoder.encode(email, "UTF-8") + "&password=" + URLEncoder.encode(pw, "UTF-8") + "&csrf=" + URLEncoder.encode(csrf, "UTF-8");
360 os.write(data.getBytes("UTF-8"));
362 headerField = huc.getHeaderField("Set-Cookie");
363 if (headerField == null) {
366 if (huc.getResponseCode() != 302) {
367 fail(fetchStartErrorMessage(IOUtils.readURL(huc)));
369 return stripCookie(headerField);
372 public static String login(final PrivateKey pk, final X509Certificate ce) throws NoSuchAlgorithmException, KeyManagementException, IOException, MalformedURLException {
374 HttpURLConnection connection = (HttpURLConnection) new URL("https://" + getSecureServerName() + "/login").openConnection();
375 authenticateClientCert(pk, ce, connection);
376 if (connection.getResponseCode() == 302) {
377 assertEquals("https://" + getSecureServerName() + "/", connection.getHeaderField("Location").replaceFirst(":443$", ""));
378 return stripCookie(connection.getHeaderField("Set-Cookie"));
384 public static void authenticateClientCert(final PrivateKey pk, final X509Certificate ce, HttpURLConnection connection) throws NoSuchAlgorithmException, KeyManagementException {
385 KeyManager km = new X509KeyManager() {
388 public String chooseClientAlias(String[] arg0, Principal[] arg1, Socket arg2) {
393 public String chooseServerAlias(String arg0, Principal[] arg1, Socket arg2) {
398 public X509Certificate[] getCertificateChain(String arg0) {
399 return new X509Certificate[] {
405 public String[] getClientAliases(String arg0, Principal[] arg1) {
406 return new String[] {
412 public PrivateKey getPrivateKey(String arg0) {
413 if (arg0.equals("client")) {
420 public String[] getServerAliases(String arg0, Principal[] arg1) {
421 return new String[] {
426 SSLContext sc = SSLContext.getInstance("TLS");
427 sc.init(new KeyManager[] {
430 if (connection instanceof HttpsURLConnection) {
431 ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
435 public static String getCSRF(URLConnection u) throws IOException {
436 return getCSRF(u, 0);
439 public static String getCSRF(URLConnection u, int formIndex) throws IOException {
440 String content = IOUtils.readURL(u);
441 return getCSRF(formIndex, content);
444 public static String getCSRF(int formIndex, String content) throws Error {
445 Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
446 Matcher m = p.matcher(content);
447 for (int i = 0; i < formIndex + 1; i++) {
449 throw new Error("No CSRF Token:\n" + content);
455 public static String executeBasicWebInteraction(String cookie, String path, String query) throws MalformedURLException, UnsupportedEncodingException, IOException {
456 return executeBasicWebInteraction(cookie, path, query, 0);
459 public static String executeBasicWebInteraction(String cookie, String path, String query, int formIndex) throws IOException, MalformedURLException, UnsupportedEncodingException {
460 HttpURLConnection uc = post(cookie, path, query, formIndex);
461 if (uc.getResponseCode() == 302) {
464 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
468 public static HttpURLConnection post(String cookie, String path, String query, int formIndex) throws IOException, MalformedURLException, UnsupportedEncodingException {
469 URLConnection uc = new URL("https://" + getServerName() + path).openConnection();
470 uc.addRequestProperty("Cookie", cookie);
471 String csrf = getCSRF(uc, formIndex);
473 uc = new URL("https://" + getServerName() + path).openConnection();
474 uc.addRequestProperty("Cookie", cookie);
475 uc.setDoOutput(true);
476 OutputStream os = uc.getOutputStream();
477 os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
479 ).getBytes("UTF-8"));
481 return (HttpURLConnection) uc;
484 public static HttpURLConnection get(String cookie, String path) throws IOException {
485 URLConnection uc = new URL("https://" + getServerName() + path).openConnection();
486 uc.addRequestProperty("Cookie", cookie);
487 return (HttpURLConnection) uc;
490 public EmailAddress createVerifiedEmail(User u) throws InterruptedException, GigiApiException {
491 return createVerifiedEmail(u, createUniqueName() + "test@test.tld");
494 public EmailAddress createVerifiedEmail(User u, String email) throws InterruptedException, GigiApiException {
495 EmailAddress addr = new EmailAddress(u, email, Locale.ENGLISH);
496 TestMail testMail = getMailReceiver().receive();
497 assertEquals(addr.getAddress(), testMail.getTo());
498 String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
500 getMailReceiver().clearMails();
504 public static URLConnection cookie(URLConnection openConnection, String cookie) {
505 openConnection.setRequestProperty("Cookie", cookie);
506 return openConnection;
509 private static User supporter;
511 public static User getSupporter() throws GigiApiException, IOException {
512 if (supporter != null) {
515 int i = createVerifiedUser("fn", "ln", createUniqueName() + "@email.com", TEST_PASSWORD);
516 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
518 ps.setString(2, Group.SUPPORTER.getDBName());
523 supporter = User.getById(i);