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.security.Key;
11 import java.security.cert.Certificate;
12 import java.util.Properties;
15 * This class intercepts emails so that the test cases can evaluate them
18 public class TestEmailProvider extends EmailProvider {
20 private ServerSocket servs;
22 private Socket client;
24 private DataOutputStream out;
26 private DataInputStream in;
28 private EmailProvider target;
30 protected TestEmailProvider(Properties props) {
32 String name = props.getProperty("emailProvider.test.target");
34 Class<?> c = Class.forName(name);
35 target = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(props);
37 } catch (ReflectiveOperationException e) {
41 servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
42 } catch (IOException e) {
48 public synchronized void sendMail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
50 if ( !assureLocalConnection() && target != null) {
51 target.sendMail(to, subject, message, from, replyto, toname, fromname, errorsto, extra);
66 } catch (IOException e) {
72 private boolean assureLocalConnection() throws IOException {
76 } catch (IOException e) {
80 if (client == null || client.isClosed()) {
81 servs.setSoTimeout(2000);
83 client = servs.accept();
84 } catch (SocketTimeoutException e) {
87 out = new DataOutputStream(client.getOutputStream());
88 in = new DataInputStream(client.getInputStream());
94 public synchronized String checkEmailServer(int forUid, String address) throws IOException {
96 if ( !assureLocalConnection() && target != null) {
97 return checkEmailServer(forUid, address);
100 out.writeUTF("challengeAddrBox");
101 out.writeUTF(address);
103 } catch (IOException e) {
109 private void write(String to) throws IOException {
111 out.writeUTF("<null>");
118 protected void init(Certificate c, Key k) {
120 if (target != null) {