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/03/02 13:08:55 UTC

[4/4] mina-sshd git commit: [SSHD-426] Fix SftpTest#testOpen code to take into account Windows behavior

[SSHD-426] Fix SftpTest#testOpen code to take into account Windows behavior


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

Branch: refs/heads/master
Commit: 91a721e945c961b3c02e940805ba1a0f0d696fb6
Parents: ef86ae5
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Mar 2 11:07:05 2015 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Mar 2 13:00:54 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/sshd/client/SftpClient.java |   4 +-
 .../sshd/client/sftp/DefaultSftpClient.java     |   3 +-
 .../apache/sshd/client/sftp/SftpFileSystem.java |   3 +-
 .../apache/sshd/server/sftp/SftpSubsystem.java  |   8 +-
 .../src/test/java/org/apache/sshd/SftpTest.java | 169 ++++++++++---------
 5 files changed, 98 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/91a721e9/sshd-core/src/main/java/org/apache/sshd/client/SftpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SftpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/SftpClient.java
index 5c5aeb5..4001aaf 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/SftpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/SftpClient.java
@@ -21,8 +21,8 @@ package org.apache.sshd.client;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.nio.ByteBuffer;
 import java.nio.file.attribute.FileTime;
+import java.util.Collection;
 import java.util.EnumSet;
 import java.util.concurrent.TimeUnit;
 
@@ -214,7 +214,7 @@ public interface SftpClient extends AutoCloseable {
     // Low level API
     //
 
-    Handle open(String path, EnumSet<OpenMode> options) throws IOException;
+    Handle open(String path, Collection<OpenMode> options) throws IOException;
 
     void close(Handle handle) throws IOException;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/91a721e9/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
index 0389f6f..a793cc6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/DefaultSftpClient.java
@@ -27,6 +27,7 @@ import java.io.InterruptedIOException;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.attribute.FileTime;
+import java.util.Collection;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -627,7 +628,7 @@ public class DefaultSftpClient implements SftpClient {
         }
     }
 
