Debian Codegraph @

ansible_collections/community/dns/tests/unit/plugins/module_utils/test__names.py

raw
1 # Copyright (c) 2021, Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import pytest
8
9 from ansible_collections.community.dns.plugins.module_utils._names import (
10 InvalidDomainName,
11 is_ascii_label,
12 join_labels,
13 normalize_label,
14 split_into_labels,
15 )
16
17 TEST_IS_ASCII_LABEL = [
18 ("asdf", True),
19 ("", True),
20 ("ä", False),
21 ("☹", False),
22 ("_dmarc", True),
23 ]
24
25
26 @pytest.mark.parametrize("domain, result", TEST_IS_ASCII_LABEL)
27 def test_is_ascii_label(domain, result):
28 assert is_ascii_label(domain) == result
29
30
31 TEST_LABEL_SPLIT = [
32 ("", [], ""),
33 (".", [], "."),
34 ("com", ["com"], ""),
35 ("com.", ["com"], "."),
36 ("foo.bar", ["bar", "foo"], ""),
37 ("foo.bar.", ["bar", "foo"], "."),
38 ("*.bar.", ["bar", "*"], "."),
39 ("☺.A", ["A", "☺"], ""),
40 ]
41
42
43 @pytest.mark.parametrize("domain, labels, tail", TEST_LABEL_SPLIT)
44 def test_split_into_labels(domain, labels, tail):
45 _labels, _tail = split_into_labels(domain)
46 assert _labels == labels
47 assert _tail == tail
48 assert join_labels(_labels, _tail) == domain
49
50
51 TEST_LABEL_SPLIT_ERRORS = [
52 ".bar.",
53 "..bar",
54 "-bar",
55 "bar-",
56 ]
57
58
59 @pytest.mark.parametrize("domain", TEST_LABEL_SPLIT_ERRORS)
60 def test_split_into_labels_errors(domain):
61 with pytest.raises(InvalidDomainName):
62 split_into_labels(domain)
63
64
65 TEST_LABEL_JOIN = [
66 ([], "", ""),
67 ([], ".", "."),
68 (["a", "b", "c"], "", "c.b.a"),
69 (["a", "b", "c"], ".", "c.b.a."),
70 ]
71
72
73 @pytest.mark.parametrize("labels, tail, result", TEST_LABEL_JOIN)
74 def test_join_labels(labels, tail, result):
75 domain = join_labels(labels, tail)
76 assert domain == result
77 _labels, _tail = split_into_labels(domain)
78 assert _labels == labels
79 assert _tail == tail
80
81
82 TEST_LABEL_NORMALIZE = [
83 ("", ""),
84 ("*", "*"),
85 ("foo", "foo"),
86 ("Foo", "foo"),
87 ("_dmarc", "_dmarc"),
88 ("hëllö", "xn--hll-jma1d"),
89 ("食狮", "xn--85x722f"),
90 ("☺", "xn--74h"),
91 ("😉", "xn--n28h"),
92 ]
93
94
95 @pytest.mark.parametrize("label, normalized_label", TEST_LABEL_NORMALIZE)
96 def test_normalize_label(label, normalized_label):
97 print(normalize_label(label))
98 assert normalize_label(label) == normalized_label