From d937840c661248a848580e307b618950bbf34ef8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Sat, 19 Jul 2014 00:12:45 +0200 Subject: [PATCH] Factor out more test-things. --- tests/org/cacert/gigi/LoginTest.java | 13 ------- .../cacert/gigi/testUtils/ManagedTest.java | 34 +++++++++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/tests/org/cacert/gigi/LoginTest.java b/tests/org/cacert/gigi/LoginTest.java index f4bfe85f..8a71090f 100644 --- a/tests/org/cacert/gigi/LoginTest.java +++ b/tests/org/cacert/gigi/LoginTest.java @@ -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; - } - } diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index 9e077c60..aa800251 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -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 { -- 2.39.2