Apache Lucene open-source search software
LUCENE-10525 Improve WindowsFS emulation to catch invalid file names (#829)
Gautam Worah8d9a333fac6d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 425562fe7f1..4dfee8b9e04 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -165,6 +165,8 @@ Other* LUCENE-10526: Test-framework: Add FilterFileSystemProvider.wrapPath(Path) method for mock filesystemsto override if they need to extend the Path implementation. (Gautam Worah, Robert Muir)+* LUCENE-10525: Test-framework: Add detection of illegal windows filenames to WindowsFS. (Gautam Worah)+======================= Lucene 9.1.0 =======================API Changesdiff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/FilterPath.java b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/FilterPath.javaindex 99d5897690d..3019c38c209 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/FilterPath.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/FilterPath.java@@ -127,8 +127,8 @@ public class FilterPath implements Path, Unwrappable<Path> {}@Override- public boolean startsWith(String other) {- return delegate.startsWith(other);+ public final boolean startsWith(String other) {+ return Path.super.startsWith(other);}@Override@@ -137,8 +137,8 @@ public class FilterPath implements Path, Unwrappable<Path> {}@Override- public boolean endsWith(String other) {- return delegate.startsWith(other);+ public final boolean endsWith(String other) {+ return Path.super.endsWith(other);}@Override@@ -152,8 +152,8 @@ public class FilterPath implements Path, Unwrappable<Path> {}@Override- public Path resolve(String other) {- return wrap(delegate.resolve(other));+ public final Path resolve(String other) {+ return Path.super.resolve(other);}@Override@@ -162,8 +162,8 @@ public class FilterPath implements Path, Unwrappable<Path> {}@Override- public Path resolveSibling(String other) {- return wrap(delegate.resolveSibling(other));+ public final Path resolveSibling(String other) {+ return Path.super.resolveSibling(other);}@Override@@ -270,7 +270,7 @@ public class FilterPath implements Path, Unwrappable<Path> {return Unwrappable.unwrapAll(path);}- private final Path wrap(Path other) {+ protected final Path wrap(Path other) {return fileSystem.parent.wrapPath(other);}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsFS.java b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsFS.javaindex 23575d770cd..624ce4f0728 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsFS.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsFS.java@@ -166,4 +166,9 @@ public class WindowsFS extends HandleTrackingFS {return super.deleteIfExists(path);}}++ @Override+ public FilterPath wrapPath(Path path) {+ return new WindowsPath(path, fileSystem);+ }}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsPath.java b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsPath.javanew file mode 100644index 00000000000..a92fcb2887f--- /dev/null+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/mockfile/WindowsPath.java@@ -0,0 +1,62 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.tests.mockfile;++import java.nio.file.InvalidPathException;+import java.nio.file.Path;+import java.util.Arrays;+import java.util.HashSet;++class WindowsPath extends FilterPath {++ static HashSet<Character> RESERVED_CHARACTERS =+ new HashSet<>(Arrays.asList('<', '>', ':', '\"', '\\', '|', '?', '*'));++ static HashSet<String> RESERVED_NAMES =+ new HashSet<>(+ Arrays.asList(+ "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",+ "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8",+ "LPT9"));++ WindowsPath(Path path, FilterFileSystem fileSystem) {+ super(path, fileSystem);+ }++ @Override+ public Path resolve(Path other) {+ checkInvalidPath(other);+ return wrap(delegate.resolve(toDelegate(other)));+ }++ private void checkInvalidPath(Path other) {+ String fileName = other.getFileName().toString();++ if (RESERVED_NAMES.contains(fileName)) {+ throw new InvalidPathException(+ fileName, "File name: " + fileName + " is one of the reserved file names");+ }++ for (int i = 0; i < fileName.length(); i++) {+ if (RESERVED_CHARACTERS.contains(fileName.charAt(i))) {+ throw new InvalidPathException(+ fileName,+ "File name: " + fileName + " contains a reserved character: " + fileName.charAt(i));+ }+ }+ }+}diff --git a/lucene/test-framework/src/test/org/apache/lucene/tests/mockfile/TestWindowsFS.java b/lucene/test-framework/src/test/org/apache/lucene/tests/mockfile/TestWindowsFS.javaindex a414470d471..9c3f53cdede 100644--- a/lucene/test-framework/src/test/org/apache/lucene/tests/mockfile/TestWindowsFS.java+++ b/lucene/test-framework/src/test/org/apache/lucene/tests/mockfile/TestWindowsFS.java@@ -16,14 +16,17 @@*/package org.apache.lucene.tests.mockfile;+import com.carrotsearch.randomizedtesting.generators.RandomStrings;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.file.Files;+import java.nio.file.InvalidPathException;import java.nio.file.NoSuchFileException;import java.nio.file.Path;import java.nio.file.StandardCopyOption;+import java.util.Random;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.atomic.AtomicBoolean;import org.apache.lucene.util.Constants;@@ -35,6 +38,9 @@ public class TestWindowsFS extends MockFileSystemTestCase {public void setUp() throws Exception {super.setUp();// irony: currently we don't emulate windows well enough to work on windows!+ // TODO: Can we fork this class and create a new class with only those tests that can run on+ // Windows and then check if+ // the Lucene WindowsFS error is same as the one OG Windows throwsassumeFalse("windows is not supported", Constants.WINDOWS);}@@ -180,4 +186,33 @@ public class TestWindowsFS extends MockFileSystemTestCase {assertEquals(2, stream.read());}}++ public void testFileName() {+ Character[] reservedCharacters = WindowsPath.RESERVED_CHARACTERS.toArray(new Character[0]);+ String[] reservedNames = WindowsPath.RESERVED_NAMES.toArray(new String[0]);+ String fileName;+ Random r = random();+ Path dir = wrap(createTempDir());++ if (r.nextBoolean()) {+ // We need at least one char after the special char+ // For example, in case of '/' we interpret `foo/` to just be a file `foo` which is valid+ fileName =+ RandomStrings.randomAsciiLettersOfLength(r, r.nextInt(10))+ + reservedCharacters[r.nextInt(reservedCharacters.length)]+ + RandomStrings.randomAsciiLettersOfLength(r, r.nextInt(1, 10));+ } else {+ fileName = reservedNames[r.nextInt(reservedNames.length)];+ }+ expectThrows(InvalidPathException.class, () -> dir.resolve(fileName));++ // some other basic tests+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo:bar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo:bar:tar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo?bar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo<bar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo\\bar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo*bar|tar"));+ expectThrows(InvalidPathException.class, () -> dir.resolve("foo|bar?tar"));+ }}