]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[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                 public TestMail(String to, String subject, String message, String from,
21                                 String replyto) {
22                         this.to = to;
23                         this.subject = subject;
24                         this.message = message;
25                         this.from = from;
26                         this.replyto = replyto;
27                 }
28                 public String getTo() {
29                         return to;
30                 }
31                 public String getSubject() {
32                         return subject;
33                 }
34                 public String getMessage() {
35                         return message;
36                 }
37                 public String getFrom() {
38                         return from;
39                 }
40                 public String getReplyto() {
41                         return replyto;
42                 }
43                 public String extractLink() {
44                         Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)");
45                         Matcher m = link.matcher(getMessage());
46                         m.find();
47                         return m.group(0);
48                 }
49
50         }
51         private Socket s;
52         private DataInputStream dis;
53         private DataOutputStream dos;
54
55         public TestEmailReciever(SocketAddress target) throws IOException {
56                 s = new Socket();
57                 s.connect(target);
58                 s.setKeepAlive(true);
59                 s.setSoTimeout(1000 * 60 * 60);
60                 dis = new DataInputStream(s.getInputStream());
61                 dos = new DataOutputStream(s.getOutputStream());
62                 new Thread(this).start();
63         }
64         LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
65
66         public TestMail recieve() throws InterruptedException {
67                 return mails.poll(5, TimeUnit.SECONDS);
68         }
69         @Override
70         public void run() {
71                 try {
72                         while (true) {
73                                 String type = dis.readUTF();
74                                 if (type.equals("mail")) {
75                                         String to = dis.readUTF();
76                                         String subject = dis.readUTF();
77                                         String message = dis.readUTF();
78                                         String from = dis.readUTF();
79                                         String replyto = dis.readUTF();
80                                         mails.add(new TestMail(to, subject, message, from, replyto));
81                                 } else if (type.equals("challengeAddrBox")) {
82                                         String email = dis.readUTF();
83                                         if (approveRegex.matcher(email).matches()) {
84                                                 System.out.println("approving mbox: " + email);
85                                                 dos.writeUTF("OK");
86                                         } else {
87                                                 System.out.println("rejecting mbox: " + email);
88                                                 dos.writeUTF("FAIL");
89                                         }
90                                         dos.flush();
91                                 } else if (type.equals("ping")) {
92                                 } else {
93                                         System.err.println("Unknown type: " + type);
94                                 }
95                         }
96                 } catch (IOException e) {
97                         e.printStackTrace();
98                 }
99
100         }
101         Pattern approveRegex = Pattern.compile(".*");
102         public void setApproveRegex(Pattern approveRegex) {
103                 this.approveRegex = approveRegex;
104         }
105
106         public void clearMails() {
107                 mails.clear();
108         }
109         public void reset() {
110                 clearMails();
111                 approveRegex = Pattern.compile(".*");
112         }
113
114 }