1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.test;
 24 
 25 import org.openjdk.skara.email.*;
 26 
 27 import java.io.*;
 28 import java.net.*;
 29 import java.time.*;
 30 import java.util.concurrent.ConcurrentLinkedDeque;
 31 import java.util.regex.Pattern;
 32 
 33 public class SMTPServer implements AutoCloseable {
 34     private final ServerSocket serverSocket;
 35     private final Thread acceptThread;
 36     private final ConcurrentLinkedDeque<Email> emails = new ConcurrentLinkedDeque<>();
 37 
 38     private static Pattern ehloPattern = Pattern.compile("^EHLO .*$");
 39     private static Pattern mailFromPattern = Pattern.compile("^MAIL FROM:.*$");
 40     private static Pattern rcptToPattern = Pattern.compile("^RCPT TO:<.*$");
 41     private static Pattern dataPattern = Pattern.compile("^DATA$");
 42     private static Pattern messageEndPattern = Pattern.compile("^\\.$");
 43     private static Pattern quitPattern = Pattern.compile("^QUIT$");
 44 
 45     private class AcceptThread implements Runnable {
 46         private void handleSession(SMTPSession session) throws IOException {
 47             session.sendCommand("220 localhost SMTP", ehloPattern);
 48             session.sendCommand("250 HELP", mailFromPattern);
 49             session.sendCommand("250 FROM OK", rcptToPattern);
 50             session.sendCommand("250 RCPT OK", dataPattern);
 51             session.sendCommand("354 Enter message now, end with .");
 52             var message = session.readLinesUntil(messageEndPattern);
 53             session.sendCommand("250 MESSAGE OK", quitPattern);
 54 
 55             var email = Email.parse(String.join("\n", message));
 56             emails.addLast(email);
 57         }
 58 
 59         @Override
 60         public void run() {
 61             while (!serverSocket.isClosed()) {
 62                 try {
 63                     try (var socket = serverSocket.accept();
 64                          var input = new InputStreamReader(socket.getInputStream());
 65                          var output = new OutputStreamWriter(socket.getOutputStream())) {
 66                         var session = new SMTPSession(input, output);
 67                         handleSession(session);
 68                     }
 69                 } catch (SocketException e) {
 70                     // Socket closed
 71                 } catch (IOException e) {
 72                     throw new UncheckedIOException(e);
 73                 }
 74             }
 75         }
 76     }
 77 
 78     public SMTPServer() throws IOException {
 79         serverSocket = new ServerSocket(0);
 80         acceptThread = new Thread(new AcceptThread());
 81         acceptThread.start();
 82     }
 83 
 84     public String address() {
 85         var host = serverSocket.getInetAddress();
 86         return InetAddress.getLoopbackAddress().getHostAddress() + ":" + serverSocket.getLocalPort();
 87     }
 88 
 89     public Email receive(Duration timeout) {
 90         var start = Instant.now();
 91         while (emails.isEmpty() && start.plus(timeout).isAfter(Instant.now())) {
 92             try {
 93                 Thread.sleep(10);
 94             } catch (InterruptedException ignored) {
 95             }
 96         }
 97 
 98         if (emails.isEmpty()) {
 99             throw new RuntimeException("No email received");
100         }
101         return emails.removeFirst();
102     }
103 
104     @Override
105     public void close() throws IOException {
106         serverSocket.close();
107     }
108 }