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 2015/12/03 07:38:54 UTC
[4/7] mina-sshd git commit: Using a VirtualFileSystemFactory for the
test instead of an anonymous FileSystemFactory
Using a VirtualFileSystemFactory for the test instead of an anonymous FileSystemFactory
* Related to SSHD-605
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/3d57e889
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/3d57e889
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/3d57e889
Branch: refs/heads/master
Commit: 3d57e889df8d54828f4ee9f3767edb38f4124d5e
Parents: 14eb57c
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Thu Dec 3 08:37:01 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Thu Dec 3 08:37:01 2015 +0200
----------------------------------------------------------------------
.../org/apache/sshd/client/scp/ScpTest.java | 93 ++++++++------------
.../sshd/client/simple/SimpleScpClientTest.java | 12 +--
.../client/simple/SimpleSftpClientTest.java | 12 +--
.../sftp/AbstractSftpClientTestSupport.java | 13 +--
.../subsystem/sftp/SftpFileSystemTest.java | 11 +--
.../sshd/client/subsystem/sftp/SftpTest.java | 47 +++++++++-
6 files changed, 93 insertions(+), 95 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpTest.java
index a5e7197..5c85dec 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpTest.java
@@ -25,34 +25,25 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import ch.ethz.ssh2.Connection;
-import ch.ethz.ssh2.ConnectionInfo;
-import ch.ethz.ssh2.SCPClient;
-import com.jcraft.jsch.ChannelExec;
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.Factory;
import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.file.root.RootedFileSystemProvider;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.random.Random;
import org.apache.sshd.common.scp.ScpHelper;
import org.apache.sshd.common.scp.ScpTransferEventListener;
-import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.io.IoUtils;
@@ -64,13 +55,21 @@ import org.apache.sshd.util.test.SimpleUserInfo;
import org.apache.sshd.util.test.Utils;
import org.junit.After;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.jcraft.jsch.ChannelExec;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+
+import ch.ethz.ssh2.Connection;
+import ch.ethz.ssh2.ConnectionInfo;
+import ch.ethz.ssh2.SCPClient;
+
/**
* Test for SCP support.
*
@@ -81,19 +80,17 @@ public class ScpTest extends BaseTestSupport {
private SshServer sshd;
private int port;
- private com.jcraft.jsch.Session session;
private final FileSystemFactory fileSystemFactory;
public ScpTest() throws IOException {
Path targetPath = detectTargetFolder();
Path parentPath = targetPath.getParent();
- final FileSystem fileSystem = new RootedFileSystemProvider().newFileSystem(parentPath, Collections.<String, Object>emptyMap());
- fileSystemFactory = new FileSystemFactory() {
- @Override
- public FileSystem createFileSystem(Session session) throws IOException {
- return fileSystem;
- }
- };
+ fileSystemFactory = new VirtualFileSystemFactory(parentPath);
+ }
+
+ @BeforeClass
+ public static void jschInit() {
+ JSchLogger.init();
}
@Before
@@ -105,34 +102,14 @@ public class ScpTest extends BaseTestSupport {
port = sshd.getPort();
}
- protected com.jcraft.jsch.Session getJschSession() throws JSchException {
- JSchLogger.init();
- JSch sch = new JSch();
- session = sch.getSession("sshd", TEST_LOCALHOST, port);
- session.setUserInfo(new SimpleUserInfo("sshd"));
- session.connect();
- return session;
- }
-
@After
public void tearDown() throws Exception {
- if (session != null) {
- session.disconnect();
- }
-
if (sshd != null) {
sshd.stop(true);
}
}
@Test
- @Ignore
- public void testExternal() throws Exception {
- System.out.println("Scp available on port " + port);
- Thread.sleep(5 * 60000);
- }
-
- @Test
public void testNormalizedScpRemotePaths() throws Exception {
Path targetPath = detectTargetFolder();
Path parentPath = targetPath.getParent();
@@ -742,7 +719,7 @@ public class ScpTest extends BaseTestSupport {
@Test
public void testJschScp() throws Exception {
- session = getJschSession();
+ com.jcraft.jsch.Session session = getJschSession();
try {
String data = getCurrentTestName() + "\n";
@@ -757,31 +734,39 @@ public class ScpTest extends BaseTestSupport {
target.delete();
assertFalse(target.exists());
- sendFile(unixPath, fileName, data);
+ sendFile(session, unixPath, fileName, data);
assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(5L));
target.delete();
assertFalse(target.exists());
- sendFile(unixDir, fileName, data);
+ sendFile(session, unixDir, fileName, data);
assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(5L));
- sendFileError("target", ScpHelper.SCP_COMMAND_PREFIX, data);
+ sendFileError(session, "target", ScpHelper.SCP_COMMAND_PREFIX, data);
- readFileError(unixDir);
+ readFileError(session, unixDir);
- assertEquals("Mismatched file data", data, readFile(unixPath, target.length()));
- assertEquals("Mismatched dir data", data, readDir(unixDir, fileName, target.length()));
+ assertEquals("Mismatched file data", data, readFile(session, unixPath, target.length()));
+ assertEquals("Mismatched dir data", data, readDir(session, unixDir, fileName, target.length()));
target.delete();
root.delete();
- sendDir("target", ScpHelper.SCP_COMMAND_PREFIX, fileName, data);
+ sendDir(session, "target", ScpHelper.SCP_COMMAND_PREFIX, fileName, data);
assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(5L));
} finally {
session.disconnect();
}
}
+ protected com.jcraft.jsch.Session getJschSession() throws JSchException {
+ JSch sch = new JSch();
+ com.jcraft.jsch.Session session = sch.getSession(getCurrentTestName(), TEST_LOCALHOST, port);
+ session.setUserInfo(new SimpleUserInfo(getCurrentTestName()));
+ session.connect();
+ return session;
+ }
+
@Test
public void testWithGanymede() throws Exception {
Path targetPath = detectTargetFolder();
@@ -800,6 +785,7 @@ public class ScpTest extends BaseTestSupport {
PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE
));
+ ch.ethz.ssh2.log.Logger.enabled = true;
final Connection conn = new Connection(TEST_LOCALHOST, port);
try {
ConnectionInfo info = conn.connect(null, (int) TimeUnit.SECONDS.toMillis(5L), (int) TimeUnit.SECONDS.toMillis(11L));
@@ -832,7 +818,7 @@ public class ScpTest extends BaseTestSupport {
}
}
- protected String readFile(String path, long expectedSize) throws Exception {
+ protected String readFile(com.jcraft.jsch.Session session, String path, long expectedSize) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
c.setCommand("scp -f " + path);
c.connect();
@@ -865,7 +851,7 @@ public class ScpTest extends BaseTestSupport {
}
}
- protected String readDir(String path, String fileName, long expectedSize) throws Exception {
+ protected String readDir(com.jcraft.jsch.Session session, String path, String fileName, long expectedSize) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
c.setCommand("scp -r -f " + path);
c.connect();
@@ -905,7 +891,7 @@ public class ScpTest extends BaseTestSupport {
}
}
- protected String readFileError(String path) throws Exception {
+ protected void readFileError(com.jcraft.jsch.Session session, String path) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
String command = "scp -f " + path;
c.setCommand(command);
@@ -917,13 +903,12 @@ public class ScpTest extends BaseTestSupport {
os.write(0);
os.flush();
assertEquals("Mismatched response for command: " + command, 2, is.read());
- return null;
} finally {
c.disconnect();
}
}
- protected void sendFile(String path, String name, String data) throws Exception {
+ protected void sendFile(com.jcraft.jsch.Session session, String path, String name, String data) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
String command = "scp -t " + path;
c.setCommand(command);
@@ -958,7 +943,7 @@ public class ScpTest extends BaseTestSupport {
assertEquals("No ACK for command=" + command, 0, is.read());
}
- protected void sendFileError(String path, String name, String data) throws Exception {
+ protected void sendFileError(com.jcraft.jsch.Session session, String path, String name, String data) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
String command = "scp -t " + path;
c.setCommand(command);
@@ -978,7 +963,7 @@ public class ScpTest extends BaseTestSupport {
}
}
- protected void sendDir(String path, String dirName, String fileName, String data) throws Exception {
+ protected void sendDir(com.jcraft.jsch.Session session, String path, String dirName, String fileName, String data) throws Exception {
ChannelExec c = (ChannelExec) session.openChannel("exec");
String command = "scp -t -r " + path;
c.setCommand(command);
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleScpClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleScpClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleScpClientTest.java
index 5fbd1cc..e05c093 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleScpClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleScpClientTest.java
@@ -20,14 +20,12 @@
package org.apache.sshd.client.simple;
import java.io.IOException;
-import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.Collections;
import org.apache.sshd.client.scp.CloseableScpClient;
import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.file.root.RootedFileSystemProvider;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.scp.ScpHelper;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.io.IoUtils;
@@ -49,13 +47,7 @@ public class SimpleScpClientTest extends BaseSimpleClientTestSupport {
public SimpleScpClientTest() throws Exception {
targetPath = detectTargetFolder();
parentPath = targetPath.getParent();
- final FileSystem fileSystem = new RootedFileSystemProvider().newFileSystem(parentPath, Collections.<String, Object>emptyMap());
- fileSystemFactory = new FileSystemFactory() {
- @Override
- public FileSystem createFileSystem(Session session) throws IOException {
- return fileSystem;
- }
- };
+ fileSystemFactory = new VirtualFileSystemFactory(parentPath);
}
@Override
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java
index 6482442..4fe9a78 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java
@@ -21,11 +21,9 @@ package org.apache.sshd.client.simple;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
-import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
@@ -33,7 +31,7 @@ import java.util.List;
import org.apache.sshd.client.subsystem.sftp.SftpClient;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.file.root.RootedFileSystemProvider;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.subsystem.sftp.SftpConstants;
import org.apache.sshd.common.util.io.IoUtils;
@@ -57,13 +55,7 @@ public class SimpleSftpClientTest extends BaseSimpleClientTestSupport {
public SimpleSftpClientTest() throws Exception {
targetPath = detectTargetFolder();
parentPath = targetPath.getParent();
- final FileSystem fileSystem = new RootedFileSystemProvider().newFileSystem(parentPath, Collections.<String, Object>emptyMap());
- fileSystemFactory = new FileSystemFactory() {
- @Override
- public FileSystem createFileSystem(Session session) throws IOException {
- return fileSystem;
- }
- };
+ fileSystemFactory = new VirtualFileSystemFactory(parentPath);
}
@Override
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClientTestSupport.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClientTestSupport.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClientTestSupport.java
index c743cad..b4e9fa9 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClientTestSupport.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/AbstractSftpClientTestSupport.java
@@ -20,15 +20,13 @@
package org.apache.sshd.client.subsystem.sftp;
import java.io.IOException;
-import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.Collections;
import org.apache.sshd.client.subsystem.sftp.extensions.SftpClientExtension;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.file.root.RootedFileSystemProvider;
-import org.apache.sshd.common.session.Session;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.command.ScpCommandFactory;
@@ -41,19 +39,12 @@ import org.apache.sshd.util.test.BaseTestSupport;
public abstract class AbstractSftpClientTestSupport extends BaseTestSupport {
protected SshServer sshd;
protected int port;
- protected final FileSystem rootFileSystem;
protected final FileSystemFactory fileSystemFactory;
protected AbstractSftpClientTestSupport() throws IOException {
Path targetPath = detectTargetFolder();
Path parentPath = targetPath.getParent();
- rootFileSystem = new RootedFileSystemProvider().newFileSystem(parentPath, Collections.<String, Object>emptyMap());
- fileSystemFactory = new FileSystemFactory() {
- @Override
- public FileSystem createFileSystem(Session session) throws IOException {
- return rootFileSystem;
- }
- };
+ fileSystemFactory = new VirtualFileSystemFactory(parentPath);
}
protected void setupServer() throws Exception {
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java
index cb00f99..64ca4aa 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java
@@ -57,8 +57,7 @@ import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.file.root.RootedFileSystemProvider;
-import org.apache.sshd.common.session.Session;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.subsystem.sftp.SftpConstants;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
@@ -85,13 +84,7 @@ public class SftpFileSystemTest extends BaseTestSupport {
public SftpFileSystemTest() throws IOException {
Path targetPath = detectTargetFolder();
Path parentPath = targetPath.getParent();
- final FileSystem fileSystem = new RootedFileSystemProvider().newFileSystem(parentPath, Collections.<String, Object>emptyMap());
- fileSystemFactory = new FileSystemFactory() {
- @Override
- public FileSystem createFileSystem(Session session) throws IOException {
- return fileSystem;
- }
- };
+ fileSystemFactory = new VirtualFileSystemFactory(parentPath);
}
@Before
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3d57e889/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
index a902e88..3988a64 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
@@ -59,6 +59,7 @@ import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.OptionalFeature;
import org.apache.sshd.common.PropertyResolverUtils;
import org.apache.sshd.common.file.FileSystemFactory;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.random.Random;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.subsystem.sftp.SftpConstants;
@@ -90,6 +91,7 @@ import org.apache.sshd.util.test.Utils;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@@ -111,10 +113,14 @@ public class SftpTest extends AbstractSftpClientTestSupport {
super();
}
+ @BeforeClass
+ public static void jschInit() {
+ JSchLogger.init();
+ }
+
@Before
public void setUp() throws Exception {
setupServer();
- JSchLogger.init();
JSch sch = new JSch();
session = sch.getSession("sshd", TEST_LOCALHOST, port);
session.setUserInfo(new SimpleUserInfo("sshd"));
@@ -258,6 +264,45 @@ public class SftpTest extends AbstractSftpClientTestSupport {
}
}
+ @Test // see SSHD-605
+ public void testCannotEscapeUserRoot() throws Exception {
+ Path targetPath = detectTargetFolder();
+ Path lclSftp = Utils.resolve(targetPath, SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName(), getCurrentTestName());
+ assertHierarchyTargetFolderExists(lclSftp);
+ sshd.setFileSystemFactory(new VirtualFileSystemFactory(lclSftp));
+
+ try (SshClient client = setupTestClient()) {
+ client.start();
+
+ try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
+ session.addPasswordIdentity(getCurrentTestName());
+ session.auth().verify(5L, TimeUnit.SECONDS);
+
+ String escapePath = targetPath.toString();
+ if (OsUtils.isWin32()) {
+ escapePath = "/" + escapePath.replace(File.separatorChar, '/');
+ }
+
+ try (SftpClient sftp = session.createSftpClient()) {
+ SftpClient.Attributes attrs = sftp.stat(escapePath);
+ fail("Unexpected escape success for path=" + escapePath + ": " + attrs);
+ } catch(SftpException e) {
+ if (OsUtils.isWin32()) {
+ assertEquals("Mismatched status for " + escapePath,
+ SftpConstants.getStatusName(SftpConstants.SSH_FX_INVALID_FILENAME),
+ SftpConstants.getStatusName(e.getStatus()));
+ } else {
+ assertEquals("Mismatched status for " + escapePath,
+ SftpConstants.getStatusName(SftpConstants.SSH_FX_NO_SUCH_FILE),
+ SftpConstants.getStatusName(e.getStatus()));
+ }
+ }
+ } finally {
+ client.stop();
+ }
+ }
+ }
+
@Test
public void testNormalizeRemoteRootValues() throws Exception {
try (SshClient client = setupTestClient()) {