X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Fclub%2Fwpia%2Fgigi%2FtestUtils%2FManagedTest.java;h=a6d51becff72a9f90dcda4e1175041aaf4a9b72d;hp=fd602b8302750d2ad4de834182f8863097254f90;hb=62b9a6519d6304820fdfa739ef9281e1cdaccb7d;hpb=20c4322f87aa9abe1cf9147f5734f2df32eb8fa3 diff --git a/tests/club/wpia/gigi/testUtils/ManagedTest.java b/tests/club/wpia/gigi/testUtils/ManagedTest.java index fd602b83..a6d51bec 100644 --- a/tests/club/wpia/gigi/testUtils/ManagedTest.java +++ b/tests/club/wpia/gigi/testUtils/ManagedTest.java @@ -17,13 +17,16 @@ import java.net.URLConnection; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.GeneralSecurityException; import java.security.KeyManagementException; +import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.sql.SQLException; import java.util.Locale; +import java.util.Map.Entry; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,6 +45,9 @@ import club.wpia.gigi.DevelLauncher; import club.wpia.gigi.GigiApiException; import club.wpia.gigi.database.GigiPreparedStatement; import club.wpia.gigi.database.GigiResultSet; +import club.wpia.gigi.dbObjects.Certificate; +import club.wpia.gigi.dbObjects.Certificate.CSRType; +import club.wpia.gigi.dbObjects.Digest; import club.wpia.gigi.dbObjects.EmailAddress; import club.wpia.gigi.dbObjects.Group; import club.wpia.gigi.dbObjects.Job; @@ -70,6 +76,10 @@ public class ManagedTest extends ConfiguredTest { private static String acceptLanguage = null; + protected static Certificate loginCertificate; + + protected static PrivateKey loginPrivateKey; + public static void setAcceptLanguage(String acceptLanguage) { ManagedTest.acceptLanguage = acceptLanguage; } @@ -95,17 +105,26 @@ public class ManagedTest extends ConfiguredTest { private static boolean inited = false; public static Properties initEnvironment() { + return initEnvironment(new Properties()); + } + + public static Properties initEnvironment(Properties additionalConfig) { try { Properties mainProps = ConfiguredTest.initEnvironment(); + for (Entry i : additionalConfig.entrySet()) { + if (i.getKey() instanceof String && i.getValue() instanceof String) { + mainProps.setProperty((String) i.getKey(), (String) i.getValue()); + } + } if (inited) { return mainProps; } inited = true; + url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https"); purgeDatabase(); String type = testProps.getProperty("type"); generateMainProps(mainProps); if (type.equals("local")) { - url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https"); String[] parts = testProps.getProperty("mail").split(":", 2); ter = new TestEmailReceiver(new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); ter.start(); @@ -114,7 +133,6 @@ public class ManagedTest extends ConfiguredTest { } return mainProps; } - url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https"); gigi = Runtime.getRuntime().exec(testProps.getProperty("java")); DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream()); System.out.println("... starting server"); @@ -168,14 +186,18 @@ public class ManagedTest extends ConfiguredTest { public static void purgeDatabase() throws SQLException, IOException { purgeOnlyDB(); - clearCaches(); + if (gigi != null) { + clearCaches(); + } } public static void clearCaches() throws IOException { ObjectCache.clearAllCaches(); // String type = testProps.getProperty("type"); URL u = new URL("https://" + getServerName() + "/manage"); - u.openConnection().getHeaderField("Location"); + URLConnection connection = u.openConnection(); + connection.getHeaderField("Location"); + connection.getInputStream().close(); } private static void generateMainProps(Properties mainProps) { @@ -275,7 +297,7 @@ public class ManagedTest extends ConfiguredTest { public static void registerUser(String firstName, String lastName, String email, String password) { try { - 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"; + 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&dp_agree=1"; String data = fetchStartErrorMessage(runRegister(query)); assertNull(data); } catch (UnsupportedEncodingException e) { @@ -288,7 +310,7 @@ public class ManagedTest extends ConfiguredTest { public static int createVerifiedUser(String firstName, String lastName, String email, String password) { registerUser(firstName, lastName, email, password); try { - ter.receive().verify(); + ter.receive(email).verify(); try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `users` WHERE `email`=?")) { ps.setString(1, email); @@ -466,12 +488,16 @@ public class ManagedTest extends ConfiguredTest { } public static HttpURLConnection post(String cookie, String path, String query, int formIndex) throws IOException, MalformedURLException, UnsupportedEncodingException { - URLConnection uc = new URL("https://" + getServerName() + path).openConnection(); - uc.addRequestProperty("Cookie", cookie); + String server = getServerName(); + if (loginCertificate != null) { + server = getSecureServerName(); + } + URLConnection uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); String csrf = getCSRF(uc, formIndex); - uc = new URL("https://" + getServerName() + path).openConnection(); - uc.addRequestProperty("Cookie", cookie); + uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); uc.setDoOutput(true); OutputStream os = uc.getOutputStream(); os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // @@ -482,8 +508,12 @@ public class ManagedTest extends ConfiguredTest { } public static HttpURLConnection get(String cookie, String path) throws IOException { - URLConnection uc = new URL("https://" + getServerName() + path).openConnection(); - uc.addRequestProperty("Cookie", cookie); + String server = getServerName(); + if (loginCertificate != null) { + server = getSecureServerName(); + } + URLConnection uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); return (HttpURLConnection) uc; } @@ -493,11 +523,10 @@ public class ManagedTest extends ConfiguredTest { public EmailAddress createVerifiedEmail(User u, String email) throws InterruptedException, GigiApiException { EmailAddress addr = new EmailAddress(u, email, Locale.ENGLISH); - TestMail testMail = getMailReceiver().receive(); - assertEquals(addr.getAddress(), testMail.getTo()); + TestMail testMail = getMailReceiver().receive(addr.getAddress()); String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1); addr.verify(hash); - getMailReceiver().clearMails(); + getMailReceiver().assertEmpty(); return addr; } @@ -523,4 +552,40 @@ public class ManagedTest extends ConfiguredTest { supporter = User.getById(i); return supporter; } + + protected static void authenticate(HttpURLConnection uc, String cookie) throws IOException { + uc.addRequestProperty("Cookie", cookie); + if (loginCertificate != null) { + try { + authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc); + } catch (GeneralSecurityException | GigiApiException e) { + throw new IOException(e); + } + } + } + + protected String cookieWithCertificateLogin(User u) throws IOException, GigiApiException { + + try { + KeyPair kp; + kp = generateKeypair(); + + String csr; + csr = generatePEMCSR(kp, "CN=" + u.getPreferredName().toString()); + + Certificate c = new Certificate(u, u, Certificate.buildDN("CN", u.getPreferredName().toString()), Digest.SHA256, csr, CSRType.CSR, getClientProfile()); + final PrivateKey pk = kp.getPrivate(); + await(c.issue(null, "2y", u)); + final X509Certificate ce = c.cert(); + c.setLoginEnabled(true); + loginCertificate = c; + loginPrivateKey = pk; + return login(pk, ce); + } catch (InterruptedException e) { + throw new GigiApiException(e.toString()); + } catch (GeneralSecurityException e) { + throw new GigiApiException(e.toString()); + } + + } }