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;
16 import org.cacert.gigi.email.TestEmailProvider;
19 * This class reveives emails from the current system under test. It is the
20 * counterpart to the {@link TestEmailProvider} who is loaded into the system to
21 * intercept the emails. This class resides in the VM that executes the
22 * testcases and supplies the intercepted emails to the current test case.
24 public final class TestEmailReceiver extends EmailProvider implements Runnable, MailReceiver {
27 * An email that has been intercepted.
29 public static class TestMail {
39 public TestMail(String to, String subject, String message, String replyto) {
41 this.subject = subject;
42 this.message = message;
43 this.replyto = replyto;
46 public String getTo() {
50 public String getSubject() {
54 public String getMessage() {
58 public String getReplyto() {
62 public String extractLink() {
63 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
64 Matcher m = link.matcher(getMessage());
69 public void verify() throws IOException {
70 String link = extractLink();
71 String[] parts = link.split("\\?");
72 URL u = new URL("https://" + ManagedTest.getServerName() + "/verify?" + parts[1]);
74 URLConnection csrfConn = u.openConnection();
75 String csrf = ManagedTest.getCSRF(csrfConn, 0);
77 u = new URL("https://" + ManagedTest.getServerName() + "/verify");
78 URLConnection uc = u.openConnection();
79 ManagedTest.cookie(uc, ManagedTest.stripCookie(csrfConn.getHeaderField("Set-Cookie")));
81 uc.getOutputStream().write((parts[1] + "&csrf=" + csrf).getBytes("UTF-8"));
83 uc.getInputStream().close();
90 private DataInputStream dis;
92 private DataOutputStream dos;
95 * Creates a new TestEmailReceiver based on the address where the
96 * {@link TestEmailProvider} is listening. This class is only ready after
97 * {@link #start()} has been called.
100 * the address where the {@link TestEmailProvider} is listening.
101 * @throws IOException
102 * if the connection cannot be opened
104 public TestEmailReceiver(SocketAddress target) throws IOException {
107 s.setKeepAlive(true);
108 s.setSoTimeout(1000 * 60 * 60);
109 dis = new DataInputStream(s.getInputStream());
110 dos = new DataOutputStream(s.getOutputStream());
115 * Spawns a new {@link Thread} that reads incoming {@link TestMail}s.
119 public void start() {
120 new Thread(this, "Mail receiver").start();
123 private LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReceiver.TestMail>();
126 * Retrieves an outgoing mail from the system. The method will return a
127 * {@link TestMail} or fail.
129 * @return The intercepted {@link TestMail}
133 public TestMail receive() {
137 poll = mails.poll(60, TimeUnit.SECONDS);
139 } catch (InterruptedException e) {
140 throw new AssertionError("Interrupted while receiving mails");
143 throw new AssertionError("Mail receiving timed out");
150 * Retrieves an outgoing mail from the system or returns <code>null</code>
151 * if there was no mail sent in 30 seconds.
153 * @return The intercepted {@link TestMail} or <code>null</code> if no mail
157 public TestMail poll() {
165 String type = dis.readUTF();
166 if (type.equals("mail")) {
167 String to = dis.readUTF();
168 String subject = dis.readUTF();
169 String message = dis.readUTF();
170 String replyto = dis.readUTF();
171 mails.add(new TestMail(to, subject, message, replyto));
172 } else if (type.equals("challengeAddrBox")) {
173 String email = dis.readUTF();
174 dos.writeUTF(quickEmailCheck(email));
176 } else if (type.equals("ping")) {
178 System.err.println("Unknown type: " + type);
181 } catch (IOException e) {
189 private String quickEmailCheck(String email) throws IOException {
190 if (approveRegex.matcher(email).matches()) {
197 String error = "FAIL";
200 * Sets the error that will be sent back to incoming "fast mail checks" that
201 * only check for the availability of a mailbox.
204 * the error Massage to return in
205 * {@link EmailProvider#checkEmailServer(int, String)}
207 public void setEmailCheckError(String error) {
211 private Pattern approveRegex = Pattern.compile(".*");
214 * Specifies a pattern that will be used for incoming
215 * {@link EmailProvider#checkEmailServer(int, String)} calls to determine
216 * whether the mailbox should exist.
218 * @param approveRegex
219 * the regex that will perform the check
221 public void setApproveRegex(Pattern approveRegex) {
222 this.approveRegex = approveRegex;
226 * Removes all queued mails.
229 public void clearMails() {
234 * Resets this class to its initial state
237 * @see #setApproveRegex(Pattern)
238 * @see #setEmailCheckError(String)
240 public void reset() {
243 approveRegex = Pattern.compile(".*");
246 private boolean closed = false;
249 * stops reading for incoming messages
253 public void destroy() {
257 } catch (IOException e) {
263 public String checkEmailServer(int forUid, String address) throws IOException {
264 return quickEmailCheck(address);
268 public void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
269 mails.add(new TestMail(to, subject, message, replyto));