This is an automated email from the ASF dual-hosted git repository.

gnodet 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 848edb1  Improve ProxyTest example
848edb1 is described below

commit 848edb18f6e2a8dd3cfc50c9ca993a15fcbcebf2
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Mon Jul 27 20:15:51 2020 +0200

    Improve ProxyTest example
---
 .../java/org/apache/sshd/client/ProxyTest.java     | 39 +++++++++++-----------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ProxyTest.java 
b/sshd-core/src/test/java/org/apache/sshd/client/ProxyTest.java
index 82902e9..1500729 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/ProxyTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/ProxyTest.java
@@ -40,6 +40,8 @@ import org.junit.runners.MethodSorters;
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class ProxyTest extends BaseTestSupport {
 
+    private ClientSession proxySession;
+
     public ProxyTest() {
         super();
     }
@@ -50,8 +52,21 @@ public class ProxyTest extends BaseTestSupport {
              SshServer proxy = setupTestServer();
              SshClient client = setupTestClient()) {
 
+            // setup server with an echo command
+            server.setCommandFactory((session, command) -> new 
CommandExecutionHelper(command) {
+                @Override
+                protected boolean handleCommandLine(String command) throws 
Exception {
+                    OutputStream stdout = getOutputStream();
+                    stdout.write(command.getBytes(StandardCharsets.US_ASCII));
+                    stdout.flush();
+                    return false;
+                }
+            });
             server.start();
+            // setup proxy with a forwarding filter to allow the local port 
forwarding
+            proxy.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
             proxy.start();
+            // setup client
             client.start();
 
             String command = "ls -la";
@@ -66,6 +81,8 @@ public class ProxyTest extends BaseTestSupport {
                 result = out.toString();
             }
             assertEquals(command, result);
+            // make sure the proxy session is closed / closing
+            assertTrue(proxySession == null || proxySession.isClosing() || 
proxySession.isClosed());
         }
     }
 
@@ -77,7 +94,7 @@ public class ProxyTest extends BaseTestSupport {
             throws java.io.IOException {
         ClientSession session;
         if (proxyHost != null) {
-            ClientSession proxySession = client.connect(proxyUser, proxyHost, 
proxyPort)
+            proxySession = client.connect(proxyUser, proxyHost, proxyPort)
                     .verify(CONNECT_TIMEOUT).getSession();
             proxySession.addPasswordIdentity(proxyPassword);
             proxySession.auth().verify(AUTH_TIMEOUT);
@@ -87,7 +104,7 @@ public class ProxyTest extends BaseTestSupport {
             SshdSocketAddress bound = tracker.getBoundAddress();
             session = client.connect(user, bound.getHostName(), 
bound.getPort())
                     .verify(CONNECT_TIMEOUT).getSession();
-            session.addCloseFutureListener(f -> IoUtils.closeQuietly(tracker));
+            session.addCloseFutureListener(f -> IoUtils.closeQuietly(tracker, 
proxySession));
         } else {
             session = client.connect(user, host, 
port).verify(CONNECT_TIMEOUT).getSession();
         }
@@ -96,22 +113,4 @@ public class ProxyTest extends BaseTestSupport {
         return session;
     }
 
-    @Override
-    protected SshServer setupTestServer() {
-        SshServer sshd = super.setupTestServer();
-        // setup forwarding filter to allow the local port forwarding
-        sshd.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
-        // setup an echo command
-        sshd.setCommandFactory((session, command) -> new 
CommandExecutionHelper(command) {
-            @Override
-            protected boolean handleCommandLine(String command) throws 
Exception {
-                OutputStream stdout = getOutputStream();
-                stdout.write(command.getBytes(StandardCharsets.US_ASCII));
-                stdout.flush();
-                return false;
-            }
-        });
-        return sshd;
-    }
-
 }

Reply via email to