]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Remove the "approving mbox"-debug-text
[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("https?://[^\\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                                                 dos.writeUTF("OK");
85                                         } else {
86                                                 dos.writeUTF("FAIL");
87                                         }
88                                         dos.flush();
89                                 } else if (type.equals("ping")) {
90                                 } else {
91                                         System.err.println("Unknown type: " + type);
92                                 }
93                         }
94                 } catch (IOException e) {
95                         e.printStackTrace();
96                 }
97
98         }
99         Pattern approveRegex = Pattern.compile(".*");
100         public void setApproveRegex(Pattern approveRegex) {
101                 this.approveRegex = approveRegex;
102         }
103
104         public void clearMails() {
105                 mails.clear();
106         }
107         public void reset() {
108                 clearMails();
109                 approveRegex = Pattern.compile(".*");
110         }
111
112 }