1% iBeacon unit tests 2# 3# Type the following command to launch start the tests: 4# $ test/run_tests -P "load_contrib('ibeacon')" -t test/contrib/ibeacon.uts 5 6+ iBeacon tests 7 8= Presence check 9 10Apple_BLE_Frame 11IBeacon_Data 12Apple_BLE_Submessage 13 14= Apple multiple submessages 15 16# Observed in the wild; handoff + nearby message. 17# Meaning unknown. 18d = hex_bytes('D6BE898E4024320CFB574D5A02011A1AFF4C000C0E009C6B8F40440F1583EC895148B410050318C0B525B8F7D4') 19p = BTLE(d) 20 21assert len(p[Apple_BLE_Frame].plist) == 2 22assert p[Apple_BLE_Frame].plist[0].subtype == 0x0c # handoff 23assert (raw(p[Apple_BLE_Frame].plist[0].payload) == 24 hex_bytes('009c6b8f40440f1583ec895148b4')) 25assert p[Apple_BLE_Frame].plist[1].subtype == 0x10 # nearby 26assert raw(p[Apple_BLE_Frame].plist[1].payload) == hex_bytes('0318c0b525') 27 28= iBeacon (decode LE Set Advertising Data) 29 30# from https://en.wikipedia.org/wiki/IBeacon#Technical_details 31d = hex_bytes('1E02011A1AFF4C000215FB0B57A2822844CD913A94A122BA120600010002D100') 32p = HCI_Cmd_LE_Set_Advertising_Data(d) 33 34assert len(p[Apple_BLE_Frame].plist) == 1 35assert p[IBeacon_Data].uuid == UUID("fb0b57a2-8228-44cd-913a-94a122ba1206") 36assert p[IBeacon_Data].major == 1 37assert p[IBeacon_Data].minor == 2 38assert p[IBeacon_Data].tx_power == -47 39 40d2 = raw(p) 41assert d == d2 42 43= iBeacon (encode LE Set Advertising Data) 44 45d = hex_bytes('1E0201061AFF4C000215FB0B57A2822844CD913A94A122BA120600010002D100') 46p = Apple_BLE_Submessage()/IBeacon_Data( 47 uuid='fb0b57a2-8228-44cd-913a-94a122ba1206', 48 major=1, minor=2, tx_power=-47) 49 50sap = p.build_set_advertising_data()[HCI_Cmd_LE_Set_Advertising_Data] 51assert d == raw(sap) 52 53pa = Apple_BLE_Frame(plist=[p]) 54sapa = pa.build_set_advertising_data()[HCI_Cmd_LE_Set_Advertising_Data] 55assert d == raw(sapa) 56 57# Also try to build with Submessage directly 58sapa = p.build_set_advertising_data()[HCI_Cmd_LE_Set_Advertising_Data] 59assert d == raw(sapa) 60 61= iBeacon (decode advertising frame) 62 63# from https://en.wikipedia.org/wiki/IBeacon#Spoofing 64d = hex_bytes('043E2A02010001FCED16D4EED61E0201061AFF4C000215B9407F30F5F8466EAFF925556B57FE6DEDFCD416B6B4') 65p = HCI_Hdr(d) 66 67assert p[HCI_LE_Meta_Advertising_Report].addr == 'd6:ee:d4:16:ed:fc' 68assert len(p[Apple_BLE_Frame].plist) == 1 69assert p[IBeacon_Data].uuid == UUID('b9407f30-f5f8-466e-aff9-25556b57fe6d') 70 71+ Overflow area 72 73= Basic overflow area packet 74 75d = hex_bytes('14ff4c000100000000000000000000000000000080') 76p = EIR_Hdr(d) 77 78assert raw(p) == d 79assert len(p[Apple_BLE_Frame].plist) == 1 80assert p[Apple_BLE_Submessage].subtype == 0x01 81assert p[Apple_BLE_Submessage].len == None 82 83payload = p[Apple_BLE_Submessage].payload 84assert isinstance(payload, Raw) 85assert raw(payload) == hex_bytes('00000000000000000000000000000080') 86