1 package org.cacert.gigi.email;
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.net.SocketTimeoutException;
10 import java.util.Properties;
13 * This class intercepts emails so that the test cases can evaluate them
16 public class TestEmailProvider extends DelegateMailProvider {
18 private ServerSocket servs;
20 private Socket client;
22 private DataOutputStream out;
24 private DataInputStream in;
26 protected TestEmailProvider(Properties props) {
27 super(props, props.getProperty("emailProvider.test.target"));
29 servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
30 } catch (IOException e) {
36 public synchronized void sendMail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
38 if ( !ensureLocalConnection() && getTarget() != null) {
39 super.sendMail(to, subject, message, from, replyto, toname, fromname, errorsto, extra);
54 } catch (IOException e) {
60 private boolean ensureLocalConnection() throws IOException {
64 } catch (IOException e) {
68 if (client == null || client.isClosed()) {
69 servs.setSoTimeout(2000);
71 client = servs.accept();
72 } catch (SocketTimeoutException e) {
75 out = new DataOutputStream(client.getOutputStream());
76 in = new DataInputStream(client.getInputStream());
82 public synchronized String checkEmailServer(int forUid, String address) throws IOException {
84 if ( !ensureLocalConnection() && getTarget() != null) {
85 return super.checkEmailServer(forUid, address);
88 out.writeUTF("challengeAddrBox");
89 out.writeUTF(address);
91 } catch (IOException e) {
97 private void write(String to) throws IOException {
99 out.writeUTF("<null>");