Skip to content

Commit 9e0b517

Browse files
authored
[IO-807] Characterization test for broken symlinks when copying directories (#547)
* test broken symlink when copying directory * whitespace * assertThrows * final * line length
1 parent 0fbae67 commit 9e0b517

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static org.junit.jupiter.api.Assertions.assertThrows;
2727
import static org.junit.jupiter.api.Assertions.assertTrue;
2828
import static org.junit.jupiter.api.Assertions.fail;
29+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
30+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2931

3032
import java.io.BufferedOutputStream;
3133
import java.io.ByteArrayOutputStream;
@@ -43,6 +45,7 @@
4345
import java.nio.charset.StandardCharsets;
4446
import java.nio.charset.UnsupportedCharsetException;
4547
import java.nio.file.Files;
48+
import java.nio.file.LinkOption;
4649
import java.nio.file.Path;
4750
import java.nio.file.Paths;
4851
import java.nio.file.StandardCopyOption;
@@ -808,6 +811,38 @@ public void testToURLs3() {
808811
"Can't convert null list");
809812
}
810813

814+
// IO-807
815+
@Test
816+
public void testCopyDirectory_brokenSymLink() throws IOException {
817+
// Make a file
818+
final File sourceDirectory = new File(tempDirFile, "source_directory");
819+
sourceDirectory.mkdir();
820+
final File targetFile = new File(sourceDirectory, "hello.txt");
821+
FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
822+
823+
// Make a symlink to the file
824+
final Path targetPath = targetFile.toPath();
825+
final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
826+
Files.createSymbolicLink(linkPath, targetPath);
827+
assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath);
828+
assumeTrue(Files.exists(linkPath));
829+
assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
830+
831+
// Delete the file file to break the symlink
832+
assumeTrue(targetFile.delete());
833+
assumeFalse(Files.exists(linkPath));
834+
assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
835+
836+
// Now copy sourceDirectory, including the broken link, to another directory
837+
final File destination = new File(tempDirFile, "destination");
838+
final FileNotFoundException thrown = assertThrows(
839+
FileNotFoundException.class,
840+
() -> FileUtils.copyDirectory(sourceDirectory, destination),
841+
"ignored broken link"
842+
);
843+
assertTrue(thrown.getMessage().contains("linkfile' does not exist"));
844+
}
845+
811846
@Test
812847
public void testCopyDirectoryPreserveDates() throws Exception {
813848
final File source = new File(tempDirFile, "source");

0 commit comments

Comments
 (0)