]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Fix: some nullpointer references (coverity).
[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("UTF-8"));
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         setInstance(this);
98     }
99
100     public void start() {
101         new Thread(this).start();
102     }
103
104     LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
105
106     public TestMail recieve() throws InterruptedException {
107         TestMail poll = mails.poll(5, TimeUnit.SECONDS);
108         return poll;
109     }
110
111     @Override
112     public void run() {
113         try {
114             while (true) {
115                 String type = dis.readUTF();
116                 if (type.equals("mail")) {
117                     String to = dis.readUTF();
118                     String subject = dis.readUTF();
119                     String message = dis.readUTF();
120                     String from = dis.readUTF();
121                     String replyto = dis.readUTF();
122                     mails.add(new TestMail(to, subject, message, from, replyto));
123                 } else if (type.equals("challengeAddrBox")) {
124                     String email = dis.readUTF();
125                     dos.writeUTF(quickEmailCheck(email));
126                     dos.flush();
127                 } else if (type.equals("ping")) {
128                 } else {
129                     System.err.println("Unknown type: " + type);
130                 }
131             }
132         } catch (IOException e) {
133             if ( !closed) {
134                 e.printStackTrace();
135             }
136         }
137
138     }
139
140     private String quickEmailCheck(String email) throws IOException {
141         if (approveRegex.matcher(email).matches()) {
142             return "OK";
143         } else {
144             return error;
145         }
146     }
147
148     String error = "FAIL";
149
150     public void setEmailCheckError(String error) {
151         this.error = error;
152     }
153
154     Pattern approveRegex = Pattern.compile(".*");
155
156     public void setApproveRegex(Pattern approveRegex) {
157         this.approveRegex = approveRegex;
158     }
159
160     public void clearMails() {
161         mails.clear();
162     }
163
164     public void reset() {
165         clearMails();
166         error = "FAIL";
167         approveRegex = Pattern.compile(".*");
168     }
169
170     boolean closed = false;
171
172     public void destroy() {
173         try {
174             closed = true;
175             s.close();
176         } catch (IOException e) {
177             e.printStackTrace();
178         }
179     }
180
181     @Override
182     public String checkEmailServer(int forUid, String address) throws IOException {
183         return quickEmailCheck(address);
184     }
185
186     @Override
187     public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
188         mails.add(new TestMail(to, subject, message, from, replyto));
189     }
190
191 }