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 "hci/remote_name_request.h"
18
19 #include <com_android_bluetooth_flags.h>
20 #include <flag_macros.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23
24 #include <algorithm>
25 #include <chrono>
26 #include <future>
27 #include <tuple>
28 #include <utility>
29
30 #include "hci/address.h"
31 #include "hci/hci_layer_fake.h"
32 #include "os/thread.h"
33
34 namespace bluetooth {
35 namespace hci {
36 namespace {
37
38 using ::testing::Eq;
39
40 const auto address1 = Address::FromString("A1:A2:A3:A4:A5:A6").value();
41 const auto address2 = Address::FromString("B1:B2:B3:B4:B5:B6").value();
42 const auto address3 = Address::FromString("C1:C2:C3:C4:C5:C6").value();
43
44 const auto remote_name1 = std::array<uint8_t, 248>{1, 2, 3};
45
46 const auto timeout = std::chrono::milliseconds(100);
47
48 MATCHER(IsSet, "Future is not set") {
49 if (arg.wait_for(timeout) != std::future_status::ready) {
50 return false;
51 }
52 const_cast<std::future<void>&>(arg).get();
53 return true;
54 }
55
56 MATCHER_P(IsSetWithValue, matcher, "Future is not set with value") {
57 if (arg.wait_for(timeout) != std::future_status::ready) {
58 return false;
59 }
60 EXPECT_THAT(const_cast<std::decay_t<decltype(arg)>&>(arg).get(), matcher);
61 return true;
62 }
63
64 class RemoteNameRequestModuleTest : public ::testing::Test {
65 protected:
SetUp()66 void SetUp() override {
67 test_hci_layer_ = new HciLayerFake;
68 fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
69
70 fake_registry_.Start<RemoteNameRequestModule>(&thread_);
71 ASSERT_TRUE(fake_registry_.IsStarted<RemoteNameRequestModule>());
72
73 client_handler_ = fake_registry_.GetTestModuleHandler(&RemoteNameRequestModule::Factory);
74 ASSERT_NE(client_handler_, nullptr);
75
76 remote_name_request_module_ = static_cast<RemoteNameRequestModule*>(
77 fake_registry_.GetModuleUnderTest(&RemoteNameRequestModule::Factory));
78
79 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
80 }
81
TearDown()82 void TearDown() override {
83 fake_registry_.SynchronizeModuleHandler(&RemoteNameRequestModule::Factory, timeout);
84 fake_registry_.StopAll();
85 }
86
87 template <typename... T>
impossibleCallback()88 common::ContextualOnceCallback<void(T...)> impossibleCallback() {
89 return client_handler_->BindOnce([](T... /* args */) { ADD_FAILURE(); });
90 }
91
92 template <typename... T>
emptyCallback()93 common::ContextualOnceCallback<void(T...)> emptyCallback() {
94 return client_handler_->BindOnce([](T... /* args */) {});
95 }
96
97 template <typename... T>
promiseCallback(std::promise<void> promise)98 common::ContextualOnceCallback<void(T...)> promiseCallback(std::promise<void> promise) {
99 return client_handler_->BindOnce(
100 [](std::promise<void> promise, T... /* args */) { promise.set_value(); },
101 std::move(promise));
102 }
103
104 template <typename... T>
capturingPromiseCallback(std::promise<std::tuple<T...>> promise)105 common::ContextualOnceCallback<void(T...)> capturingPromiseCallback(
106 std::promise<std::tuple<T...>> promise) {
107 return client_handler_->BindOnce([](std::promise<std::tuple<T...>> promise,
108 T... args) { promise.set_value(std::make_tuple(args...)); },
109 std::move(promise));
110 }
111
112 template <typename T>
capturingPromiseCallback(std::promise<T> promise)113 common::ContextualOnceCallback<void(T)> capturingPromiseCallback(std::promise<T> promise) {
114 return client_handler_->BindOnce([](std::promise<T> promise, T arg) { promise.set_value(arg); },
115 std::move(promise));
116 }
117
118 TestModuleRegistry fake_registry_;
119 os::Thread& thread_ = fake_registry_.GetTestThread();
120 HciLayerFake* test_hci_layer_ = nullptr;
121 RemoteNameRequestModule* remote_name_request_module_ = nullptr;
122 os::Handler* client_handler_ = nullptr;
123 };
124
TEST_F(RemoteNameRequestModuleTest,CorrectCommandSent)125 TEST_F(RemoteNameRequestModuleTest, CorrectCommandSent) {
126 // start a remote name request
127 remote_name_request_module_->StartRemoteNameRequest(
128 address1,
129 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
130 ClockOffsetValid::INVALID),
131 impossibleCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
132 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
133
134 // verify that the correct HCI command was sent
135 auto command = test_hci_layer_->GetCommand();
136 auto discovery_command = DiscoveryCommandView::Create(command);
137 ASSERT_TRUE(discovery_command.IsValid());
138 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
139 ASSERT_TRUE(rnr_command.IsValid());
140 EXPECT_EQ(rnr_command.GetBdAddr(), address1);
141 EXPECT_EQ(rnr_command.GetPageScanRepetitionMode(), PageScanRepetitionMode::R0);
142 EXPECT_EQ(rnr_command.GetClockOffset(), 3);
143 EXPECT_EQ(rnr_command.GetClockOffsetValid(), ClockOffsetValid::INVALID);
144 }
145
TEST_F(RemoteNameRequestModuleTest,FailToSendCommand)146 TEST_F(RemoteNameRequestModuleTest, FailToSendCommand) {
147 auto promise = std::promise<ErrorCode>{};
148 auto future = promise.get_future();
149
150 // start a remote name request
151 remote_name_request_module_->StartRemoteNameRequest(
152 address1,
153 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
154 ClockOffsetValid::INVALID),
155 capturingPromiseCallback<ErrorCode>(std::move(promise)), impossibleCallback<uint64_t>(),
156 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
157 // on the command, return a failure HCI status
158 test_hci_layer_->GetCommand();
159 test_hci_layer_->IncomingEvent(
160 RemoteNameRequestStatusBuilder::Create(ErrorCode::STATUS_UNKNOWN, 1));
161
162 // the completion callback should be immediately invoked with the failing status
163 EXPECT_THAT(future, IsSetWithValue(Eq(ErrorCode::STATUS_UNKNOWN)));
164 }
165
TEST_F(RemoteNameRequestModuleTest,SendCommandSuccessfully)166 TEST_F(RemoteNameRequestModuleTest, SendCommandSuccessfully) {
167 auto promise = std::promise<ErrorCode>{};
168 auto future = promise.get_future();
169
170 // start a remote name request
171 remote_name_request_module_->StartRemoteNameRequest(
172 address1,
173 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
174 ClockOffsetValid::INVALID),
175 capturingPromiseCallback<ErrorCode>(std::move(promise)), impossibleCallback<uint64_t>(),
176 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
177 // the command receives a successful reply, so it successfully starts
178 test_hci_layer_->GetCommand();
179 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
180
181 // the completion callback should be invoked with the failing status
182 EXPECT_THAT(future, IsSetWithValue(Eq(ErrorCode::SUCCESS)));
183 }
184
TEST_F(RemoteNameRequestModuleTest,SendCommandThenCancelIt)185 TEST_F(RemoteNameRequestModuleTest, SendCommandThenCancelIt) {
186 auto promise = std::promise<ErrorCode>{};
187 auto future = promise.get_future();
188
189 // start a remote name request
190 remote_name_request_module_->StartRemoteNameRequest(
191 address1,
192 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
193 ClockOffsetValid::INVALID),
194 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
195 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
196
197 // we successfully start
198 test_hci_layer_->GetCommand();
199 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
200
201 // but then the request is cancelled
202 remote_name_request_module_->CancelRemoteNameRequest(address1);
203
204 // get the cancel command and check it is correct
205 auto command = test_hci_layer_->GetCommand();
206 auto discovery_command = DiscoveryCommandView::Create(command);
207 ASSERT_TRUE(discovery_command.IsValid());
208 auto cancel_command =
209 RemoteNameRequestCancelView::Create(DiscoveryCommandView::Create(discovery_command));
210 ASSERT_TRUE(cancel_command.IsValid());
211 EXPECT_EQ(cancel_command.GetBdAddr(), address1);
212 }
213
TEST_F(RemoteNameRequestModuleTest,SendCommandThenCancelItCallback)214 TEST_F(RemoteNameRequestModuleTest, SendCommandThenCancelItCallback) {
215 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
216 auto future = promise.get_future();
217
218 // start a remote name request
219 remote_name_request_module_->StartRemoteNameRequest(
220 address1,
221 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
222 ClockOffsetValid::INVALID),
223 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
224 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
225
226 // we successfully start
227 test_hci_layer_->GetCommand();
228 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
229
230 // but then the request is cancelled successfully (the status doesn't matter)
231 remote_name_request_module_->CancelRemoteNameRequest(address1);
232 test_hci_layer_->GetCommand();
233 test_hci_layer_->IncomingEvent(
234 RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
235
236 // verify that the completion has NOT yet been invoked (we need to wait for the RNR itself to
237 // complete)
238 EXPECT_THAT(future.wait_for(timeout), std::future_status::timeout);
239
240 // let the RNR complete with a failure
241 test_hci_layer_->IncomingEvent(RemoteNameRequestCompleteBuilder::Create(
242 ErrorCode::UNKNOWN_CONNECTION, address1, remote_name1));
243
244 // only now should the name callback be invoked
245 EXPECT_THAT(future,
246 IsSetWithValue(Eq(std::make_tuple(ErrorCode::UNKNOWN_CONNECTION, remote_name1))));
247 }
248
249 // TODO(aryarahul) - unify HciLayerFake so this test can be run
TEST_F(RemoteNameRequestModuleTest,DISABLED_SendCommandThenCancelItCallbackInteropWorkaround)250 TEST_F(RemoteNameRequestModuleTest, DISABLED_SendCommandThenCancelItCallbackInteropWorkaround) {
251 // Some controllers INCORRECTLY give us an ACL Connection Complete event, rather than a Remote
252 // Name Request Complete event, if we issue a cancellation. We should nonetheless handle this
253 // properly.
254
255 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
256 auto future = promise.get_future();
257
258 // start a remote name request
259 remote_name_request_module_->StartRemoteNameRequest(
260 address1,
261 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
262 ClockOffsetValid::INVALID),
263 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
264 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
265
266 // we successfully start
267 test_hci_layer_->GetCommand();
268 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
269
270 // but then the request is cancelled successfully (the status doesn't matter)
271 remote_name_request_module_->CancelRemoteNameRequest(address1);
272 test_hci_layer_->GetCommand();
273 test_hci_layer_->IncomingEvent(
274 RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
275
276 // get the INCORRECT ACL connection complete event
277 test_hci_layer_->IncomingEvent(ConnectionCompleteBuilder::Create(
278 ErrorCode::UNKNOWN_CONNECTION, 0, address1, LinkType::ACL, Enable::DISABLED));
279
280 // we expect the name callback to be invoked nonetheless
281 EXPECT_THAT(future, IsSetWithValue(Eq(std::make_tuple(ErrorCode::UNKNOWN_CONNECTION,
282 std::array<uint8_t, 248>{}))));
283 }
284
285 // This test should be replaced with the above one, so we test the integration of AclManager and
286 // RnrModule
TEST_F(RemoteNameRequestModuleTest,SendCommandThenCancelItCallbackInteropWorkaround)287 TEST_F(RemoteNameRequestModuleTest, SendCommandThenCancelItCallbackInteropWorkaround) {
288 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
289 auto future = promise.get_future();
290
291 // start a remote name request
292 remote_name_request_module_->StartRemoteNameRequest(
293 address1,
294 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
295 ClockOffsetValid::INVALID),
296 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
297 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
298
299 // we successfully start
300 test_hci_layer_->GetCommand();
301 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
302
303 // but then the request is cancelled successfully (the status doesn't matter)
304 remote_name_request_module_->CancelRemoteNameRequest(address1);
305 test_hci_layer_->GetCommand();
306 test_hci_layer_->IncomingEvent(
307 RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
308
309 // the INCORRECT ACL connection complete event will, from ACLManager, trigger this event
310 remote_name_request_module_->ReportRemoteNameRequestCancellation(address1);
311
312 // we expect the name callback to be invoked nonetheless
313 EXPECT_THAT(future, IsSetWithValue(Eq(std::make_tuple(ErrorCode::UNKNOWN_CONNECTION,
314 std::array<uint8_t, 248>{}))));
315 }
316
TEST_F(RemoteNameRequestModuleTest,SendCommandThenCancelItCancelFails)317 TEST_F(RemoteNameRequestModuleTest, SendCommandThenCancelItCancelFails) {
318 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
319 auto future = promise.get_future();
320
321 // start a remote name request
322 remote_name_request_module_->StartRemoteNameRequest(
323 address1,
324 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
325 ClockOffsetValid::INVALID),
326 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
327 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
328
329 // we successfully start
330 test_hci_layer_->GetCommand();
331 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
332
333 // but then the request is cancelled successfully (the status doesn't matter)
334 remote_name_request_module_->CancelRemoteNameRequest(address1);
335 test_hci_layer_->GetCommand();
336 test_hci_layer_->IncomingEvent(RemoteNameRequestCancelCompleteBuilder::Create(
337 1, ErrorCode::INVALID_HCI_COMMAND_PARAMETERS, address1));
338
339 // we expect the name callback to be invoked nonetheless
340 EXPECT_THAT(future, IsSetWithValue(Eq(std::make_tuple(ErrorCode::INVALID_HCI_COMMAND_PARAMETERS,
341 std::array<uint8_t, 248>{}))));
342 }
343
TEST_F(RemoteNameRequestModuleTest,HostSupportedEvents)344 TEST_F(RemoteNameRequestModuleTest, HostSupportedEvents) {
345 auto promise = std::promise<uint64_t>{};
346 auto future = promise.get_future();
347
348 // start a remote name request
349 remote_name_request_module_->StartRemoteNameRequest(
350 address1,
351 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
352 ClockOffsetValid::INVALID),
353 emptyCallback<ErrorCode>(), capturingPromiseCallback<uint64_t>(std::move(promise)),
354 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
355 test_hci_layer_->GetCommand();
356 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
357
358 // verify that the completion has NOT yet been invoked (we need to wait for the RNR itself to
359 // complete)
360 EXPECT_THAT(future.wait_for(timeout), std::future_status::timeout);
361
362 // report host supported events
363 test_hci_layer_->IncomingEvent(
364 RemoteHostSupportedFeaturesNotificationBuilder::Create(address1, 1234));
365
366 // verify that we got the features
367 EXPECT_THAT(future, IsSetWithValue(Eq((uint64_t)1234)));
368 }
369
TEST_F(RemoteNameRequestModuleTest,CompletedRemoteNameRequest)370 TEST_F(RemoteNameRequestModuleTest, CompletedRemoteNameRequest) {
371 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
372 auto future = promise.get_future();
373
374 // start a remote name request
375 remote_name_request_module_->StartRemoteNameRequest(
376 address1,
377 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
378 ClockOffsetValid::INVALID),
379 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
380 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
381 test_hci_layer_->GetCommand();
382 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
383
384 // verify that the completion has NOT yet been invoked (we need to wait for the RNR itself to
385 // complete)
386 EXPECT_THAT(future.wait_for(timeout), std::future_status::timeout);
387
388 // report remote name (with some random status that should be passed through)
389 test_hci_layer_->IncomingEvent(RemoteNameRequestCompleteBuilder::Create(ErrorCode::STATUS_UNKNOWN,
390 address1, remote_name1));
391
392 // verify that the callback was invoked with the same status
393 EXPECT_THAT(future, IsSetWithValue(Eq(std::make_tuple(ErrorCode::STATUS_UNKNOWN, remote_name1))));
394 }
395
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsSecondOneStarts)396 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsSecondOneStarts) {
397 auto promise1 = std::promise<void>{};
398 auto future1 = promise1.get_future();
399 auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
400 auto future2 = promise2.get_future();
401
402 // start a remote name request
403 remote_name_request_module_->StartRemoteNameRequest(
404 address1,
405 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
406 ClockOffsetValid::INVALID),
407 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
408 promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
409
410 // enqueue a second one
411 remote_name_request_module_->StartRemoteNameRequest(
412 address2,
413 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
414 ClockOffsetValid::VALID),
415 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
416 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
417
418 // acknowledge that the first one has started
419 test_hci_layer_->GetCommand();
420 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
421
422 // report remote name for the first one
423 test_hci_layer_->IncomingEvent(RemoteNameRequestCompleteBuilder::Create(ErrorCode::STATUS_UNKNOWN,
424 address1, remote_name1));
425
426 // verify that the first callback was invoked
427 EXPECT_THAT(future1, IsSet());
428
429 // verify that the second request has now started (so we are participating in the ACL scheduling
430 // process)
431 auto command = test_hci_layer_->GetCommand();
432 auto discovery_command = DiscoveryCommandView::Create(command);
433 ASSERT_TRUE(discovery_command.IsValid());
434 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
435 ASSERT_TRUE(rnr_command.IsValid());
436 EXPECT_EQ(rnr_command.GetBdAddr(), address2);
437 EXPECT_EQ(rnr_command.GetPageScanRepetitionMode(), PageScanRepetitionMode::R1);
438 EXPECT_EQ(rnr_command.GetClockOffset(), 4);
439 EXPECT_EQ(rnr_command.GetClockOffsetValid(), ClockOffsetValid::VALID);
440 }
441
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsSecondOneCancelledWhileQueued)442 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsSecondOneCancelledWhileQueued) {
443 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
444 auto future = promise.get_future();
445
446 // start a remote name request
447 remote_name_request_module_->StartRemoteNameRequest(
448 address1,
449 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
450 ClockOffsetValid::INVALID),
451 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
452 emptyCallback<ErrorCode, std::array<uint8_t, 248>>());
453
454 // enqueue a second one
455 remote_name_request_module_->StartRemoteNameRequest(
456 address2,
457 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
458 ClockOffsetValid::VALID),
459 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
460 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
461
462 // acknowledge that the first one has started
463 test_hci_layer_->GetCommand();
464 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
465
466 // cancel the second one
467 remote_name_request_module_->CancelRemoteNameRequest(address2);
468
469 // verify that the cancellation callback was properly invoked immediately
470 EXPECT_THAT(
471 future,
472 IsSetWithValue(Eq(std::make_tuple(ErrorCode::PAGE_TIMEOUT, std::array<uint8_t, 248>{}))));
473 }
474
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsCancelFirst)475 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsCancelFirst) {
476 auto promise1 = std::promise<void>{};
477 auto future1 = promise1.get_future();
478 auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
479 auto future2 = promise2.get_future();
480
481 // start a remote name request
482 remote_name_request_module_->StartRemoteNameRequest(
483 address1,
484 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
485 ClockOffsetValid::INVALID),
486 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
487 promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
488
489 // enqueue a second one
490 remote_name_request_module_->StartRemoteNameRequest(
491 address2,
492 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
493 ClockOffsetValid::VALID),
494 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
495 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
496
497 // acknowledge that the first one has started
498 test_hci_layer_->GetCommand();
499 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
500
501 // cancel the first one
502 remote_name_request_module_->CancelRemoteNameRequest(address1);
503
504 // let the cancel complete
505 test_hci_layer_->GetCommand();
506 test_hci_layer_->IncomingEvent(
507 RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
508 test_hci_layer_->IncomingEvent(RemoteNameRequestCompleteBuilder::Create(
509 ErrorCode::UNKNOWN_CONNECTION, address1, remote_name1));
510
511 // verify that the second request has now started
512 auto command = test_hci_layer_->GetCommand();
513 auto discovery_command = DiscoveryCommandView::Create(command);
514 ASSERT_TRUE(discovery_command.IsValid());
515 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
516 ASSERT_TRUE(rnr_command.IsValid());
517 EXPECT_EQ(rnr_command.GetBdAddr(), address2);
518 }
519
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsCancelFirstWithBuggyController)520 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsCancelFirstWithBuggyController) {
521 auto promise1 = std::promise<void>{};
522 auto future1 = promise1.get_future();
523 auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
524 auto future2 = promise2.get_future();
525
526 // start a remote name request
527 remote_name_request_module_->StartRemoteNameRequest(
528 address1,
529 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
530 ClockOffsetValid::INVALID),
531 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
532 promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
533
534 // enqueue a second one
535 remote_name_request_module_->StartRemoteNameRequest(
536 address2,
537 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
538 ClockOffsetValid::VALID),
539 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
540 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
541
542 // acknowledge that the first one has started
543 test_hci_layer_->GetCommand();
544 test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
545
546 // cancel the first one
547 remote_name_request_module_->CancelRemoteNameRequest(address1);
548
549 // let the cancel complete
550 test_hci_layer_->GetCommand();
551 test_hci_layer_->IncomingEvent(
552 RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
553 // send the INCORRECT response that we tolerate for interop reasons
554 remote_name_request_module_->ReportRemoteNameRequestCancellation(address1);
555
556 // verify that the second request has now started
557 auto command = test_hci_layer_->GetCommand();
558 auto discovery_command = DiscoveryCommandView::Create(command);
559 ASSERT_TRUE(discovery_command.IsValid());
560 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
561 ASSERT_TRUE(rnr_command.IsValid());
562 EXPECT_EQ(rnr_command.GetBdAddr(), address2);
563 }
564
TEST_F(RemoteNameRequestModuleTest,FailToSendCommandThenSendNext)565 TEST_F(RemoteNameRequestModuleTest, FailToSendCommandThenSendNext) {
566 auto promise = std::promise<ErrorCode>{};
567 auto future = promise.get_future();
568
569 // start a remote name request
570 remote_name_request_module_->StartRemoteNameRequest(
571 address1,
572 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
573 ClockOffsetValid::INVALID),
574 capturingPromiseCallback<ErrorCode>(std::move(promise)), impossibleCallback<uint64_t>(),
575 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
576 // on the command, return a failure HCI status
577 test_hci_layer_->GetCommand();
578 test_hci_layer_->IncomingEvent(
579 RemoteNameRequestStatusBuilder::Create(ErrorCode::STATUS_UNKNOWN, 1));
580
581 // start a second request
582 remote_name_request_module_->StartRemoteNameRequest(
583 address2,
584 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
585 ClockOffsetValid::VALID),
586 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
587 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
588
589 // verify that it started
590 auto command = test_hci_layer_->GetCommand();
591 auto discovery_command = DiscoveryCommandView::Create(command);
592 ASSERT_TRUE(discovery_command.IsValid());
593 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
594 ASSERT_TRUE(rnr_command.IsValid());
595 EXPECT_EQ(rnr_command.GetBdAddr(), address2);
596 }
597
TEST_F(RemoteNameRequestModuleTest,FailToSendCommandThenDequeueNext)598 TEST_F(RemoteNameRequestModuleTest, FailToSendCommandThenDequeueNext) {
599 auto promise = std::promise<ErrorCode>{};
600 auto future = promise.get_future();
601
602 // start a remote name request
603 remote_name_request_module_->StartRemoteNameRequest(
604 address1,
605 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
606 ClockOffsetValid::INVALID),
607 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
608 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
609
610 // enqueue a second one
611 remote_name_request_module_->StartRemoteNameRequest(
612 address2,
613 RemoteNameRequestBuilder::Create(address2, PageScanRepetitionMode::R1, 4,
614 ClockOffsetValid::VALID),
615 impossibleCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
616 impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
617
618 // for the first, return a failure HCI status
619 test_hci_layer_->GetCommand();
620 test_hci_layer_->IncomingEvent(
621 RemoteNameRequestStatusBuilder::Create(ErrorCode::STATUS_UNKNOWN, 1));
622
623 // verify that the second one started
624 auto command = test_hci_layer_->GetCommand();
625 auto discovery_command = DiscoveryCommandView::Create(command);
626 ASSERT_TRUE(discovery_command.IsValid());
627 auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
628 ASSERT_TRUE(rnr_command.IsValid());
629 EXPECT_EQ(rnr_command.GetBdAddr(), address2);
630 }
631
TEST_F(RemoteNameRequestModuleTest,CancelJustWhenRNREventReturns)632 TEST_F(RemoteNameRequestModuleTest, CancelJustWhenRNREventReturns) {
633 auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
634 auto future = promise.get_future();
635
636 // start a remote name request
637 remote_name_request_module_->StartRemoteNameRequest(
638 address1,
639 RemoteNameRequestBuilder::Create(address1, PageScanRepetitionMode::R0, 3,
640 ClockOffsetValid::INVALID),
641 emptyCallback<ErrorCode>(), impossibleCallback<uint64_t>(),
642 capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
643
644 // we successfully start
645 test_hci_layer_->GetCommand();
646
647 auto promise2 = std::promise<void>();
648 auto future2 = promise2.get_future();
649 client_handler_->Post(base::BindOnce(
650 [](RemoteNameRequestModule* remote_name_request_module, HciLayerFake* test_hci_layer,
651 std::promise<void> promise2) {
652 // but then the request is cancelled successfully (the status doesn't matter)
653 remote_name_request_module->CancelRemoteNameRequest(address1);
654
655 // Send an rnr event completed with page timeout status
656 test_hci_layer->IncomingEvent(
657 RemoteNameRequestStatusBuilder::Create(ErrorCode::PAGE_TIMEOUT, 1));
658
659 promise2.set_value();
660 },
661 remote_name_request_module_, test_hci_layer_, std::move(promise2)));
662
663 future2.wait();
664 }
665
666 } // namespace
667 } // namespace hci
668 } // namespace bluetooth
669