1 
2 
3 /*
4  * Copyright 2022 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <gtest/gtest.h>
20 
21 #include "model/controller/link_layer_controller.h"
22 #include "test_helpers.h"
23 
24 namespace rootcanal {
25 
26 using namespace bluetooth::hci;
27 
28 class LeExtendedCreateConnectionTest : public ::testing::Test {
29 public:
30   LeExtendedCreateConnectionTest() = default;
31   ~LeExtendedCreateConnectionTest() override = default;
32 
33 protected:
34   Address address_{0};
35   ControllerProperties properties_{};
36   LinkLayerController controller_{address_, properties_};
37 };
38 
TEST_F(LeExtendedCreateConnectionTest,ConnectUsingPublicAddress)39 TEST_F(LeExtendedCreateConnectionTest, ConnectUsingPublicAddress) {
40   ASSERT_EQ(controller_.LeExtendedCreateConnection(
41                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
42                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
43                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
44                                                  0x0)}),
45             ErrorCode::SUCCESS);
46 }
47 
TEST_F(LeExtendedCreateConnectionTest,ConnectUsingRandomAddress)48 TEST_F(LeExtendedCreateConnectionTest, ConnectUsingRandomAddress) {
49   ASSERT_EQ(controller_.LeSetRandomAddress(Address{1}), ErrorCode::SUCCESS);
50   ASSERT_EQ(controller_.LeExtendedCreateConnection(
51                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::RANDOM_DEVICE_ADDRESS,
52                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
53                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
54                                                  0x0)}),
55             ErrorCode::SUCCESS);
56 }
57 
TEST_F(LeExtendedCreateConnectionTest,ConnectUsingResolvableAddress)58 TEST_F(LeExtendedCreateConnectionTest, ConnectUsingResolvableAddress) {
59   ASSERT_EQ(controller_.LeSetRandomAddress(Address{1}), ErrorCode::SUCCESS);
60   ASSERT_EQ(controller_.LeExtendedCreateConnection(
61                     InitiatorFilterPolicy::USE_PEER_ADDRESS,
62                     OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS,
63                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
64                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
65                                                  0x0)}),
66             ErrorCode::SUCCESS);
67 }
68 
TEST_F(LeExtendedCreateConnectionTest,InitiatingActive)69 TEST_F(LeExtendedCreateConnectionTest, InitiatingActive) {
70   ASSERT_EQ(controller_.LeExtendedCreateConnection(
71                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
72                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
73                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
74                                                  0x0)}),
75             ErrorCode::SUCCESS);
76 
77   ASSERT_EQ(controller_.LeExtendedCreateConnection(
78                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
79                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
80                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
81                                                  0x0)}),
82             ErrorCode::COMMAND_DISALLOWED);
83 }
84 
TEST_F(LeExtendedCreateConnectionTest,NoPhy)85 TEST_F(LeExtendedCreateConnectionTest, NoPhy) {
86   ASSERT_EQ(controller_.LeExtendedCreateConnection(
87                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
88                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x0, {}),
89             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
90 }
91 
TEST_F(LeExtendedCreateConnectionTest,ReservedPhy)92 TEST_F(LeExtendedCreateConnectionTest, ReservedPhy) {
93   ASSERT_EQ(controller_.LeExtendedCreateConnection(
94                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
95                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x8,
96                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
97                                                  0x0)}),
98             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
99 }
100 
TEST_F(LeExtendedCreateConnectionTest,InvalidPhyParameters)101 TEST_F(LeExtendedCreateConnectionTest, InvalidPhyParameters) {
102   ASSERT_EQ(controller_.LeExtendedCreateConnection(
103                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
104                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1, {}),
105             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
106 
107   ASSERT_EQ(
108           controller_.LeExtendedCreateConnection(
109                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
110                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
111                   {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0, 0x0),
112                    MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
113                                                0x0)}),
114           ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
115 }
116 
TEST_F(LeExtendedCreateConnectionTest,InvalidScanInterval)117 TEST_F(LeExtendedCreateConnectionTest, InvalidScanInterval) {
118   ASSERT_EQ(
119           controller_.LeExtendedCreateConnection(
120                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
121                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
122                   {MakeInitiatingPhyParameters(0x3, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0, 0x0)}),
123           ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
124 
125   ASSERT_EQ(controller_.LeExtendedCreateConnection(
126                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
127                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
128                     {MakeInitiatingPhyParameters(0x4001, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
129                                                  0x0)}),
130             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
131 }
132 
TEST_F(LeExtendedCreateConnectionTest,InvalidScanWindow)133 TEST_F(LeExtendedCreateConnectionTest, InvalidScanWindow) {
134   ASSERT_EQ(
135           controller_.LeExtendedCreateConnection(
136                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
137                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
138                   {MakeInitiatingPhyParameters(0x200, 0x3, 0x100, 0x200, 0x010, 0x0c80, 0x0, 0x0)}),
139           ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
140 
141   ASSERT_EQ(controller_.LeExtendedCreateConnection(
142                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
143                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
144                     {MakeInitiatingPhyParameters(0x200, 0x4001, 0x100, 0x200, 0x010, 0x0c80, 0x0,
145                                                  0x0)}),
146             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
147 
148   ASSERT_EQ(controller_.LeExtendedCreateConnection(
149                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
150                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
151                     {MakeInitiatingPhyParameters(0x100, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
152                                                  0x0)}),
153             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
154 }
155 
TEST_F(LeExtendedCreateConnectionTest,InvalidConnectionInterval)156 TEST_F(LeExtendedCreateConnectionTest, InvalidConnectionInterval) {
157   ASSERT_EQ(
158           controller_.LeExtendedCreateConnection(
159                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
160                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
161                   {MakeInitiatingPhyParameters(0x200, 0x200, 0x5, 0x200, 0x010, 0x0c80, 0x0, 0x0)}),
162           ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
163 
164   ASSERT_EQ(controller_.LeExtendedCreateConnection(
165                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
166                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
167                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x0c81, 0x200, 0x010, 0x0c80, 0x0,
168                                                  0x0)}),
169             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
170 
171   ASSERT_EQ(
172           controller_.LeExtendedCreateConnection(
173                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
174                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
175                   {MakeInitiatingPhyParameters(0x200, 0x200, 0x200, 0x5, 0x010, 0x0c80, 0x0, 0x0)}),
176           ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
177 
178   ASSERT_EQ(controller_.LeExtendedCreateConnection(
179                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
180                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
181                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x200, 0x0c81, 0x010, 0x0c80, 0x0,
182                                                  0x0)}),
183             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
184 
185   ASSERT_EQ(controller_.LeExtendedCreateConnection(
186                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
187                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
188                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x200, 0x100, 0x010, 0x0c80, 0x0,
189                                                  0x0)}),
190             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
191 }
192 
TEST_F(LeExtendedCreateConnectionTest,InvalidMaxLatency)193 TEST_F(LeExtendedCreateConnectionTest, InvalidMaxLatency) {
194   ASSERT_EQ(controller_.LeExtendedCreateConnection(
195                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
196                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
197                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x01f4, 0x0c80, 0x0,
198                                                  0x0)}),
199             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
200 }
201 
TEST_F(LeExtendedCreateConnectionTest,InvalidSupervisionTimeout)202 TEST_F(LeExtendedCreateConnectionTest, InvalidSupervisionTimeout) {
203   ASSERT_EQ(
204           controller_.LeExtendedCreateConnection(
205                   InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
206                   AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
207                   {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x9, 0x0, 0x0)}),
208           ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
209 
210   ASSERT_EQ(controller_.LeExtendedCreateConnection(
211                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
212                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
213                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c81, 0x0,
214                                                  0x0)}),
215             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
216 
217   ASSERT_EQ(controller_.LeExtendedCreateConnection(
218                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
219                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
220                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x1f3, 0x0c80, 0x0,
221                                                  0x0)}),
222             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
223 }
224 
TEST_F(LeExtendedCreateConnectionTest,NoRandomAddress)225 TEST_F(LeExtendedCreateConnectionTest, NoRandomAddress) {
226   ASSERT_EQ(controller_.LeExtendedCreateConnection(
227                     InitiatorFilterPolicy::USE_PEER_ADDRESS, OwnAddressType::RANDOM_DEVICE_ADDRESS,
228                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
229                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
230                                                  0x0)}),
231             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
232 
233   ASSERT_EQ(controller_.LeExtendedCreateConnection(
234                     InitiatorFilterPolicy::USE_PEER_ADDRESS,
235                     OwnAddressType::RESOLVABLE_OR_RANDOM_ADDRESS,
236                     AddressWithType{Address{1}, AddressType::PUBLIC_DEVICE_ADDRESS}, 0x1,
237                     {MakeInitiatingPhyParameters(0x200, 0x200, 0x100, 0x200, 0x010, 0x0c80, 0x0,
238                                                  0x0)}),
239             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
240 }
241 
242 }  // namespace rootcanal
243