X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2Fping%2FTestSSL.java;h=19aafe258b3bff4986dd4aa4aebc06ae5e9422c9;hp=04b5a6468020dc983ed5b90973df29020036f386;hb=1a1d19be124739d8093781ac8ad5b33416cc6115;hpb=e409ba881965634f63f0b67824bc93dda4ec4327 diff --git a/tests/org/cacert/gigi/ping/TestSSL.java b/tests/org/cacert/gigi/ping/TestSSL.java index 04b5a646..19aafe25 100644 --- a/tests/org/cacert/gigi/ping/TestSSL.java +++ b/tests/org/cacert/gigi/ping/TestSSL.java @@ -1,6 +1,7 @@ package org.cacert.gigi.ping; import static org.junit.Assert.*; +import static org.junit.Assume.*; import java.io.IOException; import java.net.Socket; @@ -29,41 +30,77 @@ import javax.net.ssl.X509TrustManager; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.dbObjects.Certificate; -import org.cacert.gigi.dbObjects.CertificateProfile; import org.cacert.gigi.dbObjects.Certificate.CSRType; -import org.cacert.gigi.pages.account.DomainOverview; +import org.cacert.gigi.dbObjects.CertificateProfile; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.pages.account.domain.DomainOverview; import org.cacert.gigi.testUtils.IOUtils; import org.cacert.gigi.testUtils.PingTest; -import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; +import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail; import org.junit.Test; public class TestSSL extends PingTest { + public abstract static class AsyncTask { + + T res; + + Thread runner; + + Exception ex; + + public T join() throws InterruptedException { + runner.join(); + if (ex != null) { + throw new Error(ex); + } + return res; + } + + public void start() { + runner = new Thread() { + + @Override + public void run() { + try { + res = AsyncTask.this.run(); + } catch (Exception e) { + ex = e; + } + } + }; + runner.start(); + } + + public abstract T run() throws Exception; + + } + private KeyPair kp; private Certificate c; - @Test + @Test(timeout = 70000) public void sslAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { testEmailAndSSL(0, 0, true); } - @Test + @Test(timeout = 70000) public void sslWongTypeAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { testEmailAndSSL(1, 0, true); } - @Test + @Test(timeout = 70000) public void sslOneMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { testEmailAndSSL(2, 0, true); } - @Test + @Test(timeout = 70000) public void sslBothMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { testEmailAndSSL(3, 0, true); } - @Test + @Test(timeout = 70000) public void sslWrongTypeAndMailFail() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { testEmailAndSSL(1, 1, false); } @@ -88,15 +125,15 @@ public class TestSSL extends PingTest { private void testEmailAndSSL(int sslVariant, int emailVariant, boolean successMail) throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException { String test = getTestProps().getProperty("domain.local"); - + assumeNotNull(test); URL u = new URL("https://" + getServerName() + DomainOverview.PATH); initailizeDomainForm(u); createCertificate(test, CertificateProfile.getByName(sslVariant == 1 ? "client" : "server")); - SSLServerSocket sss = createSSLServer(kp.getPrivate(), c.cert()); + final SSLServerSocket sss = createSSLServer(kp.getPrivate(), c.cert()); int port = sss.getLocalPort(); - SSLServerSocket sss2 = createSSLServer(kp.getPrivate(), c.cert()); + final SSLServerSocket sss2 = createSSLServer(kp.getPrivate(), c.cert()); int port2 = sss2.getLocalPort(); if (sslVariant == 3 || sslVariant == 2) { sss2.close(); @@ -104,7 +141,7 @@ public class TestSSL extends PingTest { sss.close(); } } - String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + // + String content = "adddomain&newdomain=" + URLEncoder.encode(test, "UTF-8") + // "&emailType=y&email=2&SSLType=y" + // "&ssl-type-0=direct&ssl-port-0=" + port + // "&ssl-type-1=direct&ssl-port-1=" + port2 + // @@ -113,14 +150,25 @@ public class TestSSL extends PingTest { "&adddomain&csrf=" + csrf; URL u2 = sendDomainForm(u, content); boolean firstSucceeds = sslVariant != 0 && sslVariant != 2; - assertTrue(firstSucceeds ^ acceptSSLServer(sss)); + AsyncTask ass = new AsyncTask() { + + @Override + public Boolean run() throws Exception { + return acceptSSLServer(sss); + } + }; + ass.start(); + System.out.println(port + " and " + port2 + " ready"); + System.err.println(port + " and " + port2 + " ready"); + boolean accept2 = acceptSSLServer(sss2); + boolean accept1 = ass.join(); + assertTrue(firstSucceeds ^ accept1); boolean secondsSucceeds = sslVariant != 0; - assertTrue(secondsSucceeds ^ acceptSSLServer(sss2)); + assertTrue(secondsSucceeds ^ accept2); - TestMail mail = getMailReciever().recieve(); + TestMail mail = getMailReciever().receive(); if (emailVariant == 0) { - String link = mail.extractLink(); - new URL(link).openConnection().getHeaderField(""); + mail.verify(); } waitForPings(3); @@ -137,13 +185,12 @@ public class TestSSL extends PingTest { private void createCertificate(String test, CertificateProfile profile) throws GeneralSecurityException, IOException, SQLException, InterruptedException, GigiApiException { kp = generateKeypair(); String csr = generatePEMCSR(kp, "CN=" + test); - c = new Certificate(userid, "/CN=" + test, "sha256", csr, CSRType.CSR, profile); + c = new Certificate(User.getById(id), Certificate.buildDN("CN", test), "sha256", csr, CSRType.CSR, profile); c.issue(null, "2y").waitFor(60000); } private boolean acceptSSLServer(SSLServerSocket sss) throws IOException { - try { - Socket s = sss.accept(); + try (Socket s = sss.accept()) { s.getOutputStream().write('b'); s.getOutputStream().close(); return true; @@ -221,4 +268,11 @@ public class TestSSL extends PingTest { return (SSLServerSocket) sssf.createServerSocket(0); } + public static void main(String[] args) throws Exception { + initEnvironment(); + TestSSL t1 = new TestSSL(); + t1.sslAndMailSuccess(); + tearDownServer(); + } + }