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 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <stdlib.h>
19 
20 #include <cstddef>
21 
22 #include "btif/include/btif_storage.h"
23 #include "btif/include/stack_manager_t.h"
24 #include "common/init_flags.h"
25 #include "device/include/interop.h"
26 #include "mock_btif_config.h"
27 #include "osi/include/allocator.h"
28 #include "profile/avrcp/avrcp_config.h"
29 #include "stack/include/avrc_api.h"
30 #include "stack/include/avrc_defs.h"
31 #include "stack/include/bt_types.h"
32 #include "stack/include/bt_uuid16.h"
33 #include "stack/sdp/sdpint.h"
34 #include "test/fake/fake_osi.h"
35 #include "test/mock/mock_btif_config.h"
36 #include "test/mock/mock_osi_allocator.h"
37 #include "test/mock/mock_osi_properties.h"
38 #include "test/mock/mock_stack_l2cap_api.h"
39 
40 #ifndef BT_DEFAULT_BUFFER_SIZE
41 #define BT_DEFAULT_BUFFER_SIZE (4096 + 16)
42 #endif
43 
44 #define INVALID_LENGTH 5
45 #define INVALID_UUID 0X1F
46 #define UUID_HF_LSB 0X1E
47 
48 #define PROFILE_VERSION_POSITION 7
49 #define SDP_PROFILE_DESC_LENGTH 8
50 #define HFP_PROFILE_MINOR_VERSION_6 0x06
51 #define HFP_PROFILE_MINOR_VERSION_7 0x07
52 
53 static int L2CA_ConnectReqWithSecurity_cid = 0x42;
54 static RawAddress addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
55 static tSDP_DISCOVERY_DB* sdp_db = nullptr;
56 
57 using testing::_;
58 using testing::DoAll;
59 using testing::Return;
60 using testing::SetArrayArgument;
61 
62 bool sdp_dynamic_change_hfp_version(const tSDP_ATTRIBUTE* p_attr,
63                                     const RawAddress& remote_address);
64 void hfp_fallback(bool& is_hfp_fallback, const tSDP_ATTRIBUTE* p_attr);
65 
66 void sdp_callback(const RawAddress& bd_addr, tSDP_RESULT result);
67 tCONN_CB* find_ccb(uint16_t cid, uint8_t state);
68 
69 const char* test_flags_feature_disabled[] = {
70     "INIT_dynamic_avrcp_version_enhancement=false",
71     nullptr,
72 };
73 
74 const char* test_flags_feature_enabled[] = {
75     "INIT_dynamic_avrcp_version_enhancement=true",
76     nullptr,
77 };
78 const char* hfp_test_flags_feature_disabled[] = {
79     "INIT_hfp_dynamic_version=false",
80     nullptr,
81 };
82 
83 const char* hfp_test_flags_feature_enabled[] = {
84     "INIT_hfp_dynamic_version=true",
85     nullptr,
86 };
87 
88 namespace {
89 // convenience mock
90 class IopMock {
91  public:
92   MOCK_METHOD(bool, InteropMatchAddr,
93               (const interop_feature_t, const RawAddress*));
94   MOCK_METHOD(bool, InteropMatchName, (const interop_feature_t, const char*));
95   MOCK_METHOD(void, InteropDatabaseAdd, (uint16_t, const RawAddress*, size_t));
96   MOCK_METHOD(void, InteropDatabaseClear, ());
97   MOCK_METHOD(bool, InteropMatchAddrOrName,
98               (const interop_feature_t, const RawAddress*,
99                bt_status_t (*)(const RawAddress*, bt_property_t*)));
100   MOCK_METHOD(bool, InteropMatchManufacturer,
101               (const interop_feature_t, uint16_t));
102   MOCK_METHOD(bool, InteropMatchVendorProductIds,
103               (const interop_feature_t, uint16_t, uint16_t));
104   MOCK_METHOD(bool, InteropDatabaseMatchVersion,
105               (const interop_feature_t, uint16_t));
106   MOCK_METHOD(bool, InteropMatchAddrGetMaxLat,
107               (const interop_feature_t, const RawAddress*, uint16_t*));
108   MOCK_METHOD(bool, InteropGetAllowlistedMediaPlayersList, (list_t*));
109   MOCK_METHOD(int, InteropFeatureNameToFeatureId, (const char*));
110   MOCK_METHOD(void, InteropDatabaseAddAddr,
111               (uint16_t, const RawAddress*, size_t));
112 };
113 
114 class AvrcpVersionMock {
115  public:
116   MOCK_METHOD0(AvrcpProfileVersionMock, uint16_t(void));
117 };
118 
119 std::unique_ptr<IopMock> localIopMock;
120 std::unique_ptr<AvrcpVersionMock> localAvrcpVersionMock;
121 }  // namespace
122 
interop_match_addr(const interop_feature_t feature,const RawAddress * addr)123 bool interop_match_addr(const interop_feature_t feature,
124                         const RawAddress* addr) {
125   return localIopMock->InteropMatchAddr(feature, addr);
126 }
interop_match_name(const interop_feature_t feature,const char * name)127 bool interop_match_name(const interop_feature_t feature, const char* name) {
128   return localIopMock->InteropMatchName(feature, name);
129 }
interop_database_add(uint16_t feature,const RawAddress * addr,size_t length)130 void interop_database_add(uint16_t feature, const RawAddress* addr,
131                           size_t length) {
132   return localIopMock->InteropDatabaseAdd(feature, addr, length);
133 }
interop_database_clear()134 void interop_database_clear() { localIopMock->InteropDatabaseClear(); }
135 
interop_match_addr_or_name(const interop_feature_t feature,const RawAddress * addr,bt_status_t (* get_remote_device_property)(const RawAddress *,bt_property_t *))136 bool interop_match_addr_or_name(const interop_feature_t feature,
137                                 const RawAddress* addr,
138                                 bt_status_t (*get_remote_device_property)(
139                                     const RawAddress*, bt_property_t*)) {
140   return localIopMock->InteropMatchAddrOrName(feature, addr,
141                                               get_remote_device_property);
142 }
143 
interop_match_manufacturer(const interop_feature_t feature,uint16_t manufacturer)144 bool interop_match_manufacturer(const interop_feature_t feature,
145                                 uint16_t manufacturer) {
146   return localIopMock->InteropMatchManufacturer(feature, manufacturer);
147 }
148 
interop_match_vendor_product_ids(const interop_feature_t feature,uint16_t vendor_id,uint16_t product_id)149 bool interop_match_vendor_product_ids(const interop_feature_t feature,
150                                       uint16_t vendor_id, uint16_t product_id) {
151   return localIopMock->InteropMatchVendorProductIds(feature, vendor_id,
152                                                     product_id);
153 }
154 
interop_database_match_version(const interop_feature_t feature,uint16_t version)155 bool interop_database_match_version(const interop_feature_t feature,
156                                     uint16_t version) {
157   return localIopMock->InteropDatabaseMatchVersion(feature, version);
158 }
interop_match_addr_get_max_lat(const interop_feature_t feature,const RawAddress * addr,uint16_t * max_lat)159 bool interop_match_addr_get_max_lat(const interop_feature_t feature,
160                                     const RawAddress* addr, uint16_t* max_lat) {
161   return localIopMock->InteropMatchAddrGetMaxLat(feature, addr, max_lat);
162 }
163 
interop_feature_name_to_feature_id(const char * feature_name)164 int interop_feature_name_to_feature_id(const char* feature_name) {
165   return localIopMock->InteropFeatureNameToFeatureId(feature_name);
166 }
167 
interop_database_add_addr(uint16_t feature,const RawAddress * addr,size_t length)168 void interop_database_add_addr(uint16_t feature, const RawAddress* addr,
169                                size_t length) {
170   return localIopMock->InteropDatabaseAddAddr(feature, addr, length);
171 }
172 
AVRC_GetProfileVersion()173 uint16_t AVRC_GetProfileVersion() {
174   return localAvrcpVersionMock->AvrcpProfileVersionMock();
175 }
176 
177 uint8_t avrc_value[8] = {
178     ((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE),  // data_element
179     6,                                                    // data_len
180     ((UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES),             // uuid_element
181     0,                                                    // uuid
182     0,                                                    // uuid
183     ((UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES),             // version_element
184     0,                                                    // version
185     0                                                     // version
186 };
187 tSDP_ATTRIBUTE avrcp_attr = {
188     .len = 0,
189     .value_ptr = (uint8_t*)(&avrc_value),
190     .id = 0,
191     .type = 0,
192 };
193 
194 uint8_t avrc_feat_value[2] = {
195     0,  // feature
196     0   // feature
197 };
198 tSDP_ATTRIBUTE avrcp_feat_attr = {
199     .len = 0,
200     .value_ptr = (uint8_t*)(&avrc_feat_value),
201     .id = 0,
202     .type = 0,
203 };
204 
205 uint8_t hfp_value[8] = {0, 0, 0, 0x11, 0x1E, 0, 0, 0};
206 
207 tSDP_ATTRIBUTE hfp_attr = {
208     .len = 0,
209     .value_ptr = (uint8_t*)(hfp_value),
210     .id = 0,
211     .type = 0,
212 };
213 
set_hfp_attr(uint32_t len,uint16_t id,uint16_t uuid)214 void set_hfp_attr(uint32_t len, uint16_t id, uint16_t uuid) {
215   hfp_attr.value_ptr[4] = uuid;
216   hfp_attr.len = len;
217   hfp_attr.id = id;
218 }
219 
set_avrcp_feat_attr(uint32_t len,uint16_t id,uint16_t feature)220 void set_avrcp_feat_attr(uint32_t len, uint16_t id, uint16_t feature) {
221   UINT16_TO_BE_FIELD(avrc_feat_value, feature);
222   avrcp_feat_attr.len = len;
223   avrcp_feat_attr.id = id;
224 }
225 
set_avrcp_attr(uint32_t len,uint16_t id,uint16_t uuid,uint16_t version)226 void set_avrcp_attr(uint32_t len, uint16_t id, uint16_t uuid,
227                     uint16_t version) {
228   UINT16_TO_BE_FIELD(avrc_value + 3, uuid);
229   UINT16_TO_BE_FIELD(avrc_value + 6, version);
230   avrcp_attr.len = len;
231   avrcp_attr.id = id;
232 }
233 
get_avrc_target_version(tSDP_ATTRIBUTE * p_attr)234 uint16_t get_avrc_target_version(tSDP_ATTRIBUTE* p_attr) {
235   uint8_t* p_version = p_attr->value_ptr + 6;
236   uint16_t version =
237       (((uint16_t)(*(p_version))) << 8) + ((uint16_t)(*((p_version) + 1)));
238   return version;
239 }
240 
get_avrc_target_feature(tSDP_ATTRIBUTE * p_attr)241 uint16_t get_avrc_target_feature(tSDP_ATTRIBUTE* p_attr) {
242   uint8_t* p_feature = p_attr->value_ptr;
243   uint16_t feature =
244       (((uint16_t)(*(p_feature))) << 8) + ((uint16_t)(*((p_feature) + 1)));
245   return feature;
246 }
247 
248 class StackSdpMockAndFakeTest : public ::testing::Test {
249  protected:
SetUp()250   void SetUp() override {
251     fake_osi_ = std::make_unique<test::fake::FakeOsi>();
252     test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity.body =
253         [](uint16_t /* psm */, const RawAddress& /* p_bd_addr */,
254            uint16_t /* sec_level */) {
255           return ++L2CA_ConnectReqWithSecurity_cid;
256         };
257     test::mock::stack_l2cap_api::L2CA_DataWrite.body = [](uint16_t /* cid */,
258                                                           BT_HDR* p_data) {
259       osi_free_and_reset((void**)&p_data);
260       return 0;
261     };
262     test::mock::stack_l2cap_api::L2CA_DisconnectReq.body =
263         [](uint16_t /* cid */) { return true; };
264     test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity.body =
265         [](uint16_t /* psm */, const tL2CAP_APPL_INFO& /* p_cb_info */,
266            bool /* enable_snoop */, tL2CAP_ERTM_INFO* /* p_ertm_info */,
267            uint16_t /* my_mtu */, uint16_t /* required_remote_mtu */,
268            uint16_t /* sec_level */) {
269           return 42;  // return non zero
270         };
271   }
272 
TearDown()273   void TearDown() override {
274     test::mock::stack_l2cap_api::L2CA_ConnectReqWithSecurity = {};
275     test::mock::stack_l2cap_api::L2CA_RegisterWithSecurity = {};
276     test::mock::stack_l2cap_api::L2CA_DataWrite = {};
277     test::mock::stack_l2cap_api::L2CA_DisconnectReq = {};
278   }
279   std::unique_ptr<test::fake::FakeOsi> fake_osi_;
280 };
281 
282 class StackSdpInitTest : public StackSdpMockAndFakeTest {
283  protected:
SetUp()284   void SetUp() override {
285     StackSdpMockAndFakeTest::SetUp();
286     sdp_init();
287     sdp_db = (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
288   }
289 
TearDown()290   void TearDown() override {
291     osi_free(sdp_db);
292     StackSdpMockAndFakeTest::TearDown();
293   }
294 };
295 
296 class StackSdpUtilsTest : public StackSdpInitTest {
297  protected:
SetUp()298   void SetUp() override {
299     StackSdpInitTest::SetUp();
300     bluetooth::common::InitFlags::Load(hfp_test_flags_feature_disabled);
301     bluetooth::common::InitFlags::Load(test_flags_feature_disabled);
302     GetInterfaceToProfiles()->profileSpecific_HACK->AVRC_GetProfileVersion =
303         AVRC_GetProfileVersion;
304     test::mock::btif_config::btif_config_get_bin.body =
305         [this](const std::string& section, const std::string& key,
306                uint8_t* value, size_t* length) {
307           return btif_config_interface_.GetBin(section, key, value, length);
308         };
309     test::mock::btif_config::btif_config_get_bin_length.body =
310         [this](const std::string& section, const std::string& key) {
311           return btif_config_interface_.GetBinLength(section, key);
312         };
313     test::mock::osi_properties::osi_property_get_bool.body =
314         [](const char* /* key */, bool /* default_value */) { return true; };
315 
316     localIopMock = std::make_unique<IopMock>();
317     localAvrcpVersionMock = std::make_unique<AvrcpVersionMock>();
318     set_avrcp_attr(8, ATTR_ID_BT_PROFILE_DESC_LIST,
319                    UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
320     set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
321     set_hfp_attr(SDP_PROFILE_DESC_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST,
322                  UUID_HF_LSB);
323   }
324 
TearDown()325   void TearDown() override {
326     GetInterfaceToProfiles()->profileSpecific_HACK->AVRC_GetProfileVersion =
327         nullptr;
328     test::mock::btif_config::btif_config_get_bin_length = {};
329     test::mock::btif_config::btif_config_get_bin = {};
330     test::mock::osi_properties::osi_property_get_bool = {};
331 
332     localIopMock.reset();
333     localAvrcpVersionMock.reset();
334     StackSdpInitTest::TearDown();
335   }
336   bluetooth::manager::MockBtifConfigInterface btif_config_interface_;
337 };
338 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_device_in_iop_table_versoin_1_4)339 TEST_F(StackSdpUtilsTest,
340        sdpu_set_avrc_target_version_device_in_iop_table_versoin_1_4) {
341   RawAddress bdaddr;
342   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
343       .WillOnce(Return(true));
344   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
345   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_4);
346 }
347 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_device_in_iop_table_versoin_1_3)348 TEST_F(StackSdpUtilsTest,
349        sdpu_set_avrc_target_version_device_in_iop_table_versoin_1_3) {
350   RawAddress bdaddr;
351   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
352       .WillOnce(Return(false));
353   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
354       .WillOnce(Return(true));
355   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
356   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_3);
357 }
358 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_wrong_len)359 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_wrong_len) {
360   RawAddress bdaddr;
361   set_avrcp_attr(5, ATTR_ID_BT_PROFILE_DESC_LIST,
362                  UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
363   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
364   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
365 }
366 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_wrong_attribute_id)367 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_wrong_attribute_id) {
368   RawAddress bdaddr;
369   set_avrcp_attr(8, ATTR_ID_SERVICE_CLASS_ID_LIST,
370                  UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
371   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
372   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
373 }
374 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_wrong_uuid)375 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_wrong_uuid) {
376   RawAddress bdaddr;
377   set_avrcp_attr(8, ATTR_ID_BT_PROFILE_DESC_LIST, UUID_SERVCLASS_AUDIO_SOURCE,
378                  AVRC_REV_1_5);
379   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
380   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
381 }
382 
383 // device's controller version older than our target version
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_device_older_version)384 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_device_older_version) {
385   RawAddress bdaddr;
386   uint8_t config_0104[2] = {0x04, 0x01};
387   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
388       .WillOnce(Return(false));
389   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
390       .WillOnce(Return(false));
391   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
392       .WillOnce(Return(2));
393   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
394       .WillOnce(DoAll(SetArrayArgument<2>(config_0104, config_0104 + 2),
395                       Return(true)));
396   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
397   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_4);
398 }
399 
400 // device's controller version same as our target version
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_device_same_version)401 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_device_same_version) {
402   RawAddress bdaddr;
403   uint8_t config_0105[2] = {0x05, 0x01};
404   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
405       .WillOnce(Return(false));
406   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
407       .WillOnce(Return(false));
408   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
409       .WillOnce(Return(2));
410   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
411       .WillOnce(DoAll(SetArrayArgument<2>(config_0105, config_0105 + 2),
412                       Return(true)));
413   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
414   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
415 }
416 
417 // device's controller version higher than our target version
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_device_newer_version)418 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_device_newer_version) {
419   RawAddress bdaddr;
420   uint8_t config_0106[2] = {0x06, 0x01};
421   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
422       .WillOnce(Return(false));
423   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
424       .WillOnce(Return(false));
425   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
426       .WillOnce(Return(2));
427   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
428       .WillOnce(DoAll(SetArrayArgument<2>(config_0106, config_0106 + 2),
429                       Return(true)));
430   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
431   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
432 }
433 
434 // cannot read device's controller version from bt_config
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_no_config_value)435 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_no_config_value) {
436   RawAddress bdaddr;
437   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
438       .WillOnce(Return(false));
439   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
440       .WillOnce(Return(false));
441   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
442       .WillOnce(Return(0));
443   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
444   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
445 }
446 
447 // read device's controller version from bt_config return only 1 byte
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_config_value_1_byte)448 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_config_value_1_byte) {
449   RawAddress bdaddr;
450   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
451       .WillOnce(Return(false));
452   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
453       .WillOnce(Return(false));
454   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
455       .WillOnce(Return(1));
456   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
457   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
458 }
459 
460 // read device's controller version from bt_config return 3 bytes
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_config_value_3_bytes)461 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_config_value_3_bytes) {
462   RawAddress bdaddr;
463   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
464       .WillOnce(Return(false));
465   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
466       .WillOnce(Return(false));
467   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
468       .WillOnce(Return(3));
469   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
470   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
471 }
472 
473 // cached controller version is not valid
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_version_config_value_not_valid)474 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_version_config_value_not_valid) {
475   RawAddress bdaddr;
476   uint8_t config_not_valid[2] = {0x12, 0x34};
477   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
478       .WillOnce(Return(false));
479   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
480       .WillOnce(Return(false));
481   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
482       .WillOnce(Return(2));
483   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
484       .WillOnce(
485           DoAll(SetArrayArgument<2>(config_not_valid, config_not_valid + 2),
486                 Return(true)));
487   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
488   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
489 }
490 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_wrong_len)491 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_feature_wrong_len) {
492   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
493   RawAddress bdaddr;
494   set_avrcp_attr(8, ATTR_ID_BT_PROFILE_DESC_LIST,
495                  UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
496   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
497   set_avrcp_feat_attr(6, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
498   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
499   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
500                                 get_avrc_target_version(&avrcp_attr));
501   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_5);
502 }
503 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_wrong_attribute_id)504 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_feature_wrong_attribute_id) {
505   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
506   RawAddress bdaddr;
507   set_avrcp_attr(8, ATTR_ID_BT_PROFILE_DESC_LIST,
508                  UUID_SERVCLASS_AV_REMOTE_CONTROL, AVRC_REV_1_5);
509   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
510   set_avrcp_feat_attr(2, ATTR_ID_BT_PROFILE_DESC_LIST, AVRCP_SUPF_TG_1_5);
511   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
512   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
513                                 get_avrc_target_version(&avrcp_attr));
514   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_5);
515 }
516 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_device_in_iop_table_versoin_1_4)517 TEST_F(StackSdpUtilsTest,
518        sdpu_set_avrc_target_feature_device_in_iop_table_versoin_1_4) {
519   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
520   RawAddress bdaddr;
521   uint8_t feature_0105[2] = {0xC1, 0x00};
522   EXPECT_CALL(*localAvrcpVersionMock, AvrcpProfileVersionMock())
523       .WillOnce(Return(AVRC_REV_1_5));
524   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
525       .WillOnce(Return(true));
526   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
527   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_4);
528   set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
529   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
530       .WillOnce(Return(2));
531   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
532       .WillOnce(DoAll(SetArrayArgument<2>(feature_0105, feature_0105 + 2),
533                       Return(true)));
534   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
535                                 get_avrc_target_version(&avrcp_attr));
536   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_4);
537 }
538 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_device_in_iop_table_versoin_1_3)539 TEST_F(StackSdpUtilsTest,
540        sdpu_set_avrc_target_feature_device_in_iop_table_versoin_1_3) {
541   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
542   RawAddress bdaddr;
543   uint8_t feature_0105[2] = {0xC1, 0x00};
544   EXPECT_CALL(*localAvrcpVersionMock, AvrcpProfileVersionMock())
545       .WillOnce(Return(AVRC_REV_1_5));
546   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
547       .WillOnce(Return(false));
548   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
549       .WillOnce(Return(true));
550   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
551   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_3);
552   set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
553   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
554       .WillOnce(Return(2));
555   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
556       .WillOnce(DoAll(SetArrayArgument<2>(feature_0105, feature_0105 + 2),
557                       Return(true)));
558   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
559                                 get_avrc_target_version(&avrcp_attr));
560   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_3);
561 }
562 
563 // cannot read device's controller feature from bt_config
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_no_config_value)564 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_feature_no_config_value) {
565   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
566   RawAddress bdaddr;
567   EXPECT_CALL(*localAvrcpVersionMock, AvrcpProfileVersionMock())
568       .WillOnce(Return(AVRC_REV_1_5));
569   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
570   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
571   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
572       .WillOnce(Return(0));
573   set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
574   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
575                                 get_avrc_target_version(&avrcp_attr));
576   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_5);
577 }
578 
579 // read device's controller feature from bt_config return only 1 byte
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_config_value_1_byte)580 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_feature_config_value_1_byte) {
581   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
582   RawAddress bdaddr;
583   EXPECT_CALL(*localAvrcpVersionMock, AvrcpProfileVersionMock())
584       .WillOnce(Return(AVRC_REV_1_5));
585   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
586   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_5);
587   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
588       .WillOnce(Return(1));
589   set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
590   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
591                                 get_avrc_target_version(&avrcp_attr));
592   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr), AVRCP_SUPF_TG_1_5);
593 }
594 
TEST_F(StackSdpUtilsTest,sdpu_set_avrc_target_feature_device_versoin_1_6)595 TEST_F(StackSdpUtilsTest, sdpu_set_avrc_target_feature_device_versoin_1_6) {
596   bluetooth::common::InitFlags::Load(test_flags_feature_enabled);
597   RawAddress bdaddr;
598   uint8_t config_0106[2] = {0x06, 0x01};
599   uint8_t feature_0106[2] = {0xC1, 0x01};
600   EXPECT_CALL(*localAvrcpVersionMock, AvrcpProfileVersionMock())
601       .WillOnce(Return(AVRC_REV_1_6));
602   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_4_ONLY, &bdaddr))
603       .WillOnce(Return(false));
604   EXPECT_CALL(*localIopMock, InteropMatchAddr(INTEROP_AVRCP_1_3_ONLY, &bdaddr))
605       .WillOnce(Return(false));
606   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
607       .WillOnce(Return(2));
608   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
609       .WillOnce(DoAll(SetArrayArgument<2>(config_0106, config_0106 + 2),
610                       Return(true)));
611   sdpu_set_avrc_target_version(&avrcp_attr, &bdaddr);
612   ASSERT_EQ(get_avrc_target_version(&avrcp_attr), AVRC_REV_1_6);
613   set_avrcp_feat_attr(2, ATTR_ID_SUPPORTED_FEATURES, AVRCP_SUPF_TG_1_5);
614   EXPECT_CALL(btif_config_interface_, GetBinLength(bdaddr.ToString(), _))
615       .WillOnce(Return(2));
616   EXPECT_CALL(btif_config_interface_, GetBin(bdaddr.ToString(), _, _, _))
617       .WillOnce(DoAll(SetArrayArgument<2>(feature_0106, feature_0106 + 2),
618                       Return(true)));
619   sdpu_set_avrc_target_features(&avrcp_feat_attr, &bdaddr,
620                                 get_avrc_target_version(&avrcp_attr));
621   ASSERT_EQ(get_avrc_target_feature(&avrcp_feat_attr),
622             AVRCP_SUPF_TG_1_6 | AVRC_SUPF_TG_PLAYER_COVER_ART);
623 }
624 
TEST_F(StackSdpUtilsTest,dynamic_hfp_version_with_invalid_length)625 TEST_F(StackSdpUtilsTest, dynamic_hfp_version_with_invalid_length) {
626   bluetooth::common::InitFlags::Load(hfp_test_flags_feature_enabled);
627   RawAddress bdaddr(RawAddress::kEmpty);
628   set_hfp_attr(INVALID_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST, UUID_HF_LSB);
629   ASSERT_EQ(sdp_dynamic_change_hfp_version(&hfp_attr, bdaddr), false);
630 }
631 
TEST_F(StackSdpUtilsTest,dynamic_hfp_version_with_invalid_UUID)632 TEST_F(StackSdpUtilsTest, dynamic_hfp_version_with_invalid_UUID) {
633   bluetooth::common::InitFlags::Load(hfp_test_flags_feature_enabled);
634   RawAddress bdaddr(RawAddress::kEmpty);
635   set_hfp_attr(SDP_PROFILE_DESC_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST,
636                INVALID_UUID);
637   ASSERT_EQ(sdp_dynamic_change_hfp_version(&hfp_attr, bdaddr), false);
638 }
639 
TEST_F(StackSdpUtilsTest,check_HFP_version_change_fail)640 TEST_F(StackSdpUtilsTest, check_HFP_version_change_fail) {
641   bluetooth::common::InitFlags::Load(hfp_test_flags_feature_enabled);
642   RawAddress bdaddr(RawAddress::kEmpty);
643   set_hfp_attr(SDP_PROFILE_DESC_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST,
644                UUID_HF_LSB);
645   test::mock::osi_properties::osi_property_get_bool.body =
646       [](const char* /* key */, bool /* default_value */) { return false; };
647   EXPECT_CALL(*localIopMock,
648               InteropMatchAddrOrName(INTEROP_HFP_1_7_ALLOWLIST, &bdaddr,
649                                      &btif_storage_get_remote_device_property))
650       .WillOnce(Return(false));
651   EXPECT_CALL(*localIopMock,
652               InteropMatchAddrOrName(INTEROP_HFP_1_9_ALLOWLIST, &bdaddr,
653                                      &btif_storage_get_remote_device_property))
654       .WillOnce(Return(false));
655   ASSERT_EQ(sdp_dynamic_change_hfp_version(&hfp_attr, bdaddr), false);
656 }
657 
TEST_F(StackSdpUtilsTest,check_HFP_version_change_success)658 TEST_F(StackSdpUtilsTest, check_HFP_version_change_success) {
659   bluetooth::common::InitFlags::Load(hfp_test_flags_feature_enabled);
660   RawAddress bdaddr(RawAddress::kEmpty);
661   set_hfp_attr(SDP_PROFILE_DESC_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST,
662                UUID_HF_LSB);
663   EXPECT_CALL(*localIopMock,
664               InteropMatchAddrOrName(INTEROP_HFP_1_7_ALLOWLIST, &bdaddr,
665                                      &btif_storage_get_remote_device_property))
666       .WillOnce(Return(true));
667   EXPECT_CALL(*localIopMock,
668               InteropMatchAddrOrName(INTEROP_HFP_1_9_ALLOWLIST, &bdaddr,
669                                      &btif_storage_get_remote_device_property))
670       .WillOnce(Return(true));
671   ASSERT_EQ(sdp_dynamic_change_hfp_version(&hfp_attr, bdaddr), true);
672 }
673 
TEST_F(StackSdpUtilsTest,check_HFP_version_fallback_success)674 TEST_F(StackSdpUtilsTest, check_HFP_version_fallback_success) {
675   bluetooth::common::InitFlags::Load(hfp_test_flags_feature_enabled);
676   RawAddress bdaddr(RawAddress::kEmpty);
677   set_hfp_attr(SDP_PROFILE_DESC_LENGTH, ATTR_ID_BT_PROFILE_DESC_LIST,
678                UUID_HF_LSB);
679   EXPECT_CALL(*localIopMock,
680               InteropMatchAddrOrName(INTEROP_HFP_1_7_ALLOWLIST, &bdaddr,
681                                      &btif_storage_get_remote_device_property))
682       .WillOnce(Return(true));
683   EXPECT_CALL(*localIopMock,
684               InteropMatchAddrOrName(INTEROP_HFP_1_9_ALLOWLIST, &bdaddr,
685                                      &btif_storage_get_remote_device_property))
686       .WillOnce(Return(true));
687   bool is_hfp_fallback = sdp_dynamic_change_hfp_version(&hfp_attr, bdaddr);
688   ASSERT_EQ(hfp_attr.value_ptr[PROFILE_VERSION_POSITION],
689             HFP_PROFILE_MINOR_VERSION_7);
690   hfp_fallback(is_hfp_fallback, &hfp_attr);
691   ASSERT_EQ(hfp_attr.value_ptr[PROFILE_VERSION_POSITION],
692             HFP_PROFILE_MINOR_VERSION_6);
693 }
694 
TEST_F(StackSdpUtilsTest,sdpu_compare_uuid_with_attr_u16)695 TEST_F(StackSdpUtilsTest, sdpu_compare_uuid_with_attr_u16) {
696   tSDP_DISC_ATTR attr = {
697       .p_next_attr = nullptr,
698       .attr_id = 0,
699       .attr_len_type = bluetooth::Uuid::kNumBytes16,
700       .attr_value =
701           {
702               .v =
703                   {
704                       .u16 = 0x1234,
705                   },
706           },
707   };
708 
709   bool is_valid{false};
710   bluetooth::Uuid uuid = bluetooth::Uuid::FromString("1234", &is_valid);
711 
712   ASSERT_EQ(uuid.As16Bit(), attr.attr_value.v.u16);
713   ASSERT_TRUE(is_valid);
714   ASSERT_TRUE(sdpu_compare_uuid_with_attr(uuid, &attr));
715 }
716 
TEST_F(StackSdpUtilsTest,sdpu_compare_uuid_with_attr_u32)717 TEST_F(StackSdpUtilsTest, sdpu_compare_uuid_with_attr_u32) {
718   tSDP_DISC_ATTR attr = {
719       .p_next_attr = nullptr,
720       .attr_id = 0,
721       .attr_len_type = bluetooth::Uuid::kNumBytes32,
722       .attr_value =
723           {
724               .v =
725                   {
726                       .u32 = 0x12345678,
727                   },
728           },
729   };
730 
731   bool is_valid{false};
732   bluetooth::Uuid uuid = bluetooth::Uuid::FromString("12345678", &is_valid);
733 
734   ASSERT_EQ(uuid.As32Bit(), attr.attr_value.v.u32);
735   ASSERT_TRUE(is_valid);
736   ASSERT_TRUE(sdpu_compare_uuid_with_attr(uuid, &attr));
737 }
738 
TEST_F(StackSdpUtilsTest,sdpu_compare_uuid_with_attr_u128)739 TEST_F(StackSdpUtilsTest, sdpu_compare_uuid_with_attr_u128) {
740   tSDP_DISC_ATTR* p_attr =
741       (tSDP_DISC_ATTR*)calloc(1, sizeof(tSDP_DISC_ATTR) + 16);
742   tSDP_DISC_ATTR attr = {
743       .p_next_attr = nullptr,
744       .attr_id = 0,
745       .attr_len_type = bluetooth::Uuid::kNumBytes128,
746       .attr_value = {},
747   };
748 
749   uint8_t data[] = {
750       0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
751       0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
752   };
753 
754   memcpy(p_attr, &attr, sizeof(tSDP_DISC_ATTR));
755   memcpy(p_attr->attr_value.v.array, data, 16);
756 
757   bool is_valid{false};
758   bluetooth::Uuid uuid = bluetooth::Uuid::FromString(
759       "12345678-9abc-def0-1234-56789abcdef0", &is_valid);
760 
761   ASSERT_EQ(0,
762             memcmp(uuid.To128BitBE().data(), (void*)p_attr->attr_value.v.array,
763                    bluetooth::Uuid::kNumBytes128));
764   ASSERT_TRUE(is_valid);
765   ASSERT_TRUE(sdpu_compare_uuid_with_attr(uuid, p_attr));
766 
767   free(p_attr);
768 }
769