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 LeSetAdvertisingEnableTest : public ::testing::Test {
26 public:
27   LeSetAdvertisingEnableTest() = default;
28   ~LeSetAdvertisingEnableTest() override = default;
29 
30 protected:
31   Address address_{0};
32   ControllerProperties properties_{};
33   LinkLayerController controller_{address_, properties_};
34 };
35 
TEST_F(LeSetAdvertisingEnableTest,EnableUsingPublicAddress)36 TEST_F(LeSetAdvertisingEnableTest, EnableUsingPublicAddress) {
37   ASSERT_EQ(controller_.LeSetAdvertisingParameters(
38                     0x0800, 0x0800, AdvertisingType::ADV_IND, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
39                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty, 0x7,
40                     AdvertisingFilterPolicy::ALL_DEVICES),
41             ErrorCode::SUCCESS);
42   ASSERT_EQ(controller_.LeSetAdvertisingEnable(true), ErrorCode::SUCCESS);
43 }
44 
TEST_F(LeSetAdvertisingEnableTest,EnableUsingRandomAddress)45 TEST_F(LeSetAdvertisingEnableTest, EnableUsingRandomAddress) {
46   ASSERT_EQ(controller_.LeSetAdvertisingParameters(
47                     0x0800, 0x0800, AdvertisingType::ADV_IND, OwnAddressType::RANDOM_DEVICE_ADDRESS,
48                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty, 0x7,
49                     AdvertisingFilterPolicy::ALL_DEVICES),
50             ErrorCode::SUCCESS);
51   ASSERT_EQ(controller_.LeSetRandomAddress(Address{1}), ErrorCode::SUCCESS);
52   ASSERT_EQ(controller_.LeSetAdvertisingEnable(true), ErrorCode::SUCCESS);
53 }
54 
TEST_F(LeSetAdvertisingEnableTest,EnableUsingResolvableAddress)55 TEST_F(LeSetAdvertisingEnableTest, EnableUsingResolvableAddress) {
56   ASSERT_EQ(controller_.LeSetAdvertisingParameters(
57                     0x0800, 0x0800, AdvertisingType::ADV_IND,
58                     OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS,
59                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address{1}, 0x7,
60                     AdvertisingFilterPolicy::ALL_DEVICES),
61             ErrorCode::SUCCESS);
62   ASSERT_EQ(controller_.LeAddDeviceToResolvingList(
63                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address{1},
64                     std::array<uint8_t, 16>{1}, std::array<uint8_t, 16>{1}),
65             ErrorCode::SUCCESS);
66   ASSERT_EQ(controller_.LeSetAddressResolutionEnable(true), ErrorCode::SUCCESS);
67   // Note: the command will fail if the peer address is not in the resolvable
68   // address list and the random address is not set.
69   // Success here signifies that the RPA was successfully generated.
70   ASSERT_EQ(controller_.LeSetAdvertisingEnable(true), ErrorCode::SUCCESS);
71 }
72 
TEST_F(LeSetAdvertisingEnableTest,Disable)73 TEST_F(LeSetAdvertisingEnableTest, Disable) {
74   ASSERT_EQ(controller_.LeSetAdvertisingEnable(false), ErrorCode::SUCCESS);
75 }
76 
TEST_F(LeSetAdvertisingEnableTest,NoRandomAddress)77 TEST_F(LeSetAdvertisingEnableTest, NoRandomAddress) {
78   ASSERT_EQ(controller_.LeSetAdvertisingParameters(
79                     0x0800, 0x0800, AdvertisingType::ADV_IND, OwnAddressType::RANDOM_DEVICE_ADDRESS,
80                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty, 0x7,
81                     AdvertisingFilterPolicy::ALL_DEVICES),
82             ErrorCode::SUCCESS);
83 
84   ASSERT_EQ(controller_.LeSetAdvertisingEnable(true), ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
85 }
86 
TEST_F(LeSetAdvertisingEnableTest,NoResolvableOrRandomAddress)87 TEST_F(LeSetAdvertisingEnableTest, NoResolvableOrRandomAddress) {
88   ASSERT_EQ(controller_.LeSetAddressResolutionEnable(true), ErrorCode::SUCCESS);
89   ASSERT_EQ(controller_.LeSetAdvertisingParameters(
90                     0x0800, 0x0800, AdvertisingType::ADV_IND,
91                     OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS,
92                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty, 0x7,
93                     AdvertisingFilterPolicy::ALL_DEVICES),
94             ErrorCode::SUCCESS);
95 
96   ASSERT_EQ(controller_.LeSetAdvertisingEnable(true), ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
97 }
98 
99 }  // namespace rootcanal
100