]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
[EMPTY] Organize all imports
[gigi.git] / tests / org / cacert / gigi / testUtils / TestEmailReciever.java
1 package org.cacert.gigi.testUtils;
2
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;
8 import java.util.concurrent.LinkedBlockingQueue;
9 import java.util.concurrent.TimeUnit;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.cacert.gigi.email.EmailProvider;
14
15 public class TestEmailReciever extends EmailProvider implements Runnable {
16
17     public class TestMail {
18
19         String to;
20
21         String subject;
22
23         String message;
24
25         String from;
26
27         String replyto;
28
29         public TestMail(String to, String subject, String message, String from, String replyto) {
30             this.to = to;
31             this.subject = subject;
32             this.message = message;
33             this.from = from;
34             this.replyto = replyto;
35         }
36
37         public String getTo() {
38             return to;
39         }
40
41         public String getSubject() {
42             return subject;
43         }
44
45         public String getMessage() {
46             return message;
47         }
48
49         public String getFrom() {
50             return from;
51         }
52
53         public String getReplyto() {
54             return replyto;
55         }
56
57         public String extractLink() {
58             Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
59             Matcher m = link.matcher(getMessage());
60             m.find();
61             return m.group(0);
62         }
63
64     }
65
66     private Socket s;
67
68     private DataInputStream dis;
69
70     private DataOutputStream dos;
71
72     public TestEmailReciever(SocketAddress target) throws IOException {
73         s = new Socket();
74         s.connect(target);
75         s.setKeepAlive(true);
76         s.setSoTimeout(1000 * 60 * 60);
77         dis = new DataInputStream(s.getInputStream());
78         dos = new DataOutputStream(s.getOutputStream());
79         new Thread(this).start();
80         setInstance(this);
81     }
82
83     LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
84
85     public TestMail recieve() throws InterruptedException {
86         return mails.poll(5, TimeUnit.SECONDS);
87     }
88
89     @Override
90     public void run() {
91         try {
92             while (true) {
93                 String type = dis.readUTF();
94                 if (type.equals("mail")) {
95                     String to = dis.readUTF();
96                     String subject = dis.readUTF();
97                     String message = dis.readUTF();
98                     String from = dis.readUTF();
99                     String replyto = dis.readUTF();
100                     mails.add(new TestMail(to, subject, message, from, replyto));
101                 } else if (type.equals("challengeAddrBox")) {
102                     String email = dis.readUTF();
103                     dos.writeUTF(quickEmailCheck(email));
104                     dos.flush();
105                 } else if (type.equals("ping")) {
106                 } else {
107                     System.err.println("Unknown type: " + type);
108                 }
109             }
110         } catch (IOException e) {
111             if ( !closed) {
112                 e.printStackTrace();
113             }
114         }
115
116     }
117
118     private String quickEmailCheck(String email) throws IOException {
119         if (approveRegex.matcher(email).matches()) {
120             return "OK";
121         } else {
122             return error;
123         }
124     }
125
126     String error = "FAIL";
127
128     public void setEmailCheckError(String error) {
129         this.error = error;
130     }
131
132     Pattern approveRegex = Pattern.compile(".*");
133
134     public void setApproveRegex(Pattern approveRegex) {
135         this.approveRegex = approveRegex;
136     }
137
138     public void clearMails() {
139         mails.clear();
140     }
141
142     public void reset() {
143         clearMails();
144         error = "FAIL";
145         approveRegex = Pattern.compile(".*");
146     }
147
148     boolean closed = false;
149
150     public void destroy() {
151         try {
152             closed = true;
153             s.close();
154         } catch (IOException e) {
155             e.printStackTrace();
156         }
157     }
158
159     @Override
160     public String checkEmailServer(int forUid, String address) throws IOException {
161         return quickEmailCheck(address);
162     }
163
164     @Override
165     public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
166         mails.add(new TestMail(to, subject, message, from, replyto));
167     }
168
169 }