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