Debian Codegraph @

tests/test_utils.py

raw symbols SCIP
1 import pytest
2
3 from bluetooth_data_tools import (
4 human_readable_name,
5 int_to_bluetooth_address,
6 mac_to_int,
7 manufacturer_data_to_raw,
8 newest_manufacturer_data,
9 short_address,
10 )
11
12
13 def test_int_to_bluetooth_address():
14 assert int_to_bluetooth_address(0) == "00:00:00:00:00:00"
15 assert int_to_bluetooth_address(1) == "00:00:00:00:00:01"
16 assert int_to_bluetooth_address(0xFFFFFFFFFFFF) == "FF:FF:FF:FF:FF:FF"
17 assert int_to_bluetooth_address(0x123456789ABC) == "12:34:56:78:9A:BC"
18 assert int_to_bluetooth_address(0xDEF012345678) == "DE:F0:12:34:56:78"
19
20
21 def test_newest_manufacturer_data():
22 data = {1: b"\x01\x02\x03\x04"}
23 assert newest_manufacturer_data(data) == b"\x01\x02\x03\x04"
24 assert newest_manufacturer_data({}) is None
25
26
27 def test_newest_manufacturer_data_id_zero():
28 """Manufacturer ID 0 is assigned (Ericsson Technology Licensing) and must
29 not be dropped by an accidental truthiness check on the dict key."""
30 assert newest_manufacturer_data({0: b"\x01\x02\x03\x04"}) == b"\x01\x02\x03\x04"
31 assert newest_manufacturer_data({1: b"first", 0: b"newest"}) == b"newest"
32
33
34 def test_manufacturer_data_to_raw():
35 assert (
36 manufacturer_data_to_raw(1, b"\x01\x02\x03\x04")
37 == b"\x00\x00\x01\x00\x01\x02\x03\x04"
38 )
39
40
41 def test_short_address():
42 assert short_address("AA:BB:CC:DD:EE:FF") == "EEFF"
43
44
45 def test_short_address_hyphen_separator():
46 """Windows-style hyphen-separated addresses are accepted, matching mac_to_int."""
47 assert short_address("AA-BB-CC-DD-EE-FF") == "EEFF"
48
49
50 def test_short_address_unseparated():
51 """Unseparated 12-char form is accepted, matching mac_to_int."""
52 assert short_address("AABBCCDDEEFF") == "EEFF"
53
54
55 def test_short_address_lowercase():
56 assert short_address("aa:bb:cc:dd:ee:ff") == "EEFF"
57
58
59 @pytest.mark.parametrize(
60 "address",
61 ["AA:BB:CC:DD:EE:FF", "AA-BB-CC-DD-EE-FF", "AABBCCDDEEFF", "aabbccddeeff"],
62 )
63 def test_short_address_format_parity(address):
64 """Every address format mac_to_int accepts must also resolve via short_address."""
65 assert mac_to_int(address) == 0xAABBCCDDEEFF
66 assert short_address(address) == "EEFF"
67
68
69 def test_human_readable_name():
70 assert (
71 human_readable_name("My Device", "Your Device", "AA:BB:CC:DD:EE:FF")
72 == "My Device (EEFF)"
73 )
74
75
76 def test_mac_to_int():
77 assert mac_to_int("00:00:00:00:00:00") == 0
78 assert mac_to_int("00:00:00:00:00:01") == 1
79 assert mac_to_int("FF:FF:FF:FF:FF:FF") == 0xFFFFFFFFFFFF
80
81
82 def test_mac_to_int_hyphen_separator():
83 """Windows-style hyphen-separated addresses are accepted, matching short_address."""
84 assert mac_to_int("AA-BB-CC-DD-EE-FF") == 0xAABBCCDDEEFF
85 assert mac_to_int("00-00-00-00-00-00") == 0
86
87
88 def test_mac_to_int_lowercase():
89 assert mac_to_int("aa:bb:cc:dd:ee:ff") == 0xAABBCCDDEEFF
90
91
92 def test_mac_to_int_unseparated():
93 """Unseparated 12-char form is accepted."""
94 assert mac_to_int("AABBCCDDEEFF") == 0xAABBCCDDEEFF
95 assert mac_to_int("0123456789ab") == 0x0123456789AB
96
97
98 @pytest.mark.parametrize(
99 "value",
100 [0, 1, 0xAB, 0xDEADBEEF, 0xFFFFFFFFFFFF, 0x123456789ABC],
101 )
102 def test_mac_to_int_round_trip(value):
103 """mac_to_int is the inverse of int_to_bluetooth_address."""
104 assert mac_to_int(int_to_bluetooth_address(value)) == value
105
106
107 @pytest.mark.parametrize(
108 "bad",
109 [
110 "not-a-mac",
111 "AA:BB:CC:DD:EE:GG",
112 "AA/BB/CC/DD/EE/FF",
113 "",
114 ],
115 )
116 def test_mac_to_int_invalid(bad):
117 """Malformed input raises ValueError (matches int() fallback semantics)."""
118 with pytest.raises(ValueError):
119 mac_to_int(bad)
120
121
122 @pytest.mark.parametrize(
123 "bad",
124 [
125 "AABBCCDDEEFFA1234", # 17 hex chars, no separators
126 "AABB-CCDDEEFF-123", # 17 chars, separators in the wrong positions
127 "AA:BB:CC:DD:EE:F", # 16 chars (too short by one)
128 "AA:BB:CC:DD:EE:FFF", # 18 chars (too long by one)
129 "AABBCCDDEE_F", # 12 chars with a non-hex underscore
130 "AABBCCDDEE F", # 12 chars with embedded whitespace
131 ],
132 )
133 def test_mac_to_int_invalid_parity(bad):
134 """Inputs the native parser rejects must also be rejected by the
135 pure-Python fallback, so both build modes behave identically.
136
137 The old fallback (strip separators, then int(..., 16)) was looser than
138 the native C parser: it ignored separator position, accepted a fully
139 unseparated 17-char hex string as a 68-bit value, and let int() honor
140 underscore digit separators / surrounding whitespace.
141 """
142 with pytest.raises(ValueError):
143 mac_to_int(bad)