diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2022-03-25 10:43:30 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2022-03-25 10:43:30 +0100 |
commit | 41f20e0ffe572f2b42b389e21c5c683a38f319a2 (patch) | |
tree | d5e1fa9112e5b842c0128c613c378a943e14cd34 /vespajlib | |
parent | 7d8808a32d70cbf17a45059b4541e350307c01dd (diff) |
Improve error message
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/path/Path.java | 5 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/path/PathTest.java | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/path/Path.java b/vespajlib/src/main/java/com/yahoo/path/Path.java index 12f93d15737..4fcabe57d25 100644 --- a/vespajlib/src/main/java/com/yahoo/path/Path.java +++ b/vespajlib/src/main/java/com/yahoo/path/Path.java @@ -43,6 +43,10 @@ public final class Path { * @param elements a list of path elements */ private Path(List<String> elements, String delimiter) { + for (String element : elements) + if ("..".equals(element)) + throw new IllegalArgumentException("'..' is not allowed in path"); + this.elements = ImmutableList.copyOf(elements); this.delimiter = delimiter; } @@ -138,6 +142,7 @@ public final class Path { * @throws IllegalStateException if this path is empty */ public Path withLast(String element) { + if (element.contains(delimiter)) throw new IllegalArgumentException("single element cannot contain delimiter " + delimiter); if (element.isEmpty()) throw new IllegalStateException("Cannot set the last element of an empty path"); List<String> newElements = new ArrayList<>(elements); newElements.set(newElements.size() -1, element); diff --git a/vespajlib/src/test/java/com/yahoo/path/PathTest.java b/vespajlib/src/test/java/com/yahoo/path/PathTest.java index ae4581f1e9d..9446320d2c9 100644 --- a/vespajlib/src/test/java/com/yahoo/path/PathTest.java +++ b/vespajlib/src/test/java/com/yahoo/path/PathTest.java @@ -105,6 +105,16 @@ public class PathTest { assertEquals("/foo/bar/baz/foo/bar/baz", p3.getAbsolute()); } + @Test(expected = IllegalArgumentException.class) + public void testDoubleDot() { + Path.fromString("foo/../bar"); + } + + @Test(expected = IllegalArgumentException.class) + public void testLastWithDelimiter() { + Path.fromString("foo/bar").withLast("../../baz"); + } + private Path getRelativePath() { return Path.fromString("foo/bar/baz"); } |