-    public Handle open(String path, EnumSet<OpenMode> options) throws IOException {
+    public Handle open(String path, Collection<OpenMode> options) throws IOException {
         Buffer buffer = new Buffer();
         buffer.putString(path);
         if (version == SFTP_V3) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/91a721e9/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
index 98d6679..734a3e2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/sftp/SftpFileSystem.java
@@ -25,6 +25,7 @@ import java.nio.file.attribute.GroupPrincipal;
 import java.nio.file.attribute.UserPrincipal;
 import java.nio.file.attribute.UserPrincipalLookupService;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashSet;
@@ -142,7 +143,7 @@ public class SftpFileSystem extends BaseFileSystem<SftpPath> {
         }
 
         @Override
-        public Handle open(String path, EnumSet<OpenMode> options) throws IOException {
+        public Handle open(String path, Collection<OpenMode> options) throws IOException {
             return delegate.open(path, options);
         }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/91a721e9/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
index 963f90a..16fc902 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/sftp/SftpSubsystem.java
@@ -392,7 +392,7 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
     private UnsupportedAttributePolicy unsupportedAttributePolicy = UnsupportedAttributePolicy.Warn;
 
     protected static abstract class Handle implements java.io.Closeable {
-        Path file;
+        private Path file;
 
         public Handle(Path file) {
             this.file = file;
@@ -453,9 +453,9 @@ public class SftpSubsystem implements Command, Runnable, SessionAware, FileSyste
     }
 
     protected class FileHandle extends Handle {
-        final FileChannel channel;
-        long pos;
-        final List<FileLock> locks = new ArrayList<>();
+        private final FileChannel channel;
+        private long pos;
+        private final List<FileLock> locks = new ArrayList<>();
 
         public FileHandle(Path file, int flags, int access, Map<String, Object> attrs) throws IOException {
             super(file);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/91a721e9/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 71d6f20..78cb70a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -95,89 +95,96 @@ public class SftpTest extends BaseTest {
     public void testOpen() throws Exception {
         SshClient client = SshClient.setUpDefaultClient();
         client.start();
-        ClientSession session = client.connect("x", "localhost", port).await().getSession();
-        session.addPasswordIdentity("x");
-        session.auth().verify();
-
-        String file = "target/sftp/client/testOpen.txt";
-        File javaFile = new File(file);
-
-        javaFile.getParentFile().mkdirs();
-        javaFile.createNewFile();
-        javaFile.setWritable(false, false);
-        javaFile.setReadable(false, false);
-
-        SftpClient sftp = session.createSftpClient();
-        SftpClient.Handle h;
-
-        boolean	isWindows = OsUtils.isWin32();
-
-        try {
-            h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
-            // NOTE: on Windows files are always readable
-            // see https://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/windows/org/apache/harmony/luni/tests/java/io/WinFileTest.java
-            Assert.assertTrue("Empty read should have failed", isWindows);
-            sftp.close(h);
-        } catch (IOException e) {
-            if (isWindows) {
-                throw e;
-            }
-        }
-
-        try {
-            h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
-            fail("Empty write should have failed");
-        } catch (IOException e) {
-            // ok
-        }
-
-        try {
-            h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate));
-            fail("Empty truncate should have failed");
-        } catch (IOException e) {
-            // ok
-        }
-
-        // NOTE: on Windows files are always readable
-        int	perms=sftp.stat(file).perms;
-        int	permsMask=SftpClient.S_IWUSR | (isWindows ? 0 : SftpClient.S_IRUSR);
-        Assert.assertEquals("Mismatched permissions - 0x" + Integer.toHexString(perms), 0, (perms & permsMask));
-
-        javaFile.setWritable(true, false);
-
-        h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate, SftpClient.OpenMode.Write));
-        sftp.close(h);
-
-        h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
-        byte[] d = "0123456789\n".getBytes();
-        sftp.write(h, 0, d, 0, d.length);
-        sftp.write(h, d.length, d, 0, d.length);
-        sftp.close(h);
-        h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
-        sftp.write(h, d.length * 2, d, 0, d.length);
-        sftp.close(h);
-        h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
-        sftp.write(h, 3, "-".getBytes(), 0, 1);
-        sftp.close(h);
-
-        try {
-            h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
-            // NOTE: on Windows files are always readable
-            Assert.assertTrue("Data read should have failed", isWindows);
-            sftp.close(h);
-        } catch (IOException e) {
-            if (isWindows) {
-                throw e;
+        
+        try (ClientSession session = client.connect("x", "localhost", port).await().getSession()) {
+            session.addPasswordIdentity("x");
+            session.auth().verify();
+    
+            String file = "target/sftp/client/testOpen.txt";
+            File javaFile = new File(file);
+    
+            assertHierarchyTargetFolderExists(javaFile.getParentFile());
+            javaFile.createNewFile();
+            javaFile.setWritable(false, false);
+            javaFile.setReadable(false, false);
+    
+            try (SftpClient sftp = session.createSftpClient()) {
+                SftpClient.Handle h;
+        
+                boolean	isWindows = OsUtils.isWin32();
+        
+                try {
+                    h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
+                    // NOTE: on Windows files are always readable
+                    // see https://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/windows/org/apache/harmony/luni/tests/java/io/WinFileTest.java
+                    Assert.assertTrue("Empty read should have failed", isWindows);
+                    sftp.close(h);
+                } catch (IOException e) {
+                    if (isWindows) {
+                        throw e;
+                    }
+                }
+        
+                try {
+                    h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
+                    fail("Empty write should have failed");
+                } catch (IOException e) {
+                    // ok
+                }
+        
+                try {
+                    h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate));
+                    // NOTE: on Windows files are always readable
+                    Assert.assertTrue("Empty truncate should have failed", isWindows);
+                    sftp.close(h);
+                } catch (IOException e) {
+                    // ok
+                }
+        
+                // NOTE: on Windows files are always readable
+                int	perms=sftp.stat(file).perms;
+                int	permsMask=SftpClient.S_IWUSR | (isWindows ? 0 : SftpClient.S_IRUSR);
+                Assert.assertEquals("Mismatched permissions - 0x" + Integer.toHexString(perms), 0, (perms & permsMask));
+        
+                javaFile.setWritable(true, false);
+        
+                h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate, SftpClient.OpenMode.Write));
+                sftp.close(h);
+        
+                h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
+                byte[] d = "0123456789\n".getBytes();
+                sftp.write(h, 0, d, 0, d.length);
+                sftp.write(h, d.length, d, 0, d.length);
+                sftp.close(h);
+                h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
+                sftp.write(h, d.length * 2, d, 0, d.length);
+                sftp.close(h);
+                h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write));
+                sftp.write(h, 3, "-".getBytes(), 0, 1);
+                sftp.close(h);
+        
+                try {
+                    h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
+                    // NOTE: on Windows files are always readable
+                    Assert.assertTrue("Data read should have failed", isWindows);
+                    sftp.close(h);
+                } catch (IOException e) {
+                    if (isWindows) {
+                        throw e;
+                    }
+                }
+        
+                javaFile.setReadable(true, false);
+        
+                h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
+                byte[] buf = new byte[3];
+                int l = sftp.read(h, 2l, buf, 0, 3);
+                assertEquals("Mismatched read data", "2-4", new String(buf, 0, l));
+                sftp.close(h);
             }
+        } finally {
+            client.stop();
         }
-
-        javaFile.setReadable(true, false);
-
-        h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read));
-        byte[] buf = new byte[3];
-        int l = sftp.read(h, 2l, buf, 0, 3);
-        assertEquals("Mismatched read data", "2-4", new String(buf, 0, l));
-        sftp.close(h);
     }
 
     @Test