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 9e0b5174 [IO-807] Characterization test for broken symlinks when
copying directories (#547)
9e0b5174 is described below
commit 9e0b5174f34477cfdf60c2127fccf88f6ede8837
Author: Elliotte Rusty Harold <[email protected]>
AuthorDate: Mon Jan 1 15:24:33 2024 +0000
[IO-807] Characterization test for broken symlinks when copying directories
(#547)
* test broken symlink when copying directory
* whitespace
* assertThrows
* final
* line length
---
.../java/org/apache/commons/io/FileUtilsTest.java | 35 ++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java
b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index a1305135..84eae472 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -26,6 +26,8 @@ import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
@@ -43,6 +45,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
+import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@@ -808,6 +811,38 @@ public class FileUtilsTest extends AbstractTempDirTest {
"Can't convert null list");
}
+ // IO-807
+ @Test
+ public void testCopyDirectory_brokenSymLink() throws IOException {
+ // Make a file
+ final File sourceDirectory = new File(tempDirFile, "source_directory");
+ sourceDirectory.mkdir();
+ final File targetFile = new File(sourceDirectory, "hello.txt");
+ FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
+
+ // Make a symlink to the file
+ final Path targetPath = targetFile.toPath();
+ final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
+ Files.createSymbolicLink(linkPath, targetPath);
+ assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink
here: " + linkPath);
+ assumeTrue(Files.exists(linkPath));
+ assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
+
+ // Delete the file file to break the symlink
+ assumeTrue(targetFile.delete());
+ assumeFalse(Files.exists(linkPath));
+ assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
+
+ // Now copy sourceDirectory, including the broken link, to another
directory
+ final File destination = new File(tempDirFile, "destination");
+ final FileNotFoundException thrown = assertThrows(
+ FileNotFoundException.class,
+ () -> FileUtils.copyDirectory(sourceDirectory, destination),
+ "ignored broken link"
+ );
+ assertTrue(thrown.getMessage().contains("linkfile' does not exist"));
+ }
+
@Test
public void testCopyDirectoryPreserveDates() throws Exception {
final File source = new File(tempDirFile, "source");