def test__convert_from_python__bool__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._convert_from_python(1, 2, True) self.assertEqual(b'\x01', result) result = pairing._convert_from_python(1, 2, False) self.assertEqual(b'\x00', result)
def test__find_characteristic_in_pairing_data__found(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._find_characteristic_in_pairing_data(1, 2) self.assertIsNotNone(result) self.assertIn('format', result) self.assertEqual('bool', result['format'])
def test__convert_from_python__float__wrong_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) self.assertRaises(FormatError, pairing._convert_from_python, 1, 4, 'hallo') self.assertRaises(FormatError, pairing._convert_from_python, 1, 4, True)
def test__convert_from_python__string__wrong_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) self.assertRaises(FormatError, pairing._convert_from_python, 1, 9, 2) self.assertRaises(FormatError, pairing._convert_from_python, 1, 9, True) self.assertRaises(FormatError, pairing._convert_from_python, 1, 9, 10E20) self.assertRaises(FormatError, pairing._convert_from_python, 1, 9, 2.53) self.assertRaises(FormatError, pairing._convert_from_python, 1, 9, -1)
def test__convert_from_python__uint64__wrong_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) self.assertRaises(FormatError, pairing._convert_from_python, 1, 8, 'hallo') self.assertRaises(FormatError, pairing._convert_from_python, 1, 8, True) self.assertRaises(FormatError, pairing._convert_from_python, 1, 8, 10E20) self.assertRaises(FormatError, pairing._convert_from_python, 1, 8, 2.53) self.assertRaises(FormatError, pairing._convert_from_python, 1, 8, -1)
def finish_pairing(pin): Controller.check_pin_format(pin) pairing = perform_pair_setup_part2(pin, str(uuid.uuid4()), write_fun, salt, pub_key) pairing['AccessoryMAC'] = accessory_mac pairing['Connection'] = 'BLE' self.pairings[alias] = BlePairing(pairing, adapter)
def load_data(self, filename): """ Loads the pairing data of the controller from a file. :param filename: the file name of the pairing data :raises ConfigLoadingError: if the config could not be loaded. The reason is given in the message. :raises TransportNotSupportedError: if the dependencies for the selected transport are not installed """ try: with open(filename, 'r') as input_fp: data = json.load(input_fp) for pairing_id in data: if 'Connection' not in data[pairing_id]: # This is a pre BLE entry in the file with the pairing data, hence it is for an IP based # accessory. So we set the connection type (in case save data is used everything will be fine) # and also issue a warning data[pairing_id]['Connection'] = 'IP' self.logger.warning( 'Loaded pairing for %s with missing connection type. Assume this is IP based.', pairing_id) if data[pairing_id]['Connection'] == 'IP': if not IP_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('IP') self.pairings[pairing_id] = IpPairing(data[pairing_id]) elif data[pairing_id]['Connection'] == 'BLE': if not BLE_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('BLE') self.pairings[pairing_id] = BlePairing( data[pairing_id], self.ble_adapter) elif data[pairing_id][ 'Connection'] == 'ADDITIONAL_PAIRING': self.pairings[pairing_id] = AdditionalPairing( data[pairing_id]) else: # ignore anything else, issue warning self.logger.warning( 'could not load pairing %s of type "%s"', pairing_id, data[pairing_id]['Connection']) except PermissionError: raise ConfigLoadingError( 'Could not open "{f}" due to missing permissions'.format( f=filename)) except JSONDecodeError: raise ConfigLoadingError( 'Cannot parse "{f}" as JSON file'.format(f=filename)) except FileNotFoundError: raise ConfigLoadingError( 'Could not open "{f}" because it does not exist'.format( f=filename))
def finish_pairing(pin): Controller.check_pin_format(pin) state_machine = perform_pair_setup_part2(pin, str(uuid.uuid4()), salt, pub_key) request, expected = state_machine.send(None) while True: try: response = write_fun(request, expected) request, expected = state_machine.send(response) except StopIteration as result: pairing = result.value break pairing['AccessoryMAC'] = accessory_mac pairing['Connection'] = 'BLE' self.pairings[alias] = BlePairing(pairing, adapter)
def test__convert_from_python__int__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._convert_from_python(1, 3, 1234) self.assertEqual(b'\xd2\x04\x00\x00', result)
def test__convert_from_python__bool__wrong_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) self.assertRaises(FormatError, pairing._convert_from_python, 1, 2, 123)
def test__find_characteristic_in_pairing_data__not_found(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._find_characteristic_in_pairing_data(1, 1) self.assertIsNone(result)
def test__convert_from_python__tlv__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) data = TLV.encode_list([(6, bytearray(b'\x03'))]) self.assertEqual(b'\x06\x01\x03', pairing._convert_from_python(1, 10, data))
def test__convert_from_python__string__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) self.assertEqual(b'Hello', pairing._convert_from_python(1, 9, 'Hello'))
def test__convert_from_python__uint64__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._convert_from_python(1, 8, 42) self.assertEqual(b'\x2A\x00\x00\x00\x00\x00\x00\x00', result)
def test__convert_from_python__float__proper_values(self): pairing_data = self._create_pairing_data() pairing = BlePairing(pairing_data) result = pairing._convert_from_python(1, 4, 3.141) self.assertEqual(b'\x25\x06\x49\x40', result)