You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/01/27 22:17:12 UTC
(commons-io) branch master updated: Add characterization test for copying a symlinked directory (#570)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 278ca074 Add characterization test for copying a symlinked directory (#570)
278ca074 is described below
commit 278ca074ac80193b7ce0abe1377fa99701530c1f
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Sat Jan 27 17:17:07 2024 -0500
Add characterization test for copying a symlinked directory (#570)
* test copying a symlinked directory
* checkstyle
* make sure test directories don't contain each other
* test that we don't infinitely loop when copying directories that contain themselves via symlinks
---
.../java/org/apache/commons/io/FileUtilsTest.java | 65 ++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index 02b643e0..6291dfb6 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -1113,6 +1113,71 @@ public class FileUtilsTest extends AbstractTempDirTest {
assertEquals("HELLO WORLD", contents);
}
+ /**
+ * See what happens when copyDirectory copies a directory that is a symlink
+ * to another directory containing non-symlinked files.
+ * This is a characterization test to explore current behavior, and arguably
+ * represents a bug. This behavior, and the test, is likely to change
+ * and should not be relied on.
+ */
+ @Test
+ public void testCopyDir_symLink() throws Exception {
+ // Make a directory
+ final File realDirectory = new File(tempDirFile, "real_directory");
+ realDirectory.mkdir();
+ final File content = new File(realDirectory, "hello.txt");
+ FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
+
+ // Make a symlink to the directory
+ final Path linkPath = tempDirFile.toPath().resolve("link_to_directory");
+ Files.createSymbolicLink(linkPath, realDirectory.toPath());
+
+ // Now copy symlink
+ final File destination = new File(tempDirFile, "destination");
+
+ // Is the copy a symlink or an actual directory?
+ FileUtils.copyDirectory(linkPath.toFile(), destination);
+
+ // delete the original file so that if we can read the bytes from the
+ // copied directory it's definitely been copied, not linked.
+ assumeTrue(content.delete());
+
+ assertFalse(Files.isSymbolicLink(destination.toPath()));
+ final File copied_content = new File(destination, "hello.txt");
+ final String actual = FileUtils.readFileToString(copied_content, "UTF8");
+ assertEquals("HELLO WORLD", actual);
+ }
+
+ @Test
+ public void testCopyDir_symLinkCycle() throws Exception {
+ // Make a directory
+ final File topDirectory = new File(tempDirFile, "topDirectory");
+ topDirectory.mkdir();
+ final File content = new File(topDirectory, "hello.txt");
+ FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
+ final File childDirectory = new File(topDirectory, "child_directory");
+ childDirectory.mkdir();
+
+ // Make a symlink to the top directory
+ final Path linkPath = childDirectory.toPath().resolve("link_to_top");
+ Files.createSymbolicLink(linkPath, topDirectory.toPath());
+
+ // Now copy symlink
+ final File destination = new File(tempDirFile, "destination");
+ FileUtils.copyDirectory(linkPath.toFile(), destination);
+
+ // delete the original file so that if we can read the bytes from the
+ // copied directory it's definitely been copied, not linked.
+ assumeTrue(content.delete());
+
+ assertFalse(Files.isSymbolicLink(destination.toPath()));
+ final File copied_content = new File(destination, "hello.txt");
+ final String actual = FileUtils.readFileToString(copied_content, "UTF8");
+ assertEquals("HELLO WORLD", actual);
+
+ final File[] copied = destination.listFiles();
+ assertEquals(2, copied.length);
+ }
@Test
public void testCopyFile1() throws Exception {