| 1 |
import base64 |
| 2 |
import logging |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from bluetooth_data_tools import ( |
| 7 |
parse_advertisement_data, |
| 8 |
parse_advertisement_data_bytes, |
| 9 |
parse_advertisement_data_tuple, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
def test_parse_advertisement_data_Prodigio_D83567A4F5A5_bytes(): |
| 14 |
data = b"".join( |
| 15 |
[ |
| 16 |
base64.b64decode("AgoEFglQcm9kaWdpb19EODM1NjdBNEY1QTU="), |
| 17 |
base64.b64decode("AgEGEQYbxdWlAgCqneMRKvIQGaoGCf8CJUQJgAcAAg=="), |
| 18 |
] |
| 19 |
) |
| 20 |
|
| 21 |
(local_name, service_uuids, service_data, manufacturer_data, tx_power) = ( |
| 22 |
parse_advertisement_data_bytes(data) |
| 23 |
) |
| 24 |
|
| 25 |
assert local_name == "Prodigio_D83567A4F5A5" |
| 26 |
assert service_uuids == ["06aa1910-f22a-11e3-9daa-0002a5d5c51b"] |
| 27 |
assert service_data == {} |
| 28 |
assert manufacturer_data == {9474: b"D\t\x80\x07\x00\x02"} |
| 29 |
assert tx_power == 4 |
| 30 |
|
| 31 |
|
| 32 |
def test_parse_advertisement_data_Prodigio_D83567A4F5A5(): |
| 33 |
data = [ |
| 34 |
base64.b64decode("AgoEFglQcm9kaWdpb19EODM1NjdBNEY1QTU="), |
| 35 |
base64.b64decode("AgEGEQYbxdWlAgCqneMRKvIQGaoGCf8CJUQJgAcAAg=="), |
| 36 |
] |
| 37 |
|
| 38 |
adv = parse_advertisement_data(data) |
| 39 |
|
| 40 |
assert adv.local_name == "Prodigio_D83567A4F5A5" |
| 41 |
assert adv.service_uuids == ["06aa1910-f22a-11e3-9daa-0002a5d5c51b"] |
| 42 |
assert adv.service_data == {} |
| 43 |
assert adv.manufacturer_data == {9474: b"D\t\x80\x07\x00\x02"} |
| 44 |
assert adv.tx_power == 4 |
| 45 |
|
| 46 |
|
| 47 |
def test_parse_advertisement_data_unknown_apple_device(): |
| 48 |
data = [ |
| 49 |
base64.b64decode("AgEaAgoFCv9MABAFChx3+Vs="), |
| 50 |
] |
| 51 |
|
| 52 |
adv = parse_advertisement_data(data) |
| 53 |
|
| 54 |
assert adv.local_name is None |
| 55 |
assert adv.service_uuids == [] |
| 56 |
assert adv.service_data == {} |
| 57 |
assert adv.manufacturer_data == {76: b"\x10\x05\n\x1cw\xf9["} |
| 58 |
assert adv.tx_power == 5 |
| 59 |
|
| 60 |
|
| 61 |
def test_parse_advertisement_data_empty(): |
| 62 |
data = [ |
| 63 |
b"\x00", |
| 64 |
] |
| 65 |
|
| 66 |
adv = parse_advertisement_data(data) |
| 67 |
|
| 68 |
assert adv.local_name is None |
| 69 |
assert adv.service_uuids == [] |
| 70 |
assert adv.service_data == {} |
| 71 |
assert adv.manufacturer_data == {} |
| 72 |
assert adv.tx_power is None |
| 73 |
|
| 74 |
|
| 75 |
def test_parse_advertisement_data_flags_only(): |
| 76 |
data = [ |
| 77 |
b"\x01\x01\x06", |
| 78 |
] |
| 79 |
|
| 80 |
adv = parse_advertisement_data(data) |
| 81 |
|
| 82 |
assert adv.local_name is None |
| 83 |
assert adv.service_uuids == [] |
| 84 |
assert adv.service_data == {} |
| 85 |
assert adv.manufacturer_data == {} |
| 86 |
assert adv.tx_power is None |
| 87 |
|
| 88 |
|
| 89 |
def test_parse_advertisement_data_ignores_invalid(): |
| 90 |
data = [ |
| 91 |
b"\x02\x01\x1a\x02\n\x05\n\xffL\x00\x10\x05\n\x1cw\xf9[\x02\x01", |
| 92 |
] |
| 93 |
|
| 94 |
adv = parse_advertisement_data(data) |
| 95 |
|
| 96 |
assert adv.local_name is None |
| 97 |
assert adv.service_uuids == [] |
| 98 |
assert adv.service_data == {} |
| 99 |
assert adv.manufacturer_data == {76: b"\x10\x05\n\x1cw\xf9["} |
| 100 |
assert adv.tx_power == 5 |
| 101 |
|
| 102 |
|
| 103 |
def test_parse_advertisement_data_trailing_minimum_ad_struct(caplog): |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
data = [b"\x04\xff\x4c\x00\x10\x01\x09"] |
| 108 |
|
| 109 |
with caplog.at_level(logging.DEBUG, logger="bluetooth_data_tools.gap"): |
| 110 |
adv = parse_advertisement_data(data) |
| 111 |
|
| 112 |
assert adv.manufacturer_data == {76: b"\x10"} |
| 113 |
assert any("Invalid BLE GAP AD structure" in r.message for r in caplog.records) |
| 114 |
|
| 115 |
|
| 116 |
def test_parse_advertisement_data_ignores_zero_type(): |
| 117 |
data = [ |
| 118 |
b"\x02\x01\x1a\x02\n\x05\n\xffL\x00\x10\x05\n\x1cw\xf9[\x02\x00", |
| 119 |
] |
| 120 |
|
| 121 |
adv = parse_advertisement_data(data) |
| 122 |
|
| 123 |
assert adv.local_name is None |
| 124 |
assert adv.service_uuids == [] |
| 125 |
assert adv.service_data == {} |
| 126 |
assert adv.manufacturer_data == {76: b"\x10\x05\n\x1cw\xf9["} |
| 127 |
assert adv.tx_power == 5 |
| 128 |
|
| 129 |
|
| 130 |
def test_parse_advertisement_data_unknown_fd3d(): |
| 131 |
data = [ |
| 132 |
base64.b64decode("AgEGD/9pCWBV+Tw02tgAEDEAAA=="), |
| 133 |
base64.b64decode("BhY9/WcAZA=="), |
| 134 |
] |
| 135 |
|
| 136 |
adv = parse_advertisement_data(data) |
| 137 |
|
| 138 |
assert adv.local_name is None |
| 139 |
assert adv.service_uuids == [] |
| 140 |
assert adv.service_data == {"0000fd3d-0000-1000-8000-00805f9b34fb": b"g\x00d"} |
| 141 |
assert adv.manufacturer_data == {2409: b"`U\xf9<4\xda\xd8\x00\x101\x00\x00"} |
| 142 |
assert adv.tx_power is None |
| 143 |
|
| 144 |
|
| 145 |
def test_parse_advertisement_data_moat(): |
| 146 |
data = [ |
| 147 |
base64.b64decode("AgEGAwMAEBUWABDfeeOmErMVUHBjVGIcb7kL//8="), |
| 148 |
base64.b64decode("AgoAAwMAIAsWAFDfeeOmErO5CwgJTW9hdF9TMg=="), |
| 149 |
] |
| 150 |
|
| 151 |
adv = parse_advertisement_data(data) |
| 152 |
|
| 153 |
assert adv.local_name == "Moat_S2" |
| 154 |
assert adv.service_uuids == [ |
| 155 |
"00001000-0000-1000-8000-00805f9b34fb", |
| 156 |
"00002000-0000-1000-8000-00805f9b34fb", |
| 157 |
] |
| 158 |
assert adv.service_data == { |
| 159 |
"00001000-0000-1000-8000-00805f9b34fb": b"\xdfy\xe3\xa6\x12\xb3\x15PpcTb" |
| 160 |
b"\x1co\xb9\x0b\xff\xff", |
| 161 |
"00005000-0000-1000-8000-00805f9b34fb": b"\xdfy\xe3\xa6\x12\xb3\xb9\x0b", |
| 162 |
} |
| 163 |
assert adv.manufacturer_data == {} |
| 164 |
assert adv.tx_power == 0 |
| 165 |
|
| 166 |
|
| 167 |
def test_parse_advertisement_data_unknown_apple_215(): |
| 168 |
data = [ |
| 169 |
base64.b64decode("AgEGGv9MAAIV1Ubfl0dXR+++CT4ty90MdxU2zcm1"), |
| 170 |
] |
| 171 |
|
| 172 |
adv = parse_advertisement_data(data) |
| 173 |
|
| 174 |
assert adv.local_name is None |
| 175 |
assert adv.service_uuids == [] |
| 176 |
assert adv.service_data == {} |
| 177 |
assert adv.manufacturer_data == { |
| 178 |
76: b"\x02\x15\xd5F\xdf\x97GWG\xef\xbe\t>-\xcb\xdd\x0cw\x156\xcd\xc9\xb5" |
| 179 |
} |
| 180 |
assert adv.tx_power is None |
| 181 |
|
| 182 |
|
| 183 |
def test_parse_advertisement_data_oral_b_toothbrush(): |
| 184 |
data = [ |
| 185 |
base64.b64decode("AgEGDv/cAAYyawNSAAEECQAEAwIN/g=="), |
| 186 |
base64.b64decode("EglPcmFsLUIgVG9vdGhicnVzaAUSEABQAAIKAA=="), |
| 187 |
] |
| 188 |
|
| 189 |
adv = parse_advertisement_data(data) |
| 190 |
|
| 191 |
assert adv.local_name == "Oral-B Toothbrush" |
| 192 |
assert adv.service_uuids == ["0000fe0d-0000-1000-8000-00805f9b34fb"] |
| 193 |
assert adv.service_data == {} |
| 194 |
assert adv.manufacturer_data == {220: b"\x062k\x03R\x00\x01\x04\t\x00\x04"} |
| 195 |
assert adv.tx_power == 0 |
| 196 |
|
| 197 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 198 |
"Oral-B Toothbrush", |
| 199 |
["0000fe0d-0000-1000-8000-00805f9b34fb"], |
| 200 |
{}, |
| 201 |
{220: b"\x062k\x03R\x00\x01\x04\t\x00\x04"}, |
| 202 |
0, |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
def test_parse_advertisement_short_local_name(): |
| 207 |
data = [ |
| 208 |
base64.b64decode("AgEGFv9MAAYxAOTEm+77PgUADQABAmMRIGUECE5hbg=="), |
| 209 |
] |
| 210 |
|
| 211 |
adv = parse_advertisement_data(data) |
| 212 |
|
| 213 |
assert adv.local_name == "Nan" |
| 214 |
assert adv.service_uuids == [] |
| 215 |
assert adv.service_data == {} |
| 216 |
assert adv.manufacturer_data == { |
| 217 |
76: b"\x061\x00\xe4\xc4\x9b\xee\xfb>\x05\x00\r\x00\x01\x02c\x11 e" |
| 218 |
} |
| 219 |
assert adv.tx_power is None |
| 220 |
|
| 221 |
|
| 222 |
def test_parse_advertisement_complete_overrides_short_local_name(): |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
data = [b"\x04\x08Bar\x04\x09Foo"] |
| 227 |
|
| 228 |
adv = parse_advertisement_data(data) |
| 229 |
|
| 230 |
assert adv.local_name == "Foo" |
| 231 |
|
| 232 |
|
| 233 |
def test_parse_advertisement_short_local_name_ignored_after_complete(): |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
data = [b"\x04\x09Foo\x04\x08Bar"] |
| 238 |
|
| 239 |
adv = parse_advertisement_data(data) |
| 240 |
|
| 241 |
assert adv.local_name == "Foo" |
| 242 |
|
| 243 |
|
| 244 |
def test_parse_advertisement_data_32bit_service_data(): |
| 245 |
data = [ |
| 246 |
b"\x07\x20\x1a\x02\n\x05\n\xff", |
| 247 |
] |
| 248 |
|
| 249 |
adv = parse_advertisement_data(data) |
| 250 |
|
| 251 |
assert adv.local_name is None |
| 252 |
assert adv.service_uuids == [] |
| 253 |
assert adv.service_data == {"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff"} |
| 254 |
assert adv.manufacturer_data == {} |
| 255 |
assert adv.tx_power is None |
| 256 |
|
| 257 |
|
| 258 |
def test_parse_advertisement_data_128bit_service_data(): |
| 259 |
data = [ |
| 260 |
b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04", |
| 261 |
] |
| 262 |
|
| 263 |
adv = parse_advertisement_data(data) |
| 264 |
|
| 265 |
assert adv.local_name is None |
| 266 |
assert adv.service_uuids == [] |
| 267 |
assert adv.service_data == {"00090401-0052-036b-3206-ff0a050a021a": b"\x04"} |
| 268 |
assert adv.manufacturer_data == {} |
| 269 |
assert adv.tx_power is None |
| 270 |
|
| 271 |
|
| 272 |
def test_parse_advertisement_data_32bit_and_128bit_service_data(): |
| 273 |
data = [ |
| 274 |
b"\x07\x20\x1a\x02\n\x05\n\xff", |
| 275 |
b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04", |
| 276 |
] |
| 277 |
|
| 278 |
adv = parse_advertisement_data(data) |
| 279 |
|
| 280 |
assert adv.local_name is None |
| 281 |
assert adv.service_uuids == [] |
| 282 |
assert adv.service_data == { |
| 283 |
"00090401-0052-036b-3206-ff0a050a021a": b"\x04", |
| 284 |
"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff", |
| 285 |
} |
| 286 |
assert adv.manufacturer_data == {} |
| 287 |
assert adv.tx_power is None |
| 288 |
|
| 289 |
|
| 290 |
def test_parse_advertisement_data_128bit_and_32bit_service_data(): |
| 291 |
data = [ |
| 292 |
b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04", |
| 293 |
b"\x07\x20\x1a\x02\n\x05\n\xff", |
| 294 |
] |
| 295 |
|
| 296 |
adv = parse_advertisement_data(data) |
| 297 |
|
| 298 |
assert adv.local_name is None |
| 299 |
assert adv.service_uuids == [] |
| 300 |
assert adv.service_data == { |
| 301 |
"00090401-0052-036b-3206-ff0a050a021a": b"\x04", |
| 302 |
"050a021a-0000-1000-8000-00805f9b34fb": b"\n\xff", |
| 303 |
} |
| 304 |
assert adv.manufacturer_data == {} |
| 305 |
assert adv.tx_power is None |
| 306 |
|
| 307 |
|
| 308 |
def test_parse_advertisement_data_128bit_service_data_tuple(): |
| 309 |
data = (b"\x12\x21\x1a\x02\n\x05\n\xff\x062k\x03R\x00\x01\x04\t\x00\x04",) |
| 310 |
|
| 311 |
adv = parse_advertisement_data(data) |
| 312 |
|
| 313 |
assert adv.local_name is None |
| 314 |
assert adv.service_uuids == [] |
| 315 |
assert adv.service_data == {"00090401-0052-036b-3206-ff0a050a021a": b"\x04"} |
| 316 |
assert adv.manufacturer_data == {} |
| 317 |
assert adv.tx_power is None |
| 318 |
|
| 319 |
|
| 320 |
def test_parse_advertisement_data_zero_padded(): |
| 321 |
data = [ |
| 322 |
bytes.fromhex( |
| 323 |
"02.01.06.0E.FF.69.09.FA.62.0F.CF.2D.F2.DA.0F" |
| 324 |
".00.22.04.00.09.16.3D.FD.63.C0.56.00.22.04".replace(".", "") |
| 325 |
) |
| 326 |
] |
| 327 |
|
| 328 |
adv = parse_advertisement_data(data) |
| 329 |
|
| 330 |
assert adv.local_name is None |
| 331 |
assert adv.service_uuids == [] |
| 332 |
assert adv.service_data == { |
| 333 |
"0000fd3d-0000-1000-8000-00805f9b34fb": b'c\xc0V\x00"\x04' |
| 334 |
} |
| 335 |
assert adv.manufacturer_data == {2409: b'\xfab\x0f\xcf-\xf2\xda\x0f\x00"\x04'} |
| 336 |
assert adv.tx_power is None |
| 337 |
|
| 338 |
|
| 339 |
def test_parse_adv_data(): |
| 340 |
data = [ |
| 341 |
bytes.fromhex( |
| 342 |
"02.01.06.05.02.E0.FF.E7.FE.0B.FF.65.0B.88.A0" |
| 343 |
".C8.47.8C.EA.D1.C1.0C.09.4D.32.5F.42.31.41.38" |
| 344 |
".53.31.30.50".replace(".", "") |
| 345 |
) |
| 346 |
] |
| 347 |
adv = parse_advertisement_data(data) |
| 348 |
|
| 349 |
assert adv.local_name == "M2_B1A8S10P" |
| 350 |
assert adv.service_uuids == [ |
| 351 |
"0000ffe0-0000-1000-8000-00805f9b34fb", |
| 352 |
"0000fee7-0000-1000-8000-00805f9b34fb", |
| 353 |
] |
| 354 |
assert adv.service_data == {} |
| 355 |
assert adv.manufacturer_data == {2917: b"\x88\xa0\xc8G\x8c\xea\xd1\xc1"} |
| 356 |
assert adv.tx_power is None |
| 357 |
|
| 358 |
|
| 359 |
def test_parse_multiple_16bit_uuids(): |
| 360 |
"""Test parsing advertisement data with 3 16-bit service UUIDs.""" |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
data = [bytes.fromhex("070234127856BC9A")] |
| 366 |
adv = parse_advertisement_data(data) |
| 367 |
|
| 368 |
assert adv.service_uuids == [ |
| 369 |
"00001234-0000-1000-8000-00805f9b34fb", |
| 370 |
"00005678-0000-1000-8000-00805f9b34fb", |
| 371 |
"00009abc-0000-1000-8000-00805f9b34fb", |
| 372 |
] |
| 373 |
assert adv.local_name is None |
| 374 |
assert adv.service_data == {} |
| 375 |
assert adv.manufacturer_data == {} |
| 376 |
assert adv.tx_power is None |
| 377 |
|
| 378 |
|
| 379 |
def test_parse_multiple_32bit_uuids(): |
| 380 |
"""Test parsing advertisement data with 2 32-bit service UUIDs.""" |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
data = [bytes.fromhex("090478563412F0DEBC9A")] |
| 386 |
adv = parse_advertisement_data(data) |
| 387 |
|
| 388 |
assert adv.service_uuids == [ |
| 389 |
"12345678-0000-1000-8000-00805f9b34fb", |
| 390 |
"9abcdef0-0000-1000-8000-00805f9b34fb", |
| 391 |
] |
| 392 |
assert adv.local_name is None |
| 393 |
assert adv.service_data == {} |
| 394 |
assert adv.manufacturer_data == {} |
| 395 |
assert adv.tx_power is None |
| 396 |
|
| 397 |
|
| 398 |
def test_parse_mixed_16bit_32bit_uuids(): |
| 399 |
"""Test parsing advertisement data with 16-bit UUID followed by 32-bit UUID.""" |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
data = [ |
| 404 |
bytes.fromhex( |
| 405 |
"03023412" |
| 406 |
"090478563412F0DEBC9A" |
| 407 |
) |
| 408 |
] |
| 409 |
adv = parse_advertisement_data(data) |
| 410 |
|
| 411 |
assert adv.service_uuids == [ |
| 412 |
"00001234-0000-1000-8000-00805f9b34fb", |
| 413 |
"12345678-0000-1000-8000-00805f9b34fb", |
| 414 |
"9abcdef0-0000-1000-8000-00805f9b34fb", |
| 415 |
] |
| 416 |
assert adv.local_name is None |
| 417 |
assert adv.service_data == {} |
| 418 |
assert adv.manufacturer_data == {} |
| 419 |
assert adv.tx_power is None |
| 420 |
|
| 421 |
|
| 422 |
def test_parse_mixed_16bit_128bit_uuids(): |
| 423 |
"""Test parsing advertisement data with 16-bit UUID followed by 128-bit UUID.""" |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
data = [ |
| 429 |
bytes.fromhex( |
| 430 |
"03023412" |
| 431 |
"110600004455664416a7d4419be200840e55" |
| 432 |
) |
| 433 |
] |
| 434 |
adv = parse_advertisement_data(data) |
| 435 |
|
| 436 |
assert adv.service_uuids == [ |
| 437 |
"00001234-0000-1000-8000-00805f9b34fb", |
| 438 |
"550e8400-e29b-41d4-a716-446655440000", |
| 439 |
] |
| 440 |
assert adv.local_name is None |
| 441 |
assert adv.service_data == {} |
| 442 |
assert adv.manufacturer_data == {} |
| 443 |
assert adv.tx_power is None |
| 444 |
|
| 445 |
|
| 446 |
def test_parse_advertisement_data_zero_padded_scan_included(): |
| 447 |
data = [ |
| 448 |
b"\x02\x01\x06\t\xffY\x00\xfe\x024\x9e\xa6\xba\x00\x00\x00\x00\x00\x00\x00\x00" |
| 449 |
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x07\x1b\xc5\xd5\xa5\x02\x00\xb8" |
| 450 |
b'\x9f\xe6\x11M"\x00\r\xa2\xcb\x06\x16\x00\rH\x10\x00\x00\x00\x00\x00\x00\x00' |
| 451 |
] |
| 452 |
|
| 453 |
adv = parse_advertisement_data(data) |
| 454 |
|
| 455 |
assert adv.local_name is None |
| 456 |
assert adv.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 457 |
assert adv.service_data == {"00000d00-0000-1000-8000-00805f9b34fb": b"H\x10\x00"} |
| 458 |
assert adv.manufacturer_data == {89: b"\xfe\x024\x9e\xa6\xba"} |
| 459 |
assert adv.tx_power is None |
| 460 |
|
| 461 |
|
| 462 |
def test_parse_advertisement_data_recovers_from_corrupt_data(): |
| 463 |
data = [ |
| 464 |
b"\x03\x03\x9f\xfe\x17\x16\x9f\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 465 |
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x06\xf8" |
| 466 |
] |
| 467 |
|
| 468 |
adv = parse_advertisement_data(data) |
| 469 |
|
| 470 |
assert adv.local_name is None |
| 471 |
assert adv.service_uuids == ["0000fe9f-0000-1000-8000-00805f9b34fb"] |
| 472 |
assert adv.service_data == { |
| 473 |
"0000fe9f-0000-1000-8000-00805f9b34fb": b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 474 |
} |
| 475 |
assert adv.manufacturer_data == {} |
| 476 |
assert adv.tx_power is None |
| 477 |
|
| 478 |
|
| 479 |
def test_parse_advertisement_data_recovers_from_corrupt_data_2(): |
| 480 |
data = [ |
| 481 |
b"\x1a\xff\xc3\x03_\xef5B\xb0I\x0f\xbb\x00&\x01\x00m*" |
| 482 |
b"\xb2c\xd8\xb0\x02\n\x00\x00\xf2\x02\x01\x06 " |
| 483 |
] |
| 484 |
|
| 485 |
adv = parse_advertisement_data(data) |
| 486 |
|
| 487 |
assert adv.local_name is None |
| 488 |
assert adv.service_uuids == [] |
| 489 |
assert adv.service_data == {} |
| 490 |
assert adv.manufacturer_data == { |
| 491 |
963: b"_\xef5B\xb0I\x0f\xbb\x00&\x01\x00m*\xb2c\xd8\xb0\x02\n\x00\x00\xf2" |
| 492 |
} |
| 493 |
assert adv.tx_power is None |
| 494 |
|
| 495 |
|
| 496 |
def test_parse_advertisement_data_recovers_from_corrupt_data_3(): |
| 497 |
data = [ |
| 498 |
b"\x03\x03\x9f\xfe\x17\x16\x9f\xfe\x00\x00\x00\x00\x00\x00\x00" |
| 499 |
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x06 " |
| 500 |
] |
| 501 |
|
| 502 |
adv = parse_advertisement_data(data) |
| 503 |
|
| 504 |
assert adv.local_name is None |
| 505 |
assert adv.service_uuids == ["0000fe9f-0000-1000-8000-00805f9b34fb"] |
| 506 |
assert adv.service_data == { |
| 507 |
"0000fe9f-0000-1000-8000-00805f9b34fb": b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 508 |
} |
| 509 |
assert adv.manufacturer_data == {} |
| 510 |
assert adv.tx_power is None |
| 511 |
|
| 512 |
|
| 513 |
def test_parse_advertisement_data_recovers_from_corrupt_data_43(): |
| 514 |
data = [ |
| 515 |
b"\x1a\xff\xc3\x03_\xef5B\xb3I\x0f\xbb\x00&\x01\x01m*" |
| 516 |
b"\xb2c\xd8.\x02\n\x00\x00\xf2\x02\x01\xc6\xf8" |
| 517 |
] |
| 518 |
|
| 519 |
adv = parse_advertisement_data(data) |
| 520 |
|
| 521 |
assert adv.local_name is None |
| 522 |
assert adv.service_uuids == [] |
| 523 |
assert adv.service_data == {} |
| 524 |
assert adv.manufacturer_data == { |
| 525 |
963: b"_\xef5B\xb3I\x0f\xbb\x00&\x01\x01m*\xb2c\xd8.\x02\n\x00\x00\xf2" |
| 526 |
} |
| 527 |
assert adv.tx_power is None |
| 528 |
|
| 529 |
|
| 530 |
def test_name_parser(): |
| 531 |
"""Test parsing name from https://github.com/esphome/issues/issues/4838.""" |
| 532 |
|
| 533 |
data = ( |
| 534 |
b"\t\tPineTime\002\001\006\021\007\236\312\334$\016\345\251(340\223\363\243\265\001\000@n", |
| 535 |
) |
| 536 |
|
| 537 |
adv = parse_advertisement_data(data) |
| 538 |
|
| 539 |
assert adv.local_name == "PineTime" |
| 540 |
assert adv.service_uuids == ["01b5a3f3-9330-3433-28a9-e50e24dcca9e"] |
| 541 |
assert adv.service_data == {} |
| 542 |
assert adv.manufacturer_data == {} |
| 543 |
assert adv.tx_power is None |
| 544 |
|
| 545 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 546 |
"PineTime", |
| 547 |
["01b5a3f3-9330-3433-28a9-e50e24dcca9e"], |
| 548 |
{}, |
| 549 |
{}, |
| 550 |
None, |
| 551 |
) |
| 552 |
|
| 553 |
|
| 554 |
def test_invalid_gap_num(): |
| 555 |
"""Test skip invalid gap type.""" |
| 556 |
|
| 557 |
data = ( |
| 558 |
b"\t\x00PineTime\002\001\006\021\007\236\312\334$\016\345\251(340\223\363\243\265\001\000@n", |
| 559 |
) |
| 560 |
|
| 561 |
adv = parse_advertisement_data(data) |
| 562 |
|
| 563 |
assert adv.local_name is None |
| 564 |
assert adv.service_uuids == ["01b5a3f3-9330-3433-28a9-e50e24dcca9e"] |
| 565 |
assert adv.service_data == {} |
| 566 |
assert adv.manufacturer_data == {} |
| 567 |
assert adv.tx_power is None |
| 568 |
|
| 569 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 570 |
None, |
| 571 |
["01b5a3f3-9330-3433-28a9-e50e24dcca9e"], |
| 572 |
{}, |
| 573 |
{}, |
| 574 |
None, |
| 575 |
) |
| 576 |
|
| 577 |
|
| 578 |
def test_out_of_bounds_length(): |
| 579 |
"""Test out of bound length.""" |
| 580 |
|
| 581 |
data = ( |
| 582 |
b"\xff\x00PineTime\002\001\006\021\007\236\312\334$\016\345\251(340\223\363\243\265\001\000@n", |
| 583 |
) |
| 584 |
|
| 585 |
adv = parse_advertisement_data(data) |
| 586 |
|
| 587 |
assert adv.local_name is None |
| 588 |
assert adv.service_uuids == [] |
| 589 |
assert adv.service_data == {} |
| 590 |
assert adv.manufacturer_data == {} |
| 591 |
assert adv.tx_power is None |
| 592 |
|
| 593 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 594 |
None, |
| 595 |
[], |
| 596 |
{}, |
| 597 |
{}, |
| 598 |
None, |
| 599 |
) |
| 600 |
|
| 601 |
|
| 602 |
def test_invalid_ad_debug_log_renders_without_error(caplog): |
| 603 |
"""Malformed AD must produce a renderable debug log (regression: format-string mismatch).""" |
| 604 |
|
| 605 |
|
| 606 |
data = b"\x05\x09\xab\xcd" |
| 607 |
with caplog.at_level("DEBUG", logger="bluetooth_data_tools.gap"): |
| 608 |
adv = parse_advertisement_data((data,)) |
| 609 |
assert adv.local_name is None |
| 610 |
e. |
| 611 |
messages = [r.getMessage() for r in caplog.records] |
| 612 |
assert any("Invalid BLE GAP AD structure" in m for m in messages) |
| 613 |
|
| 614 |
|
| 615 |
def test_invalid_ad_debug_log_reports_structure_offset(caplog): |
| 616 |
"""The logged offset must locate the malformed AD, not the structure after it.""" |
| 617 |
|
| 618 |
|
| 619 |
data = b"\x02\x01\x06\x20\xff\xaa\xbb" |
| 620 |
with caplog.at_level("DEBUG", logger="bluetooth_data_tools.gap"): |
| 621 |
parse_advertisement_data((data,)) |
| 622 |
messages = [r.getMessage() for r in caplog.records] |
| 623 |
assert any("Invalid BLE GAP AD structure at offset 3:" in m for m in messages), ( |
| 624 |
messages |
| 625 |
) |
| 626 |
|
| 627 |
|
| 628 |
def test_out_of_bounds_length_by_one(): |
| 629 |
"""Test out of bound length by one.""" |
| 630 |
|
| 631 |
data = (b"\x01\x08",) |
| 632 |
|
| 633 |
adv = parse_advertisement_data(data) |
| 634 |
|
| 635 |
assert adv.local_name is None |
| 636 |
assert adv.service_uuids == [] |
| 637 |
assert adv.service_data == {} |
| 638 |
assert adv.manufacturer_data == {} |
| 639 |
assert adv.tx_power is None |
| 640 |
|
| 641 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 642 |
None, |
| 643 |
[], |
| 644 |
{}, |
| 645 |
{}, |
| 646 |
None, |
| 647 |
) |
| 648 |
|
| 649 |
|
| 650 |
def test_manufacturer_data_short_by_one(): |
| 651 |
"""Test short manufacturer data.""" |
| 652 |
|
| 653 |
data = (b"\x02\xff\x01",) |
| 654 |
|
| 655 |
adv = parse_advertisement_data(data) |
| 656 |
|
| 657 |
assert adv.local_name is None |
| 658 |
assert adv.service_uuids == [] |
| 659 |
assert adv.service_data == {} |
| 660 |
assert adv.manufacturer_data == {} |
| 661 |
assert adv.tx_power is None |
| 662 |
|
| 663 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 664 |
None, |
| 665 |
[], |
| 666 |
{}, |
| 667 |
{}, |
| 668 |
None, |
| 669 |
) |
| 670 |
|
| 671 |
|
| 672 |
def test_manufacturer_data_short_by_two(): |
| 673 |
"""Test short manufacturer data.""" |
| 674 |
|
| 675 |
data = (b"\x02\xff\x01\x01",) |
| 676 |
|
| 677 |
adv = parse_advertisement_data(data) |
| 678 |
|
| 679 |
assert adv.local_name is None |
| 680 |
assert adv.service_uuids == [] |
| 681 |
assert adv.service_data == {} |
| 682 |
assert adv.manufacturer_data == {} |
| 683 |
assert adv.tx_power is None |
| 684 |
|
| 685 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 686 |
None, |
| 687 |
[], |
| 688 |
{}, |
| 689 |
{}, |
| 690 |
None, |
| 691 |
) |
| 692 |
|
| 693 |
|
| 694 |
def test_manufacturer_data_short_by_three(): |
| 695 |
"""Test manufacturer data with minimum size (company ID only, no payload). |
| 696 |
|
| 697 |
This is now valid after fixing issue #179 - manufacturer data with just |
| 698 |
a company ID and empty payload is accepted, consistent with service data. |
| 699 |
""" |
| 700 |
|
| 701 |
data = (b"\x03\xff\x01\x01",) |
| 702 |
|
| 703 |
adv = parse_advertisement_data(data) |
| 704 |
|
| 705 |
assert adv.local_name is None |
| 706 |
assert adv.service_uuids == [] |
| 707 |
assert adv.service_data == {} |
| 708 |
assert adv.manufacturer_data == {257: b""} |
| 709 |
assert adv.tx_power is None |
| 710 |
|
| 711 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 712 |
None, |
| 713 |
[], |
| 714 |
{}, |
| 715 |
{257: b""}, |
| 716 |
None, |
| 717 |
) |
| 718 |
|
| 719 |
|
| 720 |
def test_manufacturer_data_short_by_four(): |
| 721 |
"""Test short manufacturer data. |
| 722 |
|
| 723 |
Manufacturer data claims length of 4 but only has 3 bytes available |
| 724 |
(type + company ID, no payload bytes when 1 payload byte is claimed). |
| 725 |
""" |
| 726 |
|
| 727 |
data = (b"\x04\xff\x01\x01",) |
| 728 |
|
| 729 |
adv = parse_advertisement_data(data) |
| 730 |
|
| 731 |
assert adv.local_name is None |
| 732 |
assert adv.service_uuids == [] |
| 733 |
assert adv.service_data == {} |
| 734 |
assert adv.manufacturer_data == {} |
| 735 |
assert adv.tx_power is None |
| 736 |
|
| 737 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 738 |
None, |
| 739 |
[], |
| 740 |
{}, |
| 741 |
{}, |
| 742 |
None, |
| 743 |
) |
| 744 |
|
| 745 |
|
| 746 |
def test_manufacturer_data_short_by_five(): |
| 747 |
"""Test short manufacturer data.""" |
| 748 |
|
| 749 |
data = (b"\x05\xff\x01\x01\x01",) |
| 750 |
|
| 751 |
adv = parse_advertisement_data(data) |
| 752 |
|
| 753 |
assert adv.local_name is None |
| 754 |
assert adv.service_uuids == [] |
| 755 |
assert adv.service_data == {} |
| 756 |
assert adv.manufacturer_data == {} |
| 757 |
assert adv.tx_power is None |
| 758 |
|
| 759 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 760 |
None, |
| 761 |
[], |
| 762 |
{}, |
| 763 |
{}, |
| 764 |
None, |
| 765 |
) |
| 766 |
|
| 767 |
|
| 768 |
def test_manufacturer_data_single_byte(): |
| 769 |
"""Test single byte manufacturer data.""" |
| 770 |
|
| 771 |
data = (b"\x04\xff\x01\x01\x01",) |
| 772 |
|
| 773 |
adv = parse_advertisement_data(data) |
| 774 |
|
| 775 |
assert adv.local_name is None |
| 776 |
assert adv.service_uuids == [] |
| 777 |
assert adv.service_data == {} |
| 778 |
assert adv.manufacturer_data == {257: b"\x01"} |
| 779 |
assert adv.tx_power is None |
| 780 |
|
| 781 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 782 |
None, |
| 783 |
[], |
| 784 |
{}, |
| 785 |
{257: b"\x01"}, |
| 786 |
None, |
| 787 |
) |
| 788 |
|
| 789 |
|
| 790 |
def test_service_data_short(): |
| 791 |
"""Test short service data.""" |
| 792 |
|
| 793 |
data = (b"\x02\x16\x01",) |
| 794 |
|
| 795 |
adv = parse_advertisement_data(data) |
| 796 |
|
| 797 |
assert adv.local_name is None |
| 798 |
assert adv.service_uuids == [] |
| 799 |
assert adv.service_data == {} |
| 800 |
assert adv.manufacturer_data == {} |
| 801 |
assert adv.tx_power is None |
| 802 |
|
| 803 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 804 |
None, |
| 805 |
[], |
| 806 |
{}, |
| 807 |
{}, |
| 808 |
None, |
| 809 |
) |
| 810 |
|
| 811 |
|
| 812 |
def test_32bit_uuid_short(): |
| 813 |
"""Test short 32bit uuid data.""" |
| 814 |
|
| 815 |
data = (b"\x02 \x01",) |
| 816 |
|
| 817 |
adv = parse_advertisement_data(data) |
| 818 |
|
| 819 |
assert adv.local_name is None |
| 820 |
assert adv.service_uuids == [] |
| 821 |
assert adv.service_data == {} |
| 822 |
assert adv.manufacturer_data == {} |
| 823 |
assert adv.tx_power is None |
| 824 |
|
| 825 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 826 |
None, |
| 827 |
[], |
| 828 |
{}, |
| 829 |
{}, |
| 830 |
None, |
| 831 |
) |
| 832 |
|
| 833 |
|
| 834 |
def test_128bit_uuid_short(): |
| 835 |
"""Test short 128bit uuid data.""" |
| 836 |
|
| 837 |
data = (b"\x02!\x01",) |
| 838 |
|
| 839 |
adv = parse_advertisement_data(data) |
| 840 |
|
| 841 |
assert adv.local_name is None |
| 842 |
assert adv.service_uuids == [] |
| 843 |
assert adv.service_data == {} |
| 844 |
assert adv.manufacturer_data == {} |
| 845 |
assert adv.tx_power is None |
| 846 |
|
| 847 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 848 |
None, |
| 849 |
[], |
| 850 |
{}, |
| 851 |
{}, |
| 852 |
None, |
| 853 |
) |
| 854 |
|
| 855 |
|
| 856 |
def test_zero_padded_end(): |
| 857 |
"""Test name with zero padded end.""" |
| 858 |
|
| 859 |
data = (b"\x02\x08a\x00",) |
| 860 |
|
| 861 |
adv = parse_advertisement_data(data) |
| 862 |
|
| 863 |
assert adv.local_name == "a" |
| 864 |
assert adv.service_uuids == [] |
| 865 |
assert adv.service_data == {} |
| 866 |
assert adv.manufacturer_data == {} |
| 867 |
assert adv.tx_power is None |
| 868 |
|
| 869 |
assert parse_advertisement_data_tuple(tuple(data)) == ( |
| 870 |
"a", |
| 871 |
[], |
| 872 |
{}, |
| 873 |
{}, |
| 874 |
None, |
| 875 |
) |
| 876 |
|
| 877 |
|
| 878 |
def test_zero_padded_out_of_bounds_length(): |
| 879 |
"""Test zero padded out of bound length.""" |
| 880 |
|
| 881 |
data = ( |
| 882 |
b"\x00\xff\x00PineTime\002\001\006\021\007\236\312\334$\016\345\251(340\223\363\243\265\001\000@n", |
| 883 |
) |
| 884 |
|
| 885 |
adv = parse_advertisement_data(data) |
| 886 |
|
| 887 |
assert adv.local_name is None |
| 888 |
assert adv.service_uuids == [] |
| 889 |
assert adv.service_data == {} |
| 890 |
assert adv.manufacturer_data == {} |
| 891 |
assert adv.tx_power is None |
| 892 |
|
| 893 |
assert parse_advertisement_data_tuple( |
| 894 |
tuple( |
| 895 |
data, |
| 896 |
) |
| 897 |
) == ( |
| 898 |
None, |
| 899 |
[], |
| 900 |
{}, |
| 901 |
{}, |
| 902 |
None, |
| 903 |
) |
| 904 |
|
| 905 |
|
| 906 |
def test_data_shorter_than_length() -> None: |
| 907 |
"""Test data shorter than length.""" |
| 908 |
|
| 909 |
for len in range(30): |
| 910 |
data = bytes((len,)) + b"!\x01" |
| 911 |
|
| 912 |
adv = parse_advertisement_data((data,)) |
| 913 |
|
| 914 |
assert adv.local_name is None |
| 915 |
assert adv.service_uuids == [] |
| 916 |
assert adv.service_data == {} |
| 917 |
assert adv.manufacturer_data == {} |
| 918 |
assert adv.tx_power is None |
| 919 |
|
| 920 |
assert parse_advertisement_data_tuple((data,)) == ( |
| 921 |
None, |
| 922 |
[], |
| 923 |
{}, |
| 924 |
{}, |
| 925 |
None, |
| 926 |
) |
| 927 |
|
| 928 |
|
| 929 |
@pytest.mark.parametrize( |
| 930 |
"data", |
| 931 |
[ |
| 932 |
(b"\x02 a\xc4|\x04@*'\x9c\xa5C\r\xa1\xe6\x1e\xe7\x8f\xa57D\xe6$\x03",), |
| 933 |
( |
| 934 |
b"\xe8\x9d\x83\x8b\x96\x85\x07U\x19$&\x1c\x91\x80\xba\x04Z", |
| 935 |
b'"EU\xcb\x11\xeb\xdc\xd4)\xc9\x1b\x8c\xfe\x1e\xd7\xca\x98\xfe\xcd%\xf6>\xbb\xe3\xbc\xfc5', |
| 936 |
b"(\x86\x18\xe9m>\x89\xa7\xe1=\x9fE\x0f\xbcE\xb2<", |
| 937 |
), |
| 938 |
( |
| 939 |
b"\xf4\x9e\x0f!\x93n\xae\x1e\x89\xc2\xb4\x97\x98\xdc~\x9a\xb3\x06\x7f\x11", |
| 940 |
b"\x89\xc2\x07\xff\xe7QEG&;\x06\xab\xebN\xf5\xc4g\xfe", |
| 941 |
b"\xeb\x852\x0b,KB", |
| 942 |
), |
| 943 |
( |
| 944 |
b'\xcbu\xa5\xe4\xa1\xdd`c\x88\xa9\xe2\xaah\xe5"%X\x87\xf0', |
| 945 |
b'\xac;\x8f\xc43!\xb0?"\xb8\n<', |
| 946 |
b"\x10nyT\xed\x96\x07\x9a", |
| 947 |
), |
| 948 |
( |
| 949 |
b"\r\xd6'\xder\xfc@T\xb3Q\x12EH\x9e\xe9\xf8x\xe3\xcc\ti\xfb", |
| 950 |
b"\xfe@\x89", |
| 951 |
b"Gt\xc3_\xf9\x8e\xdd#", |
| 952 |
), |
| 953 |
( |
| 954 |
b'\x17b\x02c\xbe%"\x06\xfe\x05c4\x04\x97\x03)|\xd0\xf4\xeb~\xb1gI', |
| 955 |
b"r\xc0\x9a>^o\xcb\xdb\xc2r\xca\x1a'VL\xac\xd7\x8e\xa7\xeb\xc2:\xe2_\x96\xe31r", |
| 956 |
b"\\^\xe6\x96]%\x17\x02\xa5\xdcAE\x92\xb2\x1c\x1a\xd4Y\x07\xddZ\xebC\x12B\xb6\xcb\x8a", |
| 957 |
), |
| 958 |
( |
| 959 |
b"\x1b\x87c\xb7\x12nK\xf3_\xb4\x18)x<\xe2\x05\xc8\xdb\xf8\x1b\xf7 \xe6\x99\xe3Z\xd1C\x88\xda\x98", |
| 960 |
b"\xbb\xbe@,\xa6\xdcj*\x85u\xe3\xc4", |
| 961 |
b"w\xb7\xeb\xb0", |
| 962 |
), |
| 963 |
( |
| 964 |
b"\x88\x83\x7fN\xd0E\xa6\xc5(h\xcd\xe7\x80\\\xf0\xe6n:\x10K\x02\x92", |
| 965 |
b"\xc5\xe6`a\xf7\xf5 \x1eV\xd6]8$\xf0\xf0", |
| 966 |
b"\x01rS\xf1\xe2ht\xd6\x8f\xc6\x0e\xd0~", |
| 967 |
), |
| 968 |
( |
| 969 |
b"\x10\xa6\xc4}\xb6\xc9n\x00", |
| 970 |
b"\xa8\xd8#'@\xde\x9a\xf6;&\x88\x0f\xc1m\xe5v\xa3\x17O\x8b\x92\x94\\\x91\x8b\xee\xa1\xed", |
| 971 |
b'\xccA\x9c\x01;\xd1\xe8e\xeb\xf3\x8b\xa1\xd6\xe5P]\xc7"_@\x10\x1e\x8b\x90o\r\xd9', |
| 972 |
), |
| 973 |
], |
| 974 |
) |
| 975 |
def test_negative_splice_pos_does_not_crash(data: tuple[bytes, bytes, bytes]) -> None: |
| 976 |
"""Test data that would cause a negative splice position. |
| 977 |
|
| 978 |
All of the data here will result in a negative splice position |
| 979 |
when attempting to parse the advertisement data. This is a |
| 980 |
regression test to ensure that the parser does not crash |
| 981 |
when it encounters this data. |
| 982 |
|
| 983 |
A negative splice position is caused by the parser |
| 984 |
attempting to splice the data at a position that is |
| 985 |
greater than the length of the data. This can happen |
| 986 |
when the data is malformed or when the parser is |
| 987 |
attempting to parse data that is not in the expected |
| 988 |
format. The parser should be able to handle this |
| 989 |
gracefully and return an empty advertisement object |
| 990 |
without crashing. |
| 991 |
""" |
| 992 |
|
| 993 |
adv = parse_advertisement_data(data) |
| 994 |
|
| 995 |
assert adv.local_name is None |
| 996 |
assert adv.service_uuids == [] |
| 997 |
assert adv.service_data == {} |
| 998 |
assert adv.manufacturer_data == {} |
| 999 |
assert adv.tx_power is None |
| 1000 |
|
| 1001 |
assert parse_advertisement_data_tuple( |
| 1002 |
tuple( |
| 1003 |
data, |
| 1004 |
) |
| 1005 |
) == ( |
| 1006 |
None, |
| 1007 |
[], |
| 1008 |
{}, |
| 1009 |
{}, |
| 1010 |
None, |
| 1011 |
) |
| 1012 |
|
| 1013 |
|
| 1014 |
def test_parse_advertisement_with_empty_service_data(): |
| 1015 |
"""Test parsing advertisement with empty service data payload (issue #179). |
| 1016 |
|
| 1017 |
This tests the case where service data contains only a UUID with no payload bytes. |
| 1018 |
The parser should accept this as valid empty service data and continue parsing |
| 1019 |
subsequent advertisement structures. |
| 1020 |
""" |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
data = bytes.fromhex( |
| 1028 |
"02010603160a180303faff07ff0001509040a210094b5431323230302d422d3030313030" |
| 1029 |
) |
| 1030 |
|
| 1031 |
adv = parse_advertisement_data((data,)) |
| 1032 |
|
| 1033 |
assert adv.local_name == "KT12200-B-00100" |
| 1034 |
assert adv.service_uuids == ["0000fffa-0000-1000-8000-00805f9b34fb"] |
| 1035 |
assert adv.service_data == {"0000180a-0000-1000-8000-00805f9b34fb": b""} |
| 1036 |
assert adv.manufacturer_data == {256: b"\x50\x90\x40\xa2"} |
| 1037 |
assert adv.tx_power is None |
| 1038 |
|
| 1039 |
|
| 1040 |
def test_parse_advertisement_data_multiple_128bit_uuids(): |
| 1041 |
"""Two 128-bit UUIDs packed into a single AD struct must both be returned. |
| 1042 |
|
| 1043 |
Per Core Spec Vol 3 Part C §11, the Complete/Incomplete List of 128-bit |
| 1044 |
Service UUIDs AD types carry a list (length = 1 + 16N), not a single UUID. |
| 1045 |
Larger packets (scan response / extended advertising) can carry more than one. |
| 1046 |
""" |
| 1047 |
uuid1 = bytes.fromhex("00112233445566778899aabbccddeeff") |
| 1048 |
uuid2 = bytes.fromhex("0f1e2d3c4b5a69788796a5b4c3d2e1f0") |
| 1049 |
|
| 1050 |
data = b"\x21\x07" + uuid1 + uuid2 |
| 1051 |
|
| 1052 |
adv = parse_advertisement_data((data,)) |
| 1053 |
|
| 1054 |
assert adv.local_name is None |
| 1055 |
assert adv.service_uuids == [ |
| 1056 |
rm |
| 1057 |
"ffeeddcc-bbaa-9988-7766-554433221100", |
| 1058 |
"f0e1d2c3-b4a5-9687-7869-5a4b3c2d1e0f", |
| 1059 |
] |
| 1060 |
assert adv.service_data == {} |
| 1061 |
assert adv.manufacturer_data == {} |
| 1062 |
assert adv.tx_power is None |
| 1063 |
|
| 1064 |
|
| 1065 |
def test_parse_advertisement_data_128bit_uuid_malformed_length(): |
| 1066 |
"""Malformed 128-bit UUID payloads (length not 1 + 16N) must be skipped. |
| 1067 |
|
| 1068 |
Previously the parser passed the truncated/excess bytes straight to the |
| 1069 |
UUID formatter, producing bogus UUID strings (all zeros for short, a |
| 1070 |
64-hex-char garbage string for double-length). |
| 1071 |
""" |
| 1072 |
D" |
| 1073 |
short = b"\x0a\x07" + bytes(9) |
| 1074 |
adv = parse_advertisement_data((short,)) |
| 1075 |
assert adv.service_uuids == [] |
| 1076 |
|
| 1077 |
us |
| 1078 |
|
| 1079 |
one_and_a_half = ( |
| 1080 |
b"\x12\x07" + bytes.fromhex("00112233445566778899aabbccddeeff") + b"\x42" |
| 1081 |
) |
| 1082 |
adv = parse_advertisement_data((one_and_a_half,)) |
| 1083 |
assert adv.service_uuids == ["ffeeddcc-bbaa-9988-7766-554433221100"] |
| 1084 |
|
| 1085 |
|
| 1086 |
def test_parse_advertisement_data_tx_power_single_byte(): |
| 1087 |
"""Per BLE Core Spec Vol 3 Part C §11, TX Power Level is exactly 1 signed octet.""" |
| 1088 |
|
| 1089 |
data = (b"\x02\x0a\xce",) |
| 1090 |
adv = parse_advertisement_data(data) |
| 1091 |
assert adv.tx_power == -50 |
| 1092 |
|
| 1093 |
|
| 1094 |
def test_parse_advertisement_data_tx_power_multibyte_rejected(): |
| 1095 |
"""Malformed multi-byte TX Power must be skipped, not folded into a multi-byte signed int. |
| 1096 |
|
| 1097 |
The previous behaviour read the entire (variable-length) payload as a |
| 1098 |
little-endian signed integer, producing values like -32768 for what |
| 1099 |
should be invalid input. |
| 1100 |
""" |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
data = (b"\x03\x0a\x00\x80",) |
| 1105 |
adv = parse_advertisement_data(data) |
| 1106 |
assert adv.tx_power is None |
| 1107 |
|
| 1108 |
|
| 1109 |
def test_parse_advertisement_data_16bit_uuid_malformed_length(): |
| 1110 |
"""16-bit UUID list with a trailing odd byte must drop the remainder, not |
| 1111 |
emit a UUID built from a 1-byte slice. |
| 1112 |
|
| 1113 |
Mirrors the 128-bit malformed-length protection (PR #226) for the |
| 1114 |
16-bit list branch. |
| 1115 |
""" |
| 1116 |
a |
| 1117 |
|
| 1118 |
payload = b"\x06\x03\xaa\xbb\xcc\xdd\xee" |
| 1119 |
adv = parse_advertisement_data((payload,)) |
| 1120 |
assert adv.service_uuids == [ |
| 1121 |
"0000bbaa-0000-1000-8000-00805f9b34fb", |
| 1122 |
"0000ddcc-0000-1000-8000-00805f9b34fb", |
| 1123 |
] |
| 1124 |
|
| 1125 |
ng |
| 1126 |
|
| 1127 |
one_and_a_half = b"\x04\x02\x11\x22\x33" |
| 1128 |
adv = parse_advertisement_data((one_and_a_half,)) |
| 1129 |
assert adv.service_uuids == ["00002211-0000-1000-8000-00805f9b34fb"] |
| 1130 |
|
| 1131 |
|
| 1132 |
def test_parse_advertisement_data_32bit_uuid_malformed_length(): |
| 1133 |
"""32-bit UUID list with a tail < 4 bytes must drop the remainder. |
| 1134 |
|
| 1135 |
Mirrors the 128-bit malformed-length protection (PR #226) for the |
| 1136 |
32-bit list branch. |
| 1137 |
""" |
| 1138 |
ID |
| 1139 |
|
| 1140 |
payload = b"\x08\x05\x11\x22\x33\x44\xaa\xbb\xcc" |
| 1141 |
adv = parse_advertisement_data((payload,)) |
| 1142 |
assert adv.service_uuids == ["44332211-0000-1000-8000-00805f9b34fb"] |
| 1143 |
|
| 1144 |
ng |
| 1145 |
|
| 1146 |
one_and_a_quarter = b"\x06\x04\xde\xad\xbe\xef\x99" |
| 1147 |
adv = parse_advertisement_data((one_and_a_quarter,)) |
| 1148 |
assert adv.service_uuids == ["efbeadde-0000-1000-8000-00805f9b34fb"] |
| 1149 |
|
| 1150 |
|
| 1151 |
def test_parse_advertisement_data_32bit_uuid_high_bit_set(): |
| 1152 |
"""32-bit UUIDs with bit 31 set must decode as the unsigned value. |
| 1153 |
|
| 1154 |
Guards the signed-shift UB fix: ``gap_data[i + 3] << 24`` on an |
| 1155 |
``unsigned char`` promotes to (signed) ``int`` in C, so values >= 0x80 |
| 1156 |
in the top byte would be undefined behavior under Cython without the |
| 1157 |
intermediate ``unsigned int`` staging. The decoded UUID must reflect |
| 1158 |
the full unsigned 32-bit value, not a sign-extended negative. |
| 1159 |
""" |
| 1160 |
Ds |
| 1161 |
|
| 1162 |
payload = b"\x09\x05\x33\x22\x11\xff\x01\x00\x00\x80" |
| 1163 |
adv = parse_advertisement_data((payload,)) |
| 1164 |
assert adv.service_uuids == [ |
| 1165 |
"ff112233-0000-1000-8000-00805f9b34fb", |
| 1166 |
"80000001-0000-1000-8000-00805f9b34fb", |
| 1167 |
] |
| 1168 |
|
| 1169 |
|
| 1170 |
def test_parse_advertisement_data_32bit_service_data_high_bit_set(): |
| 1171 |
"""32-bit service-data UUID with bit 31 set must key on the unsigned value. |
| 1172 |
|
| 1173 |
Same UB guard as the list branch, applied to the |
| 1174 |
``TYPE_SERVICE_DATA`` 32-bit decode. |
| 1175 |
""" |
| 1176 |
|
| 1177 |
|
| 1178 |
payload = b"\x07\x20\x33\x22\x11\xff\xaa\xbb" |
| 1179 |
adv = parse_advertisement_data((payload,)) |
| 1180 |
assert adv.service_data == { |
| 1181 |
"ff112233-0000-1000-8000-00805f9b34fb": b"\xaa\xbb", |
| 1182 |
} |
| 1183 |
|
| 1184 |
|
| 1185 |
def test_parse_advertisement_data_multiple_manufacturer_entries(): |
| 1186 |
"""Two manufacturer-specific-data AD structs in one packet must both |
| 1187 |
survive into the resulting dict, keyed by company ID. |
| 1188 |
|
| 1189 |
BLE Core Spec Vol 3 Part C §11 permits the Manufacturer Specific Data |
| 1190 |
AD type to appear more than once in a single advertisement. |
| 1191 |
""" |
| 1192 |
|
| 1193 |
|
| 1194 |
payload = b"\x05\xff\x4c\x00\x01\x02" + b"\x06\xff\x59\x00\x03\x04\x05" |
| 1195 |
adv = parse_advertisement_data((payload,)) |
| 1196 |
assert adv.manufacturer_data == { |
| 1197 |
0x004C: b"\x01\x02", |
| 1198 |
0x0059: b"\x03\x04\x05", |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
def test_parse_advertisement_data_multi_chunk_tuple_matches_bytes(): |
| 1203 |
"""A multi-chunk tuple input must produce the same result as the joined bytes. |
| 1204 |
|
| 1205 |
This pins the parse_advertisement_data tuple fast-path that caches on the |
| 1206 |
tuple itself rather than on the joined bytes. |
| 1207 |
""" |
| 1208 |
chunks = ( |
| 1209 |
b"\x02\x01\x06\x03\x03\x12\x18", |
| 1210 |
b"\x10\tLOOKin_98F33163\x03\x19\xc1\x03", |
| 1211 |
) |
| 1212 |
joined = b"".join(chunks) |
| 1213 |
|
| 1214 |
from_tuple = parse_advertisement_data(chunks) |
| 1215 |
from_bytes = parse_advertisement_data([joined]) |
| 1216 |
|
| 1217 |
assert from_tuple.local_name == from_bytes.local_name == "LOOKin_98F33163" |
| 1218 |
assert from_tuple.service_uuids == from_bytes.service_uuids |
| 1219 |
assert from_tuple.service_data == from_bytes.service_data |
| 1220 |
assert from_tuple.manufacturer_data == from_bytes.manufacturer_data |
| 1221 |
assert from_tuple.tx_power == from_bytes.tx_power |
| 1222 |
|
| 1223 |
|
| 1224 |
def test_parse_advertisement_data_single_element_tuple_unwraps_to_bytes_cache(): |
| 1225 |
"""A 1-element tuple skips the join and uses the bytes-keyed cache directly.""" |
| 1226 |
payload = b"\x02\x01\x06\x05\tRZSS" |
| 1227 |
from_tuple = parse_advertisement_data((payload,)) |
| 1228 |
from_list = parse_advertisement_data([payload]) |
| 1229 |
assert from_tuple.local_name == from_list.local_name == "RZSS" |
| 1230 |
|
| 1231 |
|
| 1232 |
def test_parse_advertisement_data_tuple_miss_falls_through_bytes_cache(): |
| 1233 |
"""A tuple-cache miss must consult the bytes-keyed cache before paying for |
| 1234 |
a full parse, so identical content arriving via a fresh tuple identity |
| 1235 |
still benefits from a previous bytes-form call. |
| 1236 |
""" |
| 1237 |
payload = b"\x02\x01\x06\x05\tRZSS" |
| 1238 |
|
| 1239 |
|
| 1240 |
parse_advertisement_data_tuple.cache_clear() |
| 1241 |
expected = parse_advertisement_data_bytes(payload) |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
assert parse_advertisement_data_tuple((payload,)) == expected |
| 1246 |
|
| 1247 |
assert parse_advertisement_data_tuple((b"", payload)) == expected |