X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FManagedTest.java;h=f379b3f204d2d99f2f256be79e4173d34fcbce04;hb=4de35d0ba81eadeceb437dace9e06ef59caa2b39;hp=9e077c60115885e2fef69dacb7e8dc6f0b21b957;hpb=93170a75f1ba10a05012510b1b87f30adbc4e91d;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 9e077c60..f379b3f2 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -1,12 +1,9 @@ package org.cacert.gigi.testUtils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +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; @@ -25,9 +22,8 @@ import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.security.PrivateKey; 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; @@ -38,335 +34,500 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.X509KeyManager; import org.cacert.gigi.DevelLauncher; -import org.cacert.gigi.database.DatabaseConnection; -import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.database.SQLFileManager.ImportType; +import org.cacert.gigi.dbObjects.CATS; +import org.cacert.gigi.dbObjects.Domain; +import org.cacert.gigi.dbObjects.DomainPingType; +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.Manager; +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.DatabaseManager; +import org.cacert.gigi.util.ServerConstants; import org.cacert.gigi.util.SimpleSigner; +import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; -public class ManagedTest { - private final String registerService = "/register"; - - 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(); - static { - InitTruststore.run(); - HttpURLConnection.setFollowRedirects(false); - } - - @BeforeClass - public static void connectToServer() { - 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") }); - - String type = testProps.getProperty("type"); - if (type.equals("local")) { - url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort"); - String[] parts = testProps.getProperty("mail").split(":", 2); - ter = new TestEmailReciever(new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); - return; - } - url = testProps.getProperty("name.www") + ":" + 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.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")); - - 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); - toGigi.flush(); - - final BufferedReader br = new BufferedReader(new InputStreamReader(gigi.getErrorStream())); - 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 (ClassNotFoundException e1) { - e1.printStackTrace(); - } catch (SQLException e1) { - e1.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - - @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(); - } - } - - @After - public void removeMails() { - ter.reset(); - } - - public TestMail waitForMail() { - try { - return ter.recieve(); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static TestEmailReciever getMailReciever() { - return ter; - } - - public String runRegister(String param) throws IOException { - URL regist = new URL("https://" + getServerName() + registerService); - HttpURLConnection uc = (HttpURLConnection) regist.openConnection(); - HttpURLConnection csrfConn = (HttpURLConnection) regist.openConnection(); - - 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()); - String d = IOUtils.readURL(uc); - return d; - } - - public String fetchStartErrorMessage(String d) throws IOException { - String formFail = "