| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 template <typename U, typename T> | 51 template <typename U, typename T> |
| 52 U SerializeAndDeserialize(T input) { | 52 U SerializeAndDeserialize(T input) { |
| 53 using InputDataType = typename std::remove_pointer<decltype( | 53 using InputDataType = typename std::remove_pointer<decltype( |
| 54 std::declval<T>().get())>::type::Data_*; | 54 std::declval<T>().get())>::type::Data_*; |
| 55 using OutputDataType = typename std::remove_pointer<decltype( | 55 using OutputDataType = typename std::remove_pointer<decltype( |
| 56 std::declval<U>().get())>::type::Data_*; | 56 std::declval<U>().get())>::type::Data_*; |
| 57 | 57 |
| 58 mojo::internal::SerializationContext context; | 58 mojo::internal::SerializationContext context; |
| 59 size_t size = GetSerializedSize_(input, &context); | 59 size_t size = mojo::internal::PrepareToSerialize<T>(input, &context); |
| 60 mojo::internal::FixedBufferForTesting buf(size + 32); | 60 mojo::internal::FixedBufferForTesting buf(size + 32); |
| 61 InputDataType data; | 61 InputDataType data; |
| 62 Serialize_(std::move(input), &buf, &data, &context); | 62 mojo::internal::Serialize<T>(input, &buf, &data, &context); |
| 63 | 63 |
| 64 data->EncodePointers(); | 64 data->EncodePointers(); |
| 65 | 65 |
| 66 // Set the subsequent area to a special value, so that we can find out if we | 66 // Set the subsequent area to a special value, so that we can find out if we |
| 67 // mistakenly access the area. | 67 // mistakenly access the area. |
| 68 void* subsequent_area = buf.Allocate(32); | 68 void* subsequent_area = buf.Allocate(32); |
| 69 memset(subsequent_area, 0xAA, 32); | 69 memset(subsequent_area, 0xAA, 32); |
| 70 | 70 |
| 71 OutputDataType output_data = reinterpret_cast<OutputDataType>(data); | 71 OutputDataType output_data = reinterpret_cast<OutputDataType>(data); |
| 72 output_data->DecodePointers(); | 72 output_data->DecodePointers(); |
| 73 | 73 |
| 74 U output; | 74 U output; |
| 75 Deserialize_(output_data, &output, &context); | 75 mojo::internal::Deserialize<U>(output_data, &output, &context); |
| 76 return std::move(output); | 76 return std::move(output); |
| 77 } | 77 } |
| 78 | 78 |
| 79 using StructTest = testing::Test; | 79 using StructTest = testing::Test; |
| 80 | 80 |
| 81 } // namespace | 81 } // namespace |
| 82 | 82 |
| 83 TEST_F(StructTest, Rect) { | 83 TEST_F(StructTest, Rect) { |
| 84 RectPtr rect; | 84 RectPtr rect; |
| 85 EXPECT_TRUE(rect.is_null()); | 85 EXPECT_TRUE(rect.is_null()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // NoDefaultFieldValues contains handles, so Clone() is not available, but | 129 // NoDefaultFieldValues contains handles, so Clone() is not available, but |
| 130 // NoDefaultFieldValuesPtr should still compile. | 130 // NoDefaultFieldValuesPtr should still compile. |
| 131 NoDefaultFieldValuesPtr no_default_field_values(NoDefaultFieldValues::New()); | 131 NoDefaultFieldValuesPtr no_default_field_values(NoDefaultFieldValues::New()); |
| 132 EXPECT_FALSE(no_default_field_values->f13.is_valid()); | 132 EXPECT_FALSE(no_default_field_values->f13.is_valid()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Serialization test of a struct with no pointer or handle members. | 135 // Serialization test of a struct with no pointer or handle members. |
| 136 TEST_F(StructTest, Serialization_Basic) { | 136 TEST_F(StructTest, Serialization_Basic) { |
| 137 RectPtr rect(MakeRect()); | 137 RectPtr rect(MakeRect()); |
| 138 | 138 |
| 139 size_t size = GetSerializedSize_(rect, nullptr); | 139 size_t size = mojo::internal::PrepareToSerialize<RectPtr>(rect, nullptr); |
| 140 EXPECT_EQ(8U + 16U, size); | 140 EXPECT_EQ(8U + 16U, size); |
| 141 | 141 |
| 142 mojo::internal::FixedBufferForTesting buf(size); | 142 mojo::internal::FixedBufferForTesting buf(size); |
| 143 internal::Rect_Data* data; | 143 internal::Rect_Data* data; |
| 144 Serialize_(std::move(rect), &buf, &data, nullptr); | 144 mojo::internal::Serialize<RectPtr>(rect, &buf, &data, nullptr); |
| 145 | 145 |
| 146 RectPtr rect2; | 146 RectPtr rect2; |
| 147 Deserialize_(data, &rect2, nullptr); | 147 mojo::internal::Deserialize<RectPtr>(data, &rect2, nullptr); |
| 148 | 148 |
| 149 CheckRect(*rect2); | 149 CheckRect(*rect2); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Construction of a struct with struct pointers from null. | 152 // Construction of a struct with struct pointers from null. |
| 153 TEST_F(StructTest, Construction_StructPointers) { | 153 TEST_F(StructTest, Construction_StructPointers) { |
| 154 RectPairPtr pair; | 154 RectPairPtr pair; |
| 155 EXPECT_TRUE(pair.is_null()); | 155 EXPECT_TRUE(pair.is_null()); |
| 156 | 156 |
| 157 pair = RectPair::New(); | 157 pair = RectPair::New(); |
| 158 EXPECT_FALSE(pair.is_null()); | 158 EXPECT_FALSE(pair.is_null()); |
| 159 EXPECT_TRUE(pair->first.is_null()); | 159 EXPECT_TRUE(pair->first.is_null()); |
| 160 EXPECT_TRUE(pair->first.is_null()); | 160 EXPECT_TRUE(pair->first.is_null()); |
| 161 | 161 |
| 162 pair = nullptr; | 162 pair = nullptr; |
| 163 EXPECT_TRUE(pair.is_null()); | 163 EXPECT_TRUE(pair.is_null()); |
| 164 } | 164 } |
| 165 | 165 |
| 166 // Serialization test of a struct with struct pointers. | 166 // Serialization test of a struct with struct pointers. |
| 167 TEST_F(StructTest, Serialization_StructPointers) { | 167 TEST_F(StructTest, Serialization_StructPointers) { |
| 168 RectPairPtr pair(RectPair::New()); | 168 RectPairPtr pair(RectPair::New()); |
| 169 pair->first = MakeRect(); | 169 pair->first = MakeRect(); |
| 170 pair->second = MakeRect(); | 170 pair->second = MakeRect(); |
| 171 | 171 |
| 172 size_t size = GetSerializedSize_(pair, nullptr); | 172 size_t size = mojo::internal::PrepareToSerialize<RectPairPtr>(pair, nullptr); |
| 173 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), size); | 173 EXPECT_EQ(8U + 16U + 2 * (8U + 16U), size); |
| 174 | 174 |
| 175 mojo::internal::FixedBufferForTesting buf(size); | 175 mojo::internal::FixedBufferForTesting buf(size); |
| 176 internal::RectPair_Data* data; | 176 internal::RectPair_Data* data; |
| 177 Serialize_(std::move(pair), &buf, &data, nullptr); | 177 mojo::internal::Serialize<RectPairPtr>(pair, &buf, &data, nullptr); |
| 178 | 178 |
| 179 RectPairPtr pair2; | 179 RectPairPtr pair2; |
| 180 Deserialize_(data, &pair2, nullptr); | 180 mojo::internal::Deserialize<RectPairPtr>(data, &pair2, nullptr); |
| 181 | 181 |
| 182 CheckRect(*pair2->first); | 182 CheckRect(*pair2->first); |
| 183 CheckRect(*pair2->second); | 183 CheckRect(*pair2->second); |
| 184 } | 184 } |
| 185 | 185 |
| 186 // Serialization test of a struct with an array member. | 186 // Serialization test of a struct with an array member. |
| 187 TEST_F(StructTest, Serialization_ArrayPointers) { | 187 TEST_F(StructTest, Serialization_ArrayPointers) { |
| 188 NamedRegionPtr region(NamedRegion::New()); | 188 NamedRegionPtr region(NamedRegion::New()); |
| 189 region->name = "region"; | 189 region->name = "region"; |
| 190 region->rects = Array<RectPtr>::New(4); | 190 region->rects = Array<RectPtr>::New(4); |
| 191 for (size_t i = 0; i < region->rects.size(); ++i) | 191 for (size_t i = 0; i < region->rects.size(); ++i) |
| 192 region->rects[i] = MakeRect(static_cast<int32_t>(i) + 1); | 192 region->rects[i] = MakeRect(static_cast<int32_t>(i) + 1); |
| 193 | 193 |
| 194 size_t size = GetSerializedSize_(region, nullptr); | 194 size_t size = |
| 195 mojo::internal::PrepareToSerialize<NamedRegionPtr>(region, nullptr); |
| 195 EXPECT_EQ(8U + // header | 196 EXPECT_EQ(8U + // header |
| 196 8U + // name pointer | 197 8U + // name pointer |
| 197 8U + // rects pointer | 198 8U + // rects pointer |
| 198 8U + // name header | 199 8U + // name header |
| 199 8U + // name payload (rounded up) | 200 8U + // name payload (rounded up) |
| 200 8U + // rects header | 201 8U + // rects header |
| 201 4 * 8U + // rects payload (four pointers) | 202 4 * 8U + // rects payload (four pointers) |
| 202 4 * (8U + // rect header | 203 4 * (8U + // rect header |
| 203 16U), // rect payload (four ints) | 204 16U), // rect payload (four ints) |
| 204 size); | 205 size); |
| 205 | 206 |
| 206 mojo::internal::FixedBufferForTesting buf(size); | 207 mojo::internal::FixedBufferForTesting buf(size); |
| 207 internal::NamedRegion_Data* data; | 208 internal::NamedRegion_Data* data; |
| 208 Serialize_(std::move(region), &buf, &data, nullptr); | 209 mojo::internal::Serialize<NamedRegionPtr>(region, &buf, &data, nullptr); |
| 209 | 210 |
| 210 NamedRegionPtr region2; | 211 NamedRegionPtr region2; |
| 211 Deserialize_(data, ®ion2, nullptr); | 212 mojo::internal::Deserialize<NamedRegionPtr>(data, ®ion2, nullptr); |
| 212 | 213 |
| 213 EXPECT_EQ(String("region"), region2->name); | 214 EXPECT_EQ(String("region"), region2->name); |
| 214 | 215 |
| 215 EXPECT_EQ(4U, region2->rects.size()); | 216 EXPECT_EQ(4U, region2->rects.size()); |
| 216 for (size_t i = 0; i < region2->rects.size(); ++i) | 217 for (size_t i = 0; i < region2->rects.size(); ++i) |
| 217 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1); | 218 CheckRect(*region2->rects[i], static_cast<int32_t>(i) + 1); |
| 218 } | 219 } |
| 219 | 220 |
| 220 // Serialization test of a struct with null array pointers. | 221 // Serialization test of a struct with null array pointers. |
| 221 TEST_F(StructTest, Serialization_NullArrayPointers) { | 222 TEST_F(StructTest, Serialization_NullArrayPointers) { |
| 222 NamedRegionPtr region(NamedRegion::New()); | 223 NamedRegionPtr region(NamedRegion::New()); |
| 223 EXPECT_TRUE(region->name.is_null()); | 224 EXPECT_TRUE(region->name.is_null()); |
| 224 EXPECT_TRUE(region->rects.is_null()); | 225 EXPECT_TRUE(region->rects.is_null()); |
| 225 | 226 |
| 226 size_t size = GetSerializedSize_(region, nullptr); | 227 size_t size = |
| 228 mojo::internal::PrepareToSerialize<NamedRegionPtr>(region, nullptr); |
| 227 EXPECT_EQ(8U + // header | 229 EXPECT_EQ(8U + // header |
| 228 8U + // name pointer | 230 8U + // name pointer |
| 229 8U, // rects pointer | 231 8U, // rects pointer |
| 230 size); | 232 size); |
| 231 | 233 |
| 232 mojo::internal::FixedBufferForTesting buf(size); | 234 mojo::internal::FixedBufferForTesting buf(size); |
| 233 internal::NamedRegion_Data* data; | 235 internal::NamedRegion_Data* data; |
| 234 Serialize_(std::move(region), &buf, &data, nullptr); | 236 mojo::internal::Serialize<NamedRegionPtr>(region, &buf, &data, nullptr); |
| 235 | 237 |
| 236 NamedRegionPtr region2; | 238 NamedRegionPtr region2; |
| 237 Deserialize_(data, ®ion2, nullptr); | 239 mojo::internal::Deserialize<NamedRegionPtr>(data, ®ion2, nullptr); |
| 238 | 240 |
| 239 EXPECT_TRUE(region2->name.is_null()); | 241 EXPECT_TRUE(region2->name.is_null()); |
| 240 EXPECT_TRUE(region2->rects.is_null()); | 242 EXPECT_TRUE(region2->rects.is_null()); |
| 241 } | 243 } |
| 242 | 244 |
| 243 // Tests deserializing structs as a newer version. | 245 // Tests deserializing structs as a newer version. |
| 244 TEST_F(StructTest, Versioning_OldToNew) { | 246 TEST_F(StructTest, Versioning_OldToNew) { |
| 245 { | 247 { |
| 246 MultiVersionStructV0Ptr input(MultiVersionStructV0::New()); | 248 MultiVersionStructV0Ptr input(MultiVersionStructV0::New()); |
| 247 input->f_int32 = 123; | 249 input->f_int32 = 123; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 EXPECT_TRUE(output->Equals(*expected_output)); | 418 EXPECT_TRUE(output->Equals(*expected_output)); |
| 417 } | 419 } |
| 418 } | 420 } |
| 419 | 421 |
| 420 // Serialization test for native struct. | 422 // Serialization test for native struct. |
| 421 TEST_F(StructTest, Serialization_NativeStruct) { | 423 TEST_F(StructTest, Serialization_NativeStruct) { |
| 422 using Data = mojo::internal::NativeStruct_Data; | 424 using Data = mojo::internal::NativeStruct_Data; |
| 423 { | 425 { |
| 424 // Serialization of a null native struct. | 426 // Serialization of a null native struct. |
| 425 NativeStructPtr native; | 427 NativeStructPtr native; |
| 426 size_t size = GetSerializedSize_(native, nullptr); | 428 size_t size = |
| 429 mojo::internal::PrepareToSerialize<NativeStructPtr>(native, nullptr); |
| 427 EXPECT_EQ(0u, size); | 430 EXPECT_EQ(0u, size); |
| 428 mojo::internal::FixedBufferForTesting buf(size); | 431 mojo::internal::FixedBufferForTesting buf(size); |
| 429 | 432 |
| 430 Data* data = nullptr; | 433 Data* data = nullptr; |
| 431 Serialize_(std::move(native), &buf, &data, nullptr); | 434 mojo::internal::Serialize<NativeStructPtr>(std::move(native), &buf, &data, |
| 435 nullptr); |
| 432 | 436 |
| 433 EXPECT_EQ(nullptr, data); | 437 EXPECT_EQ(nullptr, data); |
| 434 | 438 |
| 435 NativeStructPtr output_native; | 439 NativeStructPtr output_native; |
| 436 Deserialize_(data, &output_native, nullptr); | 440 mojo::internal::Deserialize<NativeStructPtr>(data, &output_native, nullptr); |
| 437 EXPECT_TRUE(output_native.is_null()); | 441 EXPECT_TRUE(output_native.is_null()); |
| 438 } | 442 } |
| 439 | 443 |
| 440 { | 444 { |
| 441 // Serialization of a native struct with null data. | 445 // Serialization of a native struct with null data. |
| 442 NativeStructPtr native(NativeStruct::New()); | 446 NativeStructPtr native(NativeStruct::New()); |
| 443 size_t size = GetSerializedSize_(native, nullptr); | 447 size_t size = |
| 448 mojo::internal::PrepareToSerialize<NativeStructPtr>(native, nullptr); |
| 444 EXPECT_EQ(0u, size); | 449 EXPECT_EQ(0u, size); |
| 445 mojo::internal::FixedBufferForTesting buf(size); | 450 mojo::internal::FixedBufferForTesting buf(size); |
| 446 | 451 |
| 447 Data* data = nullptr; | 452 Data* data = nullptr; |
| 448 Serialize_(std::move(native), &buf, &data, nullptr); | 453 mojo::internal::Serialize<NativeStructPtr>(std::move(native), &buf, &data, |
| 454 nullptr); |
| 449 | 455 |
| 450 EXPECT_EQ(nullptr, data); | 456 EXPECT_EQ(nullptr, data); |
| 451 | 457 |
| 452 NativeStructPtr output_native; | 458 NativeStructPtr output_native; |
| 453 Deserialize_(data, &output_native, nullptr); | 459 mojo::internal::Deserialize<NativeStructPtr>(data, &output_native, nullptr); |
| 454 EXPECT_TRUE(output_native.is_null()); | 460 EXPECT_TRUE(output_native.is_null()); |
| 455 } | 461 } |
| 456 | 462 |
| 457 { | 463 { |
| 458 NativeStructPtr native(NativeStruct::New()); | 464 NativeStructPtr native(NativeStruct::New()); |
| 459 native->data = Array<uint8_t>(2); | 465 native->data = Array<uint8_t>(2); |
| 460 native->data[0] = 'X'; | 466 native->data[0] = 'X'; |
| 461 native->data[1] = 'Y'; | 467 native->data[1] = 'Y'; |
| 462 | 468 |
| 463 size_t size = GetSerializedSize_(native, nullptr); | 469 size_t size = |
| 470 mojo::internal::PrepareToSerialize<NativeStructPtr>(native, nullptr); |
| 464 EXPECT_EQ(16u, size); | 471 EXPECT_EQ(16u, size); |
| 465 mojo::internal::FixedBufferForTesting buf(size); | 472 mojo::internal::FixedBufferForTesting buf(size); |
| 466 | 473 |
| 467 Data* data = nullptr; | 474 Data* data = nullptr; |
| 468 Serialize_(std::move(native), &buf, &data, nullptr); | 475 mojo::internal::Serialize<NativeStructPtr>(std::move(native), &buf, &data, |
| 476 nullptr); |
| 469 | 477 |
| 470 EXPECT_NE(nullptr, data); | 478 EXPECT_NE(nullptr, data); |
| 471 | 479 |
| 472 data->EncodePointers(); | 480 data->EncodePointers(); |
| 473 data->DecodePointers(); | 481 data->DecodePointers(); |
| 474 | 482 |
| 475 NativeStructPtr output_native; | 483 NativeStructPtr output_native; |
| 476 Deserialize_(data, &output_native, nullptr); | 484 mojo::internal::Deserialize<NativeStructPtr>(data, &output_native, nullptr); |
| 477 EXPECT_FALSE(output_native.is_null()); | 485 EXPECT_FALSE(output_native.is_null()); |
| 478 EXPECT_FALSE(output_native->data.is_null()); | 486 EXPECT_FALSE(output_native->data.is_null()); |
| 479 EXPECT_EQ(2u, output_native->data.size()); | 487 EXPECT_EQ(2u, output_native->data.size()); |
| 480 EXPECT_EQ('X', output_native->data[0]); | 488 EXPECT_EQ('X', output_native->data[0]); |
| 481 EXPECT_EQ('Y', output_native->data[1]); | 489 EXPECT_EQ('Y', output_native->data[1]); |
| 482 } | 490 } |
| 483 } | 491 } |
| 484 | 492 |
| 485 } // namespace test | 493 } // namespace test |
| 486 } // namespace mojo | 494 } // namespace mojo |
| OLD | NEW |