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 <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
8 #include "mojo/public/cpp/environment/environment.h" | 8 #include "mojo/public/cpp/environment/environment.h" |
9 #include "mojo/public/cpp/system/message_pipe.h" | 9 #include "mojo/public/cpp/system/message_pipe.h" |
10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | 10 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
11 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 namespace test { | 15 namespace test { |
15 namespace { | 16 namespace { |
16 | 17 |
17 RectPtr MakeRect(int32_t factor = 1) { | 18 RectPtr MakeRect(int32_t factor = 1) { |
18 RectPtr rect(Rect::New()); | 19 RectPtr rect(Rect::New()); |
19 rect->x = 1 * factor; | 20 rect->x = 1 * factor; |
20 rect->y = 2 * factor; | 21 rect->y = 2 * factor; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 EXPECT_EQ(2u, clone_region->rects.size()); | 128 EXPECT_EQ(2u, clone_region->rects.size()); |
128 EXPECT_TRUE(clone_region->rects[0].is_null()); | 129 EXPECT_TRUE(clone_region->rects[0].is_null()); |
129 CheckRect(*clone_region->rects[1]); | 130 CheckRect(*clone_region->rects[1]); |
130 | 131 |
131 // NoDefaultFieldValues contains handles, so Clone() is not available, but | 132 // NoDefaultFieldValues contains handles, so Clone() is not available, but |
132 // NoDefaultFieldValuesPtr should still compile. | 133 // NoDefaultFieldValuesPtr should still compile. |
133 NoDefaultFieldValuesPtr no_default_field_values(NoDefaultFieldValues::New()); | 134 NoDefaultFieldValuesPtr no_default_field_values(NoDefaultFieldValues::New()); |
134 EXPECT_FALSE(no_default_field_values->f13.is_valid()); | 135 EXPECT_FALSE(no_default_field_values->f13.is_valid()); |
135 } | 136 } |
136 | 137 |
138 TEST_F(StructTest, Clone_Union) { | |
139 SmallStructPtr small_struct(SmallStruct::New()); | |
140 small_struct->pod_union = PodUnion::New(); | |
141 small_struct->pod_union->set_f_int8(10); | |
142 | |
143 SmallStructPtr clone = small_struct.Clone(); | |
144 EXPECT_EQ(10, clone->pod_union->get_f_int8()); | |
145 } | |
146 | |
137 // Serialization test of a struct with no pointer or handle members. | 147 // Serialization test of a struct with no pointer or handle members. |
138 TEST_F(StructTest, Serialization_Basic) { | 148 TEST_F(StructTest, Serialization_Basic) { |
139 RectPtr rect(MakeRect()); | 149 RectPtr rect(MakeRect()); |
140 | 150 |
141 size_t size = GetSerializedSize_(rect); | 151 size_t size = GetSerializedSize_(rect); |
142 EXPECT_EQ(8U + 16U, size); | 152 EXPECT_EQ(8U + 16U, size); |
143 | 153 |
144 mojo::internal::FixedBuffer buf(size); | 154 mojo::internal::FixedBuffer buf(size); |
145 internal::Rect_Data* data; | 155 internal::Rect_Data* data; |
146 Serialize_(rect.Pass(), &buf, &data); | 156 Serialize_(rect.Pass(), &buf, &data); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 internal::NamedRegion_Data* data; | 245 internal::NamedRegion_Data* data; |
236 Serialize_(region.Pass(), &buf, &data); | 246 Serialize_(region.Pass(), &buf, &data); |
237 | 247 |
238 NamedRegionPtr region2; | 248 NamedRegionPtr region2; |
239 Deserialize_(data, ®ion2); | 249 Deserialize_(data, ®ion2); |
240 | 250 |
241 EXPECT_TRUE(region2->name.is_null()); | 251 EXPECT_TRUE(region2->name.is_null()); |
242 EXPECT_TRUE(region2->rects.is_null()); | 252 EXPECT_TRUE(region2->rects.is_null()); |
243 } | 253 } |
244 | 254 |
255 // Serialization test of a struct with a union of plain of data. | |
yzshen1
2015/03/27 16:26:33
nit: plain *old* data? (and line 273)
azani
2015/03/27 20:50:14
Done.
| |
256 TEST_F(StructTest, Serialization_UnionOfPods) { | |
257 SmallStructPtr small_struct(SmallStruct::New()); | |
258 small_struct->pod_union = PodUnion::New(); | |
259 small_struct->pod_union->set_f_int32(10); | |
260 | |
261 size_t size = GetSerializedSize_(small_struct); | |
262 | |
263 mojo::internal::FixedBuffer buf(size); | |
264 internal::SmallStruct_Data* data = nullptr; | |
265 Serialize_(small_struct.Pass(), &buf, &data); | |
266 | |
267 SmallStructPtr deserialized; | |
268 Deserialize_(data, &deserialized); | |
269 | |
270 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | |
271 } | |
272 | |
273 // Serialization test of a struct with a union of plain of structs. | |
274 TEST_F(StructTest, Serialization_UnionOfObjects) { | |
275 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | |
276 obj_struct->obj_union = ObjectUnion::New(); | |
277 String hello("hello world"); | |
278 obj_struct->obj_union->set_f_string(hello); | |
279 | |
280 size_t size = GetSerializedSize_(obj_struct); | |
281 | |
282 mojo::internal::FixedBuffer buf(size); | |
283 internal::SmallObjStruct_Data* data = nullptr; | |
284 Serialize_(obj_struct.Pass(), &buf, &data); | |
285 | |
286 SmallObjStructPtr deserialized; | |
287 Deserialize_(data, &deserialized); | |
288 | |
289 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | |
290 } | |
291 | |
245 // Tests deserializing structs as a newer version. | 292 // Tests deserializing structs as a newer version. |
246 TEST_F(StructTest, Versioning_OldToNew) { | 293 TEST_F(StructTest, Versioning_OldToNew) { |
247 { | 294 { |
248 MultiVersionStructV0Ptr input(MultiVersionStructV0::New()); | 295 MultiVersionStructV0Ptr input(MultiVersionStructV0::New()); |
249 input->f_int32 = 123; | 296 input->f_int32 = 123; |
250 MultiVersionStructPtr expected_output(MultiVersionStruct::New()); | 297 MultiVersionStructPtr expected_output(MultiVersionStruct::New()); |
251 expected_output->f_int32 = 123; | 298 expected_output->f_int32 = 123; |
252 | 299 |
253 MultiVersionStructPtr output = | 300 MultiVersionStructPtr output = |
254 SerializeAndDeserialize<MultiVersionStructPtr>(input.Pass()); | 301 SerializeAndDeserialize<MultiVersionStructPtr>(input.Pass()); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 MultiVersionStructV0Ptr expected_output(MultiVersionStructV0::New()); | 459 MultiVersionStructV0Ptr expected_output(MultiVersionStructV0::New()); |
413 expected_output->f_int32 = 123; | 460 expected_output->f_int32 = 123; |
414 | 461 |
415 MultiVersionStructV0Ptr output = | 462 MultiVersionStructV0Ptr output = |
416 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); | 463 SerializeAndDeserialize<MultiVersionStructV0Ptr>(input.Pass()); |
417 EXPECT_TRUE(output); | 464 EXPECT_TRUE(output); |
418 EXPECT_TRUE(output->Equals(*expected_output)); | 465 EXPECT_TRUE(output->Equals(*expected_output)); |
419 } | 466 } |
420 } | 467 } |
421 | 468 |
469 // Validation test of a struct with a union. | |
470 TEST_F(StructTest, Validation_UnionsInStruct) { | |
471 SmallStructPtr small_struct(SmallStruct::New()); | |
472 small_struct->pod_union = PodUnion::New(); | |
473 small_struct->pod_union->set_f_int32(10); | |
474 | |
475 size_t size = GetSerializedSize_(small_struct); | |
476 | |
477 mojo::internal::FixedBuffer buf(size); | |
478 internal::SmallStruct_Data* data = nullptr; | |
479 Serialize_(small_struct.Pass(), &buf, &data); | |
480 | |
481 void* raw_buf = buf.Leak(); | |
482 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
483 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
484 free(raw_buf); | |
485 } | |
486 | |
487 // Validation test of a struct union fails due to unknown union tag. | |
488 TEST_F(StructTest, Validation_PodUnionInStruct_Failure) { | |
489 SmallStructPtr small_struct(SmallStruct::New()); | |
490 small_struct->pod_union = PodUnion::New(); | |
491 small_struct->pod_union->set_f_int32(10); | |
492 | |
493 size_t size = GetSerializedSize_(small_struct); | |
494 | |
495 mojo::internal::FixedBuffer buf(size); | |
496 internal::SmallStruct_Data* data = nullptr; | |
497 Serialize_(small_struct.Pass(), &buf, &data); | |
498 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | |
499 | |
500 void* raw_buf = buf.Leak(); | |
501 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
502 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
503 free(raw_buf); | |
504 } | |
505 | |
506 // Validation fails due to non-nullable null union in struct. | |
507 TEST_F(StructTest, Validation_NullUnion_Failure) { | |
508 SmallStructNonNullableUnionPtr small_struct( | |
509 SmallStructNonNullableUnion::New()); | |
510 | |
511 size_t size = GetSerializedSize_(small_struct); | |
512 | |
513 mojo::internal::FixedBuffer buf(size); | |
514 internal::SmallStructNonNullableUnion_Data* data = | |
515 internal::SmallStructNonNullableUnion_Data::New(&buf); | |
516 | |
517 void* raw_buf = buf.Leak(); | |
518 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
519 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( | |
520 raw_buf, &bounds_checker)); | |
521 free(raw_buf); | |
522 } | |
523 | |
524 // Validation passes with nullable null union. | |
525 TEST_F(StructTest, Validation_NullableUnion) { | |
526 SmallStructPtr small_struct(SmallStruct::New()); | |
527 | |
528 size_t size = GetSerializedSize_(small_struct); | |
529 | |
530 mojo::internal::FixedBuffer buf(size); | |
531 internal::SmallStruct_Data* data = nullptr; | |
532 Serialize_(small_struct.Pass(), &buf, &data); | |
533 | |
534 void* raw_buf = buf.Leak(); | |
535 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
536 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
537 free(raw_buf); | |
538 } | |
539 | |
422 } // namespace test | 540 } // namespace test |
423 } // namespace mojo | 541 } // namespace mojo |
OLD | NEW |