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


The following commit(s) were added to refs/heads/master by this push:
     new 1a6000f13 [test] Use ephemeral port in PortForwardingTest
1a6000f13 is described below

commit 1a6000f1384c287e01f2175e038719ca4b470c48
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    | 37 +++++++++++++---------
 1 file changed, 22 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..032b7bde0 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
@@ -53,9 +53,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.Session;
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.mina.core.service.IoAcceptor;
 import org.apache.mina.core.service.IoHandlerAdapter;
@@ -96,12 +93,9 @@ 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;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
 
 /**
  * Port forwarding tests
@@ -847,14 +841,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 +874,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());
+                    }
                 }
             }
         }

Reply via email to