mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-26 05:52:06 +01:00
Implement flexible port selection in Socks5ClientTest
This commit is contained in:
parent
17c9661283
commit
d3cc9d12a0
2 changed files with 64 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
// Avoid NPE if serverSocket could not get created for whateve reason.
|
||||
if (serverSocket != null) {
|
||||
serverSocket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue