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

Side by Side Diff: mojo/public/cpp/bindings/tests/union_unittest.cc

Issue 1997473005: Remove requirement that mojo::Environment be instantiated. (Closed) Base URL: https://github.com/domokit/mojo.git@work797_no_utility_tls
Patch Set: rebased Created 4 years, 7 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 unified diff | Download patch
OLDNEW
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 <utility> 5 #include <utility>
6 #include <vector> 6 #include <vector>
7 7
8 #include "mojo/public/cpp/bindings/array.h" 8 #include "mojo/public/cpp/bindings/array.h"
9 #include "mojo/public/cpp/bindings/binding.h" 9 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/lib/array_internal.h" 10 #include "mojo/public/cpp/bindings/lib/array_internal.h"
11 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 11 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
12 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" 12 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
14 #include "mojo/public/cpp/bindings/lib/map_serialization.h" 14 #include "mojo/public/cpp/bindings/lib/map_serialization.h"
15 #include "mojo/public/cpp/bindings/string.h" 15 #include "mojo/public/cpp/bindings/string.h"
16 #include "mojo/public/cpp/environment/environment.h"
17 #include "mojo/public/cpp/test_support/test_utils.h" 16 #include "mojo/public/cpp/test_support/test_utils.h"
18 #include "mojo/public/cpp/utility/run_loop.h" 17 #include "mojo/public/cpp/utility/run_loop.h"
19 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 18 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
20 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" 19 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 21
23 namespace mojo { 22 namespace mojo {
24 namespace test { 23 namespace test {
25 24
26 TEST(UnionTest, PlainOldDataGetterSetter) { 25 TEST(UnionTest, PlainOldDataGetterSetter) {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 220
222 TEST(UnionTest, NullValidation) { 221 TEST(UnionTest, NullValidation) {
223 void* buf = nullptr; 222 void* buf = nullptr;
224 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0); 223 mojo::internal::BoundsChecker bounds_checker(buf, 0, 0);
225 EXPECT_EQ( 224 EXPECT_EQ(
226 mojo::internal::ValidationError::NONE, 225 mojo::internal::ValidationError::NONE,
227 internal::PodUnion_Data::Validate(buf, &bounds_checker, false, nullptr)); 226 internal::PodUnion_Data::Validate(buf, &bounds_checker, false, nullptr));
228 } 227 }
229 228
230 TEST(UnionTest, OutOfAlignmentValidation) { 229 TEST(UnionTest, OutOfAlignmentValidation) {
231 Environment environment;
232 size_t size = sizeof(internal::PodUnion_Data); 230 size_t size = sizeof(internal::PodUnion_Data);
233 // Get an aligned object and shift the alignment. 231 // Get an aligned object and shift the alignment.
234 mojo::internal::FixedBufferForTesting aligned_buf(size + 1); 232 mojo::internal::FixedBufferForTesting aligned_buf(size + 1);
235 void* raw_buf = aligned_buf.Leak(); 233 void* raw_buf = aligned_buf.Leak();
236 char* buf = reinterpret_cast<char*>(raw_buf) + 1; 234 char* buf = reinterpret_cast<char*>(raw_buf) + 1;
237 235
238 internal::PodUnion_Data* data = 236 internal::PodUnion_Data* data =
239 reinterpret_cast<internal::PodUnion_Data*>(buf); 237 reinterpret_cast<internal::PodUnion_Data*>(buf);
240 mojo::internal::BoundsChecker bounds_checker(data, 238 mojo::internal::BoundsChecker bounds_checker(data,
241 static_cast<uint32_t>(size), 0); 239 static_cast<uint32_t>(size), 0);
242 EXPECT_NE( 240 EXPECT_NE(
243 mojo::internal::ValidationError::NONE, 241 mojo::internal::ValidationError::NONE,
244 internal::PodUnion_Data::Validate(buf, &bounds_checker, false, nullptr)); 242 internal::PodUnion_Data::Validate(buf, &bounds_checker, false, nullptr));
245 free(raw_buf); 243 free(raw_buf);
246 } 244 }
247 245
248 TEST(UnionTest, OOBValidation) { 246 TEST(UnionTest, OOBValidation) {
249 Environment environment;
250 size_t size = sizeof(internal::PodUnion_Data) - 1; 247 size_t size = sizeof(internal::PodUnion_Data) - 1;
251 mojo::internal::FixedBufferForTesting buf(size); 248 mojo::internal::FixedBufferForTesting buf(size);
252 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); 249 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
253 mojo::internal::BoundsChecker bounds_checker(data, 250 mojo::internal::BoundsChecker bounds_checker(data,
254 static_cast<uint32_t>(size), 0); 251 static_cast<uint32_t>(size), 0);
255 void* raw_buf = buf.Leak(); 252 void* raw_buf = buf.Leak();
256 EXPECT_NE(mojo::internal::ValidationError::NONE, 253 EXPECT_NE(mojo::internal::ValidationError::NONE,
257 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false, 254 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false,
258 nullptr)); 255 nullptr));
259 free(raw_buf); 256 free(raw_buf);
260 } 257 }
261 258
262 TEST(UnionTest, UnknownTagDeserialization) { 259 TEST(UnionTest, UnknownTagDeserialization) {
263 Environment environment;
264 size_t size = sizeof(internal::PodUnion_Data); 260 size_t size = sizeof(internal::PodUnion_Data);
265 mojo::internal::FixedBufferForTesting buf(size); 261 mojo::internal::FixedBufferForTesting buf(size);
266 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); 262 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
267 data->size = size; 263 data->size = size;
268 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); 264 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
269 265
270 PodUnionPtr pod2 = PodUnion::New(); 266 PodUnionPtr pod2 = PodUnion::New();
271 Deserialize_(data, pod2.get()); 267 Deserialize_(data, pod2.get());
272 268
273 EXPECT_TRUE(pod2->has_unknown_tag()); 269 EXPECT_TRUE(pod2->has_unknown_tag());
274 } 270 }
275 271
276 TEST(UnionTest, UnknownTagValidation) { 272 TEST(UnionTest, UnknownTagValidation) {
277 Environment environment;
278 size_t size = sizeof(internal::PodUnion_Data); 273 size_t size = sizeof(internal::PodUnion_Data);
279 mojo::internal::FixedBufferForTesting buf(size); 274 mojo::internal::FixedBufferForTesting buf(size);
280 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf); 275 internal::PodUnion_Data* data = internal::PodUnion_Data::New(&buf);
281 data->size = size; 276 data->size = size;
282 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); 277 data->tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
283 mojo::internal::BoundsChecker bounds_checker(data, 278 mojo::internal::BoundsChecker bounds_checker(data,
284 static_cast<uint32_t>(size), 0); 279 static_cast<uint32_t>(size), 0);
285 void* raw_buf = buf.Leak(); 280 void* raw_buf = buf.Leak();
286 EXPECT_EQ(mojo::internal::ValidationError::NONE, 281 EXPECT_EQ(mojo::internal::ValidationError::NONE,
287 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false, 282 internal::PodUnion_Data::Validate(raw_buf, &bounds_checker, false,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 data->DecodePointersAndHandles(&handles); 333 data->DecodePointersAndHandles(&handles);
339 334
340 ObjectUnionPtr pod2 = ObjectUnion::New(); 335 ObjectUnionPtr pod2 = ObjectUnion::New();
341 Deserialize_(data, pod2.get()); 336 Deserialize_(data, pod2.get());
342 EXPECT_EQ(hello, pod2->get_f_string()); 337 EXPECT_EQ(hello, pod2->get_f_string());
343 EXPECT_TRUE(pod2->is_f_string()); 338 EXPECT_TRUE(pod2->is_f_string());
344 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING); 339 EXPECT_EQ(pod2->which(), ObjectUnion::Tag::F_STRING);
345 } 340 }
346 341
347 TEST(UnionTest, NullStringValidation) { 342 TEST(UnionTest, NullStringValidation) {
348 Environment environment;
349 size_t size = sizeof(internal::ObjectUnion_Data); 343 size_t size = sizeof(internal::ObjectUnion_Data);
350 mojo::internal::FixedBufferForTesting buf(size); 344 mojo::internal::FixedBufferForTesting buf(size);
351 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 345 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
352 data->size = 16; 346 data->size = 16;
353 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 347 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
354 data->data.unknown = 0x0; 348 data->data.unknown = 0x0;
355 mojo::internal::BoundsChecker bounds_checker(data, 349 mojo::internal::BoundsChecker bounds_checker(data,
356 static_cast<uint32_t>(size), 0); 350 static_cast<uint32_t>(size), 0);
357 void* raw_buf = buf.Leak(); 351 void* raw_buf = buf.Leak();
358 EXPECT_NE(mojo::internal::ValidationError::NONE, 352 EXPECT_NE(mojo::internal::ValidationError::NONE,
359 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 353 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
360 false, nullptr)); 354 false, nullptr));
361 free(raw_buf); 355 free(raw_buf);
362 } 356 }
363 357
364 TEST(UnionTest, StringPointerOverflowValidation) { 358 TEST(UnionTest, StringPointerOverflowValidation) {
365 Environment environment;
366 size_t size = sizeof(internal::ObjectUnion_Data); 359 size_t size = sizeof(internal::ObjectUnion_Data);
367 mojo::internal::FixedBufferForTesting buf(size); 360 mojo::internal::FixedBufferForTesting buf(size);
368 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 361 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
369 data->size = 16; 362 data->size = 16;
370 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 363 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
371 data->data.unknown = 0xFFFFFFFFFFFFFFFF; 364 data->data.unknown = 0xFFFFFFFFFFFFFFFF;
372 mojo::internal::BoundsChecker bounds_checker(data, 365 mojo::internal::BoundsChecker bounds_checker(data,
373 static_cast<uint32_t>(size), 0); 366 static_cast<uint32_t>(size), 0);
374 void* raw_buf = buf.Leak(); 367 void* raw_buf = buf.Leak();
375 EXPECT_NE(mojo::internal::ValidationError::NONE, 368 EXPECT_NE(mojo::internal::ValidationError::NONE,
376 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 369 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
377 false, nullptr)); 370 false, nullptr));
378 free(raw_buf); 371 free(raw_buf);
379 } 372 }
380 373
381 TEST(UnionTest, StringValidateOOB) { 374 TEST(UnionTest, StringValidateOOB) {
382 Environment environment;
383 size_t size = 32; 375 size_t size = 32;
384 mojo::internal::FixedBufferForTesting buf(size); 376 mojo::internal::FixedBufferForTesting buf(size);
385 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf); 377 internal::ObjectUnion_Data* data = internal::ObjectUnion_Data::New(&buf);
386 data->size = 16; 378 data->size = 16;
387 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING; 379 data->tag = internal::ObjectUnion_Data::ObjectUnion_Tag::F_STRING;
388 380
389 data->data.f_f_string.offset = 8; 381 data->data.f_f_string.offset = 8;
390 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string); 382 char* ptr = reinterpret_cast<char*>(&data->data.f_f_string);
391 mojo::internal::ArrayHeader* array_header = 383 mojo::internal::ArrayHeader* array_header =
392 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr); 384 reinterpret_cast<mojo::internal::ArrayHeader*>(ptr + *ptr);
(...skipping 16 matching lines...) Expand all
409 small_struct->pod_union_array[1] = PodUnion::New(); 401 small_struct->pod_union_array[1] = PodUnion::New();
410 402
411 small_struct->pod_union_array[0]->set_f_int8(10); 403 small_struct->pod_union_array[0]->set_f_int8(10);
412 small_struct->pod_union_array[1]->set_f_int16(12); 404 small_struct->pod_union_array[1]->set_f_int16(12);
413 405
414 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8()); 406 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8());
415 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16()); 407 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16());
416 } 408 }
417 409
418 TEST(UnionTest, PodUnionInArraySerialization) { 410 TEST(UnionTest, PodUnionInArraySerialization) {
419 Environment environment;
420 auto array = Array<PodUnionPtr>::New(2); 411 auto array = Array<PodUnionPtr>::New(2);
421 array[0] = PodUnion::New(); 412 array[0] = PodUnion::New();
422 array[1] = PodUnion::New(); 413 array[1] = PodUnion::New();
423 414
424 array[0]->set_f_int8(10); 415 array[0]->set_f_int8(10);
425 array[1]->set_f_int16(12); 416 array[1]->set_f_int16(12);
426 EXPECT_EQ(2U, array.size()); 417 EXPECT_EQ(2U, array.size());
427 418
428 size_t size = GetSerializedSize_(array); 419 size_t size = GetSerializedSize_(array);
429 EXPECT_EQ(40U, size); 420 EXPECT_EQ(40U, size);
430 421
431 mojo::internal::FixedBufferForTesting buf(size); 422 mojo::internal::FixedBufferForTesting buf(size);
432 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr; 423 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr;
433 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); 424 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr);
434 SerializeArray_(&array, &buf, &data, &validate_params); 425 SerializeArray_(&array, &buf, &data, &validate_params);
435 426
436 Array<PodUnionPtr> array2; 427 Array<PodUnionPtr> array2;
437 Deserialize_(data, &array2); 428 Deserialize_(data, &array2);
438 429
439 EXPECT_EQ(2U, array2.size()); 430 EXPECT_EQ(2U, array2.size());
440 431
441 EXPECT_EQ(10, array2[0]->get_f_int8()); 432 EXPECT_EQ(10, array2[0]->get_f_int8());
442 EXPECT_EQ(12, array2[1]->get_f_int16()); 433 EXPECT_EQ(12, array2[1]->get_f_int16());
443 } 434 }
444 435
445 TEST(UnionTest, PodUnionInArrayValidation) { 436 TEST(UnionTest, PodUnionInArrayValidation) {
446 Environment environment;
447 auto array = Array<PodUnionPtr>::New(2); 437 auto array = Array<PodUnionPtr>::New(2);
448 array[0] = PodUnion::New(); 438 array[0] = PodUnion::New();
449 array[1] = PodUnion::New(); 439 array[1] = PodUnion::New();
450 440
451 array[0]->set_f_int8(10); 441 array[0]->set_f_int8(10);
452 array[1]->set_f_int16(12); 442 array[1]->set_f_int16(12);
453 443
454 size_t size = GetSerializedSize_(array); 444 size_t size = GetSerializedSize_(array);
455 445
456 mojo::internal::FixedBufferForTesting buf(size); 446 mojo::internal::FixedBufferForTesting buf(size);
457 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr; 447 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr;
458 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); 448 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr);
459 SerializeArray_(&array, &buf, &data, &validate_params); 449 SerializeArray_(&array, &buf, &data, &validate_params);
460 450
461 std::vector<Handle> handles; 451 std::vector<Handle> handles;
462 data->EncodePointersAndHandles(&handles); 452 data->EncodePointersAndHandles(&handles);
463 EXPECT_TRUE(handles.empty()); 453 EXPECT_TRUE(handles.empty());
464 454
465 void* raw_buf = buf.Leak(); 455 void* raw_buf = buf.Leak();
466 mojo::internal::BoundsChecker bounds_checker(data, 456 mojo::internal::BoundsChecker bounds_checker(data,
467 static_cast<uint32_t>(size), 1); 457 static_cast<uint32_t>(size), 1);
468 458
469 EXPECT_EQ(mojo::internal::ValidationError::NONE, 459 EXPECT_EQ(mojo::internal::ValidationError::NONE,
470 Array<PodUnionPtr>::Data_::Validate(raw_buf, &bounds_checker, 460 Array<PodUnionPtr>::Data_::Validate(raw_buf, &bounds_checker,
471 &validate_params, nullptr)); 461 &validate_params, nullptr));
472 free(raw_buf); 462 free(raw_buf);
473 } 463 }
474 TEST(UnionTest, PodUnionInArraySerializationWithNull) { 464 TEST(UnionTest, PodUnionInArraySerializationWithNull) {
475 Environment environment;
476 auto array = Array<PodUnionPtr>::New(2); 465 auto array = Array<PodUnionPtr>::New(2);
477 array[0] = PodUnion::New(); 466 array[0] = PodUnion::New();
478 467
479 array[0]->set_f_int8(10); 468 array[0]->set_f_int8(10);
480 EXPECT_EQ(2U, array.size()); 469 EXPECT_EQ(2U, array.size());
481 470
482 size_t size = GetSerializedSize_(array); 471 size_t size = GetSerializedSize_(array);
483 EXPECT_EQ(40U, size); 472 EXPECT_EQ(40U, size);
484 473
485 mojo::internal::FixedBufferForTesting buf(size); 474 mojo::internal::FixedBufferForTesting buf(size);
486 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr; 475 mojo::internal::Array_Data<internal::PodUnion_Data>* data = nullptr;
487 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); 476 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr);
488 SerializeArray_(&array, &buf, &data, &validate_params); 477 SerializeArray_(&array, &buf, &data, &validate_params);
489 478
490 Array<PodUnionPtr> array2; 479 Array<PodUnionPtr> array2;
491 Deserialize_(data, &array2); 480 Deserialize_(data, &array2);
492 481
493 EXPECT_EQ(2U, array2.size()); 482 EXPECT_EQ(2U, array2.size());
494 483
495 EXPECT_EQ(10, array2[0]->get_f_int8()); 484 EXPECT_EQ(10, array2[0]->get_f_int8());
496 EXPECT_TRUE(array2[1].is_null()); 485 EXPECT_TRUE(array2[1].is_null());
497 } 486 }
498 487
499 // TODO(azani): Move back in struct_unittest.cc when possible. 488 // TODO(azani): Move back in struct_unittest.cc when possible.
500 // Struct tests 489 // Struct tests
501 TEST(UnionTest, Clone_Union) { 490 TEST(UnionTest, Clone_Union) {
502 Environment environment;
503 SmallStructPtr small_struct(SmallStruct::New()); 491 SmallStructPtr small_struct(SmallStruct::New());
504 small_struct->pod_union = PodUnion::New(); 492 small_struct->pod_union = PodUnion::New();
505 small_struct->pod_union->set_f_int8(10); 493 small_struct->pod_union->set_f_int8(10);
506 494
507 SmallStructPtr clone = small_struct.Clone(); 495 SmallStructPtr clone = small_struct.Clone();
508 EXPECT_EQ(10, clone->pod_union->get_f_int8()); 496 EXPECT_EQ(10, clone->pod_union->get_f_int8());
509 } 497 }
510 498
511 // Serialization test of a struct with a union of plain old data. 499 // Serialization test of a struct with a union of plain old data.
512 TEST(UnionTest, Serialization_UnionOfPods) { 500 TEST(UnionTest, Serialization_UnionOfPods) {
513 Environment environment;
514 SmallStructPtr small_struct(SmallStruct::New()); 501 SmallStructPtr small_struct(SmallStruct::New());
515 small_struct->pod_union = PodUnion::New(); 502 small_struct->pod_union = PodUnion::New();
516 small_struct->pod_union->set_f_int32(10); 503 small_struct->pod_union->set_f_int32(10);
517 504
518 size_t size = GetSerializedSize_(*small_struct); 505 size_t size = GetSerializedSize_(*small_struct);
519 506
520 mojo::internal::FixedBufferForTesting buf(size); 507 mojo::internal::FixedBufferForTesting buf(size);
521 internal::SmallStruct_Data* data = nullptr; 508 internal::SmallStruct_Data* data = nullptr;
522 EXPECT_EQ(mojo::internal::ValidationError::NONE, 509 EXPECT_EQ(mojo::internal::ValidationError::NONE,
523 Serialize_(small_struct.get(), &buf, &data)); 510 Serialize_(small_struct.get(), &buf, &data));
524 511
525 SmallStructPtr deserialized(SmallStruct::New()); 512 SmallStructPtr deserialized(SmallStruct::New());
526 Deserialize_(data, deserialized.get()); 513 Deserialize_(data, deserialized.get());
527 514
528 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); 515 EXPECT_EQ(10, deserialized->pod_union->get_f_int32());
529 } 516 }
530 517
531 // Serialization test of a struct with a union of structs. 518 // Serialization test of a struct with a union of structs.
532 TEST(UnionTest, Serialization_UnionOfObjects) { 519 TEST(UnionTest, Serialization_UnionOfObjects) {
533 Environment environment;
534 SmallObjStructPtr obj_struct(SmallObjStruct::New()); 520 SmallObjStructPtr obj_struct(SmallObjStruct::New());
535 obj_struct->obj_union = ObjectUnion::New(); 521 obj_struct->obj_union = ObjectUnion::New();
536 String hello("hello world"); 522 String hello("hello world");
537 obj_struct->obj_union->set_f_string(hello); 523 obj_struct->obj_union->set_f_string(hello);
538 524
539 size_t size = GetSerializedSize_(*obj_struct); 525 size_t size = GetSerializedSize_(*obj_struct);
540 526
541 mojo::internal::FixedBufferForTesting buf(size); 527 mojo::internal::FixedBufferForTesting buf(size);
542 internal::SmallObjStruct_Data* data = nullptr; 528 internal::SmallObjStruct_Data* data = nullptr;
543 EXPECT_EQ(mojo::internal::ValidationError::NONE, 529 EXPECT_EQ(mojo::internal::ValidationError::NONE,
544 Serialize_(obj_struct.get(), &buf, &data)); 530 Serialize_(obj_struct.get(), &buf, &data));
545 531
546 std::vector<Handle> handles; 532 std::vector<Handle> handles;
547 data->EncodePointersAndHandles(&handles); 533 data->EncodePointersAndHandles(&handles);
548 data->DecodePointersAndHandles(&handles); 534 data->DecodePointersAndHandles(&handles);
549 535
550 SmallObjStructPtr deserialized(SmallObjStruct::New()); 536 SmallObjStructPtr deserialized(SmallObjStruct::New());
551 Deserialize_(data, deserialized.get()); 537 Deserialize_(data, deserialized.get());
552 538
553 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); 539 EXPECT_EQ(hello, deserialized->obj_union->get_f_string());
554 } 540 }
555 541
556 // Validation test of a struct with a union. 542 // Validation test of a struct with a union.
557 TEST(UnionTest, Validation_UnionsInStruct) { 543 TEST(UnionTest, Validation_UnionsInStruct) {
558 Environment environment;
559 SmallStructPtr small_struct(SmallStruct::New()); 544 SmallStructPtr small_struct(SmallStruct::New());
560 small_struct->pod_union = PodUnion::New(); 545 small_struct->pod_union = PodUnion::New();
561 small_struct->pod_union->set_f_int32(10); 546 small_struct->pod_union->set_f_int32(10);
562 547
563 size_t size = GetSerializedSize_(*small_struct); 548 size_t size = GetSerializedSize_(*small_struct);
564 549
565 mojo::internal::FixedBufferForTesting buf(size); 550 mojo::internal::FixedBufferForTesting buf(size);
566 internal::SmallStruct_Data* data = nullptr; 551 internal::SmallStruct_Data* data = nullptr;
567 EXPECT_EQ(mojo::internal::ValidationError::NONE, 552 EXPECT_EQ(mojo::internal::ValidationError::NONE,
568 Serialize_(small_struct.get(), &buf, &data)); 553 Serialize_(small_struct.get(), &buf, &data));
569 554
570 std::vector<Handle> handles; 555 std::vector<Handle> handles;
571 data->EncodePointersAndHandles(&handles); 556 data->EncodePointersAndHandles(&handles);
572 EXPECT_TRUE(handles.empty()); 557 EXPECT_TRUE(handles.empty());
573 558
574 void* raw_buf = buf.Leak(); 559 void* raw_buf = buf.Leak();
575 mojo::internal::BoundsChecker bounds_checker(data, 560 mojo::internal::BoundsChecker bounds_checker(data,
576 static_cast<uint32_t>(size), 0); 561 static_cast<uint32_t>(size), 0);
577 EXPECT_EQ( 562 EXPECT_EQ(
578 mojo::internal::ValidationError::NONE, 563 mojo::internal::ValidationError::NONE,
579 internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker, nullptr)); 564 internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker, nullptr));
580 free(raw_buf); 565 free(raw_buf);
581 } 566 }
582 567
583 // Validation test of a struct union fails due to unknown union tag. 568 // Validation test of a struct union fails due to unknown union tag.
584 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { 569 TEST(UnionTest, Validation_PodUnionInStruct_Failure) {
585 Environment environment;
586 SmallStructPtr small_struct(SmallStruct::New()); 570 SmallStructPtr small_struct(SmallStruct::New());
587 small_struct->pod_union = PodUnion::New(); 571 small_struct->pod_union = PodUnion::New();
588 small_struct->pod_union->set_f_int32(10); 572 small_struct->pod_union->set_f_int32(10);
589 573
590 size_t size = GetSerializedSize_(*small_struct); 574 size_t size = GetSerializedSize_(*small_struct);
591 575
592 mojo::internal::FixedBufferForTesting buf(size); 576 mojo::internal::FixedBufferForTesting buf(size);
593 internal::SmallStruct_Data* data = nullptr; 577 internal::SmallStruct_Data* data = nullptr;
594 EXPECT_EQ(mojo::internal::ValidationError::NONE, 578 EXPECT_EQ(mojo::internal::ValidationError::NONE,
595 Serialize_(small_struct.get(), &buf, &data)); 579 Serialize_(small_struct.get(), &buf, &data));
596 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); 580 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
597 581
598 std::vector<Handle> handles; 582 std::vector<Handle> handles;
599 data->EncodePointersAndHandles(&handles); 583 data->EncodePointersAndHandles(&handles);
600 EXPECT_TRUE(handles.empty()); 584 EXPECT_TRUE(handles.empty());
601 585
602 void* raw_buf = buf.Leak(); 586 void* raw_buf = buf.Leak();
603 mojo::internal::BoundsChecker bounds_checker(data, 587 mojo::internal::BoundsChecker bounds_checker(data,
604 static_cast<uint32_t>(size), 0); 588 static_cast<uint32_t>(size), 0);
605 EXPECT_EQ( 589 EXPECT_EQ(
606 mojo::internal::ValidationError::NONE, 590 mojo::internal::ValidationError::NONE,
607 internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker, nullptr)); 591 internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker, nullptr));
608 free(raw_buf); 592 free(raw_buf);
609 } 593 }
610 594
611 // Validation fails due to non-nullable null union in struct. 595 // Validation fails due to non-nullable null union in struct.
612 TEST(UnionTest, Validation_NullUnion_Failure) { 596 TEST(UnionTest, Validation_NullUnion_Failure) {
613 Environment environment;
614 SmallStructNonNullableUnionPtr small_struct( 597 SmallStructNonNullableUnionPtr small_struct(
615 SmallStructNonNullableUnion::New()); 598 SmallStructNonNullableUnion::New());
616 599
617 size_t size = GetSerializedSize_(*small_struct); 600 size_t size = GetSerializedSize_(*small_struct);
618 601
619 mojo::internal::FixedBufferForTesting buf(size); 602 mojo::internal::FixedBufferForTesting buf(size);
620 internal::SmallStructNonNullableUnion_Data* data = 603 internal::SmallStructNonNullableUnion_Data* data =
621 internal::SmallStructNonNullableUnion_Data::New(&buf); 604 internal::SmallStructNonNullableUnion_Data::New(&buf);
622 605
623 void* raw_buf = buf.Leak(); 606 void* raw_buf = buf.Leak();
624 mojo::internal::BoundsChecker bounds_checker(data, 607 mojo::internal::BoundsChecker bounds_checker(data,
625 static_cast<uint32_t>(size), 0); 608 static_cast<uint32_t>(size), 0);
626 EXPECT_NE(mojo::internal::ValidationError::NONE, 609 EXPECT_NE(mojo::internal::ValidationError::NONE,
627 internal::SmallStructNonNullableUnion_Data::Validate( 610 internal::SmallStructNonNullableUnion_Data::Validate(
628 raw_buf, &bounds_checker, nullptr)); 611 raw_buf, &bounds_checker, nullptr));
629 free(raw_buf); 612 free(raw_buf);
630 } 613 }
631 614
632 // Validation passes with nullable null union. 615 // Validation passes with nullable null union.
633 TEST(UnionTest, Validation_NullableUnion) { 616 TEST(UnionTest, Validation_NullableUnion) {
634 Environment environment;
635 SmallStructPtr small_struct(SmallStruct::New()); 617 SmallStructPtr small_struct(SmallStruct::New());
636 618
637 size_t size = GetSerializedSize_(*small_struct); 619 size_t size = GetSerializedSize_(*small_struct);
638 620
639 mojo::internal::FixedBufferForTesting buf(size); 621 mojo::internal::FixedBufferForTesting buf(size);
640 internal::SmallStruct_Data* data = nullptr; 622 internal::SmallStruct_Data* data = nullptr;
641 EXPECT_EQ(mojo::internal::ValidationError::NONE, 623 EXPECT_EQ(mojo::internal::ValidationError::NONE,
642 Serialize_(small_struct.get(), &buf, &data)); 624 Serialize_(small_struct.get(), &buf, &data));
643 625
644 std::vector<Handle> handles; 626 std::vector<Handle> handles;
(...skipping 17 matching lines...) Expand all
662 small_struct->Serialize(buf, sizeof(buf)); 644 small_struct->Serialize(buf, sizeof(buf));
663 EXPECT_TRUE(small_struct->pod_union.is_null()); 645 EXPECT_TRUE(small_struct->pod_union.is_null());
664 646
665 SmallStructPtr deserialized_struct = SmallStruct::New(); 647 SmallStructPtr deserialized_struct = SmallStruct::New();
666 EXPECT_TRUE(deserialized_struct->Deserialize(buf, sizeof(buf))); 648 EXPECT_TRUE(deserialized_struct->Deserialize(buf, sizeof(buf)));
667 EXPECT_TRUE(deserialized_struct->pod_union.is_null()); 649 EXPECT_TRUE(deserialized_struct->pod_union.is_null());
668 } 650 }
669 651
670 // Validation passes with nullable null union containing non-nullable objects. 652 // Validation passes with nullable null union containing non-nullable objects.
671 TEST(UnionTest, Validation_NullableObjectUnion) { 653 TEST(UnionTest, Validation_NullableObjectUnion) {
672 Environment environment;
673 StructNullObjectUnionPtr small_struct(StructNullObjectUnion::New()); 654 StructNullObjectUnionPtr small_struct(StructNullObjectUnion::New());
674 655
675 size_t size = GetSerializedSize_(*small_struct); 656 size_t size = GetSerializedSize_(*small_struct);
676 657
677 mojo::internal::FixedBufferForTesting buf(size); 658 mojo::internal::FixedBufferForTesting buf(size);
678 internal::StructNullObjectUnion_Data* data = nullptr; 659 internal::StructNullObjectUnion_Data* data = nullptr;
679 EXPECT_EQ(mojo::internal::ValidationError::NONE, 660 EXPECT_EQ(mojo::internal::ValidationError::NONE,
680 Serialize_(small_struct.get(), &buf, &data)); 661 Serialize_(small_struct.get(), &buf, &data));
681 662
682 std::vector<Handle> handles; 663 std::vector<Handle> handles;
(...skipping 18 matching lines...) Expand all
701 small_struct->pod_union_map.insert("two", PodUnion::New()); 682 small_struct->pod_union_map.insert("two", PodUnion::New());
702 683
703 small_struct->pod_union_map["one"]->set_f_int8(8); 684 small_struct->pod_union_map["one"]->set_f_int8(8);
704 small_struct->pod_union_map["two"]->set_f_int16(16); 685 small_struct->pod_union_map["two"]->set_f_int16(16);
705 686
706 EXPECT_EQ(8, small_struct->pod_union_map["one"]->get_f_int8()); 687 EXPECT_EQ(8, small_struct->pod_union_map["one"]->get_f_int8());
707 EXPECT_EQ(16, small_struct->pod_union_map["two"]->get_f_int16()); 688 EXPECT_EQ(16, small_struct->pod_union_map["two"]->get_f_int16());
708 } 689 }
709 690
710 TEST(UnionTest, PodUnionInMapSerialization) { 691 TEST(UnionTest, PodUnionInMapSerialization) {
711 Environment environment;
712 Map<String, PodUnionPtr> map; 692 Map<String, PodUnionPtr> map;
713 map.insert("one", PodUnion::New()); 693 map.insert("one", PodUnion::New());
714 map.insert("two", PodUnion::New()); 694 map.insert("two", PodUnion::New());
715 695
716 map["one"]->set_f_int8(8); 696 map["one"]->set_f_int8(8);
717 map["two"]->set_f_int16(16); 697 map["two"]->set_f_int16(16);
718 698
719 size_t size = GetSerializedSize_(map); 699 size_t size = GetSerializedSize_(map);
720 EXPECT_EQ(120U, size); 700 EXPECT_EQ(120U, size);
721 701
722 mojo::internal::FixedBufferForTesting buf(size); 702 mojo::internal::FixedBufferForTesting buf(size);
723 mojo::internal::Map_Data<mojo::internal::String_Data*, 703 mojo::internal::Map_Data<mojo::internal::String_Data*,
724 internal::PodUnion_Data>* data = nullptr; 704 internal::PodUnion_Data>* data = nullptr;
725 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); 705 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr);
726 EXPECT_EQ(mojo::internal::ValidationError::NONE, 706 EXPECT_EQ(mojo::internal::ValidationError::NONE,
727 SerializeMap_(&map, &buf, &data, &validate_params)); 707 SerializeMap_(&map, &buf, &data, &validate_params));
728 708
729 Map<String, PodUnionPtr> map2; 709 Map<String, PodUnionPtr> map2;
730 Deserialize_(data, &map2); 710 Deserialize_(data, &map2);
731 711
732 EXPECT_EQ(8, map2["one"]->get_f_int8()); 712 EXPECT_EQ(8, map2["one"]->get_f_int8());
733 EXPECT_EQ(16, map2["two"]->get_f_int16()); 713 EXPECT_EQ(16, map2["two"]->get_f_int16());
734 } 714 }
735 715
736 TEST(UnionTest, PodUnionInMapSerializationWithNull) { 716 TEST(UnionTest, PodUnionInMapSerializationWithNull) {
737 Environment environment;
738 Map<String, PodUnionPtr> map; 717 Map<String, PodUnionPtr> map;
739 map.insert("one", PodUnion::New()); 718 map.insert("one", PodUnion::New());
740 map.insert("two", nullptr); 719 map.insert("two", nullptr);
741 720
742 map["one"]->set_f_int8(8); 721 map["one"]->set_f_int8(8);
743 722
744 size_t size = GetSerializedSize_(map); 723 size_t size = GetSerializedSize_(map);
745 EXPECT_EQ(120U, size); 724 EXPECT_EQ(120U, size);
746 725
747 mojo::internal::FixedBufferForTesting buf(size); 726 mojo::internal::FixedBufferForTesting buf(size);
(...skipping 14 matching lines...) Expand all
762 DummyStructPtr dummy(DummyStruct::New()); 741 DummyStructPtr dummy(DummyStruct::New());
763 dummy->f_int8 = 8; 742 dummy->f_int8 = 8;
764 743
765 ObjectUnionPtr obj(ObjectUnion::New()); 744 ObjectUnionPtr obj(ObjectUnion::New());
766 obj->set_f_dummy(dummy.Pass()); 745 obj->set_f_dummy(dummy.Pass());
767 746
768 EXPECT_EQ(8, obj->get_f_dummy()->f_int8); 747 EXPECT_EQ(8, obj->get_f_dummy()->f_int8);
769 } 748 }
770 749
771 TEST(UnionTest, StructInUnionSerialization) { 750 TEST(UnionTest, StructInUnionSerialization) {
772 Environment environment;
773 DummyStructPtr dummy(DummyStruct::New()); 751 DummyStructPtr dummy(DummyStruct::New());
774 dummy->f_int8 = 8; 752 dummy->f_int8 = 8;
775 753
776 ObjectUnionPtr obj(ObjectUnion::New()); 754 ObjectUnionPtr obj(ObjectUnion::New());
777 obj->set_f_dummy(dummy.Pass()); 755 obj->set_f_dummy(dummy.Pass());
778 756
779 size_t size = GetSerializedSize_(obj); 757 size_t size = GetSerializedSize_(obj);
780 EXPECT_EQ(32U, size); 758 EXPECT_EQ(32U, size);
781 759
782 mojo::internal::FixedBufferForTesting buf(size); 760 mojo::internal::FixedBufferForTesting buf(size);
783 auto* data = internal::ObjectUnion_Data::New(&buf); 761 auto* data = internal::ObjectUnion_Data::New(&buf);
784 SerializeUnion_(obj.get(), &buf, &data); 762 SerializeUnion_(obj.get(), &buf, &data);
785 763
786 std::vector<Handle> handles; 764 std::vector<Handle> handles;
787 data->EncodePointersAndHandles(&handles); 765 data->EncodePointersAndHandles(&handles);
788 data->DecodePointersAndHandles(&handles); 766 data->DecodePointersAndHandles(&handles);
789 767
790 ObjectUnionPtr obj2 = ObjectUnion::New(); 768 ObjectUnionPtr obj2 = ObjectUnion::New();
791 Deserialize_(data, obj2.get()); 769 Deserialize_(data, obj2.get());
792 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8); 770 EXPECT_EQ(8, obj2->get_f_dummy()->f_int8);
793 } 771 }
794 772
795 TEST(UnionTest, StructInUnionValidation) { 773 TEST(UnionTest, StructInUnionValidation) {
796 Environment environment;
797 DummyStructPtr dummy(DummyStruct::New()); 774 DummyStructPtr dummy(DummyStruct::New());
798 dummy->f_int8 = 8; 775 dummy->f_int8 = 8;
799 776
800 ObjectUnionPtr obj(ObjectUnion::New()); 777 ObjectUnionPtr obj(ObjectUnion::New());
801 obj->set_f_dummy(dummy.Pass()); 778 obj->set_f_dummy(dummy.Pass());
802 779
803 size_t size = GetSerializedSize_(obj); 780 size_t size = GetSerializedSize_(obj);
804 781
805 mojo::internal::FixedBufferForTesting buf(size); 782 mojo::internal::FixedBufferForTesting buf(size);
806 auto* data = internal::ObjectUnion_Data::New(&buf); 783 auto* data = internal::ObjectUnion_Data::New(&buf);
807 SerializeUnion_(obj.get(), &buf, &data); 784 SerializeUnion_(obj.get(), &buf, &data);
808 785
809 std::vector<Handle> handles; 786 std::vector<Handle> handles;
810 data->EncodePointersAndHandles(&handles); 787 data->EncodePointersAndHandles(&handles);
811 EXPECT_TRUE(handles.empty()); 788 EXPECT_TRUE(handles.empty());
812 789
813 void* raw_buf = buf.Leak(); 790 void* raw_buf = buf.Leak();
814 mojo::internal::BoundsChecker bounds_checker(data, 791 mojo::internal::BoundsChecker bounds_checker(data,
815 static_cast<uint32_t>(size), 0); 792 static_cast<uint32_t>(size), 0);
816 EXPECT_EQ(mojo::internal::ValidationError::NONE, 793 EXPECT_EQ(mojo::internal::ValidationError::NONE,
817 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 794 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
818 false, nullptr)); 795 false, nullptr));
819 free(raw_buf); 796 free(raw_buf);
820 } 797 }
821 798
822 TEST(UnionTest, StructInUnionValidationNonNullable) { 799 TEST(UnionTest, StructInUnionValidationNonNullable) {
823 Environment environment;
824 DummyStructPtr dummy(nullptr); 800 DummyStructPtr dummy(nullptr);
825 801
826 ObjectUnionPtr obj(ObjectUnion::New()); 802 ObjectUnionPtr obj(ObjectUnion::New());
827 obj->set_f_dummy(dummy.Pass()); 803 obj->set_f_dummy(dummy.Pass());
828 804
829 size_t size = GetSerializedSize_(obj); 805 size_t size = GetSerializedSize_(obj);
830 806
831 mojo::internal::FixedBufferForTesting buf(size); 807 mojo::internal::FixedBufferForTesting buf(size);
832 auto* data = internal::ObjectUnion_Data::New(&buf); 808 auto* data = internal::ObjectUnion_Data::New(&buf);
833 SerializeUnion_(obj.get(), &buf, &data); 809 SerializeUnion_(obj.get(), &buf, &data);
834 810
835 std::vector<Handle> handles; 811 std::vector<Handle> handles;
836 data->EncodePointersAndHandles(&handles); 812 data->EncodePointersAndHandles(&handles);
837 EXPECT_TRUE(handles.empty()); 813 EXPECT_TRUE(handles.empty());
838 814
839 void* raw_buf = buf.Leak(); 815 void* raw_buf = buf.Leak();
840 mojo::internal::BoundsChecker bounds_checker(data, 816 mojo::internal::BoundsChecker bounds_checker(data,
841 static_cast<uint32_t>(size), 0); 817 static_cast<uint32_t>(size), 0);
842 EXPECT_NE(mojo::internal::ValidationError::NONE, 818 EXPECT_NE(mojo::internal::ValidationError::NONE,
843 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 819 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
844 false, nullptr)); 820 false, nullptr));
845 free(raw_buf); 821 free(raw_buf);
846 } 822 }
847 823
848 TEST(UnionTest, StructInUnionValidationNullable) { 824 TEST(UnionTest, StructInUnionValidationNullable) {
849 Environment environment;
850 DummyStructPtr dummy(nullptr); 825 DummyStructPtr dummy(nullptr);
851 826
852 ObjectUnionPtr obj(ObjectUnion::New()); 827 ObjectUnionPtr obj(ObjectUnion::New());
853 obj->set_f_nullable(dummy.Pass()); 828 obj->set_f_nullable(dummy.Pass());
854 829
855 size_t size = GetSerializedSize_(obj); 830 size_t size = GetSerializedSize_(obj);
856 831
857 mojo::internal::FixedBufferForTesting buf(size); 832 mojo::internal::FixedBufferForTesting buf(size);
858 auto* data = internal::ObjectUnion_Data::New(&buf); 833 auto* data = internal::ObjectUnion_Data::New(&buf);
859 SerializeUnion_(obj.get(), &buf, &data); 834 SerializeUnion_(obj.get(), &buf, &data);
860 835
861 std::vector<Handle> handles; 836 std::vector<Handle> handles;
862 data->EncodePointersAndHandles(&handles); 837 data->EncodePointersAndHandles(&handles);
863 EXPECT_TRUE(handles.empty()); 838 EXPECT_TRUE(handles.empty());
864 839
865 void* raw_buf = buf.Leak(); 840 void* raw_buf = buf.Leak();
866 mojo::internal::BoundsChecker bounds_checker(data, 841 mojo::internal::BoundsChecker bounds_checker(data,
867 static_cast<uint32_t>(size), 0); 842 static_cast<uint32_t>(size), 0);
868 EXPECT_EQ(mojo::internal::ValidationError::NONE, 843 EXPECT_EQ(mojo::internal::ValidationError::NONE,
869 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 844 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
870 false, nullptr)); 845 false, nullptr));
871 free(raw_buf); 846 free(raw_buf);
872 } 847 }
873 848
874 TEST(UnionTest, ArrayInUnionGetterSetter) { 849 TEST(UnionTest, ArrayInUnionGetterSetter) {
875 Environment environment;
876
877 auto array = Array<int8_t>::New(2); 850 auto array = Array<int8_t>::New(2);
878 array[0] = 8; 851 array[0] = 8;
879 array[1] = 9; 852 array[1] = 9;
880 853
881 ObjectUnionPtr obj(ObjectUnion::New()); 854 ObjectUnionPtr obj(ObjectUnion::New());
882 obj->set_f_array_int8(array.Pass()); 855 obj->set_f_array_int8(array.Pass());
883 856
884 EXPECT_EQ(8, obj->get_f_array_int8()[0]); 857 EXPECT_EQ(8, obj->get_f_array_int8()[0]);
885 EXPECT_EQ(9, obj->get_f_array_int8()[1]); 858 EXPECT_EQ(9, obj->get_f_array_int8()[1]);
886 } 859 }
887 860
888 TEST(UnionTest, ArrayInUnionSerialization) { 861 TEST(UnionTest, ArrayInUnionSerialization) {
889 Environment environment;
890
891 auto array = Array<int8_t>::New(2); 862 auto array = Array<int8_t>::New(2);
892 array[0] = 8; 863 array[0] = 8;
893 array[1] = 9; 864 array[1] = 9;
894 865
895 ObjectUnionPtr obj(ObjectUnion::New()); 866 ObjectUnionPtr obj(ObjectUnion::New());
896 obj->set_f_array_int8(array.Pass()); 867 obj->set_f_array_int8(array.Pass());
897 868
898 size_t size = GetSerializedSize_(obj); 869 size_t size = GetSerializedSize_(obj);
899 EXPECT_EQ(32U, size); 870 EXPECT_EQ(32U, size);
900 871
901 mojo::internal::FixedBufferForTesting buf(size); 872 mojo::internal::FixedBufferForTesting buf(size);
902 auto* data = internal::ObjectUnion_Data::New(&buf); 873 auto* data = internal::ObjectUnion_Data::New(&buf);
903 SerializeUnion_(obj.get(), &buf, &data); 874 SerializeUnion_(obj.get(), &buf, &data);
904 875
905 std::vector<Handle> handles; 876 std::vector<Handle> handles;
906 data->EncodePointersAndHandles(&handles); 877 data->EncodePointersAndHandles(&handles);
907 data->DecodePointersAndHandles(&handles); 878 data->DecodePointersAndHandles(&handles);
908 879
909 ObjectUnionPtr obj2 = ObjectUnion::New(); 880 ObjectUnionPtr obj2 = ObjectUnion::New();
910 Deserialize_(data, obj2.get()); 881 Deserialize_(data, obj2.get());
911 882
912 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); 883 EXPECT_EQ(8, obj2->get_f_array_int8()[0]);
913 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); 884 EXPECT_EQ(9, obj2->get_f_array_int8()[1]);
914 } 885 }
915 886
916 TEST(UnionTest, ArrayInUnionValidation) { 887 TEST(UnionTest, ArrayInUnionValidation) {
917 Environment environment;
918
919 auto array = Array<int8_t>::New(2); 888 auto array = Array<int8_t>::New(2);
920 array[0] = 8; 889 array[0] = 8;
921 array[1] = 9; 890 array[1] = 9;
922 891
923 ObjectUnionPtr obj(ObjectUnion::New()); 892 ObjectUnionPtr obj(ObjectUnion::New());
924 obj->set_f_array_int8(array.Pass()); 893 obj->set_f_array_int8(array.Pass());
925 894
926 size_t size = GetSerializedSize_(obj); 895 size_t size = GetSerializedSize_(obj);
927 mojo::internal::FixedBufferForTesting buf(size); 896 mojo::internal::FixedBufferForTesting buf(size);
928 auto* data = internal::ObjectUnion_Data::New(&buf); 897 auto* data = internal::ObjectUnion_Data::New(&buf);
929 SerializeUnion_(obj.get(), &buf, &data); 898 SerializeUnion_(obj.get(), &buf, &data);
930 899
931 std::vector<Handle> handles; 900 std::vector<Handle> handles;
932 data->EncodePointersAndHandles(&handles); 901 data->EncodePointersAndHandles(&handles);
933 EXPECT_TRUE(handles.empty()); 902 EXPECT_TRUE(handles.empty());
934 903
935 void* raw_buf = buf.Leak(); 904 void* raw_buf = buf.Leak();
936 mojo::internal::BoundsChecker bounds_checker(data, 905 mojo::internal::BoundsChecker bounds_checker(data,
937 static_cast<uint32_t>(size), 0); 906 static_cast<uint32_t>(size), 0);
938 907
939 EXPECT_EQ(mojo::internal::ValidationError::NONE, 908 EXPECT_EQ(mojo::internal::ValidationError::NONE,
940 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 909 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
941 false, nullptr)); 910 false, nullptr));
942 free(raw_buf); 911 free(raw_buf);
943 } 912 }
944 913
945 TEST(UnionTest, MapInUnionGetterSetter) { 914 TEST(UnionTest, MapInUnionGetterSetter) {
946 Environment environment;
947 Map<String, int8_t> map; 915 Map<String, int8_t> map;
948 map.insert("one", 1); 916 map.insert("one", 1);
949 map.insert("two", 2); 917 map.insert("two", 2);
950 918
951 ObjectUnionPtr obj(ObjectUnion::New()); 919 ObjectUnionPtr obj(ObjectUnion::New());
952 obj->set_f_map_int8(map.Pass()); 920 obj->set_f_map_int8(map.Pass());
953 921
954 EXPECT_EQ(1, obj->get_f_map_int8()["one"]); 922 EXPECT_EQ(1, obj->get_f_map_int8()["one"]);
955 EXPECT_EQ(2, obj->get_f_map_int8()["two"]); 923 EXPECT_EQ(2, obj->get_f_map_int8()["two"]);
956 } 924 }
957 925
958 TEST(UnionTest, MapInUnionSerialization) { 926 TEST(UnionTest, MapInUnionSerialization) {
959 Environment environment;
960 Map<String, int8_t> map; 927 Map<String, int8_t> map;
961 map.insert("one", 1); 928 map.insert("one", 1);
962 map.insert("two", 2); 929 map.insert("two", 2);
963 930
964 ObjectUnionPtr obj(ObjectUnion::New()); 931 ObjectUnionPtr obj(ObjectUnion::New());
965 obj->set_f_map_int8(map.Pass()); 932 obj->set_f_map_int8(map.Pass());
966 933
967 size_t size = GetSerializedSize_(obj); 934 size_t size = GetSerializedSize_(obj);
968 EXPECT_EQ(112U, size); 935 EXPECT_EQ(112U, size);
969 936
970 mojo::internal::FixedBufferForTesting buf(size); 937 mojo::internal::FixedBufferForTesting buf(size);
971 auto* data = internal::ObjectUnion_Data::New(&buf); 938 auto* data = internal::ObjectUnion_Data::New(&buf);
972 SerializeUnion_(obj.get(), &buf, &data); 939 SerializeUnion_(obj.get(), &buf, &data);
973 940
974 std::vector<Handle> handles; 941 std::vector<Handle> handles;
975 data->EncodePointersAndHandles(&handles); 942 data->EncodePointersAndHandles(&handles);
976 data->DecodePointersAndHandles(&handles); 943 data->DecodePointersAndHandles(&handles);
977 944
978 ObjectUnionPtr obj2 = ObjectUnion::New(); 945 ObjectUnionPtr obj2 = ObjectUnion::New();
979 Deserialize_(data, obj2.get()); 946 Deserialize_(data, obj2.get());
980 947
981 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]); 948 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]);
982 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]); 949 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]);
983 } 950 }
984 951
985 TEST(UnionTest, MapInUnionValidation) { 952 TEST(UnionTest, MapInUnionValidation) {
986 Environment environment;
987 Map<String, int8_t> map; 953 Map<String, int8_t> map;
988 map.insert("one", 1); 954 map.insert("one", 1);
989 map.insert("two", 2); 955 map.insert("two", 2);
990 956
991 ObjectUnionPtr obj(ObjectUnion::New()); 957 ObjectUnionPtr obj(ObjectUnion::New());
992 obj->set_f_map_int8(map.Pass()); 958 obj->set_f_map_int8(map.Pass());
993 959
994 size_t size = GetSerializedSize_(obj); 960 size_t size = GetSerializedSize_(obj);
995 EXPECT_EQ(112U, size); 961 EXPECT_EQ(112U, size);
996 962
(...skipping 19 matching lines...) Expand all
1016 PodUnionPtr pod(PodUnion::New()); 982 PodUnionPtr pod(PodUnion::New());
1017 pod->set_f_int8(10); 983 pod->set_f_int8(10);
1018 984
1019 ObjectUnionPtr obj(ObjectUnion::New()); 985 ObjectUnionPtr obj(ObjectUnion::New());
1020 obj->set_f_pod_union(pod.Pass()); 986 obj->set_f_pod_union(pod.Pass());
1021 987
1022 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8()); 988 EXPECT_EQ(10, obj->get_f_pod_union()->get_f_int8());
1023 } 989 }
1024 990
1025 TEST(UnionTest, UnionInUnionSerialization) { 991 TEST(UnionTest, UnionInUnionSerialization) {
1026 Environment environment;
1027 PodUnionPtr pod(PodUnion::New()); 992 PodUnionPtr pod(PodUnion::New());
1028 pod->set_f_int8(10); 993 pod->set_f_int8(10);
1029 994
1030 ObjectUnionPtr obj(ObjectUnion::New()); 995 ObjectUnionPtr obj(ObjectUnion::New());
1031 obj->set_f_pod_union(pod.Pass()); 996 obj->set_f_pod_union(pod.Pass());
1032 997
1033 size_t size = GetSerializedSize_(obj); 998 size_t size = GetSerializedSize_(obj);
1034 EXPECT_EQ(32U, size); 999 EXPECT_EQ(32U, size);
1035 1000
1036 mojo::internal::FixedBufferForTesting buf(size); 1001 mojo::internal::FixedBufferForTesting buf(size);
1037 auto* data = internal::ObjectUnion_Data::New(&buf); 1002 auto* data = internal::ObjectUnion_Data::New(&buf);
1038 SerializeUnion_(obj.get(), &buf, &data); 1003 SerializeUnion_(obj.get(), &buf, &data);
1039 1004
1040 std::vector<Handle> handles; 1005 std::vector<Handle> handles;
1041 data->EncodePointersAndHandles(&handles); 1006 data->EncodePointersAndHandles(&handles);
1042 data->DecodePointersAndHandles(&handles); 1007 data->DecodePointersAndHandles(&handles);
1043 1008
1044 ObjectUnionPtr obj2 = ObjectUnion::New(); 1009 ObjectUnionPtr obj2 = ObjectUnion::New();
1045 Deserialize_(data, obj2.get()); 1010 Deserialize_(data, obj2.get());
1046 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8()); 1011 EXPECT_EQ(10, obj2->get_f_pod_union()->get_f_int8());
1047 } 1012 }
1048 1013
1049 TEST(UnionTest, UnionInUnionValidation) { 1014 TEST(UnionTest, UnionInUnionValidation) {
1050 Environment environment;
1051 PodUnionPtr pod(PodUnion::New()); 1015 PodUnionPtr pod(PodUnion::New());
1052 pod->set_f_int8(10); 1016 pod->set_f_int8(10);
1053 1017
1054 ObjectUnionPtr obj(ObjectUnion::New()); 1018 ObjectUnionPtr obj(ObjectUnion::New());
1055 obj->set_f_pod_union(pod.Pass()); 1019 obj->set_f_pod_union(pod.Pass());
1056 1020
1057 size_t size = GetSerializedSize_(obj); 1021 size_t size = GetSerializedSize_(obj);
1058 EXPECT_EQ(32U, size); 1022 EXPECT_EQ(32U, size);
1059 1023
1060 mojo::internal::FixedBufferForTesting buf(size); 1024 mojo::internal::FixedBufferForTesting buf(size);
1061 auto* data = internal::ObjectUnion_Data::New(&buf); 1025 auto* data = internal::ObjectUnion_Data::New(&buf);
1062 SerializeUnion_(obj.get(), &buf, &data); 1026 SerializeUnion_(obj.get(), &buf, &data);
1063 1027
1064 std::vector<Handle> handles; 1028 std::vector<Handle> handles;
1065 data->EncodePointersAndHandles(&handles); 1029 data->EncodePointersAndHandles(&handles);
1066 1030
1067 void* raw_buf = buf.Leak(); 1031 void* raw_buf = buf.Leak();
1068 mojo::internal::BoundsChecker bounds_checker(data, 1032 mojo::internal::BoundsChecker bounds_checker(data,
1069 static_cast<uint32_t>(size), 0); 1033 static_cast<uint32_t>(size), 0);
1070 EXPECT_EQ(mojo::internal::ValidationError::NONE, 1034 EXPECT_EQ(mojo::internal::ValidationError::NONE,
1071 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, 1035 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker,
1072 false, nullptr)); 1036 false, nullptr));
1073 free(raw_buf); 1037 free(raw_buf);
1074 } 1038 }
1075 1039
1076 TEST(UnionTest, UnionInUnionValidationNonNullable) { 1040 TEST(UnionTest, UnionInUnionValidationNonNullable) {
1077 Environment environment;
1078 PodUnionPtr pod(nullptr); 1041 PodUnionPtr pod(nullptr);
1079 1042
1080 ObjectUnionPtr obj(ObjectUnion::New()); 1043 ObjectUnionPtr obj(ObjectUnion::New());
1081 obj->set_f_pod_union(pod.Pass()); 1044 obj->set_f_pod_union(pod.Pass());
1082 1045
1083 size_t size = GetSerializedSize_(obj); 1046 size_t size = GetSerializedSize_(obj);
1084 1047
1085 mojo::internal::FixedBufferForTesting buf(size); 1048 mojo::internal::FixedBufferForTesting buf(size);
1086 auto* data = internal::ObjectUnion_Data::New(&buf); 1049 auto* data = internal::ObjectUnion_Data::New(&buf);
1087 SerializeUnion_(obj.get(), &buf, &data); 1050 SerializeUnion_(obj.get(), &buf, &data);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1142 std::string golden("hello world"); 1105 std::string golden("hello world");
1143 WriteTextMessage(pipe0.get(), golden); 1106 WriteTextMessage(pipe0.get(), golden);
1144 1107
1145 std::string actual; 1108 std::string actual;
1146 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual); 1109 ReadTextMessage(handle2->get_f_message_pipe().get(), &actual);
1147 1110
1148 EXPECT_EQ(golden, actual); 1111 EXPECT_EQ(golden, actual);
1149 } 1112 }
1150 1113
1151 TEST(UnionTest, HandleInUnionValidation) { 1114 TEST(UnionTest, HandleInUnionValidation) {
1152 Environment environment;
1153 ScopedMessagePipeHandle pipe0; 1115 ScopedMessagePipeHandle pipe0;
1154 ScopedMessagePipeHandle pipe1; 1116 ScopedMessagePipeHandle pipe1;
1155 1117
1156 CreateMessagePipe(nullptr, &pipe0, &pipe1); 1118 CreateMessagePipe(nullptr, &pipe0, &pipe1);
1157 1119
1158 HandleUnionPtr handle(HandleUnion::New()); 1120 HandleUnionPtr handle(HandleUnion::New());
1159 handle->set_f_message_pipe(pipe1.Pass()); 1121 handle->set_f_message_pipe(pipe1.Pass());
1160 1122
1161 size_t size = GetSerializedSize_(handle); 1123 size_t size = GetSerializedSize_(handle);
1162 EXPECT_EQ(16U, size); 1124 EXPECT_EQ(16U, size);
1163 1125
1164 mojo::internal::FixedBufferForTesting buf(size); 1126 mojo::internal::FixedBufferForTesting buf(size);
1165 auto* data = internal::HandleUnion_Data::New(&buf); 1127 auto* data = internal::HandleUnion_Data::New(&buf);
1166 SerializeUnion_(handle.get(), &buf, &data); 1128 SerializeUnion_(handle.get(), &buf, &data);
1167 1129
1168 std::vector<Handle> handles; 1130 std::vector<Handle> handles;
1169 data->EncodePointersAndHandles(&handles); 1131 data->EncodePointersAndHandles(&handles);
1170 1132
1171 void* raw_buf = buf.Leak(); 1133 void* raw_buf = buf.Leak();
1172 mojo::internal::BoundsChecker bounds_checker(data, 1134 mojo::internal::BoundsChecker bounds_checker(data,
1173 static_cast<uint32_t>(size), 1); 1135 static_cast<uint32_t>(size), 1);
1174 EXPECT_EQ(mojo::internal::ValidationError::NONE, 1136 EXPECT_EQ(mojo::internal::ValidationError::NONE,
1175 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker, 1137 internal::HandleUnion_Data::Validate(raw_buf, &bounds_checker,
1176 false, nullptr)); 1138 false, nullptr));
1177 free(raw_buf); 1139 free(raw_buf);
1178 } 1140 }
1179 1141
1180 TEST(UnionTest, HandleInUnionValidationNull) { 1142 TEST(UnionTest, HandleInUnionValidationNull) {
1181 Environment environment;
1182 ScopedMessagePipeHandle pipe; 1143 ScopedMessagePipeHandle pipe;
1183 HandleUnionPtr handle(HandleUnion::New()); 1144 HandleUnionPtr handle(HandleUnion::New());
1184 handle->set_f_message_pipe(pipe.Pass()); 1145 handle->set_f_message_pipe(pipe.Pass());
1185 1146
1186 size_t size = GetSerializedSize_(handle); 1147 size_t size = GetSerializedSize_(handle);
1187 EXPECT_EQ(16U, size); 1148 EXPECT_EQ(16U, size);
1188 1149
1189 mojo::internal::FixedBufferForTesting buf(size); 1150 mojo::internal::FixedBufferForTesting buf(size);
1190 auto* data = internal::HandleUnion_Data::New(&buf); 1151 auto* data = internal::HandleUnion_Data::New(&buf);
1191 SerializeUnion_(handle.get(), &buf, &data); 1152 SerializeUnion_(handle.get(), &buf, &data);
(...skipping 19 matching lines...) Expand all
1211 private: 1172 private:
1212 void SetIntValue(int64_t int_value) override { int_value_ = int_value; } 1173 void SetIntValue(int64_t int_value) override { int_value_ = int_value; }
1213 void GetIntValue(const GetIntValueCallback& callback) override { 1174 void GetIntValue(const GetIntValueCallback& callback) override {
1214 callback.Run(int_value_); 1175 callback.Run(int_value_);
1215 } 1176 }
1216 1177
1217 int64_t int_value_; 1178 int64_t int_value_;
1218 }; 1179 };
1219 1180
1220 TEST(UnionTest, InterfaceInUnion) { 1181 TEST(UnionTest, InterfaceInUnion) {
1221 Environment env;
1222 RunLoop run_loop; 1182 RunLoop run_loop;
1223 SmallCacheImpl impl; 1183 SmallCacheImpl impl;
1224 SmallCachePtr ptr; 1184 SmallCachePtr ptr;
1225 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); 1185 Binding<SmallCache> bindings(&impl, GetProxy(&ptr));
1226 1186
1227 HandleUnionPtr handle(HandleUnion::New()); 1187 HandleUnionPtr handle(HandleUnion::New());
1228 handle->set_f_small_cache(ptr.Pass()); 1188 handle->set_f_small_cache(ptr.Pass());
1229 1189
1230 auto small_cache = 1190 auto small_cache =
1231 SmallCachePtr::Create(std::move(handle->get_f_small_cache())); 1191 SmallCachePtr::Create(std::move(handle->get_f_small_cache()));
1232 small_cache->SetIntValue(10); 1192 small_cache->SetIntValue(10);
1233 run_loop.RunUntilIdle(); 1193 run_loop.RunUntilIdle();
1234 EXPECT_EQ(10, impl.int_value()); 1194 EXPECT_EQ(10, impl.int_value());
1235 } 1195 }
1236 1196
1237 TEST(UnionTest, InterfaceInUnionSerialization) { 1197 TEST(UnionTest, InterfaceInUnionSerialization) {
1238 Environment env;
1239 RunLoop run_loop; 1198 RunLoop run_loop;
1240 SmallCacheImpl impl; 1199 SmallCacheImpl impl;
1241 SmallCachePtr ptr; 1200 SmallCachePtr ptr;
1242 Binding<SmallCache> bindings(&impl, GetProxy(&ptr)); 1201 Binding<SmallCache> bindings(&impl, GetProxy(&ptr));
1243 1202
1244 HandleUnionPtr handle(HandleUnion::New()); 1203 HandleUnionPtr handle(HandleUnion::New());
1245 handle->set_f_small_cache(ptr.Pass()); 1204 handle->set_f_small_cache(ptr.Pass());
1246 size_t size = GetSerializedSize_(handle); 1205 size_t size = GetSerializedSize_(handle);
1247 EXPECT_EQ(16U, size); 1206 EXPECT_EQ(16U, size);
1248 1207
(...skipping 21 matching lines...) Expand all
1270 UnionInterfaceImpl() {} 1229 UnionInterfaceImpl() {}
1271 ~UnionInterfaceImpl() override {} 1230 ~UnionInterfaceImpl() override {}
1272 1231
1273 private: 1232 private:
1274 void Echo(PodUnionPtr in, const EchoCallback& callback) override { 1233 void Echo(PodUnionPtr in, const EchoCallback& callback) override {
1275 callback.Run(in.Pass()); 1234 callback.Run(in.Pass());
1276 } 1235 }
1277 }; 1236 };
1278 1237
1279 TEST(UnionTest, UnionInInterface) { 1238 TEST(UnionTest, UnionInInterface) {
1280 Environment env;
1281 RunLoop run_loop; 1239 RunLoop run_loop;
1282 UnionInterfaceImpl impl; 1240 UnionInterfaceImpl impl;
1283 UnionInterfacePtr ptr; 1241 UnionInterfacePtr ptr;
1284 Binding<UnionInterface> bindings(&impl, GetProxy(&ptr)); 1242 Binding<UnionInterface> bindings(&impl, GetProxy(&ptr));
1285 1243
1286 PodUnionPtr pod(PodUnion::New()); 1244 PodUnionPtr pod(PodUnion::New());
1287 pod->set_f_int16(16); 1245 pod->set_f_int16(16);
1288 1246
1289 ptr->Echo(pod.Pass(), 1247 ptr->Echo(pod.Pass(),
1290 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); 1248 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); });
1291 run_loop.RunUntilIdle(); 1249 run_loop.RunUntilIdle();
1292 } 1250 }
1293 1251
1294 } // namespace test 1252 } // namespace test
1295 } // namespace mojo 1253 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698