ADD: testcases for real mail.
authorFelix Dörre <felix@dogcraft.de>
Mon, 22 Sep 2014 18:40:32 +0000 (20:40 +0200)
committerFelix Dörre <felix@dogcraft.de>
Mon, 22 Sep 2014 18:44:12 +0000 (20:44 +0200)
config/test.properties.template
tests/org/cacert/gigi/email/TestEmailProviderClass.java [new file with mode: 0644]
tests/org/cacert/gigi/email/TestSendmail.java [new file with mode: 0644]
tests/org/cacert/gigi/testUtils/ConfiguredTest.java [new file with mode: 0644]
tests/org/cacert/gigi/testUtils/ManagedTest.java

index 46c1d1d..02cb4d4 100644 (file)
@@ -29,3 +29,10 @@ domain.http=you-intstallation-for-the-textfiles
 domain.dnstest=the.dns.zone
 domain.testns=the.authorativ.ns.for.domain.dnstest
 domain.local=a.domain.that.resolves.to.localhost
+
+
+email.address=somemail@yourdomain.org
+email.password=somemails-imap-password
+email.imap=imap.yourdomain.org
+email.imap.user=somemail-imap-useraccount
+email.non-address=some-non-existent-domain@yourdomain.org
diff --git a/tests/org/cacert/gigi/email/TestEmailProviderClass.java b/tests/org/cacert/gigi/email/TestEmailProviderClass.java
new file mode 100644 (file)
index 0000000..edae6f4
--- /dev/null
@@ -0,0 +1,46 @@
+package org.cacert.gigi.email;
+
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import org.cacert.gigi.email.EmailProvider;
+import org.cacert.gigi.testUtils.ConfiguredTest;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestEmailProviderClass extends ConfiguredTest {
+
+    @Test
+    public void testNonmail() throws IOException {
+        String result = EmailProvider.getInstance().checkEmailServer(0, "nomail");
+        assertNotEquals(EmailProvider.OK, result);
+    }
+
+    @Test
+    public void testFastcheckSucceed() throws IOException {
+        String succmail = getTestProps().getProperty("email.address");
+        assumeNotNull(succmail);
+
+        String result = EmailProvider.getInstance().checkEmailServer(0, succmail);
+        assertEquals(EmailProvider.OK, result);
+    }
+
+    @Test
+    public void testFastcheckFail() throws IOException {
+        String failmail = getTestProps().getProperty("email.non-address");
+        assumeNotNull(failmail);
+
+        String result = EmailProvider.getInstance().checkEmailServer(0, failmail);
+        assertNotEquals(EmailProvider.OK, result);
+    }
+
+    @BeforeClass
+    public static void initMailsystem() {
+        Properties prop = new Properties();
+        prop.setProperty("emailProvider", "org.cacert.gigi.email.Sendmail");
+        EmailProvider.initSystem(prop, null, null);
+    }
+}
diff --git a/tests/org/cacert/gigi/email/TestSendmail.java b/tests/org/cacert/gigi/email/TestSendmail.java
new file mode 100644 (file)
index 0000000..ce51307
--- /dev/null
@@ -0,0 +1,119 @@
+package org.cacert.gigi.email;
+
+import static org.junit.Assume.*;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.security.GeneralSecurityException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SignatureException;
+import java.security.cert.CertificateException;
+import java.util.Base64;
+import java.util.Date;
+import java.util.Properties;
+
+import javax.net.ssl.SSLSocketFactory;
+
+import org.cacert.gigi.testUtils.ConfiguredTest;
+import org.junit.Test;
+
+import sun.security.x509.AlgorithmId;
+import sun.security.x509.CertificateAlgorithmId;
+import sun.security.x509.CertificateSerialNumber;
+import sun.security.x509.CertificateValidity;
+import sun.security.x509.CertificateVersion;
+import sun.security.x509.CertificateX509Key;
+import sun.security.x509.X500Name;
+import sun.security.x509.X509CertImpl;
+import sun.security.x509.X509CertInfo;
+
+public class TestSendmail extends ConfiguredTest {
+
+    @Test
+    public void testSendmail() throws IOException, GeneralSecurityException {
+        initSelfsign();
+
+        String succmail = getTestProps().getProperty("email.address");
+        String pass = getTestProps().getProperty("email.password");
+        String imap = getTestProps().getProperty("email.imap");
+        String imapuser = getTestProps().getProperty("email.imap.user");
+        assumeNotNull(succmail, pass, imap, imapuser);
+
+        String subj = "subj-" + createUniqueName();
+        String msg = "msg-" + createUniqueName();
+        EmailProvider.getInstance().sendmail(succmail, subj, msg, "system@cacert.org", "system@cacert.org", "Testtarget", "Testsender", null, false);
+
+        Socket s = SSLSocketFactory.getDefault().createSocket(imap, 993);
+        PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
+        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
+        pw.println("a001 login " + imapuser + " " + pass);
+        imapUntil(br, "a001");
+        pw.println("a002 select inbox");
+        String overview = imapUntil(br, "a002");
+        overview = overview.replaceFirst(".*\\* ([0-9]+) EXISTS.*", "$1");
+        int cont = Integer.parseInt(overview);
+
+        int msgid = -1;
+        for (int i = 1; i <= cont; i++) {
+            pw.println("m003" + i + " fetch " + i + " body[header]");
+            String body = imapUntil(br, "m003" + i);
+            if (body.contains(subj)) {
+                msgid = i;
+                break;
+            }
+        }
+        assertNotEquals( -1, msgid);
+        pw.println("a003 fetch " + msgid + " body[]");
+        String body = imapUntil(br, "a003");
+        pw.println("delete store " + msgid + " +flags \\deleted");
+        imapUntil(br, "delete");
+        pw.println("exp expunge");
+        imapUntil(br, "exp");
+        pw.println("log logout");
+        imapUntil(br, "log");
+        assertThat(body, containsString("From: support@cacert.local"));
+        assertThat(body, containsString("To: gigi-testuser@dogcraft.de"));
+        assertThat(body, containsString("Subject: " + subj));
+        assertThat(body, containsString(Base64.getEncoder().encodeToString(msg.getBytes("UTF-8"))));
+
+        // TODO maybe verify signature
+
+    }
+
+    private String imapUntil(BufferedReader br, String target) throws IOException {
+        StringBuffer response = new StringBuffer();
+        String line = "";
+        while ( !line.startsWith(target)) {
+            line = br.readLine();
+            response.append(line);
+        }
+        return response.toString();
+    }
+
+    private void initSelfsign() throws GeneralSecurityException, CertificateException, IOException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
+        Properties prop = new Properties();
+        prop.setProperty("emailProvider", "org.cacert.gigi.email.Sendmail");
+        KeyPair kp = generateKeypair();
+        X509CertInfo info = new X509CertInfo();
+        // Add all mandatory attributes
+        info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
+        info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new java.util.Random().nextInt() & 0x7fffffff));
+        AlgorithmId algID = AlgorithmId.get("SHA256WithRSA");
+        info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
+        info.set(X509CertInfo.SUBJECT, new X500Name("EMAIL=system@cacert.org"));
+        info.set(X509CertInfo.KEY, new CertificateX509Key(kp.getPublic()));
+        info.set(X509CertInfo.VALIDITY, new CertificateValidity(new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 60 * 60 * 1000)));
+        info.set(X509CertInfo.ISSUER, new X500Name("CN=test-issue"));
+        X509CertImpl cert = new X509CertImpl(info);
+        cert.sign(kp.getPrivate(), "SHA256WithRSA");
+        EmailProvider.initSystem(prop, cert, kp.getPrivate());
+    }
+}
diff --git a/tests/org/cacert/gigi/testUtils/ConfiguredTest.java b/tests/org/cacert/gigi/testUtils/ConfiguredTest.java
new file mode 100644 (file)
index 0000000..a6bbed8
--- /dev/null
@@ -0,0 +1,92 @@
+package org.cacert.gigi.testUtils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.Signature;
+import java.util.Properties;
+
+import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.util.PEM;
+import org.junit.BeforeClass;
+
+import sun.security.pkcs10.PKCS10;
+import sun.security.pkcs10.PKCS10Attributes;
+import sun.security.x509.X500Name;
+
+public class ConfiguredTest {
+
+    static Properties testProps = new Properties();
+
+    public static Properties getTestProps() {
+        return testProps;
+    }
+
+    private static boolean envInited = false;
+
+    @BeforeClass
+    public static void initEnvironment() throws IOException {
+        if (envInited) {
+            return;
+        }
+        envInited = true;
+        testProps.load(new FileInputStream("config/test.properties"));
+        if ( !DatabaseConnection.isInited()) {
+            DatabaseConnection.init(testProps);
+        }
+    }
+
+    public static KeyPair generateKeypair() throws GeneralSecurityException {
+        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
+        kpg.initialize(4096);
+        KeyPair keyPair = null;
+        File f = new File("testKeypair");
+        if (f.exists()) {
+            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
+                keyPair = (KeyPair) ois.readObject();
+            } catch (ClassNotFoundException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        } else {
+            keyPair = kpg.generateKeyPair();
+            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
+                oos.writeObject(keyPair);
+                oos.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return keyPair;
+    }
+
+    public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
+        return generatePEMCSR(kp, dn, new PKCS10Attributes());
+    }
+
+    public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
+        return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
+    }
+
+    public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
+        PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
+        Signature s = Signature.getInstance(signature);
+        s.initSign(kp.getPrivate());
+        p10.encodeAndSign(new X500Name(dn), s);
+        return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
+    }
+
+    static int count = 0;
+
+    public static String createUniqueName() {
+        return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
+    }
+
+}
index 0b1fb4e..cc02323 100644 (file)
@@ -4,13 +4,8 @@ import static org.junit.Assert.*;
 
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
@@ -22,14 +17,10 @@ import java.net.URLConnection;
 import java.net.URLEncoder;
 import java.nio.file.Files;
 import java.nio.file.Paths;
