X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FTestEmailReciever.java;fp=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FTestEmailReciever.java;h=446d2aad769d59fe5efd55b71f3ebde148eaa23a;hp=53e461625eea2ccfd9783e32925f9a00731cd7ee;hb=a95fcf745db63e39d16aa3ec34a4d4f00b9b60d5;hpb=2b2cdb102fe3f3a34d8fcfd22e24b30ca09fe4ba diff --git a/tests/org/cacert/gigi/testUtils/TestEmailReciever.java b/tests/org/cacert/gigi/testUtils/TestEmailReciever.java index 53e46162..446d2aad 100644 --- a/tests/org/cacert/gigi/testUtils/TestEmailReciever.java +++ b/tests/org/cacert/gigi/testUtils/TestEmailReciever.java @@ -1,11 +1,14 @@ package org.cacert.gigi.testUtils; import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.IOException; import java.net.SocketAddress; import java.net.Socket; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class TestEmailReciever implements Runnable { public class TestMail { @@ -37,10 +40,17 @@ public class TestEmailReciever implements Runnable { public String getReplyto() { return replyto; } + public String extractLink() { + Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)"); + Matcher m = link.matcher(getMessage()); + m.find(); + return m.group(0); + } } private Socket s; private DataInputStream dis; + private DataOutputStream dos; public TestEmailReciever(SocketAddress target) throws IOException { s = new Socket(); @@ -48,6 +58,7 @@ public class TestEmailReciever implements Runnable { s.setKeepAlive(true); s.setSoTimeout(1000 * 60 * 60); dis = new DataInputStream(s.getInputStream()); + dos = new DataOutputStream(s.getOutputStream()); new Thread(this).start(); } LinkedBlockingQueue mails = new LinkedBlockingQueue(); @@ -59,21 +70,44 @@ public class TestEmailReciever implements Runnable { public void run() { try { while (true) { - String to = dis.readUTF(); - String subject = dis.readUTF(); - String message = dis.readUTF(); - String from = dis.readUTF(); - String replyto = dis.readUTF(); - mails.add(new TestMail(to, subject, message, from, replyto)); + String type = dis.readUTF(); + if (type.equals("mail")) { + String to = dis.readUTF(); + String subject = dis.readUTF(); + String message = dis.readUTF(); + String from = dis.readUTF(); + String replyto = dis.readUTF(); + 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.flush(); + } else { + System.err.println("Unknown type: " + type); + } } } catch (IOException e) { e.printStackTrace(); } } + Pattern approveRegex = Pattern.compile(".*"); + public void setApproveRegex(Pattern approveRegex) { + this.approveRegex = approveRegex; + } public void clearMails() { mails.clear(); } + public void reset() { + clearMails(); + approveRegex = Pattern.compile(".*"); + } }