1import logging
2import sys
3
4import pytest
5
6logging.basicConfig(
7 level=logging.INFO,
8 format="%(asctime)s test %(levelname)s: %(message)s",
9 datefmt="%Y-%m-%d %H:%M:%S",
10)
11
12logger = logging.getLogger("ambassador")
13
14from ambassador.ir.irutils import hostglob_matches
15
16
17@pytest.mark.compilertest
18def test_hostglob_matches():
19 for v1, v2, wanted_result in [
20 ("a.example.com", "a.example.com", True),
21 ("a.example.com", "b.example.com", False),
22 ("*", "foo.example.com", True),
23 ("*.example.com", "a.example.com", True),
24 ("*example.com", "b.example.com", True),
25 # This is never OK: the "*" can't match a bare ".".
26 ("*example.com", ".example.com", False),
27 # This is OK, because DNS allows names to end with a "."
28 ("foo.example*", "foo.example.com.", True),
29 # This is never OK: the "*" cannot match an empty string.
30 ("*example.com", "example.com", False),
31 ("*ple.com", "b.example.com", True),
32 ("*.example.com", "a.example.org", False),
33 ("*example.com", "a.example.org", False),
34 ("*ple.com", "a.example.org", False),
35 ("a.example.*", "a.example.com", True),
36 ("a.example*", "a.example.com", True),
37 ("a.exa*", "a.example.com", True),
38 ("a.example.*", "a.example.org", True),
39 ("a.example.*", "b.example.com", False),
40 ("a.example*", "b.example.com", False),
41 ("a.exa*", "b.example.com", False),
42 # '*' has to appear at the beginning or the end, not in the middle.
43 ("a.*.com", "a.example.com", False),
44 # Various DNS glob situations disagree about whether "*" can cross subdomain
45 # boundaries. We follow what Envoy does, which is to allow crossing.
46 ("*.com", "a.example.com", True),
47 ("*.com", "a.example.org", False),
48 ("*.example.com", "*.example.com", True),
49 # This looks wrong but it's OK: both match e.g. foo.example.com.
50 ("*example.com", "*.example.com", True),
51 # These are ugly corner cases, but they should still work!
52 ("*.example.com", "a.example.*", True),
53 ("*.example.com", "a.b.example.*", True),
54 ("*.example.baz.com", "a.b.example.*", True),
55 ("*.foo.bar", "baz.zing.*", True),
56 # The Host.hostname is overloaded in that it determines the SNI for TLS and the
57 # virtual host name for :authority header matching for HTTP. These are valid
58 # scenarios that users try when using non-standard ports so we make sure they work.
59 ("*.local:8500", "quote.local", False),
60 ("*.local:8500", "quote.local:8500", True),
61 ("*", "quote.local:8500", True),
62 ("quote.*", "quote.local:8500", True),
63 ("quote.*", "*.local:8500", True),
64 ("quote.com:8500", "quote.com:8500", True),
65 ]:
66 assert hostglob_matches(v1, v2) == wanted_result, f"1. {v1} ~ {v2} != {wanted_result}"
67 assert hostglob_matches(v2, v1) == wanted_result, f"2. {v2} ~ {v1} != {wanted_result}"
68
69
70if __name__ == "__main__":
71 pytest.main(sys.argv)
View as plain text