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/hci_layer.h"
18 
19 #include <bluetooth/log.h>
20 #include <gtest/gtest.h>
21 
22 #include <chrono>
23 #include <future>
24 #include <memory>
25 
26 #include "common/bind.h"
27 #include "hal/hci_hal_fake.h"
28 #include "hci/address.h"
29 #include "hci/class_of_device.h"
30 #include "module.h"
31 #include "os/fake_timer/fake_timerfd.h"
32 #include "os/handler.h"
33 #include "os/system_properties.h"
34 #include "os/thread.h"
35 #include "packet/raw_builder.h"
36 
37 // TODO(b/369381361) Enfore -Wmissing-prototypes
38 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
39 
40 using namespace std::chrono_literals;
41 
42 namespace {
43 constexpr char kOurAclEventHandlerWasInvoked[] = "Our ACL event handler was invoked.";
44 constexpr char kOurCommandCompleteHandlerWasInvoked[] = "Our command complete handler was invoked.";
45 constexpr char kOurCommandStatusHandlerWasInvoked[] = "Our command status handler was invoked.";
46 constexpr char kOurDisconnectHandlerWasInvoked[] = "Our disconnect handler was invoked.";
47 constexpr char kOurEventHandlerWasInvoked[] = "Our event handler was invoked.";
48 constexpr char kOurLeAclEventHandlerWasInvoked[] = "Our LE ACL event handler was invoked.";
49 constexpr char kOurLeAdvertisementEventHandlerWasInvoked[] =
50         "Our LE advertisement event handler was invoked.";
51 constexpr char kOurLeDisconnectHandlerWasInvoked[] = "Our LE disconnect handler was invoked.";
52 constexpr char kOurLeEventHandlerWasInvoked[] = "Our LE event handler was invoked.";
53 constexpr char kOurLeIsoEventHandlerWasInvoked[] = "Our LE ISO event handler was invoked.";
54 constexpr char kOurLeReadRemoteVersionHandlerWasInvoked[] =
55         "Our Read Remote Version complete handler was invoked.";
56 constexpr char kOurLeScanningEventHandlerWasInvoked[] =
57         "Our LE scanning event handler was invoked.";
58 constexpr char kOurReadRemoteVersionHandlerWasInvoked[] =
59         "Our Read Remote Version complete handler was invoked.";
60 constexpr char kOurLeSecurityEventHandlerWasInvoked[] =
61         "Our LE security event handler was invoked.";
62 constexpr char kOurSecurityEventHandlerWasInvoked[] = "Our security event handler was invoked.";
63 }  // namespace
64 
65 namespace bluetooth {
66 namespace hci {
67 
68 using common::BidiQueue;
69 using common::BidiQueueEnd;
70 using os::fake_timer::fake_timerfd_advance;
71 using packet::kLittleEndian;
72 using packet::PacketView;
73 using packet::RawBuilder;
74 
GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet)75 std::vector<uint8_t> GetPacketBytes(std::unique_ptr<packet::BasePacketBuilder> packet) {
76   std::vector<uint8_t> bytes;
77   BitInserter i(bytes);
78   bytes.reserve(packet->size());
79   packet->Serialize(i);
80   return bytes;
81 }
82 
getHciTimeoutMs()83 static std::chrono::milliseconds getHciTimeoutMs() {
84   static auto sHciTimeoutMs = std::chrono::milliseconds(bluetooth::os::GetSystemPropertyUint32Base(
85           "bluetooth.hci.timeout_milliseconds", HciLayer::kHciTimeoutMs.count()));
86   return sHciTimeoutMs;
87 }
88 
getHciTimeoutRestartMs()89 static std::chrono::milliseconds getHciTimeoutRestartMs() {
90   static auto sRestartHciTimeoutMs = std::chrono::milliseconds(
91           bluetooth::os::GetSystemPropertyUint32Base("bluetooth.hci.restart_timeout_milliseconds",
92                                                      HciLayer::kHciTimeoutRestartMs.count()));
93   return sRestartHciTimeoutMs;
94 }
95 
96 class HciLayerTest : public ::testing::Test {
97 protected:
SetUp()98   void SetUp() override {
99     hal_ = new hal::TestHciHal();
100     fake_registry_.InjectTestModule(&hal::HciHal::Factory, hal_);
101     fake_registry_.Start<HciLayer>(&fake_registry_.GetTestThread());
102     hci_ = static_cast<HciLayer*>(fake_registry_.GetModuleUnderTest(&HciLayer::Factory));
103     hci_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
104     ASSERT_TRUE(fake_registry_.IsStarted<HciLayer>());
105     ::testing::FLAGS_gtest_death_test_style = "threadsafe";
106     sync_handler();
107   }
108 
TearDown()109   void TearDown() override {
110     fake_registry_.SynchronizeModuleHandler(&HciLayer::Factory, std::chrono::milliseconds(20));
111     fake_registry_.StopAll();
112   }
113 
FakeTimerAdvance(uint64_t ms)114   void FakeTimerAdvance(uint64_t ms) {
115     hci_handler_->Post(common::BindOnce(fake_timerfd_advance, ms));
116   }
117 
FailIfResetNotSent()118   void FailIfResetNotSent() {
119     (hci_handler_->BindOnceOn(this, &HciLayerTest::fail_if_reset_not_sent))();
120     sync_handler();
121   }
122 
fail_if_reset_not_sent()123   void fail_if_reset_not_sent() {
124     auto sent_command = hal_->GetSentCommand();
125     ASSERT_TRUE(sent_command.has_value());
126     auto reset_view = ResetView::Create(CommandView::Create(*sent_command));
127     ASSERT_TRUE(reset_view.IsValid());
128   }
129 
sync_handler()130   void sync_handler() {
131     log::assert_that(fake_registry_.GetTestThread().GetReactor()->WaitForIdle(2s),
132                      "assert failed: fake_registry_.GetTestThread().GetReactor()->WaitForIdle(2s)");
133   }
134 
135   hal::TestHciHal* hal_ = nullptr;
136   HciLayer* hci_ = nullptr;
137   os::Handler* hci_handler_ = nullptr;
138   TestModuleRegistry fake_registry_;
139 };
140 
141 class HciLayerDeathTest : public HciLayerTest {};
142 
TEST_F(HciLayerTest,setup_teardown)143 TEST_F(HciLayerTest, setup_teardown) {}
144 
TEST_F(HciLayerTest,reset_command_sent_on_start)145 TEST_F(HciLayerTest, reset_command_sent_on_start) { FailIfResetNotSent(); }
146 
TEST_F(HciLayerTest,controller_debug_info_requested_on_hci_timeout)147 TEST_F(HciLayerTest, controller_debug_info_requested_on_hci_timeout) {
148   FailIfResetNotSent();
149   FakeTimerAdvance(getHciTimeoutMs().count());
150 
151   sync_handler();
152 
153   auto sent_command = hal_->GetSentCommand();
154   ASSERT_TRUE(sent_command.has_value());
155   auto debug_info_view = ControllerDebugInfoView::Create(VendorCommandView::Create(*sent_command));
156   ASSERT_TRUE(debug_info_view.IsValid());
157 }
158 
TEST_F(HciLayerDeathTest,abort_after_hci_restart_timeout)159 TEST_F(HciLayerDeathTest, abort_after_hci_restart_timeout) {
160   FailIfResetNotSent();
161   FakeTimerAdvance(getHciTimeoutMs().count());
162 
163   auto sent_command = hal_->GetSentCommand();
164   ASSERT_TRUE(sent_command.has_value());
165   auto debug_info_view = ControllerDebugInfoView::Create(VendorCommandView::Create(*sent_command));
166   ASSERT_TRUE(debug_info_view.IsValid());
167 
168   ASSERT_DEATH(
169           {
170             sync_handler();
171             FakeTimerAdvance(getHciTimeoutRestartMs().count());
172             sync_handler();
173           },
174           "");
175 }
176 
TEST_F(HciLayerDeathTest,discard_event_after_hci_timeout)177 TEST_F(HciLayerDeathTest, discard_event_after_hci_timeout) {
178   FailIfResetNotSent();
179   FakeTimerAdvance(getHciTimeoutMs().count());
180 
181   auto sent_command = hal_->GetSentCommand();
182   ASSERT_TRUE(sent_command.has_value());
183   auto debug_info_view = ControllerDebugInfoView::Create(VendorCommandView::Create(*sent_command));
184   ASSERT_TRUE(debug_info_view.IsValid());
185 
186   // This event should be discarded, not cause an abort.
187   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
188   sync_handler();
189 
190   ASSERT_DEATH(
191           {
192             FakeTimerAdvance(getHciTimeoutRestartMs().count());
193             sync_handler();
194           },
195           "");
196 }
197 
TEST_F(HciLayerDeathTest,abort_on_root_inflammation_event)198 TEST_F(HciLayerDeathTest, abort_on_root_inflammation_event) {
199   FailIfResetNotSent();
200 
201   ASSERT_DEATH(
202           {
203             sync_handler();
204             hal_->InjectEvent(BqrRootInflammationEventBuilder::Create(
205                     0x01, 0x01, std::make_unique<packet::RawBuilder>()));
206             FakeTimerAdvance(getHciTimeoutRestartMs().count());
207             sync_handler();
208           },
209           "");
210 }
211 
TEST_F(HciLayerDeathTest,abort_on_hardware_error)212 TEST_F(HciLayerDeathTest, abort_on_hardware_error) {
213   FailIfResetNotSent();
214 
215   ASSERT_DEATH(
216           {
217             sync_handler();
218             hal_->InjectEvent(HardwareErrorBuilder::Create(0xbb));
219             sync_handler();
220           },
221           "");
222 }
223 
TEST_F(HciLayerTest,successful_reset)224 TEST_F(HciLayerTest, successful_reset) {
225   FailIfResetNotSent();
226   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
227   sync_handler();
228 }
229 
TEST_F(HciLayerDeathTest,abort_if_reset_complete_returns_error)230 TEST_F(HciLayerDeathTest, abort_if_reset_complete_returns_error) {
231   FailIfResetNotSent();
232   ASSERT_DEATH(
233           {
234             hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::HARDWARE_FAILURE));
235             sync_handler();
236           },
237           "");
238 }
239 
TEST_F(HciLayerTest,event_handler_is_invoked)240 TEST_F(HciLayerTest, event_handler_is_invoked) {
241   FailIfResetNotSent();
242   hci_->RegisterEventHandler(EventCode::COMMAND_COMPLETE,
243                              hci_handler_->Bind([](EventView /* view */) {
244                                log::debug("{}", kOurEventHandlerWasInvoked);
245                              }));
246   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
247 }
248 
TEST_F(HciLayerTest,le_event_handler_is_invoked)249 TEST_F(HciLayerTest, le_event_handler_is_invoked) {
250   FailIfResetNotSent();
251   hci_->RegisterLeEventHandler(SubeventCode::ENHANCED_CONNECTION_COMPLETE,
252                                hci_handler_->Bind([](LeMetaEventView /* view */) {
253                                  log::debug("{}", kOurLeEventHandlerWasInvoked);
254                                }));
255   hci::Address remote_address;
256   Address::FromString("D0:05:04:03:02:01", remote_address);
257   hal_->InjectEvent(LeEnhancedConnectionCompleteBuilder::Create(
258           ErrorCode::SUCCESS, 0x0041, Role::PERIPHERAL, AddressType::PUBLIC_DEVICE_ADDRESS,
259           remote_address, Address::kEmpty, Address::kEmpty, 0x0024, 0x0000, 0x0011,
260           ClockAccuracy::PPM_30));
261 }
262 
TEST_F(HciLayerDeathTest,abort_on_second_register_event_handler)263 TEST_F(HciLayerDeathTest, abort_on_second_register_event_handler) {
264   FailIfResetNotSent();
265   ASSERT_DEATH(
266           {
267             hci_->RegisterEventHandler(EventCode::SIMPLE_PAIRING_COMPLETE,
268                                        hci_handler_->Bind([](EventView /* view */) {}));
269             hci_->RegisterEventHandler(EventCode::SIMPLE_PAIRING_COMPLETE,
270                                        hci_handler_->Bind([](EventView /* view */) {}));
271             sync_handler();
272           },
273           "");
274 }
275 
TEST_F(HciLayerDeathTest,abort_on_second_register_le_event_handler)276 TEST_F(HciLayerDeathTest, abort_on_second_register_le_event_handler) {
277   ASSERT_DEATH(
278           {
279             FailIfResetNotSent();
280             hci_->RegisterLeEventHandler(SubeventCode::ENHANCED_CONNECTION_COMPLETE,
281                                          hci_handler_->Bind([](LeMetaEventView /* view */) {}));
282             hci_->RegisterLeEventHandler(SubeventCode::ENHANCED_CONNECTION_COMPLETE,
283                                          hci_handler_->Bind([](LeMetaEventView /* view */) {}));
284             sync_handler();
285           },
286           "");
287 }
288 
TEST_F(HciLayerTest,our_acl_event_callback_is_invoked)289 TEST_F(HciLayerTest, our_acl_event_callback_is_invoked) {
290   FailIfResetNotSent();
291   hci_->GetAclConnectionInterface(
292           hci_handler_->Bind(
293                   [](EventView /* view */) { log::debug("{}", kOurAclEventHandlerWasInvoked); }),
294           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {}),
295           hci_handler_->Bind([](Address /* bd_addr */, ClassOfDevice /* cod */) {}),
296           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
297                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
298                                 uint16_t /* sub_version */) {}));
299   hal_->InjectEvent(ReadClockOffsetCompleteBuilder::Create(ErrorCode::SUCCESS, 0x0001, 0x0123));
300 }
301 
TEST_F(HciLayerTest,our_disconnect_callback_is_invoked)302 TEST_F(HciLayerTest, our_disconnect_callback_is_invoked) {
303   FailIfResetNotSent();
304   hci_->GetAclConnectionInterface(
305           hci_handler_->Bind([](EventView /* view */) {}),
306           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {
307             log::debug("{}", kOurDisconnectHandlerWasInvoked);
308           }),
309           hci_handler_->Bind([](Address /* bd_addr */, ClassOfDevice /* cod */) {}),
310           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
311                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
312                                 uint16_t /* sub_version */) {}));
313   hal_->InjectEvent(DisconnectionCompleteBuilder::Create(
314           ErrorCode::SUCCESS, 0x0001, ErrorCode::REMOTE_USER_TERMINATED_CONNECTION));
315 }
316 
TEST_F(HciLayerTest,our_read_remote_version_callback_is_invoked)317 TEST_F(HciLayerTest, our_read_remote_version_callback_is_invoked) {
318   FailIfResetNotSent();
319   hci_->GetAclConnectionInterface(
320           hci_handler_->Bind([](EventView /* view */) {}),
321           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {}),
322           hci_handler_->Bind([](Address /* bd_addr */, ClassOfDevice /* cod */) {}),
323           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
324                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
325                                 uint16_t /* sub_version */) {
326             log::debug("{}", kOurReadRemoteVersionHandlerWasInvoked);
327           }));
328   hal_->InjectEvent(ReadRemoteVersionInformationCompleteBuilder::Create(ErrorCode::SUCCESS, 0x0001,
329                                                                         0x0b, 0x000f, 0x0000));
330 }
331 
TEST_F(HciLayerTest,our_le_acl_event_callback_is_invoked)332 TEST_F(HciLayerTest, our_le_acl_event_callback_is_invoked) {
333   FailIfResetNotSent();
334   hci_->GetLeAclConnectionInterface(
335           hci_handler_->Bind([](LeMetaEventView /* view */) {
336             log::debug("{}", kOurLeAclEventHandlerWasInvoked);
337           }),
338           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {}),
339           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
340                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
341                                 uint16_t /* sub_version */) {}));
342   hal_->InjectEvent(LeDataLengthChangeBuilder::Create(0x0001, 0x001B, 0x0148, 0x001B, 0x0148));
343 }
344 
TEST_F(HciLayerTest,our_le_disconnect_callback_is_invoked)345 TEST_F(HciLayerTest, our_le_disconnect_callback_is_invoked) {
346   FailIfResetNotSent();
347   hci_->GetLeAclConnectionInterface(
348           hci_handler_->Bind([](LeMetaEventView /* view */) {}),
349           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {
350             log::debug("{}", kOurLeDisconnectHandlerWasInvoked);
351           }),
352           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
353                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
354                                 uint16_t /* sub_version */) {}));
355   hal_->InjectEvent(DisconnectionCompleteBuilder::Create(
356           ErrorCode::SUCCESS, 0x0001, ErrorCode::REMOTE_USER_TERMINATED_CONNECTION));
357 }
358 
TEST_F(HciLayerTest,our_le_read_remote_version_callback_is_invoked)359 TEST_F(HciLayerTest, our_le_read_remote_version_callback_is_invoked) {
360   FailIfResetNotSent();
361   hci_->GetLeAclConnectionInterface(
362           hci_handler_->Bind([](LeMetaEventView /* view */) {}),
363           hci_handler_->Bind([](uint16_t /* handle */, ErrorCode /* reason */) {}),
364           hci_handler_->Bind([](hci::ErrorCode /* hci_status */, uint16_t /* handle */,
365                                 uint8_t /* version */, uint16_t /* manufacturer_name */,
366                                 uint16_t /* sub_version */) {
367             log::debug("{}", kOurLeReadRemoteVersionHandlerWasInvoked);
368           }));
369   hal_->InjectEvent(ReadRemoteVersionInformationCompleteBuilder::Create(ErrorCode::SUCCESS, 0x0001,
370                                                                         0x0b, 0x000f, 0x0000));
371 }
372 
TEST_F(HciLayerTest,our_security_callback_is_invoked)373 TEST_F(HciLayerTest, our_security_callback_is_invoked) {
374   FailIfResetNotSent();
375   hci_->GetSecurityInterface(hci_handler_->Bind(
376           [](EventView /* view */) { log::debug("{}", kOurSecurityEventHandlerWasInvoked); }));
377   hal_->InjectEvent(EncryptionChangeBuilder::Create(ErrorCode::SUCCESS, 0x0001,
378                                                     bluetooth::hci::EncryptionEnabled::ON));
379 }
380 
TEST_F(HciLayerTest,our_le_security_callback_is_invoked)381 TEST_F(HciLayerTest, our_le_security_callback_is_invoked) {
382   FailIfResetNotSent();
383   hci_->GetLeSecurityInterface(hci_handler_->Bind([](LeMetaEventView /* view */) {
384     log::debug("{}", kOurLeSecurityEventHandlerWasInvoked);
385   }));
386   hal_->InjectEvent(LeLongTermKeyRequestBuilder::Create(0x0001, {0, 0, 0, 0, 0, 0, 0, 0}, 0));
387 }
388 
TEST_F(HciLayerTest,our_le_advertising_callback_is_invoked)389 TEST_F(HciLayerTest, our_le_advertising_callback_is_invoked) {
390   FailIfResetNotSent();
391   hci_->GetLeAdvertisingInterface(hci_handler_->Bind([](LeMetaEventView /* view */) {
392     log::debug("{}", kOurLeAdvertisementEventHandlerWasInvoked);
393   }));
394   hal_->InjectEvent(
395           LeAdvertisingSetTerminatedBuilder::Create(ErrorCode::SUCCESS, 0x01, 0x001, 0x01));
396 }
397 
TEST_F(HciLayerTest,our_le_scanning_callback_is_invoked)398 TEST_F(HciLayerTest, our_le_scanning_callback_is_invoked) {
399   FailIfResetNotSent();
400   hci_->GetLeScanningInterface(hci_handler_->Bind([](LeMetaEventView /* view */) {
401     log::debug("{}", kOurLeScanningEventHandlerWasInvoked);
402   }));
403   hal_->InjectEvent(LeScanTimeoutBuilder::Create());
404 }
405 
TEST_F(HciLayerTest,our_le_iso_callback_is_invoked)406 TEST_F(HciLayerTest, our_le_iso_callback_is_invoked) {
407   FailIfResetNotSent();
408   hci_->GetLeIsoInterface(hci_handler_->Bind(
409           [](LeMetaEventView /* view */) { log::debug("{}", kOurLeIsoEventHandlerWasInvoked); }));
410   hal_->InjectEvent(LeCisRequestBuilder::Create(0x0001, 0x0001, 0x01, 0x01));
411 }
412 
TEST_F(HciLayerTest,our_command_complete_callback_is_invoked)413 TEST_F(HciLayerTest, our_command_complete_callback_is_invoked) {
414   FailIfResetNotSent();
415   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
416   hci_->EnqueueCommand(ResetBuilder::Create(),
417                        hci_handler_->BindOnce([](CommandCompleteView /* view */) {
418                          log::debug("{}", kOurCommandCompleteHandlerWasInvoked);
419                        }));
420   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
421 }
422 
TEST_F(HciLayerTest,our_command_status_callback_is_invoked)423 TEST_F(HciLayerTest, our_command_status_callback_is_invoked) {
424   FailIfResetNotSent();
425   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
426   hci_->EnqueueCommand(ReadClockOffsetBuilder::Create(0x001),
427                        hci_handler_->BindOnce([](CommandStatusView /* view */) {
428                          log::debug("{}", kOurCommandStatusHandlerWasInvoked);
429                        }));
430   hal_->InjectEvent(ReadClockOffsetStatusBuilder::Create(ErrorCode::SUCCESS, 1));
431 }
432 
TEST_F(HciLayerTest,vendor_specific_status_instead_of_complete)433 TEST_F(HciLayerTest, vendor_specific_status_instead_of_complete) {
434   std::promise<OpCode> callback_promise;
435   auto callback_future = callback_promise.get_future();
436   FailIfResetNotSent();
437   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
438   hci_->EnqueueCommand(LeGetVendorCapabilitiesBuilder::Create(),
439                        hci_handler_->BindOnce(
440                                [](std::promise<OpCode> promise, CommandCompleteView view) {
441                                  ASSERT_TRUE(view.IsValid());
442                                  promise.set_value(view.GetCommandOpCode());
443                                },
444                                std::move(callback_promise)));
445   hal_->InjectEvent(CommandStatusBuilder::Create(ErrorCode::UNKNOWN_HCI_COMMAND, 1,
446                                                  OpCode::LE_GET_VENDOR_CAPABILITIES,
447                                                  std::make_unique<RawBuilder>()));
448 
449   ASSERT_EQ(std::future_status::ready, callback_future.wait_for(std::chrono::seconds(1)));
450   ASSERT_EQ(OpCode::LE_GET_VENDOR_CAPABILITIES, callback_future.get());
451 }
452 
TEST_F(HciLayerDeathTest,command_complete_callback_is_invoked_with_an_opcode_that_does_not_match_command_queue)453 TEST_F(HciLayerDeathTest,
454        command_complete_callback_is_invoked_with_an_opcode_that_does_not_match_command_queue) {
455   ASSERT_DEATH(
456           {
457             FailIfResetNotSent();
458             hci_->EnqueueCommand(ReadClockOffsetBuilder::Create(0x001),
459                                  hci_handler_->BindOnce([](CommandCompleteView /* view */) {}));
460             hal_->InjectEvent(ReadClockOffsetStatusBuilder::Create(ErrorCode::SUCCESS, 1));
461             sync_handler();
462           },
463           "");
464 }
465 
TEST_F(HciLayerDeathTest,command_status_callback_is_invoked_with_an_opcode_that_does_not_match_command_queue)466 TEST_F(HciLayerDeathTest,
467        command_status_callback_is_invoked_with_an_opcode_that_does_not_match_command_queue) {
468   ASSERT_DEATH(
469           {
470             FailIfResetNotSent();
471             hci_->EnqueueCommand(ReadClockOffsetBuilder::Create(0x001),
472                                  hci_handler_->BindOnce([](CommandStatusView /* view */) {}));
473             hal_->InjectEvent(ReadClockOffsetStatusBuilder::Create(ErrorCode::SUCCESS, 1));
474             sync_handler();
475           },
476           "");
477 }
478 
TEST_F(HciLayerDeathTest,command_complete_callback_is_invoked_but_command_queue_empty)479 TEST_F(HciLayerDeathTest, command_complete_callback_is_invoked_but_command_queue_empty) {
480   ASSERT_DEATH(
481           {
482             FailIfResetNotSent();
483             hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
484             hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
485             sync_handler();
486           },
487           "");
488 }
489 
TEST_F(HciLayerDeathTest,command_status_callback_is_invoked_but_command_queue_empty)490 TEST_F(HciLayerDeathTest, command_status_callback_is_invoked_but_command_queue_empty) {
491   ASSERT_DEATH(
492           {
493             FailIfResetNotSent();
494             hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
495             hal_->InjectEvent(ReadClockOffsetStatusBuilder::Create(ErrorCode::SUCCESS, 1));
496             sync_handler();
497           },
498           "");
499 }
500 
TEST_F(HciLayerTest,command_status_callback_is_invoked_with_failure_status)501 TEST_F(HciLayerTest, command_status_callback_is_invoked_with_failure_status) {
502   FailIfResetNotSent();
503   hal_->InjectEvent(ResetCompleteBuilder::Create(1, ErrorCode::SUCCESS));
504   hci_->EnqueueCommand(ReadClockOffsetBuilder::Create(0x001),
505                        hci_handler_->BindOnce([](CommandStatusView /* view */) {}));
506   hal_->InjectEvent(ReadClockOffsetStatusBuilder::Create(ErrorCode::HARDWARE_FAILURE, 1));
507   sync_handler();
508 }
509 
510 }  // namespace hci
511 }  // namespace bluetooth
512