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

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

Issue 1509703002: Mojo C++ bindings: Fix bug: array<>, map<> should only initialize elements if they're not null when… (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years 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 "mojo/public/cpp/bindings/array.h" 5 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_internal.h" 6 #include "mojo/public/cpp/bindings/lib/array_internal.h"
7 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 7 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
9 #include "mojo/public/cpp/bindings/tests/container_test_util.h" 9 #include "mojo/public/cpp/bindings/tests/container_test_util.h"
10 #include "mojo/public/cpp/bindings/tests/iterator_test_util.h" 10 #include "mojo/public/cpp/bindings/tests/iterator_test_util.h"
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 mojo::internal::ValidationError::NONE, 428 mojo::internal::ValidationError::NONE,
429 SerializeArray_(&iface_array, &buf, &output, &validate_non_nullable)); 429 SerializeArray_(&iface_array, &buf, &output, &validate_non_nullable));
430 EXPECT_FALSE(iface_array[0].is_bound()); 430 EXPECT_FALSE(iface_array[0].is_bound());
431 431
432 Deserialize_(output, &iface_array); 432 Deserialize_(output, &iface_array);
433 EXPECT_TRUE(iface_array[0].is_bound()); 433 EXPECT_TRUE(iface_array[0].is_bound());
434 } 434 }
435 435
436 // Test serializing and deserializing a struct with an Array<> of another struct 436 // Test serializing and deserializing a struct with an Array<> of another struct
437 // which has an InterfacePtr. 437 // which has an InterfacePtr.
438 TEST_F(ArrayTest, Serialization_StructWithArrayOfIntefacePtr) { 438 TEST_F(ArrayTest, Serialization_StructWithArrayOfInterfacePtr) {
439 StructWithInterfaceArray struct_arr_iface; 439 StructWithInterfaceArray struct_arr_iface;
440 struct_arr_iface.structs_array = Array<StructWithInterfacePtr>::New(1); 440 struct_arr_iface.structs_array = Array<StructWithInterfacePtr>::New(1);
441 struct_arr_iface.nullable_structs_array = 441 struct_arr_iface.nullable_structs_array =
442 Array<StructWithInterfacePtr>::New(1); 442 Array<StructWithInterfacePtr>::New(1);
443 443
444 size_t size = GetSerializedSize_(struct_arr_iface); 444 size_t size = GetSerializedSize_(struct_arr_iface);
445 EXPECT_EQ(8U // struct header 445 EXPECT_EQ(8U // struct header
446 + 8U // offset to |structs_array| 446 + 8U // offset to |structs_array|
447 + (8U // array header 447 + (8U // array header
448 + 8U // offset to StructWithInterface 448 + 8U) // offset to StructWithInterface (nullptr)
449 + (8U // StructWithInterface header 449 + 8U // offset to |structs_nullable_array|
450 + 8U)) // Interface_Data 450 + 8U // offset to |nullable_structs_array|
451 + 8U // offset to |structs_nullable_array| 451 + (8U // array header
452 + 8U // offset to |nullable_structs_array| 452 + 8U) // offset to StructWithinInterface (nullptr)
453 + (8U // array header 453 + 8U, // offset to |nullable_structs_nullable_array|
454 + 8U // offset to StructWithinInterface
455 + (8U // StructWithInterface header
456 + 8U)) // Interface_Data
457 + 8U, // offset to |nullable_structs_nullable_array|
458 size); 454 size);
459 455
460 FixedBufferForTesting buf(size * 2); 456 FixedBufferForTesting buf(size * 2);
461 StructWithInterfaceArray::Data_* struct_arr_iface_data; 457 StructWithInterfaceArray::Data_* struct_arr_iface_data;
462 // 1. This should fail because |structs_array| has an invalid InterfacePtr<> 458 // 1. This should fail because |structs_array| has an invalid InterfacePtr<>
463 // and it is not nullable. 459 // and it is not nullable.
464 EXPECT_EQ(mojo::internal::ValidationError::UNEXPECTED_NULL_POINTER, 460 EXPECT_EQ(mojo::internal::ValidationError::UNEXPECTED_NULL_POINTER,
465 Serialize_(&struct_arr_iface, &buf, &struct_arr_iface_data)); 461 Serialize_(&struct_arr_iface, &buf, &struct_arr_iface_data));
466 462
467 // 2. Adding in a struct with a valid InterfacePtr<> will let it serialize. 463 // 2. Adding in a struct with a valid InterfacePtr<> will let it serialize.
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 EXPECT_LE(i1, i2); 740 EXPECT_LE(i1, i2);
745 } 741 }
746 742
747 { 743 {
748 SCOPED_TRACE("Array iterator bidirectionality test."); 744 SCOPED_TRACE("Array iterator bidirectionality test.");
749 ExpectBidiIteratorConcept(arr.begin(), arr.end(), values); 745 ExpectBidiIteratorConcept(arr.begin(), arr.end(), values);
750 ExpectBidiMutableIteratorConcept(arr.begin(), arr.end(), values); 746 ExpectBidiMutableIteratorConcept(arr.begin(), arr.end(), values);
751 } 747 }
752 } 748 }
753 749
750 // Test serializing and deserializing of an array with null elements.
751 TEST_F(ArrayTest, Serialization_ArrayOfStructPtr) {
752 ArrayValidateParams validate_nullable(2, true, nullptr);
753 ArrayValidateParams validate_non_nullable(2, false, nullptr);
754
755 Array<RectPtr> array = Array<RectPtr>::New(2);
756 array[1] = Rect::New();
757 array[1]->x = 1;
758 array[1]->y = 2;
759 array[1]->width = 3;
760 array[1]->height = 4;
761
762 size_t size_with_null = GetSerializedSize_(array);
763 EXPECT_EQ(8U + // array header
764 2 * 8U + // array payload (2 pointers)
765 8U + 4 * 4U, // struct header + contents (4 int32)
766 size_with_null);
767 Array_Data<Rect::Data_*>* output_with_null;
768
769 // 1. Array with non-nullable structs should fail serialization due to
770 // the null first element.
771 {
772 FixedBufferForTesting buf_with_null(size_with_null);
773 EXPECT_EQ(mojo::internal::ValidationError::UNEXPECTED_NULL_POINTER,
774 SerializeArray_(&array, &buf_with_null, &output_with_null,
775 &validate_non_nullable));
776 }
777
778 // 2. Array with nullable structs should succeed.
779 {
780 FixedBufferForTesting buf_with_null(size_with_null);
781 EXPECT_EQ(mojo::internal::ValidationError::NONE,
782 SerializeArray_(&array, &buf_with_null, &output_with_null,
783 &validate_nullable));
784
785 Array<RectPtr> array2;
786 Deserialize_(output_with_null, &array2);
787 EXPECT_TRUE(array2[0].is_null());
788 EXPECT_FALSE(array2[1].is_null());
789 EXPECT_EQ(1, array2[1]->x);
790 EXPECT_EQ(2, array2[1]->y);
791 EXPECT_EQ(3, array2[1]->width);
792 EXPECT_EQ(4, array2[1]->height);
793 }
794
795 // 3. Array with non-nullable structs should succeed after we fill in
796 // the missing first element.
797 {
798 array[0] = Rect::New();
799 array[0]->x = -1;
800 array[0]->y = -2;
801 array[0]->width = -3;
802 array[0]->height = -4;
803
804 size_t size_without_null = GetSerializedSize_(array);
805 EXPECT_EQ(8U + // array header
806 2 * 8U + // array payload (2 pointers)
807 2 * (8U + 4 * 4U), // struct header + contents (4 int32)
808 size_without_null);
809
810 FixedBufferForTesting buf_without_null(size_without_null);
811 Array_Data<Rect::Data_*>* output_without_null;
812 EXPECT_EQ(mojo::internal::ValidationError::NONE,
813 SerializeArray_(&array, &buf_without_null, &output_without_null,
814 &validate_non_nullable));
815
816 Array<RectPtr> array3;
817 Deserialize_(output_without_null, &array3);
818 EXPECT_FALSE(array3[0].is_null());
819 EXPECT_EQ(-1, array3[0]->x);
820 EXPECT_EQ(-2, array3[0]->y);
821 EXPECT_EQ(-3, array3[0]->width);
822 EXPECT_EQ(-4, array3[0]->height);
823 EXPECT_FALSE(array3[1].is_null());
824 EXPECT_EQ(1, array3[1]->x);
825 EXPECT_EQ(2, array3[1]->y);
826 EXPECT_EQ(3, array3[1]->width);
827 EXPECT_EQ(4, array3[1]->height);
828 }
829 }
830
754 } // namespace 831 } // namespace
755 } // namespace test 832 } // namespace test
756 } // namespace mojo 833 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698