Debian Codegraph @

tests/tools/test_identifiers.py

raw
1 """Tests for the identifiers tools."""
2
3 from typing import Any
4
5 import pytest
6
7 from asusrouter.tools.identifiers import (
8 ERROR_MAC_BYTE,
9 ERROR_MAC_INT,
10 ERROR_MAC_STR,
11 ERROR_MAC_UNSUPPORTED_TYPE,
12 MacAddress,
13 )
14
15 CORRECT_MAC = "aa:bb:cc:dd:ee:ff"
16 CORRECT_MAC_HEX = CORRECT_MAC.replace(":", "")
17 CORRECT_MAC_BYTES = bytes.fromhex(CORRECT_MAC_HEX)
18 CORRECT_MAC_INT = int.from_bytes(CORRECT_MAC_BYTES, "big")
19 CORRECT_MAC_AS_ASUS = CORRECT_MAC.upper()
20
21
22 def test_from_instance() -> None:
23 """Test initialization from an instance."""
24
25 instance = MacAddress.from_value(CORRECT_MAC)
26
27 assert MacAddress(instance) == instance
28 assert MacAddress.from_value(instance) == instance
29
30
31 def test_from_bytes() -> None:
32 """Test initialization from bytes."""
33
34 assert str(MacAddress.from_value(CORRECT_MAC_BYTES)) == CORRECT_MAC
35 assert str(MacAddress(CORRECT_MAC_BYTES)) == CORRECT_MAC
36
37
38 @pytest.mark.parametrize(
39 ("value"),
40 [
41 bytes.fromhex("00"),
42 bytes.fromhex("aabbff"),
43 ],
44 )
45 def test_from_bytes_fail(value: bytes) -> None:
46 """Test initialization from invalid bytes."""
47
48 with pytest.raises(ValueError, match=ERROR_MAC_BYTE):
49 MacAddress.from_value(value)
50
51 with pytest.raises(ValueError, match=ERROR_MAC_BYTE):
52 MacAddress(value)
53
54
55 @pytest.mark.parametrize(
56 ("value", "result"),
57 [
58 (CORRECT_MAC_INT, CORRECT_MAC),
59 (123, "00:00:00:00:00:7b"),
60 (" 123 ", "00:00:00:00:00:7b"),
61 ],
62 )
63 def test_from_int(value: int, result: str) -> None:
64 """Test initialization from an integer."""
65
66 instance = MacAddress(value)
67 assert str(instance) == result
68 assert MacAddress.from_value(value) == instance
69
70
71 @pytest.mark.parametrize(
72 ("value"),
73 [
74 -1,
75 1 << 48,
76 ],
77 )
78 def test_from_int_fail(value: int) -> None:
79 """Test initialization from an invalid integer."""
80
81 with pytest.raises(ValueError, match=ERROR_MAC_INT):
82 MacAddress.from_value(value)
83
84
85 @pytest.mark.parametrize(
86 ("value", "result"),
87 [
88 (CORRECT_MAC, CORRECT_MAC),
89 ("AA-BB-CC-DD-EE-FF", CORRECT_MAC),
90 ("aabb.ccdd.eeff", CORRECT_MAC),
91 ("AABBCCDDEEFF", CORRECT_MAC),
92 (f" {CORRECT_MAC} ", CORRECT_MAC),
93 ],
94 )
95 def test_from_string(value: str, result: str) -> None:
96 """Test initialization from various string formats."""
97
98 instance = MacAddress(value)
99 assert str(instance) == result
100 assert MacAddress.from_value(value) == instance
101
102
103 @pytest.mark.parametrize(
104 ("value"),
105 [
106 "not a mac",
107 "ff:aa:bb:cc:dd:ee:ff",
108 ],
109 )
110 def test_from_string_fail(value: str) -> None:
111 """Test initialization from invalid string formats."""
112
113 with pytest.raises(ValueError, match=ERROR_MAC_STR):
114 MacAddress.from_value(value)
115
116
117 @pytest.mark.parametrize(
118 ("value"),
119 [
120 None,
121 object(),
122 ],
123 )
124 def test_from_unsupported(value: Any) -> None:
125 """Test initialization from unsupported types."""
126
127 with pytest.raises(ValueError, match=ERROR_MAC_UNSUPPORTED_TYPE):
128 MacAddress.from_value(value)
129
130
131 @pytest.mark.parametrize(
132 ("value", "result"),
133 [
134 (CORRECT_MAC, CORRECT_MAC_AS_ASUS),
135 ("00:aa:11:bb:22:cc", "00:AA:11:BB:22:CC"),
136 ],
137 )
138 def test_to_asus(value: str, result: str) -> None:
139 """Test conversion to ASUS format."""
140
141 instance = MacAddress.from_value(value)
142 assert instance.as_asus() == result
143
144
145 def test_to_bytes() -> None:
146 """Test conversion to bytes."""
147
148 instance = MacAddress.from_value(CORRECT_MAC)
149 assert instance.to_bytes() == CORRECT_MAC_BYTES
150
151
152 def test_to_int() -> None:
153 """Test conversion to integer."""
154
155 instance = MacAddress.from_value(CORRECT_MAC)
156 assert instance.to_int() == CORRECT_MAC_INT
157
158
159 @pytest.mark.parametrize(
160 ("value", "expected"),
161 [
162 ("00:00:00:00:00:00", "02:00:00:00:00:00"),
163 ("44:00:00:00:00:00", "46:00:00:00:00:00"),
164 ("99:00:00:00:00:00", "9a:00:00:00:00:00"),
165 ("ff:00:00:00:00:00", "fe:00:00:00:00:00"),
166 ],
167 )
168 def test_set_locally_administered(value: str, expected: str) -> None:
169 """Test setting the locally-administered bit."""
170
171 instance = MacAddress.from_value(value)
172 instance.set_locally_administered()
173 assert str(instance) == expected
174
175
176 def test_repr() -> None:
177 """Test string representation."""
178
179 instance = MacAddress.from_value(CORRECT_MAC)
180 assert repr(instance) == CORRECT_MAC
181
182
183 @pytest.mark.parametrize(
184 ("value", "result"),
185 [
186 (CORRECT_MAC, True),
187 ("00:00:00:00:00:00", False),
188 (MacAddress(CORRECT_MAC), True),
189 (MacAddress("00:00:00:00:00:00"), False),
190 ],
191 )
192 def test_eq(value: Any, result: bool) -> None:
193 """Test equality comparison."""
194
195 instance = MacAddress.from_value(CORRECT_MAC)
196 assert (instance == value) is result
197
198
199 def test_eq_returns_notimplemented_for_uncomparable() -> None:
200 """Test equality comparison with uncomparable types."""
201
202 mac = MacAddress.from_value(CORRECT_MAC)
203
204 class Uncomparable:
205 pass
206
207 other = Uncomparable()
208
209 # Direct unbound call returns the NotImplemented sentinel
210 assert MacAddress.__eq__(mac, other) is NotImplemented
211
212 # Public comparison uses NotImplemented
213 assert (mac == other) is False
214 assert (other == mac) is False
215
216
217 def test_hash() -> None:
218 """Test hash function."""
219
220 instance = MacAddress.from_value(CORRECT_MAC)
221 assert hash(instance) == hash(CORRECT_MAC_BYTES)