• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <algorithm>
23 #include <chrono>
24 #include <future>
25 #include <map>
26 #include <tuple>
27 
28 #include "common/bind.h"
29 #include "common/init_flags.h"
30 #include "hci/address.h"
31 #include "hci/controller_mock.h"
32 #include "hci/hci_layer_fake.h"
33 #include "os/thread.h"
34 
35 namespace bluetooth {
36 namespace hci {
37 namespace {
38 
39 using ::testing::Eq;
40 
41 const auto address1 = Address::FromString("A1:A2:A3:A4:A5:A6").value();
42 const auto address2 = Address::FromString("B1:B2:B3:B4:B5:B6").value();
43 const auto address3 = Address::FromString("C1:C2:C3:C4:C5:C6").value();
44 
45 const auto remote_name1 = std::array<uint8_t, 248>{1, 2, 3};
46 
47 const auto timeout = std::chrono::milliseconds(100);
48 
49 MATCHER(IsSet, "Future is not set") {
50   if (arg.wait_for(timeout) != std::future_status::ready) {
51     return false;
52   }
53   const_cast<std::future<void>&>(arg).get();
54   return true;
55 }
56 
57 MATCHER_P(IsSetWithValue, matcher, "Future is not set with value") {
58   if (arg.wait_for(timeout) != std::future_status::ready) {
59     return false;
60   }
61   EXPECT_THAT(const_cast<std::decay_t<decltype(arg)>&>(arg).get(), matcher);
62   return true;
63 }
64 
65 class RemoteNameRequestModuleTest : public ::testing::Test {
66  protected:
SetUp()67   void SetUp() override {
68     test_hci_layer_ = new TestHciLayer;
69     fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
70 
71     fake_registry_.Start<RemoteNameRequestModule>(&thread_);
72     ASSERT_TRUE(fake_registry_.IsStarted<RemoteNameRequestModule>());
73 
74     client_handler_ = fake_registry_.GetTestModuleHandler(&RemoteNameRequestModule::Factory);
75     ASSERT_NE(client_handler_, nullptr);
76 
77     remote_name_request_module_ = static_cast<RemoteNameRequestModule*>(
78         fake_registry_.GetModuleUnderTest(&RemoteNameRequestModule::Factory));
79 
80     ::testing::FLAGS_gtest_death_test_style = "threadsafe";
81   }
82 
TearDown()83   void TearDown() override {
84     fake_registry_.SynchronizeModuleHandler(&RemoteNameRequestModule::Factory, timeout);
85     fake_registry_.StopAll();
86   }
87 
88   template <typename... T>
impossibleCallback()89   common::ContextualOnceCallback<void(T...)> impossibleCallback() {
90     return client_handler_->BindOnce([](T... args) { ADD_FAILURE(); });
91   }
92 
93   template <typename... T>
emptyCallback()94   common::ContextualOnceCallback<void(T...)> emptyCallback() {
95     return client_handler_->BindOnce([](T... args) {});
96   }
97 
98   template <typename... T>
promiseCallback(std::promise<void> promise)99   common::ContextualOnceCallback<void(T...)> promiseCallback(std::promise<void> promise) {
100     return client_handler_->BindOnce(
101         [](std::promise<void> promise, T... args) { promise.set_value(); }, 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   TestHciLayer* 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 TestHciLayer 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,HostSupportedEvents)330 TEST_F(RemoteNameRequestModuleTest, HostSupportedEvents) {
331   auto promise = std::promise<uint64_t>{};
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       capturingPromiseCallback<uint64_t>(std::move(promise)),
341       impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
342   test_hci_layer_->GetCommand();
343   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
344 
345   // verify that the completion has NOT yet been invoked (we need to wait for the RNR itself to
346   // complete)
347   EXPECT_THAT(future.wait_for(timeout), std::future_status::timeout);
348 
349   // report host supported events
350   test_hci_layer_->IncomingEvent(
351       RemoteHostSupportedFeaturesNotificationBuilder::Create(address1, 1234));
352 
353   // verify that we got the features
354   EXPECT_THAT(future, IsSetWithValue(Eq((uint64_t)1234)));
355 }
356 
TEST_F(RemoteNameRequestModuleTest,CompletedRemoteNameRequest)357 TEST_F(RemoteNameRequestModuleTest, CompletedRemoteNameRequest) {
358   auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
359   auto future = promise.get_future();
360 
361   // start a remote name request
362   remote_name_request_module_->StartRemoteNameRequest(
363       address1,
364       RemoteNameRequestBuilder::Create(
365           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
366       emptyCallback<ErrorCode>(),
367       impossibleCallback<uint64_t>(),
368       capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
369   test_hci_layer_->GetCommand();
370   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
371 
372   // verify that the completion has NOT yet been invoked (we need to wait for the RNR itself to
373   // complete)
374   EXPECT_THAT(future.wait_for(timeout), std::future_status::timeout);
375 
376   // report remote name (with some random status that should be passed through)
377   test_hci_layer_->IncomingEvent(
378       RemoteNameRequestCompleteBuilder::Create(ErrorCode::STATUS_UNKNOWN, address1, remote_name1));
379 
380   // verify that the callback was invoked with the same status
381   EXPECT_THAT(future, IsSetWithValue(Eq(std::make_tuple(ErrorCode::STATUS_UNKNOWN, remote_name1))));
382 }
383 
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsSecondOneStarts)384 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsSecondOneStarts) {
385   auto promise1 = std::promise<void>{};
386   auto future1 = promise1.get_future();
387   auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
388   auto future2 = promise2.get_future();
389 
390   // start a remote name request
391   remote_name_request_module_->StartRemoteNameRequest(
392       address1,
393       RemoteNameRequestBuilder::Create(
394           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
395       emptyCallback<ErrorCode>(),
396       impossibleCallback<uint64_t>(),
397       promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
398 
399   // enqueue a second one
400   remote_name_request_module_->StartRemoteNameRequest(
401       address2,
402       RemoteNameRequestBuilder::Create(
403           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
404       emptyCallback<ErrorCode>(),
405       impossibleCallback<uint64_t>(),
406       capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
407 
408   // acknowledge that the first one has started
409   test_hci_layer_->GetCommand();
410   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
411 
412   // report remote name for the first one
413   test_hci_layer_->IncomingEvent(
414       RemoteNameRequestCompleteBuilder::Create(ErrorCode::STATUS_UNKNOWN, address1, remote_name1));
415 
416   // verify that the first callback was invoked
417   EXPECT_THAT(future1, IsSet());
418 
419   // verify that the second request has now started (so we are participating in the ACL scheduling
420   // process)
421   auto command = test_hci_layer_->GetCommand();
422   auto discovery_command = DiscoveryCommandView::Create(command);
423   ASSERT_TRUE(discovery_command.IsValid());
424   auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
425   ASSERT_TRUE(rnr_command.IsValid());
426   EXPECT_EQ(rnr_command.GetBdAddr(), address2);
427   EXPECT_EQ(rnr_command.GetPageScanRepetitionMode(), PageScanRepetitionMode::R1);
428   EXPECT_EQ(rnr_command.GetClockOffset(), 4);
429   EXPECT_EQ(rnr_command.GetClockOffsetValid(), ClockOffsetValid::VALID);
430 }
431 
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsSecondOneCancelledWhileQueued)432 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsSecondOneCancelledWhileQueued) {
433   auto promise = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
434   auto future = promise.get_future();
435 
436   // start a remote name request
437   remote_name_request_module_->StartRemoteNameRequest(
438       address1,
439       RemoteNameRequestBuilder::Create(
440           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
441       emptyCallback<ErrorCode>(),
442       impossibleCallback<uint64_t>(),
443       emptyCallback<ErrorCode, std::array<uint8_t, 248>>());
444 
445   // enqueue a second one
446   remote_name_request_module_->StartRemoteNameRequest(
447       address2,
448       RemoteNameRequestBuilder::Create(
449           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
450       emptyCallback<ErrorCode>(),
451       impossibleCallback<uint64_t>(),
452       capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise)));
453 
454   // acknowledge that the first one has started
455   test_hci_layer_->GetCommand();
456   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
457 
458   // cancel the second one
459   remote_name_request_module_->CancelRemoteNameRequest(address2);
460 
461   // verify that the cancellation callback was properly invoked immediately
462   EXPECT_THAT(
463       future,
464       IsSetWithValue(Eq(std::make_tuple(ErrorCode::PAGE_TIMEOUT, std::array<uint8_t, 248>{}))));
465 }
466 
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsCancelFirst)467 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsCancelFirst) {
468   auto promise1 = std::promise<void>{};
469   auto future1 = promise1.get_future();
470   auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
471   auto future2 = promise2.get_future();
472 
473   // start a remote name request
474   remote_name_request_module_->StartRemoteNameRequest(
475       address1,
476       RemoteNameRequestBuilder::Create(
477           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
478       emptyCallback<ErrorCode>(),
479       impossibleCallback<uint64_t>(),
480       promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
481 
482   // enqueue a second one
483   remote_name_request_module_->StartRemoteNameRequest(
484       address2,
485       RemoteNameRequestBuilder::Create(
486           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
487       emptyCallback<ErrorCode>(),
488       impossibleCallback<uint64_t>(),
489       capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
490 
491   // acknowledge that the first one has started
492   test_hci_layer_->GetCommand();
493   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
494 
495   // cancel the first one
496   remote_name_request_module_->CancelRemoteNameRequest(address1);
497 
498   // let the cancel complete
499   test_hci_layer_->GetCommand();
500   test_hci_layer_->IncomingEvent(
501       RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
502   test_hci_layer_->IncomingEvent(RemoteNameRequestCompleteBuilder::Create(
503       ErrorCode::UNKNOWN_CONNECTION, address1, remote_name1));
504 
505   // verify that the second request has now started
506   auto command = test_hci_layer_->GetCommand();
507   auto discovery_command = DiscoveryCommandView::Create(command);
508   ASSERT_TRUE(discovery_command.IsValid());
509   auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
510   ASSERT_TRUE(rnr_command.IsValid());
511   EXPECT_EQ(rnr_command.GetBdAddr(), address2);
512 }
513 
TEST_F(RemoteNameRequestModuleTest,QueuingRemoteNameRequestsCancelFirstWithBuggyController)514 TEST_F(RemoteNameRequestModuleTest, QueuingRemoteNameRequestsCancelFirstWithBuggyController) {
515   auto promise1 = std::promise<void>{};
516   auto future1 = promise1.get_future();
517   auto promise2 = std::promise<std::tuple<ErrorCode, std::array<uint8_t, 248>>>{};
518   auto future2 = promise2.get_future();
519 
520   // start a remote name request
521   remote_name_request_module_->StartRemoteNameRequest(
522       address1,
523       RemoteNameRequestBuilder::Create(
524           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
525       emptyCallback<ErrorCode>(),
526       impossibleCallback<uint64_t>(),
527       promiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise1)));
528 
529   // enqueue a second one
530   remote_name_request_module_->StartRemoteNameRequest(
531       address2,
532       RemoteNameRequestBuilder::Create(
533           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
534       emptyCallback<ErrorCode>(),
535       impossibleCallback<uint64_t>(),
536       capturingPromiseCallback<ErrorCode, std::array<uint8_t, 248>>(std::move(promise2)));
537 
538   // acknowledge that the first one has started
539   test_hci_layer_->GetCommand();
540   test_hci_layer_->IncomingEvent(RemoteNameRequestStatusBuilder::Create(ErrorCode::SUCCESS, 1));
541 
542   // cancel the first one
543   remote_name_request_module_->CancelRemoteNameRequest(address1);
544 
545   // let the cancel complete
546   test_hci_layer_->GetCommand();
547   test_hci_layer_->IncomingEvent(
548       RemoteNameRequestCancelCompleteBuilder::Create(1, ErrorCode::SUCCESS, address1));
549   // send the INCORRECT response that we tolerate for interop reasons
550   remote_name_request_module_->ReportRemoteNameRequestCancellation(address1);
551 
552   // verify that the second request has now started
553   auto command = test_hci_layer_->GetCommand();
554   auto discovery_command = DiscoveryCommandView::Create(command);
555   ASSERT_TRUE(discovery_command.IsValid());
556   auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
557   ASSERT_TRUE(rnr_command.IsValid());
558   EXPECT_EQ(rnr_command.GetBdAddr(), address2);
559 }
560 
TEST_F(RemoteNameRequestModuleTest,FailToSendCommandThenSendNext)561 TEST_F(RemoteNameRequestModuleTest, FailToSendCommandThenSendNext) {
562   auto promise = std::promise<ErrorCode>{};
563   auto future = promise.get_future();
564 
565   // start a remote name request
566   remote_name_request_module_->StartRemoteNameRequest(
567       address1,
568       RemoteNameRequestBuilder::Create(
569           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
570       capturingPromiseCallback<ErrorCode>(std::move(promise)),
571       impossibleCallback<uint64_t>(),
572       impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
573   // on the command, return a failure HCI status
574   test_hci_layer_->GetCommand();
575   test_hci_layer_->IncomingEvent(
576       RemoteNameRequestStatusBuilder::Create(ErrorCode::STATUS_UNKNOWN, 1));
577 
578   // start a second request
579   remote_name_request_module_->StartRemoteNameRequest(
580       address2,
581       RemoteNameRequestBuilder::Create(
582           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
583       emptyCallback<ErrorCode>(),
584       impossibleCallback<uint64_t>(),
585       impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
586 
587   // verify that it started
588   auto command = test_hci_layer_->GetCommand();
589   auto discovery_command = DiscoveryCommandView::Create(command);
590   ASSERT_TRUE(discovery_command.IsValid());
591   auto rnr_command = RemoteNameRequestView::Create(DiscoveryCommandView::Create(discovery_command));
592   ASSERT_TRUE(rnr_command.IsValid());
593   EXPECT_EQ(rnr_command.GetBdAddr(), address2);
594 }
595 
TEST_F(RemoteNameRequestModuleTest,FailToSendCommandThenDequeueNext)596 TEST_F(RemoteNameRequestModuleTest, FailToSendCommandThenDequeueNext) {
597   auto promise = std::promise<ErrorCode>{};
598   auto future = promise.get_future();
599 
600   // start a remote name request
601   remote_name_request_module_->StartRemoteNameRequest(
602       address1,
603       RemoteNameRequestBuilder::Create(
604           address1, PageScanRepetitionMode::R0, 3, ClockOffsetValid::INVALID),
605       emptyCallback<ErrorCode>(),
606       impossibleCallback<uint64_t>(),
607       impossibleCallback<ErrorCode, std::array<uint8_t, 248>>());
608 
609   // enqueue a second one
610   remote_name_request_module_->StartRemoteNameRequest(
611       address2,
612       RemoteNameRequestBuilder::Create(
613           address2, PageScanRepetitionMode::R1, 4, ClockOffsetValid::VALID),
614       impossibleCallback<ErrorCode>(),
615       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 
632 }  // namespace
633 }  // namespace hci
634 }  // namespace bluetooth
635