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