X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FTestEmailReciever.java;fp=tests%2Forg%2Fcacert%2Fgigi%2FtestUtils%2FTestEmailReciever.java;h=0000000000000000000000000000000000000000;hb=cd0c67fc376ea0ab65cfcb195efcf095f9942d89;hp=099bbf1a09c7776e8d1760e46bb63023af023a59;hpb=285506739b7109f16dbff1c24a45e0728e8c1b98;p=gigi.git diff --git a/tests/org/cacert/gigi/testUtils/TestEmailReciever.java b/tests/org/cacert/gigi/testUtils/TestEmailReciever.java deleted file mode 100644 index 099bbf1a..00000000 --- a/tests/org/cacert/gigi/testUtils/TestEmailReciever.java +++ /dev/null @@ -1,226 +0,0 @@ -package org.cacert.gigi.testUtils; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.URL; -import java.net.URLConnection; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.cacert.gigi.email.EmailProvider; - -public final class TestEmailReciever extends EmailProvider implements Runnable { - - public class TestMail { - - String to; - - String subject; - - String message; - - String from; - - String replyto; - - public TestMail(String to, String subject, String message, String from, String replyto) { - this.to = to; - this.subject = subject; - this.message = message; - this.from = from; - this.replyto = replyto; - } - - public String getTo() { - return to; - } - - public String getSubject() { - return subject; - } - - public String getMessage() { - return message; - } - - public String getFrom() { - return from; - } - - public String getReplyto() { - return replyto; - } - - public String extractLink() { - Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)"); - Matcher m = link.matcher(getMessage()); - m.find(); - return m.group(0); - } - - public void verify() throws IOException { - String[] parts = extractLink().split("\\?"); - URL u = new URL("https://" + ManagedTest.getServerName() + "/verify?" + parts[1]); - - URLConnection csrfConn = u.openConnection(); - String csrf = ManagedTest.getCSRF(csrfConn, 0); - - u = new URL("https://" + ManagedTest.getServerName() + "/verify"); - URLConnection uc = u.openConnection(); - ManagedTest.cookie(uc, ManagedTest.stripCookie(csrfConn.getHeaderField("Set-Cookie"))); - uc.setDoOutput(true); - uc.getOutputStream().write((parts[1] + "&csrf=" + csrf).getBytes("UTF-8")); - uc.connect(); - uc.getInputStream().close(); - } - - } - - private Socket s; - - private DataInputStream dis; - - private DataOutputStream dos; - - public TestEmailReciever(SocketAddress target) throws IOException { - s = new Socket(); - s.connect(target); - s.setKeepAlive(true); - s.setSoTimeout(1000 * 60 * 60); - dis = new DataInputStream(s.getInputStream()); - dos = new DataOutputStream(s.getOutputStream()); - setInstance(this); - } - - public void start() { - new Thread(this, "Mail reciever").start(); - } - - LinkedBlockingQueue mails = new LinkedBlockingQueue(); - - /** - * Retrieves an outgoing mail from the system. The method will return a - * {@link TestMail} or fail. - * - * @return The intercepted {@link TestMail} - * @see #poll() - */ - public TestMail receive() { - TestMail poll; - - try { - poll = mails.poll(60, TimeUnit.SECONDS); - - } catch (InterruptedException e) { - throw new AssertionError("Interrupted while recieving mails"); - } - if (poll == null) { - throw new AssertionError("Mail recieving timed out"); - } - - return poll; - } - - /** - * Retrieves an outgoing mail from the system or returns null - * if there was no mail sent in 30 seconds. - * - * @return The intercepted {@link TestMail} or null if no mail - * has been sent. - * @see #receive() - */ - public TestMail poll() { - try { - return mails.poll(60, TimeUnit.SECONDS); - - } catch (InterruptedException e) { - throw new AssertionError("Interrupted while recieving mails"); - } - } - - @Override - public void run() { - try { - while (true) { - String type = dis.readUTF(); - if (type.equals("mail")) { - String to = dis.readUTF(); - String subject = dis.readUTF(); - String message = dis.readUTF(); - String from = dis.readUTF(); - String replyto = dis.readUTF(); - mails.add(new TestMail(to, subject, message, from, replyto)); - } else if (type.equals("challengeAddrBox")) { - String email = dis.readUTF(); - dos.writeUTF(quickEmailCheck(email)); - dos.flush(); - } else if (type.equals("ping")) { - } else { - System.err.println("Unknown type: " + type); - } - } - } catch (IOException e) { - if ( !closed) { - e.printStackTrace(); - } - } - - } - - private String quickEmailCheck(String email) throws IOException { - if (approveRegex.matcher(email).matches()) { - return "OK"; - } else { - return error; - } - } - - String error = "FAIL"; - - public void setEmailCheckError(String error) { - this.error = error; - } - - Pattern approveRegex = Pattern.compile(".*"); - - public void setApproveRegex(Pattern approveRegex) { - this.approveRegex = approveRegex; - } - - public void clearMails() { - mails.clear(); - } - - public void reset() { - clearMails(); - error = "FAIL"; - approveRegex = Pattern.compile(".*"); - } - - boolean closed = false; - - public void destroy() { - try { - closed = true; - s.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public String checkEmailServer(int forUid, String address) throws IOException { - return quickEmailCheck(address); - } - - @Override - public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException { - mails.add(new TestMail(to, subject, message, from, replyto)); - } - -}