def test_pack_ptr(): assert pack(["a", "a"]) == b"\xD2\x41\x61\xA0" assert (pack(["foo", "bar", "foo", "bar"]) == b"\xD4\x43\x66\x6F\x6F\x43\x62\x61\x72\xA0\xA1") assert (pack({ "a": "b", "c": { "d": "a" }, "d": True }) == b"\xE3\x41\x61\x41\x62\x41\x63\xE1\x41\x64\xA0\xA3\x01")
async def finish_pairing(self, pin: int) -> HapCredentials: """Finish pairing process.""" self.srp.step1(pin) pub_key, proof = self.srp.step2(self._atv_pub_key, self._atv_salt) resp = await self.protocol.exchange_opack( FrameType.PS_Next, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x03", TlvValue.PublicKey: pub_key, TlvValue.Proof: proof, }), "_pwTy": 1, }, ) pairing_data = _get_pairing_data(resp) atv_proof = pairing_data[TlvValue.Proof] log_binary(_LOGGER, "Device", Proof=atv_proof) # TODO: Dummy data: what to set? needed at all? additional_data = { "altIRK": b"-\x54\xe0\x7a\x88*en\x11\xab\x82v-'%\xc5", "accountID": "DC6A7CB6-CA1A-4BF4-880D-A61B717814DB", "model": "AppleTV6,2", "wifiMAC": b"@\xff\xa1\x8f\xa1\xb9", "name": "Living Room", "mac": b"@\xc4\xff\x8f\xb1\x99", } encrypted_data = self.srp.step3( additional_data={17: opack.pack(additional_data)}) resp = await self.protocol.exchange_opack( FrameType.PS_Next, { PAIRING_DATA_KEY: hap_tlv8.write_tlv({ TlvValue.SeqNo: b"\x05", TlvValue.EncryptedData: encrypted_data, }), "_pwTy": 1, }, ) pairing_data = _get_pairing_data(resp) encrypted_data = pairing_data[TlvValue.EncryptedData] return self.srp.step4(encrypted_data)
def test_golden(): data = { "_i": "_systemInfo", "_x": 1254122577, "_btHP": False, "_c": { "_pubID": "AA:BB:CC:DD:EE:FF", "_sv": "230.1", "_bf": 0, "_siriInfo": { "collectorElectionVersion": 1.0, "deviceCapabilities": { "seymourEnabled": 1, "voiceTriggerEnabled": 2 }, "sharedDataProtoBuf": 512 * b"\x08", }, "_stA": [ "com.apple.LiveAudio", "com.apple.siri.wakeup", "com.apple.Seymour", "com.apple.announce", "com.apple.coreduet.sync", "com.apple.SeymourSession", ], "_i": "6c62fca18b11", "_clFl": 128, "_idsID": "44E14ABC-DDDD-4188-B661-11BAAAF6ECDE", "_hkUID": [UUID("17ed160a-81f8-4488-962c-6b1a83eb0081")], "_dC": "1", "_sf": 256, "model": "iPhone10,6", "name": "iPhone", }, "_t": 2, } packed = pack(data) unpacked = unpack(packed) assert DeepDiff(unpacked, data, ignore_order=True)
async def exchange_opack( self, frame_type: FrameType, data: object, timeout: int = DEFAULT_TIMEOUT) -> Dict[str, object]: """Send data as OPACK and decode result as OPACK.""" _LOGGER.debug("Send OPACK: %s", data) _, payload = await self.connection.exchange(frame_type, opack.pack(data), timeout) unpacked_object, _ = opack.unpack(payload) _LOGGER.debug("Receive OPACK: %s", unpacked_object) if not isinstance(unpacked_object, dict): raise exceptions.ProtocolError( f"Received unexpected type: {type(unpacked_object)}") return unpacked_object
def test_pack_short_strings(): assert pack("a") == b"\x41\x61" assert pack("abc") == b"\x43\x61\x62\x63" assert pack(0x20 * "a") == b"\x60" + (0x20 * b"\x61")
def test_pack_dict(): assert pack({}) == b"\xe0" assert pack({"a": 12, False: None}) == b"\xe2\x41\x61\x14\x02\x04" assert pack({True: {"a": 2}}) == b"\xe1\x01\xe1\x41\x61\x0a"
def test_pack_endless_dict(): assert pack(dict((chr(x), chr(x + 1)) for x in range(97, 127, 2))) == ( b"\xEF" + b"\x41" + b"\x41".join(bytes([x]) for x in range(97, 127)) + b"\x03")
def test_pack_array(): assert pack([]) == b"\xd0" assert pack([1, "test", False]) == b"\xd3\x09\x44\x74\x65\x73\x74\x02" assert pack([[True]]) == b"\xd1\xd1\x01"
def test_pack_endless_array(): assert pack(15 * ["a"]) == b"\xDF\x41\x61" + 14 * b"\xa0" + b"\x03"
def test_pack_short_raw_bytes(): assert pack(b"\xac") == b"\x71\xac" assert pack(b"\x12\x34\x56") == b"\x73\x12\x34\x56" assert pack(0x20 * b"\xad") == b"\x90" + (0x20 * b"\xad")
def test_pack_longer_raw_bytes(): assert pack(33 * b"\x61") == b"\x91\x21" + (33 * b"\x61") assert pack(256 * b"\x61") == b"\x92\x00\x01" + (256 * b"\x61")
def test_pack_none(): assert pack(None) == b"\x04"
def test_pack_boolean(): assert pack(True) == b"\x01" assert pack(False) == b"\x02"
def test_pack_float64(): assert pack(1.0) == b"\x36\x00\x00\x00\x00\x00\x00\xF0\x3F"
def test_pack_larger_integers(): assert pack(0x28) == b"\x30\x28" assert pack(0x1FF) == b"\x31\xFF\x01" assert pack(0x1FFFFFF) == b"\x32\xFF\xFF\xFF\x01" assert pack(0x1FFFFFFFFFFFFFF) == b"\x33\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"
def test_pack_small_integers(): assert pack(0) == b"\x08" assert pack(0xF) == b"\x17" assert pack(0x27) == b"\x2F"
def test_pack_absolute_time(): with pytest.raises(NotImplementedError): pack(datetime.now())
def test_pack_uuid(): assert (pack(UUID("{12345678-1234-5678-1234-567812345678}")) == b"\x05\x124Vx\x124Vx\x124Vx\x124Vx")
def test_pack_longer_strings(): assert pack(33 * "a") == b"\x61\x21" + (33 * b"\x61") assert pack(256 * "a") == b"\x62\x00\x01" + (256 * b"\x61")
def test_pack_unsupported_type(): with pytest.raises(TypeError): pack(set())