Skip to content

Commit 2addb4e

Browse files
authored
[IO-845] test links to targets outside the source directory (#571)
* test links to targets outside the source directory * final
1 parent 4ad9b41 commit 2addb4e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/test/java/org/apache/commons/io/FileUtilsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,38 @@ public void testContentEqualsIgnoreEOL() throws Exception {
750750
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
751751
}
752752

753+
/**
754+
* Test what happens when copyDirectory copies a directory that contains a symlink
755+
* to a file outside the copied directory.
756+
*/
757+
@Test
758+
public void testCopyDirectory_symLinkExternalFile() throws Exception {
759+
// make a file
760+
final File content = new File(tempDirFile, "hello.txt");
761+
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
762+
763+
// Make a directory
764+
final File realDirectory = new File(tempDirFile, "real_directory");
765+
realDirectory.mkdir();
766+
767+
// Make a symlink to the file
768+
final Path linkPath = realDirectory.toPath().resolve("link_to_file");
769+
Files.createSymbolicLink(linkPath, content.toPath());
770+
771+
// Now copy the directory
772+
final File destination = new File(tempDirFile, "destination");
773+
FileUtils.copyDirectory(realDirectory, destination);
774+
775+
// test that the copied directory contains a link to the original file
776+
final File copiedLink = new File(destination, "link_to_file");
777+
assertTrue(Files.isSymbolicLink(copiedLink.toPath()));
778+
final String actual = FileUtils.readFileToString(copiedLink, "UTF8");
779+
assertEquals("HELLO WORLD", actual);
780+
781+
final Path source = Files.readSymbolicLink(copiedLink.toPath());
782+
assertEquals(content.toPath(), source);
783+
}
784+
753785
/**
754786
* See what happens when copyDirectory copies a directory that is a symlink
755787
* to another directory containing non-symlinked files.

0 commit comments

Comments
 (0)