1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include "model/controller/link_layer_controller.h"
20 
21 namespace rootcanal {
22 
23 using namespace bluetooth::hci;
24 
25 class LeSetExtendedScanEnableTest : public ::testing::Test {
26 public:
27   LeSetExtendedScanEnableTest() = default;
28   ~LeSetExtendedScanEnableTest() override = default;
29 
30 protected:
31   Address address_{0};
32   ControllerProperties properties_{};
33   LinkLayerController controller_{address_, properties_};
34 };
35 
MakeScanningPhyParameters(LeScanType scan_type,uint16_t scan_interval,uint16_t scan_window)36 static ScanningPhyParameters MakeScanningPhyParameters(LeScanType scan_type, uint16_t scan_interval,
37                                                        uint16_t scan_window) {
38   ScanningPhyParameters parameters;
39   parameters.le_scan_type_ = scan_type;
40   parameters.le_scan_interval_ = scan_interval;
41   parameters.le_scan_window_ = scan_window;
42   return parameters;
43 }
44 
TEST_F(LeSetExtendedScanEnableTest,EnableUsingPublicAddress)45 TEST_F(LeSetExtendedScanEnableTest, EnableUsingPublicAddress) {
46   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
47                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
48                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
49             ErrorCode::SUCCESS);
50   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
51             ErrorCode::SUCCESS);
52 }
53 
TEST_F(LeSetExtendedScanEnableTest,EnableUsingRandomAddress)54 TEST_F(LeSetExtendedScanEnableTest, EnableUsingRandomAddress) {
55   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
56                     OwnAddressType::RANDOM_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
57                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
58             ErrorCode::SUCCESS);
59   ASSERT_EQ(controller_.LeSetRandomAddress(Address{1}), ErrorCode::SUCCESS);
60   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
61             ErrorCode::SUCCESS);
62 }
63 
TEST_F(LeSetExtendedScanEnableTest,EnableUsingResolvableAddress)64 TEST_F(LeSetExtendedScanEnableTest, EnableUsingResolvableAddress) {
65   ASSERT_EQ(
66           controller_.LeSetExtendedScanParameters(
67                   OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL,
68                   0x1, {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
69           ErrorCode::SUCCESS);
70   ASSERT_EQ(controller_.LeSetRandomAddress(Address{1}), ErrorCode::SUCCESS);
71   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
72             ErrorCode::SUCCESS);
73 }
74 
TEST_F(LeSetExtendedScanEnableTest,ResetEachPeriod)75 TEST_F(LeSetExtendedScanEnableTest, ResetEachPeriod) {
76   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
77                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
78                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
79             ErrorCode::SUCCESS);
80   ASSERT_EQ(
81           controller_.LeSetExtendedScanEnable(true, FilterDuplicates::RESET_EACH_PERIOD, 100, 1000),
82           ErrorCode::SUCCESS);
83 }
84 
TEST_F(LeSetExtendedScanEnableTest,Disable)85 TEST_F(LeSetExtendedScanEnableTest, Disable) {
86   ASSERT_EQ(controller_.LeSetExtendedScanEnable(false, FilterDuplicates::DISABLED, 0, 0),
87             ErrorCode::SUCCESS);
88 }
89 
TEST_F(LeSetExtendedScanEnableTest,ValidDuration)90 TEST_F(LeSetExtendedScanEnableTest, ValidDuration) {
91   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
92                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
93                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
94             ErrorCode::SUCCESS);
95 
96   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 127, 1),
97             ErrorCode::SUCCESS);
98 }
99 
TEST_F(LeSetExtendedScanEnableTest,InvalidDuration)100 TEST_F(LeSetExtendedScanEnableTest, InvalidDuration) {
101   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
102                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
103                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
104             ErrorCode::SUCCESS);
105 
106   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::RESET_EACH_PERIOD, 0, 0),
107             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
108   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 128, 1),
109             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
110 }
111 
TEST_F(LeSetExtendedScanEnableTest,NoRandomAddress)112 TEST_F(LeSetExtendedScanEnableTest, NoRandomAddress) {
113   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
114                     OwnAddressType::RANDOM_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
115                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
116             ErrorCode::SUCCESS);
117   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
118             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
119 
120   ASSERT_EQ(
121           controller_.LeSetExtendedScanParameters(
122                   OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL,
123                   0x1, {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
124           ErrorCode::SUCCESS);
125   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
126             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
127 }
128 
129 }  // namespace rootcanal
130