You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "tomaswolf (via GitHub)" <gi...@apache.org> on 2023/04/22 13:28:10 UTC

[GitHub] [mina-sshd] tomaswolf commented on a diff in pull request #362: Fix rooted filesystem

tomaswolf commented on code in PR #362:
URL: https://github.com/apache/mina-sshd/pull/362#discussion_r1174415617


##########
sshd-common/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java:
##########
@@ -20,60 +20,118 @@
 package org.apache.sshd.common.file.root;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.Channel;
 import java.nio.channels.FileChannel;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.DirectoryStream;
 import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
+import java.nio.file.LinkOption;
+import java.nio.file.NoSuchFileException;
 import java.nio.file.OpenOption;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.Objects;
 import java.util.Random;
 import java.util.TreeSet;
+import java.util.function.Supplier;
 
+import com.google.common.jimfs.Configuration;
+import com.google.common.jimfs.Jimfs;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.test.CommonTestSupportUtils;
 import org.apache.sshd.util.test.NoIoTestCase;
+import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.FixMethodOrder;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
 import org.junit.runners.MethodSorters;
+import org.junit.runners.Parameterized;
 
 /**
  * Tests the RootedFileSystemProvider implementation of {@link java.nio.file.spi.FileSystemProvider} checking that
  * permissions for generic FS commands are not permitted outside of the root directory.
- *
+ * <p>
  * Individual tests are form pairs (e.g. testX, testXInvalid) where testXInvalid is expected to test a parent path of
  * {@link RootedFileSystem#getRoot()}
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 @Category({ NoIoTestCase.class })
+@RunWith(Parameterized.class)
 public class RootedFileSystemProviderTest extends AssertableFile {
-    private static RootedFileSystem fileSystem;
-    private static Path rootSandbox;
+    private static final String DOESNT_EXIST = "../doesnt_exist/../";
 
-    public RootedFileSystemProviderTest() {
-        super();
-    }
+    private static FileSystem unixInMemFs;
+    private static FileSystem windowsInMemFs;
+    private static FileSystem osxInMemFs;
+    private final FileSystem hostFilesystem;
+    private final RootedFileSystem fileSystem;
+    private final Path rootSandbox;
+    private final FileHelper fileHelper;
 
-    @BeforeClass
-    public static void initializeFileSystem() throws IOException {
+    public RootedFileSystemProviderTest(String fsType, Supplier<FileSystem> hostFilesystem) throws Exception {
+        super();
+        this.hostFilesystem = hostFilesystem.get();
         Path targetFolder = Objects.requireNonNull(
                 CommonTestSupportUtils.detectTargetFolder(RootedFileSystemProviderTest.class),
                 "Failed to detect target folder");
-        rootSandbox = FileHelper.createTestSandbox(targetFolder.resolve(TEMP_SUBFOLDER_NAME));
+        Path targetFolderOnHostFs = getTargetFolderOnHostFs(targetFolder);
+        fileHelper = new FileHelper();
+        rootSandbox = fileHelper.createTestSandbox(targetFolderOnHostFs.resolve(TEMP_SUBFOLDER_NAME));
         fileSystem = (RootedFileSystem) new RootedFileSystemProvider().newFileSystem(rootSandbox, Collections.emptyMap());
     }
 
+    @Parameterized.Parameters(name = "{0}FS")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(
+                new Object[] { "Windows", (Supplier<FileSystem>) () -> windowsInMemFs },
+                new Object[] { "Unix", (Supplier<FileSystem>) () -> unixInMemFs },
+                new Object[] { "MacOS", (Supplier<FileSystem>) () -> osxInMemFs },
+                new Object[] { "Native", (Supplier<FileSystem>) FileSystems::getDefault });
+    }
+
+    @BeforeClass
+    public static void initializeFileSystem() {
+        unixInMemFs = Jimfs.newFileSystem(Configuration.unix());
+        windowsInMemFs = Jimfs.newFileSystem(Configuration.windows());
+        osxInMemFs = Jimfs.newFileSystem(Configuration.osX());
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        List<Exception> failures = new ArrayList<>();
+        for (FileSystem fs : Arrays.asList(unixInMemFs, windowsInMemFs, osxInMemFs)) {
+            try {
+                fs.close();
+            } catch (Exception ex) {
+                failures.add(ex);
+            }
+        }
+        Assert.assertEquals(Collections.emptyList(), failures);
+    }
+
+    private Path getTargetFolderOnHostFs(Path targetFolder) {
+        return this.hostFilesystem.getSeparator().equals("\\")
+                ? this.hostFilesystem.getPath("C:", targetFolder.toString())

Review Comment:
   This looks suspicious. In CI, we're on drive D:.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org