Revert "JAMES-1862 Remove abstract level in StartTls tests" This reverts commit c32479864011c17cb09ca80f332e0d1fd150a324.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/27b63e59 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/27b63e59 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/27b63e59 Branch: refs/heads/master Commit: 27b63e5951d95c691e1fa2eed4a4048ba489001c Parents: a69a813 Author: Antoine Duprat <[email protected]> Authored: Tue Dec 6 11:41:12 2016 +0100 Committer: Antoine Duprat <[email protected]> Committed: Tue Dec 6 11:41:12 2016 +0100 ---------------------------------------------------------------------- .../smtp/AbstractStartTlsSMTPServerTest.java | 165 +++++++++++++++ .../smtp/netty/NettyStartTlsSMTPServerTest.java | 211 ++++--------------- 2 files changed, 204 insertions(+), 172 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/27b63e59/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 new file mode 100644 index 0000000..02f87ec --- /dev/null +++ b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractStartTlsSMTPServerTest.java @@ -0,0 +1,165 @@ +/**************************************************************** + * 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/27b63e59/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 41f3b9c..d643611 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,172 +1,39 @@ -/**************************************************************** - * 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(); - } - } - - } - -} +/**************************************************************** + * 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; + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
