This is an automated email from the ASF dual-hosted git repository. twolf pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit b58aff8f7c1ef36009e95aa9f6d18d9c96d0fb7b Author: Thomas Wolf <tw...@apache.org> AuthorDate: Tue Apr 22 20:48:42 2025 +0200 [test] Use ephemeral port in PortForwardingTest Don't use hard-coded 8080: the test would fail if something was already using that port. The test wants to create multiple local forwardings on the same port, but different network interfaces. So let the first one choose any free port, then use that same port number for the other interfaces, too. --- .../sshd/common/forward/PortForwardingTest.java | 36 +++++++++++++--------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/sshd-core/src/test/java/org/apache/sshd/common/forward/PortForwardingTest.java b/sshd-core/src/test/java/org/apache/sshd/common/forward/PortForwardingTest.java index f05bd4dad..44c84de8f 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/forward/PortForwardingTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/forward/PortForwardingTest.java @@ -96,19 +96,12 @@ import org.junit.jupiter.api.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - /** * Port forwarding tests */ @TestMethodOrder(MethodName.class) @SuppressWarnings("checkstyle:MethodCount") -public class PortForwardingTest extends BaseTestSupport { +class PortForwardingTest extends BaseTestSupport { public static final int SO_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(13L); @@ -190,7 +183,7 @@ public class PortForwardingTest extends BaseTestSupport { private final Logger log = LoggerFactory.getLogger(getClass()); - public PortForwardingTest() { + PortForwardingTest() { super(); } @@ -847,14 +840,23 @@ public class PortForwardingTest extends BaseTestSupport { List<String> allAddresses = getHostAddresses(); log.info("{} - test on addresses={}", getCurrentTestName(), allAddresses); + Assumptions.assumeTrue(allAddresses.size() > 1, "Test makes only sense with at least 2 IP addresses"); + // Create multiple local forwardings on the same port, but different network interfaces try (ClientSession session = createNativeSession(null)) { List<ExplicitPortForwardingTracker> trackers = new ArrayList<>(); try { + int port = 0; for (String host : allAddresses) { ExplicitPortForwardingTracker tracker = session.createLocalPortForwardingTracker( - new SshdSocketAddress(host, 8080), + new SshdSocketAddress(host, port), new SshdSocketAddress("test.javastack.org", 80)); SshdSocketAddress boundAddress = tracker.getBoundAddress(); + if (port == 0) { + port = boundAddress.getPort(); + assertNotEquals(0, port); + } else { + assertEquals(port, boundAddress.getPort()); + } log.info("{} - test for binding={}", getCurrentTestName(), boundAddress); testRemoteURL(new Proxy(Proxy.Type.HTTP, boundAddress.toInetSocketAddress()), "http://test.javastack.org/"); @@ -871,11 +873,15 @@ public class PortForwardingTest extends BaseTestSupport { Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); while (eni.hasMoreElements()) { NetworkInterface networkInterface = eni.nextElement(); - Enumeration<InetAddress> eia = networkInterface.getInetAddresses(); - while (eia.hasMoreElements()) { - InetAddress ia = eia.nextElement(); - if (ia instanceof Inet4Address) { - addresses.add(ia.getHostAddress()); + if (networkInterface.isUp()) { + // TODO: if a VPN tunnel exists, we may get a tunnel address, but that will work + // only inside that VPN. How could we recognize and exclude such tunnel interfaces? + Enumeration<InetAddress> eia = networkInterface.getInetAddresses(); + while (eia.hasMoreElements()) { + InetAddress ia = eia.nextElement(); + if (ia instanceof Inet4Address) { + addresses.add(ia.getHostAddress()); + } } } }