You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/02/28 17:44:17 UTC

[1/5] mina-sshd git commit: [SSHD-792] Using common code to generate welcome banner for SshFsMounter

Repository: mina-sshd
Updated Branches:
  refs/heads/master b8bf38740 -> 169ff4e43


[SSHD-792] Using common code to generate welcome banner for SshFsMounter


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/83ea75fa
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/83ea75fa
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/83ea75fa

Branch: refs/heads/master
Commit: 83ea75fad641ec9c564ce45e5f6d16e4c8d153e9
Parents: c047525
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Sun Feb 25 12:21:23 2018 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Wed Feb 28 19:47:03 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/sshd/server/SshServer.java  |  5 ++---
 .../server/subsystem/sftp/SshFsMounter.java     | 23 ++++++++++----------
 2 files changed, 14 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/83ea75fa/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java
index e8dc4bf..5725152 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java
@@ -600,8 +600,6 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
             System.exit(-1);
         }
 
-        System.err.println("Starting SSHD on port " + port);
-
         SshServer sshd = SshServer.setUpDefaultServer();
         Map<String, Object> props = sshd.getProperties();
         props.putAll(options);
@@ -626,8 +624,9 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
             command -> new ProcessShellFactory(GenericUtils.split(command, ' ')).create()
         ).build());
         sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
-        sshd.start();
 
+        System.err.println("Starting SSHD on port " + port);
+        sshd.start();
         Thread.sleep(Long.MAX_VALUE);
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/83ea75fa/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java b/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java
index 9320cc7..6b57926 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java
@@ -26,13 +26,15 @@ import java.io.OutputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.TreeMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 
+import org.apache.sshd.common.PropertyResolver;
+import org.apache.sshd.common.PropertyResolverUtils;
 import org.apache.sshd.common.config.SshConfigFileReader;
 import org.apache.sshd.common.io.IoServiceFactory;
 import org.apache.sshd.common.io.mina.MinaServiceFactory;
