1 /*
2 * Copyright 2021 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <sys/socket.h>
20
21 #include <future>
22 #include <map>
23 #include <memory>
24 #include <string>
25
26 #include "bta/include/bta_ag_api.h"
27 #include "bta/include/bta_av_api.h"
28 #include "bta/include/bta_hd_api.h"
29 #include "bta/include/bta_hf_client_api.h"
30 #include "bta/include/bta_hh_api.h"
31 #include "btcore/include/module.h"
32 #include "btif/include/btif_api.h"
33 #include "btif/include/btif_common.h"
34 #include "btif/include/btif_util.h"
35 #include "btif_bqr.h"
36 #include "btif_jni_task.h"
37 #include "btm_api_types.h"
38 #include "common/bind.h"
39 #include "common/contextual_callback.h"
40 #include "common/postable_context.h"
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
43 #include "hci/controller_interface_mock.h"
44 #include "hci/hci_layer_mock.h"
45 #include "include/hardware/bluetooth.h"
46 #include "include/hardware/bt_av.h"
47 #include "main/shim/entry.h"
48 #include "main_thread.h"
49 #include "packet/base_packet_builder.h"
50 #include "packet/bit_inserter.h"
51 #include "packet/packet_view.h"
52 #include "packet/raw_builder.h"
53 #include "test/common/core_interface.h"
54 #include "test/mock/mock_main_shim_entry.h"
55 #include "test/mock/mock_osi_properties.h"
56 #include "test/mock/mock_stack_btm_sec.h"
57 #include "types/raw_address.h"
58
59 namespace bluetooth::testing {
60 void set_hal_cbacks(bt_callbacks_t* callbacks);
61 } // namespace bluetooth::testing
62
63 namespace bluetooth::legacy::testing {
64 void bta_dm_acl_down(const RawAddress& bd_addr, tBT_TRANSPORT transport);
65 void bta_dm_acl_up(const RawAddress& bd_addr, tBT_TRANSPORT transport,
66 uint16_t acl_handle);
67 } // namespace bluetooth::legacy::testing
68
69 const tBTA_AG_RES_DATA tBTA_AG_RES_DATA::kEmpty = {};
70
71 using bluetooth::common::BindOnce;
72 using bluetooth::common::ContextualCallback;
73 using bluetooth::common::ContextualOnceCallback;
74 using bluetooth::common::PostableContext;
75 using bluetooth::hci::BqrA2dpAudioChoppyEventBuilder;
76 using bluetooth::hci::BqrEventBuilder;
77 using bluetooth::hci::BqrLinkQualityEventBuilder;
78 using bluetooth::hci::BqrLmpLlMessageTraceEventBuilder;
79 using bluetooth::hci::BqrLogDumpEventBuilder;
80 using bluetooth::hci::BqrPacketType;
81 using bluetooth::hci::CommandBuilder;
82 using bluetooth::hci::CommandCompleteBuilder;
83 using bluetooth::hci::CommandCompleteView;
84 using bluetooth::hci::CommandStatusBuilder;
85 using bluetooth::hci::CommandStatusView;
86 using bluetooth::hci::CommandView;
87 using bluetooth::hci::ControllerBqrBuilder;
88 using bluetooth::hci::ControllerBqrCompleteBuilder;
89 using bluetooth::hci::ControllerBqrCompleteView;
90 using bluetooth::hci::ControllerBqrView;
91 using bluetooth::hci::ErrorCode;
92 using bluetooth::hci::EventView;
93 using bluetooth::hci::QualityReportId;
94 using bluetooth::hci::Role;
95 using bluetooth::hci::VendorCommandView;
96 using bluetooth::hci::VendorSpecificEventView;
97 using bluetooth::hci::VseSubeventCode;
98 using bluetooth::packet::BasePacketBuilder;
99 using bluetooth::packet::BitInserter;
100 using bluetooth::packet::kLittleEndian;
101 using bluetooth::packet::PacketView;
102 using bluetooth::packet::RawBuilder;
103 using testing::_;
104 using testing::DoAll;
105 using testing::Invoke;
106 using testing::Matcher;
107 using testing::Return;
108 using testing::SaveArg;
109
110 module_t bt_utils_module;
111 module_t gd_controller_module;
112 module_t gd_shim_module;
113 module_t osi_module;
114 module_t rust_module;
115
116 namespace {
117
BuilderToView(std::unique_ptr<BasePacketBuilder> builder)118 PacketView<kLittleEndian> BuilderToView(
119 std::unique_ptr<BasePacketBuilder> builder) {
120 std::shared_ptr<std::vector<uint8_t>> packet_bytes =
121 std::make_shared<std::vector<uint8_t>>();
122 BitInserter it(*packet_bytes);
123 builder->Serialize(it);
124 return PacketView<kLittleEndian>(packet_bytes);
125 }
126
127 const RawAddress kRawAddress({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
128 const uint16_t kHciHandle = 123;
129
130 auto timeout_time = std::chrono::seconds(3);
131
132 std::map<std::string, std::function<void()>> callback_map_;
133 #define TESTCB \
134 if (callback_map_.find(__func__) != callback_map_.end()) \
135 callback_map_[__func__]();
136
adapter_state_changed_callback(bt_state_t)137 void adapter_state_changed_callback(bt_state_t /* state */) {}
adapter_properties_callback(bt_status_t,int,bt_property_t *)138 void adapter_properties_callback(bt_status_t /* status */,
139 int /* num_properties */,
140 bt_property_t* /* properties */) {}
remote_device_properties_callback(bt_status_t,RawAddress *,int,bt_property_t *)141 void remote_device_properties_callback(bt_status_t /* status */,
142 RawAddress* /* bd_addr */,
143 int /* num_properties */,
144 bt_property_t* /* properties */) {}
device_found_callback(int,bt_property_t *)145 void device_found_callback(int /* num_properties */,
146 bt_property_t* /* properties */) {}
discovery_state_changed_callback(bt_discovery_state_t)147 void discovery_state_changed_callback(bt_discovery_state_t /* state */) {}
pin_request_callback(RawAddress *,bt_bdname_t *,uint32_t,bool)148 void pin_request_callback(RawAddress* /* remote_bd_addr */,
149 bt_bdname_t* /* bd_name */, uint32_t /* cod */,
150 bool /* min_16_digit */) {}
ssp_request_callback(RawAddress *,bt_ssp_variant_t,uint32_t)151 void ssp_request_callback(RawAddress* /* remote_bd_addr */,
152 bt_ssp_variant_t /* pairing_variant */,
153 uint32_t /* pass_key */) {}
bond_state_changed_callback(bt_status_t,RawAddress *,bt_bond_state_t,int)154 void bond_state_changed_callback(bt_status_t /* status */,
155 RawAddress* /* remote_bd_addr */,
156 bt_bond_state_t /* state */,
157 int /* fail_reason */) {}
address_consolidate_callback(RawAddress *,RawAddress *)158 void address_consolidate_callback(RawAddress* /* main_bd_addr */,
159 RawAddress* /* secondary_bd_addr */) {}
le_address_associate_callback(RawAddress *,RawAddress *)160 void le_address_associate_callback(RawAddress* /* main_bd_addr */,
161 RawAddress* /* secondary_bd_addr */) {}
acl_state_changed_callback(bt_status_t,RawAddress *,bt_acl_state_t,int,bt_hci_error_code_t,bt_conn_direction_t,uint16_t)162 void acl_state_changed_callback(bt_status_t /* status */,
163 RawAddress* /* remote_bd_addr */,
164 bt_acl_state_t /* state */,
165 int /* transport_link_type */,
166 bt_hci_error_code_t /* hci_reason */,
167 bt_conn_direction_t /* direction */,
168 uint16_t /* acl_handle */) {}
link_quality_report_callback(uint64_t,int,int,int,int,int,int)169 void link_quality_report_callback(uint64_t /* timestamp */, int /* report_id */,
170 int /* rssi */, int /* snr */,
171 int /* retransmission_count */,
172 int /* packets_not_receive_count */,
173 int /* negative_acknowledgement_count */) {
174 TESTCB;
175 }
callback_thread_event(bt_cb_thread_evt)176 void callback_thread_event(bt_cb_thread_evt /* evt */) { TESTCB; }
dut_mode_recv_callback(uint16_t,uint8_t *,uint8_t)177 void dut_mode_recv_callback(uint16_t /* opcode */, uint8_t* /* buf */,
178 uint8_t /* len */) {}
le_test_mode_callback(bt_status_t,uint16_t)179 void le_test_mode_callback(bt_status_t /* status */,
180 uint16_t /* num_packets */) {}
energy_info_callback(bt_activity_energy_info *,bt_uid_traffic_t *)181 void energy_info_callback(bt_activity_energy_info* /* energy_info */,
182 bt_uid_traffic_t* /* uid_data */) {}
generate_local_oob_data_callback(tBT_TRANSPORT,bt_oob_data_t)183 void generate_local_oob_data_callback(tBT_TRANSPORT /* transport */,
184 bt_oob_data_t /* oob_data */) {}
switch_buffer_size_callback(bool)185 void switch_buffer_size_callback(bool /* is_low_latency_buffer_size */) {}
switch_codec_callback(bool)186 void switch_codec_callback(bool /* is_low_latency_buffer_size */) {}
le_rand_callback(uint64_t)187 void le_rand_callback(uint64_t /* random */) {}
key_missing_callback(const RawAddress)188 void key_missing_callback(const RawAddress /* bd_addr */) {}
189 #undef TESTCB
190
191 bt_callbacks_t callbacks = {
192 .size = sizeof(bt_callbacks_t),
193 .adapter_state_changed_cb = adapter_state_changed_callback,
194 .adapter_properties_cb = adapter_properties_callback,
195 .remote_device_properties_cb = remote_device_properties_callback,
196 .device_found_cb = device_found_callback,
197 .discovery_state_changed_cb = discovery_state_changed_callback,
198 .pin_request_cb = pin_request_callback,
199 .ssp_request_cb = ssp_request_callback,
200 .bond_state_changed_cb = bond_state_changed_callback,
201 .address_consolidate_cb = address_consolidate_callback,
202 .le_address_associate_cb = le_address_associate_callback,
203 .acl_state_changed_cb = acl_state_changed_callback,
204 .thread_evt_cb = callback_thread_event,
205 .dut_mode_recv_cb = dut_mode_recv_callback,
206 .le_test_mode_cb = le_test_mode_callback,
207 .energy_info_cb = energy_info_callback,
208 .link_quality_report_cb = link_quality_report_callback,
209 .generate_local_oob_data_cb = generate_local_oob_data_callback,
210 .switch_buffer_size_cb = switch_buffer_size_callback,
211 .switch_codec_cb = switch_codec_callback,
212 .le_rand_cb = le_rand_callback,
213 .key_missing_cb = key_missing_callback,
214 };
215
216 } // namespace
217
218 class BtifUtilsTest : public ::testing::Test {};
219
220 class BtifCoreTest : public ::testing::Test {
221 protected:
SetUp()222 void SetUp() override {
223 callback_map_.clear();
224 bluetooth::hci::testing::mock_controller_ = &controller_;
225 bluetooth::testing::set_hal_cbacks(&callbacks);
226 auto promise = std::promise<void>();
227 auto future = promise.get_future();
228 callback_map_["callback_thread_event"] = [&promise]() {
229 promise.set_value();
230 };
231 InitializeCoreInterface();
232 ASSERT_EQ(std::future_status::ready, future.wait_for(timeout_time));
233 callback_map_.erase("callback_thread_event");
234 }
235
TearDown()236 void TearDown() override {
237 auto promise = std::promise<void>();
238 auto future = promise.get_future();
239 callback_map_["callback_thread_event"] = [&promise]() {
240 promise.set_value();
241 };
242 CleanCoreInterface();
243 ASSERT_EQ(std::future_status::ready, future.wait_for(timeout_time));
244 bluetooth::hci::testing::mock_controller_ = nullptr;
245 callback_map_.erase("callback_thread_event");
246 }
247 bluetooth::hci::testing::MockControllerInterface controller_;
248 };
249
250 class BtifCoreWithControllerTest : public BtifCoreTest {
251 protected:
SetUp()252 void SetUp() override {
253 BtifCoreTest::SetUp();
254 ON_CALL(controller_, SupportsSniffSubrating).WillByDefault(Return(true));
255 }
256
TearDown()257 void TearDown() override { BtifCoreTest::TearDown(); }
258 };
259
260 class BtifCoreWithConnectionTest : public BtifCoreWithControllerTest {
261 protected:
SetUp()262 void SetUp() override {
263 BtifCoreWithControllerTest::SetUp();
264 bluetooth::legacy::testing::bta_dm_acl_up(kRawAddress, BT_TRANSPORT_AUTO,
265 kHciHandle);
266 }
267
TearDown()268 void TearDown() override {
269 bluetooth::legacy::testing::bta_dm_acl_down(kRawAddress, BT_TRANSPORT_AUTO);
270 BtifCoreWithControllerTest::TearDown();
271 }
272 };
273
274 std::promise<int> promise0;
callback0(int val)275 void callback0(int val) { promise0.set_value(val); }
276
TEST_F(BtifCoreTest,test_nop)277 TEST_F(BtifCoreTest, test_nop) {}
278
TEST_F(BtifCoreTest,test_post_on_bt_simple0)279 TEST_F(BtifCoreTest, test_post_on_bt_simple0) {
280 const int val = kHciHandle;
281 promise0 = std::promise<int>();
282 std::future<int> future0 = promise0.get_future();
283 post_on_bt_jni([=]() { callback0(val); });
284 ASSERT_EQ(std::future_status::ready, future0.wait_for(timeout_time));
285 ASSERT_EQ(val, future0.get());
286 }
287
TEST_F(BtifCoreTest,test_post_on_bt_jni_simple1)288 TEST_F(BtifCoreTest, test_post_on_bt_jni_simple1) {
289 std::promise<void> promise;
290 std::future<void> future = promise.get_future();
291 post_on_bt_jni([=, &promise]() { promise.set_value(); });
292 ASSERT_EQ(std::future_status::ready, future.wait_for(timeout_time));
293 }
294
TEST_F(BtifCoreTest,test_post_on_bt_jni_simple2)295 TEST_F(BtifCoreTest, test_post_on_bt_jni_simple2) {
296 std::promise<void> promise;
297 std::future<void> future = promise.get_future();
298 BtJniClosure closure = [&promise]() { promise.set_value(); };
299 post_on_bt_jni(closure);
300 ASSERT_EQ(std::future_status::ready, future.wait_for(timeout_time));
301 }
302
TEST_F(BtifCoreTest,test_post_on_bt_jni_simple3)303 TEST_F(BtifCoreTest, test_post_on_bt_jni_simple3) {
304 const int val = 456;
305 std::promise<int> promise;
306 auto future = promise.get_future();
307 BtJniClosure closure = [&promise, val]() { promise.set_value(val); };
308 post_on_bt_jni(closure);
309 ASSERT_EQ(std::future_status::ready, future.wait_for(timeout_time));
310 ASSERT_EQ(val, future.get());
311 }
312
313 extern const char* dump_av_sm_event_name(int event);
TEST_F(BtifUtilsTest,dump_av_sm_event_name)314 TEST_F(BtifUtilsTest, dump_av_sm_event_name) {
315 std::vector<std::pair<int, std::string>> events = {
316 std::make_pair(BTA_AV_ENABLE_EVT, "BTA_AV_ENABLE_EVT"),
317 std::make_pair(BTA_AV_REGISTER_EVT, "BTA_AV_REGISTER_EVT"),
318 std::make_pair(BTA_AV_OPEN_EVT, "BTA_AV_OPEN_EVT"),
319 std::make_pair(BTA_AV_CLOSE_EVT, "BTA_AV_CLOSE_EVT"),
320 std::make_pair(BTA_AV_START_EVT, "BTA_AV_START_EVT"),
321 std::make_pair(BTA_AV_STOP_EVT, "BTA_AV_STOP_EVT"),
322 std::make_pair(BTA_AV_PROTECT_REQ_EVT, "BTA_AV_PROTECT_REQ_EVT"),
323 std::make_pair(BTA_AV_PROTECT_RSP_EVT, "BTA_AV_PROTECT_RSP_EVT"),
324 std::make_pair(BTA_AV_RC_OPEN_EVT, "BTA_AV_RC_OPEN_EVT"),
325 std::make_pair(BTA_AV_RC_CLOSE_EVT, "BTA_AV_RC_CLOSE_EVT"),
326 std::make_pair(BTA_AV_RC_BROWSE_OPEN_EVT, "BTA_AV_RC_BROWSE_OPEN_EVT"),
327 std::make_pair(BTA_AV_RC_BROWSE_CLOSE_EVT, "BTA_AV_RC_BROWSE_CLOSE_EVT"),
328 std::make_pair(BTA_AV_REMOTE_CMD_EVT, "BTA_AV_REMOTE_CMD_EVT"),
329 std::make_pair(BTA_AV_REMOTE_RSP_EVT, "BTA_AV_REMOTE_RSP_EVT"),
330 std::make_pair(BTA_AV_VENDOR_CMD_EVT, "BTA_AV_VENDOR_CMD_EVT"),
331 std::make_pair(BTA_AV_VENDOR_RSP_EVT, "BTA_AV_VENDOR_RSP_EVT"),
332 std::make_pair(BTA_AV_RECONFIG_EVT, "BTA_AV_RECONFIG_EVT"),
333 std::make_pair(BTA_AV_SUSPEND_EVT, "BTA_AV_SUSPEND_EVT"),
334 std::make_pair(BTA_AV_PENDING_EVT, "BTA_AV_PENDING_EVT"),
335 std::make_pair(BTA_AV_META_MSG_EVT, "BTA_AV_META_MSG_EVT"),
336 std::make_pair(BTA_AV_REJECT_EVT, "BTA_AV_REJECT_EVT"),
337 std::make_pair(BTA_AV_RC_FEAT_EVT, "BTA_AV_RC_FEAT_EVT"),
338 std::make_pair(BTA_AV_RC_PSM_EVT, "BTA_AV_RC_PSM_EVT"),
339 std::make_pair(BTA_AV_OFFLOAD_START_RSP_EVT,
340 "BTA_AV_OFFLOAD_START_RSP_EVT"),
341 };
342 for (const auto& event : events) {
343 ASSERT_EQ(event.second, dump_av_sm_event_name(event.first));
344 }
345 std::ostringstream oss;
346 oss << "UNKNOWN_EVENT";
347 ASSERT_EQ(oss.str(), dump_av_sm_event_name(std::numeric_limits<int>::max()));
348 }
349
TEST_F(BtifUtilsTest,dump_dm_search_event)350 TEST_F(BtifUtilsTest, dump_dm_search_event) {
351 std::vector<std::pair<uint16_t, std::string>> events = {
352 std::make_pair(BTA_DM_INQ_RES_EVT, "BTA_DM_INQ_RES_EVT"),
353 std::make_pair(BTA_DM_INQ_CMPL_EVT, "BTA_DM_INQ_CMPL_EVT"),
354 std::make_pair(BTA_DM_DISC_CMPL_EVT, "BTA_DM_DISC_CMPL_EVT"),
355 std::make_pair(BTA_DM_SEARCH_CANCEL_CMPL_EVT,
356 "BTA_DM_SEARCH_CANCEL_CMPL_EVT"),
357 std::make_pair(BTA_DM_NAME_READ_EVT, "BTA_DM_NAME_READ_EVT"),
358 };
359 for (const auto& event : events) {
360 ASSERT_TRUE(dump_dm_search_event(event.first).starts_with(event.second));
361 }
362 ASSERT_TRUE(dump_dm_search_event(std::numeric_limits<uint16_t>::max())
363 .starts_with("Unknown"));
364 }
365
TEST_F(BtifUtilsTest,dump_property_type)366 TEST_F(BtifUtilsTest, dump_property_type) {
367 std::vector<std::pair<bt_property_type_t, std::string>> types = {
368 std::make_pair(BT_PROPERTY_BDNAME, "BT_PROPERTY_BDNAME"),
369 std::make_pair(BT_PROPERTY_BDADDR, "BT_PROPERTY_BDADDR"),
370 std::make_pair(BT_PROPERTY_UUIDS, "BT_PROPERTY_UUIDS"),
371 std::make_pair(BT_PROPERTY_CLASS_OF_DEVICE,
372 "BT_PROPERTY_CLASS_OF_DEVICE"),
373 std::make_pair(BT_PROPERTY_TYPE_OF_DEVICE, "BT_PROPERTY_TYPE_OF_DEVICE"),
374 std::make_pair(BT_PROPERTY_REMOTE_RSSI, "BT_PROPERTY_REMOTE_RSSI"),
375 std::make_pair(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,
376 "BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT"),
377 std::make_pair(BT_PROPERTY_ADAPTER_BONDED_DEVICES,
378 "BT_PROPERTY_ADAPTER_BONDED_DEVICES"),
379 std::make_pair(BT_PROPERTY_ADAPTER_SCAN_MODE,
380 "BT_PROPERTY_ADAPTER_SCAN_MODE"),
381 std::make_pair(BT_PROPERTY_REMOTE_FRIENDLY_NAME,
382 "BT_PROPERTY_REMOTE_FRIENDLY_NAME"),
383 };
384 for (const auto& type : types) {
385 EXPECT_TRUE(dump_property_type(type.first).starts_with(type.second));
386 }
387 EXPECT_TRUE(dump_property_type(static_cast<bt_property_type_t>(
388 std::numeric_limits<uint16_t>::max()))
389 .starts_with("Unknown"));
390 }
391
TEST_F(BtifUtilsTest,dump_dm_event)392 TEST_F(BtifUtilsTest, dump_dm_event) {
393 std::vector<std::pair<uint8_t, std::string>> events = {
394 std::make_pair(BTA_DM_PIN_REQ_EVT, "BTA_DM_PIN_REQ_EVT"),
395 std::make_pair(BTA_DM_AUTH_CMPL_EVT, "BTA_DM_AUTH_CMPL_EVT"),
396 std::make_pair(BTA_DM_LINK_UP_EVT, "BTA_DM_LINK_UP_EVT"),
397 std::make_pair(BTA_DM_LINK_DOWN_EVT, "BTA_DM_LINK_DOWN_EVT"),
398 std::make_pair(BTA_DM_BOND_CANCEL_CMPL_EVT,
399 "BTA_DM_BOND_CANCEL_CMPL_EVT"),
400 std::make_pair(BTA_DM_SP_CFM_REQ_EVT, "BTA_DM_SP_CFM_REQ_EVT"),
401 std::make_pair(BTA_DM_SP_KEY_NOTIF_EVT, "BTA_DM_SP_KEY_NOTIF_EVT"),
402 std::make_pair(BTA_DM_BLE_KEY_EVT, "BTA_DM_BLE_KEY_EVT"),
403 std::make_pair(BTA_DM_BLE_SEC_REQ_EVT, "BTA_DM_BLE_SEC_REQ_EVT"),
404 std::make_pair(BTA_DM_BLE_PASSKEY_NOTIF_EVT,
405 "BTA_DM_BLE_PASSKEY_NOTIF_EVT"),
406 std::make_pair(BTA_DM_BLE_PASSKEY_REQ_EVT, "BTA_DM_BLE_PASSKEY_REQ_EVT"),
407 std::make_pair(BTA_DM_BLE_OOB_REQ_EVT, "BTA_DM_BLE_OOB_REQ_EVT"),
408 std::make_pair(BTA_DM_BLE_SC_OOB_REQ_EVT, "BTA_DM_BLE_SC_OOB_REQ_EVT"),
409 std::make_pair(BTA_DM_BLE_LOCAL_IR_EVT, "BTA_DM_BLE_LOCAL_IR_EVT"),
410 std::make_pair(BTA_DM_BLE_LOCAL_ER_EVT, "BTA_DM_BLE_LOCAL_ER_EVT"),
411 std::make_pair(BTA_DM_BLE_AUTH_CMPL_EVT, "BTA_DM_BLE_AUTH_CMPL_EVT"),
412 std::make_pair(BTA_DM_DEV_UNPAIRED_EVT, "BTA_DM_DEV_UNPAIRED_EVT"),
413 std::make_pair(BTA_DM_ENER_INFO_READ, "BTA_DM_ENER_INFO_READ"),
414 };
415 for (const auto& event : events) {
416 ASSERT_TRUE(dump_dm_event(event.first).starts_with(event.second));
417 }
418 ASSERT_TRUE(dump_dm_event(std::numeric_limits<uint8_t>::max())
419 .starts_with("Unknown"));
420 }
421
TEST_F(BtifUtilsTest,dump_hf_event)422 TEST_F(BtifUtilsTest, dump_hf_event) {
423 std::vector<std::pair<uint8_t, std::string>> events = {
424 std::make_pair(BTA_AG_ENABLE_EVT, "BTA_AG_ENABLE_EVT"),
425 std::make_pair(BTA_AG_REGISTER_EVT, "BTA_AG_REGISTER_EVT"),
426 std::make_pair(BTA_AG_OPEN_EVT, "BTA_AG_OPEN_EVT"),
427 std::make_pair(BTA_AG_CLOSE_EVT, "BTA_AG_CLOSE_EVT"),
428 std::make_pair(BTA_AG_CONN_EVT, "BTA_AG_CONN_EVT"),
429 std::make_pair(BTA_AG_AUDIO_OPEN_EVT, "BTA_AG_AUDIO_OPEN_EVT"),
430 std::make_pair(BTA_AG_AUDIO_CLOSE_EVT, "BTA_AG_AUDIO_CLOSE_EVT"),
431 std::make_pair(BTA_AG_SPK_EVT, "BTA_AG_SPK_EVT"),
432 std::make_pair(BTA_AG_MIC_EVT, "BTA_AG_MIC_EVT"),
433 std::make_pair(BTA_AG_AT_CKPD_EVT, "BTA_AG_AT_CKPD_EVT"),
434 std::make_pair(BTA_AG_DISABLE_EVT, "BTA_AG_DISABLE_EVT"),
435 std::make_pair(BTA_AG_CODEC_EVT, "BTA_AG_CODEC_EVT"),
436 std::make_pair(BTA_AG_AT_A_EVT, "BTA_AG_AT_A_EVT"),
437 std::make_pair(BTA_AG_AT_D_EVT, "BTA_AG_AT_D_EVT"),
438 std::make_pair(BTA_AG_AT_CHLD_EVT, "BTA_AG_AT_CHLD_EVT"),
439 std::make_pair(BTA_AG_AT_CHUP_EVT, "BTA_AG_AT_CHUP_EVT"),
440 std::make_pair(BTA_AG_AT_CIND_EVT, "BTA_AG_AT_CIND_EVT"),
441 std::make_pair(BTA_AG_AT_VTS_EVT, "BTA_AG_AT_VTS_EVT"),
442 std::make_pair(BTA_AG_AT_BINP_EVT, "BTA_AG_AT_BINP_EVT"),
443 std::make_pair(BTA_AG_AT_BLDN_EVT, "BTA_AG_AT_BLDN_EVT"),
444 std::make_pair(BTA_AG_AT_BVRA_EVT, "BTA_AG_AT_BVRA_EVT"),
445 std::make_pair(BTA_AG_AT_NREC_EVT, "BTA_AG_AT_NREC_EVT"),
446 std::make_pair(BTA_AG_AT_CNUM_EVT, "BTA_AG_AT_CNUM_EVT"),
447 std::make_pair(BTA_AG_AT_BTRH_EVT, "BTA_AG_AT_BTRH_EVT"),
448 std::make_pair(BTA_AG_AT_CLCC_EVT, "BTA_AG_AT_CLCC_EVT"),
449 std::make_pair(BTA_AG_AT_COPS_EVT, "BTA_AG_AT_COPS_EVT"),
450 std::make_pair(BTA_AG_AT_UNAT_EVT, "BTA_AG_AT_UNAT_EVT"),
451 std::make_pair(BTA_AG_AT_CBC_EVT, "BTA_AG_AT_CBC_EVT"),
452 std::make_pair(BTA_AG_AT_BAC_EVT, "BTA_AG_AT_BAC_EVT"),
453 std::make_pair(BTA_AG_AT_BCS_EVT, "BTA_AG_AT_BCS_EVT"),
454 std::make_pair(BTA_AG_AT_BIND_EVT, "BTA_AG_AT_BIND_EVT"),
455 std::make_pair(BTA_AG_AT_BIEV_EVT, "BTA_AG_AT_BIEV_EVT"),
456 std::make_pair(BTA_AG_AT_BIA_EVT, "BTA_AG_AT_BIA_EVT"),
457 };
458 for (const auto& event : events) {
459 ASSERT_TRUE(dump_hf_event(event.first).starts_with(event.second));
460 }
461 ASSERT_TRUE(dump_hf_event(std::numeric_limits<uint8_t>::max())
462 .starts_with("Unknown"));
463 }
464
TEST_F(BtifUtilsTest,dump_hf_client_event)465 TEST_F(BtifUtilsTest, dump_hf_client_event) {
466 std::vector<std::pair<int, std::string>> events = {
467 std::make_pair(BTA_HF_CLIENT_ENABLE_EVT, "BTA_HF_CLIENT_ENABLE_EVT"),
468 std::make_pair(BTA_HF_CLIENT_REGISTER_EVT, "BTA_HF_CLIENT_REGISTER_EVT"),
469 std::make_pair(BTA_HF_CLIENT_OPEN_EVT, "BTA_HF_CLIENT_OPEN_EVT"),
470 std::make_pair(BTA_HF_CLIENT_CLOSE_EVT, "BTA_HF_CLIENT_CLOSE_EVT"),
471 std::make_pair(BTA_HF_CLIENT_CONN_EVT, "BTA_HF_CLIENT_CONN_EVT"),
472 std::make_pair(BTA_HF_CLIENT_AUDIO_OPEN_EVT,
473 "BTA_HF_CLIENT_AUDIO_OPEN_EVT"),
474 std::make_pair(BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT,
475 "BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT"),
476 std::make_pair(BTA_HF_CLIENT_AUDIO_LC3_OPEN_EVT,
477 "BTA_HF_CLIENT_AUDIO_LC3_OPEN_EVT"),
478 std::make_pair(BTA_HF_CLIENT_AUDIO_CLOSE_EVT,
479 "BTA_HF_CLIENT_AUDIO_CLOSE_EVT"),
480 std::make_pair(BTA_HF_CLIENT_SPK_EVT, "BTA_HF_CLIENT_SPK_EVT"),
481 std::make_pair(BTA_HF_CLIENT_MIC_EVT, "BTA_HF_CLIENT_MIC_EVT"),
482 std::make_pair(BTA_HF_CLIENT_DISABLE_EVT, "BTA_HF_CLIENT_DISABLE_EVT"),
483 std::make_pair(BTA_HF_CLIENT_IND_EVT, "BTA_HF_CLIENT_IND_EVT"),
484 std::make_pair(BTA_HF_CLIENT_VOICE_REC_EVT,
485 "BTA_HF_CLIENT_VOICE_REC_EVT"),
486 std::make_pair(BTA_HF_CLIENT_OPERATOR_NAME_EVT,
487 "BTA_HF_CLIENT_OPERATOR_NAME_EVT"),
488 std::make_pair(BTA_HF_CLIENT_CLIP_EVT, "BTA_HF_CLIENT_CLIP_EVT"),
489 std::make_pair(BTA_HF_CLIENT_CCWA_EVT, "BTA_HF_CLIENT_CCWA_EVT"),
490 std::make_pair(BTA_HF_CLIENT_AT_RESULT_EVT,
491 "BTA_HF_CLIENT_AT_RESULT_EVT"),
492 std::make_pair(BTA_HF_CLIENT_CLCC_EVT, "BTA_HF_CLIENT_CLCC_EVT"),
493 std::make_pair(BTA_HF_CLIENT_CNUM_EVT, "BTA_HF_CLIENT_CNUM_EVT"),
494 std::make_pair(BTA_HF_CLIENT_BTRH_EVT, "BTA_HF_CLIENT_BTRH_EVT"),
495 std::make_pair(BTA_HF_CLIENT_BSIR_EVT, "BTA_HF_CLIENT_BSIR_EVT"),
496 std::make_pair(BTA_HF_CLIENT_BINP_EVT, "BTA_HF_CLIENT_BINP_EVT"),
497 std::make_pair(BTA_HF_CLIENT_RING_INDICATION,
498 "BTA_HF_CLIENT_RING_INDICATION"),
499 };
500 for (const auto& event : events) {
501 ASSERT_TRUE(dump_hf_client_event(event.first).starts_with(event.second));
502 }
503 ASSERT_TRUE(dump_hf_client_event(std::numeric_limits<uint16_t>::max())
504 .starts_with("Unknown"));
505 }
506
TEST_F(BtifCoreTest,bta_hh_event_text)507 TEST_F(BtifCoreTest, bta_hh_event_text) {
508 std::vector<std::pair<int, std::string>> events = {
509 std::make_pair(BTA_HH_EMPTY_EVT, "BTA_HH_EMPTY_EVT"),
510 std::make_pair(BTA_HH_ENABLE_EVT, "BTA_HH_ENABLE_EVT"),
511 std::make_pair(BTA_HH_DISABLE_EVT, "BTA_HH_DISABLE_EVT"),
512 std::make_pair(BTA_HH_OPEN_EVT, "BTA_HH_OPEN_EVT"),
513 std::make_pair(BTA_HH_CLOSE_EVT, "BTA_HH_CLOSE_EVT"),
514 std::make_pair(BTA_HH_GET_DSCP_EVT, "BTA_HH_GET_DSCP_EVT"),
515 std::make_pair(BTA_HH_GET_PROTO_EVT, "BTA_HH_GET_PROTO_EVT"),
516 std::make_pair(BTA_HH_GET_RPT_EVT, "BTA_HH_GET_RPT_EVT"),
517 std::make_pair(BTA_HH_GET_IDLE_EVT, "BTA_HH_GET_IDLE_EVT"),
518 std::make_pair(BTA_HH_SET_PROTO_EVT, "BTA_HH_SET_PROTO_EVT"),
519 std::make_pair(BTA_HH_SET_RPT_EVT, "BTA_HH_SET_RPT_EVT"),
520 std::make_pair(BTA_HH_SET_IDLE_EVT, "BTA_HH_SET_IDLE_EVT"),
521 std::make_pair(BTA_HH_VC_UNPLUG_EVT, "BTA_HH_VC_UNPLUG_EVT"),
522 std::make_pair(BTA_HH_ADD_DEV_EVT, "BTA_HH_ADD_DEV_EVT"),
523 std::make_pair(BTA_HH_RMV_DEV_EVT, "BTA_HH_RMV_DEV_EVT"),
524 std::make_pair(BTA_HH_API_ERR_EVT, "BTA_HH_API_ERR_EVT"),
525 };
526 for (const auto& event : events) {
527 ASSERT_TRUE(bta_hh_event_text(event.first).starts_with(event.second));
528 }
529 ASSERT_TRUE(bta_hh_event_text(std::numeric_limits<uint16_t>::max())
530 .starts_with("Unknown"));
531 }
532
TEST_F(BtifUtilsTest,dump_hd_event)533 TEST_F(BtifUtilsTest, dump_hd_event) {
534 std::vector<std::pair<uint16_t, std::string>> events = {
535 std::make_pair(BTA_HD_ENABLE_EVT, "BTA_HD_ENABLE_EVT"),
536 std::make_pair(BTA_HD_DISABLE_EVT, "BTA_HD_DISABLE_EVT"),
537 std::make_pair(BTA_HD_REGISTER_APP_EVT, "BTA_HD_REGISTER_APP_EVT"),
538 std::make_pair(BTA_HD_UNREGISTER_APP_EVT, "BTA_HD_UNREGISTER_APP_EVT"),
539 std::make_pair(BTA_HD_OPEN_EVT, "BTA_HD_OPEN_EVT"),
540 std::make_pair(BTA_HD_CLOSE_EVT, "BTA_HD_CLOSE_EVT"),
541 std::make_pair(BTA_HD_GET_REPORT_EVT, "BTA_HD_GET_REPORT_EVT"),
542 std::make_pair(BTA_HD_SET_REPORT_EVT, "BTA_HD_SET_REPORT_EVT"),
543 std::make_pair(BTA_HD_SET_PROTOCOL_EVT, "BTA_HD_SET_PROTOCOL_EVT"),
544 std::make_pair(BTA_HD_INTR_DATA_EVT, "BTA_HD_INTR_DATA_EVT"),
545 std::make_pair(BTA_HD_VC_UNPLUG_EVT, "BTA_HD_VC_UNPLUG_EVT"),
546 std::make_pair(BTA_HD_CONN_STATE_EVT, "BTA_HD_CONN_STATE_EVT"),
547 std::make_pair(BTA_HD_API_ERR_EVT, "BTA_HD_API_ERR_EVT"),
548 };
549 for (const auto& event : events) {
550 ASSERT_TRUE(dump_hd_event(event.first).starts_with(event.second));
551 }
552 ASSERT_TRUE(dump_hd_event(std::numeric_limits<uint16_t>::max())
553 .starts_with("Unknown"));
554 }
555
TEST_F(BtifUtilsTest,dump_thread_evt)556 TEST_F(BtifUtilsTest, dump_thread_evt) {
557 std::vector<std::pair<bt_cb_thread_evt, std::string>> events = {
558 std::make_pair(ASSOCIATE_JVM, "ASSOCIATE_JVM"),
559 std::make_pair(DISASSOCIATE_JVM, "DISASSOCIATE_JVM"),
560 };
561 for (const auto& event : events) {
562 ASSERT_TRUE(dump_thread_evt(event.first).starts_with(event.second));
563 }
564 ASSERT_TRUE(dump_thread_evt(static_cast<bt_cb_thread_evt>(
565 std::numeric_limits<uint16_t>::max()))
566 .starts_with("Unknown"));
567 }
568
TEST_F(BtifUtilsTest,dump_av_conn_state)569 TEST_F(BtifUtilsTest, dump_av_conn_state) {
570 std::vector<std::pair<uint16_t, std::string>> events = {
571 std::make_pair(BTAV_CONNECTION_STATE_DISCONNECTED,
572 "BTAV_CONNECTION_STATE_DISCONNECTED"),
573 std::make_pair(BTAV_CONNECTION_STATE_CONNECTING,
574 "BTAV_CONNECTION_STATE_CONNECTING"),
575 std::make_pair(BTAV_CONNECTION_STATE_CONNECTED,
576 "BTAV_CONNECTION_STATE_CONNECTED"),
577 std::make_pair(BTAV_CONNECTION_STATE_DISCONNECTING,
578 "BTAV_CONNECTION_STATE_DISCONNECTING"),
579 };
580 for (const auto& event : events) {
581 ASSERT_TRUE(dump_av_conn_state(event.first).starts_with(event.second));
582 }
583 ASSERT_TRUE(dump_av_conn_state(std::numeric_limits<uint16_t>::max())
584 .starts_with("Unknown"));
585 }
586
TEST_F(BtifUtilsTest,dump_av_audio_state)587 TEST_F(BtifUtilsTest, dump_av_audio_state) {
588 std::vector<std::pair<uint16_t, std::string>> events = {
589 std::make_pair(BTAV_AUDIO_STATE_REMOTE_SUSPEND,
590 "BTAV_AUDIO_STATE_REMOTE_SUSPEND"),
591 std::make_pair(BTAV_AUDIO_STATE_STOPPED, "BTAV_AUDIO_STATE_STOPPED"),
592 std::make_pair(BTAV_AUDIO_STATE_STARTED, "BTAV_AUDIO_STATE_STARTED"),
593 };
594 for (const auto& event : events) {
595 ASSERT_TRUE(dump_av_audio_state(event.first).starts_with(event.second));
596 }
597 ASSERT_TRUE(dump_av_audio_state(std::numeric_limits<uint16_t>::max())
598 .starts_with("Unknown"));
599 }
600
TEST_F(BtifUtilsTest,dump_adapter_scan_mode)601 TEST_F(BtifUtilsTest, dump_adapter_scan_mode) {
602 std::vector<std::pair<bt_scan_mode_t, std::string>> events = {
603 std::make_pair(BT_SCAN_MODE_NONE, "BT_SCAN_MODE_NONE"),
604 std::make_pair(BT_SCAN_MODE_CONNECTABLE, "BT_SCAN_MODE_CONNECTABLE"),
605 std::make_pair(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE,
606 "BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE"),
607 };
608 for (const auto& event : events) {
609 ASSERT_TRUE(dump_adapter_scan_mode(event.first).starts_with(event.second));
610 }
611 ASSERT_TRUE(dump_adapter_scan_mode(
612 static_cast<bt_scan_mode_t>(std::numeric_limits<int>::max()))
613 .starts_with("Unknown"));
614 }
615
TEST_F(BtifUtilsTest,dump_bt_status)616 TEST_F(BtifUtilsTest, dump_bt_status) {
617 std::vector<std::pair<bt_status_t, std::string>> events = {
618 std::make_pair(BT_STATUS_SUCCESS, "BT_STATUS_SUCCESS"),
619 std::make_pair(BT_STATUS_FAIL, "BT_STATUS_FAIL"),
620 std::make_pair(BT_STATUS_NOT_READY, "BT_STATUS_NOT_READY"),
621 std::make_pair(BT_STATUS_NOMEM, "BT_STATUS_NOMEM"),
622 std::make_pair(BT_STATUS_BUSY, "BT_STATUS_BUSY"),
623 std::make_pair(BT_STATUS_UNSUPPORTED, "BT_STATUS_UNSUPPORTED"),
624 };
625 for (const auto& event : events) {
626 ASSERT_TRUE(dump_bt_status(event.first).starts_with(event.second));
627 }
628 ASSERT_TRUE(
629 dump_bt_status(static_cast<bt_status_t>(std::numeric_limits<int>::max()))
630 .starts_with("Unknown"));
631 }
632
TEST_F(BtifUtilsTest,dump_rc_event)633 TEST_F(BtifUtilsTest, dump_rc_event) {
634 std::vector<std::pair<uint8_t, std::string>> events = {
635 std::make_pair(BTA_AV_RC_OPEN_EVT, "BTA_AV_RC_OPEN_EVT"),
636 std::make_pair(BTA_AV_RC_CLOSE_EVT, "BTA_AV_RC_CLOSE_EVT"),
637 std::make_pair(BTA_AV_RC_BROWSE_OPEN_EVT, "BTA_AV_RC_BROWSE_OPEN_EVT"),
638 std::make_pair(BTA_AV_RC_BROWSE_CLOSE_EVT, "BTA_AV_RC_BROWSE_CLOSE_EVT"),
639 std::make_pair(BTA_AV_REMOTE_CMD_EVT, "BTA_AV_REMOTE_CMD_EVT"),
640 std::make_pair(BTA_AV_REMOTE_RSP_EVT, "BTA_AV_REMOTE_RSP_EVT"),
641 std::make_pair(BTA_AV_VENDOR_CMD_EVT, "BTA_AV_VENDOR_CMD_EVT"),
642 std::make_pair(BTA_AV_VENDOR_RSP_EVT, "BTA_AV_VENDOR_RSP_EVT"),
643 std::make_pair(BTA_AV_META_MSG_EVT, "BTA_AV_META_MSG_EVT"),
644 std::make_pair(BTA_AV_RC_FEAT_EVT, "BTA_AV_RC_FEAT_EVT"),
645 std::make_pair(BTA_AV_RC_PSM_EVT, "BTA_AV_RC_PSM_EVT"),
646 };
647 for (const auto& event : events) {
648 ASSERT_TRUE(dump_rc_event(event.first).starts_with(event.second));
649 }
650 ASSERT_TRUE(dump_rc_event(std::numeric_limits<uint8_t>::max())
651 .starts_with("Unknown"));
652 }
653
TEST_F(BtifUtilsTest,dump_rc_notification_event_id)654 TEST_F(BtifUtilsTest, dump_rc_notification_event_id) {
655 std::vector<std::pair<uint8_t, std::string>> events = {
656 std::make_pair(AVRC_EVT_PLAY_STATUS_CHANGE,
657 "AVRC_EVT_PLAY_STATUS_CHANGE"),
658 std::make_pair(AVRC_EVT_TRACK_CHANGE, "AVRC_EVT_TRACK_CHANGE"),
659 std::make_pair(AVRC_EVT_TRACK_REACHED_END, "AVRC_EVT_TRACK_REACHED_END"),
660 std::make_pair(AVRC_EVT_TRACK_REACHED_START,
661 "AVRC_EVT_TRACK_REACHED_START"),
662 std::make_pair(AVRC_EVT_PLAY_POS_CHANGED, "AVRC_EVT_PLAY_POS_CHANGED"),
663 std::make_pair(AVRC_EVT_BATTERY_STATUS_CHANGE,
664 "AVRC_EVT_BATTERY_STATUS_CHANGE"),
665 std::make_pair(AVRC_EVT_SYSTEM_STATUS_CHANGE,
666 "AVRC_EVT_SYSTEM_STATUS_CHANGE"),
667 std::make_pair(AVRC_EVT_APP_SETTING_CHANGE,
668 "AVRC_EVT_APP_SETTING_CHANGE"),
669 std::make_pair(AVRC_EVT_VOLUME_CHANGE, "AVRC_EVT_VOLUME_CHANGE"),
670 std::make_pair(AVRC_EVT_ADDR_PLAYER_CHANGE,
671 "AVRC_EVT_ADDR_PLAYER_CHANGE"),
672 std::make_pair(AVRC_EVT_AVAL_PLAYERS_CHANGE,
673 "AVRC_EVT_AVAL_PLAYERS_CHANGE"),
674 std::make_pair(AVRC_EVT_NOW_PLAYING_CHANGE,
675 "AVRC_EVT_NOW_PLAYING_CHANGE"),
676 std::make_pair(AVRC_EVT_UIDS_CHANGE, "AVRC_EVT_UIDS_CHANGE"),
677 };
678 for (const auto& event : events) {
679 ASSERT_TRUE(
680 dump_rc_notification_event_id(event.first).starts_with(event.second));
681 }
682 ASSERT_TRUE(dump_rc_notification_event_id(std::numeric_limits<uint8_t>::max())
683 .starts_with("Unknown"));
684 }
685
TEST_F(BtifUtilsTest,dump_rc_pdu)686 TEST_F(BtifUtilsTest, dump_rc_pdu) {
687 std::vector<std::pair<uint8_t, std::string>> pdus = {
688 std::make_pair(AVRC_PDU_LIST_PLAYER_APP_ATTR,
689 "AVRC_PDU_LIST_PLAYER_APP_ATTR"),
690 std::make_pair(AVRC_PDU_LIST_PLAYER_APP_VALUES,
691 "AVRC_PDU_LIST_PLAYER_APP_VALUES"),
692 std::make_pair(AVRC_PDU_GET_CUR_PLAYER_APP_VALUE,
693 "AVRC_PDU_GET_CUR_PLAYER_APP_VALUE"),
694 std::make_pair(AVRC_PDU_SET_PLAYER_APP_VALUE,
695 "AVRC_PDU_SET_PLAYER_APP_VALUE"),
696 std::make_pair(AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT,
697 "AVRC_PDU_GET_PLAYER_APP_ATTR_TEXT"),
698 std::make_pair(AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT,
699 "AVRC_PDU_GET_PLAYER_APP_VALUE_TEXT"),
700 std::make_pair(AVRC_PDU_INFORM_DISPLAY_CHARSET,
701 "AVRC_PDU_INFORM_DISPLAY_CHARSET"),
702 std::make_pair(AVRC_PDU_INFORM_BATTERY_STAT_OF_CT,
703 "AVRC_PDU_INFORM_BATTERY_STAT_OF_CT"),
704 std::make_pair(AVRC_PDU_GET_ELEMENT_ATTR, "AVRC_PDU_GET_ELEMENT_ATTR"),
705 std::make_pair(AVRC_PDU_GET_PLAY_STATUS, "AVRC_PDU_GET_PLAY_STATUS"),
706 std::make_pair(AVRC_PDU_REGISTER_NOTIFICATION,
707 "AVRC_PDU_REGISTER_NOTIFICATION"),
708 std::make_pair(AVRC_PDU_REQUEST_CONTINUATION_RSP,
709 "AVRC_PDU_REQUEST_CONTINUATION_RSP"),
710 std::make_pair(AVRC_PDU_ABORT_CONTINUATION_RSP,
711 "AVRC_PDU_ABORT_CONTINUATION_RSP"),
712 std::make_pair(AVRC_PDU_SET_ABSOLUTE_VOLUME,
713 "AVRC_PDU_SET_ABSOLUTE_VOLUME"),
714 std::make_pair(AVRC_PDU_SET_ADDRESSED_PLAYER,
715 "AVRC_PDU_SET_ADDRESSED_PLAYER"),
716 std::make_pair(AVRC_PDU_CHANGE_PATH, "AVRC_PDU_CHANGE_PATH"),
717 std::make_pair(AVRC_PDU_GET_CAPABILITIES, "AVRC_PDU_GET_CAPABILITIES"),
718 std::make_pair(AVRC_PDU_SET_BROWSED_PLAYER,
719 "AVRC_PDU_SET_BROWSED_PLAYER"),
720 std::make_pair(AVRC_PDU_GET_FOLDER_ITEMS, "AVRC_PDU_GET_FOLDER_ITEMS"),
721 std::make_pair(AVRC_PDU_GET_ITEM_ATTRIBUTES,
722 "AVRC_PDU_GET_ITEM_ATTRIBUTES"),
723 std::make_pair(AVRC_PDU_PLAY_ITEM, "AVRC_PDU_PLAY_ITEM"),
724 std::make_pair(AVRC_PDU_SEARCH, "AVRC_PDU_SEARCH"),
725 std::make_pair(AVRC_PDU_ADD_TO_NOW_PLAYING,
726 "AVRC_PDU_ADD_TO_NOW_PLAYING"),
727 std::make_pair(AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS,
728 "AVRC_PDU_GET_TOTAL_NUM_OF_ITEMS"),
729 std::make_pair(AVRC_PDU_GENERAL_REJECT, "AVRC_PDU_GENERAL_REJECT"),
730 };
731 for (const auto& pdu : pdus) {
732 ASSERT_TRUE(dump_rc_pdu(pdu.first).starts_with(pdu.second));
733 }
734 ASSERT_TRUE(
735 dump_rc_pdu(std::numeric_limits<uint8_t>::max()).starts_with("Unknown"));
736 }
737
TEST_F(BtifCoreWithControllerTest,btif_dm_get_connection_state__unconnected)738 TEST_F(BtifCoreWithControllerTest, btif_dm_get_connection_state__unconnected) {
739 ASSERT_EQ(0, btif_dm_get_connection_state(kRawAddress));
740 }
741
TEST_F(BtifCoreWithConnectionTest,btif_dm_get_connection_state__connected_no_encryption)742 TEST_F(BtifCoreWithConnectionTest,
743 btif_dm_get_connection_state__connected_no_encryption) {
744 test::mock::stack_btm_sec::BTM_IsEncrypted.body =
745 [](const RawAddress& /* bd_addr */, tBT_TRANSPORT transport) {
746 switch (transport) {
747 case BT_TRANSPORT_BR_EDR:
748 return false;
749 case BT_TRANSPORT_LE:
750 return false;
751 }
752 return false;
753 };
754 ASSERT_EQ(1, btif_dm_get_connection_state(kRawAddress));
755 test::mock::stack_btm_sec::BTM_IsEncrypted = {};
756 }
757
TEST_F(BtifCoreWithConnectionTest,btif_dm_get_connection_state__connected_classic_encryption)758 TEST_F(BtifCoreWithConnectionTest,
759 btif_dm_get_connection_state__connected_classic_encryption) {
760 test::mock::stack_btm_sec::BTM_IsEncrypted.body =
761 [](const RawAddress& /* bd_addr */, tBT_TRANSPORT transport) {
762 switch (transport) {
763 case BT_TRANSPORT_BR_EDR:
764 return true;
765 case BT_TRANSPORT_LE:
766 return false;
767 }
768 return false;
769 };
770 ASSERT_EQ(3, btif_dm_get_connection_state(kRawAddress));
771
772 test::mock::stack_btm_sec::BTM_IsEncrypted = {};
773 }
774
TEST_F(BtifCoreWithConnectionTest,btif_dm_get_connection_state__connected_le_encryption)775 TEST_F(BtifCoreWithConnectionTest,
776 btif_dm_get_connection_state__connected_le_encryption) {
777 test::mock::stack_btm_sec::BTM_IsEncrypted.body =
778 [](const RawAddress& /* bd_addr */, tBT_TRANSPORT transport) {
779 switch (transport) {
780 case BT_TRANSPORT_BR_EDR:
781 return false;
782 case BT_TRANSPORT_LE:
783 return true;
784 }
785 return false;
786 };
787 ASSERT_EQ(5, btif_dm_get_connection_state(kRawAddress));
788 test::mock::stack_btm_sec::BTM_IsEncrypted = {};
789 }
790
TEST_F(BtifCoreWithConnectionTest,btif_dm_get_connection_state__connected_both_encryption)791 TEST_F(BtifCoreWithConnectionTest,
792 btif_dm_get_connection_state__connected_both_encryption) {
793 test::mock::stack_btm_sec::BTM_IsEncrypted.body =
794 [](const RawAddress& /* bd_addr */, tBT_TRANSPORT transport) {
795 switch (transport) {
796 case BT_TRANSPORT_BR_EDR:
797 return true;
798 case BT_TRANSPORT_LE:
799 return true;
800 }
801 return false;
802 };
803 ASSERT_EQ(7, btif_dm_get_connection_state(kRawAddress));
804 test::mock::stack_btm_sec::BTM_IsEncrypted = {};
805 }
806
TEST_F(BtifCoreWithConnectionTest,btif_dm_get_connection_state_sync)807 TEST_F(BtifCoreWithConnectionTest, btif_dm_get_connection_state_sync) {
808 test::mock::stack_btm_sec::BTM_IsEncrypted.body =
809 [](const RawAddress& /* bd_addr */, tBT_TRANSPORT transport) {
810 switch (transport) {
811 case BT_TRANSPORT_BR_EDR:
812 return true;
813 case BT_TRANSPORT_LE:
814 return true;
815 }
816 return false;
817 };
818 ASSERT_EQ(7, btif_dm_get_connection_state_sync(kRawAddress));
819
820 test::mock::stack_btm_sec::BTM_IsEncrypted = {};
821 }
822
823 auto get_properties = [](const char* key, char* value,
__anon645579b10d02(const char* key, char* value, const char* ) 824 const char* /* default_value */) -> size_t {
825 static bluetooth::bqr::BqrConfiguration config{
826 .report_action = bluetooth::bqr::REPORT_ACTION_ADD,
827 .quality_event_mask = 0x1ffff, // Everything
828 .minimum_report_interval_ms = 1000,
829 .vnd_quality_mask = 29,
830 .vnd_trace_mask = 5,
831 .report_interval_multiple = 2,
832 };
833 if (std::string(key) == bluetooth::bqr::kpPropertyEventMask) {
834 std::string event_mask = std::to_string(config.quality_event_mask);
835 std::copy(event_mask.cbegin(), event_mask.cend(), value);
836 return event_mask.size();
837 }
838 if (std::string(key) == bluetooth::bqr::kpPropertyMinReportIntervalMs) {
839 std::string interval = std::to_string(config.minimum_report_interval_ms);
840 std::copy(interval.cbegin(), interval.cend(), value);
841 return interval.size();
842 }
843 return 0;
844 };
845
TEST_F(BtifCoreWithControllerTest,debug_dump_unconfigured)846 TEST_F(BtifCoreWithControllerTest, debug_dump_unconfigured) {
847 int fds[2];
848 ASSERT_EQ(0, socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, fds));
849 static int write_fd = fds[0];
850 static int read_fd = fds[1];
851 auto reading_promise = std::make_unique<std::promise<void>>();
852 auto reading_done = reading_promise->get_future();
853
854 do_in_main_thread(FROM_HERE,
855 BindOnce([]() { bluetooth::bqr::DebugDump(write_fd); }));
856 do_in_main_thread(
857 FROM_HERE, BindOnce(
858 [](std::unique_ptr<std::promise<void>> done_promise) {
859 char line_buf[1024] = "";
860 int bytes_read = read(read_fd, line_buf, 1024);
861 EXPECT_GT(bytes_read, 0);
862 EXPECT_NE(
863 std::string(line_buf).find("Event queue is empty"),
864 std::string::npos);
865 done_promise->set_value();
866 },
867 std::move(reading_promise)));
868 EXPECT_EQ(std::future_status::ready,
869 reading_done.wait_for(std::chrono::seconds(1)));
870 close(write_fd);
871 close(read_fd);
872 }
873
874 class BtifCoreWithVendorSupportTest : public BtifCoreWithControllerTest {
875 protected:
SetUp()876 void SetUp() override {
877 BtifCoreWithControllerTest::SetUp();
878 bluetooth::hci::testing::mock_hci_layer_ = &hci_;
879 test::mock::osi_properties::osi_property_get.body = get_properties;
880
881 std::promise<void> configuration_promise;
882 auto configuration_done = configuration_promise.get_future();
883 EXPECT_CALL(
884 hci_,
885 EnqueueCommand(
886 _, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_)))
887 .WillOnce(
888 // Replace with real PDL for 0xfc17
889 [&configuration_promise](
890 std::unique_ptr<CommandBuilder> cmd,
891 ContextualOnceCallback<void(CommandCompleteView)> callback) {
892 auto cmd_view = VendorCommandView::Create(
893 CommandView::Create(BuilderToView(std::move(cmd))));
894 EXPECT_TRUE(cmd_view.IsValid());
895 auto response = CommandCompleteView::Create(EventView::Create(
896 BuilderToView(CommandCompleteBuilder::Create(
897 1, cmd_view.GetOpCode(),
898 std::make_unique<RawBuilder>()))));
899 EXPECT_TRUE(response.IsValid());
900 callback(response);
901 configuration_promise.set_value();
902 })
903 .RetiresOnSaturation();
904 EXPECT_CALL(
905 hci_,
906 EnqueueCommand(
907 _, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_)))
908 .WillOnce(
909 [](std::unique_ptr<CommandBuilder> cmd,
910 ContextualOnceCallback<void(CommandCompleteView)> callback) {
911 auto cmd_view =
912 ControllerBqrView::Create(VendorCommandView::Create(
913 CommandView::Create(BuilderToView(std::move(cmd)))));
914 EXPECT_TRUE(cmd_view.IsValid());
915 auto response = ControllerBqrCompleteView::Create(
916 CommandCompleteView::Create(EventView::Create(
917 BuilderToView(ControllerBqrCompleteBuilder::Create(
918 1, ErrorCode::SUCCESS,
919 cmd_view.GetBqrQualityEventMask())))));
920 EXPECT_TRUE(response.IsValid());
921 callback(response);
922 })
923 .RetiresOnSaturation();
924 EXPECT_CALL(
925 hci_,
926 RegisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT, _))
927 .WillOnce(SaveArg<1>(&this->vse_callback_));
928 do_in_main_thread(FROM_HERE, BindOnce([]() {
929 bluetooth::bqr::EnableBtQualityReport(get_main());
930 }));
931 ASSERT_EQ(std::future_status::ready,
932 configuration_done.wait_for(std::chrono::seconds(1)));
933 }
934
TearDown()935 void TearDown() override {
936 std::promise<void> disable_promise;
937 auto disable_future = disable_promise.get_future();
938 auto set_promise = [&disable_promise]() { disable_promise.set_value(); };
939 EXPECT_CALL(
940 hci_,
941 UnregisterVendorSpecificEventHandler(VseSubeventCode::BQR_EVENT));
942 EXPECT_CALL(
943 hci_,
944 EnqueueCommand(
945 _, Matcher<ContextualOnceCallback<void(CommandCompleteView)>>(_)))
946 .WillOnce(Invoke(set_promise))
947 .RetiresOnSaturation();
948 do_in_main_thread(FROM_HERE, BindOnce([]() {
949 bluetooth::bqr::EnableBtQualityReport(nullptr);
950 }));
951 ASSERT_EQ(std::future_status::ready,
952 disable_future.wait_for(std::chrono::seconds(1)));
953
954 bluetooth::hci::testing::mock_hci_layer_ = nullptr;
955 BtifCoreWithControllerTest::TearDown();
956 }
957 bluetooth::hci::testing::MockHciLayer hci_;
958 ContextualCallback<void(VendorSpecificEventView)> vse_callback_;
959 PostableContext* context;
960 };
961
TEST_F(BtifCoreWithVendorSupportTest,configure_bqr_test)962 TEST_F(BtifCoreWithVendorSupportTest, configure_bqr_test) {}
963
TEST_F(BtifCoreWithVendorSupportTest,send_a2dp_audio_choppy)964 TEST_F(BtifCoreWithVendorSupportTest, send_a2dp_audio_choppy) {
965 std::promise<void> a2dp_event_promise;
966 auto event_reported = a2dp_event_promise.get_future();
967 callback_map_["link_quality_report_callback"] = [&a2dp_event_promise]() {
968 a2dp_event_promise.set_value();
969 };
970 auto view = VendorSpecificEventView::Create(
971 EventView::Create(BuilderToView(BqrLinkQualityEventBuilder::Create(
972 QualityReportId::A2DP_AUDIO_CHOPPY, BqrPacketType::TYPE_3DH3, 0x123,
973 Role::CENTRAL, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
974 std::make_unique<RawBuilder>()))));
975 EXPECT_TRUE(view.IsValid());
976 vse_callback_(view);
977 ASSERT_EQ(std::future_status::ready,
978 event_reported.wait_for(std::chrono::seconds(1)));
979 }
980
TEST_F(BtifCoreWithVendorSupportTest,send_lmp_ll_trace)981 TEST_F(BtifCoreWithVendorSupportTest, send_lmp_ll_trace) {
982 auto payload = std::make_unique<RawBuilder>();
983 payload->AddOctets({'d', 'a', 't', 'a'});
984 auto view = VendorSpecificEventView::Create(EventView::Create(BuilderToView(
985 BqrLmpLlMessageTraceEventBuilder::Create(0x123, std::move(payload)))));
986 EXPECT_TRUE(view.IsValid());
987 vse_callback_(view);
988 }
989
990 class BtifCoreVseWithSocketTest : public BtifCoreWithVendorSupportTest {
991 protected:
SetUp()992 void SetUp() override {
993 BtifCoreWithVendorSupportTest::SetUp();
994 int fds[2];
995 ASSERT_EQ(0, socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, fds));
996 write_fd_ = fds[0];
997 read_fd_ = fds[1];
998 }
TearDown()999 void TearDown() override {
1000 BtifCoreWithVendorSupportTest::TearDown();
1001 close(write_fd_);
1002 close(read_fd_);
1003 }
1004 int write_fd_;
1005 int read_fd_;
1006 };
1007
TEST_F(BtifCoreVseWithSocketTest,debug_dump_empty)1008 TEST_F(BtifCoreVseWithSocketTest, debug_dump_empty) {
1009 static int write_fd = write_fd_;
1010 static int read_fd = read_fd_;
1011 auto reading_promise = std::make_unique<std::promise<void>>();
1012 auto reading_done = reading_promise->get_future();
1013
1014 do_in_main_thread(FROM_HERE,
1015 BindOnce([]() { bluetooth::bqr::DebugDump(write_fd); }));
1016 do_in_main_thread(
1017 FROM_HERE, BindOnce(
1018 [](std::unique_ptr<std::promise<void>> done_promise) {
1019 char line_buf[1024] = "";
1020 int bytes_read = read(read_fd, line_buf, 1024);
1021 EXPECT_GT(bytes_read, 0);
1022 EXPECT_NE(
1023 std::string(line_buf).find("Event queue is empty"),
1024 std::string::npos);
1025 done_promise->set_value();
1026 },
1027 std::move(reading_promise)));
1028 EXPECT_EQ(std::future_status::ready,
1029 reading_done.wait_for(std::chrono::seconds(1)));
1030 }
1031
1032 namespace bluetooth::bqr::testing {
1033 void set_lmp_trace_log_fd(int fd);
1034 }
1035
TEST_F(BtifCoreVseWithSocketTest,send_lmp_ll_msg)1036 TEST_F(BtifCoreVseWithSocketTest, send_lmp_ll_msg) {
1037 auto payload = std::make_unique<RawBuilder>();
1038 payload->AddOctets({'d', 'a', 't', 'a'});
1039 auto view = VendorSpecificEventView::Create(EventView::Create(BuilderToView(
1040 BqrLmpLlMessageTraceEventBuilder::Create(0x123, std::move(payload)))));
1041 EXPECT_TRUE(view.IsValid());
1042
1043 static int read_fd = read_fd_;
1044 auto reading_promise = std::make_unique<std::promise<void>>();
1045 auto reading_done = reading_promise->get_future();
1046
1047 static int write_fd = write_fd_;
1048 do_in_main_thread(FROM_HERE, BindOnce([]() {
1049 bluetooth::bqr::testing::set_lmp_trace_log_fd(write_fd);
1050 }));
1051 vse_callback_(view);
1052
1053 do_in_main_thread(FROM_HERE,
1054 BindOnce(
1055 [](std::unique_ptr<std::promise<void>> done_promise) {
1056 char line_buf[1024] = "";
1057 std::string line;
1058 int bytes_read = read(read_fd, line_buf, 1024);
1059 EXPECT_GT(bytes_read, 0);
1060 line = std::string(line_buf);
1061 EXPECT_NE(line.find("Handle: 0x0123"),
1062 std::string::npos);
1063 EXPECT_NE(line.find("data"), std::string::npos);
1064 done_promise->set_value();
1065 },
1066 std::move(reading_promise)));
1067 EXPECT_EQ(std::future_status::ready,
1068 reading_done.wait_for(std::chrono::seconds(1)));
1069 }
1070
TEST_F(BtifCoreVseWithSocketTest,debug_dump_a2dp_choppy_no_payload)1071 TEST_F(BtifCoreVseWithSocketTest, debug_dump_a2dp_choppy_no_payload) {
1072 auto payload = std::make_unique<RawBuilder>();
1073 auto view = VendorSpecificEventView::Create(
1074 EventView::Create(BuilderToView(BqrA2dpAudioChoppyEventBuilder::Create(
1075 BqrPacketType::TYPE_3DH3, 0x123, Role::CENTRAL, 1, 2 /* rssi */, 3, 4,
1076 5 /* afh_select_uni */, 6 /* lsto */, 7, 8, 9, 10,
1077 11 /* last_tx_ack_timestamp */, 12, 13, 14,
1078 15 /* buffer_underflow_bytes */, std::move(payload)))));
1079 EXPECT_TRUE(view.IsValid());
1080 vse_callback_(view);
1081
1082 static int write_fd = write_fd_;
1083 static int read_fd = read_fd_;
1084 auto reading_promise = std::make_unique<std::promise<void>>();
1085 auto reading_done = reading_promise->get_future();
1086
1087 do_in_main_thread(FROM_HERE,
1088 BindOnce([]() { bluetooth::bqr::DebugDump(write_fd); }));
1089 do_in_main_thread(
1090 FROM_HERE,
1091 BindOnce(
1092 [](std::unique_ptr<std::promise<void>> done_promise) {
1093 char line_buf[1024] = "";
1094 std::string line;
1095 int bytes_read = read(read_fd, line_buf, 1024);
1096 EXPECT_GT(bytes_read, 0);
1097 line = std::string(line_buf);
1098 EXPECT_EQ(line.find("Event queue is empty"), std::string::npos);
1099 EXPECT_NE(line.find("Handle: 0x0123"), std::string::npos);
1100 EXPECT_NE(line.find("UndFlow: 15"), std::string::npos);
1101 EXPECT_NE(line.find("A2DP Choppy"), std::string::npos);
1102 done_promise->set_value();
1103 },
1104 std::move(reading_promise)));
1105 EXPECT_EQ(std::future_status::ready,
1106 reading_done.wait_for(std::chrono::seconds(1)));
1107 }
1108
TEST_F(BtifCoreVseWithSocketTest,debug_dump_a2dp_choppy)1109 TEST_F(BtifCoreVseWithSocketTest, debug_dump_a2dp_choppy) {
1110 auto payload = std::make_unique<RawBuilder>();
1111 payload->AddOctets({'d', 'a', 't', 'a'});
1112 auto view = VendorSpecificEventView::Create(
1113 EventView::Create(BuilderToView(BqrA2dpAudioChoppyEventBuilder::Create(
1114 BqrPacketType::TYPE_3DH3, 0x123, Role::CENTRAL, 1, 2, 3, 4, 5, 6, 7,
1115 8, 9, 10, 11, 12, 13, 14, 15, std::move(payload)))));
1116 EXPECT_TRUE(view.IsValid());
1117 vse_callback_(view);
1118
1119 static int write_fd = write_fd_;
1120 static int read_fd = read_fd_;
1121 auto reading_promise = std::make_unique<std::promise<void>>();
1122 auto reading_done = reading_promise->get_future();
1123
1124 do_in_main_thread(FROM_HERE,
1125 BindOnce([]() { bluetooth::bqr::DebugDump(write_fd); }));
1126 do_in_main_thread(
1127 FROM_HERE,
1128 BindOnce(
1129 [](std::unique_ptr<std::promise<void>> done_promise) {
1130 char line_buf[1024] = "";
1131 std::string line;
1132 int bytes_read = read(read_fd, line_buf, 1024);
1133 EXPECT_GT(bytes_read, 0);
1134 line = std::string(line_buf);
1135 EXPECT_EQ(line.find("Event queue is empty"), std::string::npos);
1136 EXPECT_NE(line.find("Handle: 0x0123"), std::string::npos);
1137 EXPECT_NE(line.find("UndFlow: 15"), std::string::npos);
1138 EXPECT_NE(line.find("A2DP Choppy"), std::string::npos);
1139 done_promise->set_value();
1140 },
1141 std::move(reading_promise)));
1142 EXPECT_EQ(std::future_status::ready,
1143 reading_done.wait_for(std::chrono::seconds(1)));
1144 }
1145