| 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 <string> |
| 8 #include <utility> |
| 9 |
| 7 #include "base/macros.h" | 10 #include "base/macros.h" |
| 8 #include "device/bluetooth/bluetooth_uuid.h" | 11 #include "device/bluetooth/bluetooth_uuid.h" |
| 9 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace { | 15 namespace { |
| 13 | 16 |
| 14 constexpr char kUuidStr[] = "12345678-1234-5678-9abc-def123456789"; | 17 constexpr char kUuidStr[] = "12345678-1234-5678-9abc-def123456789"; |
| 15 constexpr uint8_t kUuidArray[] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, | 18 constexpr uint8_t kUuidArray[] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, |
| 16 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, | 19 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, |
| 17 0x23, 0x45, 0x67, 0x89}; | 20 0x23, 0x45, 0x67, 0x89}; |
| 18 constexpr size_t kUuidSize = 16; | 21 constexpr size_t kUuidSize = 16; |
| 19 | 22 |
| 20 constexpr char kUuid16Str[] = "1234"; | 23 constexpr char kUuid16Str[] = "1234"; |
| 21 constexpr uint16_t kUuid16 = 0x1234; | 24 constexpr uint16_t kUuid16 = 0x1234; |
| 22 constexpr uint8_t kServiceData[] = {0x11, 0x22, 0x33, 0x44, 0x55}; | 25 constexpr uint8_t kServiceData[] = {0x11, 0x22, 0x33, 0x44, 0x55}; |
| 23 constexpr uint8_t kManufacturerData[] = {0x00, 0xe0}; | 26 constexpr uint8_t kManufacturerData[] = {0x00, 0xe0}; |
| 24 | 27 |
| 25 template <typename MojoType, typename UserType> | 28 template <typename MojoType, typename UserType> |
| 26 mojo::StructPtr<MojoType> ConvertToMojo(UserType& input) { | 29 mojo::StructPtr<MojoType> ConvertToMojo(UserType& input) { |
| 27 mojo::Array<uint8_t> data = MojoType::Serialize(&input); | 30 std::vector<uint8_t> data = MojoType::Serialize(&input); |
| 28 mojo::StructPtr<MojoType> output; | 31 mojo::StructPtr<MojoType> output; |
| 29 MojoType::Deserialize(std::move(data), &output); | 32 MojoType::Deserialize(std::move(data), &output); |
| 30 return output; | 33 return output; |
| 31 } | 34 } |
| 32 | 35 |
| 33 template <typename MojoType, typename UserType> | 36 template <typename MojoType, typename UserType> |
| 34 bool ConvertFromMojo(mojo::StructPtr<MojoType> input, UserType* output) { | 37 bool ConvertFromMojo(mojo::StructPtr<MojoType> input, UserType* output) { |
| 35 mojo::Array<uint8_t> data = MojoType::Serialize(&input); | 38 std::vector<uint8_t> data = MojoType::Serialize(&input); |
| 36 return MojoType::Deserialize(std::move(data), output); | 39 return MojoType::Deserialize(std::move(data), output); |
| 37 } | 40 } |
| 38 | 41 |
| 39 } // namespace | 42 } // namespace |
| 40 | 43 |
| 41 namespace mojo { | 44 namespace mojo { |
| 42 | 45 |
| 43 TEST(BluetoothStructTraitsTest, SerializeBluetoothUUID) { | 46 TEST(BluetoothStructTraitsTest, SerializeBluetoothUUID) { |
| 44 device::BluetoothUUID uuid_device(kUuidStr); | 47 device::BluetoothUUID uuid_device(kUuidStr); |
| 45 arc::mojom::BluetoothUUIDPtr uuid_mojo = | 48 arc::mojom::BluetoothUUIDPtr uuid_mojo = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 63 | 66 |
| 64 // Size checks are performed in serialization code. UUIDs with a length | 67 // Size checks are performed in serialization code. UUIDs with a length |
| 65 // other than 16 bytes will not make it through the mojo deserialization | 68 // other than 16 bytes will not make it through the mojo deserialization |
| 66 // code since arc::mojom::BluetoothUUID::uuid is defined as | 69 // code since arc::mojom::BluetoothUUID::uuid is defined as |
| 67 // array<uint8, 16>. | 70 // array<uint8, 16>. |
| 68 } | 71 } |
| 69 | 72 |
| 70 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisement) { | 73 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisement) { |
| 71 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = | 74 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = |
| 72 arc::mojom::BluetoothAdvertisement::New(); | 75 arc::mojom::BluetoothAdvertisement::New(); |
| 73 mojo::Array<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; | 76 std::vector<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; |
| 74 | 77 |
| 75 // Create service UUIDs. | 78 // Create service UUIDs. |
| 76 arc::mojom::BluetoothAdvertisingDataPtr data = | 79 arc::mojom::BluetoothAdvertisingDataPtr data = |
| 77 arc::mojom::BluetoothAdvertisingData::New(); | 80 arc::mojom::BluetoothAdvertisingData::New(); |
| 78 std::vector<device::BluetoothUUID> service_uuids; | 81 std::vector<device::BluetoothUUID> service_uuids; |
| 79 service_uuids.push_back((device::BluetoothUUID(kUuidStr))); | 82 service_uuids.push_back((device::BluetoothUUID(kUuidStr))); |
| 80 data->set_service_uuids(service_uuids); | 83 data->set_service_uuids(service_uuids); |
| 81 adv_data.push_back(std::move(data)); | 84 adv_data.push_back(std::move(data)); |
| 82 | 85 |
| 83 // Create service data. | 86 // Create service data. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 EXPECT_EQ(cic & 0xff, kManufacturerData[0]); | 129 EXPECT_EQ(cic & 0xff, kManufacturerData[0]); |
| 127 EXPECT_EQ((cic >> 8) & 0xff, kManufacturerData[1]); | 130 EXPECT_EQ((cic >> 8) & 0xff, kManufacturerData[1]); |
| 128 EXPECT_EQ(converted_manufacturer->begin()->second.size(), | 131 EXPECT_EQ(converted_manufacturer->begin()->second.size(), |
| 129 static_cast<unsigned long>(arraysize(kManufacturerData) - | 132 static_cast<unsigned long>(arraysize(kManufacturerData) - |
| 130 sizeof(uint16_t))); | 133 sizeof(uint16_t))); |
| 131 } | 134 } |
| 132 | 135 |
| 133 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisementFailure) { | 136 TEST(BluetoothStructTraitsTest, DeserializeBluetoothAdvertisementFailure) { |
| 134 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = | 137 arc::mojom::BluetoothAdvertisementPtr advertisement_mojo = |
| 135 arc::mojom::BluetoothAdvertisement::New(); | 138 arc::mojom::BluetoothAdvertisement::New(); |
| 136 mojo::Array<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; | 139 std::vector<arc::mojom::BluetoothAdvertisingDataPtr> adv_data; |
| 137 | 140 |
| 138 // Create empty manufacturer data. Manufacturer data must include the CIC | 141 // Create empty manufacturer data. Manufacturer data must include the CIC |
| 139 // which is 2 bytes long. | 142 // which is 2 bytes long. |
| 140 arc::mojom::BluetoothAdvertisingDataPtr data = | 143 arc::mojom::BluetoothAdvertisingDataPtr data = |
| 141 arc::mojom::BluetoothAdvertisingData::New(); | 144 arc::mojom::BluetoothAdvertisingData::New(); |
| 142 data->set_manufacturer_data((mojo::Array<uint8_t>())); | 145 data->set_manufacturer_data(std::vector<uint8_t>()); |
| 143 adv_data.push_back(std::move(data)); | 146 adv_data.push_back(std::move(data)); |
| 144 | 147 |
| 145 advertisement_mojo->type = | 148 advertisement_mojo->type = |
| 146 arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE; | 149 arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE; |
| 147 advertisement_mojo->data = std::move(adv_data); | 150 advertisement_mojo->data = std::move(adv_data); |
| 148 | 151 |
| 149 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement; | 152 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement; |
| 150 EXPECT_FALSE(ConvertFromMojo(std::move(advertisement_mojo), &advertisement)); | 153 EXPECT_FALSE(ConvertFromMojo(std::move(advertisement_mojo), &advertisement)); |
| 151 } | 154 } |
| 152 | 155 |
| 153 } // namespace mojo | 156 } // namespace mojo |
| OLD | NEW |