]> 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 880f600631fa7a77c2b976d23bdfd5ae8039126f..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;
@@ -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;
+       }
+
 }