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 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());
97 new Thread(this).start();
101 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
103 public TestMail recieve() throws InterruptedException {
104 return mails.poll(5, TimeUnit.SECONDS);
111 String type = dis.readUTF();
112 if (type.equals("mail")) {
113 String to = dis.readUTF();
114 String subject = dis.readUTF();
115 String message = dis.readUTF();
116 String from = dis.readUTF();
117 String replyto = dis.readUTF();
118 mails.add(new TestMail(to, subject, message, from, replyto));
119 } else if (type.equals("challengeAddrBox")) {
120 String email = dis.readUTF();
121 dos.writeUTF(quickEmailCheck(email));
123 } else if (type.equals("ping")) {
125 System.err.println("Unknown type: " + type);
128 } catch (IOException e) {
136 private String quickEmailCheck(String email) throws IOException {
137 if (approveRegex.matcher(email).matches()) {
144 String error = "FAIL";
146 public void setEmailCheckError(String error) {
150 Pattern approveRegex = Pattern.compile(".*");
152 public void setApproveRegex(Pattern approveRegex) {
153 this.approveRegex = approveRegex;
156 public void clearMails() {
160 public void reset() {
163 approveRegex = Pattern.compile(".*");
166 boolean closed = false;
168 public void destroy() {
172 } catch (IOException e) {
178 public String checkEmailServer(int forUid, String address) throws IOException {
179 return quickEmailCheck(address);
183 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
184 mails.add(new TestMail(to, subject, message, from, replyto));