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