-import java.security.GeneralSecurityException;
 import java.security.KeyManagementException;
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
 import java.security.NoSuchAlgorithmException;
 import java.security.Principal;
 import java.security.PrivateKey;
-import java.security.Signature;
 import java.security.cert.X509Certificate;
 import java.sql.SQLException;
 import java.util.Locale;
@@ -55,18 +46,13 @@ import org.cacert.gigi.pages.account.MyDetails;
 import org.cacert.gigi.pages.main.RegisterPage;
 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
 import org.cacert.gigi.util.DatabaseManager;
-import org.cacert.gigi.util.PEM;
 import org.cacert.gigi.util.ServerConstants;
 import org.cacert.gigi.util.SimpleSigner;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
-import sun.security.pkcs10.PKCS10;
-import sun.security.pkcs10.PKCS10Attributes;
-import sun.security.x509.X500Name;
-
-public class ManagedTest {
+public class ManagedTest extends ConfiguredTest {
 
     static {
         System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
@@ -93,24 +79,16 @@ public class ManagedTest {
         return url;
     }
 
-    static Properties testProps = new Properties();
-
-    public static Properties getTestProps() {
-        return testProps;
-    }
-
     static {
         InitTruststore.run();
         HttpURLConnection.setFollowRedirects(false);
     }
 
     @BeforeClass
-    public static void connectToServer() {
+    public static void initEnvironment() {
         try {
-            testProps.load(new FileInputStream("config/test.properties"));
-            if ( !DatabaseConnection.isInited()) {
-                DatabaseConnection.init(testProps);
-            }
+            ConfiguredTest.initEnvironment();
+
             purgeDatabase();
             String type = testProps.getProperty("type");
             Properties mainProps = generateMainProps();
@@ -333,12 +311,6 @@ public class ManagedTest {
         return uid;
     }
 
-    static int count = 0;
-
-    public static String createUniqueName() {
-        return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
-    }
-
     private static String stripCookie(String headerField) {
         return headerField.substring(0, headerField.indexOf(';'));
     }
@@ -447,47 +419,6 @@ public class ManagedTest {
         return m.group(1);
     }
 
-    public static KeyPair generateKeypair() throws GeneralSecurityException {
-        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
-        kpg.initialize(4096);
-        KeyPair keyPair = null;
-        File f = new File("testKeypair");
-        if (f.exists()) {
-            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
-                keyPair = (KeyPair) ois.readObject();
-            } catch (ClassNotFoundException e) {
-                e.printStackTrace();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        } else {
-            keyPair = kpg.generateKeyPair();
-            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
-                oos.writeObject(keyPair);
-                oos.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        return keyPair;
-    }
-
-    public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
-        return generatePEMCSR(kp, dn, new PKCS10Attributes());
-    }
-
-    public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
-        return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
-    }
-
-    public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
-        PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
-        Signature s = Signature.getInstance(signature);
-        s.initSign(kp.getPrivate());
-        p10.encodeAndSign(new X500Name(dn), s);
-        return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
-    }
-
     public static String executeBasicWebInteraction(String cookie, String path, String query) throws MalformedURLException, UnsupportedEncodingException, IOException {
         return executeBasicWebInteraction(cookie, path, query, 0);
     }