1 /*
2  * Copyright 2024 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 "discovery/device/bt_property.h"
18 
19 #include <cstdint>
20 #include <future>
21 
22 #include "gtest/gtest.h"
23 #include "hardware/bluetooth.h"
24 #include "os/log.h"
25 #include "stack/include/bt_name.h"
26 
27 using namespace bluetooth::property;
28 
29 namespace {
30 
31 constexpr size_t kNumberTestedProperties = 21;
32 
33 constexpr size_t kBdPropNameLength = kBdNameLength + sizeof(kBdNameDelim);
34 
35 constexpr uint8_t kReallyLongName[kBdPropNameLength] =
36     "aaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaa"
37     "aaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaa"
38     "aAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaaaAaaaaaaaa";
39 
40 // BT_PROPERTY_BDNAME
41 constexpr BD_NAME kBdName{'k', 'B', 'd', 'N', 'a', 'm', 'e', '\0'};
42 
43 // BT_PROPERTY_BDADDR
44 const RawAddress kRawAddress{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}};
45 
46 // BT_PROPERTY_UUIDS
47 const bluetooth::Uuid uuids[] = {
48     {bluetooth::Uuid::FromString("00000001-1001-1000-8000-00805f9b34fb")},
49     {bluetooth::Uuid::FromString("00000001-1002-1000-8000-00805f9b34fb")},
50     {bluetooth::Uuid::FromString("00000001-1003-1000-8000-00805f9b34fb")},
51 };
52 const std::vector<bluetooth::Uuid> kUuids(uuids, uuids + sizeof(uuids) / sizeof(uuids[0]));
53 
54 // BT_PROPERTY_CLASS_OF_DEVICE
55 constexpr uint32_t kClassOfDevice{0x99663300};
56 
57 // BT_PROPERTY_TYPE_OF_DEVICE
58 constexpr bt_device_type_t kTypeOfDevice{BT_DEVICE_DEVTYPE_BREDR};
59 
60 // BT_PROPERTY_SERVICE_RECORD
61 const bt_service_record_t kServiceRecord{
62     .uuid = uuids[0],
63     .channel = 0x1234,
64     .name = {'k', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'R', 'e',
65              'c', 'o', 'r', 'd', '.', 'n', 'a', 'm', 'e', '\0'},
66 };
67 
68 // BT_PROPERTY_ADAPTER_SCAN_MODE
69 constexpr bt_scan_mode_t kAdapterScanMode{BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE};
70 //
71 // BT_PROPERTY_ADAPTER_BONDED_DEVICES
72 const RawAddress kAdapterBondedDevices[] = {
73     {{0x11, 0x22, 0x33, 0x44, 0x55}},
74     {{0x12, 0x22, 0x33, 0x44, 0x55}},
75     {{0x13, 0x22, 0x33, 0x44, 0x55}},
76     {{0x14, 0x22, 0x33, 0x44, 0x55}},
77     {{0x15, 0x22, 0x33, 0x44, 0x55}},
78 };
79 constexpr size_t kNumBondedDevices =
80     sizeof(kAdapterBondedDevices) / sizeof(kAdapterBondedDevices[0]);
81 
82 // BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT
83 constexpr uint32_t kAdapterDiscoverableTimeout{0x4488cc00};
84 
85 // BT_PROPERTY_REMOTE_FRIENDLY_NAME
86 const uint8_t kRemoteFriendlyName[] = {'k', 'R', 'e', 'm', 'o', 't', 'e', 'F', 'r', 'i',
87                                        'e', 'n', 'd', 'l', 'y', 'N', 'a', 'm', 'e', '\0'};
88 
89 // BT_PROPERTY_REMOTE_RSSI
90 constexpr int8_t kRemoteRssi{0x10};
91 
92 // BT_PROPERTY_REMOTE_VERSION_INFO
93 bt_remote_version_t kRemoteVersionInfo{
94     .version = 1,
95     .sub_ver = 2,
96     .manufacturer = 3,
97 };
98 
99 // BT_PROPERTY_LOCAL_LE_FEATURES
100 constexpr bt_local_le_features_t kLocalLeFeatures{
101     .version_supported = 0x1234,
102     .local_privacy_enabled = 0x11,
103     .max_adv_instance = 0x22,
104     .rpa_offload_supported = 0x33,
105     .max_irk_list_size = 0x44,
106     .max_adv_filter_supported = 0x55,
107     .activity_energy_info_supported = 0x66,
108     .scan_result_storage_size = 0x5678,
109     .total_trackable_advertisers = 0x9abc,
110     .extended_scan_support = true,
111     .debug_logging_supported = true,
112     .le_2m_phy_supported = true,
113     .le_coded_phy_supported = true,
114     .le_extended_advertising_supported = true,
115     .le_periodic_advertising_supported = true,
116     .le_maximum_advertising_data_length = 0x1357,
117     .dynamic_audio_buffer_supported = 0x22446688,
118     .le_periodic_advertising_sync_transfer_sender_supported = true,
119     .le_connected_isochronous_stream_central_supported = true,
120     .le_isochronous_broadcast_supported = true,
121     .le_periodic_advertising_sync_transfer_recipient_supported = true,
122     .adv_filter_extended_features_mask = 0x3366,
123     .le_channel_sounding_supported = true,
124 };
125 
126 // BT_PROPERTY_RESERVED_0F
127 // BT_PROPERTY_DYNAMIC_AUDIO_BUFFER
128 
129 // BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER
130 constexpr bool kRemoteIsCoordinatedSetMember{true};
131 
132 // BT_PROPERTY_APPEARANCE
133 constexpr uint16_t kAppearance{0x44};
134 
135 // BT_PROPERTY_VENDOR_PRODUCT_INFO
136 constexpr bt_vendor_product_info_t kVendorProductInfo{
137     .vendor_id_src = 0x02,
138     .vendor_id = 0x1235,
139     .product_id = 0x5679,
140     .version = 0x9abd,
141 };
142 
143 // BT_PROPERTY_REMOTE_ASHA_CAPABILITY
144 constexpr int16_t kRemoteAshaCapability{0x89};
145 
146 // BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID
147 constexpr uint32_t kRemoteAshaTruncatedHisyncId{0x22446688};
148 
149 // BT_PROPERTY_REMOTE_MODEL_NUM
150 constexpr bt_bdname_t kRemoteModelNum{
151     .name = {'k', 'R', 'e', 'm', 'o', 't', 'e', 'M', 'o', 'd', 'e', 'l', 'N', 'u', 'm', '\0'},
152 };
153 
154 // BT_PROPERTY_REMOTE_ADDR_TYPE
155 constexpr uint8_t kRemoteAddrType{0x55};
156 
157 // BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP
158 constexpr int kRemoteDeviceTimestamp{0x12345678};
159 
160 // Fill the given property type with the well known property data set
fill_property(const bt_property_type_t & type,std::vector<std::shared_ptr<BtProperty>> & properties)161 void fill_property(
162     const bt_property_type_t& type, std::vector<std::shared_ptr<BtProperty>>& properties) {
163   switch (type) {
164     case BT_PROPERTY_BDNAME: {
165       properties.push_back(BdName::Create(kBdName));
166       ASSERT_EQ(kBdPropNameLength, properties.back()->Size());
167     } break;
168 
169     case BT_PROPERTY_BDADDR:
170       properties.push_back(BdAddr::Create(kRawAddress));
171       ASSERT_EQ(sizeof(RawAddress), properties.back()->Size());
172       break;
173 
174     case BT_PROPERTY_UUIDS: {
175       properties.push_back(Uuids::Create(kUuids));
176       ASSERT_EQ(sizeof(bluetooth::Uuid) * kUuids.size(), properties.back()->Size());
177     } break;
178 
179     case BT_PROPERTY_CLASS_OF_DEVICE:
180       properties.push_back(ClassOfDevice::Create(kClassOfDevice));
181       ASSERT_EQ(sizeof(uint32_t), properties.back()->Size());
182       break;
183 
184     case BT_PROPERTY_TYPE_OF_DEVICE:
185       properties.push_back(TypeOfDevice::Create(kTypeOfDevice));
186       ASSERT_EQ(sizeof(bt_device_type_t), properties.back()->Size());
187       break;
188 
189     case BT_PROPERTY_SERVICE_RECORD:
190       properties.push_back(ServiceRecord::Create(kServiceRecord));
191       ASSERT_EQ(sizeof(bt_service_record_t), properties.back()->Size());
192       break;
193 
194     case BT_PROPERTY_ADAPTER_SCAN_MODE:
195       properties.push_back(AdapterScanMode::Create(kAdapterScanMode));
196       ASSERT_EQ(sizeof(bt_scan_mode_t), properties.back()->Size());
197       break;
198 
199     case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
200       properties.push_back(AdapterBondedDevices::Create(kAdapterBondedDevices, kNumBondedDevices));
201       ASSERT_EQ(sizeof(RawAddress) * kNumBondedDevices, properties.back()->Size());
202     } break;
203 
204     case BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
205       properties.push_back(AdapterDiscoverableTimeout::Create(kAdapterDiscoverableTimeout));
206       ASSERT_EQ(sizeof(uint32_t), properties.back()->Size());
207       break;
208 
209     case BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
210       properties.push_back(
211           RemoteFriendlyName::Create(kRemoteFriendlyName, sizeof(kRemoteFriendlyName)));
212       ASSERT_EQ(sizeof(kRemoteFriendlyName) + sizeof(kBdNameDelim), properties.back()->Size());
213     } break;
214 
215     case BT_PROPERTY_REMOTE_RSSI:
216       properties.push_back(RemoteRSSI::Create(kRemoteRssi));
217       ASSERT_EQ(sizeof(int8_t), properties.back()->Size());
218       break;
219 
220     case BT_PROPERTY_REMOTE_VERSION_INFO:
221       properties.push_back(RemoteVersionInfo::Create(kRemoteVersionInfo));
222       ASSERT_EQ(sizeof(bt_remote_version_t), properties.back()->Size());
223       break;
224 
225     case BT_PROPERTY_LOCAL_LE_FEATURES:
226       properties.push_back(LocalLeFeatures::Create(kLocalLeFeatures));
227       ASSERT_EQ(sizeof(kLocalLeFeatures), properties.back()->Size());
228       break;
229 
230     case BT_PROPERTY_RESERVED_0E:
231     case BT_PROPERTY_RESERVED_0F:
232     case BT_PROPERTY_DYNAMIC_AUDIO_BUFFER:
233       break;
234 
235     case BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER:
236       properties.push_back(RemoteIsCoordinatedSetMember::Create(kRemoteIsCoordinatedSetMember));
237       ASSERT_EQ(sizeof(kRemoteIsCoordinatedSetMember), properties.back()->Size());
238       break;
239 
240     case BT_PROPERTY_APPEARANCE:
241       properties.push_back(Appearance::Create(kAppearance));
242       ASSERT_EQ(sizeof(kAppearance), properties.back()->Size());
243       break;
244 
245     case BT_PROPERTY_VENDOR_PRODUCT_INFO:
246       properties.push_back(VendorProductInfo::Create(kVendorProductInfo));
247       ASSERT_EQ(sizeof(kVendorProductInfo), properties.back()->Size());
248       break;
249 
250     case BT_PROPERTY_REMOTE_ASHA_CAPABILITY:
251       properties.push_back(RemoteASHACapability::Create(kRemoteAshaCapability));
252       ASSERT_EQ(sizeof(kRemoteAshaCapability), properties.back()->Size());
253       break;
254 
255     case BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID:
256       properties.push_back(RemoteASHATruncatedHiSyncId::Create(kRemoteAshaTruncatedHisyncId));
257       ASSERT_EQ(sizeof(kRemoteAshaTruncatedHisyncId), properties.back()->Size());
258       break;
259 
260     case BT_PROPERTY_REMOTE_MODEL_NUM: {
261       properties.push_back(RemoteModelNum::Create(kRemoteModelNum));
262       ASSERT_EQ(sizeof(kRemoteModelNum), properties.back()->Size());
263     } break;
264 
265     case BT_PROPERTY_REMOTE_ADDR_TYPE:
266       properties.push_back(RemoteAddrType::Create(kRemoteAddrType));
267       ASSERT_EQ(sizeof(kRemoteAddrType), properties.back()->Size());
268       break;
269 
270     case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
271       properties.push_back(RemoteDeviceTimestamp::Create(kRemoteDeviceTimestamp));
272       ASSERT_EQ(sizeof(kRemoteDeviceTimestamp), properties.back()->Size());
273       break;
274 
275     default:
276       FAIL() << "Illegal property type:" << type;
277       break;
278   }
279 }
280 
281 // Verify the given property type with the well known property data set
verify_property(const bt_property_type_t & type,const bt_property_t & property)282 void verify_property(const bt_property_type_t& type, const bt_property_t& property) {
283   ASSERT_EQ(type, property.type);
284   switch (property.type) {
285     case BT_PROPERTY_BDNAME:
286       ASSERT_EQ((int)kBdPropNameLength, property.len);
287       ASSERT_STREQ((const char*)kBdName, (const char*)property.val);
288       break;
289 
290     case BT_PROPERTY_BDADDR:
291       ASSERT_EQ((int)sizeof(RawAddress), property.len);
292       ASSERT_EQ(kRawAddress, *((RawAddress*)property.val));
293       break;
294 
295     case BT_PROPERTY_UUIDS: {
296       ASSERT_EQ((int)(sizeof(bluetooth::Uuid) * kUuids.size()), property.len);
297       const bluetooth::Uuid* uuid = (const bluetooth::Uuid*)property.val;
298       ASSERT_EQ(uuids[0], *uuid++);
299       ASSERT_EQ(uuids[1], *uuid++);
300       ASSERT_EQ(uuids[2], *uuid++);
301     } break;
302 
303     case BT_PROPERTY_CLASS_OF_DEVICE:
304       ASSERT_EQ((int)sizeof(uint32_t), property.len);
305       ASSERT_EQ(kClassOfDevice, *((uint32_t*)property.val));
306       break;
307 
308     case BT_PROPERTY_TYPE_OF_DEVICE:
309       ASSERT_EQ((int)sizeof(uint32_t), property.len);
310       ASSERT_EQ(kTypeOfDevice, *((uint32_t*)property.val));
311       break;
312 
313     case BT_PROPERTY_SERVICE_RECORD:
314       ASSERT_EQ((int)sizeof(bt_service_record_t), property.len);
315       ASSERT_EQ(kServiceRecord.uuid, ((bt_service_record_t*)property.val)->uuid);
316       ASSERT_EQ(kServiceRecord.channel, ((bt_service_record_t*)property.val)->channel);
317       ASSERT_STREQ(kServiceRecord.name, ((bt_service_record_t*)property.val)->name);
318       break;
319 
320     case BT_PROPERTY_ADAPTER_SCAN_MODE:
321       ASSERT_EQ((int)sizeof(bt_scan_mode_t), property.len);
322       ASSERT_EQ(kAdapterScanMode, *((bt_scan_mode_t*)property.val));
323       break;
324 
325     case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
326       ASSERT_EQ((int)sizeof(kAdapterBondedDevices), property.len);
327       const RawAddress* raw_address = static_cast<RawAddress*>(property.val);
328       for (size_t i = 0; i < kNumBondedDevices; i++, raw_address++) {
329         ASSERT_EQ(kAdapterBondedDevices[i], *raw_address);
330       }
331     } break;
332 
333     case BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
334       ASSERT_EQ((int)sizeof(uint32_t), property.len);
335       ASSERT_EQ(kAdapterDiscoverableTimeout, *((uint32_t*)property.val));
336       break;
337 
338     case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
339       ASSERT_EQ((int)(sizeof(kRemoteFriendlyName) + sizeof(kBdNameDelim)), property.len);
340       ASSERT_STREQ((const char*)kRemoteFriendlyName, (const char*)property.val);
341       break;
342 
343     case BT_PROPERTY_REMOTE_RSSI:
344       ASSERT_EQ((int)sizeof(int8_t), property.len);
345       ASSERT_EQ(kRemoteRssi, *((int8_t*)property.val));
346       break;
347 
348     case BT_PROPERTY_REMOTE_VERSION_INFO:
349       ASSERT_EQ((int)sizeof(bt_remote_version_t), property.len);
350       ASSERT_EQ(kRemoteVersionInfo.version, ((bt_remote_version_t*)property.val)->version);
351       ASSERT_EQ(kRemoteVersionInfo.sub_ver, ((bt_remote_version_t*)property.val)->sub_ver);
352       ASSERT_EQ(
353           kRemoteVersionInfo.manufacturer, ((bt_remote_version_t*)property.val)->manufacturer);
354       break;
355 
356     case BT_PROPERTY_LOCAL_LE_FEATURES:
357       ASSERT_EQ((int)sizeof(bt_local_le_features_t), property.len);
358       ASSERT_EQ(
359           kLocalLeFeatures.version_supported,
360           ((bt_local_le_features_t*)property.val)->version_supported);
361       ASSERT_EQ(
362           kLocalLeFeatures.local_privacy_enabled,
363           ((bt_local_le_features_t*)property.val)->local_privacy_enabled);
364       ASSERT_EQ(
365           kLocalLeFeatures.local_privacy_enabled,
366           ((bt_local_le_features_t*)property.val)->local_privacy_enabled);
367       ASSERT_EQ(
368           kLocalLeFeatures.max_adv_instance,
369           ((bt_local_le_features_t*)property.val)->max_adv_instance);
370       ASSERT_EQ(
371           kLocalLeFeatures.rpa_offload_supported,
372           ((bt_local_le_features_t*)property.val)->rpa_offload_supported);
373       ASSERT_EQ(
374           kLocalLeFeatures.max_irk_list_size,
375           ((bt_local_le_features_t*)property.val)->max_irk_list_size);
376       ASSERT_EQ(
377           kLocalLeFeatures.max_adv_filter_supported,
378           ((bt_local_le_features_t*)property.val)->max_adv_filter_supported);
379       ASSERT_EQ(
380           kLocalLeFeatures.activity_energy_info_supported,
381           ((bt_local_le_features_t*)property.val)->activity_energy_info_supported);
382       ASSERT_EQ(
383           kLocalLeFeatures.scan_result_storage_size,
384           ((bt_local_le_features_t*)property.val)->scan_result_storage_size);
385       ASSERT_EQ(
386           kLocalLeFeatures.total_trackable_advertisers,
387           ((bt_local_le_features_t*)property.val)->total_trackable_advertisers);
388       ASSERT_EQ(
389           kLocalLeFeatures.extended_scan_support,
390           ((bt_local_le_features_t*)property.val)->extended_scan_support);
391       ASSERT_EQ(
392           kLocalLeFeatures.debug_logging_supported,
393           ((bt_local_le_features_t*)property.val)->debug_logging_supported);
394       ASSERT_EQ(
395           kLocalLeFeatures.le_2m_phy_supported,
396           ((bt_local_le_features_t*)property.val)->le_2m_phy_supported);
397       ASSERT_EQ(
398           kLocalLeFeatures.le_coded_phy_supported,
399           ((bt_local_le_features_t*)property.val)->le_coded_phy_supported);
400       ASSERT_EQ(
401           kLocalLeFeatures.le_extended_advertising_supported,
402           ((bt_local_le_features_t*)property.val)->le_extended_advertising_supported);
403       ASSERT_EQ(
404           kLocalLeFeatures.le_periodic_advertising_supported,
405           ((bt_local_le_features_t*)property.val)->le_periodic_advertising_supported);
406       ASSERT_EQ(
407           kLocalLeFeatures.le_maximum_advertising_data_length,
408           ((bt_local_le_features_t*)property.val)->le_maximum_advertising_data_length);
409       ASSERT_EQ(
410           kLocalLeFeatures.dynamic_audio_buffer_supported,
411           ((bt_local_le_features_t*)property.val)->dynamic_audio_buffer_supported);
412       ASSERT_EQ(
413           kLocalLeFeatures.le_periodic_advertising_sync_transfer_sender_supported,
414           ((bt_local_le_features_t*)property.val)
415               ->le_periodic_advertising_sync_transfer_sender_supported);
416       ASSERT_EQ(
417           kLocalLeFeatures.le_connected_isochronous_stream_central_supported,
418           ((bt_local_le_features_t*)property.val)
419               ->le_connected_isochronous_stream_central_supported);
420       ASSERT_EQ(
421           kLocalLeFeatures.le_isochronous_broadcast_supported,
422           ((bt_local_le_features_t*)property.val)->le_isochronous_broadcast_supported);
423       ASSERT_EQ(
424           kLocalLeFeatures.le_periodic_advertising_sync_transfer_recipient_supported,
425           ((bt_local_le_features_t*)property.val)
426               ->le_periodic_advertising_sync_transfer_recipient_supported);
427       ASSERT_EQ(
428           kLocalLeFeatures.adv_filter_extended_features_mask,
429           ((bt_local_le_features_t*)property.val)->adv_filter_extended_features_mask);
430       ASSERT_EQ(
431           kLocalLeFeatures.le_channel_sounding_supported,
432           ((bt_local_le_features_t*)property.val)->le_channel_sounding_supported);
433       break;
434 
435     case BT_PROPERTY_RESERVED_0E:
436     case BT_PROPERTY_RESERVED_0F:
437     case BT_PROPERTY_DYNAMIC_AUDIO_BUFFER:
438       break;
439 
440     case BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER:
441       ASSERT_EQ((int)sizeof(bool), property.len);
442       ASSERT_EQ(kRemoteIsCoordinatedSetMember, *((bool*)property.val));
443       break;
444 
445     case BT_PROPERTY_APPEARANCE:
446       ASSERT_EQ((int)sizeof(uint16_t), property.len);
447       ASSERT_EQ(kAppearance, *((uint16_t*)property.val));
448       break;
449 
450     case BT_PROPERTY_VENDOR_PRODUCT_INFO:
451       ASSERT_EQ((int)sizeof(bt_vendor_product_info_t), property.len);
452       ASSERT_EQ(
453           kVendorProductInfo.vendor_id_src,
454           ((bt_vendor_product_info_t*)property.val)->vendor_id_src);
455       ASSERT_EQ(kVendorProductInfo.vendor_id, ((bt_vendor_product_info_t*)property.val)->vendor_id);
456       ASSERT_EQ(
457           kVendorProductInfo.product_id, ((bt_vendor_product_info_t*)property.val)->product_id);
458       ASSERT_EQ(kVendorProductInfo.version, ((bt_vendor_product_info_t*)property.val)->version);
459       break;
460 
461     case BT_PROPERTY_REMOTE_ASHA_CAPABILITY:
462       ASSERT_EQ((int)sizeof(int16_t), property.len);
463       ASSERT_EQ(kRemoteAshaCapability, *((int16_t*)property.val));
464       break;
465 
466     case BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID:
467       ASSERT_EQ((int)sizeof(uint32_t), property.len);
468       ASSERT_EQ(kRemoteAshaTruncatedHisyncId, *((uint32_t*)property.val));
469       break;
470 
471     case BT_PROPERTY_REMOTE_MODEL_NUM:
472       ASSERT_EQ((int)sizeof(kRemoteModelNum.name), property.len);
473       ASSERT_STREQ((const char*)kRemoteModelNum.name, ((const char*)property.val));
474       break;
475 
476     case BT_PROPERTY_REMOTE_ADDR_TYPE:
477       ASSERT_EQ((int)sizeof(uint8_t), property.len);
478       ASSERT_EQ(kRemoteAddrType, *((uint8_t*)property.val));
479       break;
480 
481     case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP:
482       ASSERT_EQ((int)sizeof(int), property.len);
483       ASSERT_EQ(kRemoteDeviceTimestamp, *((int*)property.val));
484       break;
485 
486     default:
487       FAIL() << "Illegal property type:" << type;
488       break;
489   }
490 }
491 
492 // Fill a property container with all possible property types
fill_properties(std::vector<std::shared_ptr<BtProperty>> & properties)493 void fill_properties(std::vector<std::shared_ptr<BtProperty>>& properties) {
494   fill_property(BT_PROPERTY_BDNAME, properties);
495   fill_property(BT_PROPERTY_BDADDR, properties);
496   fill_property(BT_PROPERTY_UUIDS, properties);
497   fill_property(BT_PROPERTY_CLASS_OF_DEVICE, properties);
498   fill_property(BT_PROPERTY_TYPE_OF_DEVICE, properties);
499   fill_property(BT_PROPERTY_SERVICE_RECORD, properties);
500   fill_property(BT_PROPERTY_ADAPTER_SCAN_MODE, properties);
501   fill_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES, properties);
502   fill_property(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT, properties);
503   fill_property(BT_PROPERTY_REMOTE_FRIENDLY_NAME, properties);
504   fill_property(BT_PROPERTY_REMOTE_RSSI, properties);
505   fill_property(BT_PROPERTY_REMOTE_VERSION_INFO, properties);
506   fill_property(BT_PROPERTY_LOCAL_LE_FEATURES, properties);
507   fill_property(BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER, properties);
508   fill_property(BT_PROPERTY_APPEARANCE, properties);
509   fill_property(BT_PROPERTY_VENDOR_PRODUCT_INFO, properties);
510   fill_property(BT_PROPERTY_REMOTE_ASHA_CAPABILITY, properties);
511   fill_property(BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID, properties);
512   fill_property(BT_PROPERTY_REMOTE_MODEL_NUM, properties);
513   fill_property(BT_PROPERTY_REMOTE_ADDR_TYPE, properties);
514   fill_property(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP, properties);
515   ASSERT_EQ(kNumberTestedProperties, properties.size());
516 }
517 
518 }  // namespace
519    //
520 class BtPropertyTest : public testing::Test {
521  protected:
SetUp()522   void SetUp() override {}
TearDown()523   void TearDown() override {}
524 };
525 
TEST_F(BtPropertyTest,bt_property_text_test)526 TEST_F(BtPropertyTest, bt_property_text_test) {
527   {
528     bt_property_t prop = {
529         .type = BT_PROPERTY_BDNAME,
530         .len = (int)sizeof(kBdName),
531         .val = (void*)kBdName,
532     };
533     ASSERT_STREQ("type:BT_PROPERTY_BDNAME name:kBdName", bt_property_text(prop).c_str());
534   }
535 
536   {
537     bt_property_t prop = {
538         .type = BT_PROPERTY_BDADDR,
539         .len = (int)sizeof(kRawAddress),
540         .val = (void*)&kRawAddress,
541     };
542     ASSERT_STREQ("type:BT_PROPERTY_BDADDR addr:11:22:33:44:55:66", bt_property_text(prop).c_str());
543   }
544 
545   {
546     bt_property_t prop = {
547         .type = BT_PROPERTY_UUIDS,
548         .len = (int)(sizeof(bluetooth::Uuid) * kUuids.size()),
549         .val = (void*)&kUuids[0],
550     };
551     ASSERT_STREQ(
552         "type:BT_PROPERTY_UUIDS uuids:00000001-1001-1000-8000-00805f9b34fb "
553         "00000001-1002-1000-8000-00805f9b34fb 00000001-1003-1000-8000-00805f9b34fb",
554         bt_property_text(prop).c_str());
555   }
556 
557   {
558     bt_property_t prop = {
559         .type = BT_PROPERTY_CLASS_OF_DEVICE,
560         .len = (int)sizeof(kClassOfDevice),
561         .val = (void*)&kClassOfDevice,
562     };
563     ASSERT_STREQ("type:BT_PROPERTY_CLASS_OF_DEVICE cod:0x99663300", bt_property_text(prop).c_str());
564   }
565 
566   {
567     bt_property_t prop = {
568         .type = BT_PROPERTY_TYPE_OF_DEVICE,
569         .len = (int)sizeof(kTypeOfDevice),
570         .val = (void*)&kTypeOfDevice,
571     };
572     ASSERT_STREQ(
573         "type:BT_PROPERTY_TYPE_OF_DEVICE type_of_device:1", bt_property_text(prop).c_str());
574   }
575 
576   {
577     bt_property_t prop = {
578         .type = BT_PROPERTY_SERVICE_RECORD,
579         .len = (int)sizeof(kServiceRecord),
580         .val = (void*)&kServiceRecord,
581     };
582     ASSERT_STREQ(
583         "type:BT_PROPERTY_SERVICE_RECORD uuid:00000001-1001-1000-8000-00805f9b34fb channel:4660 "
584         "name:\"kServiceRecord.name\"",
585         bt_property_text(prop).c_str());
586   }
587 
588   {
589     bt_property_t prop = {
590         .type = BT_PROPERTY_ADAPTER_SCAN_MODE,
591         .len = (int)sizeof(kAdapterScanMode),
592         .val = (void*)&kAdapterScanMode,
593     };
594     ASSERT_STREQ("type:BT_PROPERTY_ADAPTER_SCAN_MODE scan_mode:2", bt_property_text(prop).c_str());
595   }
596 
597   {
598     bt_property_t prop = {
599         .type = BT_PROPERTY_ADAPTER_BONDED_DEVICES,
600         .len = (int)(sizeof(kAdapterBondedDevices)),
601         .val = (void*)kAdapterBondedDevices,
602     };
603     ASSERT_STREQ(
604         "type:BT_PROPERTY_ADAPTER_BONDED_DEVICES addrs:11:22:33:44:55:00 12:22:33:44:55:00 "
605         "13:22:33:44:55:00 14:22:33:44:55:00 15:22:33:44:55:00",
606         bt_property_text(prop).c_str());
607   }
608 
609   {
610     bt_property_t prop = {
611         .type = BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT,
612         .len = (int)sizeof(kAdapterDiscoverableTimeout),
613         .val = (void*)&kAdapterDiscoverableTimeout,
614     };
615     ASSERT_STREQ(
616         "type:BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT discoverable_timeout:1149815808",
617         bt_property_text(prop).c_str());
618   }
619 
620   {
621     bt_bdname_t bd_name;
622     bd_name_from_char_pointer(bd_name.name, (const char*)kRemoteFriendlyName);
623     bt_property_t prop = {
624         .type = BT_PROPERTY_REMOTE_FRIENDLY_NAME,
625         .len = (int)sizeof(bd_name.name),
626         .val = (void*)&bd_name.name,
627     };
628     ASSERT_STREQ(
629         "type:BT_PROPERTY_REMOTE_FRIENDLY_NAME remote_friendly_name:kRemoteFriendlyName",
630         bt_property_text(prop).c_str());
631   }
632 
633   {
634     bt_property_t prop = {
635         .type = BT_PROPERTY_REMOTE_RSSI,
636         .len = (int)sizeof(kRemoteRssi),
637         .val = (void*)&kRemoteRssi,
638     };
639     ASSERT_STREQ("type:BT_PROPERTY_REMOTE_RSSI rssi:16", bt_property_text(prop).c_str());
640   }
641 
642   {
643     bt_property_t prop = {
644         .type = BT_PROPERTY_REMOTE_VERSION_INFO,
645         .len = (int)sizeof(kRemoteVersionInfo),
646         .val = (void*)&kRemoteVersionInfo,
647     };
648     ASSERT_STREQ(
649         "type:BT_PROPERTY_REMOTE_VERSION_INFO version:1 sub:2 mfr:3",
650         bt_property_text(prop).c_str());
651   }
652 
653   {
654     bt_property_t prop = {
655         .type = BT_PROPERTY_LOCAL_LE_FEATURES,
656         .len = (int)sizeof(kLocalLeFeatures),
657         .val = (void*)&kLocalLeFeatures,
658     };
659     ASSERT_STREQ(
660         "type:BT_PROPERTY_LOCAL_LE_FEATURES version_supported:4660 local_privacy_enabled:17 "
661         "max_adv_instance:34 rpa_offload_supported:51 max_irk_list_size:68 "
662         "max_adv_filter_supported:85 activity_energy_info_supported:102 "
663         "scan_result_storage_size:22136 total_trackable_advertisers:39612 extended_scan_support:1 "
664         "debug_logging_supported:1 le_2m_phy_supported:1 le_coded_phy_supported:1 "
665         "le_extended_advertising_supported:1 le_periodic_advertising_supported:1 "
666         "le_maximum_advertising_data_length:4951 dynamic_audio_buffer_supported:574908040 "
667         "le_periodic_advertising_sync_transfer_sender_supported:1 "
668         "le_connected_isochronous_stream_central_supported:1 le_isochronous_broadcast_supported:1 "
669         "le_periodic_advertising_sync_transfer_recipient_supported:1 "
670         "adv_filter_extended_features_mask:13158"
671         "le_channel_sounding_supported:1 ",
672         bt_property_text(prop).c_str());
673   }
674 
675   {
676     bt_property_t prop = {
677         .type = BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER,
678         .len = (int)sizeof(kRemoteIsCoordinatedSetMember),
679         .val = (void*)&kRemoteIsCoordinatedSetMember,
680     };
681     ASSERT_STREQ(
682         "type:BT_PROPERTY_REMOTE_IS_COORDINATED_SET_MEMBER is_coordinated_set_member:true",
683         bt_property_text(prop).c_str());
684   }
685 
686   {
687     bt_property_t prop = {
688         .type = BT_PROPERTY_APPEARANCE,
689         .len = (int)sizeof(kAppearance),
690         .val = (void*)&kAppearance,
691     };
692     ASSERT_STREQ("type:BT_PROPERTY_APPEARANCE appearance:0x44", bt_property_text(prop).c_str());
693   }
694 
695   {
696     bt_property_t prop = {
697         .type = BT_PROPERTY_VENDOR_PRODUCT_INFO,
698         .len = (int)sizeof(kVendorProductInfo),
699         .val = (void*)&kVendorProductInfo,
700     };
701     ASSERT_STREQ(
702         "type:BT_PROPERTY_VENDOR_PRODUCT_INFO vendor_id_src:2 vendor_id:4661 product_id:22137 "
703         "version:39613",
704         bt_property_text(prop).c_str());
705   }
706 
707   {
708     bt_property_t prop = {
709         .type = BT_PROPERTY_REMOTE_ASHA_CAPABILITY,
710         .len = (int)sizeof(kRemoteAshaCapability),
711         .val = (void*)&kRemoteAshaCapability,
712     };
713     ASSERT_STREQ(
714         "type:BT_PROPERTY_REMOTE_ASHA_CAPABILITY remote_asha_capability:137",
715         bt_property_text(prop).c_str());
716   }
717 
718   {
719     bt_property_t prop = {
720         .type = BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID,
721         .len = (int)sizeof(kRemoteAshaTruncatedHisyncId),
722         .val = (void*)&kRemoteAshaTruncatedHisyncId,
723     };
724     ASSERT_STREQ(
725         "type:BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID remote_asha_truncated_hisyncid:574908040",
726         bt_property_text(prop).c_str());
727   }
728 
729   {
730     bt_property_t prop = {
731         .type = BT_PROPERTY_REMOTE_MODEL_NUM,
732         .len = (int)sizeof(kRemoteModelNum.name),
733         .val = (void*)kRemoteModelNum.name,
734     };
735     ASSERT_STREQ(
736         "type:BT_PROPERTY_REMOTE_MODEL_NUM remote_model_num:kRemoteModelNum",
737         bt_property_text(prop).c_str());
738   }
739 
740   {
741     bt_property_t prop = {
742         .type = BT_PROPERTY_REMOTE_ADDR_TYPE,
743         .len = (int)sizeof(kRemoteAddrType),
744         .val = (void*)&kRemoteAddrType,
745     };
746     ASSERT_STREQ(
747         "type:BT_PROPERTY_REMOTE_ADDR_TYPE remote_asha_truncated_hisyncid:0x55",
748         bt_property_text(prop).c_str());
749   }
750 
751   {
752     bt_property_t prop = {
753         .type = BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP,
754         .len = (int)sizeof(kRemoteDeviceTimestamp),
755         .val = (void*)&kRemoteDeviceTimestamp,
756     };
757     ASSERT_STREQ("type:BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP", bt_property_text(prop).c_str());
758   }
759 }
760 
TEST_F(BtPropertyTest,verify_property_sizes)761 TEST_F(BtPropertyTest, verify_property_sizes) {
762   std::vector<std::shared_ptr<BtProperty>> properties;
763   fill_properties(properties);
764 }
765 
TEST_F(BtPropertyTest,fill_and_serialize)766 TEST_F(BtPropertyTest, fill_and_serialize) {
767   std::vector<std::shared_ptr<BtProperty>> properties;
768   fill_properties(properties);
769 
770   BtPropertyLegacy legacy(properties);
771 
772   ASSERT_EQ(kNumberTestedProperties, legacy.NumProperties());
773 }
774 
TEST_F(BtPropertyTest,serialize_and_verify)775 TEST_F(BtPropertyTest, serialize_and_verify) {
776   std::vector<std::shared_ptr<BtProperty>> properties;
777   fill_properties(properties);
778 
779   BtPropertyLegacy legacy(properties);
780 
781   for (const auto p : legacy.Properties()) {
782     verify_property(p.type, p);
783   }
784 }
785 
TEST_F(BtPropertyTest,name_too_long)786 TEST_F(BtPropertyTest, name_too_long) {
787   std::vector<std::shared_ptr<BtProperty>> properties;
788   BD_NAME bd_name;
789   for (size_t i = 0; i < kBdPropNameLength; i++) {
790     bd_name[i] = ((i + 1) % 10) ? 'a' : 'A';
791   }
792 
793   properties.push_back(BdName::Create(bd_name));
794   BtPropertyLegacy legacy(properties);
795   ASSERT_EQ(1U, legacy.NumProperties());
796 
797   bt_property_t bt_properties[1];
798   legacy.Export(bt_properties, 1U);
799 
800   ASSERT_STREQ((const char*)kReallyLongName, (const char*)bt_properties[0].val);
801 }
802 
803 class BtPropertyArrayTest : public testing::Test {
804  protected:
SetUp()805   void SetUp() override {
806     fill_properties(properties);
807   }
TearDown()808   void TearDown() override {}
809   std::vector<std::shared_ptr<BtProperty>> properties;
810   bt_property_t props[kNumberTestedProperties];
811 };
812 
TEST_F(BtPropertyArrayTest,serialize_and_verify)813 TEST_F(BtPropertyArrayTest, serialize_and_verify) {
814   BtPropertyLegacy legacy(properties);
815 
816   for (const auto p : legacy.Properties()) {
817     verify_property(p.type, p);
818   }
819 }
820 
TEST_F(BtPropertyArrayTest,async_data)821 TEST_F(BtPropertyArrayTest, async_data) {
822   auto future = std::async(std::launch::async, []() {
823     std::vector<std::shared_ptr<BtProperty>> properties;
824     fill_properties(properties);
825     return properties;
826   });
827 
828   auto properties = future.get();
829 
830   BtPropertyLegacy legacy(properties);
831 
832   for (const auto p : legacy.Properties()) {
833     verify_property(p.type, p);
834   }
835 }
836 
837 class BtPropertyDynamicArrayTest : public testing::Test {
838  protected:
SetUp()839   void SetUp() override {
840     fill_properties(properties);
841     props = (bt_property_t*)malloc(sizeof(bt_property_t) * properties.size());
842   }
TearDown()843   void TearDown() override {
844     free(props);
845   }
846 
847   bt_property_t* props{nullptr};
848   std::vector<std::shared_ptr<BtProperty>> properties;
849 };
850 
TEST_F(BtPropertyDynamicArrayTest,serialize_and_verify)851 TEST_F(BtPropertyDynamicArrayTest, serialize_and_verify) {
852   BtPropertyLegacy legacy(properties);
853   // Legacy now has complete copy of properties
854   properties.clear();
855 
856   legacy.Export(props, kNumberTestedProperties);
857 
858   bt_property_t* p = props;
859   for (size_t i = 0; i < kNumberTestedProperties; i++) {
860     verify_property(p->type, *p);
861   }
862 }
863 
864 class BtPropertyMultiAllocationTest : public testing::Test {
865  protected:
866   static constexpr size_t kNumProperties = 1;
867   static constexpr size_t kNumThreads = 20;
868 
SetUp()869   void SetUp() override {}
TearDown()870   void TearDown() override {}
871 
872   std::vector<std::future<std::vector<std::shared_ptr<BtProperty>>>> future_vector;
873 
874   bt_property_t bt_properties[kNumThreads][kNumberTestedProperties] = {};
875 
876   std::vector<std::shared_ptr<BtProperty>> properties;
877 };
878 
TEST_F(BtPropertyMultiAllocationTest,async_data_multi)879 TEST_F(BtPropertyMultiAllocationTest, async_data_multi) {
880   for (size_t i = 0; i < kNumThreads; i++) {
881     future_vector.push_back(std::async(std::launch::async, [i]() {
882       std::vector<std::shared_ptr<BtProperty>> properties;
883       properties.emplace_back(RemoteDeviceTimestamp::Create((uint32_t)i));
884       return properties;
885     }));
886   }
887 
888   for (size_t i = 0; i < kNumThreads; i++) {
889     std::vector<std::shared_ptr<BtProperty>> props = future_vector[i].get();
890     BtPropertyLegacy legacy(props);
891     memcpy(
892         bt_properties[i], (const void*)legacy.Ptr(), (size_t)legacy.Len() * sizeof(bt_property_t));
893 
894     ASSERT_EQ(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP, (int)bt_properties[i]->type);
895     ASSERT_EQ((int)sizeof(uint32_t), bt_properties[i]->len);
896     ASSERT_EQ((int)i, *(int*)bt_properties[i]->val);
897   }
898 }
899