1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
ash / components / arc / bluetooth / bluetooth_type_converters_unittest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "ash/components/arc/bluetooth/bluetooth_type_converters.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include "base/values.h"
#include "device/bluetooth/bluetooth_gatt_service.h"
#include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kAddressStr[] = "1A:2B:3C:4D:5E:6F";
constexpr char kInvalidAddressStr[] = "00:00:00:00:00:00";
constexpr uint8_t kAddressArray[] = {0x1a, 0x2b, 0x3c, 0x4d, 0x5e, 0x6f};
constexpr size_t kAddressSize = 6;
constexpr uint8_t kFillerByte = 0x79;
arc::mojom::BluetoothSdpAttributePtr CreateDeepMojoSequenceAttribute(
size_t depth) {
auto value = arc::mojom::BluetoothSdpAttribute::New();
if (depth > 0u) {
value->type = bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE;
value->value = base::Value();
value->sequence.push_back(CreateDeepMojoSequenceAttribute(depth - 1));
value->type_size = static_cast<uint32_t>(value->sequence.size());
} else {
uint16_t data = 3;
value->type = bluez::BluetoothServiceAttributeValueBlueZ::UINT;
value->type_size = static_cast<uint32_t>(sizeof(data));
value->value = base::Value(data);
}
return value;
}
size_t GetDepthOfMojoAttribute(
const arc::mojom::BluetoothSdpAttributePtr& attribute) {
size_t depth = 1;
if (attribute->type == bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE) {
for (const auto& value : attribute->sequence)
depth = std::max(depth, GetDepthOfMojoAttribute(value) + 1);
}
return depth;
}
bluez::BluetoothServiceAttributeValueBlueZ CreateDeepBlueZSequenceAttribute(
size_t depth) {
if (depth > 0u) {
auto sequence = std::make_unique<
bluez::BluetoothServiceAttributeValueBlueZ::Sequence>();
sequence->push_back(CreateDeepBlueZSequenceAttribute(depth - 1));
return bluez::BluetoothServiceAttributeValueBlueZ(std::move(sequence));
} else {
return bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::UINT, sizeof(uint16_t),
base::Value(3));
}
}
size_t GetDepthOfBlueZAttribute(
const bluez::BluetoothServiceAttributeValueBlueZ& attribute) {
size_t depth = 1;
if (attribute.type() ==
bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE) {
for (const auto& value : attribute.sequence())
depth = std::max(depth, GetDepthOfBlueZAttribute(value) + 1);
}
return depth;
}
} // namespace
namespace mojo {
TEST(BluetoothTypeConverterTest, ConvertMojoBluetoothAddressFromString) {
arc::mojom::BluetoothAddressPtr address_mojo =
arc::mojom::BluetoothAddress::From(std::string(kAddressStr));
EXPECT_EQ(kAddressSize, address_mojo->address.size());
for (size_t i = 0; i < kAddressSize; i++)
EXPECT_EQ(kAddressArray[i], address_mojo->address[i]);
}
TEST(BluetoothTypeConverterTest, ConvertMojoBluetoothAddressToString) {
arc::mojom::BluetoothAddressPtr address_mojo =
arc::mojom::BluetoothAddress::New();
// Test address is shorter than expected (invalid address).
for (size_t i = 0; i < kAddressSize - 1; i++)
address_mojo->address.push_back(kAddressArray[i]);
EXPECT_EQ(kInvalidAddressStr, address_mojo->To<std::string>());
// Test success case.
address_mojo->address.push_back(kAddressArray[kAddressSize - 1]);
EXPECT_EQ(kAddressStr, address_mojo->To<std::string>());
// Test address is longer than expected (invalid address).
address_mojo->address.push_back(kFillerByte);
EXPECT_EQ(kInvalidAddressStr, address_mojo->To<std::string>());
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_NullType) {
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE;
mojo->type_size = 0;
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE,
blue_z.type());
EXPECT_EQ(0u, blue_z.size());
EXPECT_EQ(base::Value::Type::NONE, blue_z.value().type());
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_BoolType) {
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::BOOL;
mojo->type_size = static_cast<uint32_t>(sizeof(bool));
mojo->value = base::Value(true);
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::BOOL, blue_z.type());
EXPECT_EQ(sizeof(bool), blue_z.size());
ASSERT_EQ(base::Value::Type::BOOLEAN, blue_z.value().type());
EXPECT_TRUE(blue_z.value().GetBool());
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_UintType) {
constexpr uint16_t kValue = 10;
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::UINT;
mojo->type_size = static_cast<uint32_t>(sizeof(kValue));
mojo->value = base::Value(static_cast<int>(kValue));
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UINT, blue_z.type());
EXPECT_EQ(sizeof(kValue), blue_z.size());
ASSERT_EQ(base::Value::Type::INTEGER, blue_z.value().type());
EXPECT_EQ(kValue, static_cast<uint16_t>(blue_z.value().GetInt()));
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_IntType) {
constexpr int16_t kValue = 20;
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::INT;
mojo->type_size = static_cast<uint32_t>(sizeof(kValue));
mojo->value = base::Value(static_cast<int>(kValue));
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::INT, blue_z.type());
EXPECT_EQ(sizeof(kValue), blue_z.size());
ASSERT_EQ(base::Value::Type::INTEGER, blue_z.value().type());
EXPECT_EQ(kValue, static_cast<int16_t>(blue_z.value().GetInt()));
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_UuidType) {
// Construct Mojo attribute with TYPE_UUID.
constexpr char kValue[] = "00000000-0000-1000-8000-00805f9b34fb";
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::UUID;
// UUIDs are all stored in string form, but it can be converted to one of
// UUID16, UUID32 and UUID128.
mojo->type_size = static_cast<uint32_t>(sizeof(uint16_t));
mojo->value = base::Value(kValue);
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UUID, blue_z.type());
EXPECT_EQ(sizeof(uint16_t), blue_z.size());
ASSERT_EQ(base::Value::Type::STRING, blue_z.value().type());
EXPECT_EQ(kValue, blue_z.value().GetString());
}
TEST(BluetoothTypeConverterTest,
ConvertMojoValueAttributeToBlueZAttribute_StringType) {
// Construct Mojo attribute with TYPE_STRING. TYPE_URL is the same case as
// TYPE_STRING.
constexpr char kValue[] = "Some SDP service";
constexpr size_t kValueSize = sizeof(kValue) - 1; // Subtract '\0' size.
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::STRING;
// Subtract '\0'-terminate size.
mojo->type_size = static_cast<uint32_t>(kValueSize);
mojo->value = base::Value(kValue);
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::STRING, blue_z.type());
EXPECT_EQ(kValueSize, blue_z.size());
ASSERT_EQ(base::Value::Type::STRING, blue_z.value().type());
EXPECT_EQ(kValue, blue_z.value().GetString());
}
TEST(BluetoothTypeConverterTest, ConvertMojoSequenceAttributeToBlueZAttribute) {
constexpr char kL2capUuid[] = "00000100-0000-1000-8000-00805f9b34fb";
constexpr uint16_t kL2capChannel = 3;
// Create a sequence with the above two values.
auto sequence_mojo = arc::mojom::BluetoothSdpAttribute::New();
sequence_mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE;
{
// Create an UUID value.
auto value_uuid = arc::mojom::BluetoothSdpAttribute::New();
value_uuid->type = bluez::BluetoothServiceAttributeValueBlueZ::UUID;
value_uuid->type_size = static_cast<uint32_t>(sizeof(uint16_t));
value_uuid->value = base::Value(kL2capUuid);
sequence_mojo->sequence.push_back(std::move(value_uuid));
}
{
// Create an UINT value.
auto value_channel = arc::mojom::BluetoothSdpAttribute::New();
value_channel->type = bluez::BluetoothServiceAttributeValueBlueZ::UINT;
value_channel->type_size = static_cast<uint32_t>(sizeof(kL2capChannel));
value_channel->value = base::Value(static_cast<int>(kL2capChannel));
sequence_mojo->sequence.push_back(std::move(value_channel));
}
sequence_mojo->type_size = sequence_mojo->sequence.size();
sequence_mojo->value = std::nullopt;
auto sequence_blue_z =
sequence_mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE,
sequence_blue_z.type());
EXPECT_EQ(sequence_mojo->sequence.size(), sequence_blue_z.sequence().size());
const auto& sequence = sequence_blue_z.sequence();
ASSERT_EQ(2u, sequence.size());
{
const auto& blue_z = sequence[0];
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UUID, blue_z.type());
EXPECT_EQ(sizeof(uint16_t), blue_z.size());
ASSERT_EQ(base::Value::Type::STRING, blue_z.value().type());
EXPECT_EQ(kL2capUuid, blue_z.value().GetString());
}
{
const auto& blue_z = sequence[1];
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UINT, blue_z.type());
EXPECT_EQ(sizeof(kL2capChannel), blue_z.size());
ASSERT_EQ(base::Value::Type::INTEGER, blue_z.value().type());
EXPECT_EQ(kL2capChannel, static_cast<uint16_t>(blue_z.value().GetInt()));
}
}
TEST(BluetoothTypeConverterTest,
ConvertInvalidMojoValueAttributeToBlueZAttribute) {
// Create a Mojo attribute without value defined.
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::UINT;
mojo->type_size = static_cast<uint32_t>(sizeof(uint32_t));
mojo->value = std::nullopt;
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE,
blue_z.type());
EXPECT_EQ(0u, blue_z.size());
EXPECT_EQ(base::Value::Type::NONE, blue_z.value().type());
}
TEST(BluetoothTypeConverterTest,
ConvertInvalidMojoSequenceAttributeToBlueZAttribute_NoData) {
// Create a Mojo attribute with an empty sequence.
auto mojo = arc::mojom::BluetoothSdpAttribute::New();
mojo->type = bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE;
mojo->type_size = 0;
mojo->value = std::nullopt;
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE,
blue_z.type());
EXPECT_EQ(0u, blue_z.size());
EXPECT_EQ(base::Value::Type::NONE, blue_z.value().type());
}
TEST(BluetoothTypeConverterTest,
ConvertInvalidMojoSequenceAttributeToBlueZAttribute_TooDeep) {
// Create a Mojo attribute with the depth = arc::kBluetoothSDPMaxDepth + 3.
auto mojo = CreateDeepMojoSequenceAttribute(arc::kBluetoothSDPMaxDepth + 3);
auto blue_z = mojo.To<bluez::BluetoothServiceAttributeValueBlueZ>();
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE,
blue_z.type());
EXPECT_EQ(1u, blue_z.size());
EXPECT_EQ(arc::kBluetoothSDPMaxDepth, GetDepthOfBlueZAttribute(blue_z));
}
TEST(BluetoothTypeConverterTest,
ConvertBlueZValueAttributeToMojoAttribute_NullType) {
// Check NULL type.
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ();
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::NULLTYPE, mojo->type);
EXPECT_EQ(0u, mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
EXPECT_TRUE(mojo->value->is_none());
}
TEST(BluetoothTypeConverterTest,
ConvertBlueZValueAttributeToMojoAttribute_UintType) {
// Check integer types (INT, UINT).
constexpr uint16_t kValue = 10;
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::UINT, sizeof(kValue),
base::Value(static_cast<int>(kValue)));
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UINT, mojo->type);
EXPECT_EQ(sizeof(kValue), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_int());
EXPECT_EQ(kValue, static_cast<uint16_t>(mojo->value->GetInt()));
}
TEST(BluetoothTypeConverterTest,
ConvertBlueZValueAttributeToMojoAttribute_BoolType) {
// Check bool type.
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::BOOL, sizeof(bool),
base::Value(false));
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::BOOL, mojo->type);
EXPECT_EQ(static_cast<uint32_t>(sizeof(bool)), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_bool());
EXPECT_FALSE(mojo->value->GetBool());
}
TEST(BluetoothTypeConverterTest,
ConvertBlueZValueAttributeToMojoAttribute_UuidType) {
// Check UUID type.
constexpr char kValue[] = "00000100-0000-1000-8000-00805f9b34fb";
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::UUID, sizeof(uint16_t),
base::Value(kValue));
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UUID, mojo->type);
EXPECT_EQ(static_cast<uint32_t>(sizeof(uint16_t)), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_string());
EXPECT_EQ(kValue, mojo->value->GetString());
}
TEST(BluetoothTypeConverterTest,
ConvertBlueZValueAttributeToMojoAttribute_StringType) {
// Check string types (STRING, URL).
constexpr char kValue[] = "Some Service Name";
constexpr size_t kValueSize = sizeof(kValue) - 1; // Subtract '\0' size.
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::STRING, kValueSize,
base::Value(kValue));
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::STRING, mojo->type);
EXPECT_EQ(static_cast<uint32_t>(kValueSize), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_string());
EXPECT_EQ(kValue, mojo->value->GetString());
}
TEST(BluetoothTypeConverterTest, ConvertBlueZSequenceAttributeToMojoAttribute) {
constexpr char kL2capUuid[] = "00000100-0000-1000-8000-00805f9b34fb";
constexpr uint16_t kL2capChannel = 3;
auto sequence =
std::make_unique<bluez::BluetoothServiceAttributeValueBlueZ::Sequence>();
sequence->push_back(bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::UUID, sizeof(uint16_t),
base::Value(kL2capUuid)));
sequence->push_back(bluez::BluetoothServiceAttributeValueBlueZ(
bluez::BluetoothServiceAttributeValueBlueZ::UINT, sizeof(uint16_t),
base::Value(kL2capChannel)));
auto blue_z = bluez::BluetoothServiceAttributeValueBlueZ(std::move(sequence));
ASSERT_EQ(2u, blue_z.sequence().size());
auto sequence_mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE,
sequence_mojo->type);
EXPECT_EQ(static_cast<uint32_t>(blue_z.size()), sequence_mojo->type_size);
EXPECT_EQ(sequence_mojo->type_size, sequence_mojo->sequence.size());
{
const auto& mojo = sequence_mojo->sequence[0];
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UUID, mojo->type);
EXPECT_EQ(static_cast<uint32_t>(sizeof(uint16_t)), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_string());
EXPECT_EQ(kL2capUuid, mojo->value->GetString());
}
{
const auto& mojo = sequence_mojo->sequence[1];
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::UINT, mojo->type);
EXPECT_EQ(static_cast<uint32_t>(sizeof(uint16_t)), mojo->type_size);
ASSERT_TRUE(mojo->value.has_value());
ASSERT_TRUE(mojo->value->is_int());
EXPECT_EQ(kL2capChannel, static_cast<uint16_t>(mojo->value->GetInt()));
}
}
TEST(BluetoothTypeConverterTest,
ConvertInvalidBlueZSequenceAttributeToBlueZAttribute_TooDeep) {
bluez::BluetoothServiceAttributeValueBlueZ blue_z =
CreateDeepBlueZSequenceAttribute(arc::kBluetoothSDPMaxDepth + 3);
auto mojo = ConvertTo<arc::mojom::BluetoothSdpAttributePtr>(blue_z);
EXPECT_EQ(bluez::BluetoothServiceAttributeValueBlueZ::SEQUENCE, mojo->type);
EXPECT_EQ(1u, mojo->type_size);
EXPECT_EQ(arc::kBluetoothSDPMaxDepth, GetDepthOfMojoAttribute(mojo));
}
} // namespace mojo