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:19 UTC

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

[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);
+            }
+        }
+    }
+}