]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/ManagedTest.java
Implement testing of internal certificate issuing (and login with it)
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
index 2f685dbf9c1a4bb95c5a807db9f67d0de16be730..2159a66322f002e709208907bd151367ee95d922 100644 (file)
@@ -28,6 +28,7 @@ import org.cacert.gigi.DevelLauncher;
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
 import org.cacert.gigi.util.DatabaseManager;
+import org.cacert.gigi.util.SimpleSigner;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -62,7 +63,7 @@ public class ManagedTest {
 
                        String type = testProps.getProperty("type");
                        if (type.equals("local")) {
-                               url = testProps.getProperty("server");
+                               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;
@@ -73,9 +74,9 @@ public class ManagedTest {
                        System.out.println("... starting server");
                        Properties mainProps = new Properties();
                        mainProps.setProperty("host", "127.0.0.1");
-                       mainProps.setProperty("name.secure", "sec");
+                       mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
                        mainProps.setProperty("name.www", testProps.getProperty("name.www"));
-                       mainProps.setProperty("name.static", "stat");
+                       mainProps.setProperty("name.static", testProps.getProperty("name.static"));
 
                        mainProps.setProperty("port", testProps.getProperty("serverPort"));
                        mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
@@ -113,12 +114,15 @@ public class ManagedTest {
                                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();
                }
 
        }
@@ -131,6 +135,11 @@ public class ManagedTest {
                        return;
                }
                gigi.destroy();
+               try {
+                       SimpleSigner.stopSigner();
+               } catch (InterruptedException e) {
+                       e.printStackTrace();
+               }
        }
 
        @After
@@ -151,10 +160,17 @@ public class ManagedTest {
        }
 
        public String runRegister(String param) throws IOException {
-               HttpURLConnection uc = (HttpURLConnection) new URL("https://" + getServerName() + registerService)
-                       .openConnection();
+               URL regist = new URL("https://" + getServerName() + registerService);
+               HttpURLConnection uc = (HttpURLConnection) regist.openConnection();
+               HttpURLConnection csrfConn = (HttpURLConnection) regist.openConnection();
+
+               String headerField = csrfConn.getHeaderField("Set-Cookie");
+               headerField = headerField.substring(0, headerField.indexOf(';'));
+
+               String csrf = getCSRF(csrfConn);
+               uc.addRequestProperty("Cookie", headerField);
                uc.setDoOutput(true);
-               uc.getOutputStream().write(param.getBytes());
+               uc.getOutputStream().write((param + "&csrf=" + csrf).getBytes());
                String d = IOUtils.readURL(uc);
                return d;
        }
@@ -261,11 +277,26 @@ public class ManagedTest {
 
        public String getCSRF(URLConnection u) throws IOException {
                String content = IOUtils.readURL(u);
-               Pattern p = Pattern.compile("<input type='csrf' value='([^']+)'>");
+               Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
                Matcher m = p.matcher(content);
                if (!m.find()) {
-                       throw new Error("New CSRF Token");
+                       throw new Error("No CSRF Token");
                }
                return m.group(1);
        }
+
+       public static String[] generateCSR(String dn) throws IOException {
+               Process p = Runtime.getRuntime().exec(
+                       new String[] { "openssl", "req", "-newkey", "rsa:1024", "-nodes", "-subj", dn, "-config",
+                                       "keys/selfsign.config" });
+               String csr = IOUtils.readURL(new InputStreamReader(p.getInputStream()));
+
+               String[] parts = csr.split("(?<=-----)\n(?=-----)");
+               if (parts.length != 2) {
+                       System.err.println(IOUtils.readURL(new InputStreamReader(p.getErrorStream())));
+                       throw new Error();
+               }
+               return parts;
+       }
+
 }