@@ -244,13 +246,12 @@ public final class SshFsMounter {
     public static void main(String[] args) throws Exception {
         int port = SshConfigFileReader.DEFAULT_PORT;
         boolean error = false;
-        Map<String, String> options = new LinkedHashMap<>();
-
+        Map<String, Object> options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
         int numArgs = GenericUtils.length(args);
         for (int i = 0; i < numArgs; i++) {
             String argName = args[i];
             if ("-p".equals(argName)) {
-                if (i + 1 >= numArgs) {
+                if ((i + 1) >= numArgs) {
                     System.err.println("option requires an argument: " + argName);
                     break;
                 }
@@ -272,7 +273,7 @@ public final class SshFsMounter {
                     break;
                 }
             } else if ("-o".equals(argName)) {
-                if (i + 1 >= numArgs) {
+                if ((i + 1) >= numArgs) {
                     System.err.println("option requires and argument: " + argName);
                     error = true;
                     break;
@@ -300,28 +301,28 @@ public final class SshFsMounter {
             System.exit(-1);
         }
 
-        System.err.println("Starting SSHD on port " + port);
-
         SshServer sshd = Utils.setupTestServer(SshFsMounter.class);
         Map<String, Object> props = sshd.getProperties();
-//        FactoryManagerUtils.updateProperty(props, ServerFactoryManager.WELCOME_BANNER, "Welcome to SSH-FS Mounter\n");
         props.putAll(options);
-        sshd.setPort(port);
-
+        PropertyResolver resolver = PropertyResolverUtils.toPropertyResolver(options);
         File targetFolder = Objects.requireNonNull(Utils.detectTargetFolder(MounterCommandFactory.class), "Failed to detect target folder");
         if (SecurityUtils.isBouncyCastleRegistered()) {
             sshd.setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(new File(targetFolder, "key.pem").toPath()));
         } else {
             sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(targetFolder, "key.ser")));
         }
+        // Should come AFTER key pair provider setup so auto-welcome can be generated if needed
+        SshServer.setupServerBanner(sshd, resolver);
 
         sshd.setShellFactory(InteractiveProcessShellFactory.INSTANCE);
         sshd.setPasswordAuthenticator(AcceptAllPasswordAuthenticator.INSTANCE);
         sshd.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
         sshd.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(MounterCommandFactory.INSTANCE).build());
         sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
-        sshd.start();
+        sshd.setPort(port);
 
+        System.err.println("Starting SSHD on port " + port);
+        sshd.start();
         Thread.sleep(Long.MAX_VALUE);
     }
 }


[3/5] mina-sshd git commit: [SSHD-792] Using case insensitive host name matching in SshdSocketAddress and derived classes

Posted by lg...@apache.org.
[SSHD-792] Using case insensitive host name matching in SshdSocketAddress and derived classes


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/acc40c2b
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/acc40c2b
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/acc40c2b

Branch: refs/heads/master
Commit: acc40c2b31d7437af3409b8041d6aa7d701d0de1
Parents: 83ea75f
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Sun Feb 25 12:35:56 2018 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Wed Feb 28 19:47:04 2018 +0200

----------------------------------------------------------------------
 .../common/forward/LocalForwardingEntry.java    | 22 +++++--
 .../sshd/common/util/net/SshdSocketAddress.java |  4 +-
 .../forward/LocalForwardingEntryTest.java       | 66 ++++++++++++++++++++
 3 files changed, 84 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/acc40c2b/sshd-core/src/main/java/org/apache/sshd/common/forward/LocalForwardingEntry.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/LocalForwardingEntry.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/LocalForwardingEntry.java
index 8ebb4ec..ff7fd5b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/LocalForwardingEntry.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/LocalForwardingEntry.java
@@ -19,8 +19,8 @@
 
 package org.apache.sshd.common.forward;
 
+import java.net.InetSocketAddress;
 import java.util.Collection;
-import java.util.Objects;
 
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.ValidateUtils;
@@ -33,6 +33,16 @@ public class LocalForwardingEntry extends SshdSocketAddress {
     private static final long serialVersionUID = 423661570180889621L;
     private final String alias;
 
+    // NOTE !!! it is crucial to use the bound address host name first
+    public LocalForwardingEntry(SshdSocketAddress local, InetSocketAddress bound) {
+        this(local, new SshdSocketAddress(bound.getHostString(), bound.getPort()));
+    }
+
+    // NOTE !!! it is crucial to use the bound address host name first
+    public LocalForwardingEntry(SshdSocketAddress local, SshdSocketAddress bound) {
+        this(bound.getHostName(), local.getHostName(), bound.getPort());
+    }
+
     public LocalForwardingEntry(String hostName, String alias, int port) {
         super(hostName, port);
         this.alias = ValidateUtils.checkNotNullAndNotEmpty(alias, "No host alias");
@@ -46,7 +56,7 @@ public class LocalForwardingEntry extends SshdSocketAddress {
     protected boolean isEquivalent(SshdSocketAddress that) {
         if (super.isEquivalent(that) && (that instanceof LocalForwardingEntry)) {
             LocalForwardingEntry entry = (LocalForwardingEntry) that;
-            if (Objects.equals(this.getAlias(), entry.getAlias())) {
+            if (GenericUtils.safeCompare(this.getAlias(), entry.getAlias(), false) == 0) {
                 return true;
             }
         }
@@ -61,7 +71,7 @@ public class LocalForwardingEntry extends SshdSocketAddress {
 
     @Override
     public int hashCode() {
-        return super.hashCode() + Objects.hashCode(getAlias());
+        return super.hashCode() + GenericUtils.hashCode(getAlias(), Boolean.FALSE);
     }
 
     @Override
@@ -73,9 +83,9 @@ public class LocalForwardingEntry extends SshdSocketAddress {
      * @param host    The host - ignored if {@code null}/empty - i.e., no match reported
      * @param port    The port - ignored if non-positive - i.e., no match reported
      * @param entries The {@link Collection} of {@link LocalForwardingEntry} to check
-     *                - ignored if {@code null}/empty - i.e., no match reported
+     * - ignored if {@code null}/empty - i.e., no match reported
      * @return The <U>first</U> entry whose host or alias matches the host name - case
-     * <U>sensitive</U> <B>and</B> has a matching port - {@code null} if no match found
+     * <U>insensitive</U> <B>and</B> has a matching port - {@code null} if no match found
      */
     public static LocalForwardingEntry findMatchingEntry(String host, int port, Collection<? extends LocalForwardingEntry> entries) {
         if (GenericUtils.isEmpty(host) || (port <= 0) || (GenericUtils.isEmpty(entries))) {
@@ -83,7 +93,7 @@ public class LocalForwardingEntry extends SshdSocketAddress {
         }
 
         for (LocalForwardingEntry e : entries) {
-            if ((port == e.getPort()) && (host.equals(e.getHostName()) || host.equals(e.getAlias()))) {
+            if ((port == e.getPort()) && (host.equalsIgnoreCase(e.getHostName()) || host.equalsIgnoreCase(e.getAlias()))) {
                 return e;
             }
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/acc40c2b/sshd-core/src/main/java/org/apache/sshd/common/util/net/SshdSocketAddress.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/net/SshdSocketAddress.java b/sshd-core/src/main/java/org/apache/sshd/common/util/net/SshdSocketAddress.java
index 010b402..c14b7f1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/net/SshdSocketAddress.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/net/SshdSocketAddress.java
@@ -192,7 +192,7 @@ public class SshdSocketAddress extends SocketAddress {
             return true;
         } else {
             return (this.getPort() == that.getPort())
-                && Objects.equals(this.getHostName(), that.getHostName());
+                && (GenericUtils.safeCompare(this.getHostName(), that.getHostName(), false) == 0);
         }
     }
 
@@ -209,7 +209,7 @@ public class SshdSocketAddress extends SocketAddress {
 
     @Override
     public int hashCode() {
-        return Objects.hashCode(getHostName()) + getPort();
+        return GenericUtils.hashCode(getHostName(), Boolean.FALSE) + getPort();
     }
 
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/acc40c2b/sshd-core/src/test/java/org/apache/sshd/common/forward/LocalForwardingEntryTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/forward/LocalForwardingEntryTest.java b/sshd-core/src/test/java/org/apache/sshd/common/forward/LocalForwardingEntryTest.java
new file mode 100644
index 0000000..c14ac4b
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/forward/LocalForwardingEntryTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.sshd.common.forward;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * TODO Add javadoc
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class LocalForwardingEntryTest extends BaseTestSupport {
+    public LocalForwardingEntryTest() {
+        super();
+    }
+
+    @Test   // NOTE: this also checks indirectly SshSocketAddress host comparison case-insensitive
+    public void testCaseInsensitiveMatching() {
+        LocalForwardingEntry expected = new LocalForwardingEntry(getClass().getSimpleName(), getCurrentTestName(), 7365);
+        String hostname = expected.getHostName();
+        String alias = expected.getAlias();
+        int port = expected.getPort();
+        List<LocalForwardingEntry> entries = IntStream.rangeClosed(1, 4)
+            .mapToObj(seed -> new LocalForwardingEntry(hostname + "-" + seed, alias + "-" + seed, port + seed))
+            .collect(Collectors.toCollection(ArrayList::new));
+        entries.add(expected);
+
+        for (String host : new String[] {hostname, alias}) {
+            for (int index = 1; index <= 4; index++) {
+                Collections.shuffle(entries);
+
+                LocalForwardingEntry actual = LocalForwardingEntry.findMatchingEntry(host, port, entries);
+                assertSame("Mismatched result for host=" + host, expected, actual);
+
+                host = shuffleCase(host);
+            }
+        }
+    }
+}


[4/5] mina-sshd git commit: [SSHD-786] Avoid AcceptPendingException on failed session attempt to re-accept incoming connections

Posted by lg...@apache.org.
[SSHD-786] Avoid AcceptPendingException on failed session attempt to re-accept incoming connections

Note: Also removed the ClientDeadlockTest since it was dependent on a specific timing race condition


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/deb2445e
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/deb2445e
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/deb2445e

Branch: refs/heads/master
Commit: deb2445ef9ec5bbfb8a3ed4e0693288b1a86f7f1
Parents: acc40c2
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Wed Feb 28 08:29:48 2018 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Wed Feb 28 19:47:04 2018 +0200

----------------------------------------------------------------------
 .../sshd/common/io/nio2/Nio2Acceptor.java       | 52 ++++++++++++--------
 .../apache/sshd/client/ClientDeadlockTest.java  | 37 ++++++++------
 2 files changed, 54 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/deb2445e/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
index 0427c80..92196ea 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
@@ -164,6 +164,7 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
         return getClass().getSimpleName() + "[" + getBoundAddresses() + "]";
     }
 
+    @SuppressWarnings("synthetic-access")
     protected class AcceptCompletionHandler extends Nio2CompletionHandler<AsynchronousSocketChannel, SocketAddress> {
         protected final AsynchronousServerSocketChannel socket;
 
@@ -172,7 +173,6 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
         protected void onCompleted(AsynchronousSocketChannel result, SocketAddress address) {
             // Verify that the address has not been unbound
             if (!channels.containsKey(address)) {
@@ -184,6 +184,7 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
 
             Nio2Session session = null;
             Long sessionId = null;
+            boolean keepAccepting;
             try {
                 // Create a session
                 IoHandler handler = getIoHandler();
@@ -201,8 +202,10 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
                 } else {
                     session.startReading();
                 }
+
+                keepAccepting = true;
             } catch (Throwable exc) {
-                failed(exc, address);
+                keepAccepting = okToReaccept(exc, address);
 
                 // fail fast the accepted connection
                 if (session != null) {
@@ -219,15 +222,18 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
                 unmapSession(sessionId);
             }
 
-            try {
-                // Accept new connections
-                socket.accept(address, this);
-            } catch (Throwable exc) {
-                failed(exc, address);
+            if (keepAccepting) {
+                try {
+                    // Accept new connections
+                    socket.accept(address, this);
+                } catch (Throwable exc) {
+                    failed(exc, address);
+                }
+            } else {
+                log.error("=====> onCompleted({}) no longer accepting incoming connections <====", address);
             }
         }
 
-        @SuppressWarnings("synthetic-access")
         protected Nio2Session createSession(Nio2Acceptor acceptor, SocketAddress address, AsynchronousSocketChannel channel, IoHandler handler) throws Throwable {
             if (log.isTraceEnabled()) {
                 log.trace("createNio2Session({}) address={}", acceptor, address);
@@ -236,15 +242,28 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
         protected void onFailed(Throwable exc, SocketAddress address) {
+            if (okToReaccept(exc, address)) {
+                try {
+                    // Accept new connections
+                    socket.accept(address, this);
+                } catch (Throwable t) {
+                    // Do not call failed(t, address) to avoid infinite recursion
+                    log.error("Failed (" + t.getClass().getSimpleName()
+                        + " to re-accept new connections on " + address
+                        + ": " + t.getMessage(), t);
+                }
+            }
+        }
+
+        protected boolean okToReaccept(Throwable exc, SocketAddress address) {
             AsynchronousServerSocketChannel channel = channels.get(address);
             if (channel == null) {
                 if (log.isDebugEnabled()) {
                     log.debug("Caught {} for untracked channel of {}: {}",
                         exc.getClass().getSimpleName(), address, exc.getMessage());
                 }
-                return;
+                return false;
             }
 
             if (disposing.get()) {
@@ -252,22 +271,13 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
                     log.debug("Caught {} for tracked channel of {} while disposing: {}",
                         exc.getClass().getSimpleName(), address, exc.getMessage());
                 }
-                return;
+                return false;
             }
 
             log.warn("Caught " + exc.getClass().getSimpleName()
                    + " while accepting incoming connection from " + address
                    + ": " + exc.getMessage(), exc);
-
-            try {
-                // Accept new connections
-                socket.accept(address, this);
-            } catch (Throwable t) {
-                // Do not call failed(t, address) to avoid infinite recursion
-                log.error("Failed (" + t.getClass().getSimpleName()
-                    + " to re-accept new connections on " + address
-                    + ": " + t.getMessage(), t);
-            }
+            return true;
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/deb2445e/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java b/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
index 9c37ba4..23c60f7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/ClientDeadlockTest.java
@@ -19,27 +19,28 @@
 package org.apache.sshd.client;
 
 import java.io.IOException;
-import java.util.EnumSet;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.client.session.ClientSession;
+import org.apache.sshd.client.session.ClientSessionImpl;
+import org.apache.sshd.client.session.SessionFactory;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.server.SshServer;
-import org.apache.sshd.server.session.ServerSessionImpl;
-import org.apache.sshd.server.session.SessionFactory;
 import org.apache.sshd.util.test.BaseTestSupport;
 import org.junit.After;
 import org.junit.Before;
+import org.junit.FixMethodOrder;
 import org.junit.Test;
+import org.junit.runners.MethodSorters;
 
 /**
  * TODO Add javadoc
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class ClientDeadlockTest extends BaseTestSupport {
-
     private SshServer sshd;
     private SshClient client;
     private int port;
@@ -51,16 +52,16 @@ public class ClientDeadlockTest extends BaseTestSupport {
     @Before
     public void setUp() throws Exception {
         sshd = setupTestServer();
-        sshd.setSessionFactory(new SessionFactory(sshd) {
-            @Override
-            protected ServerSessionImpl doCreateSession(IoSession ioSession) throws Exception {
-                throw new IOException("Closing");
-            }
-        });
         sshd.start();
         port = sshd.getPort();
 
         client = setupTestClient();
+        client.setSessionFactory(new SessionFactory(client) {
+            @Override
+            protected ClientSessionImpl doCreateSession(IoSession ioSession) throws Exception {
+                throw new SimulatedException(getCurrentTestName());
+            }
+        });
     }
 
     @After
@@ -73,13 +74,21 @@ public class ClientDeadlockTest extends BaseTestSupport {
         }
     }
 
-    @Test
+    @Test(expected = SimulatedException.class)
     public void testSimpleClient() throws Exception {
         client.start();
 
         ConnectFuture future = client.connect(getCurrentTestName(), TEST_LOCALHOST, port);
-        ClientSession session = future.verify(5L, TimeUnit.SECONDS).getSession();
-        session.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.CLOSED), TimeUnit.SECONDS.toMillis(7L));
-        assertFalse(session.isOpen());
+        try (ClientSession session = future.verify(5L, TimeUnit.SECONDS).getSession()) {
+            fail("Unexpected session established: " + session);
+        }
+    }
+
+    static class SimulatedException extends IOException {
+        private static final long serialVersionUID = 2460966941758520525L;
+
+        SimulatedException(String message) {
+            super(message);
+        }
     }
 }


[2/5] mina-sshd git commit: [SSHD-792] Added capability to remove associated IoSession attribute

Posted by lg...@apache.org.
[SSHD-792] Added capability to remove associated IoSession attribute


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/c047525e
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/c047525e
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/c047525e

Branch: refs/heads/master
Commit: c047525ee4e149ac84bbe62fa95a413bcdb46b68
Parents: b8bf387
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Sun Feb 25 12:20:37 2018 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Wed Feb 28 19:47:03 2018 +0200

----------------------------------------------------------------------
 .../java/org/apache/sshd/common/io/IoSession.java  |  8 ++++++++
 .../apache/sshd/common/io/mina/MinaSession.java    |  5 +++++
 .../apache/sshd/common/io/nio2/Nio2Session.java    | 17 +++++++++++++++--
 .../session/helpers/AbstractSessionTest.java       |  5 +++++
 4 files changed, 33 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c047525e/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java b/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
index 3edb6fa..90866c6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
@@ -48,6 +48,14 @@ public interface IoSession extends PacketWriter, Closeable {
     Object setAttribute(Object key, Object value);
 
     /**
+     * Removes a user-defined attribute with the specified key.
+     *
+     * @param key The key of the attribute we want to remove
+     * @return The old value of the attribute - <tt>null</tt> if not found.
+     */
+    Object removeAttribute(Object key);
+
+    /**
      * @return the socket address of remote peer.
      */
     SocketAddress getRemoteAddress();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c047525e/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
index 81d5b18..259f044 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
@@ -71,6 +71,11 @@ public class MinaSession extends AbstractInnerCloseable implements IoSession {
     }
 
     @Override
+    public Object removeAttribute(Object key) {
+        return session.removeAttribute(key);
+    }
+
+    @Override
     public SocketAddress getRemoteAddress() {
         return session.getRemoteAddress();
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c047525e/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
index d86ee34..33e49b4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
@@ -83,12 +83,23 @@ public class Nio2Session extends AbstractCloseable implements IoSession {
 
     @Override
     public Object getAttribute(Object key) {
-        return attributes.get(key);
+        synchronized (attributes) {
+            return attributes.get(key);
+        }
     }
 
     @Override
     public Object setAttribute(Object key, Object value) {
-        return attributes.put(key, value);
+        synchronized (attributes) {
+            return attributes.put(key, value);
+        }
+    }
+
+    @Override
+    public Object removeAttribute(Object key) {
+        synchronized (attributes) {
+            return attributes.remove(key);
+        }
     }
 
     @Override
@@ -241,6 +252,8 @@ public class Nio2Session extends AbstractCloseable implements IoSession {
                 log.trace("doCloseImmediately(" + this + ") IoHandler#sessionClosed failure details", e);
             }
         }
+
+        attributes.clear();
     }
 
     @Override   // co-variant return

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c047525e/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
index 026dab2..afb9ff7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/session/helpers/AbstractSessionTest.java
@@ -351,6 +351,11 @@ public class AbstractSessionTest extends BaseTestSupport {
         }
 
         @Override
+        public Object removeAttribute(Object key) {
+            return null;
+        }
+
+        @Override
         public SocketAddress getRemoteAddress() {
             return null;
         }


[5/5] mina-sshd git commit: [SSHD-792] Using more fine-grained decision whether to close a tunnel session gracefully or not

Posted by lg...@apache.org.
[SSHD-792] Using more fine-grained decision whether to close a tunnel session gracefully or not


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/169ff4e4
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/169ff4e4
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/169ff4e4

Branch: refs/heads/master
Commit: 169ff4e43dde1b939ed193fc89e5b615457e0fc2
Parents: deb2445
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Wed Feb 28 15:18:36 2018 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Wed Feb 28 19:47:04 2018 +0200

----------------------------------------------------------------------
 .../common/forward/DefaultForwardingFilter.java | 49 +++++++++++---------
 1 file changed, 26 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/169ff4e4/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
index a6f46fa..21de6dc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
@@ -34,6 +34,7 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.sshd.client.channel.ClientChannelEvent;
 import org.apache.sshd.common.Closeable;
@@ -915,18 +916,17 @@ public class DefaultForwardingFilter
         return getClass().getSimpleName() + "[" + getSession() + "]";
     }
 
-    //
-    // Static IoHandler implementation
-    //
-
+    @SuppressWarnings("synthetic-access")
     class StaticIoHandler implements IoHandler {
+        private final AtomicLong messagesCounter = new AtomicLong(0L);
+        private final boolean traceEnabled = log.isTraceEnabled();
+
         StaticIoHandler() {
             super();
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
-        public void sessionCreated(final IoSession session) throws Exception {
+        public void sessionCreated(IoSession session) throws Exception {
             InetSocketAddress local = (InetSocketAddress) session.getLocalAddress();
             int localPort = local.getPort();
             SshdSocketAddress remote = localToRemote.get(localPort);
@@ -934,12 +934,8 @@ public class DefaultForwardingFilter
                 log.debug("sessionCreated({}) remote={}", session, remote);
             }
 
-            TcpipClientChannel channel;
-            if (remote != null) {
-                channel = new TcpipClientChannel(TcpipClientChannel.Type.Direct, session, remote);
-            } else {
-                channel = new TcpipClientChannel(TcpipClientChannel.Type.Forwarded, session, null);
-            }
+            TcpipClientChannel.Type channelType = (remote == null) ? TcpipClientChannel.Type.Forwarded : TcpipClientChannel.Type.Direct;
+            TcpipClientChannel channel = new TcpipClientChannel(channelType, session, remote);
             session.setAttribute(TcpipClientChannel.class, channel);
 
             service.registerChannel(channel);
@@ -958,28 +954,35 @@ public class DefaultForwardingFilter
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
         public void sessionClosed(IoSession session) throws Exception {
-            TcpipClientChannel channel = (TcpipClientChannel) session.getAttribute(TcpipClientChannel.class);
+            TcpipClientChannel channel = (TcpipClientChannel) session.removeAttribute(TcpipClientChannel.class);
             if (channel != null) {
+                Throwable cause = (Throwable) session.getAttribute(Throwable.class);
                 if (log.isDebugEnabled()) {
-                    log.debug("sessionClosed({}) closing channel={}", session, channel);
+                    log.debug("sessionClosed({}) closing channel={} after {} messages - cause={}",
+                            session, channel, messagesCounter, (cause == null) ? null : cause.getClass().getSimpleName());
                 }
-                channel.close(false);
+                // If exception signaled then close channel immediately
+                channel.close(cause != null);
             }
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
         public void messageReceived(IoSession session, Readable message) throws Exception {
             TcpipClientChannel channel = (TcpipClientChannel) session.getAttribute(TcpipClientChannel.class);
+            long totalMessages = messagesCounter.incrementAndGet();
             Buffer buffer = new ByteArrayBuffer(message.available() + Long.SIZE, false);
             buffer.putBuffer(message);
 
+            if (traceEnabled) {
+                log.trace("messageReceived({}) channel={}, count={}, handle len={}",
+                          session, channel, totalMessages, message.available());
+            }
+
             Collection<ClientChannelEvent> result = channel.waitFor(STATIC_IO_MSG_RECEIVED_EVENTS, Long.MAX_VALUE);
-            if (log.isTraceEnabled()) {
-                log.trace("messageReceived({}) channel={}, len={} wait result: {}",
-                          session, channel, result, buffer.array());
+            if (traceEnabled) {
+                log.trace("messageReceived({}) channel={}, count={}, len={} wait result: {}",
+                          session, channel, totalMessages, message.available(), result);
             }
 
             OutputStream outputStream = channel.getInvertedIn();
@@ -988,15 +991,15 @@ public class DefaultForwardingFilter
         }
 
         @Override
-        @SuppressWarnings("synthetic-access")
         public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
+            session.setAttribute(Throwable.class, cause);
             if (log.isDebugEnabled()) {
                 log.debug("exceptionCaught({}) {}: {}", session, cause.getClass().getSimpleName(), cause.getMessage());
             }
-            if (log.isTraceEnabled()) {
+            if (traceEnabled) {
                 log.trace("exceptionCaught(" + session + ") caught exception details", cause);
             }
-            session.close(false);
+            session.close(true);
         }
     }
 }