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