JAMES-1862 Refactor NettyStartTlsSMTPServerTest

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/99521482
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/99521482
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/99521482

Branch: refs/heads/master
Commit: 99521482330c8a33b3c51f963a450ebec5fd5d0b
Parents: eece4ac
Author: Antoine Duprat <adup...@apache.org>
Authored: Wed Nov 30 10:56:22 2016 +0100
Committer: Antoine Duprat <adup...@apache.org>
Committed: Thu Dec 1 14:17:03 2016 +0100

----------------------------------------------------------------------
 protocols/smtp/pom.xml                          |   4 +
 .../smtp/netty/NettyStartTlsSMTPServerTest.java | 191 +++++++++++--------
 2 files changed, 112 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/99521482/protocols/smtp/pom.xml
----------------------------------------------------------------------
diff --git a/protocols/smtp/pom.xml b/protocols/smtp/pom.xml
index 7adc77b..aaf2b49 100644
--- a/protocols/smtp/pom.xml
+++ b/protocols/smtp/pom.xml
@@ -50,6 +50,10 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
             <optional>true</optional>

http://git-wip-us.apache.org/repos/asf/james-project/blob/99521482/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettyStartTlsSMTPServerTest.java
----------------------------------------------------------------------
diff --git 
a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettyStartTlsSMTPServerTest.java
 
b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettyStartTlsSMTPServerTest.java
index c9dda94..5a8058c 100644
--- 
a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettyStartTlsSMTPServerTest.java
+++ 
b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettyStartTlsSMTPServerTest.java
@@ -22,11 +22,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.net.InetSocketAddress;
 import java.net.Socket;
-import java.util.Arrays;
 import java.util.Locale;
 import java.util.Properties;
 
-import javax.mail.Address;
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.mail.internet.InternetAddress;
@@ -49,16 +47,26 @@ import 
org.apache.james.protocols.smtp.SMTPConfigurationImpl;
 import org.apache.james.protocols.smtp.SMTPProtocol;
 import org.apache.james.protocols.smtp.SMTPProtocolHandlerChain;
 import org.apache.james.protocols.smtp.utils.TestMessageHook;
+import org.junit.After;
 import org.junit.Test;
 
