1#!/usr/bin/env python3
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from cert.gd_base_test import GdBaseTestClass
18from cert.event_stream import EventStream
19from cert.truth import assertThat
20from google.protobuf import empty_pb2 as empty_proto
21from hci.facade import hci_facade_pb2 as hci_facade
22from hci.facade import le_advertising_manager_facade_pb2 as le_advertising_facade
23from hci.facade import le_initiator_address_facade_pb2 as le_initiator_address_facade
24from bluetooth_packets_python3 import hci_packets
25from facade import common_pb2 as common
26
27
28class LeScanningWithSecurityTest(GdBaseTestClass):
29
30    def setup_class(self):
31        super().setup_class(dut_module='SECURITY', cert_module='HCI_INTERFACES')
32
33    def register_for_event(self, event_code):
34        msg = hci_facade.EventCodeMsg(code=int(event_code))
35        self.cert.hci.RegisterEventHandler(msg)
36
37    def register_for_le_event(self, event_code):
38        msg = hci_facade.LeSubeventCodeMsg(code=int(event_code))
39        self.cert.hci.RegisterLeEventHandler(msg)
40
41    def enqueue_hci_command(self, command, expect_complete):
42        cmd_bytes = bytes(command.Serialize())
43        cmd = common.Data(command=cmd_bytes)
44        if (expect_complete):
45            self.cert.hci.EnqueueCommandWithComplete(cmd)
46        else:
47            self.cert.hci.EnqueueCommandWithStatus(cmd)
48
49    def test_le_ad_scan_dut_scans(self):
50        """
51            Verify that the IUT address policy is correctly initiated by SecurityManager, and we can start a scan.
52        """
53        cert_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
54            address_policy=le_initiator_address_facade.AddressPolicy.USE_STATIC_ADDRESS,
55            address_with_type=common.BluetoothAddressWithType(
56                address=common.BluetoothAddress(address=bytes(b'C0:05:04:03:02:01')),
57                type=common.RANDOM_DEVICE_ADDRESS),
58            rotation_irk=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
59            minimum_rotation_time=0,
60            maximum_rotation_time=0)
61        self.cert.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(cert_privacy_policy)
62        with EventStream(
63                # DUT Scans
64                self.dut.hci_le_scanning_manager.StartScan(empty_proto.Empty())) as advertising_event_stream:
65
66            # CERT Advertises
67            gap_name = hci_packets.GapData()
68            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
69            gap_name.data = list(bytes(b'Im_The_CERT!'))
70            gap_data = le_advertising_facade.GapDataMsg(data=bytes(gap_name.Serialize()))
71            config = le_advertising_facade.AdvertisingConfig(
72                advertisement=[gap_data],
73                interval_min=512,
74                interval_max=768,
75                advertising_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
76                own_address_type=common.USE_RANDOM_DEVICE_ADDRESS,
77                channel_map=7,
78                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.ALL_DEVICES)
79            request = le_advertising_facade.CreateAdvertiserRequest(config=config)
80
81            create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(request)
82
83            assertThat(advertising_event_stream).emits(lambda packet: b'Im_The_CERT' in packet.event)
84
85            remove_request = le_advertising_facade.RemoveAdvertiserRequest(advertiser_id=create_response.advertiser_id)
86            self.cert.hci_le_advertising_manager.RemoveAdvertiser(remove_request)
87