From ae232b73d74daa75806fae1d351ae019959bdc20 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 3 Aug 2025 13:54:30 +0100 Subject: [PATCH] windows test issue --- .../test/java/org/apache/poi/util/TestIOUtils.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java index 094e138eef..b2e3c673a7 100644 --- a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java +++ b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java @@ -594,7 +594,7 @@ final class TestIOUtils { final File parent = TempFile.createTempDirectory("path-traversal-test"); try { // this path is ok because it doesn't walk out of the parent directory - final String path0 = "a/b/c/../d/e/../../f/g/./h"; + final String path0 = windowsPathIfNecessary("a/b/c/../d/e/../../f/g/./h"); File outFile = IOUtils.newFile(parent, path0); assertTrue(outFile.getAbsolutePath().endsWith(path0), "unexpected path: " + outFile.getAbsolutePath()); @@ -609,7 +609,7 @@ final class TestIOUtils { try { // this path is ok because it doesn't walk out of the parent directory // the initial slash is ignored and the generated path is relative to the parent directory - final String path0 = "/a/b/c.txt"; + final String path0 = windowsPathIfNecessary("/a/b/c.txt"); File outFile = IOUtils.newFile(parent, path0); assertTrue(outFile.getAbsolutePath().endsWith(path0), "unexpected path: " + outFile.getAbsolutePath()); @@ -622,13 +622,18 @@ final class TestIOUtils { void testDisallowedPathTraversal() throws IOException { final File parent = TempFile.createTempDirectory("path-traversal-test"); try { - final String path0 = "../a/b/c.txt"; + final String path0 = windowsPathIfNecessary("../a/b/c.txt"); Assertions.assertThrows(IOException.class, () -> IOUtils.newFile(parent, path0)); } finally { assertTrue(parent.delete()); } } + private static String windowsPathIfNecessary(String path) { + // this is a workaround for the Windows file system which doesn't allow slashes in file names + return File.pathSeparatorChar == '/' ? path : path.replace('/', File.pathSeparatorChar); + } + /** * This returns 0 for the first call to skip and then reads * as requested. This tests that the fallback to read() works.