JAMES-1862 Remove abstract level in StartTls tests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5ca0f7b0 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5ca0f7b0 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5ca0f7b0 Branch: refs/heads/master Commit: 5ca0f7b0366b49a5c7b32d25d8ccb8525bb9582c Parents: 8633537 Author: Antoine Duprat <adup...@apache.org> Authored: Wed Nov 30 10:27:35 2016 +0100 Committer: Antoine Duprat <adup...@apache.org> Committed: Thu Dec 1 14:17:03 2016 +0100 ---------------------------------------------------------------------- .../smtp/AbstractStartTlsSMTPServerTest.java | 165 --------------- .../smtp/netty/NettyStartTlsSMTPServerTest.java | 211 +++++++++++++++---- 2 files changed, 172 insertions(+), 204 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5ca0f7b0/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractStartTlsSMTPServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractStartTlsSMTPServerTest.java b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractStartTlsSMTPServerTest.java deleted file mode 100644 index 02f87ec..0000000 --- a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractStartTlsSMTPServerTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.protocols.smtp; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -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; -import javax.mail.internet.MimeMessage; - -import org.apache.commons.net.smtp.SMTPReply; -import org.apache.commons.net.smtp.SMTPSClient; -import org.apache.james.protocols.api.Encryption; -import org.apache.james.protocols.api.Protocol; -import org.apache.james.protocols.api.ProtocolServer; -import org.apache.james.protocols.api.handler.ProtocolHandler; -import org.apache.james.protocols.api.handler.WiringException; -import org.apache.james.protocols.api.utils.BogusSSLSocketFactory; -import org.apache.james.protocols.api.utils.BogusSslContextFactory; -import org.apache.james.protocols.api.utils.BogusTrustManagerFactory; -import org.apache.james.protocols.api.utils.MockLogger; -import org.apache.james.protocols.api.utils.TestUtils; -import org.apache.james.protocols.smtp.utils.TestMessageHook; -import org.junit.Test; - -import com.sun.mail.smtp.SMTPTransport; - -public abstract class AbstractStartTlsSMTPServerTest { - - protected SMTPSClient createClient() { - SMTPSClient client = new SMTPSClient(false, BogusSslContextFactory.getClientContext()); - client.setTrustManager(BogusTrustManagerFactory.getTrustManagers()[0]); - return client; - } - - protected abstract ProtocolServer createServer(Protocol protocol, InetSocketAddress address, Encryption enc); - - - protected Protocol createProtocol(ProtocolHandler... handlers) throws WiringException { - SMTPProtocolHandlerChain chain = new SMTPProtocolHandlerChain(); - chain.addAll(0, Arrays.asList(handlers)); - chain.wireExtensibleHandlers(); - return new SMTPProtocol(chain, new SMTPConfigurationImpl(), new MockLogger()); - } - - - @Test - public void testStartTLS() 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()); - assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); - - client.sendCommand("EHLO localhost"); - assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); - - boolean startTLSAnnounced = false; - for (String reply: client.getReplyStrings()) { - if (reply.toUpperCase(Locale.UK).endsWith("STARTTLS")) { - startTLSAnnounced = true; - break; - } - } - assertTrue(startTLSAnnounced); - - assertTrue(client.execTLS()); - - client.quit(); - assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); - - client.disconnect(); - - - } finally { - if (server != null) { - server.unbind(); - } - } - - } - - @Test - public void testStartTLSWithJavamail() throws Exception { - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - - 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); - - - assertEquals(1, hook.getQueued().size()); - - - - } finally { - if (server != null) { - server.unbind(); - } - } - - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/5ca0f7b0/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 d643611..41f3b9c 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 @@ -1,39 +1,172 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.protocols.smtp.netty; - -import java.net.InetSocketAddress; - -import org.apache.james.protocols.api.Encryption; -import org.apache.james.protocols.api.Protocol; -import org.apache.james.protocols.api.ProtocolServer; -import org.apache.james.protocols.netty.NettyServer; -import org.apache.james.protocols.smtp.AbstractStartTlsSMTPServerTest; - -public class NettyStartTlsSMTPServerTest extends AbstractStartTlsSMTPServerTest{ - - @Override - protected ProtocolServer createServer(Protocol protocol, InetSocketAddress address, Encryption enc) { - NettyServer server = new NettyServer(protocol, enc); - server.setListenAddresses(address); - - return server; - } - -} +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.protocols.smtp.netty; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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; +import javax.mail.internet.MimeMessage; + +import org.apache.commons.net.smtp.SMTPReply; +import org.apache.commons.net.smtp.SMTPSClient; +import org.apache.james.protocols.api.Encryption; +import org.apache.james.protocols.api.Protocol; +import org.apache.james.protocols.api.ProtocolServer; +import org.apache.james.protocols.api.handler.ProtocolHandler; +import org.apache.james.protocols.api.handler.WiringException; +import org.apache.james.protocols.api.utils.BogusSSLSocketFactory; +import org.apache.james.protocols.api.utils.BogusSslContextFactory; +import org.apache.james.protocols.api.utils.BogusTrustManagerFactory; +import org.apache.james.protocols.api.utils.MockLogger; +import org.apache.james.protocols.api.utils.TestUtils; +import org.apache.james.protocols.netty.NettyServer; +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.Test; + +import com.sun.mail.smtp.SMTPTransport; + +public class NettyStartTlsSMTPServerTest { + + private ProtocolServer createServer(Protocol protocol, InetSocketAddress address, Encryption enc) { + NettyServer server = new NettyServer(protocol, enc); + server.setListenAddresses(address); + + return server; + } + + private SMTPSClient createClient() { + SMTPSClient client = new SMTPSClient(false, BogusSslContextFactory.getClientContext()); + client.setTrustManager(BogusTrustManagerFactory.getTrustManagers()[0]); + return client; + } + + private Protocol createProtocol(ProtocolHandler... handlers) throws WiringException { + SMTPProtocolHandlerChain chain = new SMTPProtocolHandlerChain(); + chain.addAll(0, Arrays.asList(handlers)); + chain.wireExtensibleHandlers(); + return new SMTPProtocol(chain, new SMTPConfigurationImpl(), new MockLogger()); + } + + @Test + public void testStartTLS() 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()); + assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); + + client.sendCommand("EHLO localhost"); + assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); + + boolean startTLSAnnounced = false; + for (String reply: client.getReplyStrings()) { + if (reply.toUpperCase(Locale.UK).endsWith("STARTTLS")) { + startTLSAnnounced = true; + break; + } + } + assertTrue(startTLSAnnounced); + + assertTrue(client.execTLS()); + + client.quit(); + assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); + + client.disconnect(); + + + } finally { + if (server != null) { + server.unbind(); + } + } + + } + + @Test + public void testStartTLSWithJavamail() throws Exception { + InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); + + + 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); + + + assertEquals(1, hook.getQueued().size()); + + + + } finally { + if (server != null) { + server.unbind(); + } + } + + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org