Debian Codegraph @

ansible_collections/community/crypto/tests/unit/plugins/module_utils/test__caa.py

raw
1 # Copyright (c) Ansible Project
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 re
8 import typing as t
9
10 import pytest
11
12 from ansible_collections.community.crypto.plugins.module_utils._caa import (
13 _check_domain_name,
14 _check_label,
15 _check_value,
16 join_issue_value,
17 parse_issue_value,
18 )
19
20 TEST_CHECK_VALUE: list[tuple[str, str | None]] = [
21 ("", None),
22 ("a", None),
23 (
24 "!\"#$%&'()*+,-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
25 None,
26 ),
27 ("a=b", None),
28 ("\x1f", "Invalid value '\\x1f'"),
29 (" ", "Invalid value ' '"),
30 (";", "Invalid value ';'"),
31 ("\x7f", "Invalid value '\\x7f'"),
32 ("a b", "Invalid value 'a b'"),
33 ]
34
35
36 @pytest.mark.parametrize("value, error", TEST_CHECK_VALUE)
37 def test_check_value(
38 value: str,
39 error: str | None,
40 ) -> None:
41 if error is None:
42 _check_value(value)
43 else:
44 with pytest.raises(ValueError, match=f"^{re.escape(error)}$"):
45 _check_value(value)
46
47
48 TEST_CHECK_LABEL: list[tuple[str, str | None]] = [
49 ("", "Invalid value ''"),
50 ("a", None),
51 ("0", None),
52 ("a-", None),
53 ("a=", "Invalid value 'a='"),
54 ("-a", "Invalid value '-a'"),
55 (" ", "Invalid value ' '"),
56 ("\t", "Invalid value '\\t'"),
57 ("a0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-", None),
58 ]
59
60
61 @pytest.mark.parametrize("value, error", TEST_CHECK_LABEL)
62 def test_check_label(
63 value: str,
64 error: str | None,
65 ) -> None:
66 if error is None:
67 _check_label(value, "value")
68 else:
69 with pytest.raises(ValueError, match=f"^{re.escape(error)}$"):
70 _check_label(value, "value")
71
72
73 TEST_CHECK_DOMAIN_NAME: list[tuple[str, str | None]] = [
74 ("", "Invalid label ''"),
75 ("a.", "Invalid label ''"),
76 (".a", "Invalid label ''"),
77 ("a.-", "Invalid label '-'"),
78 ("a.b", None),
79 ("a.b.c.d.e.f.g.h.i.j.k.l", None),
80 ("letsencrypt.org", None),
81 ]
82
83
84 @pytest.mark.parametrize("value, error", TEST_CHECK_DOMAIN_NAME)
85 def test_check_domain_name(
86 value: str,
87 error: str | None,
88 ) -> None:
89 if error is None:
90 _check_domain_name(value)
91 else:
92 with pytest.raises(ValueError, match=f"^{re.escape(error)}$"):
93 _check_domain_name(value)
94
95
96 TEST_PARSE_ISSUE_VALUE: list[
97 tuple[str, dict[str, t.Any], str | None, list[tuple[str, str]]]
98 ] = [
99 ("", {}, None, []),
100 (";", {}, None, []),
101 ("a=b", {"strict": False}, "a=b", []),
102 ("; a=b", {"strict": False}, None, [("a", "b")]),
103 ("a; a=b", {"strict": False}, "a", [("a", "b")]),
104 ("a; a=b; c-d=e", {"strict": False}, "a", [("a", "b"), ("c-d", "e")]),
105 ("ca1.example.net", {}, "ca1.example.net", []),
106 ("ca1.example.net; account=230123", {}, "ca1.example.net", [("account", "230123")]),
107 ]
108
109
110 @pytest.mark.parametrize(
111 "value, kwargs, expected_domain_name, expected_pairs", TEST_PARSE_ISSUE_VALUE
112 )
113 def test_parse_issue_value(
114 value: str,
115 kwargs: dict[str, t.Any],
116 expected_domain_name: str | None,
117 expected_pairs: list[tuple[str, str]],
118 ) -> None:
119 assert parse_issue_value(value, **kwargs) == (expected_domain_name, expected_pairs)
120
121
122 TEST_PARSE_ISSUE_VALUE_FAIL: list[tuple[str, dict[str, t.Any], str]] = [
123 ("a=b", {}, "Invalid label 'a=b'"),
124 ("a; a.b=b", {}, "Invalid tag 'a.b'"),
125 ("a; a=b; a=c", {}, "Tag 'a' appears multiple times"),
126 ("%%%%%", {}, "Invalid label '%%%%%'"),
127 ]
128
129
130 @pytest.mark.parametrize("value, kwargs, expected_error", TEST_PARSE_ISSUE_VALUE_FAIL)
131 def test_parse_issue_value_fail(
132 value: str, kwargs: dict[str, t.Any], expected_error: str
133 ) -> None:
134 with pytest.raises(ValueError, match=f"^{re.escape(expected_error)}$"):
135 parse_issue_value(value, **kwargs)
136
137
138 TEST_JOIN_ISSUE_VALUE: list[
139 tuple[str | None, list[tuple[str, str]], dict[str, t.Any], str]
140 ] = [
141 (None, [], {}, ""),
142 ("a", [], {}, "a"),
143 (None, [("a", "b")], {}, "; a=b"),
144 ("a", [("a", "b")], {}, "a; a=b"),
145 ]
146
147
148 @pytest.mark.parametrize(
149 "domain_name, pairs, kwargs, expected_result", TEST_JOIN_ISSUE_VALUE
150 )
151 def test_join_issue_value(
152 domain_name: str | None,
153 pairs: list[tuple[str, str]],
154 kwargs: dict[str, t.Any],
155 expected_result: str,
156 ) -> None:
157 assert join_issue_value(domain_name, pairs, **kwargs) == expected_result
158
159
160 TEST_JOIN_ISSUE_VALUE_FAIL: list[
161 tuple[str | None, list[tuple[str, str]], dict[str, t.Any], str]
162 ] = [
163 ("", [], {}, "Invalid label ''"),
164 (None, [("", "")], {}, "Invalid tag ''"),
165 (None, [("a", " ")], {}, "Invalid value ' '"),
166 (None, [("a", "a"), ("a", "b")], {}, "Tag 'a' appears multiple times"),
167 ]
168
169
170 @pytest.mark.parametrize(
171 "domain_name, pairs, kwargs, expected_error", TEST_JOIN_ISSUE_VALUE_FAIL
172 )
173 def test_join_issue_value_fail(
174 domain_name: str | None,
175 pairs: list[tuple[str, str]],
176 kwargs: dict[str, t.Any],
177 expected_error: str,
178 ) -> None:
179 with pytest.raises(ValueError, match=f"^{re.escape(expected_error)}$"):
180 join_issue_value(domain_name, pairs, **kwargs)