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