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 TestEmailReceiver 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 TestEmailReceiver(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<TestEmailReceiver.TestMail>();
107 * Retrieves an outgoing mail from the system. The method will return a
108 * {@link TestMail} or fail.
110 * @return The intercepted {@link TestMail}
113 public TestMail receive() {
117 poll = mails.poll(60, TimeUnit.SECONDS);
119 } catch (InterruptedException e) {
120 throw new AssertionError("Interrupted while recieving mails");
123 throw new AssertionError("Mail recieving timed out");
130 * Retrieves an outgoing mail from the system or returns <code>null</code>
131 * if there was no mail sent in 30 seconds.
133 * @return The intercepted {@link TestMail} or <code>null</code> if no mail
137 public TestMail poll() {
139 return mails.poll(60, TimeUnit.SECONDS);
141 } catch (InterruptedException e) {
142 throw new AssertionError("Interrupted while recieving mails");
150 String type = dis.readUTF();
151 if (type.equals("mail")) {
152 String to = dis.readUTF();
153 String subject = dis.readUTF();
154 String message = dis.readUTF();
155 String from = dis.readUTF();
156 String replyto = dis.readUTF();
157 mails.add(new TestMail(to, subject, message, from, replyto));
158 } else if (type.equals("challengeAddrBox")) {
159 String email = dis.readUTF();
160 dos.writeUTF(quickEmailCheck(email));
162 } else if (type.equals("ping")) {
164 System.err.println("Unknown type: " + type);
167 } catch (IOException e) {
175 private String quickEmailCheck(String email) throws IOException {
176 if (approveRegex.matcher(email).matches()) {
183 String error = "FAIL";
185 public void setEmailCheckError(String error) {
189 Pattern approveRegex = Pattern.compile(".*");
191 public void setApproveRegex(Pattern approveRegex) {
192 this.approveRegex = approveRegex;
195 public void clearMails() {
199 public void reset() {
202 approveRegex = Pattern.compile(".*");
205 boolean closed = false;
207 public void destroy() {
211 } catch (IOException e) {
217 public String checkEmailServer(int forUid, String address) throws IOException {
218 return quickEmailCheck(address);
222 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
223 mails.add(new TestMail(to, subject, message, from, replyto));