Chromium Code Reviews| Index: mojo/public/cpp/bindings/tests/struct_unittest.cc |
| diff --git a/mojo/public/cpp/bindings/tests/struct_unittest.cc b/mojo/public/cpp/bindings/tests/struct_unittest.cc |
| index 7f5a376cc63ae5513b0fd764fd5953687a952361..d4a90718bbce7a63ae89a8c60865172188b2aa4a 100644 |
| --- a/mojo/public/cpp/bindings/tests/struct_unittest.cc |
| +++ b/mojo/public/cpp/bindings/tests/struct_unittest.cc |
| @@ -8,6 +8,7 @@ |
| #include "mojo/public/cpp/environment/environment.h" |
| #include "mojo/public/cpp/system/message_pipe.h" |
| #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
| +#include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace mojo { |
| @@ -134,6 +135,15 @@ TEST_F(StructTest, Clone) { |
| EXPECT_FALSE(no_default_field_values->f13.is_valid()); |
| } |
| +TEST_F(StructTest, Clone_Union) { |
| + SmallStructPtr small_struct(SmallStruct::New()); |
| + small_struct->pod_union = PodUnion::New(); |
| + small_struct->pod_union->set_f_int8(10); |
| + |
| + SmallStructPtr clone = small_struct.Clone(); |
| + EXPECT_EQ(10, clone->pod_union->get_f_int8()); |
| +} |
| + |
| // Serialization test of a struct with no pointer or handle members. |
| TEST_F(StructTest, Serialization_Basic) { |
| RectPtr rect(MakeRect()); |
| @@ -242,6 +252,43 @@ TEST_F(StructTest, Serialization_NullArrayPointers) { |
| EXPECT_TRUE(region2->rects.is_null()); |
| } |
| +// 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.
|
| +TEST_F(StructTest, Serialization_UnionOfPods) { |
| + SmallStructPtr small_struct(SmallStruct::New()); |
| + small_struct->pod_union = PodUnion::New(); |
| + small_struct->pod_union->set_f_int32(10); |
| + |
| + size_t size = GetSerializedSize_(small_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallStruct_Data* data = nullptr; |
| + Serialize_(small_struct.Pass(), &buf, &data); |
| + |
| + SmallStructPtr deserialized; |
| + Deserialize_(data, &deserialized); |
| + |
| + EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); |
| +} |
| + |
| +// Serialization test of a struct with a union of plain of structs. |
| +TEST_F(StructTest, Serialization_UnionOfObjects) { |
| + SmallObjStructPtr obj_struct(SmallObjStruct::New()); |
| + obj_struct->obj_union = ObjectUnion::New(); |
| + String hello("hello world"); |
| + obj_struct->obj_union->set_f_string(hello); |
| + |
| + size_t size = GetSerializedSize_(obj_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallObjStruct_Data* data = nullptr; |
| + Serialize_(obj_struct.Pass(), &buf, &data); |
| + |
| + SmallObjStructPtr deserialized; |
| + Deserialize_(data, &deserialized); |
| + |
| + EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); |
| +} |
| + |
| // Tests deserializing structs as a newer version. |
| TEST_F(StructTest, Versioning_OldToNew) { |
| { |
| @@ -419,5 +466,76 @@ TEST_F(StructTest, Versioning_NewToOld) { |
| } |
| } |
| +// Validation test of a struct with a union. |
| +TEST_F(StructTest, Validation_UnionsInStruct) { |
| + SmallStructPtr small_struct(SmallStruct::New()); |
| + small_struct->pod_union = PodUnion::New(); |
| + small_struct->pod_union->set_f_int32(10); |
| + |
| + size_t size = GetSerializedSize_(small_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallStruct_Data* data = nullptr; |
| + Serialize_(small_struct.Pass(), &buf, &data); |
| + |
| + void* raw_buf = buf.Leak(); |
| + mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| + EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
| + free(raw_buf); |
| +} |
| + |
| +// Validation test of a struct union fails due to unknown union tag. |
| +TEST_F(StructTest, Validation_PodUnionInStruct_Failure) { |
| + SmallStructPtr small_struct(SmallStruct::New()); |
| + small_struct->pod_union = PodUnion::New(); |
| + small_struct->pod_union->set_f_int32(10); |
| + |
| + size_t size = GetSerializedSize_(small_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallStruct_Data* data = nullptr; |
| + Serialize_(small_struct.Pass(), &buf, &data); |
| + data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); |
| + |
| + void* raw_buf = buf.Leak(); |
| + mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| + EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
| + free(raw_buf); |
| +} |
| + |
| +// Validation fails due to non-nullable null union in struct. |
| +TEST_F(StructTest, Validation_NullUnion_Failure) { |
| + SmallStructNonNullableUnionPtr small_struct( |
| + SmallStructNonNullableUnion::New()); |
| + |
| + size_t size = GetSerializedSize_(small_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallStructNonNullableUnion_Data* data = |
| + internal::SmallStructNonNullableUnion_Data::New(&buf); |
| + |
| + void* raw_buf = buf.Leak(); |
| + mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| + EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( |
| + raw_buf, &bounds_checker)); |
| + free(raw_buf); |
| +} |
| + |
| +// Validation passes with nullable null union. |
| +TEST_F(StructTest, Validation_NullableUnion) { |
| + SmallStructPtr small_struct(SmallStruct::New()); |
| + |
| + size_t size = GetSerializedSize_(small_struct); |
| + |
| + mojo::internal::FixedBuffer buf(size); |
| + internal::SmallStruct_Data* data = nullptr; |
| + Serialize_(small_struct.Pass(), &buf, &data); |
| + |
| + void* raw_buf = buf.Leak(); |
| + mojo::internal::BoundsChecker bounds_checker(data, size, 0); |
| + EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); |
| + free(raw_buf); |
| +} |
| + |
| } // namespace test |
| } // namespace mojo |