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