| 1 |
"""Tests for the parser of BLE advertisements in BTHome V2 format.""" |
| 2 |
|
| 3 |
import binascii |
| 4 |
|
| 5 |
from bthome_ble.bthome_v2_encryption import decrypt_aes_ccm, encrypt_payload |
| 6 |
|
| 7 |
|
| 8 |
def test_encryption_example_1(): |
| 9 |
"""Test BTHome V2 encryption example.""" |
| 10 |
data = bytes(bytearray.fromhex("02CA0903BF13")) |
| 11 |
count_id = bytes(bytearray.fromhex("00112233")) |
| 12 |
mac = binascii.unhexlify("5448E68F80A5") |
| 13 |
uuid16 = b"\xd2\xfc" |
| 14 |
sw_version = b"\x41" |
| 15 |
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932") |
| 16 |
|
| 17 |
encrypted_payload = encrypt_payload( |
| 18 |
data=data, |
| 19 |
mac=mac, |
| 20 |
uuid16=uuid16, |
| 21 |
sw_version=sw_version, |
| 22 |
count_id=count_id, |
| 23 |
key=bindkey, |
| 24 |
) |
| 25 |
assert ( |
| 26 |
encrypted_payload |
| 27 |
== b"\xd2\xfc\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14" |
| 28 |
) |
| 29 |
assert decrypt_aes_ccm(key=bindkey, mac=mac, data=encrypted_payload) == { |
| 30 |
"humidity": 50.55, |
| 31 |
"temperature": 25.06, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
def test_encryption_example_2(): |
| 36 |
"""Test BTHome V2 encryption example.""" |
| 37 |
data = bytes(bytearray.fromhex("2101")) |
| 38 |
count_id = bytes(bytearray.fromhex("00112233")) |
| 39 |
mac = binascii.unhexlify("5448E68F80A5") |
| 40 |
uuid16 = b"\xd2\xfc" |
| 41 |
sw_version = b"\x41" |
| 42 |
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932") |
| 43 |
|
| 44 |
encrypted_payload = encrypt_payload( |
| 45 |
data=data, |
| 46 |
mac=mac, |
| 47 |
uuid16=uuid16, |
| 48 |
sw_version=sw_version, |
| 49 |
count_id=count_id, |
| 50 |
key=bindkey, |
| 51 |
) |
| 52 |
print(encrypted_payload.hex()) |
| 53 |
assert encrypted_payload == b"\xd2\xfc\x41\x87\xb9\x00\x11\x22\x33\x72\xb0\x23\x1f" |
| 54 |
assert decrypt_aes_ccm(key=bindkey, mac=mac, data=encrypted_payload) == { |
| 55 |
"Motion": 1, |
| 56 |
} |