]> WPIA git - gigi.git/blob - util-testing/org/cacert/gigi/email/TestEmailProvider.java
ab1fa37bf5a0ab0e6b16c7778ea8f43101a38ab9
[gigi.git] / util-testing / org / cacert / gigi / email / TestEmailProvider.java
1 package org.cacert.gigi.email;
2
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;
11
12 /**
13  * This class intercepts emails so that the test cases can evaluate them
14  * automatically.
15  */
16 public class TestEmailProvider extends DelegateMailProvider {
17
18     private ServerSocket servs;
19
20     private Socket client;
21
22     private DataOutputStream out;
23
24     private DataInputStream in;
25
26     protected TestEmailProvider(Properties props) {
27         super(props, props.getProperty("emailProvider.test.target"));
28         try {
29             servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33     }
34
35     @Override
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 {
37         while (true) {
38             if ( !assureLocalConnection() && getTarget() != null) {
39                 super.sendMail(to, subject, message, from, replyto, toname, fromname, errorsto, extra);
40                 return;
41             }
42             try {
43                 if (out == null) {
44                     continue;
45                 }
46                 out.writeUTF("mail");
47                 write(to);
48                 write(subject);
49                 write(message);
50                 write(from);
51                 write(replyto);
52                 out.flush();
53                 return;
54             } catch (IOException e) {
55                 client = null;
56             }
57         }
58     }
59
60     private boolean assureLocalConnection() throws IOException {
61         if (out != null) {
62             try {
63                 out.writeUTF("ping");
64             } catch (IOException e) {
65                 client = null;
66             }
67         }
68         if (client == null || client.isClosed()) {
69             servs.setSoTimeout(2000);
70             try {
71                 client = servs.accept();
72             } catch (SocketTimeoutException e) {
73                 return false;
74             }
75             out = new DataOutputStream(client.getOutputStream());
76             in = new DataInputStream(client.getInputStream());
77         }
78         return true;
79     }
80
81     @Override
82     public synchronized String checkEmailServer(int forUid, String address) throws IOException {
83         while (true) {
84             if ( !assureLocalConnection() && getTarget() != null) {
85                 return super.checkEmailServer(forUid, address);
86             }
87             try {
88                 out.writeUTF("challengeAddrBox");
89                 out.writeUTF(address);
90                 return in.readUTF();
91             } catch (IOException e) {
92                 client = null;
93             }
94         }
95     }
96
97     private void write(String to) throws IOException {
98         if (to == null) {
99             out.writeUTF("<null>");
100         } else {
101             out.writeUTF(to);
102         }
103     }
104 }