You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2013/01/17 14:04:14 UTC

svn commit: r1434654 [2/2] - in /mina/sshd/trunk/sshd-core/src: main/java/org/apache/sshd/ main/java/org/apache/sshd/client/ main/java/org/apache/sshd/client/channel/ main/java/org/apache/sshd/client/session/ main/java/org/apache/sshd/common/ main/java...

Modified: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java?rev=1434654&r1=1434653&r2=1434654&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java (original)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java Thu Jan 17 13:04:13 2013
@@ -20,10 +20,7 @@ package org.apache.sshd;
 
 import java.io.IOException;
 import java.lang.reflect.Field;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.URL;
+import java.net.*;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -46,14 +43,14 @@ import org.apache.mina.core.service.IoAc
 import org.apache.mina.core.service.IoHandlerAdapter;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
-import org.apache.sshd.client.SshdSocketAddress;
+import org.apache.sshd.common.SshdSocketAddress;
 import org.apache.sshd.client.channel.ChannelDirectTcpip;
 import org.apache.sshd.client.future.AuthFuture;
 import org.apache.sshd.client.future.ConnectFuture;
-import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.apache.sshd.util.BogusForwardingFilter;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -61,6 +58,7 @@ import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.sshd.util.Utils.getFreePort;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -76,15 +74,6 @@ public class PortForwardingTest {
     private int echoPort;
     private IoAcceptor acceptor;
 
-    private static int getFreePort() throws Exception {
-        ServerSocket s = new ServerSocket(0);
-        try {
-            return s.getLocalPort();
-        } finally {
-            s.close();
-        }
-    }
-
     @Before
     public void setUp() throws Exception {
         sshPort = getFreePort();
@@ -92,10 +81,10 @@ public class PortForwardingTest {
 
         sshd = SshServer.setUpDefaultServer();
         sshd.setPort(sshPort);
-        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
         sshd.setShellFactory(new EchoShellFactory());
         sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
-        sshd.setForwardingFilter(new BogusForwardingFilter());
+        sshd.setTcpipForwardingFilter(new BogusForwardingFilter());
         sshd.start();
 
         NioSocketAcceptor acceptor = new NioSocketAcceptor();
@@ -169,6 +158,27 @@ public class PortForwardingTest {
     }
 
     @Test
+    public void testRemoteForwardingNativeNoExplicitPort() throws Exception {
+        ClientSession session = createNativeSession();
+
+        SshdSocketAddress remote = new SshdSocketAddress("0.0.0.0", 0);
+        SshdSocketAddress local = new SshdSocketAddress("localhost", echoPort);
+
+        SshdSocketAddress bound = session.startRemotePortForwarding(remote, local);
+
+        Socket s = new Socket(bound.getHostName(), bound.getPort());
+        s.getOutputStream().write("Hello".getBytes());
+        s.getOutputStream().flush();
+        byte[] buf = new byte[1024];
+        int n = s.getInputStream().read(buf);
+        String res = new String(buf, 0, n);
+        assertEquals("Hello", res);
+        s.close();
+
+        session.stopRemotePortForwarding(bound);
+    }
+
+    @Test
     public void testLocalForwarding() throws Exception {
         Session session = createSession();
 
@@ -191,13 +201,12 @@ public class PortForwardingTest {
     public void testLocalForwardingNative() throws Exception {
         ClientSession session = createNativeSession();
 
-        int forwardedPort = getFreePort();
-        SshdSocketAddress local = new SshdSocketAddress("", forwardedPort);
+        SshdSocketAddress local = new SshdSocketAddress("", getFreePort());
         SshdSocketAddress remote = new SshdSocketAddress("localhost", echoPort);
 
-        session.startLocalPortForwarding(local, remote);
+        SshdSocketAddress bound = session.startLocalPortForwarding(local, remote);
 
-        Socket s = new Socket(local.getHostName(), local.getPort());
+        Socket s = new Socket(bound.getHostName(), bound.getPort());
         s.getOutputStream().write("Hello".getBytes());
         s.getOutputStream().flush();
         byte[] buf = new byte[1024];
@@ -206,7 +215,7 @@ public class PortForwardingTest {
         assertEquals("Hello", res);
         s.close();
 
-        session.stopLocalPortForwarding(local);
+        session.stopLocalPortForwarding(bound);
     }
 
     @Test
@@ -461,6 +470,7 @@ public class PortForwardingTest {
 
     protected ClientSession createNativeSession() throws Exception {
         SshClient client = SshClient.setUpDefaultClient();
+        client.setTcpipForwardingFilter(new BogusForwardingFilter());
         client.start();
         ConnectFuture sessionFuture = client.connect("localhost", sshPort);
         sessionFuture.await();

Modified: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ScpTest.java?rev=1434654&r1=1434653&r2=1434654&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ScpTest.java (original)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ScpTest.java Thu Jan 17 13:04:13 2013
@@ -33,10 +33,10 @@ import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Logger;
 import com.jcraft.jsch.UserInfo;
-import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -65,7 +65,7 @@ public class ScpTest {
 
         sshd = SshServer.setUpDefaultServer();
         sshd.setPort(port);
-        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
         sshd.setCommandFactory(new ScpCommandFactory());
         sshd.setShellFactory(new EchoShellFactory());
         sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());

Modified: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ServerTest.java?rev=1434654&r1=1434653&r2=1434654&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ServerTest.java (original)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/ServerTest.java Thu Jan 17 13:04:13 2013
@@ -25,11 +25,11 @@ import org.apache.mina.core.session.IoSe
 import org.apache.sshd.client.SessionFactory;
 import org.apache.sshd.client.session.ClientSessionImpl;
 import org.apache.sshd.common.SshConstants;
-import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -54,7 +54,7 @@ public class ServerTest {
 
         sshd = SshServer.setUpDefaultServer();
         sshd.setPort(port);
-        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
         sshd.setShellFactory(new EchoShellFactory());
         sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
         sshd.start();
@@ -130,7 +130,7 @@ public class ServerTest {
     public static void main(String[] args) throws Exception {
         SshServer sshd = SshServer.setUpDefaultServer();
         sshd.setPort(8001);
-        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
         sshd.setShellFactory(new EchoShellFactory());
         sshd.setCommandFactory(new ScpCommandFactory());
         sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());

Modified: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/SftpTest.java?rev=1434654&r1=1434653&r2=1434654&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/SftpTest.java (original)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/SftpTest.java Thu Jan 17 13:04:13 2013
@@ -32,12 +32,12 @@ import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.Logger;
 import com.jcraft.jsch.UserInfo;
 import org.apache.sshd.common.NamedFactory;
-import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.server.sftp.SftpSubsystem;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -61,7 +61,7 @@ public class SftpTest {
 
         sshd = SshServer.setUpDefaultServer();
         sshd.setPort(port);
-        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[]{"src/test/resources/hostkey.pem"}));
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
         sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
         sshd.setCommandFactory(new ScpCommandFactory());
         sshd.setShellFactory(new EchoShellFactory());

Modified: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java?rev=1434654&r1=1434653&r2=1434654&view=diff
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java (original)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java Thu Jan 17 13:04:13 2013
@@ -18,10 +18,9 @@
  */
 package org.apache.sshd.util;
 
-import org.apache.sshd.server.ForwardingFilter;
-import org.apache.sshd.server.session.ServerSession;
-
-import java.net.InetSocketAddress;
+import org.apache.sshd.common.SshdSocketAddress;
+import org.apache.sshd.common.Session;
+import org.apache.sshd.common.ForwardingFilter;
 
 /**
  * TODO Add javadoc
@@ -29,19 +28,19 @@ import java.net.InetSocketAddress;
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public class BogusForwardingFilter implements ForwardingFilter {
-    public boolean canForwardAgent(ServerSession session) {
+    public boolean canForwardAgent(Session session) {
         return true;
     }
 
-    public boolean canForwardX11(ServerSession session) {
+    public boolean canForwardX11(Session session) {
         return true;
     }
 
-    public boolean canConnect(InetSocketAddress address, ServerSession session) {
+    public boolean canConnect(SshdSocketAddress address, Session session) {
         return true;
     }
 
-    public boolean canListen(InetSocketAddress address, ServerSession session) {
+    public boolean canListen(SshdSocketAddress address, Session session) {
         return true;
     }
 }

Added: mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/Utils.java
URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/Utils.java?rev=1434654&view=auto
==============================================================================
--- mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/Utils.java (added)
+++ mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/util/Utils.java Thu Jan 17 13:04:13 2013
@@ -0,0 +1,58 @@
+/*
+ * 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.util;
+
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+
+import java.io.File;
+import java.net.ServerSocket;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+public class Utils {
+
+    public static FileKeyPairProvider createTestHostKeyProvider() {
+        return createTestKeyPairProvider("hostkey.pem");
+    }
+
+    public static FileKeyPairProvider createTestKeyPairProvider(String resource) {
+        return new FileKeyPairProvider(new String[] { getFile(resource) });
+    }
+
+    public static int getFreePort() throws Exception {
+        ServerSocket s = new ServerSocket(0);
+        try {
+            return s.getLocalPort();
+        } finally {
+            s.close();
+        }
+    }
+
+    private static String getFile(String resource) {
+        URL url = Utils.class.getClassLoader().getResource(resource);
+        File f;
+        try {
+            f = new File(url.toURI());
+        } catch(URISyntaxException e) {
+            f = new File(url.getPath());
+        }
+        return f.toString();
+    }
+
+}