From 227a08908b8af93a8015cb50c867b43dc994eafb Mon Sep 17 00:00:00 2001 From: Rob Spoor Date: Tue, 5 Dec 2023 17:20:39 +0100 Subject: [PATCH] Don't overwrite the default dir if the URI's path is / --- .../sftp/SFTPFileSystemProvider.java | 4 +- .../sftp/SFTPFileSystemProviderTest.java | 138 ++++++++++++++---- 2 files changed, 115 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProvider.java b/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProvider.java index d0ebbb2..af415d8 100644 --- a/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProvider.java +++ b/src/main/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProvider.java @@ -116,7 +116,9 @@ private void addUserInfoIfNeeded(SFTPEnvironment environment, String userInfo) { } private void addDefaultDirIfNeeded(SFTPEnvironment environment, String path) { - if (path != null && !path.isEmpty()) { + // It's possible for the path to be set and the environment to have a default dir - if the path is / + // If that's the case, don't overwrite the environment value + if (path != null && !path.isEmpty() && !environment.hasDefaultDir()) { environment.withDefaultDirectory(path); } } diff --git a/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProviderTest.java b/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProviderTest.java index 33569df..b6bfbed 100644 --- a/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProviderTest.java +++ b/src/test/java/com/github/robtimus/filesystems/sftp/SFTPFileSystemProviderTest.java @@ -37,6 +37,7 @@ import java.io.OutputStream; import java.net.URI; import java.nio.file.FileSystem; +import java.nio.file.FileSystemException; import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -57,6 +58,7 @@ import org.junit.jupiter.api.Test; import com.github.robtimus.filesystems.Messages; import com.github.robtimus.filesystems.URISupport; +import com.jcraft.jsch.JSchException; @SuppressWarnings("nls") class SFTPFileSystemProviderTest extends AbstractSFTPFileSystemTest { @@ -112,38 +114,122 @@ void testFileSystemNotFound() { @Nested class NewFileSystem { - @Test - void testWithMinimalEnv() throws IOException { - URI uri = URI.create(getBaseUrlWithCredentials() + getDefaultDir()); - try (FileSystem fs = FileSystems.newFileSystem(uri, createMinimalEnv())) { - Path path = fs.getPath(""); - assertEquals(getDefaultDir(), path.toAbsolutePath().toString()); + @Nested + class WithCredentials { + + @Test + void testWithCredentialsFromURI() throws IOException { + URI uri = URI.create(getBaseUrlWithCredentials() + getDefaultDir()); + try (FileSystem fs = FileSystems.newFileSystem(uri, createMinimalEnv())) { + Path path = fs.getPath(""); + assertEquals(getDefaultDir(), path.toAbsolutePath().toString()); + } } - } - @Test - void testWithMinimalIdentityEnv() throws IOException { - URI uri = URI.create(getBaseUrl() + getDefaultDir()); - try (FileSystem fs = FileSystems.newFileSystem(uri, createMinimalIdentityEnv())) { - Path path = fs.getPath(""); - assertEquals(getDefaultDir(), path.toAbsolutePath().toString()); + @Test + void testWithCredentialsFromURIWithIdentityFromEnv() throws IOException { + URI uri = URI.create(getBaseUrl() + getDefaultDir()); + try (FileSystem fs = FileSystems.newFileSystem(uri, createMinimalIdentityEnv())) { + Path path = fs.getPath(""); + assertEquals(getDefaultDir(), path.toAbsolutePath().toString()); + } } - } - @Test - void testWithUserInfoAndCredentials() { - URI uri = URI.create(getBaseUrl()); - SFTPEnvironment env = createEnv(); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> FileSystems.newFileSystem(uri, env)); - assertChainEquals(Messages.uri().hasUserInfo(uri), exception); + @Test + void testWithCredentialsFromEnv() throws IOException { + URI uri = getURI(); + try (FileSystem fs = FileSystems.newFileSystem(uri, createEnv())) { + Path path = fs.getPath(""); + assertEquals(getDefaultDir(), path.toAbsolutePath().toString()); + } + } + + @Test + void testWithCredentialsFromURIAndEnv() { + URI uri = URI.create(getBaseUrl()); + SFTPEnvironment env = createEnv(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> FileSystems.newFileSystem(uri, env)); + assertChainEquals(Messages.uri().hasUserInfo(uri), exception); + } + + @Test + void testWithoutCredentials() { + URI uri = getURI(); + SFTPEnvironment env = createMinimalEnv(); + FileSystemException exception = assertThrows(FileSystemException.class, () -> FileSystems.newFileSystem(uri, env)); + assertInstanceOf(JSchException.class, exception.getCause()); + } } - @Test - void testWithPathAndDefaultDir() { - URI uri = getURI().resolve("/path"); - SFTPEnvironment env = createEnv(); - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> FileSystems.newFileSystem(uri, env)); - assertChainEquals(Messages.uri().hasPath(uri), exception); + @Nested + class WithPath { + + @Test + void testNoPathFromURIOrEnv() throws IOException { + URI uri = getURI(); + SFTPEnvironment env = createMinimalIdentityEnv().withUsername(getUsername()); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path path = fs.getPath(""); + assertEquals("/", path.toAbsolutePath().toString()); + } + } + + @Test + void testPathFromEnv() throws IOException { + String defaultDir = getDefaultDir() + "/foo"; + addDirectoryIfNotExists(defaultDir); + + URI uri = getURI(); + SFTPEnvironment env = createEnv().withDefaultDirectory(defaultDir); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path path = fs.getPath(""); + assertEquals(defaultDir, path.toAbsolutePath().toString()); + } + } + + @Test + void testRootPathFromURINoPathFromEnv() throws IOException { + URI uri = getURI().resolve("/"); + SFTPEnvironment env = createMinimalIdentityEnv().withUsername(getUsername()); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path path = fs.getPath(""); + assertEquals("/", path.toAbsolutePath().toString()); + } + } + + @Test + void testRootPathFromURIAndPathFromEnv() throws IOException { + String defaultDir = getDefaultDir() + "/foo"; + addDirectoryIfNotExists(defaultDir); + + URI uri = getURI().resolve("/"); + SFTPEnvironment env = createEnv().withDefaultDirectory(defaultDir); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path path = fs.getPath(""); + assertEquals(defaultDir, path.toAbsolutePath().toString()); + } + } + + @Test + void testPathFromURI() throws IOException { + String defaultDir = getDefaultDir() + "/foo"; + addDirectoryIfNotExists(defaultDir); + + URI uri = getURI().resolve(defaultDir); + SFTPEnvironment env = createMinimalIdentityEnv().withUsername(getUsername()); + try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { + Path path = fs.getPath(""); + assertEquals(defaultDir, path.toAbsolutePath().toString()); + } + } + + @Test + void testPathFromURIAndEnv() { + URI uri = getURI().resolve("/path"); + SFTPEnvironment env = createEnv(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> FileSystems.newFileSystem(uri, env)); + assertChainEquals(Messages.uri().hasPath(uri), exception); + } } @Test