X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FManagedTest.java;h=da55f6848f450afac81837d00bc4cd2fc95097bc;hb=8eaf45ee6a71969eff3e04b40e4435e0dd380824;hp=2518849f92b37ff298b98554d312325ab59715f7;hpb=a5c3e98a922e432d1ec2fd0b924045638c6c9376;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 2518849f..da55f684 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,70 +34,86 @@ 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.Language; -import org.cacert.gigi.User; -import org.cacert.gigi.database.DatabaseConnection; -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.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.dbObjects.EmailAddress; +import org.cacert.gigi.dbObjects.Group; +import org.cacert.gigi.dbObjects.Job; +import org.cacert.gigi.dbObjects.ObjectCache; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.pages.account.MyDetails; +import org.cacert.gigi.pages.main.RegisterPage; +import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail; import org.cacert.gigi.util.SimpleSigner; +import org.hamcrest.CoreMatchers; 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 { - - /** - * Some password that fullfills the password criteria. - */ - protected static final String TEST_PASSWORD = "xvXV12°§"; - - private final String registerService = "/register"; + static { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } - private static TestEmailReciever ter; + private static TestEmailReceiver 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; + return url.replaceFirst(":443$", ""); + } + + public static String getSecureServerName() { + return getServerName().replaceAll("^www\\.", "secure."); } - static Properties testProps = new Properties(); static { InitTruststore.run(); HttpURLConnection.setFollowRedirects(false); } @BeforeClass - public static void connectToServer() { + public static void initEnvironmentHook() { + initEnvironment(); + } + + private static boolean inited = false; + + public static Properties initEnvironment() { try { - testProps.load(new FileInputStream("config/test.properties")); - if ( !DatabaseConnection.isInited()) { - DatabaseConnection.init(testProps); + Properties mainProps = ConfiguredTest.initEnvironment(); + if (inited) { + return mainProps; } - 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") - }); + inited = true; + purgeDatabase(); String type = testProps.getProperty("type"); - Properties mainProps = generateMainProps(); - ServerConstants.init(mainProps); + generateMainProps(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]))); - return; + ter = new TestEmailReceiver(new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); + ter.start(); + if (testProps.getProperty("withSigner", "false").equals("true")) { + SimpleSigner.runSigner(); + } + return mainProps; } - 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"); @@ -111,12 +121,13 @@ 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")) { + while ((line = br.readLine()) != null && !line.contains("System successfully started.")) { + System.err.println(line); } new Thread() { @@ -135,35 +146,48 @@ public class ManagedTest { if (line == null) { throw new Error("Server startup failed"); } - ter = new TestEmailReciever(new InetSocketAddress("localhost", 8473)); + ter = new TestEmailReceiver(new InetSocketAddress("localhost", 8473)); + ter.start(); SimpleSigner.runSigner(); + return mainProps; } catch (IOException e) { throw new Error(e); - } catch (ClassNotFoundException e1) { - e1.printStackTrace(); } catch (SQLException e1) { - e1.printStackTrace(); + throw new Error(e1); } catch (InterruptedException e) { - e.printStackTrace(); + throw new Error(e); } } - private static Properties generateMainProps() { - Properties mainProps = new Properties(); + protected static void await(Job j) throws InterruptedException { + SimpleSigner.ping(); + j.waitFor(5000); + } + + public static void purgeDatabase() throws SQLException, IOException { + purgeOnlyDB(); + 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 void generateMainProps(Properties mainProps) { + 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("port", testProps.getProperty("serverPort")); 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")); - return mainProps; + mainProps.setProperty("testing", "true"); } @AfterClass @@ -181,27 +205,31 @@ public class ManagedTest { } } + public final String uniq = createUniqueName(); + @After public void removeMails() { ter.reset(); } - public TestMail waitForMail() { - try { - return ter.recieve(); - } catch (InterruptedException e) { - throw new Error(e); - } + @After + public void clearAcceptLanguage() { + ManagedTest.setAcceptLanguage(null); } - public static TestEmailReciever getMailReciever() { + @Override + public MailReceiver getMailReceiver() { 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); @@ -209,26 +237,37 @@ 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")); + if (uc.getResponseCode() == 302) { + return ""; + } String d = IOUtils.readURL(uc); return d; } - public String fetchStartErrorMessage(String d) throws IOException { - String formFail = "