X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FManagedTest.java;h=8f6e6c5d44f360301e6efd0f5e323583bcc4081f;hp=d59690d7350af05f6c1676ac05ec17943be85b6a;hb=28675b61ff01735e091fb37fd0d150574f2f61b6;hpb=634b7f75c8fc2ed8799bad74731278fb59198c48 diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index d59690d7..8f6e6c5d 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -1,119 +1,487 @@ package org.cacert.gigi.testUtils; +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; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.Socket; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.KeyManagementException; +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.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManager; +import javax.net.ssl.SSLContext; +import javax.net.ssl.X509KeyManager; import org.cacert.gigi.DevelLauncher; +import org.cacert.gigi.GigiApiException; +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.ServerConstants; +import org.cacert.gigi.util.SimpleSigner; import org.junit.After; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; -public class ManagedTest { - private static TestEmailReciever ter; - private static Process gigi; - private static String url = "localhost:4443"; - - public static String getServerName() { - return url; - } - static Properties testProps = new Properties(); - @BeforeClass - public static void connectToServer() { - try { - testProps.load(new FileInputStream("config/test.properties")); - String type = testProps.getProperty("type"); - if (type.equals("local")) { - url = testProps.getProperty("server"); - String[] parts = testProps.getProperty("mail").split(":", 2); - ter = new TestEmailReciever(new InetSocketAddress(parts[0], - Integer.parseInt(parts[1]))); - return; - } - url = "localhost:" + testProps.getProperty("serverPort"); - gigi = Runtime.getRuntime().exec(testProps.getProperty("java")); - DataOutputStream toGigi = new DataOutputStream( - gigi.getOutputStream()); - System.out.println("... starting server"); - Properties mainProps = new Properties(); - mainProps.load(new FileInputStream("config/gigi.properties")); - mainProps.setProperty("host", "127.0.0.1"); - mainProps.setProperty("port", testProps.getProperty("serverPort")); - mainProps.setProperty("emailProvider", - "org.cacert.gigi.email.TestEmailProvider"); - mainProps.setProperty("emailProvider.port", "8473"); - - byte[] cacerts = Files - .readAllBytes(Paths.get("config/cacerts.jks")); - byte[] keystore = Files.readAllBytes(Paths - .get("config/keystore.pkcs12")); - - DevelLauncher.writeGigiConfig(toGigi, new byte[]{}, - "changeit".getBytes(), mainProps, cacerts, keystore); - toGigi.flush(); - // TODO wait for ready - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - final BufferedReader br = new BufferedReader(new InputStreamReader( - gigi.getErrorStream())); - String line; - while ((line = br.readLine()) != null - && !line.contains("Server:main: Started")) { - System.err.println(line); - } - new Thread() { - @Override - public void run() { - String line; - try { - while ((line = br.readLine()) != null) { - System.err.println(line); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - }.start(); - System.err.println(line); - if (line == null) { - throw new Error("Server startup failed"); - } - ter = new TestEmailReciever( - new InetSocketAddress("localhost", 8473)); - } catch (IOException e) { - throw new Error(e); - } - - } - @AfterClass - public static void tearDownServer() { - String type = testProps.getProperty("type"); - if (type.equals("local")) { - return; - } - gigi.destroy(); - } - - @After - public void removeMails() { - ter.clearMails(); - } - - public TestMail waitForMail() { - try { - return ter.recieve(); - } catch (InterruptedException e) { - throw new Error(e); - } - } +/** + * Base class for test suites who require a launched Gigi instance. The instance + * is cleared once per test suite. + */ +public class ManagedTest extends ConfiguredTest { + + static { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } + + /** + * Some password that fulfills the password criteria. + */ + protected static final String TEST_PASSWORD = "xvXV12°§"; + + 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 { + InitTruststore.run(); + HttpURLConnection.setFollowRedirects(false); + } + + @BeforeClass + public static void initEnvironment() { + try { + 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.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.https"); + gigi = Runtime.getRuntime().exec(testProps.getProperty("java")); + DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream()); + System.out.println("... starting server"); + + byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks")); + byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12")); + + 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(), "UTF-8")); + String line; + while ((line = br.readLine()) != null && !line.contains("Server:main: Started")) { + } + new Thread() { + + @Override + public void run() { + String line; + try { + while ((line = br.readLine()) != null) { + System.err.println(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + }.start(); + if (line == null) { + throw new Error("Server startup failed"); + } + ter = new TestEmailReciever(new InetSocketAddress("localhost", 8473)); + SimpleSigner.runSigner(); + } catch (IOException e) { + throw new Error(e); + } catch (SQLException e1) { + e1.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + 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("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; + } + + @AfterClass + public static void tearDownServer() { + String type = testProps.getProperty("type"); + ter.destroy(); + if (type.equals("local")) { + return; + } + gigi.destroy(); + try { + SimpleSigner.stopSigner(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public final String uniq = createUniqueName(); + + @After + public void removeMails() { + ter.reset(); + } + + @After + public void clearAcceptLanguage() { + acceptLanguage = null; + } + + public TestMail waitForMail() { + try { + return ter.recieve(); + } catch (InterruptedException e) { + throw new Error(e); + } + } + + public static TestEmailReciever getMailReciever() { + return ter; + } + + 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); + + String csrf = getCSRF(csrfConn); + uc.addRequestProperty("Cookie", headerField); + uc.setDoOutput(true); + uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes("UTF-8")); + String d = IOUtils.readURL(uc); + return d; + } + + public static String fetchStartErrorMessage(String d) throws IOException { + String formFail = "