]> WPIA git - gigi.git/commitdiff
Factor out more test-things.
authorFelix Dörre <felix@dogcraft.de>
Fri, 18 Jul 2014 22:12:45 +0000 (00:12 +0200)
committerFelix Dörre <felix@dogcraft.de>
Fri, 18 Jul 2014 22:26:47 +0000 (00:26 +0200)
tests/org/cacert/gigi/LoginTest.java
tests/org/cacert/gigi/testUtils/ManagedTest.java

index f4bfe85fb5e71fa622d83cd46806dc30370c998c..8a71090f601aeb7c768294980263b0e55a471d51 100644 (file)
@@ -2,16 +2,10 @@ package org.cacert.gigi;
 
 import java.io.IOException;
 import static org.junit.Assert.*;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLEncoder;
-
 import org.cacert.gigi.testUtils.ManagedTest;
 import org.junit.Test;
 
 public class LoginTest extends ManagedTest {
-       public static final String secureReference = "/account/certs/email";
 
        @Test
        public void testLoginUnverified() throws IOException {
@@ -32,11 +26,4 @@ public class LoginTest extends ManagedTest {
                assertTrue(isLoggedin(login(email, pw)));
        }
 
-       public boolean isLoggedin(String cookie) throws IOException {
-               URL u = new URL("https://" + getServerName() + secureReference);
-               HttpURLConnection huc = (HttpURLConnection) u.openConnection();
-               huc.addRequestProperty("Cookie", cookie);
-               return huc.getResponseCode() == 200;
-       }
-
 }
index 9e077c60115885e2fef69dacb7e8dc6f0b21b957..aa800251e5b7a9995fd3d7a25e9a89c7d9a1c2ad 100644 (file)
@@ -279,6 +279,15 @@ public class ManagedTest {
                return headerField.substring(0, headerField.indexOf(';'));
        }
 
+       public static final String SECURE_REFERENCE = "/account/certs/email";
+
+       public boolean isLoggedin(String cookie) throws IOException {
+               URL u = new URL("https://" + getServerName() + SECURE_REFERENCE);
+               HttpURLConnection huc = (HttpURLConnection) u.openConnection();
+               huc.addRequestProperty("Cookie", cookie);
+               return huc.getResponseCode() == 200;
+       }
+
        public String login(String email, String pw) throws IOException {
                URL u = new URL("https://" + getServerName() + "/login");
                HttpURLConnection huc = (HttpURLConnection) u.openConnection();
@@ -293,6 +302,21 @@ public class ManagedTest {
 
        public String login(final PrivateKey pk, final X509Certificate ce) throws NoSuchAlgorithmException,
                KeyManagementException, IOException, MalformedURLException {
+
+               HttpURLConnection connection = (HttpURLConnection) new URL("https://"
+                       + getServerName().replaceFirst("^www.", "secure.") + "/login").openConnection();
+               authenticateClientCert(pk, ce, connection);
+               if (connection.getResponseCode() == 302) {
+                       assertEquals("https://" + getServerName().replaceFirst("^www.", "secure.").replaceFirst(":443$", "") + "/",
+                               connection.getHeaderField("Location").replaceFirst(":443$", ""));
+                       return stripCookie(connection.getHeaderField("Set-Cookie"));
+               } else {
+                       return null;
+               }
+       }
+
+       public void authenticateClientCert(final PrivateKey pk, final X509Certificate ce, HttpURLConnection connection)
+               throws NoSuchAlgorithmException, KeyManagementException {
                KeyManager km = new X509KeyManager() {
 
                        @Override
@@ -330,19 +354,9 @@ public class ManagedTest {
                };
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(new KeyManager[] { km }, null, null);
-
-               HttpURLConnection connection = (HttpURLConnection) new URL("https://"
-                       + getServerName().replaceFirst("^www.", "secure.") + "/login").openConnection();
                if (connection instanceof HttpsURLConnection) {
                        ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
                }
-               if (connection.getResponseCode() == 302) {
-                       assertEquals("https://" + getServerName().replaceFirst("^www.", "secure.").replaceFirst(":443$", "") + "/",
-                               connection.getHeaderField("Location").replaceFirst(":443$", ""));
-                       return stripCookie(connection.getHeaderField("Set-Cookie"));
-               } else {
-                       return null;
-               }
        }
 
        public String getCSRF(URLConnection u) throws IOException {