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