Run LMTP/SMTP tests on a random port
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5f578db9 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5f578db9 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5f578db9 Branch: refs/heads/master Commit: 5f578db9b57446d99e4b01e1cbee498cbf0e8ca4 Parents: 1c73313 Author: Antoine Duprat <[email protected]> Authored: Tue Dec 20 15:47:11 2016 +0100 Committer: Antoine Duprat <[email protected]> Committed: Tue Dec 20 16:48:51 2016 +0100 ---------------------------------------------------------------------- .../protocols/lmtp/AbstractLMTPServerTest.java | 36 ++-- .../lmtp/netty/NettyLMTPSServerTest.java | 7 +- .../lmtp/netty/NettyLMTPServerTest.java | 7 +- .../protocols/smtp/AbstractSMTPSServerTest.java | 9 +- .../protocols/smtp/AbstractSMTPServerTest.java | 175 ++++++++----------- .../smtp/netty/NettySMTPSServerTest.java | 7 +- .../smtp/netty/NettySMTPServerTest.java | 8 +- .../smtp/netty/NettyStartTlsSMTPServerTest.java | 50 +++--- .../apache/james/smtpserver/SMTPServerTest.java | 138 +++++++++------ .../james/smtpserver/SMTPTestConfiguration.java | 13 +- 10 files changed, 233 insertions(+), 217 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/AbstractLMTPServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/AbstractLMTPServerTest.java b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/AbstractLMTPServerTest.java index 664f67b..3b4d1dd 100644 --- a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/AbstractLMTPServerTest.java +++ b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/AbstractLMTPServerTest.java @@ -18,7 +18,9 @@ ****************************************************************/ package org.apache.james.protocols.lmtp; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.net.InetSocketAddress; @@ -37,12 +39,12 @@ 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.MockLogger; -import org.apache.james.protocols.api.utils.TestUtils; +import org.apache.james.protocols.api.utils.ProtocolServerUtils; import org.apache.james.protocols.lmtp.hook.DeliverToRecipientHook; +import org.apache.james.protocols.smtp.AbstractSMTPServerTest; import org.apache.james.protocols.smtp.MailAddress; import org.apache.james.protocols.smtp.MailEnvelope; import org.apache.james.protocols.smtp.SMTPProtocol; -import org.apache.james.protocols.smtp.AbstractSMTPServerTest; import org.apache.james.protocols.smtp.SMTPSession; import org.apache.james.protocols.smtp.hook.HookResult; import org.apache.james.protocols.smtp.hook.HookReturnCode; @@ -91,15 +93,14 @@ public abstract class AbstractLMTPServerTest extends AbstractSMTPServerTest{ @Override public void testMailWithoutBrackets() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -127,15 +128,14 @@ public abstract class AbstractLMTPServerTest extends AbstractSMTPServerTest{ @Override public void testRcptWithoutBrackets() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -164,15 +164,14 @@ public abstract class AbstractLMTPServerTest extends AbstractSMTPServerTest{ @Test public void testEhloNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.sendCommand("HELO localhost"); @@ -196,15 +195,14 @@ public abstract class AbstractLMTPServerTest extends AbstractSMTPServerTest{ public void testDeliveryHook() throws Exception { TestDeliverHook deliverHook = new TestDeliverHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(deliverHook), address); + server = createServer(createProtocol(deliverHook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPSServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPSServerTest.java b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPSServerTest.java index 6915fcc..8dc4650 100644 --- a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPSServerTest.java +++ b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPSServerTest.java @@ -11,13 +11,16 @@ import org.apache.james.protocols.netty.NettyServer; public class NettyLMTPSServerTest extends AbstractLMTPSServerTest{ + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int RANDOM_PORT = 0; + @Override - protected ProtocolServer createServer(Protocol protocol, InetSocketAddress address) { + protected ProtocolServer createServer(Protocol protocol) { NettyServer server = NettyServer.builder() .protocol(protocol) .secure(Encryption.createTls(BogusSslContextFactory.getServerContext())) .build(); - server.setListenAddresses(address); + server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, RANDOM_PORT)); return server; } http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPServerTest.java b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPServerTest.java index 6e1a571..b45c19e 100644 --- a/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPServerTest.java +++ b/protocols/lmtp/src/test/java/org/apache/james/protocols/lmtp/netty/NettyLMTPServerTest.java @@ -27,12 +27,15 @@ import org.apache.james.protocols.netty.NettyServer; public class NettyLMTPServerTest extends AbstractLMTPServerTest { + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int RANDOM_PORT = 0; + @Override - protected ProtocolServer createServer(Protocol protocol, InetSocketAddress address) { + protected ProtocolServer createServer(Protocol protocol) { NettyServer server = NettyServer.builder() .protocol(protocol) .build(); - server.setListenAddresses(address); + server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, RANDOM_PORT)); return server; } http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPSServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPSServerTest.java b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPSServerTest.java index 1dd1376..afbdd5c 100644 --- a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPSServerTest.java +++ b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPSServerTest.java @@ -18,8 +18,6 @@ ****************************************************************/ package org.apache.james.protocols.smtp; -import java.net.InetSocketAddress; - import org.apache.commons.net.smtp.SMTPClient; import org.apache.commons.net.smtp.SMTPSClient; import org.apache.james.protocols.api.Encryption; @@ -27,7 +25,6 @@ import org.apache.james.protocols.api.Protocol; import org.apache.james.protocols.api.ProtocolServer; import org.apache.james.protocols.api.utils.BogusSslContextFactory; import org.apache.james.protocols.api.utils.BogusTrustManagerFactory; -import org.apache.james.protocols.smtp.AbstractSMTPServerTest; public abstract class AbstractSMTPSServerTest extends AbstractSMTPServerTest{ @@ -42,9 +39,9 @@ public abstract class AbstractSMTPSServerTest extends AbstractSMTPServerTest{ @Override - protected ProtocolServer createServer(Protocol protocol, InetSocketAddress address) { - return createEncryptedServer(protocol, address,Encryption.createTls(BogusSslContextFactory.getServerContext())); + protected ProtocolServer createServer(Protocol protocol) { + return createEncryptedServer(protocol, Encryption.createTls(BogusSslContextFactory.getServerContext())); } - protected abstract ProtocolServer createEncryptedServer(Protocol protocol, InetSocketAddress address, Encryption enc); + protected abstract ProtocolServer createEncryptedServer(Protocol protocol, Encryption enc); } http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPServerTest.java b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPServerTest.java index 1e73b35..801cf3b 100644 --- a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPServerTest.java +++ b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/AbstractSMTPServerTest.java @@ -18,6 +18,10 @@ ****************************************************************/ package org.apache.james.protocols.smtp; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -27,8 +31,8 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.net.smtp.SMTPClient; import org.apache.commons.net.smtp.SMTPReply; import org.apache.james.protocols.api.Protocol; @@ -39,7 +43,7 @@ import org.apache.james.protocols.api.handler.DisconnectHandler; import org.apache.james.protocols.api.handler.ProtocolHandler; import org.apache.james.protocols.api.handler.WiringException; import org.apache.james.protocols.api.utils.MockLogger; -import org.apache.james.protocols.api.utils.TestUtils; +import org.apache.james.protocols.api.utils.ProtocolServerUtils; import org.apache.james.protocols.smtp.hook.HeloHook; import org.apache.james.protocols.smtp.hook.HookResult; import org.apache.james.protocols.smtp.hook.HookReturnCode; @@ -48,7 +52,6 @@ import org.apache.james.protocols.smtp.hook.MessageHook; import org.apache.james.protocols.smtp.hook.RcptHook; import org.apache.james.protocols.smtp.utils.TestMessageHook; import org.junit.Test; -import static org.junit.Assert.*; public abstract class AbstractSMTPServerTest { @@ -61,15 +64,14 @@ public abstract class AbstractSMTPServerTest { @Test public void testSimpleDelivery() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -109,16 +111,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testStartTlsNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.sendCommand("STARTTLS"); @@ -142,16 +143,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testUnknownCommand() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.sendCommand("UNKNOWN"); @@ -175,16 +175,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testNoop() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.noop(); @@ -209,16 +208,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testMailWithoutBrackets() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -252,16 +250,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testInvalidHelo() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo(""); @@ -286,16 +283,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testRcptWithoutBrackets() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -328,17 +324,17 @@ public abstract class AbstractSMTPServerTest { @Test public void testInvalidNoBracketsEnformance() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null; try { Protocol protocol = createProtocol(hook); ((SMTPConfigurationImpl) protocol.getConfiguration()).setUseAddressBracketsEnforcement(false); - server = createServer(protocol, address); + server = createServer(protocol); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); @@ -371,16 +367,15 @@ public abstract class AbstractSMTPServerTest { @Test public void testHeloEnforcement() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -406,17 +401,17 @@ public abstract class AbstractSMTPServerTest { @Test public void testHeloEnforcementDisabled() throws Exception { TestMessageHook hook = new TestMessageHook(); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null; try { Protocol protocol = createProtocol(hook); ((SMTPConfigurationImpl) protocol.getConfiguration()).setHeloEhloEnforcement(false); - server = createServer(protocol, address); + server = createServer(protocol); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -459,16 +454,14 @@ public abstract class AbstractSMTPServerTest { } }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -508,16 +501,14 @@ public abstract class AbstractSMTPServerTest { } }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -556,16 +547,14 @@ public abstract class AbstractSMTPServerTest { } }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -607,16 +596,14 @@ public abstract class AbstractSMTPServerTest { } }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -664,16 +651,14 @@ public abstract class AbstractSMTPServerTest { }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -729,16 +714,14 @@ public abstract class AbstractSMTPServerTest { }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - - ProtocolServer server = null; try { - server = createServer(createProtocol(hook), address); + server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -770,16 +753,14 @@ public abstract class AbstractSMTPServerTest { @Test public void testNullSender() throws Exception { - - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(new ProtocolHandler[0]), address); + server = createServer(createProtocol(new ProtocolHandler[0])); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -828,15 +809,14 @@ public abstract class AbstractSMTPServerTest { }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook, testHook), address); + server = createServer(createProtocol(hook, testHook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -892,15 +872,14 @@ public abstract class AbstractSMTPServerTest { }; - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(hook, testHook), address); + server = createServer(createProtocol(hook, testHook)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); @@ -953,17 +932,15 @@ public abstract class AbstractSMTPServerTest { } }; - - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(connectHandler), address); + server = createServer(createProtocol(connectHandler)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isNegativePermanent(client.getReplyCode())); client.disconnect(); @@ -997,16 +974,14 @@ public abstract class AbstractSMTPServerTest { } }; - - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(connectHandler), address); + server = createServer(createProtocol(connectHandler)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isNegativeTransient(client.getReplyCode())); client.disconnect(); @@ -1041,16 +1016,14 @@ public abstract class AbstractSMTPServerTest { } }; - - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = null; try { - server = createServer(createProtocol(handler), address); + server = createServer(createProtocol(handler)); server.bind(); SMTPClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertTrue("Reply="+ client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); @@ -1071,7 +1044,7 @@ public abstract class AbstractSMTPServerTest { return new SMTPClient(); } - protected abstract ProtocolServer createServer(Protocol protocol, InetSocketAddress address); + protected abstract ProtocolServer createServer(Protocol protocol); protected Protocol createProtocol(ProtocolHandler... handlers) throws WiringException { http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPSServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPSServerTest.java b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPSServerTest.java index cdb3d2e..7d5594e 100644 --- a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPSServerTest.java +++ b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPSServerTest.java @@ -33,13 +33,16 @@ import org.apache.james.protocols.smtp.AbstractSMTPSServerTest; */ public class NettySMTPSServerTest extends AbstractSMTPSServerTest{ + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int RANDOM_PORT = 0; + @Override - protected ProtocolServer createEncryptedServer(Protocol protocol, InetSocketAddress address, Encryption enc) { + protected ProtocolServer createEncryptedServer(Protocol protocol, Encryption enc) { NettyServer server = NettyServer.builder() .protocol(protocol) .secure(enc) .build(); - server.setListenAddresses(address); + server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, RANDOM_PORT)); return server; } http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPServerTest.java ---------------------------------------------------------------------- diff --git a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPServerTest.java b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPServerTest.java index d796506..e037107 100644 --- a/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPServerTest.java +++ b/protocols/smtp/src/test/java/org/apache/james/protocols/smtp/netty/NettySMTPServerTest.java @@ -32,13 +32,15 @@ import org.apache.james.protocols.smtp.AbstractSMTPServerTest; */ public class NettySMTPServerTest extends AbstractSMTPServerTest{ - + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int RANDOM_PORT = 0; + @Override - protected ProtocolServer createServer(Protocol protocol, InetSocketAddress address) { + protected ProtocolServer createServer(Protocol protocol) { NettyServer server = NettyServer.builder() .protocol(protocol) .build(); - server.setListenAddresses(address); + server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, RANDOM_PORT)); return server; } } http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/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 161985c..e0ae441 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 @@ -41,7 +41,7 @@ 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.api.utils.ProtocolServerUtils; import org.apache.james.protocols.netty.AbstractChannelPipelineFactory; import org.apache.james.protocols.netty.NettyServer; import org.apache.james.protocols.smtp.AllButStartTlsDelimiterChannelHandler; @@ -59,6 +59,9 @@ import com.sun.mail.smtp.SMTPTransport; public class NettyStartTlsSMTPServerTest { + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int RANDOM_PORT = 0; + private ProtocolServer server; @After @@ -68,13 +71,13 @@ public class NettyStartTlsSMTPServerTest { } } - private ProtocolServer createServer(Protocol protocol, InetSocketAddress address, Encryption enc) { + private ProtocolServer createServer(Protocol protocol, Encryption enc) { NettyServer server = NettyServer.builder() .protocol(protocol) .secure(enc) .frameHandler(new AllButStartTlsDelimiterChannelHandler(AbstractChannelPipelineFactory.MAX_LINE_LENGTH, false, Delimiters.lineDelimiter())) .build(); - server.setListenAddresses(address); + server.setListenAddresses(new InetSocketAddress(LOCALHOST_IP, RANDOM_PORT)); return server; } @@ -95,12 +98,12 @@ public class NettyStartTlsSMTPServerTest { @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())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); SMTPSClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.quit(); @@ -109,12 +112,12 @@ public class NettyStartTlsSMTPServerTest { @Test 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())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); SMTPSClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); client.sendCommand("EHLO localhost"); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); @@ -125,12 +128,12 @@ public class NettyStartTlsSMTPServerTest { @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())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); SMTPSClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); client.sendCommand("EHLO localhost"); assertThat(new StartTLSAssert(client)).isStartTLSAnnounced(); @@ -160,12 +163,12 @@ public class NettyStartTlsSMTPServerTest { @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())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); SMTPSClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); client.sendCommand("EHLO localhost"); boolean execTLS = client.execTLS(); @@ -177,12 +180,12 @@ public class NettyStartTlsSMTPServerTest { @Test public void startTlsShouldFailWhenFollowedByInjectedCommand() throws Exception { - InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); - ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), address, Encryption.createStartTls(BogusSslContextFactory.getServerContext())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> absent()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); SMTPSClient client = createClient(); - client.connect(address.getAddress().getHostAddress(), address.getPort()); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); client.sendCommand("EHLO localhost"); client.sendCommand("STARTTLS\r\nRSET\r\n"); @@ -192,14 +195,15 @@ public class NettyStartTlsSMTPServerTest { @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())); + ProtocolServer server = createServer(createProtocol(Optional.<ProtocolHandler> of(hook)) , Encryption.createStartTls(BogusSslContextFactory.getServerContext())); server.bind(); + InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); + 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.host", bindedAddress.getHostName()); + mailProps.put("mail.smtp.port", bindedAddress.getPort()); mailProps.put("mail.smtp.socketFactory.class", BogusSSLSocketFactory.class.getName()); mailProps.put("mail.smtp.socketFactory.fallback", "false"); mailProps.put("mail.smtp.starttls.enable", "true"); @@ -215,7 +219,7 @@ public class NettyStartTlsSMTPServerTest { SMTPTransport transport = (SMTPTransport) mailSession.getTransport("smtps"); - transport.connect(new Socket(address.getHostName(), address.getPort())); + transport.connect(new Socket(bindedAddress.getHostName(), bindedAddress.getPort())); transport.sendMessage(message, rcpts); assertThat(hook.getQueued()).hasSize(1); http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java index 0efae7d..b212af3 100644 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java +++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java @@ -30,6 +30,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Writer; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; @@ -53,7 +54,7 @@ import org.apache.james.filesystem.api.mock.MockFileSystem; import org.apache.james.mailrepository.api.MailRepositoryStore; import org.apache.james.mailrepository.mock.MockMailRepositoryStore; import org.apache.james.metrics.api.Metric; -import org.apache.james.protocols.lib.PortUtil; +import org.apache.james.protocols.api.utils.ProtocolServerUtils; import org.apache.james.protocols.lib.mock.MockProtocolHandlerLoader; import org.apache.james.protocols.netty.AbstractChannelPipelineFactory; import org.apache.james.queue.api.MailQueueFactory; @@ -166,8 +167,6 @@ public class SMTPServerTest { private static final Logger log = LoggerFactory.getLogger(SMTPServerTest.class.getName()); - protected final int smtpListenerPort; - protected SMTPTestConfiguration smtpConfiguration; protected final InMemoryUsersRepository usersRepository = new InMemoryUsersRepository(); protected AlterableDNSServer dnsServer; @@ -180,16 +179,12 @@ public class SMTPServerTest { private SMTPServer smtpServer; - public SMTPServerTest() { - smtpListenerPort = PortUtil.getNonPrivilegedPort(); - } - @Before public void setUp() throws Exception { setUpFakeLoader(); // slf4j can't set programmatically any log level. It's just a facade // log.setLevel(SimpleLog.LOG_LEVEL_ALL); - smtpConfiguration = new SMTPTestConfiguration(smtpListenerPort); + smtpConfiguration = new SMTPTestConfiguration(); setUpSMTPServer(); } @@ -332,7 +327,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < AbstractChannelPipelineFactory.MAX_LINE_LENGTH; i++) { @@ -359,14 +355,15 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); SMTPClient smtpProtocol2 = new SMTPClient(); - smtpProtocol2.connect("127.0.0.1", smtpListenerPort); + smtpProtocol2.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); SMTPClient smtpProtocol3 = new SMTPClient(); try { - smtpProtocol3.connect("127.0.0.1", smtpListenerPort); + smtpProtocol3.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); Thread.sleep(3000); fail("Shold disconnect connection 3"); } catch (Exception e) { @@ -377,7 +374,7 @@ public class SMTPServerTest { smtpProtocol2.quit(); smtpProtocol2.disconnect(); - smtpProtocol3.connect("127.0.0.1", smtpListenerPort); + smtpProtocol3.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); Thread.sleep(3000); } @@ -425,7 +422,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) @@ -472,7 +470,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) @@ -505,7 +504,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) @@ -525,7 +525,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) @@ -541,7 +542,8 @@ public class SMTPServerTest { protected SMTPClient newSMTPClient() throws IOException { SMTPClient smtp = new SMTPClient(); - smtp.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtp.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); if (log.isDebugEnabled()) { smtp.addProtocolCommandListener(new ProtocolCommandListener() { @@ -615,7 +617,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) @@ -644,9 +647,10 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); SMTPClient smtpProtocol2 = new SMTPClient(); - smtpProtocol2.connect("127.0.0.1", smtpListenerPort); + smtpProtocol2.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -691,7 +695,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -735,7 +740,8 @@ public class SMTPServerTest { private void doTestHeloEhloResolv(String heloCommand) throws IOException { SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol.isConnected()) .as("first connection taken") @@ -780,7 +786,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo("abgsfe3rsf.de"); // helo should not be checked. so this should give a 250 code @@ -805,7 +812,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -853,7 +861,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -886,7 +895,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); @@ -903,7 +913,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -932,7 +943,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -965,7 +977,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -1010,7 +1023,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); @@ -1040,7 +1054,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.sendCommand("ehlo", "abgsfe3rsf.de"); // ehlo should not be checked. so this should give a 250 code @@ -1076,7 +1091,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -1121,7 +1137,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -1151,7 +1168,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); - smtpProtocol1.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") @@ -1176,7 +1194,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); @@ -1204,7 +1223,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); String[] capabilityRes = smtpProtocol.getReplyStrings(); @@ -1284,7 +1304,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1312,7 +1333,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1338,7 +1360,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1362,7 +1385,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1380,7 +1404,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1400,7 +1425,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1439,7 +1465,8 @@ public class SMTPServerTest { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); @@ -1481,7 +1508,8 @@ public class SMTPServerTest { dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); String[] capabilityRes = smtpProtocol.getReplyStrings(); @@ -1534,7 +1562,8 @@ public class SMTPServerTest { dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); @@ -1562,7 +1591,8 @@ public class SMTPServerTest { smtpConfiguration.setAddressBracketsEnforcement(false); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); @@ -1578,7 +1608,7 @@ public class SMTPServerTest { smtpProtocol.quit(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); @@ -1599,7 +1629,8 @@ public class SMTPServerTest { public void testAddressBracketsEnforcementEnabled() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); - smtpProtocol.connect("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); @@ -1629,7 +1660,8 @@ public class SMTPServerTest { public void testPipelining() throws Exception { StringBuilder buf = new StringBuilder(); init(smtpConfiguration); - Socket client = new Socket("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + Socket client = new Socket(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); buf.append("HELO TEST"); buf.append("\r\n"); @@ -1686,7 +1718,8 @@ public class SMTPServerTest { StringBuilder buf = new StringBuilder(); smtpConfiguration.setAuthorizedAddresses(""); init(smtpConfiguration); - Socket client = new Socket("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + Socket client = new Socket(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); buf.append("HELO TEST"); buf.append("\r\n"); @@ -1744,7 +1777,8 @@ public class SMTPServerTest { StringBuilder buf = new StringBuilder(); smtpConfiguration.setAuthorizedAddresses(""); init(smtpConfiguration); - Socket client = new Socket("127.0.0.1", smtpListenerPort); + InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); + Socket client = new Socket(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); buf.append("HELO TEST"); buf.append("\r\n"); http://git-wip-us.apache.org/repos/asf/james-project/blob/5f578db9/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPTestConfiguration.java ---------------------------------------------------------------------- diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPTestConfiguration.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPTestConfiguration.java index 350048c..6662885 100644 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPTestConfiguration.java +++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPTestConfiguration.java @@ -20,12 +20,15 @@ package org.apache.james.smtpserver; import org.apache.commons.configuration.DefaultConfigurationBuilder; -import org.apache.james.smtpserver.fastfail.*; +import org.apache.james.smtpserver.fastfail.DNSRBLHandler; +import org.apache.james.smtpserver.fastfail.MaxRcptHandler; +import org.apache.james.smtpserver.fastfail.ResolvableEhloHeloHandler; +import org.apache.james.smtpserver.fastfail.ReverseEqualsEhloHeloHandler; +import org.apache.james.smtpserver.fastfail.ValidSenderDomainHandler; @SuppressWarnings("serial") public class SMTPTestConfiguration extends DefaultConfigurationBuilder { - private final int m_smtpListenerPort; private int m_maxMessageSizeKB = 0; private String m_authorizedAddresses = "127.0.0.0/8"; private String m_authorizingMode = "false"; @@ -44,10 +47,6 @@ public class SMTPTestConfiguration extends DefaultConfigurationBuilder { private boolean m_addressBracketsEnforcement = true; private boolean m_startTLS = false; - public SMTPTestConfiguration(int smtpListenerPort) { - m_smtpListenerPort = smtpListenerPort; - } - public void setCheckAuthNetworks(boolean checkAuth) { m_checkAuthNetworks = checkAuth; } @@ -135,7 +134,7 @@ public class SMTPTestConfiguration extends DefaultConfigurationBuilder { addProperty("[@enabled]", true); - addProperty("bind", "127.0.0.1:" + m_smtpListenerPort); + addProperty("bind", "127.0.0.1:0"); if (m_connectionLimit != null) addProperty("connectionLimit", "" + m_connectionLimit); if (m_connectionBacklog != null) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
