Repository: mina-sshd Updated Branches: refs/heads/master 93d3aea6d -> 595858815
[SSHD-396] Fixed SftpTest#testOpen to work correctly on Windows Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/59585881 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/59585881 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/59585881 Branch: refs/heads/master Commit: 59585881559b0d1d7d1b089f209c7061aa268c5e Parents: 93d3aea Author: Guillaume Nodet <[email protected]> Authored: Tue Jan 13 16:09:38 2015 +0100 Committer: Guillaume Nodet <[email protected]> Committed: Tue Jan 13 16:09:38 2015 +0100 ---------------------------------------------------------------------- .../src/test/java/org/apache/sshd/SftpTest.java | 54 +++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/59585881/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 6585966..b4f2c0e 100644 --- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java @@ -34,6 +34,7 @@ import com.jcraft.jsch.JSch; import org.apache.sshd.client.SftpClient; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.util.Buffer; +import org.apache.sshd.common.util.OsUtils; import org.apache.sshd.server.Command; import org.apache.sshd.server.command.ScpCommandFactory; import org.apache.sshd.server.sftp.SftpSubsystem; @@ -99,40 +100,51 @@ public class SftpTest extends BaseTest { session.addPasswordIdentity("x"); session.auth().verify(); - String file = "target/sftp/client/test.txt"; + String file = "target/sftp/client/testOpen.txt"; + File javaFile = new File(file); - new File(file).getParentFile().mkdirs(); - new File(file).createNewFile(); - new File(file).setWritable(false, false); - new File(file).setReadable(false, false); + 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 { - sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read)); - fail("Should have failed"); + 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) { - // ok + if (isWindows) { + throw e; + } } try { - sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write)); - fail("Should have failed"); + h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Write)); + fail("Empty write should have failed"); } catch (IOException e) { // ok } try { - sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate)); - fail("Should have failed"); + h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate)); + fail("Empty truncate should have failed"); } catch (IOException e) { // ok } - Assert.assertEquals(0, (sftp.stat(file).perms & (SftpClient.S_IWUSR | SftpClient.S_IRUSR))); + // 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)); - new File(file).setWritable(true, false); + javaFile.setWritable(true, false); h = sftp.open(file, EnumSet.of(SftpClient.OpenMode.Truncate, SftpClient.OpenMode.Write)); sftp.close(h); @@ -150,18 +162,22 @@ public class SftpTest extends BaseTest { sftp.close(h); try { - sftp.open(file, EnumSet.of(SftpClient.OpenMode.Read)); - fail("Should have failed"); + 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) { - // ok + if (isWindows) { + throw e; + } } - new File(file).setReadable(true, false); + 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("2-4", new String(buf, 0, l)); + assertEquals("Mismatched read data", "2-4", new String(buf, 0, l)); sftp.close(h); }
