X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FManagedTest.java;h=b87585223867cb5e4ebef556e41900ef4112cf7b;hb=dbb3046c3a3b833305535937f1c7e2653f2ca247;hp=eaf6d8d233ebaa78d49121290173dbeee32ce620;hpb=424cb41682b02fbeb6ad7ba6e49e6007c018e883;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index eaf6d8d2..b8758522 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -1,263 +1,485 @@ package org.cacert.gigi.testUtils; -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; 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.sql.PreparedStatement; -import java.sql.ResultSet; +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.IOUtils; -import org.cacert.gigi.InitTruststore; -import org.cacert.gigi.database.DatabaseConnection; -import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; -import org.cacert.gigi.util.DatabaseManager; +import org.cacert.gigi.GigiApiException; +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.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.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("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.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"); - 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, 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); - } catch (ClassNotFoundException e1) { - e1.printStackTrace(); - } catch (SQLException e1) { - e1.printStackTrace(); - } - - } - @AfterClass - public static void tearDownServer() { - String type = testProps.getProperty("type"); - if (type.equals("local")) { - return; - } - gigi.destroy(); - } - - @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 { - HttpURLConnection uc = (HttpURLConnection) new URL("https://" - + getServerName() + registerService).openConnection(); - uc.setDoOutput(true); - uc.getOutputStream().write(param.getBytes()); - String d = IOUtils.readURL(uc); - return d; - } - public String fetchStartErrorMessage(String d) throws IOException { - String formFail = "