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