]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/ManagedTest.java
UPD: use more advanced hamcrest matchers where possible.
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
index f20ed763f7d4cca84b65296e129ff70147e2bc1b..df19e0cff3680139f8060a2421abf743bda2ffb4 100644 (file)
@@ -4,9 +4,13 @@ import static org.junit.Assert.*;
 
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
+import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
@@ -30,6 +34,7 @@ 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;
@@ -73,6 +78,12 @@ public class ManagedTest {
 
     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;
     }
@@ -95,10 +106,7 @@ public class ManagedTest {
             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")
-            });
+            purgeDatabase();
             String type = testProps.getProperty("type");
             Properties mainProps = generateMainProps();
             ServerConstants.init(mainProps);
@@ -144,8 +152,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) {
@@ -154,6 +160,19 @@ 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")
+            }, true);
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        }
+        System.out.println(" in " + (System.currentTimeMillis() - ms) + " ms");
+    }
+
     private static Properties generateMainProps() {
         Properties mainProps = new Properties();
         mainProps.setProperty("host", "127.0.0.1");
@@ -194,6 +213,11 @@ public class ManagedTest {
         ter.reset();
     }
 
+    @After
+    public void clearAcceptLanguage() {
+        acceptLanguage = null;
+    }
+
     public TestMail waitForMail() {
         try {
             return ter.recieve();
@@ -210,6 +234,10 @@ public class ManagedTest {
         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);
@@ -418,7 +446,26 @@ public class ManagedTest {
     public static KeyPair generateKeypair() throws GeneralSecurityException {
         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
         kpg.initialize(4096);
-        return kpg.generateKeyPair();
+        KeyPair keyPair = null;
+        File f = new File("testKeypair");
+        if (f.exists()) {
+            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
+                keyPair = (KeyPair) ois.readObject();
+            } catch (ClassNotFoundException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        } else {
+            keyPair = kpg.generateKeyPair();
+            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
+                oos.writeObject(keyPair);
+                oos.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return keyPair;
     }
 
     public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
@@ -460,9 +507,9 @@ public class ManagedTest {
 
     public static EmailAddress createVerifiedEmail(User u) throws InterruptedException, GigiApiException {
         EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
-        adrr.insert(Language.getInstance("en"));
+        adrr.insert(Language.getInstance(Locale.ENGLISH));
         TestMail testMail = getMailReciever().recieve();
-        assertTrue(adrr.getAddress().equals(testMail.getTo()));
+        assertEquals(adrr.getAddress(), testMail.getTo());
         String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
         adrr.verify(hash);
         getMailReciever().clearMails();