| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/arc/bluetooth/bluetooth_struct_traits.h" | 5 #include "components/arc/bluetooth/bluetooth_struct_traits.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | |
| 8 #include "device/bluetooth/bluetooth_uuid.h" | 7 #include "device/bluetooth/bluetooth_uuid.h" |
| 9 #include "mojo/public/cpp/bindings/array.h" | 8 #include "mojo/public/cpp/bindings/array.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 10 |
| 12 namespace { | 11 namespace { |
| 13 | 12 |
| 14 constexpr char kUuidStr[] = "12345678-1234-5678-9abc-def123456789"; | 13 constexpr char kUuidStr[] = "12345678-1234-5678-9abc-def123456789"; |
| 15 constexpr uint8_t kUuidArray[] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, | 14 constexpr uint8_t kUuidArray[] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, |
| 16 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, | 15 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, |
| 17 0x23, 0x45, 0x67, 0x89}; | 16 0x23, 0x45, 0x67, 0x89}; |
| 18 constexpr size_t kUuidSize = 16; | 17 constexpr size_t kUuidSize = 16; |
| 19 | 18 |
| 20 constexpr char kUuid16Str[] = "1234"; | |
| 21 constexpr uint16_t kUuid16 = 0x1234; | |
| 22 constexpr uint8_t kServiceData[] = {0x11, 0x22, 0x33, 0x44, 0x55}; | |
| 23 constexpr uint8_t kManufacturerData[] = {0x00, 0xe0}; | |
| 24 | |
| 25 template <typename MojoType, typename UserType> | 19 template <typename MojoType, typename UserType> |
| 26 mojo::StructPtr<MojoType> ConvertToMojo(UserType& input) { | 20 mojo::StructPtr<MojoType> ConvertToMojo(UserType& input) { |
| 27 mojo::Array<uint8_t> data = MojoType::Serialize(&input); | 21 mojo::Array<uint8_t> data = MojoType::Serialize(&input); |
| 28 mojo::StructPtr<MojoType> output; | 22 mojo::StructPtr<MojoType> output; |
| 29 MojoType::Deserialize(std::move(data), &output); | 23 MojoType::Deserialize(std::move(data), &output); |
| 30 return output; | 24 return output; |
| 31 } | 25 } |
| 32 | 26 |
| 33 template <typename MojoType, typename UserType> | 27 template <typename MojoType, typename UserType> |
| 34 bool ConvertFromMojo(mojo::StructPtr<MojoType> input, UserType* output) { | 28 bool ConvertFromMojo(mojo::StructPtr<MojoType> input, UserType* output) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 48 for (size_t i = 0; i < kUuidSize; i++) { | 42 for (size_t i = 0; i < kUuidSize; i++) { |
| 49 EXPECT_EQ(kUuidArray[i], uuid_mojo->uuid[i]); | 43 EXPECT_EQ(kUuidArray[i], uuid_mojo->uuid[i]); |
| 50 } | 44 } |
| 51 } | 45 } |
| 52 | 46 |
| 53 TEST(BluetoothStructTraitsTest, DeserializeBluetoothUUID) { | 47 TEST(BluetoothStructTraitsTest, DeserializeBluetoothUUID) { |
| 54 arc::mojom::BluetoothUUIDPtr uuid_mojo = arc::mojom::BluetoothUUID::New(); | 48 arc::mojom::BluetoothUUIDPtr uuid_mojo = arc::mojom::BluetoothUUID::New(); |
| 55 for (size_t i = 0; i < kUuidSize; i++) { | 49 for (size_t i = 0; i < kUuidSize; i++) { |
| 56 uuid_mojo->uuid.push_back(kUuidArray[i]); | 50 uuid_mojo->uuid.push_back(kUuidArray[i]); |
| 57 } | 51 } |
| 58 uuid_mojo->uuid = | |
| 59 std::vector<uint8_t>(std::begin(kUuidArray), std::end(kUuidArray)); | |
| 60 device::BluetoothUUID uuid_device; | 52 device::BluetoothUUID uuid_device; |
| 61 EXPECT_TRUE(ConvertFromMojo(std::move(uuid_mojo), &uuid_device)); | 53 EXPECT_TRUE(ConvertFromMojo(std::move(uuid_mojo), &uuid_device)); |
| 62 EXPECT_EQ(std::string(kUuidStr), uuid_device.canonical_value()); | 54 EXPECT_EQ(std::string(kUuidStr), uuid_device.canonical_value()); |
| 63 | 55 |
| 64 // Size checks are performed in serialization code. UUIDs with a length | 56 // Size checks are performed in serialization code. UUIDs with a length |
| 65 // other than 16 bytes will not make it through the mojo deserialization | 57 // other than 16 bytes will not make it through the mojo deserialization |
| 66 // code since arc::mojom::BluetoothUUID::uuid is defined as | 58 // code since arc::mojom::BluetoothUUID::uuid is defined as |
| 67 // array<uint8, 16>. | 59 // array<uint8, 16>. |
| 68 } | 60 } |
| 69 | 61 |
| 70 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisement) { | |
| 71 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = | |
| 72 arc::mojom::BluetoothAdvertisement::New(); | |
| 73 mojo::Array<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; | |
| 74 | |
| 75 // Create service UUIDs. | |
| 76 arc::mojom::BluetoothAdvertisingDataPtr data = | |
| 77 arc::mojom::BluetoothAdvertisingData::New(); | |
| 78 std::vector<device::BluetoothUUID> service_uuids; | |
| 79 service_uuids.push_back((device::BluetoothUUID(kUuidStr))); | |
| 80 data->set_service_uuids(service_uuids); | |
| 81 adv_data.push_back(std::move(data)); | |
| 82 | |
| 83 // Create service data. | |
| 84 data = arc::mojom::BluetoothAdvertisingData::New(); | |
| 85 arc::mojom::BluetoothServiceDataPtr service_data = | |
| 86 arc::mojom::BluetoothServiceData::New(); | |
| 87 service_data->uuid_16bit = kUuid16; | |
| 88 service_data->data = | |
| 89 std::vector<uint8_t>(std::begin(kServiceData), std::end(kServiceData)); | |
| 90 data->set_service_data(std::move(service_data)); | |
| 91 adv_data.push_back(std::move(data)); | |
| 92 | |
| 93 // Create manufacturer data. | |
| 94 data = arc::mojom::BluetoothAdvertisingData::New(); | |
| 95 data->set_manufacturer_data(std::vector<uint8_t>( | |
| 96 std::begin(kManufacturerData), std::end(kManufacturerData))); | |
| 97 adv_data.push_back(std::move(data)); | |
| 98 | |
| 99 advertisement_mojo->type = | |
| 100 arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE; | |
| 101 advertisement_mojo->data = std::move(adv_data); | |
| 102 | |
| 103 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement; | |
| 104 EXPECT_TRUE(ConvertFromMojo(std::move(advertisement_mojo), &advertisement)); | |
| 105 | |
| 106 EXPECT_EQ(advertisement->type(), | |
| 107 device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL); | |
| 108 | |
| 109 std::unique_ptr<device::BluetoothAdvertisement::UUIDList> converted_uuids = | |
| 110 advertisement->service_uuids(); | |
| 111 EXPECT_EQ(converted_uuids->size(), 1U); | |
| 112 EXPECT_EQ(*converted_uuids->begin(), kUuidStr); | |
| 113 | |
| 114 std::unique_ptr<device::BluetoothAdvertisement::ServiceData> | |
| 115 converted_service = advertisement->service_data(); | |
| 116 EXPECT_EQ(converted_service->size(), 1U); | |
| 117 EXPECT_EQ(converted_service->begin()->first, kUuid16Str); | |
| 118 for (size_t i = 0; i < arraysize(kServiceData); i++) { | |
| 119 EXPECT_EQ(kServiceData[i], converted_service->begin()->second[i]); | |
| 120 } | |
| 121 | |
| 122 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | |
| 123 converted_manufacturer = advertisement->manufacturer_data(); | |
| 124 EXPECT_EQ(converted_manufacturer->size(), 1U); | |
| 125 uint16_t cic = converted_manufacturer->begin()->first; | |
| 126 EXPECT_EQ(cic & 0xff, kManufacturerData[0]); | |
| 127 EXPECT_EQ((cic >> 8) & 0xff, kManufacturerData[1]); | |
| 128 EXPECT_EQ(converted_manufacturer->begin()->second.size(), | |
| 129 static_cast<unsigned long>(arraysize(kManufacturerData) - | |
| 130 sizeof(uint16_t))); | |
| 131 } | |
| 132 | |
| 133 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisementFailure) { | |
| 134 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = | |
| 135 arc::mojom::BluetoothAdvertisement::New(); | |
| 136 mojo::Array<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; | |
| 137 | |
| 138 // Create empty manufacturer data. Manufacturer data must include the CIC | |
| 139 // which is 2 bytes long. | |
| 140 arc::mojom::BluetoothAdvertisingDataPtr data = | |
| 141 arc::mojom::BluetoothAdvertisingData::New(); | |
| 142 data->set_manufacturer_data((mojo::Array<uint8_t>())); | |
| 143 adv_data.push_back(std::move(data)); | |
| 144 | |
| 145 advertisement_mojo->type = | |
| 146 arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE; | |
| 147 advertisement_mojo->data = std::move(adv_data); | |
| 148 | |
| 149 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement; | |
| 150 EXPECT_FALSE(ConvertFromMojo(std::move(advertisement_mojo), &advertisement)); | |
| 151 } | |
| 152 | |
| 153 } // namespace mojo | 62 } // namespace mojo |
| OLD | NEW |