Chromium Code Reviews| 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 "mojo/public/cpp/bindings/lib/array_internal.h" | |
| 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | |
| 5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 7 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 6 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 7 #include "mojo/public/cpp/bindings/string.h" | 9 #include "mojo/public/cpp/bindings/string.h" |
| 8 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 9 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" | 11 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 namespace mojo { | 14 namespace mojo { |
| 13 namespace test { | 15 namespace test { |
| 14 | 16 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 } | 103 } |
| 102 | 104 |
| 103 TEST(UnionTest, SerializationPod) { | 105 TEST(UnionTest, SerializationPod) { |
| 104 PodUnionPtr pod1(PodUnion::New()); | 106 PodUnionPtr pod1(PodUnion::New()); |
| 105 pod1->set_f_int8(10); | 107 pod1->set_f_int8(10); |
| 106 | 108 |
| 107 size_t size = GetSerializedSize_(pod1); | 109 size_t size = GetSerializedSize_(pod1); |
| 108 EXPECT_EQ(16U, size); | 110 EXPECT_EQ(16U, size); |
| 109 | 111 |
| 110 mojo::internal::FixedBuffer buf(size); | 112 mojo::internal::FixedBuffer buf(size); |
| 111 internal::PodUnion_Data* data; | 113 internal::PodUnion_Data* data = nullptr; |
| 112 Serialize_(pod1.Pass(), &buf, &data); | 114 SerializeUnion_(pod1.Pass(), &buf, &data, false); |
| 113 | 115 |
| 114 PodUnionPtr pod2; | 116 PodUnionPtr pod2; |
| 115 Deserialize_(data, &pod2); | 117 Deserialize_(data, &pod2); |
| 116 | 118 |
| 117 EXPECT_EQ(10, pod2->get_f_int8()); | 119 EXPECT_EQ(10, pod2->get_f_int8()); |
| 118 EXPECT_TRUE(pod2->is_f_int8()); | 120 EXPECT_TRUE(pod2->is_f_int8()); |
| 119 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); | 121 EXPECT_EQ(pod2->which(), PodUnion::Tag::F_INT8); |
| 120 } | 122 } |
| 121 | 123 |
| 122 TEST(UnionTest, ValidationJustWorksPod) { | 124 TEST(UnionTest, PodValidation) { |
| 123 PodUnionPtr pod(PodUnion::New()); | 125 PodUnionPtr pod(PodUnion::New()); |
| 124 pod->set_f_int8(10); | 126 pod->set_f_int8(10); |
| 125 | 127 |
| 126 size_t size = GetSerializedSize_(pod); | 128 size_t size = GetSerializedSize_(pod); |
| 127 EXPECT_EQ(16U, size); | 129 EXPECT_EQ(16U, size); |
| 128 | 130 |
| 129 mojo::internal::FixedBuffer buf(size); | 131 mojo::internal::FixedBuffer buf(size); |
| 130 internal::PodUnion_Data* data; | 132 internal::PodUnion_Data* data = nullptr; |
| 131 Serialize_(pod.Pass(), &buf, &data); | 133 SerializeUnion_(pod.Pass(), &buf, &data, false); |
| 132 void* raw_buf = buf.Leak(); | 134 void* raw_buf = buf.Leak(); |
| 133 mojo::internal::BoundsChecker bounds_checker(data, | 135 mojo::internal::BoundsChecker bounds_checker(data, |
| 134 static_cast<uint32_t>(size), 0); | 136 static_cast<uint32_t>(size), 0); |
| 135 EXPECT_TRUE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 137 EXPECT_TRUE( |
| 138 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 136 free(raw_buf); | 139 free(raw_buf); |
| 137 } | 140 } |
| 138 | 141 |
| 142 TEST(UnionTest, SerializeNotNull) { | |
| 143 PodUnionPtr pod(PodUnion::New()); | |
| 144 pod->set_f_int8(0); | |
| 145 size_t size = GetSerializedSize_(pod); | |
| 146 mojo::internal::FixedBuffer buf(size); | |
| 147 internal::PodUnion_Data* data = nullptr; | |
| 148 SerializeUnion_(pod.Pass(), &buf, &data, false); | |
| 149 EXPECT_FALSE(data->is_null()); | |
| 150 } | |
| 151 | |
| 152 TEST(UnionTest, SerializeIsNull) { | |
| 153 PodUnionPtr pod; | |
| 154 size_t size = GetSerializedSize_(pod); | |
| 155 EXPECT_EQ(16U, size); | |
| 156 mojo::internal::FixedBuffer buf(size); | |
| 157 internal::PodUnion_Data* data = nullptr; | |
| 158 SerializeUnion_(pod.Pass(), &buf, &data, false); | |
| 159 EXPECT_TRUE(data->is_null()); | |
| 160 | |
| 161 PodUnionPtr pod2; | |
| 162 Deserialize_(data, &pod2); | |
| 163 EXPECT_TRUE(pod2.is_null()); | |
| 164 } | |
| 165 | |
| 139 TEST(UnionTest, NullValidation) { | 166 TEST(UnionTest, NullValidation) { |
| 140 void* buf = nullptr; | 167 void* buf = nullptr; |
| 141 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); | 168 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); |
| 142 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); | 169 EXPECT_TRUE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
| 143 } | 170 } |
| 144 | 171 |
| 145 TEST(UnionTest, OutOfAlignmentValidation) { | 172 TEST(UnionTest, OutOfAlignmentValidation) { |
| 146 Environment environment; | 173 Environment environment; |
| 147 size_t size = sizeof(internal::PodUnion_Data); | 174 size_t size = sizeof(internal::PodUnion_Data); |
| 148 // Get an aligned object and shift the alignment. | 175 // Get an aligned object and shift the alignment. |
| 149 mojo::internal::FixedBuffer aligned_buf(size + 1); | 176 mojo::internal::FixedBuffer aligned_buf(size + 1); |
| 150 void* raw_buf = aligned_buf.Leak(); | 177 void* raw_buf = aligned_buf.Leak(); |
| 151 char* buf = reinterpret_cast<char*>(raw_buf) + 1; | 178 char* buf = reinterpret_cast<char*>(raw_buf) + 1; |
| 152 | 179 |
| 153 internal::PodUnion_Data* data = | 180 internal::PodUnion_Data* data = |
| 154 reinterpret_cast<internal::PodUnion_Data*>(buf); | 181 reinterpret_cast<internal::PodUnion_Data*>(buf); |
| 155 mojo::internal::BoundsChecker bounds_checker(data, | 182 mojo::internal::BoundsChecker bounds_checker(data, |
| 156 static_cast<uint32_t>(size), 0); | 183 static_cast<uint32_t>(size), 0); |
| 157 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker)); | 184 EXPECT_FALSE(internal::PodUnion_Data::Validate(buf, &bounds_checker, false)); |
| 158 free(raw_buf); | 185 free(raw_buf); |
| 159 } | 186 } |
| 160 | 187 |
| 161 TEST(UnionTest, OOBValidation) { | 188 TEST(UnionTest, OOBValidation) { |
| 162 Environment environment; | 189 Environment environment; |
| 163 size_t size = sizeof(internal::PodUnion_Data) - 1; | 190 size_t size = sizeof(internal::PodUnion_Data) - 1; |
| 164 mojo::internal::FixedBuffer buf(size); | 191 mojo::internal::FixedBuffer buf(size); |
| 165 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 192 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
| 166 mojo::internal::BoundsChecker bounds_checker(data, | 193 mojo::internal::BoundsChecker bounds_checker(data, |
| 167 static_cast<uint32_t>(size), 0); | 194 static_cast<uint32_t>(size), 0); |
| 168 void* raw_buf = buf.Leak(); | 195 void* raw_buf = buf.Leak(); |
| 169 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 196 EXPECT_FALSE( |
| 197 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 170 free(raw_buf); | 198 free(raw_buf); |
| 171 } | 199 } |
| 172 | 200 |
| 173 TEST(UnionTest, UnknownTagValidation) { | 201 TEST(UnionTest, UnknownTagValidation) { |
| 174 Environment environment; | 202 Environment environment; |
| 175 size_t size = sizeof(internal::PodUnion_Data); | 203 size_t size = sizeof(internal::PodUnion_Data); |
| 176 mojo::internal::FixedBuffer buf(size); | 204 mojo::internal::FixedBuffer buf(size); |
| 177 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); | 205 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); |
| 178 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF); | 206 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(0xFFFFFF); |
| 179 mojo::internal::BoundsChecker bounds_checker(data, | 207 mojo::internal::BoundsChecker bounds_checker(data, |
| 180 static_cast<uint32_t>(size), 0); | 208 static_cast<uint32_t>(size), 0); |
| 181 void* raw_buf = buf.Leak(); | 209 void* raw_buf = buf.Leak(); |
| 182 EXPECT_FALSE(internal::PodUnion_Data::Validate(raw_buf, &bounds_checker)); | 210 EXPECT_FALSE( |
| 211 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 183 free(raw_buf); | 212 free(raw_buf); |
| 184 } | 213 } |
| 185 | 214 |
| 186 TEST(UnionTest, StringGetterSetter) { | 215 TEST(UnionTest, StringGetterSetter) { |
| 187 ObjectUnionPtr pod(ObjectUnion::New()); | 216 ObjectUnionPtr pod(ObjectUnion::New()); |
| 188 | 217 |
| 189 String hello("hello world"); | 218 String hello("hello world"); |
| 190 pod->set_f_string(hello); | 219 pod->set_f_string(hello); |
| 191 EXPECT_EQ(hello, pod->get_f_string()); | 220 EXPECT_EQ(hello, pod->get_f_string()); |
| 192 EXPECT_TRUE(pod->is_f_string()); | 221 EXPECT_TRUE(pod->is_f_string()); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 217 } | 246 } |
| 218 | 247 |
| 219 TEST(UnionTest, StringSerialization) { | 248 TEST(UnionTest, StringSerialization) { |
| 220 ObjectUnionPtr pod1(ObjectUnion::New()); | 249 ObjectUnionPtr pod1(ObjectUnion::New()); |
| 221 | 250 |
| 222 String hello("hello world"); | 251 String hello("hello world"); |
| 223 pod1->set_f_string(hello); | 252 pod1->set_f_string(hello); |
| 224 | 253 |
| 225 size_t size = GetSerializedSize_(pod1); | 254 size_t size = GetSerializedSize_(pod1); |
| 226 mojo::internal::FixedBuffer buf(size); | 255 mojo::internal::FixedBuffer buf(size); |
| 227 internal::ObjectUnion_Data* data; | 256 internal::ObjectUnion_Data* data = nullptr; |
| 228 Serialize_(pod1.Pass(), &buf, &data); | 257 SerializeUnion_(pod1.Pass(), &buf, &data, false); |
| 229 | 258 |
| 230 ObjectUnionPtr pod2; | 259 ObjectUnionPtr pod2; |
| 231 Deserialize_(data, &pod2); | 260 Deserialize_(data, &pod2); |
| 232 EXPECT_EQ(hello, pod2->get_f_string()); | 261 EXPECT_EQ(hello, pod2->get_f_string()); |
| 233 EXPECT_TRUE(pod2->is_f_string()); | 262 EXPECT_TRUE(pod2->is_f_string()); |
| 234 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); | 263 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); |
| 235 } | 264 } |
| 236 | 265 |
| 237 TEST(UnionTest, StringValidationNull) { | 266 TEST(UnionTest, NullStringValidation) { |
| 238 Environment environment; | 267 Environment environment; |
| 239 size_t size = sizeof(internal::ObjectUnion_Data); | 268 size_t size = sizeof(internal::ObjectUnion_Data); |
| 240 mojo::internal::FixedBuffer buf(size); | 269 mojo::internal::FixedBuffer buf(size); |
| 241 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 270 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 242 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 271 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 243 data->data.unknown = 0x0; | 272 data->data.unknown = 0x0; |
| 244 mojo::internal::BoundsChecker bounds_checker(data, | 273 mojo::internal::BoundsChecker bounds_checker(data, |
| 245 static_cast<uint32_t>(size), 0); | 274 static_cast<uint32_t>(size), 0); |
| 246 void* raw_buf = buf.Leak(); | 275 void* raw_buf = buf.Leak(); |
| 247 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 276 EXPECT_FALSE( |
| 277 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 248 free(raw_buf); | 278 free(raw_buf); |
| 249 } | 279 } |
| 250 | 280 |
| 251 TEST(UnionTest, StringValidationPointerOverflow) { | 281 TEST(UnionTest, StringPointerOverflowValidation) { |
| 252 Environment environment; | 282 Environment environment; |
| 253 size_t size = sizeof(internal::ObjectUnion_Data); | 283 size_t size = sizeof(internal::ObjectUnion_Data); |
| 254 mojo::internal::FixedBuffer buf(size); | 284 mojo::internal::FixedBuffer buf(size); |
| 255 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 285 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 256 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 286 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 257 data->data.unknown = 0xFFFFFFFFFFFFFFFF; | 287 data->data.unknown = 0xFFFFFFFFFFFFFFFF; |
| 258 mojo::internal::BoundsChecker bounds_checker(data, | 288 mojo::internal::BoundsChecker bounds_checker(data, |
| 259 static_cast<uint32_t>(size), 0); | 289 static_cast<uint32_t>(size), 0); |
| 260 void* raw_buf = buf.Leak(); | 290 void* raw_buf = buf.Leak(); |
| 261 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 291 EXPECT_FALSE( |
| 292 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 262 free(raw_buf); | 293 free(raw_buf); |
| 263 } | 294 } |
| 264 | 295 |
| 265 TEST(UnionTest, StringValidationValidateString) { | 296 TEST(UnionTest, StringValidateOOB) { |
| 266 Environment environment; | 297 Environment environment; |
| 267 size_t size = 32; | 298 size_t size = 32; |
| 268 mojo::internal::FixedBuffer buf(size); | 299 mojo::internal::FixedBuffer buf(size); |
| 269 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); | 300 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); |
| 270 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; | 301 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; |
| 271 | 302 |
| 272 data->data.f_f_string = 8; | 303 data->data.f_f_string = 8; |
| 273 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); | 304 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); |
| 274 mojo::internal::ArrayHeader* array_header = | 305 mojo::internal::ArrayHeader* array_header = |
| 275 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); | 306 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); |
| 276 array_header->num_bytes = 20; // This should go out of bounds. | 307 array_header->num_bytes = 20; // This should go out of bounds. |
| 277 array_header->num_elements = 20; | 308 array_header->num_elements = 20; |
| 278 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); | 309 mojo::internal::BoundsChecker bounds_checker(data, 32, 0); |
| 279 void* raw_buf = buf.Leak(); | 310 void* raw_buf = buf.Leak(); |
| 280 EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker)); | 311 EXPECT_FALSE( |
| 312 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); | |
| 281 free(raw_buf); | 313 free(raw_buf); |
| 282 } | 314 } |
| 315 | |
| 316 TEST(UnionTest, PodUnionInStructClone) { | |
| 317 SmallStructPtr small_struct(SmallStruct::New()); | |
| 318 small_struct->pod_union = PodUnion::New(); | |
| 319 small_struct->pod_union->set_f_int8(10); | |
| 320 | |
| 321 SmallStructPtr clone = small_struct.Clone(); | |
| 322 EXPECT_EQ(10, clone->pod_union->get_f_int8()); | |
| 323 } | |
| 324 | |
| 325 TEST(UnionTest, PodUnionInStructSerialization) { | |
| 326 Environment environment; | |
| 327 SmallStructPtr small_struct(SmallStruct::New()); | |
| 328 small_struct->pod_union = PodUnion::New(); | |
| 329 small_struct->pod_union->set_f_int32(10); | |
| 330 | |
| 331 size_t size = GetSerializedSize_(small_struct); | |
| 332 | |
| 333 mojo::internal::FixedBuffer buf(size); | |
| 334 internal::SmallStruct_Data* data = nullptr; | |
| 335 Serialize_(small_struct.Pass(), &buf, &data); | |
| 336 | |
| 337 SmallStructPtr deserialized; | |
| 338 Deserialize_(data, &deserialized); | |
| 339 | |
| 340 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); | |
| 341 } | |
| 342 | |
| 343 TEST(UnionTest, ObjectUnionInStructSerialization) { | |
| 344 Environment environment; | |
| 345 SmallObjStructPtr obj_struct(SmallObjStruct::New()); | |
| 346 obj_struct->obj_union = ObjectUnion::New(); | |
| 347 String hello("hello world"); | |
| 348 obj_struct->obj_union->set_f_string(hello); | |
| 349 | |
| 350 size_t size = GetSerializedSize_(obj_struct); | |
| 351 | |
| 352 mojo::internal::FixedBuffer buf(size); | |
| 353 internal::SmallObjStruct_Data* data = nullptr; | |
| 354 Serialize_(obj_struct.Pass(), &buf, &data); | |
| 355 | |
| 356 SmallObjStructPtr deserialized; | |
| 357 Deserialize_(data, &deserialized); | |
| 358 | |
| 359 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); | |
| 360 } | |
| 361 | |
| 362 TEST(UnionTest, UnionsInStructsValidation) { | |
| 363 Environment environment; | |
| 364 SmallStructPtr small_struct(SmallStruct::New()); | |
| 365 small_struct->pod_union = PodUnion::New(); | |
| 366 small_struct->pod_union->set_f_int32(10); | |
| 367 | |
| 368 size_t size = GetSerializedSize_(small_struct); | |
| 369 | |
| 370 mojo::internal::FixedBuffer buf(size); | |
| 371 internal::SmallStruct_Data* data = nullptr; | |
| 372 Serialize_(small_struct.Pass(), &buf, &data); | |
| 373 | |
| 374 void* raw_buf = buf.Leak(); | |
| 375 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 376 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 377 free(raw_buf); | |
| 378 } | |
| 379 | |
| 380 TEST(UnionTest, PodUnionInStructValidationFailure) { | |
| 381 Environment environment; | |
| 382 SmallStructPtr small_struct(SmallStruct::New()); | |
| 383 small_struct->pod_union = PodUnion::New(); | |
| 384 small_struct->pod_union->set_f_int32(10); | |
| 385 | |
| 386 size_t size = GetSerializedSize_(small_struct); | |
| 387 | |
| 388 mojo::internal::FixedBuffer buf(size); | |
| 389 internal::SmallStruct_Data* data = nullptr; | |
| 390 Serialize_(small_struct.Pass(), &buf, &data); | |
| 391 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); | |
| 392 | |
| 393 void* raw_buf = buf.Leak(); | |
| 394 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 395 EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 396 free(raw_buf); | |
| 397 } | |
| 398 | |
| 399 TEST(UnionTest, NullUnionValidationFailure) { | |
| 400 Environment environment; | |
| 401 SmallStructNonNullableUnionPtr small_struct( | |
| 402 SmallStructNonNullableUnion::New()); | |
| 403 | |
| 404 size_t size = GetSerializedSize_(small_struct); | |
| 405 | |
| 406 mojo::internal::FixedBuffer buf(size); | |
| 407 internal::SmallStructNonNullableUnion_Data* data = | |
| 408 internal::SmallStructNonNullableUnion_Data::New(&buf); | |
| 409 | |
| 410 void* raw_buf = buf.Leak(); | |
| 411 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 412 EXPECT_FALSE(internal::SmallStructNonNullableUnion_Data::Validate( | |
| 413 raw_buf, &bounds_checker)); | |
| 414 free(raw_buf); | |
| 415 } | |
| 416 | |
| 417 TEST(UnionTest, NullableUnionValidation) { | |
| 418 Environment environment; | |
| 419 SmallStructPtr small_struct(SmallStruct::New()); | |
| 420 | |
| 421 size_t size = GetSerializedSize_(small_struct); | |
| 422 | |
| 423 mojo::internal::FixedBuffer buf(size); | |
| 424 internal::SmallStruct_Data* data = nullptr; | |
| 425 Serialize_(small_struct.Pass(), &buf, &data); | |
| 426 | |
| 427 void* raw_buf = buf.Leak(); | |
| 428 mojo::internal::BoundsChecker bounds_checker(data, size, 0); | |
| 429 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); | |
| 430 free(raw_buf); | |
| 431 } | |
| 432 | |
| 433 TEST(UnionTest, PodUnionInArray) { | |
|
viettrungluu
2015/03/04 20:58:13
Probably for consistency, tests of arrays of union
azani
2015/03/05 23:59:25
Done.
| |
| 434 SmallStructPtr small_struct(SmallStruct::New()); | |
| 435 small_struct->pod_union_array = Array<PodUnionPtr>(2); | |
| 436 small_struct->pod_union_array[0] = PodUnion::New(); | |
| 437 small_struct->pod_union_array[1] = PodUnion::New(); | |
| 438 | |
| 439 small_struct->pod_union_array[0]->set_f_int8(10); | |
| 440 small_struct->pod_union_array[1]->set_f_int16(12); | |
| 441 | |
| 442 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8()); | |
| 443 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16()); | |
| 444 } | |
| 445 | |
| 446 TEST(UnionTest, PodUnionInArraySerialization) { | |
| 447 Environment environment; | |
| 448 | |
| 449 Array<PodUnionPtr> array(2); | |
| 450 array[0] = PodUnion::New(); | |
| 451 array[1] = PodUnion::New(); | |
| 452 | |
| 453 array[0]->set_f_int8(10); | |
| 454 array[1]->set_f_int16(12); | |
| 455 EXPECT_EQ(2U, array.size()); | |
| 456 | |
| 457 size_t size = GetSerializedSize_(array); | |
| 458 EXPECT_EQ(40U, size); | |
| 459 | |
| 460 mojo::internal::FixedBuffer buf(size); | |
| 461 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | |
| 462 SerializeArray_<mojo::internal::ArrayValidateParams< | |
| 463 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data); | |
| 464 | |
| 465 Array<PodUnionPtr> array2; | |
| 466 Deserialize_(data, &array2); | |
| 467 | |
| 468 EXPECT_EQ(2U, array2.size()); | |
| 469 | |
| 470 EXPECT_EQ(10, array2[0]->get_f_int8()); | |
| 471 EXPECT_EQ(12, array2[1]->get_f_int16()); | |
| 472 } | |
| 473 | |
| 474 TEST(UnionTest, PodUnionInArraySerializationWithNull) { | |
| 475 Environment environment; | |
| 476 | |
| 477 Array<PodUnionPtr> array(2); | |
| 478 array[0] = PodUnion::New(); | |
| 479 | |
| 480 array[0]->set_f_int8(10); | |
| 481 EXPECT_EQ(2U, array.size()); | |
| 482 | |
| 483 size_t size = GetSerializedSize_(array); | |
| 484 EXPECT_EQ(40U, size); | |
| 485 | |
| 486 mojo::internal::FixedBuffer buf(size); | |
| 487 mojo::internal::Array_Data<internal::PodUnion_Data>* data; | |
| 488 SerializeArray_<mojo::internal::ArrayValidateParams< | |
| 489 0, true, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data); | |
| 490 | |
| 491 Array<PodUnionPtr> array2; | |
| 492 Deserialize_(data, &array2); | |
| 493 | |
| 494 EXPECT_EQ(2U, array2.size()); | |
| 495 | |
| 496 EXPECT_EQ(10, array2[0]->get_f_int8()); | |
| 497 EXPECT_TRUE(array2[1].is_null()); | |
| 498 } | |
| 499 | |
| 283 } // namespace test | 500 } // namespace test |
| 284 } // namespace mojo | 501 } // namespace mojo |
| OLD | NEW |