def test_fetch_hci_event(self): with EventCallbackStream( self.device_under_test.hal.FetchHciEvent( empty_pb2.Empty())) as hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) self.send_dut_hci_command( hci_packets.LeAddDeviceToWhiteListBuilder( hci_packets.WhiteListAddressType.RANDOM, '0C:05:04:03:02:01')) event = hci_packets.LeAddDeviceToWhiteListCompleteBuilder( 1, hci_packets.ErrorCode.SUCCESS) hci_event_asserts.assert_event_occurs( lambda packet: bytes(event.Serialize()) in packet.payload)
def test_loopback_hci_command(self): with EventCallbackStream( self.device_under_test.hal.FetchHciEvent( empty_pb2.Empty())) as hci_event_stream: self.send_dut_hci_command( hci_packets.WriteLoopbackModeBuilder( hci_packets.LoopbackMode.ENABLE_LOCAL)) hci_event_asserts = EventAsserts(hci_event_stream) command = hci_packets.LeAddDeviceToWhiteListBuilder( hci_packets.WhiteListAddressType.RANDOM, '0C:05:04:03:02:01') self.send_dut_hci_command(command) hci_event_asserts.assert_event_occurs( lambda packet: bytes(command.Serialize()) in packet.payload)
def test_inquiry_from_dut(self): with EventCallbackStream( self.device_under_test.hal.FetchHciEvent( empty_pb2.Empty())) as hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) self.send_cert_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN)) lap = hci_packets.Lap() lap.lap = 0x33 self.send_dut_hci_command( hci_packets.InquiryBuilder(lap, 0x30, 0xff)) hci_event_asserts.assert_event_occurs( lambda packet: b'\x02\x0f' in packet.payload # Expecting an HCI Event (code 0x02, length 0x0f) )
def test_inquiry_rssi_from_dut(self): inquiry_msg = neighbor_facade.InquiryMsg( inquiry_mode=neighbor_facade.DiscoverabilityMode.GENERAL, result_mode=neighbor_facade.ResultMode.RSSI, length_1_28s=3, max_results=0) with EventCallbackStream( self.device_under_test.neighbor.SetInquiryMode( inquiry_msg)) as inquiry_event_stream: hci_event_asserts = EventAsserts(inquiry_event_stream) self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) hci_event_asserts.assert_event_occurs( lambda msg: b'\x22\x0f' in msg.packet # Expecting an HCI Event (code 0x22, length 0x0f) )
def test_inquiry_from_dut(self): self.register_for_event(hci_packets.EventCode.INQUIRY_RESULT) with EventCallbackStream( self.device_under_test.hci.FetchEvents( empty_proto.Empty())) as hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) self.send_hal_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN)) lap = hci_packets.Lap() lap.lap = 0x33 self.enqueue_hci_command( hci_packets.InquiryBuilder(lap, 0x30, 0xff), False) hci_event_asserts.assert_event_occurs( lambda packet: b'\x02\x0f' in packet.event # Expecting an HCI Event (code 0x02, length 0x0f) )
def test_le_ad_scan_dut_scans(self): with EventCallbackStream( # DUT Scans self.device_under_test.hci_le_scanning_manager.StartScan( empty_proto.Empty())) as advertising_event_stream: hci_event_asserts = EventAsserts(advertising_event_stream) # CERT Advertises gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list(bytes(b'Im_The_CERT!')) gap_data = le_advertising_facade.GapDataMsg( data=bytes(gap_name.Serialize())) config = le_advertising_facade.AdvertisingConfig( advertisement=[gap_data], random_address=common.BluetoothAddress( address=bytes(b'A6:A5:A4:A3:A2:A1')), interval_min=512, interval_max=768, event_type=le_advertising_facade.AdvertisingEventType.ADV_IND, address_type=common.RANDOM_DEVICE_ADDRESS, peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, peer_address=common.BluetoothAddress( address=bytes(b'0C:05:04:03:02:01')), channel_map=7, filter_policy=le_advertising_facade.AdvertisingFilterPolicy. ALL_DEVICES) request = le_advertising_facade.CreateAdvertiserRequest( config=config) create_response = self.cert_device.hci_le_advertising_manager.CreateAdvertiser( request) hci_event_asserts.assert_event_occurs( lambda packet: b'Im_The_CERT' in packet.event) remove_request = le_advertising_facade.RemoveAdvertiserRequest( advertiser_id=create_response.advertiser_id) self.cert_device.hci_le_advertising_manager.RemoveAdvertiser( remove_request)
def test_local_hci_cmd_and_event(self): # Loopback mode responds with ACL and SCO connection complete self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event(hci_packets.EventCode.LOOPBACK_COMMAND) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) with EventCallbackStream( self.device_under_test.hci.FetchEvents( empty_proto.Empty())) as hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) self.enqueue_hci_command( hci_packets.WriteLoopbackModeBuilder( hci_packets.LoopbackMode.ENABLE_LOCAL), True) cmd2loop = hci_packets.ReadLocalNameBuilder() self.enqueue_hci_command(cmd2loop, True) looped_bytes = bytes(cmd2loop.Serialize()) hci_event_asserts.assert_event_occurs( lambda packet: looped_bytes in packet.event)
def test_inquiry_extended_from_dut(self): name_string = b'Im_A_Cert' gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list(bytes(name_string)) gap_data = list([gap_name]) self.enqueue_hci_command( hci_packets.WriteExtendedInquiryResponseBuilder( hci_packets.FecRequired.NOT_REQUIRED, gap_data), True) inquiry_msg = neighbor_facade.InquiryMsg( inquiry_mode=neighbor_facade.DiscoverabilityMode.GENERAL, result_mode=neighbor_facade.ResultMode.EXTENDED, length_1_28s=3, max_results=0) with EventCallbackStream( self.device_under_test.neighbor.SetInquiryMode( inquiry_msg)) as inquiry_event_stream: hci_event_asserts = EventAsserts(inquiry_event_stream) self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) hci_event_asserts.assert_event_occurs( lambda msg: name_string in msg.packet)
def test_remote_name(self): self.register_for_dut_event( hci_packets.EventCode.REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION) with EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as hci_event_stream, \ EventCallbackStream(self.device_under_test.neighbor.GetRemoteNameEvents(empty_proto.Empty())) as name_event_stream: name_event_asserts = EventAsserts(name_event_stream) hci_event_asserts = EventAsserts(hci_event_stream) cert_name = b'Im_A_Cert' padded_name = cert_name while len(padded_name) < 248: padded_name = padded_name + b'\0' self.enqueue_hci_command( hci_packets.WriteLocalNameBuilder(padded_name), True) hci_event_asserts.assert_event_occurs( lambda msg: b'\x0e\x04\x01\x13\x0c' in msg.event) address = hci_packets.Address() def get_address_from_complete(packet): packet_bytes = packet.event if b'\x0e\x0a\x01\x09\x10' in packet_bytes: nonlocal address addr_view = hci_packets.ReadBdAddrCompleteView( hci_packets.CommandCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) address = addr_view.GetBdAddr() return True return False # DUT Enables scans and gets its address self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) self.enqueue_hci_command(hci_packets.ReadBdAddrBuilder(), True) hci_event_asserts.assert_event_occurs(get_address_from_complete) cert_address = address.encode('utf8') self.device_under_test.neighbor.ReadRemoteName( neighbor_facade.RemoteNameRequestMsg( address=cert_address, page_scan_repetition_mode=1, clock_offset=0x6855)) name_event_asserts.assert_event_occurs( lambda msg: cert_name in msg.name)
def test_le_ad_scan_dut_advertises(self): self.register_for_le_event(hci_packets.SubeventCode.ADVERTISING_REPORT) self.register_for_le_event( hci_packets.SubeventCode.EXTENDED_ADVERTISING_REPORT) with EventCallbackStream( self.cert_device.hci.FetchLeSubevents( empty_proto.Empty())) as hci_le_event_stream: hci_event_asserts = EventAsserts(hci_le_event_stream) # CERT Scans self.enqueue_hci_command( hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'), True) scan_parameters = hci_packets.PhyScanParameters() scan_parameters.le_scan_type = hci_packets.LeScanType.ACTIVE scan_parameters.le_scan_interval = 40 scan_parameters.le_scan_window = 20 self.enqueue_hci_command( hci_packets.LeSetExtendedScanParametersBuilder( hci_packets.AddressType.RANDOM_DEVICE_ADDRESS, hci_packets.LeSetScanningFilterPolicy.ACCEPT_ALL, 1, [scan_parameters]), True) self.enqueue_hci_command( hci_packets.LeSetExtendedScanEnableBuilder( hci_packets.Enable.ENABLED, hci_packets.FilterDuplicates.DISABLED, 0, 0), True) # DUT Advertises gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list(bytes(b'Im_The_DUT!')) gap_data = le_advertising_facade.GapDataMsg( data=bytes(gap_name.Serialize())) config = le_advertising_facade.AdvertisingConfig( advertisement=[gap_data], random_address=common.BluetoothAddress( address=bytes(b'0D:05:04:03:02:01')), interval_min=512, interval_max=768, event_type=le_advertising_facade.AdvertisingEventType.ADV_IND, address_type=common.RANDOM_DEVICE_ADDRESS, peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, peer_address=common.BluetoothAddress( address=bytes(b'A6:A5:A4:A3:A2:A1')), channel_map=7, filter_policy=le_advertising_facade.AdvertisingFilterPolicy. ALL_DEVICES) request = le_advertising_facade.CreateAdvertiserRequest( config=config) create_response = self.device_under_test.hci_le_advertising_manager.CreateAdvertiser( request) hci_event_asserts.assert_event_occurs( lambda packet: b'Im_The_DUT' in packet.event) remove_request = le_advertising_facade.RemoveAdvertiserRequest( advertiser_id=create_response.advertiser_id) self.device_under_test.hci_le_advertising_manager.RemoveAdvertiser( remove_request) self.enqueue_hci_command( hci_packets.LeSetScanEnableBuilder( hci_packets.Enable.DISABLED, hci_packets.Enable.DISABLED), True)
def test_le_white_list_connection_cert_advertises(self): with EventCallbackStream(self.device_under_test.hal.FetchHciEvent(empty_pb2.Empty())) as hci_event_stream, \ EventCallbackStream(self.cert_device.hal.FetchHciEvent(empty_pb2.Empty())) as cert_hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) # DUT Connects self.send_dut_hci_command( hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01')) self.send_dut_hci_command( hci_packets.LeAddDeviceToWhiteListBuilder( hci_packets.WhiteListAddressType.RANDOM, '0C:05:04:03:02:01')) phy_scan_params = hci_packets.LeCreateConnPhyScanParameters() phy_scan_params.scan_interval = 0x60 phy_scan_params.scan_window = 0x30 phy_scan_params.conn_interval_min = 0x18 phy_scan_params.conn_interval_max = 0x28 phy_scan_params.conn_latency = 0 phy_scan_params.supervision_timeout = 0x1f4 phy_scan_params.min_ce_length = 0 phy_scan_params.max_ce_length = 0 self.send_dut_hci_command( hci_packets.LeExtendedCreateConnectionBuilder( hci_packets.InitiatorFilterPolicy.USE_WHITE_LIST, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.AddressType.RANDOM_DEVICE_ADDRESS, 'BA:D5:A4:A3:A2:A1', 1, [phy_scan_params])) # CERT Advertises advertising_handle = 1 self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder( advertising_handle, hci_packets.LegacyAdvertisingProperties.ADV_IND, 512, 768, 7, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.PeerAddressType. PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, 'A6:A5:A4:A3:A2:A1', hci_packets.AdvertisingFilterPolicy.ALL_DEVICES, 0x7F, 0, # SID hci_packets.Enable.DISABLED # Scan request notification )) self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder( advertising_handle, '0C:05:04:03:02:01')) gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list( bytes(b'Im_A_Cert!')) # TODO: Fix and remove ! self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingDataBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_name])) enabled_set = hci_packets.EnabledSet() enabled_set.advertising_handle = 1 enabled_set.duration = 0 enabled_set.max_extended_advertising_events = 0 self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingEnableBuilder( hci_packets.Enable.ENABLED, [enabled_set])) # LeConnectionComplete cert_hci_event_asserts.assert_event_occurs( lambda packet: b'\x3e\x13\x01\x00' in packet.payload, timeout=timedelta(seconds=20)) hci_event_asserts.assert_event_occurs( lambda packet: b'\x3e\x13\x01\x00' in packet.payload, timeout=timedelta(seconds=20))
def test_le_connection_dut_advertises(self): with EventCallbackStream(self.device_under_test.hal.FetchHciEvent(empty_pb2.Empty())) as hci_event_stream, \ EventCallbackStream(self.cert_device.hal.FetchHciEvent(empty_pb2.Empty())) as cert_hci_event_stream, \ EventCallbackStream(self.device_under_test.hal.FetchHciAcl(empty_pb2.Empty())) as acl_data_stream, \ EventCallbackStream(self.cert_device.hal.FetchHciAcl(empty_pb2.Empty())) as cert_acl_data_stream: hci_event_asserts = EventAsserts(hci_event_stream) cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) acl_data_asserts = EventAsserts(acl_data_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) # Cert Connects self.send_cert_hci_command( hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01')) phy_scan_params = hci_packets.LeCreateConnPhyScanParameters() phy_scan_params.scan_interval = 0x60 phy_scan_params.scan_window = 0x30 phy_scan_params.conn_interval_min = 0x18 phy_scan_params.conn_interval_max = 0x28 phy_scan_params.conn_latency = 0 phy_scan_params.supervision_timeout = 0x1f4 phy_scan_params.min_ce_length = 0 phy_scan_params.max_ce_length = 0 self.send_cert_hci_command( hci_packets.LeExtendedCreateConnectionBuilder( hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.AddressType.RANDOM_DEVICE_ADDRESS, '0D:05:04:03:02:01', 1, [phy_scan_params])) # DUT Advertises advertising_handle = 0 self.send_dut_hci_command( hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder( advertising_handle, hci_packets.LegacyAdvertisingProperties.ADV_IND, 400, 450, 7, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.PeerAddressType. PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, '00:00:00:00:00:00', hci_packets.AdvertisingFilterPolicy.ALL_DEVICES, 0xF8, 1, #SID hci_packets.Enable.DISABLED # Scan request notification )) self.send_dut_hci_command( hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder( advertising_handle, '0D:05:04:03:02:01')) gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list( bytes(b'Im_The_DUT!')) # TODO: Fix and remove ! self.send_dut_hci_command( hci_packets.LeSetExtendedAdvertisingDataBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_name])) gap_short_name = hci_packets.GapData() gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME gap_short_name.data = list(bytes(b'Im_The_D')) self.send_dut_hci_command( hci_packets.LeSetExtendedAdvertisingScanResponseBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_short_name])) enabled_set = hci_packets.EnabledSet() enabled_set.advertising_handle = advertising_handle enabled_set.duration = 0 enabled_set.max_extended_advertising_events = 0 self.send_dut_hci_command( hci_packets.LeSetExtendedAdvertisingEnableBuilder( hci_packets.Enable.ENABLED, [enabled_set])) conn_handle = 0xfff def payload_handle(packet): packet_bytes = packet.payload if b'\x3e\x13\x01\x00' in packet_bytes: nonlocal conn_handle cc_view = hci_packets.LeConnectionCompleteView( hci_packets.LeMetaEventView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) conn_handle = cc_view.GetConnectionHandle() return True return False cert_hci_event_asserts.assert_event_occurs(payload_handle) cert_handle = conn_handle conn_handle = 0xfff hci_event_asserts.assert_event_occurs(payload_handle) dut_handle = conn_handle # Send ACL Data self.send_dut_acl_data( dut_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'Just SomeAclData')) self.send_cert_acl_data( cert_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'Just SomeMoreAclData')) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.payload)
def test_le_ad_scan_cert_advertises(self): with EventCallbackStream( self.device_under_test.hal.FetchHciEvent( empty_pb2.Empty())) as hci_event_stream: hci_event_asserts = EventAsserts(hci_event_stream) # DUT scans self.send_dut_hci_command( hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01')) phy_scan_params = hci_packets.PhyScanParameters() phy_scan_params.le_scan_interval = 6553 phy_scan_params.le_scan_window = 6553 phy_scan_params.le_scan_type = hci_packets.LeScanType.ACTIVE self.send_dut_hci_command( hci_packets.LeSetExtendedScanParametersBuilder( hci_packets.AddressType.RANDOM_DEVICE_ADDRESS, hci_packets.LeSetScanningFilterPolicy.ACCEPT_ALL, 1, [phy_scan_params])) self.send_dut_hci_command( hci_packets.LeSetExtendedScanEnableBuilder( hci_packets.Enable.ENABLED, hci_packets.FilterDuplicates.DISABLED, 0, 0)) # CERT Advertises advertising_handle = 0 self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder( advertising_handle, hci_packets.LegacyAdvertisingProperties.ADV_IND, 512, 768, 7, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.PeerAddressType. PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, 'A6:A5:A4:A3:A2:A1', hci_packets.AdvertisingFilterPolicy.ALL_DEVICES, 0x7F, 0, # SID hci_packets.Enable.DISABLED # Scan request notification )) self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder( advertising_handle, '0C:05:04:03:02:01')) gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list( bytes(b'Im_A_Cert!')) # TODO: Fix and remove ! self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingDataBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_name])) enabled_set = hci_packets.EnabledSet() enabled_set.advertising_handle = advertising_handle enabled_set.duration = 0 enabled_set.max_extended_advertising_events = 0 self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingEnableBuilder( hci_packets.Enable.ENABLED, [enabled_set])) hci_event_asserts.assert_event_occurs( lambda packet: b'Im_A_Cert' in packet.payload) # Disable Advertising self.send_cert_hci_command( hci_packets.LeSetExtendedAdvertisingEnableBuilder( hci_packets.Enable.DISABLED, [enabled_set])) # Disable Scanning self.send_dut_hci_command( hci_packets.LeSetExtendedScanEnableBuilder( hci_packets.Enable.ENABLED, hci_packets.FilterDuplicates.DISABLED, 0, 0))
def pair_justworks(self, cert_iocap_reply, expected_ui_event): # Cert event registration self.register_for_event(hci_packets.EventCode.LINK_KEY_REQUEST) self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_REQUEST) self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_RESPONSE) self.register_for_event( hci_packets.EventCode.USER_PASSKEY_NOTIFICATION) self.register_for_event( hci_packets.EventCode.USER_CONFIRMATION_REQUEST) self.register_for_event( hci_packets.EventCode.REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION) self.register_for_event(hci_packets.EventCode.LINK_KEY_NOTIFICATION) self.register_for_event(hci_packets.EventCode.SIMPLE_PAIRING_COMPLETE) with EventCallbackStream(self.device_under_test.security.FetchUiEvents(empty_proto.Empty())) as dut_ui_stream, \ EventCallbackStream(self.device_under_test.security.FetchBondEvents(empty_proto.Empty())) as dut_bond_stream, \ EventCallbackStream(self.device_under_test.neighbor.GetRemoteNameEvents(empty_proto.Empty())) as name_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream: cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) dut_ui_event_asserts = EventAsserts(dut_ui_stream) dut_bond_asserts = EventAsserts(dut_bond_stream) dut_name_asserts = EventAsserts(name_event_stream) dut_address = self.device_under_test.hci_controller.GetMacAddress( empty_proto.Empty()).address cert_address = self.cert_device.hci_controller.GetMacAddress( empty_proto.Empty()).address # Enable Simple Secure Pairing self.enqueue_hci_command( hci_packets.WriteSimplePairingModeBuilder( hci_packets.Enable.ENABLED), True) cert_hci_event_asserts.assert_event_occurs( lambda msg: b'\x0e\x04\x01\x56\x0c' in msg.event) # Get the name self.device_under_test.neighbor.ReadRemoteName( neighbor_facade.RemoteNameRequestMsg( address=cert_address, page_scan_repetition_mode=1, clock_offset=0x6855)) dut_name_asserts.assert_event_occurs( lambda msg: self.cert_name in msg.name) self.device_under_test.security.CreateBond( common.BluetoothAddressWithType( address=common.BluetoothAddress(address=cert_address), type=common.BluetoothAddressTypeEnum.PUBLIC_DEVICE_ADDRESS) ) cert_hci_event_asserts.assert_event_occurs( lambda event: logging.debug(event.event) or hci_packets. EventCode.IO_CAPABILITY_REQUEST in event.event) self.enqueue_hci_command(cert_iocap_reply, True) cert_hci_event_asserts.assert_event_occurs( lambda event: logging.debug(event.event) or hci_packets. EventCode.USER_CONFIRMATION_REQUEST in event.event) self.enqueue_hci_command( hci_packets.UserConfirmationRequestReplyBuilder( dut_address.decode('utf8')), True) logging.info("Waiting for UI event") ui_id = -1 def get_unique_id(event): if (event.message_type == expected_ui_event): nonlocal ui_id ui_id = event.unique_id return True return False dut_ui_event_asserts.assert_event_occurs(get_unique_id) logging.info("Sending UI response") self.device_under_test.security.SendUiCallback( security_facade.UiCallbackMsg( message_type=security_facade.UiCallbackType.YES_NO, boolean=True, unique_id=ui_id)) dut_bond_asserts.assert_event_occurs( lambda bond_event: bond_event.message_type == security_facade. BondMsgType.DEVICE_BONDED)
def test_cert_connects(self): self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event(hci_packets.EventCode.ROLE_CHANGE) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) with EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \ EventCallbackStream(self.device_under_test.hci_acl_manager.FetchIncomingConnection(empty_proto.Empty())) as incoming_connection_stream, \ EventCallbackStream(self.device_under_test.hci_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream: cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) incoming_connection_asserts = EventAsserts( incoming_connection_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) acl_data_asserts = EventAsserts(acl_data_stream) # DUT Enables scans and gets its address dut_address = self.device_under_test.hci_controller.GetMacAddress( empty_proto.Empty()).address self.device_under_test.neighbor.EnablePageScan( neighbor_facade.EnableMsg(enabled=True)) # Cert connects self.enqueue_hci_command( hci_packets.CreateConnectionBuilder( dut_address.decode('utf-8'), 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R1, 0x0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH), False) conn_handle = 0xfff def get_handle(packet): packet_bytes = packet.event if b'\x03\x0b\x00' in packet_bytes: nonlocal conn_handle cc_view = hci_packets.ConnectionCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes)))) conn_handle = cc_view.GetConnectionHandle() return True return False # DUT gets a connection request incoming_connection_asserts.assert_event_occurs(get_handle) self.device_under_test.hci_acl_manager.SendAclData( acl_manager_facade.AclData( handle=conn_handle, payload=bytes( b'\x29\x00\x07\x00This is just SomeMoreAclData from the DUT' ))) conn_handle = 0xfff cert_hci_event_asserts.assert_event_occurs(get_handle) cert_handle = conn_handle self.enqueue_acl_data( cert_handle, hci_packets.PacketBoundaryFlag.FIRST_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes( b'\x26\x00\x07\x00This is just SomeAclData from the Cert')) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload)
def test_connection_cert_connects(self): self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) self.register_for_event(hci_packets.EventCode.CONNECTION_REQUEST) self.send_hal_hci_command(hci_packets.WritePageTimeoutBuilder(0x4000)) with EventCallbackStream(self.device_under_test.hci.FetchEvents(empty_proto.Empty())) as hci_event_stream, \ EventCallbackStream(self.device_under_test.hci.FetchAclPackets(empty_proto.Empty())) as acl_data_stream, \ EventCallbackStream(self.cert_device.hal.FetchHciEvent(empty_proto.Empty())) as cert_hci_event_stream, \ EventCallbackStream(self.cert_device.hal.FetchHciAcl(empty_proto.Empty())) as cert_acl_data_stream: hci_event_asserts = EventAsserts(hci_event_stream) cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) acl_data_asserts = EventAsserts(acl_data_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) address = hci_packets.Address() def get_address_from_complete(packet): packet_bytes = packet.event if b'\x0e\x0a\x01\x09\x10' in packet_bytes: nonlocal address addr_view = hci_packets.ReadBdAddrCompleteView( hci_packets.CommandCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) address = addr_view.GetBdAddr() return True return False # DUT Enables scans and gets its address self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) self.enqueue_hci_command(hci_packets.ReadBdAddrBuilder(), True) hci_event_asserts.assert_event_occurs(get_address_from_complete) # Cert Connects self.send_hal_hci_command( hci_packets.CreateConnectionBuilder( address, 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R0, 0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH)) # DUT Accepts connection_request = None def get_connect_request(packet): if b'\x04\x0a' in packet.event: nonlocal connection_request connection_request = hci_packets.ConnectionRequestView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet.event)))) return True return False hci_event_asserts.assert_event_occurs( get_connect_request, timeout=timedelta(seconds=20)) self.enqueue_hci_command( hci_packets.AcceptConnectionRequestBuilder( connection_request.GetBdAddr(), hci_packets.AcceptConnectionRequestRole.REMAIN_SLAVE), False) conn_handle = 0xfff def get_handle(packet_bytes): if b'\x03\x0b\x00' in packet_bytes: nonlocal conn_handle cc_view = hci_packets.ConnectionCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes)))) conn_handle = cc_view.GetConnectionHandle() return True return False def event_handle(packet): packet_bytes = packet.event return get_handle(packet_bytes) def payload_handle(packet): packet_bytes = packet.payload return get_handle(packet_bytes) cert_hci_event_asserts.assert_event_occurs(payload_handle) cert_handle = conn_handle conn_handle = 0xfff hci_event_asserts.assert_event_occurs(event_handle) dut_handle = conn_handle if dut_handle == 0xfff: logging.warning("Failed to get the DUT handle") return False if cert_handle == 0xfff: logging.warning("Failed to get the CERT handle") return False # Send ACL Data self.enqueue_acl_data( dut_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'This is just SomeAclData')) self.send_hal_acl_data( cert_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'This is just SomeMoreAclData')) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data)
def test_dut_connects(self): self.register_for_le_event( hci_packets.SubeventCode.CONNECTION_COMPLETE) with EventCallbackStream(self.cert_device.hci.FetchLeSubevents(empty_proto.Empty())) as cert_hci_le_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \ EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream: cert_hci_le_event_asserts = EventAsserts(cert_hci_le_event_stream) acl_data_asserts = EventAsserts(acl_data_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) # Cert Advertises advertising_handle = 0 self.enqueue_hci_command( hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder( advertising_handle, hci_packets.LegacyAdvertisingProperties.ADV_IND, 400, 450, 7, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.PeerAddressType. PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, '00:00:00:00:00:00', hci_packets.AdvertisingFilterPolicy.ALL_DEVICES, 0xF8, 1, #SID hci_packets.Enable.DISABLED # Scan request notification ), True) self.enqueue_hci_command( hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder( advertising_handle, '0C:05:04:03:02:01'), True) gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list(bytes(b'Im_A_Cert')) self.enqueue_hci_command( hci_packets.LeSetExtendedAdvertisingDataBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_name]), True) gap_short_name = hci_packets.GapData() gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME gap_short_name.data = list(bytes(b'Im_A_C')) self.enqueue_hci_command( hci_packets.LeSetExtendedAdvertisingScanResponseBuilder( advertising_handle, hci_packets.Operation.COMPLETE_ADVERTISEMENT, hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT, [gap_short_name]), True) enabled_set = hci_packets.EnabledSet() enabled_set.advertising_handle = advertising_handle enabled_set.duration = 0 enabled_set.max_extended_advertising_events = 0 self.enqueue_hci_command( hci_packets.LeSetExtendedAdvertisingEnableBuilder( hci_packets.Enable.ENABLED, [enabled_set]), True) with EventCallbackStream( self.device_under_test.hci_le_acl_manager.CreateConnection( le_acl_manager_facade.LeConnectionMsg( address_type=int( hci_packets.AddressType.RANDOM_DEVICE_ADDRESS), address=bytes( '0C:05:04:03:02:01', 'utf8')))) as connection_event_stream: connection_event_asserts = EventAsserts( connection_event_stream) # Cert gets ConnectionComplete with a handle and sends ACL data handle = 0xfff def get_handle(packet): packet_bytes = packet.event nonlocal handle if b'\x3e\x13\x01\x00' in packet_bytes: cc_view = hci_packets.LeConnectionCompleteView( hci_packets.LeMetaEventView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) handle = cc_view.GetConnectionHandle() return True if b'\x3e\x13\x0A\x00' in packet_bytes: cc_view = hci_packets.LeEnhancedConnectionCompleteView( hci_packets.LeMetaEventView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) handle = cc_view.GetConnectionHandle() return True return False cert_hci_le_event_asserts.assert_event_occurs(get_handle) cert_handle = handle self.enqueue_acl_data( cert_handle, hci_packets.PacketBoundaryFlag. FIRST_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'\x19\x00\x07\x00SomeAclData from the Cert')) # DUT gets a connection complete event and sends and receives handle = 0xfff connection_event_asserts.assert_event_occurs(get_handle) self.device_under_test.hci_le_acl_manager.SendAclData( le_acl_manager_facade.LeAclData( handle=handle, payload=bytes( b'\x1C\x00\x07\x00SomeMoreAclData from the DUT'))) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload)
def test_cert_connects(self): self.register_for_le_event( hci_packets.SubeventCode.CONNECTION_COMPLETE) with EventCallbackStream(self.cert_device.hci.FetchLeSubevents(empty_proto.Empty())) as cert_hci_le_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \ EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchIncomingConnection(empty_proto.Empty())) as incoming_connection_stream, \ EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream: cert_hci_le_event_asserts = EventAsserts(cert_hci_le_event_stream) incoming_connection_asserts = EventAsserts( incoming_connection_stream) acl_data_asserts = EventAsserts(acl_data_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) # DUT Advertises gap_name = hci_packets.GapData() gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME gap_name.data = list(bytes(b'Im_The_DUT')) gap_data = le_advertising_facade.GapDataMsg( data=bytes(gap_name.Serialize())) config = le_advertising_facade.AdvertisingConfig( advertisement=[gap_data], random_address=common.BluetoothAddress( address=bytes(b'0D:05:04:03:02:01')), interval_min=512, interval_max=768, event_type=le_advertising_facade.AdvertisingEventType.ADV_IND, address_type=common.RANDOM_DEVICE_ADDRESS, peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, peer_address=common.BluetoothAddress( address=bytes(b'A6:A5:A4:A3:A2:A1')), channel_map=7, filter_policy=le_advertising_facade.AdvertisingFilterPolicy. ALL_DEVICES) request = le_advertising_facade.CreateAdvertiserRequest( config=config) create_response = self.device_under_test.hci_le_advertising_manager.CreateAdvertiser( request) # Cert Connects self.enqueue_hci_command( hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'), True) phy_scan_params = hci_packets.LeCreateConnPhyScanParameters() phy_scan_params.scan_interval = 0x60 phy_scan_params.scan_window = 0x30 phy_scan_params.conn_interval_min = 0x18 phy_scan_params.conn_interval_max = 0x28 phy_scan_params.conn_latency = 0 phy_scan_params.supervision_timeout = 0x1f4 phy_scan_params.min_ce_length = 0 phy_scan_params.max_ce_length = 0 self.enqueue_hci_command( hci_packets.LeExtendedCreateConnectionBuilder( hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS, hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS, hci_packets.AddressType.RANDOM_DEVICE_ADDRESS, '0D:05:04:03:02:01', 1, [phy_scan_params]), False) # Cert gets ConnectionComplete with a handle and sends ACL data handle = 0xfff def get_handle(packet): packet_bytes = packet.event nonlocal handle if b'\x3e\x13\x01\x00' in packet_bytes: cc_view = hci_packets.LeConnectionCompleteView( hci_packets.LeMetaEventView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) handle = cc_view.GetConnectionHandle() return True if b'\x3e\x13\x0A\x00' in packet_bytes: cc_view = hci_packets.LeEnhancedConnectionCompleteView( hci_packets.LeMetaEventView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) handle = cc_view.GetConnectionHandle() return True return False cert_hci_le_event_asserts.assert_event_occurs(get_handle) cert_handle = handle self.enqueue_acl_data( cert_handle, hci_packets.PacketBoundaryFlag.FIRST_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'\x19\x00\x07\x00SomeAclData from the Cert')) # DUT gets a connection complete event and sends and receives handle = 0xfff incoming_connection_asserts.assert_event_occurs(get_handle) self.device_under_test.hci_le_acl_manager.SendAclData( le_acl_manager_facade.LeAclData( handle=handle, payload=bytes( b'\x1C\x00\x07\x00SomeMoreAclData from the DUT'))) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload)
def test_dut_connects(self): self.register_for_event(hci_packets.EventCode.CONNECTION_REQUEST) self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) with EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \ EventCallbackStream(self.device_under_test.hci_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream: cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) acl_data_asserts = EventAsserts(acl_data_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) # CERT Enables scans and gets its address self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) cert_address = None def get_address_from_complete(packet): packet_bytes = packet.event if b'\x0e\x0a\x01\x09\x10' in packet_bytes: nonlocal cert_address addr_view = hci_packets.ReadBdAddrCompleteView( hci_packets.CommandCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) cert_address = addr_view.GetBdAddr() return True return False self.enqueue_hci_command(hci_packets.ReadBdAddrBuilder(), True) cert_hci_event_asserts.assert_event_occurs( get_address_from_complete) with EventCallbackStream( self.device_under_test.hci_acl_manager.CreateConnection( acl_manager_facade.ConnectionMsg( address_type=int( hci_packets.AddressType.PUBLIC_DEVICE_ADDRESS), address=bytes( cert_address, 'utf8')))) as connection_event_stream: connection_event_asserts = EventAsserts( connection_event_stream) connection_request = None def get_connect_request(packet): if b'\x04\x0a' in packet.event: nonlocal connection_request connection_request = hci_packets.ConnectionRequestView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet.event)))) return True return False # Cert Accepts cert_hci_event_asserts.assert_event_occurs(get_connect_request) self.enqueue_hci_command( hci_packets.AcceptConnectionRequestBuilder( connection_request.GetBdAddr(), hci_packets.AcceptConnectionRequestRole.REMAIN_SLAVE), False) # Cert gets ConnectionComplete with a handle and sends ACL data handle = 0xfff def get_handle(packet): packet_bytes = packet.event if b'\x03\x0b\x00' in packet_bytes: nonlocal handle cc_view = hci_packets.ConnectionCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes)))) handle = cc_view.GetConnectionHandle() return True return False cert_hci_event_asserts.assert_event_occurs(get_handle) cert_handle = handle self.enqueue_acl_data( cert_handle, hci_packets.PacketBoundaryFlag. FIRST_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes( b'\x26\x00\x07\x00This is just SomeAclData from the Cert' )) # DUT gets a connection complete event and sends and receives handle = 0xfff connection_event_asserts.assert_event_occurs(get_handle) self.device_under_test.hci_acl_manager.SendAclData( acl_manager_facade.AclData( handle=handle, payload=bytes( b'\x29\x00\x07\x00This is just SomeMoreAclData from the DUT' ))) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload)