From d3cc9d12a0fd9d531c454f3faa526fa39838aa84 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 8 Jul 2016 08:07:37 +0200 Subject: [PATCH] Implement flexible port selection in Socks5ClientTest --- .../jivesoftware/smack/util/NetworkUtil.java | 54 +++++++++++++++++++ .../bytestreams/socks5/Socks5ClientTest.java | 14 +++-- 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 smack-core/src/test/java/org/jivesoftware/smack/util/NetworkUtil.java diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/NetworkUtil.java b/smack-core/src/test/java/org/jivesoftware/smack/util/NetworkUtil.java new file mode 100644 index 000000000..d111963f9 --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/NetworkUtil.java @@ -0,0 +1,54 @@ +/** + * + * Copyright 2016 Florian Schmaus + * + * Licensed 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.jivesoftware.smack.util; + +import java.io.IOException; +import java.net.BindException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class NetworkUtil { + + private static final Logger LOGGER = Logger.getLogger(NetworkUtil.class.getName()); + + public static ServerSocket getSocketOnLoopback() { + final InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); + final int portMin = 1024; + final int portMax = (1 << 16) - 1; + final int backlog = 1; + + ServerSocket serverSocket = null; + for (int port = portMin; port <= portMax; port++) { + try { + serverSocket = new ServerSocket(port, backlog, loopbackAddress); + break; + } catch (BindException e) { + LOGGER.log(Level.FINEST, "Could not bind port " + port + ", trying next", e); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + if (serverSocket == null) { + throw new IllegalStateException(); + } + + return serverSocket; + } +} diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientTest.java index 44a38e359..aab52d2a7 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientTest.java @@ -28,6 +28,7 @@ import java.net.ServerSocket; import java.net.Socket; import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.util.NetworkUtil; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost; import org.junit.After; import org.junit.Before; @@ -43,8 +44,8 @@ import org.jxmpp.jid.JidTestUtil; public class Socks5ClientTest { // settings - private String serverAddress = "127.0.0.1"; - private int serverPort = 7890; + private int serverPort; + private String serverAddress; private DomainBareJid proxyJID = JidTestUtil.MUC_EXAMPLE_ORG; private String digest = "digest"; private ServerSocket serverSocket; @@ -57,7 +58,9 @@ public class Socks5ClientTest { @Before public void setup() throws Exception { // create SOCKS5 proxy server socket - serverSocket = new ServerSocket(serverPort); + serverSocket = NetworkUtil.getSocketOnLoopback(); + serverAddress = serverSocket.getInetAddress().getHostAddress(); + serverPort = serverSocket.getLocalPort(); } /** @@ -330,6 +333,9 @@ public class Socks5ClientTest { */ @After public void cleanup() throws Exception { - serverSocket.close(); + // Avoid NPE if serverSocket could not get created for whateve reason. + if (serverSocket != null) { + serverSocket.close(); + } } }