]> WPIA git - gigi.git/commitdiff
Implement short-circuit logic for emails locally in test env.
authorFelix Dörre <felix@dogcraft.de>
Thu, 24 Jul 2014 22:18:34 +0000 (00:18 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 24 Jul 2014 23:44:47 +0000 (01:44 +0200)
src/org/cacert/gigi/email/EmailProvider.java
tests/org/cacert/gigi/testUtils/ManagedTest.java
tests/org/cacert/gigi/testUtils/TestEmailReciever.java

index f6a70912910681b5c34376d3e448a2d944dcc66b..6855f398d1332f73dccebb9bf70aeb2592751f7b 100644 (file)
@@ -23,6 +23,10 @@ public abstract class EmailProvider {
                return instance;
        }
 
+       protected static void setInstance(EmailProvider instance) {
+               EmailProvider.instance = instance;
+       }
+
        public static void init(Properties conf) {
                try {
                        Class<?> c = Class.forName(conf.getProperty("emailProvider"));
index d7a5fcac9dba458d5ce7b09a966b2f1c47e88dfe..724126ab4eb39180eee8bd4d9486f72f85f0e542 100644 (file)
@@ -39,6 +39,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.ServerConstants;
 import org.cacert.gigi.util.SimpleSigner;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -76,8 +77,9 @@ public class ManagedTest {
                        System.out.println("... purging Database");
                        DatabaseManager.run(new String[] { testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"),
                                        testProps.getProperty("sql.user"), testProps.getProperty("sql.password") });
-
                        String type = testProps.getProperty("type");
+                       Properties mainProps = generateMainProps();
+                       ServerConstants.init(mainProps);
                        if (type.equals("local")) {
                                url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
                                String[] parts = testProps.getProperty("mail").split(":", 2);
@@ -88,19 +90,6 @@ public class ManagedTest {
                        gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
                        DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
                        System.out.println("... starting server");
-                       Properties mainProps = new Properties();
-                       mainProps.setProperty("host", "127.0.0.1");
-                       mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
-                       mainProps.setProperty("name.www", testProps.getProperty("name.www"));
-                       mainProps.setProperty("name.static", testProps.getProperty("name.static"));
-
-                       mainProps.setProperty("port", testProps.getProperty("serverPort"));
-                       mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
-                       mainProps.setProperty("emailProvider.port", "8473");
-                       mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
-                       mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
-                       mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
-                       mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
 
                        byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
                        byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
@@ -143,6 +132,23 @@ public class ManagedTest {
 
        }
 
+       private static Properties generateMainProps() {
+               Properties mainProps = new Properties();
+               mainProps.setProperty("host", "127.0.0.1");
+               mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
+               mainProps.setProperty("name.www", testProps.getProperty("name.www"));
+               mainProps.setProperty("name.static", testProps.getProperty("name.static"));
+
+               mainProps.setProperty("port", testProps.getProperty("serverPort"));
+               mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
+               mainProps.setProperty("emailProvider.port", "8473");
+               mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
+               mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
+               mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
+               mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
+               return mainProps;
+       }
+
        @AfterClass
        public static void tearDownServer() {
                String type = testProps.getProperty("type");
index 849bf927f234165190080c0ddba955a44a52cc76..93cd7e4540f17e0d3024b6216117cd3fa42530b7 100644 (file)
@@ -10,7 +10,9 @@ import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class TestEmailReciever implements Runnable {
+import org.cacert.gigi.email.EmailProvider;
+
+public class TestEmailReciever extends EmailProvider implements Runnable {
        public class TestMail {
                String to;
                String subject;
@@ -67,6 +69,7 @@ public class TestEmailReciever implements Runnable {
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                new Thread(this).start();
+               setInstance(this);
        }
 
        LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
@@ -89,11 +92,7 @@ public class TestEmailReciever implements Runnable {
                                        mails.add(new TestMail(to, subject, message, from, replyto));
                                } else if (type.equals("challengeAddrBox")) {
                                        String email = dis.readUTF();
-                                       if (approveRegex.matcher(email).matches()) {
-                                               dos.writeUTF("OK");
-                                       } else {
-                                               dos.writeUTF(error);
-                                       }
+                                       dos.writeUTF(quickEmailCheck(email));
                                        dos.flush();
                                } else if (type.equals("ping")) {
                                } else {
@@ -108,6 +107,14 @@ public class TestEmailReciever implements Runnable {
 
        }
 
+       private String quickEmailCheck(String email) throws IOException {
+               if (approveRegex.matcher(email).matches()) {
+                       return "OK";
+               } else {
+                       return error;
+               }
+       }
+
        String error = "FAIL";
 
        public void setEmailCheckError(String error) {
@@ -141,4 +148,15 @@ public class TestEmailReciever implements Runnable {
                }
        }
 
+       @Override
+       public String checkEmailServer(int forUid, String address) throws IOException {
+               return quickEmailCheck(address);
+       }
+
+       @Override
+       public void sendmail(String to, String subject, String message, String from, String replyto, String toname,
+               String fromname, String errorsto, boolean extra) throws IOException {
+               mails.add(new TestMail(to, subject, message, from, replyto));
+       }
+
 }