+import com.google.common.base.Optional;
 import com.sun.mail.smtp.SMTPTransport;
 
 public class NettyStartTlsSMTPServerTest {
 
+    private ProtocolServer server;
+
+    @After
+    public void tearDown() {
+        if (server != null) {
+            server.unbind();
+        }
+    }
+
     private ProtocolServer createServer(Protocol protocol, InetSocketAddress 
address, Encryption enc) {
         NettyServer server = new NettyServer(protocol, enc);
         server.setListenAddresses(address);
-        
         return server;
     }
 
@@ -68,99 +76,116 @@ public class NettyStartTlsSMTPServerTest {
         return client;
     }
 
-    private Protocol createProtocol(ProtocolHandler... handlers) throws 
WiringException {
+    private Protocol createProtocol(Optional<ProtocolHandler> handler) throws 
WiringException {
         SMTPProtocolHandlerChain chain = new SMTPProtocolHandlerChain();
-        chain.addAll(0, Arrays.asList(handlers));
+        if (handler.isPresent()) {
+            chain.add(handler.get());
+        }
         chain.wireExtensibleHandlers();
         return new SMTPProtocol(chain, new SMTPConfigurationImpl(), new 
MockLogger());
     }
 
     @Test
-    public void testStartTLS() throws Exception {
+    public void connectShouldReturnTrueWhenConnecting() throws Exception {
         InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
-        
-        
-        ProtocolServer server = null;
-        try {
-            server = createServer(createProtocol(new ProtocolHandler[0]), 
address, Encryption.createStartTls(BogusSslContextFactory.getServerContext())); 
 
-            server.bind();
-            
-            SMTPSClient client = createClient();
-            client.connect(address.getAddress().getHostAddress(), 
address.getPort());
-            
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue();
-            
-            client.sendCommand("EHLO localhost");
-            
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue();
-            
-            boolean startTLSAnnounced = false;
-            for (String reply: client.getReplyStrings()) {
-                if (reply.toUpperCase(Locale.UK).endsWith("STARTTLS")) {
-                    startTLSAnnounced = true;
-                    break;
-                }
-            }
-            assertThat(startTLSAnnounced).isTrue();
-            assertThat(client.execTLS()).isTrue();
-            
-            client.quit();
-            assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode()))
-                .as("Reply="+ client.getReplyString())
-                .isTrue();
-            
-            client.disconnect();
-        } finally {
-            if (server != null) {
-                server.unbind();
-            }
-        }
-        
+        ProtocolServer server = 
createServer(createProtocol(Optional.<ProtocolHandler> absent()), address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
+        server.bind();
+
+        SMTPSClient client = createClient();
+        client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+        
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue();
+
+        client.quit();
+        client.disconnect();
     }
-    
+
     @Test
-    public void testStartTLSWithJavamail() throws Exception {
+    public void ehloShouldReturnTrueWhenSendingTheCommand() throws Exception {
         InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
+        ProtocolServer server = 
createServer(createProtocol(Optional.<ProtocolHandler> absent()), address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
+        server.bind();
+
+        SMTPSClient client = createClient();
+        client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+
+        client.sendCommand("EHLO localhost");
+        
assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue();
         
-        
-        ProtocolServer server = null;
-        try {
-            TestMessageHook hook = new TestMessageHook();
-            server = createServer(createProtocol(hook) , address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
-            server.bind();
-            
-            
-            Properties mailProps = new Properties();
-            mailProps.put("mail.smtp.from", "test@localhost");
-            mailProps.put("mail.smtp.host", address.getHostName());
-            mailProps.put("mail.smtp.port", address.getPort());
-            mailProps.put("mail.smtp.socketFactory.class", 
BogusSSLSocketFactory.class.getName());
-            mailProps.put("mail.smtp.socketFactory.fallback", "false");
-            mailProps.put("mail.smtp.starttls.enable", "true");
-
-            Session mailSession = Session.getDefaultInstance(mailProps);
-
-            MimeMessage message = new MimeMessage(mailSession);
-            message.setFrom(new InternetAddress("test@localhost"));
-            String[] emails = { "valid@localhost" };
-            Address rcpts[] = new Address[emails.length];
-            for (int i = 0; i < emails.length; i++) {
-                rcpts[i] = new InternetAddress(emails[i].trim().toLowerCase());
-            }
-            message.setRecipients(Message.RecipientType.TO, rcpts);
-            message.setSubject("Testmail", "UTF-8");
-            message.setText("Test.....");
-
-            SMTPTransport transport = (SMTPTransport) 
mailSession.getTransport("smtps");
-            
-            transport.connect(new Socket(address.getHostName(), 
address.getPort()));
-            transport.sendMessage(message, rcpts);
-            
-            assertThat(hook.getQueued()).hasSize(1);
-        } finally {
-            if (server != null) {
-                server.unbind();
+        client.quit();
+        client.disconnect();
+    }
+
+    @Test
+    public void startTlsShouldBeAnnouncedWhenServerSupportsIt() throws 
Exception {
+        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
+        ProtocolServer server = 
createServer(createProtocol(Optional.<ProtocolHandler> absent()), address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
+        server.bind();
+
+        SMTPSClient client = createClient();
+        client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+        client.sendCommand("EHLO localhost");
+
+        assertThat(isStartTLSAnnounced(client)).isTrue();
+
+        client.quit();
+        client.disconnect();
+    }
+
+    private boolean isStartTLSAnnounced(SMTPSClient client) {
+        for (String reply: client.getReplyStrings()) {
+            if (reply.toUpperCase(Locale.UK).endsWith("STARTTLS")) {
+                return true;
             }
         }
-        
+        return false;
+    }
+
+    @Test
+    public void startTlsShouldReturnTrueWhenServerSupportsIt() throws 
Exception {
+        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
+        ProtocolServer server = 
createServer(createProtocol(Optional.<ProtocolHandler> absent()), address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
+        server.bind();
+
+        SMTPSClient client = createClient();
+        client.connect(address.getAddress().getHostAddress(), 
address.getPort());
+        client.sendCommand("EHLO localhost");
+
+        boolean execTLS = client.execTLS();
+        assertThat(execTLS).isTrue();
+
+        client.quit();
+        client.disconnect();
     }
 
+    @Test
+    public void startTlsShouldWorkWhenUsingJavamail() throws Exception {
+        TestMessageHook hook = new TestMessageHook();
+        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 
TestUtils.getFreePort());
+        ProtocolServer server = 
createServer(createProtocol(Optional.<ProtocolHandler> of(hook)) , address, 
Encryption.createStartTls(BogusSslContextFactory.getServerContext()));  
+        server.bind();
+
+        Properties mailProps = new Properties();
+        mailProps.put("mail.smtp.from", "test@localhost");
+        mailProps.put("mail.smtp.host", address.getHostName());
+        mailProps.put("mail.smtp.port", address.getPort());
+        mailProps.put("mail.smtp.socketFactory.class", 
BogusSSLSocketFactory.class.getName());
+        mailProps.put("mail.smtp.socketFactory.fallback", "false");
+        mailProps.put("mail.smtp.starttls.enable", "true");
+
+        Session mailSession = Session.getDefaultInstance(mailProps);
+
+        InternetAddress[] rcpts = new InternetAddress[] { new 
InternetAddress("valid@localhost") };
+        MimeMessage message = new MimeMessage(mailSession);
+        message.setFrom(new InternetAddress("test@localhost"));
+        message.setRecipients(Message.RecipientType.TO, rcpts);
+        message.setSubject("Testmail", "UTF-8");
+        message.setText("Test.....");
+
+        SMTPTransport transport = (SMTPTransport) 
mailSession.getTransport("smtps");
+
+        transport.connect(new Socket(address.getHostName(), 
address.getPort()));
+        transport.sendMessage(message, rcpts);
+
+        assertThat(hook.getQueued()).hasSize(1);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to