aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/test/java/com/yahoo/security/tls/HostGlobPatternTest.java
blob: 4b3b7e22b835500775a6dd61f1770c339f96bd3c (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.tls;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;


/**
 * @author bjorncs
 */
public class HostGlobPatternTest {

    @Test
    void glob_without_wildcards_matches_entire_string() {
        assertTrue(globMatches("foo", "foo"));
        assertFalse(globMatches("foo", "fooo"));
        assertFalse(globMatches("foo", "ffoo"));
    }

    @Test
    void wildcard_glob_can_match_prefix() {
        assertTrue(globMatches("foo*", "foo"));
        assertTrue(globMatches("foo*", "foobar"));
        assertFalse(globMatches("foo*", "ffoo"));
    }

    @Test
    void wildcard_glob_can_match_suffix() {
        assertTrue(globMatches("*foo", "foo"));
        assertTrue(globMatches("*foo", "ffoo"));
        assertFalse(globMatches("*foo", "fooo"));
    }

    @Test
    void wildcard_glob_can_match_substring() {
        assertTrue(globMatches("f*o", "fo"));
        assertTrue(globMatches("f*o", "foo"));
        assertTrue(globMatches("f*o", "ffoo"));
        assertFalse(globMatches("f*o", "boo"));
    }

    @Test
    void wildcard_glob_does_not_cross_multiple_dot_delimiter_boundaries() {
        assertTrue(globMatches("*.bar.baz", "foo.bar.baz"));
        assertTrue(globMatches("*.bar.baz", ".bar.baz"));
        assertFalse(globMatches("*.bar.baz", "zoid.foo.bar.baz"));
        assertTrue(globMatches("foo.*.baz", "foo.bar.baz"));
        assertFalse(globMatches("foo.*.baz", "foo.bar.zoid.baz"));
    }

    @Test
    void single_char_glob_matches_non_dot_characters() {
        assertTrue(globMatches("f?o", "foo"));
        assertFalse(globMatches("f?o", "fooo"));
        assertFalse(globMatches("f?o", "ffoo"));
        assertFalse(globMatches("f?o", "f.o"));
    }

    @Test
    void other_regex_meta_characters_are_matched_as_literal_characters() {
        assertTrue(globMatches("<([{\\^-=$!|]})+.>", "<([{\\^-=$!|]})+.>"));
    }

    private static boolean globMatches(String pattern, String value) {
        return new HostGlobPattern(pattern).matches(value);
    }
}