1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17This is a stress test for Fuchsia GATT connections.
18
19Setup:
20This test only requires two fuchsia devices as the purpose is to test
21the robusntess of GATT connections.
22"""
23
24import time
25
26from acts import signals
27from acts.base_test import BaseTestClass
28from acts.test_decorators import test_tracker_info
29from acts.test_utils.bt.bt_test_utils import generate_id_by_size
30
31
32class FuchsiaBtScanTest(BaseTestClass):
33    scan_timeout_seconds = 30
34
35    def setup_class(self):
36        super().setup_class()
37        self.pri_dut = self.fuchsia_devices[0]
38        self.sec_dut = self.fuchsia_devices[1]
39
40        self.pri_dut.btc_lib.initBluetoothControl()
41        self.sec_dut.btc_lib.initBluetoothControl()
42
43    # TODO: add @test_tracker_info(uuid='')
44    def test_scan_with_peer_set_non_discoverable(self):
45        """Test Bluetooth scan with peer set to non discoverable.
46
47        Steps:
48        1. Set peer device to a unique device name.
49        2. Set peer device to be non-discoverable.
50        3. Perform a BT Scan with primary dut with enough time to
51        gather results.
52
53        Expected Result:
54        Verify there are no results that match the unique device
55        name in step 1.
56
57        Returns:
58          signals.TestPass if no errors
59          signals.TestFailure if there are any errors during the test.
60
61        TAGS: BR/EDR, BT
62        Priority: 1
63        """
64        local_name = generate_id_by_size(10)
65        self.sec_dut.btc_lib.setName(local_name)
66        self.sec_dut.btc_lib.setDiscoverable(False)
67
68        self.pri_dut.btc_lib.requestDiscovery(True)
69        time.sleep(self.scan_timeout_seconds)
70        self.pri_dut.btc_lib.requestDiscovery(False)
71        discovered_devices = self.pri_dut.btc_lib.getKnownRemoteDevices()
72        for device in discovered_devices.get("result").values():
73            discoverd_name = device.get("name")
74            if discoverd_name is not None and discoverd_name is local_name:
75                raise signals.TestFailure(
76                    "Found peer unexpectedly: {}.".format(device))
77        raise signals.TestPass("Successfully didn't find peer device.")
78
79    # TODO: add @test_tracker_info(uuid='')
80    def test_scan_with_peer_set_discoverable(self):
81        """Test Bluetooth scan with peer set to discoverable.
82
83        Steps:
84        1. Set peer device to a unique device name.
85        2. Set peer device to be discoverable.
86        3. Perform a BT Scan with primary dut with enough time to
87        gather results.
88
89        Expected Result:
90        Verify there is a result that match the unique device
91        name in step 1.
92
93        Returns:
94          signals.TestPass if no errors
95          signals.TestFailure if there are any errors during the test.
96
97        TAGS: BR/EDR, BT
98        Priority: 1
99        """
100        local_name = generate_id_by_size(10)
101        self.log.info("Setting local peer name to: {}".format(local_name))
102        self.sec_dut.btc_lib.setName(local_name)
103        self.sec_dut.btc_lib.setDiscoverable(True)
104
105        self.pri_dut.btc_lib.requestDiscovery(True)
106        end_time = time.time() + self.scan_timeout_seconds
107        poll_timeout = 10
108        while time.time() < end_time:
109            discovered_devices = self.pri_dut.btc_lib.getKnownRemoteDevices()
110            for device in discovered_devices.get("result").values():
111                self.log.info(device)
112                discoverd_name = device.get("name")
113                if discoverd_name is not None and discoverd_name in local_name:
114                    self.pri_dut.btc_lib.requestDiscovery(False)
115                    raise signals.TestPass("Successfully found peer device.")
116            time.sleep(poll_timeout)
117        self.pri_dut.btc_lib.requestDiscovery(False)
118        raise signals.TestFailure("Unable to find peer device.")
119