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

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

Issue 1387993002: mojo::Serialize*_() calls now propogate/return validation errors. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: unscope VALIDATION_ERROR_* enum values. early exit on null-input. additional comments Created 5 years, 2 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 <vector> 5 #include <vector>
6 #include "mojo/public/cpp/bindings/array.h" 6 #include "mojo/public/cpp/bindings/array.h"
7 #include "mojo/public/cpp/bindings/binding.h" 7 #include "mojo/public/cpp/bindings/binding.h"
8 #include "mojo/public/cpp/bindings/lib/array_internal.h" 8 #include "mojo/public/cpp/bindings/lib/array_internal.h"
9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" 10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 484
485 // Serialization test of a struct with a union of plain old data. 485 // Serialization test of a struct with a union of plain old data.
486 TEST(UnionTest, Serialization_UnionOfPods) { 486 TEST(UnionTest, Serialization_UnionOfPods) {
487 Environment environment; 487 Environment environment;
488 SmallStructPtr small_struct(SmallStruct::New()); 488 SmallStructPtr small_struct(SmallStruct::New());
489 small_struct->pod_union = PodUnion::New(); 489 small_struct->pod_union = PodUnion::New();
490 small_struct->pod_union->set_f_int32(10); 490 small_struct->pod_union->set_f_int32(10);
491 491
492 size_t size = GetSerializedSize_(*small_struct); 492 size_t size = GetSerializedSize_(*small_struct);
493 493
494 mojo::internal::FixedBufferForTesting buf(size); 494 mojo::internal::FixedBufferForTesting buf(size);
viettrungluu 2015/10/09 01:35:20 mojo::
vardhan 2015/10/09 22:31:29 (See comments regarding this in other files)
495 internal::SmallStruct_Data* data = nullptr; 495 internal::SmallStruct_Data* data = nullptr;
viettrungluu 2015/10/09 01:35:20 No mojo:: kill me now
vardhan 2015/10/09 22:31:29 :-) (this |internal| is a referring to the |inter
496 Serialize_(small_struct.get(), &buf, &data); 496 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
497 Serialize_(small_struct.get(), &buf, &data));
497 498
498 SmallStructPtr deserialized(SmallStruct::New()); 499 SmallStructPtr deserialized(SmallStruct::New());
499 Deserialize_(data, deserialized.get()); 500 Deserialize_(data, deserialized.get());
500 501
501 EXPECT_EQ(10, deserialized->pod_union->get_f_int32()); 502 EXPECT_EQ(10, deserialized->pod_union->get_f_int32());
502 } 503 }
503 504
504 // Serialization test of a struct with a union of structs. 505 // Serialization test of a struct with a union of structs.
505 TEST(UnionTest, Serialization_UnionOfObjects) { 506 TEST(UnionTest, Serialization_UnionOfObjects) {
506 Environment environment; 507 Environment environment;
507 SmallObjStructPtr obj_struct(SmallObjStruct::New()); 508 SmallObjStructPtr obj_struct(SmallObjStruct::New());
508 obj_struct->obj_union = ObjectUnion::New(); 509 obj_struct->obj_union = ObjectUnion::New();
509 String hello("hello world"); 510 String hello("hello world");
510 obj_struct->obj_union->set_f_string(hello); 511 obj_struct->obj_union->set_f_string(hello);
511 512
512 size_t size = GetSerializedSize_(*obj_struct); 513 size_t size = GetSerializedSize_(*obj_struct);
513 514
514 mojo::internal::FixedBufferForTesting buf(size); 515 mojo::internal::FixedBufferForTesting buf(size);
515 internal::SmallObjStruct_Data* data = nullptr; 516 internal::SmallObjStruct_Data* data = nullptr;
516 Serialize_(obj_struct.get(), &buf, &data); 517 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
518 Serialize_(obj_struct.get(), &buf, &data));
517 519
518 std::vector<Handle> handles; 520 std::vector<Handle> handles;
519 data->EncodePointersAndHandles(&handles); 521 data->EncodePointersAndHandles(&handles);
520 data->DecodePointersAndHandles(&handles); 522 data->DecodePointersAndHandles(&handles);
521 523
522 SmallObjStructPtr deserialized(SmallObjStruct::New()); 524 SmallObjStructPtr deserialized(SmallObjStruct::New());
523 Deserialize_(data, deserialized.get()); 525 Deserialize_(data, deserialized.get());
524 526
525 EXPECT_EQ(hello, deserialized->obj_union->get_f_string()); 527 EXPECT_EQ(hello, deserialized->obj_union->get_f_string());
526 } 528 }
527 529
528 // Validation test of a struct with a union. 530 // Validation test of a struct with a union.
529 TEST(UnionTest, Validation_UnionsInStruct) { 531 TEST(UnionTest, Validation_UnionsInStruct) {
530 Environment environment; 532 Environment environment;
531 SmallStructPtr small_struct(SmallStruct::New()); 533 SmallStructPtr small_struct(SmallStruct::New());
532 small_struct->pod_union = PodUnion::New(); 534 small_struct->pod_union = PodUnion::New();
533 small_struct->pod_union->set_f_int32(10); 535 small_struct->pod_union->set_f_int32(10);
534 536
535 size_t size = GetSerializedSize_(*small_struct); 537 size_t size = GetSerializedSize_(*small_struct);
536 538
537 mojo::internal::FixedBufferForTesting buf(size); 539 mojo::internal::FixedBufferForTesting buf(size);
538 internal::SmallStruct_Data* data = nullptr; 540 internal::SmallStruct_Data* data = nullptr;
539 Serialize_(small_struct.get(), &buf, &data); 541 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
542 Serialize_(small_struct.get(), &buf, &data));
540 543
541 void* raw_buf = buf.Leak(); 544 void* raw_buf = buf.Leak();
542 mojo::internal::BoundsChecker bounds_checker(data, 545 mojo::internal::BoundsChecker bounds_checker(data,
543 static_cast<uint32_t>(size), 0); 546 static_cast<uint32_t>(size), 0);
544 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); 547 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
545 free(raw_buf); 548 free(raw_buf);
546 } 549 }
547 550
548 // Validation test of a struct union fails due to unknown union tag. 551 // Validation test of a struct union fails due to unknown union tag.
549 TEST(UnionTest, Validation_PodUnionInStruct_Failure) { 552 TEST(UnionTest, Validation_PodUnionInStruct_Failure) {
550 Environment environment; 553 Environment environment;
551 SmallStructPtr small_struct(SmallStruct::New()); 554 SmallStructPtr small_struct(SmallStruct::New());
552 small_struct->pod_union = PodUnion::New(); 555 small_struct->pod_union = PodUnion::New();
553 small_struct->pod_union->set_f_int32(10); 556 small_struct->pod_union->set_f_int32(10);
554 557
555 size_t size = GetSerializedSize_(*small_struct); 558 size_t size = GetSerializedSize_(*small_struct);
556 559
557 mojo::internal::FixedBufferForTesting buf(size); 560 mojo::internal::FixedBufferForTesting buf(size);
558 internal::SmallStruct_Data* data = nullptr; 561 internal::SmallStruct_Data* data = nullptr;
559 Serialize_(small_struct.get(), &buf, &data); 562 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
563 Serialize_(small_struct.get(), &buf, &data));
560 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100); 564 data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
561 565
562 void* raw_buf = buf.Leak(); 566 void* raw_buf = buf.Leak();
563 mojo::internal::BoundsChecker bounds_checker(data, 567 mojo::internal::BoundsChecker bounds_checker(data,
564 static_cast<uint32_t>(size), 0); 568 static_cast<uint32_t>(size), 0);
565 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); 569 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
566 free(raw_buf); 570 free(raw_buf);
567 } 571 }
568 572
569 // Validation fails due to non-nullable null union in struct. 573 // Validation fails due to non-nullable null union in struct.
(...skipping 18 matching lines...) Expand all
588 592
589 // Validation passes with nullable null union. 593 // Validation passes with nullable null union.
590 TEST(UnionTest, Validation_NullableUnion) { 594 TEST(UnionTest, Validation_NullableUnion) {
591 Environment environment; 595 Environment environment;
592 SmallStructPtr small_struct(SmallStruct::New()); 596 SmallStructPtr small_struct(SmallStruct::New());
593 597
594 size_t size = GetSerializedSize_(*small_struct); 598 size_t size = GetSerializedSize_(*small_struct);
595 599
596 mojo::internal::FixedBufferForTesting buf(size); 600 mojo::internal::FixedBufferForTesting buf(size);
597 internal::SmallStruct_Data* data = nullptr; 601 internal::SmallStruct_Data* data = nullptr;
598 Serialize_(small_struct.get(), &buf, &data); 602 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
603 Serialize_(small_struct.get(), &buf, &data));
599 604
600 void* raw_buf = buf.Leak(); 605 void* raw_buf = buf.Leak();
601 mojo::internal::BoundsChecker bounds_checker(data, 606 mojo::internal::BoundsChecker bounds_checker(data,
602 static_cast<uint32_t>(size), 0); 607 static_cast<uint32_t>(size), 0);
603 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker)); 608 EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
604 free(raw_buf); 609 free(raw_buf);
605 } 610 }
606 611
607 // TODO(azani): Move back in map_unittest.cc when possible. 612 // TODO(azani): Move back in map_unittest.cc when possible.
608 // Map Tests 613 // Map Tests
(...skipping 19 matching lines...) Expand all
628 map["one"]->set_f_int8(8); 633 map["one"]->set_f_int8(8);
629 map["two"]->set_f_int16(16); 634 map["two"]->set_f_int16(16);
630 635
631 size_t size = GetSerializedSize_(map); 636 size_t size = GetSerializedSize_(map);
632 EXPECT_EQ(120U, size); 637 EXPECT_EQ(120U, size);
633 638
634 mojo::internal::FixedBufferForTesting buf(size); 639 mojo::internal::FixedBufferForTesting buf(size);
635 mojo::internal::Map_Data<mojo::internal::String_Data*, 640 mojo::internal::Map_Data<mojo::internal::String_Data*,
636 internal::PodUnion_Data>* data; 641 internal::PodUnion_Data>* data;
637 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr); 642 mojo::internal::ArrayValidateParams validate_params(0, false, nullptr);
638 SerializeMap_(&map, &buf, &data, &validate_params); 643 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
644 SerializeMap_(&map, &buf, &data, &validate_params));
639 645
640 Map<String, PodUnionPtr> map2; 646 Map<String, PodUnionPtr> map2;
641 Deserialize_(data, &map2); 647 Deserialize_(data, &map2);
642 648
643 EXPECT_EQ(8, map2["one"]->get_f_int8()); 649 EXPECT_EQ(8, map2["one"]->get_f_int8());
644 EXPECT_EQ(16, map2["two"]->get_f_int16()); 650 EXPECT_EQ(16, map2["two"]->get_f_int16());
645 } 651 }
646 652
647 TEST(UnionTest, PodUnionInMapSerializationWithNull) { 653 TEST(UnionTest, PodUnionInMapSerializationWithNull) {
648 Environment environment; 654 Environment environment;
649 Map<String, PodUnionPtr> map; 655 Map<String, PodUnionPtr> map;
650 map.insert("one", PodUnion::New()); 656 map.insert("one", PodUnion::New());
651 map.insert("two", nullptr); 657 map.insert("two", nullptr);
652 658
653 map["one"]->set_f_int8(8); 659 map["one"]->set_f_int8(8);
654 660
655 size_t size = GetSerializedSize_(map); 661 size_t size = GetSerializedSize_(map);
656 EXPECT_EQ(120U, size); 662 EXPECT_EQ(120U, size);
657 663
658 mojo::internal::FixedBufferForTesting buf(size); 664 mojo::internal::FixedBufferForTesting buf(size);
659 mojo::internal::Map_Data<mojo::internal::String_Data*, 665 mojo::internal::Map_Data<mojo::internal::String_Data*,
660 internal::PodUnion_Data>* data; 666 internal::PodUnion_Data>* data;
661 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr); 667 mojo::internal::ArrayValidateParams validate_params(0, true, nullptr);
662 SerializeMap_(&map, &buf, &data, &validate_params); 668 EXPECT_EQ(mojo::internal::VALIDATION_ERROR_NONE,
669 SerializeMap_(&map, &buf, &data, &validate_params));
663 670
664 Map<String, PodUnionPtr> map2; 671 Map<String, PodUnionPtr> map2;
665 Deserialize_(data, &map2); 672 Deserialize_(data, &map2);
666 673
667 EXPECT_EQ(8, map2["one"]->get_f_int8()); 674 EXPECT_EQ(8, map2["one"]->get_f_int8());
668 EXPECT_TRUE(map2["two"].is_null()); 675 EXPECT_TRUE(map2["two"].is_null());
669 } 676 }
670 677
671 TEST(UnionTest, StructInUnionGetterSetterPasser) { 678 TEST(UnionTest, StructInUnionGetterSetterPasser) {
672 DummyStructPtr dummy(DummyStruct::New()); 679 DummyStructPtr dummy(DummyStruct::New());
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 PodUnionPtr pod(PodUnion::New()); 1176 PodUnionPtr pod(PodUnion::New());
1170 pod->set_f_int16(16); 1177 pod->set_f_int16(16);
1171 1178
1172 ptr->Echo(pod.Pass(), 1179 ptr->Echo(pod.Pass(),
1173 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); }); 1180 [](PodUnionPtr out) { EXPECT_EQ(16, out->get_f_int16()); });
1174 run_loop.RunUntilIdle(); 1181 run_loop.RunUntilIdle();
1175 } 1182 }
1176 1183
1177 } // namespace test 1184 } // namespace test
1178 } // namespace mojo 1185 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698