aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/ai/vespa/validation/PathValidatorTest.java
blob: a2c1bd6bd0c0d22b9a6413c972dfcb1595821808 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ai.vespa.validation;

import org.junit.Test;

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class PathValidatorTest {

    @Test
    public void testPathValidation() {
        Path root = Path.of("/foo/");

        assertOk(Path.of("/foo/bar"), root);
        assertOk(Path.of("/foo/foo2/bar"), root);
        assertOk(Path.of("/foo/foo2/../bar"), root);
        assertOk(Path.of("/foo/../foo/bar"), root);
        assertOk(Path.of("/bar/../foo/../foo/bar"), root);

        assertInvalid(Path.of("/foo/../bar"), root);
        assertInvalid(Path.of("/foo/bar/../../bar"), root);
    }

    private void assertOk(Path path, Path root) {
        PathValidator.validateChildOf(root, path);
    }

    private void assertInvalid(Path path, Path root) {
        IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class,
                                                                         () -> PathValidator.validateChildOf(root, path));
        assertEquals("Invalid path %s".formatted(path), illegalArgumentException.getMessage());
    }
}