james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [07/11] james-project git commit: Revert "JAMES-1862 Refactor NettyStartTlsSMTPServerTest"
Date Tue, 06 Dec 2016 10:42:12 GMT
Revert "JAMES-1862 Refactor NettyStartTlsSMTPServerTest"

This reverts commit 90e6d8a999d3b10e594eebf1af7ab7192d52857f.


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

Branch: refs/heads/master
Commit: d5f1da471a2739c2a622db0d6d2e82d9a6234599
Parents: f75d4f7
Author: Antoine Duprat <aduprat@apache.org>
Authored: Tue Dec 6 11:41:10 2016 +0100
Committer: Antoine Duprat <aduprat@apache.org>
Committed: Tue Dec 6 11:41:10 2016 +0100

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


http://git-wip-us.apache.org/repos/asf/james-project/blob/d5f1da47/protocols/smtp/pom.xml
----------------------------------------------------------------------
diff --git a/protocols/smtp/pom.xml b/protocols/smtp/pom.xml
index aaf2b49..7adc77b 100644
--- a/protocols/smtp/pom.xml
+++ b/protocols/smtp/pom.xml
@@ -50,10 +50,6 @@
             <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/d5f1da47/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 5a8058c..c9dda94 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,9 +22,11 @@ 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;
@@ -47,26 +49,16 @@ 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;
     }
 
@@ -76,116 +68,99 @@ public class NettyStartTlsSMTPServerTest {
         return client;
     }
 
-    private Protocol createProtocol(Optional<ProtocolHandler> handler) throws WiringException
{
+    private Protocol createProtocol(ProtocolHandler... handlers) throws WiringException {
         SMTPProtocolHandlerChain chain = new SMTPProtocolHandlerChain();
-        if (handler.isPresent()) {
-            chain.add(handler.get());
-        }
+        chain.addAll(0, Arrays.asList(handlers));
         chain.wireExtensibleHandlers();
         return new SMTPProtocol(chain, new SMTPConfigurationImpl(), new MockLogger());
     }
 
     @Test
-    public void connectShouldReturnTrueWhenConnecting() 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());
-        assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue();
-
-        client.quit();
-        client.disconnect();
-    }
-
-    @Test
-    public void ehloShouldReturnTrueWhenSendingTheCommand() throws Exception {
+    public void testStartTLS() 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();
         
-        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;
+        
+        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();
             }
         }
-        return false;
+        
     }
-
+    
     @Test
-    public void startTlsShouldReturnTrueWhenServerSupportsIt() throws Exception {
+    public void testStartTLSWithJavamail() 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();
+        
+        
+        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();
+            }
+        }
+        
     }
 
-    @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-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message