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());
100 public void start() {
101 new Thread(this).start();
104 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
106 public TestMail recieve() throws InterruptedException {
107 return mails.poll(5, TimeUnit.SECONDS);
114 String type = dis.readUTF();
115 if (type.equals("mail")) {
116 String to = dis.readUTF();
117 String subject = dis.readUTF();
118 String message = dis.readUTF();
119 String from = dis.readUTF();
120 String replyto = dis.readUTF();
121 mails.add(new TestMail(to, subject, message, from, replyto));
122 } else if (type.equals("challengeAddrBox")) {
123 String email = dis.readUTF();
124 dos.writeUTF(quickEmailCheck(email));
126 } else if (type.equals("ping")) {
128 System.err.println("Unknown type: " + type);
131 } catch (IOException e) {
139 private String quickEmailCheck(String email) throws IOException {
140 if (approveRegex.matcher(email).matches()) {
147 String error = "FAIL";
149 public void setEmailCheckError(String error) {
153 Pattern approveRegex = Pattern.compile(".*");
155 public void setApproveRegex(Pattern approveRegex) {
156 this.approveRegex = approveRegex;
159 public void clearMails() {
163 public void reset() {
166 approveRegex = Pattern.compile(".*");
169 boolean closed = false;
171 public void destroy() {
175 } catch (IOException e) {
181 public String checkEmailServer(int forUid, String address) throws IOException {
182 return quickEmailCheck(address);
186 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
187 mails.add(new TestMail(to, subject, message, from, replyto));