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

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

Issue 1065653006: Array and map members for unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « no previous file | mojo/public/interfaces/bindings/tests/test_unions.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "mojo/public/cpp/bindings/array.h" 6 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_internal.h" 7 #include "mojo/public/cpp/bindings/lib/array_internal.h"
7 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 8 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
8 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" 9 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
10 #include "mojo/public/cpp/bindings/string.h" 11 #include "mojo/public/cpp/bindings/string.h"
11 #include "mojo/public/cpp/environment/environment.h" 12 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
13 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" 14 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 631
631 mojo::internal::FixedBuffer buf(size); 632 mojo::internal::FixedBuffer buf(size);
632 internal::ObjectUnion_Data* data = nullptr; 633 internal::ObjectUnion_Data* data = nullptr;
633 SerializeUnion_(obj.Pass(), &buf, &data, false); 634 SerializeUnion_(obj.Pass(), &buf, &data, false);
634 635
635 void* raw_buf = buf.Leak(); 636 void* raw_buf = buf.Leak();
636 mojo::internal::BoundsChecker bounds_checker(data, 637 mojo::internal::BoundsChecker bounds_checker(data,
637 static_cast<uint32_t>(size), 0); 638 static_cast<uint32_t>(size), 0);
638 EXPECT_TRUE( 639 EXPECT_TRUE(
639 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); 640 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
641 free(raw_buf);
640 } 642 }
641 643
642 TEST(UnionTest, StructInUnionValidationNonNullable) { 644 TEST(UnionTest, StructInUnionValidationNonNullable) {
643 Environment environment; 645 Environment environment;
644 DummyStructPtr dummy(nullptr); 646 DummyStructPtr dummy(nullptr);
645 647
646 ObjectUnionPtr obj(ObjectUnion::New()); 648 ObjectUnionPtr obj(ObjectUnion::New());
647 obj->set_f_dummy(dummy.Pass()); 649 obj->set_f_dummy(dummy.Pass());
648 650
649 size_t size = GetSerializedSize_(obj, false); 651 size_t size = GetSerializedSize_(obj, false);
650 652
651 mojo::internal::FixedBuffer buf(size); 653 mojo::internal::FixedBuffer buf(size);
652 internal::ObjectUnion_Data* data = nullptr; 654 internal::ObjectUnion_Data* data = nullptr;
653 SerializeUnion_(obj.Pass(), &buf, &data, false); 655 SerializeUnion_(obj.Pass(), &buf, &data, false);
654 656
655 void* raw_buf = buf.Leak(); 657 void* raw_buf = buf.Leak();
656 mojo::internal::BoundsChecker bounds_checker(data, 658 mojo::internal::BoundsChecker bounds_checker(data,
657 static_cast<uint32_t>(size), 0); 659 static_cast<uint32_t>(size), 0);
658 EXPECT_FALSE( 660 EXPECT_FALSE(
659 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); 661 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
662 free(raw_buf);
660 } 663 }
661 664
662 TEST(UnionTest, StructInUnionValidationNullable) { 665 TEST(UnionTest, StructInUnionValidationNullable) {
663 Environment environment; 666 Environment environment;
664 DummyStructPtr dummy(nullptr); 667 DummyStructPtr dummy(nullptr);
665 668
666 ObjectUnionPtr obj(ObjectUnion::New()); 669 ObjectUnionPtr obj(ObjectUnion::New());
667 obj->set_f_nullable(dummy.Pass()); 670 obj->set_f_nullable(dummy.Pass());
668 671
669 size_t size = GetSerializedSize_(obj, false); 672 size_t size = GetSerializedSize_(obj, false);
670 673
671 mojo::internal::FixedBuffer buf(size); 674 mojo::internal::FixedBuffer buf(size);
672 internal::ObjectUnion_Data* data = nullptr; 675 internal::ObjectUnion_Data* data = nullptr;
673 SerializeUnion_(obj.Pass(), &buf, &data, false); 676 SerializeUnion_(obj.Pass(), &buf, &data, false);
674 677
675 void* raw_buf = buf.Leak(); 678 void* raw_buf = buf.Leak();
676 mojo::internal::BoundsChecker bounds_checker(data, 679 mojo::internal::BoundsChecker bounds_checker(data,
677 static_cast<uint32_t>(size), 0); 680 static_cast<uint32_t>(size), 0);
678 EXPECT_TRUE( 681 EXPECT_TRUE(
679 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); 682 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
683 free(raw_buf);
684 }
685
686 TEST(UnionTest, ArrayInUnionGetterSetter) {
687 Environment environment;
688
689 Array<int8_t> array(2);
690 array[0] = 8;
691 array[1] = 9;
692
693 ObjectUnionPtr obj(ObjectUnion::New());
694 obj->set_f_array_int8(array.Pass());
695
696 EXPECT_EQ(8, obj->get_f_array_int8()[0]);
697 EXPECT_EQ(9, obj->get_f_array_int8()[1]);
698 }
699
700 TEST(UnionTest, ArrayInUnionSerialization) {
701 Environment environment;
702
703 Array<int8_t> array(2);
704 array[0] = 8;
705 array[1] = 9;
706
707 ObjectUnionPtr obj(ObjectUnion::New());
708 obj->set_f_array_int8(array.Pass());
709
710 size_t size = GetSerializedSize_(obj, false);
711 EXPECT_EQ(32U, size);
712
713 mojo::internal::FixedBuffer buf(size);
714 internal::ObjectUnion_Data* data = nullptr;
715 SerializeUnion_(obj.Pass(), &buf, &data, false);
716
717 std::vector<Handle> handles;
718 data->EncodePointersAndHandles(&handles);
719 data->DecodePointersAndHandles(&handles);
720
721 ObjectUnionPtr obj2;
722 Deserialize_(data, &obj2);
723
724 EXPECT_EQ(8, obj2->get_f_array_int8()[0]);
725 EXPECT_EQ(9, obj2->get_f_array_int8()[1]);
726 }
727
728 TEST(UnionTest, ArrayInUnionValidation) {
729 Environment environment;
730
731 Array<int8_t> array(2);
732 array[0] = 8;
733 array[1] = 9;
734
735 ObjectUnionPtr obj(ObjectUnion::New());
736 obj->set_f_array_int8(array.Pass());
737
738 size_t size = GetSerializedSize_(obj, false);
739 mojo::internal::FixedBuffer buf(size);
740 internal::ObjectUnion_Data* data = nullptr;
741 SerializeUnion_(obj.Pass(), &buf, &data, false);
742
743 std::vector<Handle> handles;
744 data->EncodePointersAndHandles(&handles);
745
746 void* raw_buf = buf.Leak();
747 mojo::internal::BoundsChecker bounds_checker(data,
748 static_cast<uint32_t>(size), 0);
749
750 EXPECT_TRUE(
751 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
752 free(raw_buf);
753 }
754
755 TEST(UnionTest, MapInUnionGetterSetter) {
756 Environment environment;
757 Map<String, int8_t> map;
758 map.insert("one", 1);
759 map.insert("two", 2);
760
761 ObjectUnionPtr obj(ObjectUnion::New());
762 obj->set_f_map_int8(map.Pass());
763
764 EXPECT_EQ(1, obj->get_f_map_int8()["one"]);
765 EXPECT_EQ(2, obj->get_f_map_int8()["two"]);
766 }
767
768 TEST(UnionTest, MapInUnionSerialization) {
769 Environment environment;
770 Map<String, int8_t> map;
771 map.insert("one", 1);
772 map.insert("two", 2);
773
774 ObjectUnionPtr obj(ObjectUnion::New());
775 obj->set_f_map_int8(map.Pass());
776
777 size_t size = GetSerializedSize_(obj, false);
778 EXPECT_EQ(112U, size);
779
780 mojo::internal::FixedBuffer buf(size);
781 internal::ObjectUnion_Data* data = nullptr;
782 SerializeUnion_(obj.Pass(), &buf, &data, false);
783
784 std::vector<Handle> handles;
785 data->EncodePointersAndHandles(&handles);
786 data->DecodePointersAndHandles(&handles);
787
788 ObjectUnionPtr obj2;
789 Deserialize_(data, &obj2);
790
791 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]);
792 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]);
793 }
794
795 TEST(UnionTest, MapInUnionValidation) {
796 Environment environment;
797 Map<String, int8_t> map;
798 map.insert("one", 1);
799 map.insert("two", 2);
800
801 ObjectUnionPtr obj(ObjectUnion::New());
802 obj->set_f_map_int8(map.Pass());
803
804 size_t size = GetSerializedSize_(obj, false);
805 EXPECT_EQ(112U, size);
806
807 mojo::internal::FixedBuffer buf(size);
808 internal::ObjectUnion_Data* data = nullptr;
809 SerializeUnion_(obj.Pass(), &buf, &data, false);
810
811 std::vector<Handle> handles;
812 data->EncodePointersAndHandles(&handles);
813
814 void* raw_buf = buf.Leak();
815 mojo::internal::BoundsChecker bounds_checker(data,
816 static_cast<uint32_t>(size), 0);
817
818 EXPECT_TRUE(
819 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
820 free(raw_buf);
680 } 821 }
681 822
682 } // namespace test 823 } // namespace test
683 } // namespace mojo 824 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/public/interfaces/bindings/tests/test_unions.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698