]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Merge remote-tracking branch 'origin/emailMgmt'
[gigi.git] / tests / org / cacert / gigi / testUtils / TestEmailReciever.java
index e0131a3b3265ad3adb1857448882d74759f9118d..93cd7e4540f17e0d3024b6216117cd3fa42530b7 100644 (file)
@@ -10,44 +10,53 @@ 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;
                String message;
                String from;
                String replyto;
-               public TestMail(String to, String subject, String message, String from,
-                               String replyto) {
+
+               public TestMail(String to, String subject, String message, String from, String replyto) {
                        this.to = to;
                        this.subject = subject;
                        this.message = message;
                        this.from = from;
                        this.replyto = replyto;
                }
+
                public String getTo() {
                        return to;
                }
+
                public String getSubject() {
                        return subject;
                }
+
                public String getMessage() {
                        return message;
                }
+
                public String getFrom() {
                        return from;
                }
+
                public String getReplyto() {
                        return replyto;
                }
+
                public String extractLink() {
-                       Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)");
+                       Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
                        Matcher m = link.matcher(getMessage());
                        m.find();
                        return m.group(0);
                }
 
        }
+
        private Socket s;
        private DataInputStream dis;
        private DataOutputStream dos;
@@ -60,12 +69,15 @@ 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>();
 
        public TestMail recieve() throws InterruptedException {
                return mails.poll(5, TimeUnit.SECONDS);
        }
+
        @Override
        public void run() {
                try {
@@ -80,13 +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()) {
-                                               System.out.println("approving mbox: " + email);
-                                               dos.writeUTF("OK");
-                                       } else {
-                                               System.out.println("rejecting mbox: " + email);
-                                               dos.writeUTF("FAIL");
-                                       }
+                                       dos.writeUTF(quickEmailCheck(email));
                                        dos.flush();
                                } else if (type.equals("ping")) {
                                } else {
@@ -94,11 +100,29 @@ public class TestEmailReciever implements Runnable {
                                }
                        }
                } catch (IOException e) {
-                       e.printStackTrace();
+                       if (!closed) {
+                               e.printStackTrace();
+                       }
                }
 
        }
+
+       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) {
+               this.error = error;
+       }
+
        Pattern approveRegex = Pattern.compile(".*");
+
        public void setApproveRegex(Pattern approveRegex) {
                this.approveRegex = approveRegex;
        }
@@ -106,9 +130,33 @@ public class TestEmailReciever implements Runnable {
        public void clearMails() {
                mails.clear();
        }
+
        public void reset() {
                clearMails();
+               error = "FAIL";
                approveRegex = Pattern.compile(".*");
        }
 
+       boolean closed = false;
+
+       public void destroy() {
+               try {
+                       closed = true;
+                       s.close();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       @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));
+       }
+
 }