X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FManagedTest.java;h=5c58e23429e02e22ac29f161e0e57fae4c7570c5;hb=9a694f0b85897d48a09f02ff0d51390c687dd4dc;hp=563017876efe509fcb558761cfac70cd885724d5;hpb=480cb29387c76ccc19f8fa8fb0abe8ae1b069730;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 56301787..5c58e234 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.*; import java.io.BufferedReader; import java.io.DataOutputStream; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; @@ -18,18 +17,13 @@ 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.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.security.PrivateKey; -import java.security.Signature; import java.security.cert.X509Certificate; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Locale; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,71 +34,80 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.X509KeyManager; import org.cacert.gigi.DevelLauncher; -import org.cacert.gigi.EmailAddress; import org.cacert.gigi.GigiApiException; -import org.cacert.gigi.User; import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.database.SQLFileManager.ImportType; +import org.cacert.gigi.dbObjects.EmailAddress; +import org.cacert.gigi.dbObjects.Group; +import org.cacert.gigi.dbObjects.ObjectCache; +import org.cacert.gigi.dbObjects.User; import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.pages.Manager; import org.cacert.gigi.pages.account.MyDetails; +import org.cacert.gigi.pages.main.RegisterPage; import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; import org.cacert.gigi.util.DatabaseManager; -import org.cacert.gigi.util.PEM; import org.cacert.gigi.util.ServerConstants; import org.cacert.gigi.util.SimpleSigner; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; -import sun.security.pkcs10.PKCS10; -import sun.security.pkcs10.PKCS10Attributes; -import sun.security.x509.X500Name; +/** + * Base class for test suites who require a launched Gigi instance. The instance + * is cleared once per test suite. + */ +public class ManagedTest extends ConfiguredTest { -public class ManagedTest { + static { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } /** - * Some password that fullfills the password criteria. + * Some password that fulfills the password criteria. */ protected static final String TEST_PASSWORD = "xvXV12°§"; - private final String registerService = "/register"; - private static TestEmailReciever ter; private static Process gigi; private static String url = "localhost:4443"; + private static String acceptLanguage = null; + + public static void setAcceptLanguage(String acceptLanguage) { + ManagedTest.acceptLanguage = acceptLanguage; + } + public static String getServerName() { return url; } - static Properties testProps = new Properties(); static { InitTruststore.run(); HttpURLConnection.setFollowRedirects(false); } @BeforeClass - public static void connectToServer() { + public static void initEnvironment() { try { - testProps.load(new FileInputStream("config/test.properties")); - if ( !DatabaseConnection.isInited()) { - DatabaseConnection.init(testProps); - } - System.out.println("... purging Database"); - DatabaseManager.run(new String[] { - testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password") - }); + ConfiguredTest.initEnvironment(); + + purgeDatabase(); String type = testProps.getProperty("type"); Properties mainProps = generateMainProps(); ServerConstants.init(mainProps); if (type.equals("local")) { - url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort"); + url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https"); String[] parts = testProps.getProperty("mail").split(":", 2); ter = new TestEmailReciever(new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); + ter.start(); return; } - url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort"); + 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"); @@ -112,10 +115,10 @@ public class ManagedTest { byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks")); byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12")); - DevelLauncher.writeGigiConfig(toGigi, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore); + DevelLauncher.writeGigiConfig(toGigi, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore); toGigi.flush(); - final BufferedReader br = new BufferedReader(new InputStreamReader(gigi.getErrorStream())); + final BufferedReader br = new BufferedReader(new InputStreamReader(gigi.getErrorStream(), "UTF-8")); String line; while ((line = br.readLine()) != null && !line.contains("Server:main: Started")) { } @@ -140,8 +143,6 @@ public class ManagedTest { SimpleSigner.runSigner(); } catch (IOException e) { throw new Error(e); - } catch (ClassNotFoundException e1) { - e1.printStackTrace(); } catch (SQLException e1) { e1.printStackTrace(); } catch (InterruptedException e) { @@ -150,20 +151,45 @@ public class ManagedTest { } + public static void purgeDatabase() throws SQLException, IOException { + System.out.print("... resetting Database"); + long ms = System.currentTimeMillis(); + try { + DatabaseManager.run(new String[] { + testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password") + }, ImportType.TRUNCATE); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + System.out.println(" in " + (System.currentTimeMillis() - ms) + " ms"); + 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"); + } + private static Properties generateMainProps() { Properties mainProps = new Properties(); + mainProps.setProperty("testrunner", "true"); mainProps.setProperty("host", "127.0.0.1"); mainProps.setProperty("name.secure", testProps.getProperty("name.secure")); mainProps.setProperty("name.www", testProps.getProperty("name.www")); mainProps.setProperty("name.static", testProps.getProperty("name.static")); + mainProps.setProperty("name.api", testProps.getProperty("name.api")); - mainProps.setProperty("port", testProps.getProperty("serverPort")); + mainProps.setProperty("https.port", testProps.getProperty("serverPort.https")); + mainProps.setProperty("http.port", testProps.getProperty("serverPort.http")); mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider"); mainProps.setProperty("emailProvider.port", "8473"); mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver")); mainProps.setProperty("sql.url", testProps.getProperty("sql.url")); mainProps.setProperty("sql.user", testProps.getProperty("sql.user")); mainProps.setProperty("sql.password", testProps.getProperty("sql.password")); + mainProps.setProperty("testing", "true"); return mainProps; } @@ -182,11 +208,18 @@ public class ManagedTest { } } + public final String uniq = createUniqueName(); + @After public void removeMails() { ter.reset(); } + @After + public void clearAcceptLanguage() { + acceptLanguage = null; + } + public TestMail waitForMail() { try { return ter.recieve(); @@ -199,10 +232,14 @@ public class ManagedTest { return ter; } - public String runRegister(String param) throws IOException { - URL regist = new URL("https://" + getServerName() + registerService); + public static String runRegister(String param) throws IOException { + URL regist = new URL("https://" + getServerName() + RegisterPage.PATH); HttpURLConnection uc = (HttpURLConnection) regist.openConnection(); HttpURLConnection csrfConn = (HttpURLConnection) regist.openConnection(); + if (acceptLanguage != null) { + csrfConn.setRequestProperty("Accept-Language", acceptLanguage); + uc.setRequestProperty("Accept-Language", acceptLanguage); + } String headerField = csrfConn.getHeaderField("Set-Cookie"); headerField = stripCookie(headerField); @@ -210,12 +247,12 @@ public class ManagedTest { String csrf = getCSRF(csrfConn); uc.addRequestProperty("Cookie", headerField); uc.setDoOutput(true); - uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes()); + uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes("UTF-8")); String d = IOUtils.readURL(uc); return d; } - public String fetchStartErrorMessage(String d) throws IOException { + public static String fetchStartErrorMessage(String d) throws IOException { String formFail = "