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