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 LeSetExtendedScanParametersTest : public ::testing::Test {
26 public:
27   LeSetExtendedScanParametersTest() = default;
28   ~LeSetExtendedScanParametersTest() 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(LeSetExtendedScanParametersTest,Success)45 TEST_F(LeSetExtendedScanParametersTest, Success) {
46   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
47                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x5,
48                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200),
49                      MakeScanningPhyParameters(LeScanType::ACTIVE, 0x2000, 0x200)}),
50             ErrorCode::SUCCESS);
51 }
52 
TEST_F(LeSetExtendedScanParametersTest,ScanningActive)53 TEST_F(LeSetExtendedScanParametersTest, ScanningActive) {
54   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
55                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x5,
56                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200),
57                      MakeScanningPhyParameters(LeScanType::ACTIVE, 0x2000, 0x200)}),
58             ErrorCode::SUCCESS);
59   ASSERT_EQ(controller_.LeSetExtendedScanEnable(true, FilterDuplicates::DISABLED, 0, 0),
60             ErrorCode::SUCCESS);
61 
62   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
63                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x5,
64                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200),
65                      MakeScanningPhyParameters(LeScanType::ACTIVE, 0x2000, 0x200)}),
66             ErrorCode::COMMAND_DISALLOWED);
67 }
68 
TEST_F(LeSetExtendedScanParametersTest,ReservedPhy)69 TEST_F(LeSetExtendedScanParametersTest, ReservedPhy) {
70   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
71                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x80,
72                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
73             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
74 }
75 
TEST_F(LeSetExtendedScanParametersTest,InvalidPhyParameters)76 TEST_F(LeSetExtendedScanParametersTest, InvalidPhyParameters) {
77   ASSERT_EQ(controller_.LeSetExtendedScanParameters(OwnAddressType::PUBLIC_DEVICE_ADDRESS,
78                                                     LeScanningFilterPolicy::ACCEPT_ALL, 0x1, {}),
79             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
80 
81   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
82                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
83                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200),
84                      MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x200)}),
85             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
86 }
87 
TEST_F(LeSetExtendedScanParametersTest,InvalidScanInterval)88 TEST_F(LeSetExtendedScanParametersTest, InvalidScanInterval) {
89   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
90                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
91                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x0, 0x200)}),
92             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
93 }
94 
TEST_F(LeSetExtendedScanParametersTest,InvalidScanWindow)95 TEST_F(LeSetExtendedScanParametersTest, InvalidScanWindow) {
96   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
97                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
98                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x0)}),
99             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
100 
101   ASSERT_EQ(controller_.LeSetExtendedScanParameters(
102                     OwnAddressType::PUBLIC_DEVICE_ADDRESS, LeScanningFilterPolicy::ACCEPT_ALL, 0x1,
103                     {MakeScanningPhyParameters(LeScanType::PASSIVE, 0x2000, 0x2001)}),
104             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
105 }
106 
107 }  // namespace rootcanal
108