Skip to content

Commit 278ca07

Browse files
authored
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
1 parent 546a4d2 commit 278ca07

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,71 @@ public void testCopyFile_symLink() throws Exception {
11131113
assertEquals("HELLO WORLD", contents);
11141114
}
11151115

1116+
/**
1117+
* See what happens when copyDirectory copies a directory that is a symlink
1118+
* to another directory containing non-symlinked files.
1119+
* This is a characterization test to explore current behavior, and arguably
1120+
* represents a bug. This behavior, and the test, is likely to change
1121+
* and should not be relied on.
1122+
*/
1123+
@Test
1124+
public void testCopyDir_symLink() throws Exception {
1125+
// Make a directory
1126+
final File realDirectory = new File(tempDirFile, "real_directory");
1127+
realDirectory.mkdir();
1128+
final File content = new File(realDirectory, "hello.txt");
1129+
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
1130+
1131+
// Make a symlink to the directory
1132+
final Path linkPath = tempDirFile.toPath().resolve("link_to_directory");
1133+
Files.createSymbolicLink(linkPath, realDirectory.toPath());
1134+
1135+
// Now copy symlink
1136+
final File destination = new File(tempDirFile, "destination");
1137+
1138+
// Is the copy a symlink or an actual directory?
1139+
FileUtils.copyDirectory(linkPath.toFile(), destination);
1140+
1141+
// delete the original file so that if we can read the bytes from the
1142+
// copied directory it's definitely been copied, not linked.
1143+
assumeTrue(content.delete());
1144+
1145+
assertFalse(Files.isSymbolicLink(destination.toPath()));
1146+
final File copied_content = new File(destination, "hello.txt");
1147+
final String actual = FileUtils.readFileToString(copied_content, "UTF8");
1148+
assertEquals("HELLO WORLD", actual);
1149+
}
1150+
1151+
@Test
1152+
public void testCopyDir_symLinkCycle() throws Exception {
1153+
// Make a directory
1154+
final File topDirectory = new File(tempDirFile, "topDirectory");
1155+
topDirectory.mkdir();
1156+
final File content = new File(topDirectory, "hello.txt");
1157+
FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
1158+
final File childDirectory = new File(topDirectory, "child_directory");
1159+
childDirectory.mkdir();
1160+
1161+
// Make a symlink to the top directory
1162+
final Path linkPath = childDirectory.toPath().resolve("link_to_top");
1163+
Files.createSymbolicLink(linkPath, topDirectory.toPath());
1164+
1165+
// Now copy symlink
1166+
final File destination = new File(tempDirFile, "destination");
1167+
FileUtils.copyDirectory(linkPath.toFile(), destination);
1168+
1169+
// delete the original file so that if we can read the bytes from the
1170+
// copied directory it's definitely been copied, not linked.
1171+
assumeTrue(content.delete());
1172+
1173+
assertFalse(Files.isSymbolicLink(destination.toPath()));
1174+
final File copied_content = new File(destination, "hello.txt");
1175+
final String actual = FileUtils.readFileToString(copied_content, "UTF8");
1176+
assertEquals("HELLO WORLD", actual);
1177+
1178+
final File[] copied = destination.listFiles();
1179+
assertEquals(2, copied.length);
1180+
}
11161181

11171182
@Test
11181183
public void testCopyFile1() throws Exception {

0 commit comments

Comments
 (0)