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 2015/02/23 16:30:09 UTC

[08/15] mina-sshd git commit: [SSHD-408] Implement sftp v4, v5 and v6

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/22581fb8/sshd-core/src/test/java/org/apache/sshd/SftpFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpFileSystemTest.java
index 750ecc9..8ad1e0b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpFileSystemTest.java
@@ -19,14 +19,23 @@
 package org.apache.sshd;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.URI;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.channels.OverlappingFileLockException;
 import java.nio.file.DirectoryStream;
+import java.nio.file.FileAlreadyExistsException;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.FileTime;
+import java.nio.file.attribute.PosixFilePermissions;
 import java.util.Arrays;
 import java.util.Map;
 
@@ -44,6 +53,7 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class SftpFileSystemTest extends BaseTest {
 
@@ -71,19 +81,40 @@ public class SftpFileSystemTest extends BaseTest {
     public void testFileSystem() throws Exception {
         Utils.deleteRecursive(new File("target/sftp"));
 
-        FileSystem fs = FileSystems.newFileSystem(URI.create("sftp://x:x@localhost:" + port + "/"), null);
+        String uri = "sftp://x:x@localhost:" + port + "/";
+
+        FileSystem fs = FileSystems.newFileSystem(URI.create(uri), null);
         Path root = fs.getRootDirectories().iterator().next();
         try (DirectoryStream<Path> ds = Files.newDirectoryStream(root)) {
             for (Path child : ds) {
                 System.out.println(child);
             }
         }
+        Path current = fs.getPath(".").toRealPath();
         Path file = fs.getPath("target/sftp/client/test.txt");
         Files.createDirectories(file.getParent());
         Files.write(file, "Hello world\n".getBytes());
         String buf = new String(Files.readAllBytes(file));
         assertEquals("Hello world\n", buf);
 
+        Path file2 = fs.getPath("target/sftp/client/test2.txt");
+        Path file3 = fs.getPath("target/sftp/client/test3.txt");
+        try {
+            Files.move(file2, file3);
+            fail("Expected an IOException");
+        } catch (NoSuchFileException e) {
+            // expected
+        }
+        Files.write(file2, "h".getBytes());
+        try {
+            Files.move(file, file2);
+            fail("Expected an IOException");
+        } catch (FileAlreadyExistsException e) {
+            // expected
+        }
+        Files.move(file, file2, StandardCopyOption.REPLACE_EXISTING);
+        Files.move(file2, file);
+
         Map<String, Object> attrs = Files.readAttributes(file, "*");
         System.out.println(attrs);
 
@@ -105,12 +136,47 @@ public class SftpFileSystemTest extends BaseTest {
         buf = new String(Files.readAllBytes(file));
         assertEquals("Hello world\n", buf);
 
+        try (FileChannel channel = FileChannel.open(file)) {
+            try (FileLock lock = channel.lock()) {
+                System.out.println("Locked " + lock.toString());
+
+                try (FileChannel channel2 = FileChannel.open(file)) {
+                    try (FileLock lock2 = channel2.lock()) {
+                        System.out.println("Locked " + lock2.toString());
+                        fail("Expected an exception");
+                    } catch (OverlappingFileLockException e) {
+                        // expected
+                    }
+                }
+
+            }
+        }
+
         Files.delete(file);
 
         fs.close();
     }
 
     @Test
+    public void testAttributes() throws Exception {
+        Utils.deleteRecursive(new File("target/sftp"));
+
+        FileSystem fs = FileSystems.newFileSystem(URI.create("sftp://x:x@localhost:" + port + "/"), null);
+        Path file = fs.getPath("target/sftp/client/test.txt");
+        Files.createDirectories(file.getParent());
+        Files.write(file, "Hello world\n".getBytes());
+
+        Map<String, Object> attrs = Files.readAttributes(file, "posix:*");
+
+        Files.setAttribute(file, "basic:size", 2l);
+        Files.setAttribute(file, "posix:permissions", PosixFilePermissions.fromString("rwxr-----"));
+        Files.setAttribute(file, "basic:lastModifiedTime", FileTime.fromMillis(100000l));
+        Files.setAttribute(file, "posix:group", file.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByGroupName("everyone"));
+
+        fs.close();
+    }
+
+    @Test
     public void testRootFileSystem() throws Exception {
         Path rootNative = Paths.get("target/root").toAbsolutePath();
         Utils.deleteRecursive(rootNative.toFile());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/22581fb8/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index b44f6af..7926544 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -31,7 +31,9 @@ import java.util.Vector;
 
 import com.jcraft.jsch.ChannelSftp;
 import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.SftpException;
 import org.apache.sshd.client.SftpClient;
+import org.apache.sshd.client.sftp.DefaultSftpClient;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.common.util.OsUtils;
@@ -50,6 +52,7 @@ import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertNull;
 import static junit.framework.Assert.assertTrue;
@@ -431,8 +434,52 @@ public class SftpTest extends BaseTest {
         URI base = new File(System.getProperty("user.dir")).getAbsoluteFile().toURI();
         String path = new File(base.relativize(url).getPath()).getParent() + "/";
         path = path.replace('\\', '/');
-        String real = c.realpath(path + "/foobar");
+        String real = c.realpath(path);
         System.out.println(real);
+        try {
+            real = c.realpath(path + "/foobar");
+            System.out.println(real);
+            fail("Expected SftpException");
+        } catch (SftpException e) {
+            // ok
+        }
+    }
+
+    @Test
+    public void testRename() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("x", "localhost", port).await().getSession();
+        session.addPasswordIdentity("x");
+        session.auth().verify();
+
+        Utils.deleteRecursive(new File("target/sftp"));
+        new File("target/sftp").mkdirs();
+        new File("target/sftp/client").delete();
+
+        SftpClient sftp = session.createSftpClient();
+        try (OutputStream os = sftp.write("target/sftp/test.txt")) {
+            os.write("Hello world!\n".getBytes());
+        }
+
+        try {
+            sftp.rename("target/sftp/test2.txt", "target/sftp/test3.txt");
+            fail("Expected an SftpException");
+        } catch (org.apache.sshd.client.SftpException e) {
+            assertEquals(DefaultSftpClient.SSH_FX_NO_SUCH_FILE, e.getStatus());
+        }
+
+        try (OutputStream os = sftp.write("target/sftp/test2.txt")) {
+            os.write("H".getBytes());
+        }
+
+        try {
+            sftp.rename("target/sftp/test.txt", "target/sftp/test2.txt");
+            fail("Expected an SftpException");
+        } catch (org.apache.sshd.client.SftpException e) {
+            assertEquals(DefaultSftpClient.SSH_FX_FILE_ALREADY_EXISTS, e.getStatus());
+        }
+        sftp.rename("target/sftp/test.txt", "target/sftp/test2.txt", SftpClient.CopyMode.Overwrite);
     }
 
     @Test
@@ -458,7 +505,7 @@ public class SftpTest extends BaseTest {
         assertTrue(target.exists());
         assertEquals("0123456789", readFile(unixPath));
 
-        c.symlink(linkUnixPath, unixPath);
+        c.symlink(unixPath, linkUnixPath);
 
         assertTrue(link.exists());
         assertEquals("0123456789", readFile(linkUnixPath));