1 package org.cacert.gigi.testUtils;
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.Socket;
7 import java.net.SocketAddress;
9 import java.net.URLConnection;
10 import java.util.concurrent.LinkedBlockingQueue;
11 import java.util.concurrent.TimeUnit;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
15 import org.cacert.gigi.email.EmailProvider;
17 public final class TestEmailReciever extends EmailProvider implements Runnable {
19 public class TestMail {
31 public TestMail(String to, String subject, String message, String from, String replyto) {
33 this.subject = subject;
34 this.message = message;
36 this.replyto = replyto;
39 public String getTo() {
43 public String getSubject() {
47 public String getMessage() {
51 public String getFrom() {
55 public String getReplyto() {
59 public String extractLink() {
60 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
61 Matcher m = link.matcher(getMessage());
66 public void verify() throws IOException {
67 String[] parts = extractLink().split("\\?");
68 URL u = new URL("https://" + ManagedTest.getServerName() + "/verify?" + parts[1]);
70 URLConnection csrfConn = u.openConnection();
71 String csrf = ManagedTest.getCSRF(csrfConn, 0);
73 u = new URL("https://" + ManagedTest.getServerName() + "/verify");
74 URLConnection uc = u.openConnection();
75 ManagedTest.cookie(uc, ManagedTest.stripCookie(csrfConn.getHeaderField("Set-Cookie")));
77 uc.getOutputStream().write((parts[1] + "&csrf=" + csrf).getBytes("UTF-8"));
79 uc.getInputStream().close();
86 private DataInputStream dis;
88 private DataOutputStream dos;
90 public TestEmailReciever(SocketAddress target) throws IOException {
94 s.setSoTimeout(1000 * 60 * 60);
95 dis = new DataInputStream(s.getInputStream());
96 dos = new DataOutputStream(s.getOutputStream());
100 public void start() {
101 new Thread(this, "Mail reciever").start();
104 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
106 public TestMail recieve() throws InterruptedException {
107 TestMail poll = mails.poll(5, TimeUnit.SECONDS);
115 String type = dis.readUTF();
116 if (type.equals("mail")) {
117 String to = dis.readUTF();
118 String subject = dis.readUTF();
119 String message = dis.readUTF();
120 String from = dis.readUTF();
121 String replyto = dis.readUTF();
122 mails.add(new TestMail(to, subject, message, from, replyto));
123 } else if (type.equals("challengeAddrBox")) {
124 String email = dis.readUTF();
125 dos.writeUTF(quickEmailCheck(email));
127 } else if (type.equals("ping")) {
129 System.err.println("Unknown type: " + type);
132 } catch (IOException e) {
140 private String quickEmailCheck(String email) throws IOException {
141 if (approveRegex.matcher(email).matches()) {
148 String error = "FAIL";
150 public void setEmailCheckError(String error) {
154 Pattern approveRegex = Pattern.compile(".*");
156 public void setApproveRegex(Pattern approveRegex) {
157 this.approveRegex = approveRegex;
160 public void clearMails() {
164 public void reset() {
167 approveRegex = Pattern.compile(".*");
170 boolean closed = false;
172 public void destroy() {
176 } catch (IOException e) {
182 public String checkEmailServer(int forUid, String address) throws IOException {
183 return quickEmailCheck(address);
187 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
188 mails.add(new TestMail(to, subject, message, from, replyto));