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..70a93cb16a6fe954ffc3ed9dfbe8ab66c0326b8b 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 { |
| @@ -419,5 +420,116 @@ TEST_F(StructTest, Versioning_NewToOld) { |
| } |
| } |
| +TEST_F(StructTest, PodUnionInStructClone) { |
| + 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()); |
| +} |
| + |
| +TEST_F(StructTest, PodUnionInStructSerialization) { |
|
yzshen1
2015/03/26 07:30:14
nit: the naming is not very consistent within this
azani
2015/03/26 22:27:39
Done.
|
| + 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()); |
| +} |
| + |
| +TEST_F(StructTest, ObjectUnionInStructSerialization) { |
| + 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()); |
| +} |
| + |
| +TEST_F(StructTest, UnionsInStructsValidation) { |
| + 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); |
| +} |
| + |
| +TEST_F(StructTest, PodUnionInStructValidationFailure) { |
| + 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); |
| +} |
| + |
| +TEST_F(StructTest, NullUnionValidationFailure) { |
| + 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); |
| +} |
| + |
| +TEST_F(StructTest, NullableUnionValidation) { |
| + 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 |