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