Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Unified Diff: mojo/public/cpp/bindings/tests/union_unittest.cc

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/tests/union_unittest.cc
diff --git a/mojo/public/cpp/bindings/tests/union_unittest.cc b/mojo/public/cpp/bindings/tests/union_unittest.cc
index f811a22a85cef5d8bc485a75f3497b541318d8f1..df68254ebf472244ccaded6d410f86f12156c102 100644
--- a/mojo/public/cpp/bindings/tests/union_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/union_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "mojo/public/cpp/bindings/lib/array_internal.h"
+#include "mojo/public/cpp/bindings/lib/array_serialization.h"
#include "mojo/public/cpp/bindings/lib/bounds_checker.h"
#include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
#include "mojo/public/cpp/bindings/string.h"
@@ -108,7 +110,7 @@ TEST(UnionTest, SerializationPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
+ internal::PodUnion_Data* data = nullptr;
Serialize_(pod1.Pass(), &buf, &data);
PodUnionPtr pod2;
@@ -119,7 +121,7 @@ TEST(UnionTest, SerializationPod) {
EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8);
}
-TEST(UnionTest, ValidationJustWorksPod) {
+TEST(UnionTest, PodValidation) {
PodUnionPtr pod(PodUnion::New());
pod->set_f_int8(10);
@@ -127,18 +129,43 @@ TEST(UnionTest, ValidationJustWorksPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
+ internal::PodUnion_Data* data = nullptr;
Serialize_(pod.Pass(), &buf, &data);
void* raw_buf = buf.Leak();
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
- EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_TRUE(
+ internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
+TEST(UnionTest, SerializeNotNull) {
+ PodUnionPtr pod(PodUnion::New());
+ pod->set_f_int8(0);
+ size_t size = GetSerializedSize_(pod);
+ mojo::internal::FixedBuffer buf(size);
+ internal::PodUnion_Data* data = nullptr;
+ Serialize_(pod.Pass(), &buf, &data);
+ EXPECT_FALSE(data->is_null());
+}
+
+TEST(UnionTest, SerializeIsNull) {
+ PodUnionPtr pod;
+ size_t size = GetSerializedSize_(pod);
+ EXPECT_EQ(16U, size);
+ mojo::internal::FixedBuffer buf(size);
+ internal::PodUnion_Data* data = nullptr;
+ Serialize_(pod.Pass(), &buf, &data);
+ EXPECT_TRUE(data->is_null());
+
+ PodUnionPtr pod2;
+ Deserialize_(data, &pod2);
+ EXPECT_TRUE(pod2.is_null());
+}
+
TEST(UnionTest, NullValidation) {
void* buf = nullptr;
mojo::internal::BoundsChecker bounds_checker(buf, 0, 0);
- EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker));
+ EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false));
}
TEST(UnionTest, OutOfAlignmentValidation) {
@@ -152,7 +179,7 @@ TEST(UnionTest, OutOfAlignmentValidation) {
internal::PodUnion_Data* data =
reinterpret_cast<internal::PodUnion_Data*>(buf);
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
- EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker));
+ EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false));
free(raw_buf);
}
@@ -163,7 +190,8 @@ TEST(UnionTest, OOBValidation) {
internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
void* raw_buf = buf.Leak();
- EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_FALSE(
+ internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
@@ -175,7 +203,8 @@ TEST(UnionTest, UnknownTagValidation) {
data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF);
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
void* raw_buf = buf.Leak();
- EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_FALSE(
+ internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
@@ -220,7 +249,7 @@ TEST(UnionTest, StringSerialization) {
size_t size = GetSerializedSize_(pod1);
mojo::internal::FixedBuffer buf(size);
- internal::ObjectUnion_Data* data;
+ internal::ObjectUnion_Data* data = nullptr;
Serialize_(pod1.Pass(), &buf, &data);
ObjectUnionPtr pod2;
@@ -230,7 +259,7 @@ TEST(UnionTest, StringSerialization) {
EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING);
}
-TEST(UnionTest, StringValidationNull) {
+TEST(UnionTest, NullStringValidation) {
Environment environment;
size_t size = sizeof(internal::ObjectUnion_Data);
mojo::internal::FixedBuffer buf(size);
@@ -239,11 +268,12 @@ TEST(UnionTest, StringValidationNull) {
data->data.unknown = 0x0;
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
void* raw_buf = buf.Leak();
- EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_FALSE(
+ internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
-TEST(UnionTest, StringValidationPointerOverflow) {
+TEST(UnionTest, StringPointerOverflowValidation) {
Environment environment;
size_t size = sizeof(internal::ObjectUnion_Data);
mojo::internal::FixedBuffer buf(size);
@@ -252,11 +282,12 @@ TEST(UnionTest, StringValidationPointerOverflow) {
data->data.unknown = 0xFFFFFFFFFFFFFFFF;
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
void* raw_buf = buf.Leak();
- EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_FALSE(
+ internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
-TEST(UnionTest, StringValidationValidateString) {
+TEST(UnionTest, StringValidateStringValidation) {
yzshen1 2015/02/25 21:07:00 nit: I cannot parse this test name...
azani 2015/03/03 00:44:15 Done.
Environment environment;
size_t size = 32;
mojo::internal::FixedBuffer buf(size);
@@ -271,8 +302,217 @@ TEST(UnionTest, StringValidationValidateString) {
array_header->num_elements = 20;
mojo::internal::BoundsChecker bounds_checker(data, 32, 0);
void* raw_buf = buf.Leak();
- EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker));
+ EXPECT_FALSE(
+ internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
free(raw_buf);
}
+
+TEST(UnionTest, 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(UnionTest, PodUnionInStructSerialization) {
+ 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(UnionTest, 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(UnionTest, 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(UnionTest, PodUnionInStructValidationFailure) {
+ Environment environment;
+ 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(UnionTest, NullUnionValidationFailure) {
+ Environment environment;
+ 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_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
+ free(raw_buf);
+}
+
+TEST(UnionTest, NullableUnionValidation) {
+ Environment environment;
+ SmallStructNullableUnionPtr small_struct(SmallStructNullableUnion::New());
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStructNullableUnion_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::SmallStructNullableUnion_Data::Validate(
+ raw_buf, &bounds_checker));
+ free(raw_buf);
+}
+
+TEST(UnionTest, PodUnionInArray) {
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union_array = Array<PodUnionPtr>(2);
+ small_struct->pod_union_array[0] = PodUnion::New();
+ small_struct->pod_union_array[1] = PodUnion::New();
+
+ small_struct->pod_union_array[0]->set_f_int8(10);
+ small_struct->pod_union_array[1]->set_f_int16(12);
+
+ EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8());
+ EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16());
+}
+
+TEST(UnionTest, PodUnionInArraySerialization) {
+ Environment environment;
+
+ Array<PodUnionPtr> array(2);
+ array[0] = PodUnion::New();
+ array[1] = PodUnion::New();
+
+ array[0]->set_f_int8(10);
+ array[1]->set_f_int16(12);
+ EXPECT_EQ(2U, array.size());
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(40U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ mojo::internal::Array_Data<internal::PodUnion_Data>* data;
+ SerializeArray_<mojo::internal::ArrayValidateParams<
+ 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
+
+ Array<PodUnionPtr> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(2U, array2.size());
+
+ EXPECT_EQ(10, array2[0]->get_f_int8());
+ EXPECT_EQ(12, array2[1]->get_f_int16());
+}
+
+TEST(UnionTest, PodUnionInArraySerializationWithNull) {
+ Environment environment;
+
+ Array<PodUnionPtr> array(2);
+ array[0] = PodUnion::New();
+
+ array[0]->set_f_int8(10);
+ EXPECT_EQ(2U, array.size());
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(40U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ mojo::internal::Array_Data<internal::PodUnion_Data>* data;
+ SerializeArray_<mojo::internal::ArrayValidateParams<
+ 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
yzshen1 2015/02/25 21:07:00 false -> true, meaning that elements are nullable.
azani 2015/03/03 00:44:15 Done.
+
+ Array<PodUnionPtr> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(2U, array2.size());
+
+ EXPECT_EQ(10, array2[0]->get_f_int8());
+ EXPECT_TRUE(array2[1].is_null());
+}
+
+/*
yzshen1 2015/02/25 21:07:00 Please remove any code if it is not needed. (Or ma
+TEST(UnionTest, PodUnionInArrayInStruct) {
+ Environment environment;
+ SmallStructPtr small_struct(SmallStruct::New());
+
+ small_struct->pod_union_array = Array<PodUnionPtr>(2);
+ small_struct->pod_union_array[0] = PodUnion::New();
+ small_struct->pod_union_array[1] = PodUnion::New();
+
+ small_struct->pod_union_array[0]->set_f_int8(10);
+ small_struct->pod_union_array[1]->set_f_int16(12);
+
+ size_t size = GetSerializedSize_(small_struct);
+ EXPECT_EQ(88U, size);
+
+ 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_array[0]->get_f_int8());
+ EXPECT_EQ(12, deserialized->pod_union_array[1]->get_f_int16());
+}
+*/
+
} // namespace test
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698