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

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, 9 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 aba7faa6fbeaf07ebe18f86cfe548c9bf9a98efe..3ef9dc3e648494ccc15d38944d846ab49dc05a70 100644
--- a/mojo/public/cpp/bindings/tests/union_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/union_unittest.cc
@@ -108,8 +108,8 @@ TEST(UnionTest, SerializationPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
- Serialize_(pod1.Pass(), &buf, &data);
+ internal::PodUnion_Data* data = nullptr;
+ SerializeUnion_(pod1.Pass(), &buf, &data, false);
PodUnionPtr pod2;
Deserialize_(data, &pod2);
@@ -119,7 +119,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,19 +127,60 @@ TEST(UnionTest, ValidationJustWorksPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
- Serialize_(pod.Pass(), &buf, &data);
+ internal::PodUnion_Data* data = nullptr;
+ SerializeUnion_(pod.Pass(), &buf, &data, false);
void* raw_buf = buf.Leak();
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(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;
+ SerializeUnion_(pod.Pass(), &buf, &data, false);
+ EXPECT_FALSE(data->is_null());
+}
+
+TEST(UnionTest, SerializeIsNullInlined) {
+ PodUnionPtr pod;
+ size_t size = GetSerializedSize_(pod);
+ EXPECT_EQ(16U, size);
+ mojo::internal::FixedBuffer buf(size);
+ internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
+
+ // Check that dirty output buffers are handled correctly by serialization.
+ data->size = 16U;
+ data->tag = PodUnion::Tag::F_UINT16;
+ data->data.f_f_int16 = 20;
+
+ SerializeUnion_(pod.Pass(), &buf, &data, true);
+ EXPECT_TRUE(data->is_null());
+
+ PodUnionPtr pod2;
+ Deserialize_(data, &pod2);
+ EXPECT_TRUE(pod2.is_null());
+}
+
+TEST(UnionTest, SerializeIsNullNotInlined) {
+ PodUnionPtr pod;
+ size_t size = GetSerializedSize_(pod);
+ EXPECT_EQ(16U, size);
+ mojo::internal::FixedBuffer buf(size);
+ internal::PodUnion_Data* data = nullptr;
+ SerializeUnion_(pod.Pass(), &buf, &data, false);
+ EXPECT_EQ(nullptr, data);
+}
+
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) {
@@ -154,7 +195,7 @@ TEST(UnionTest, OutOfAlignmentValidation) {
reinterpret_cast<internal::PodUnion_Data*>(buf);
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(size), 0);
- EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker));
+ EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false));
free(raw_buf);
}
@@ -166,7 +207,8 @@ TEST(UnionTest, OOBValidation) {
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(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);
}
@@ -179,7 +221,8 @@ TEST(UnionTest, UnknownTagValidation) {
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(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);
}
@@ -224,8 +267,8 @@ TEST(UnionTest, StringSerialization) {
size_t size = GetSerializedSize_(pod1);
mojo::internal::FixedBuffer buf(size);
- internal::ObjectUnion_Data* data;
- Serialize_(pod1.Pass(), &buf, &data);
+ internal::ObjectUnion_Data* data = nullptr;
+ SerializeUnion_(pod1.Pass(), &buf, &data, false);
ObjectUnionPtr pod2;
Deserialize_(data, &pod2);
@@ -234,7 +277,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);
@@ -244,11 +287,12 @@ TEST(UnionTest, StringValidationNull) {
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(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);
@@ -258,11 +302,12 @@ TEST(UnionTest, StringValidationPointerOverflow) {
mojo::internal::BoundsChecker bounds_checker(data,
static_cast<uint32_t>(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, StringValidateOOB) {
Environment environment;
size_t size = 32;
mojo::internal::FixedBuffer buf(size);
@@ -277,7 +322,8 @@ 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);
}
} // namespace test

Powered by Google App Engine
This is Rietveld 408576698