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
3 // found in the LICENSE file.
5 // Serialization warnings are only recorded in debug build.
8 #include "mojo/public/cpp/bindings/array.h"
9 #include "mojo/public/cpp/bindings/lib/array_internal.h"
10 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
11 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
12 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
13 #include "mojo/public/cpp/bindings/string.h"
14 #include "mojo/public/cpp/environment/environment.h"
15 #include "mojo/public/cpp/system/message_pipe.h"
16 #include "mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 using mojo::internal::ArrayValidateParams;
24 using mojo::internal::NoValidateParams;
26 // Creates an array of arrays of handles (2 X 3) for testing.
27 Array<Array<ScopedHandle> > CreateTestNestedHandleArray() {
28 Array<Array<ScopedHandle> > array(2);
29 for (size_t i = 0; i < array.size(); ++i) {
30 Array<ScopedHandle> nested_array(3);
31 for (size_t j = 0; j < nested_array.size(); ++j) {
33 nested_array[j] = ScopedHandle::From(pipe.handle1.Pass());
35 array[i] = nested_array.Pass();
41 class SerializationWarningTest : public testing::Test {
43 virtual ~SerializationWarningTest() {}
47 void TestWarning(T obj, mojo::internal::ValidationError expected_warning) {
48 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE);
50 mojo::internal::FixedBuffer buf(GetSerializedSize_(obj));
51 typename T::Data_* data;
52 Serialize_(obj.Pass(), &buf, &data);
54 EXPECT_EQ(expected_warning, warning_observer_.last_warning());
57 template <typename ValidateParams, typename T>
58 void TestArrayWarning(T obj,
59 mojo::internal::ValidationError expected_warning) {
60 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE);
62 mojo::internal::FixedBuffer buf(GetSerializedSize_(obj));
63 typename T::Data_* data;
64 SerializeArray_<ValidateParams>(obj.Pass(), &buf, &data);
66 EXPECT_EQ(expected_warning, warning_observer_.last_warning());
69 mojo::internal::SerializationWarningObserverForTesting warning_observer_;
73 TEST_F(SerializationWarningTest, HandleInStruct) {
74 Struct2Ptr test_struct(Struct2::New());
75 EXPECT_FALSE(test_struct->hdl.is_valid());
77 TestWarning(test_struct.Pass(),
78 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE);
80 test_struct = Struct2::New();
82 test_struct->hdl = ScopedHandle::From(pipe.handle1.Pass());
84 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
87 TEST_F(SerializationWarningTest, StructInStruct) {
88 Struct3Ptr test_struct(Struct3::New());
89 EXPECT_TRUE(!test_struct->struct_1);
91 TestWarning(test_struct.Pass(),
92 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
94 test_struct = Struct3::New();
95 test_struct->struct_1 = Struct1::New();
97 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
100 TEST_F(SerializationWarningTest, ArrayOfStructsInStruct) {
101 Struct4Ptr test_struct(Struct4::New());
102 EXPECT_TRUE(!test_struct->array);
104 TestWarning(test_struct.Pass(),
105 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
107 test_struct = Struct4::New();
108 test_struct->array.resize(1);
110 TestWarning(test_struct.Pass(),
111 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
113 test_struct = Struct4::New();
114 test_struct->array.resize(0);
116 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
118 test_struct = Struct4::New();
119 test_struct->array.resize(1);
120 test_struct->array[0] = Struct1::New();
122 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
125 TEST_F(SerializationWarningTest, FixedArrayOfStructsInStruct) {
126 Struct5Ptr test_struct(Struct5::New());
127 EXPECT_TRUE(!test_struct->pair);
129 TestWarning(test_struct.Pass(),
130 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
132 test_struct = Struct5::New();
133 test_struct->pair.resize(1);
134 test_struct->pair[0] = Struct1::New();
136 TestWarning(test_struct.Pass(),
137 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER);
139 test_struct = Struct5::New();
140 test_struct->pair.resize(2);
141 test_struct->pair[0] = Struct1::New();
142 test_struct->pair[1] = Struct1::New();
144 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
147 TEST_F(SerializationWarningTest, StringInStruct) {
148 Struct6Ptr test_struct(Struct6::New());
149 EXPECT_TRUE(!test_struct->str);
151 TestWarning(test_struct.Pass(),
152 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
154 test_struct = Struct6::New();
155 test_struct->str = "hello world";
157 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
160 TEST_F(SerializationWarningTest, ArrayOfArraysOfHandles) {
161 Array<Array<ScopedHandle> > test_array = CreateTestNestedHandleArray();
162 test_array[0] = Array<ScopedHandle>();
163 test_array[1][0] = ScopedHandle();
165 TestArrayWarning<ArrayValidateParams<0, true,
166 ArrayValidateParams<0, true, NoValidateParams> > >(
167 test_array.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
169 test_array = CreateTestNestedHandleArray();
170 test_array[0] = Array<ScopedHandle>();
171 TestArrayWarning<ArrayValidateParams<0, false,
172 ArrayValidateParams<0, true, NoValidateParams> > >(
174 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
176 test_array = CreateTestNestedHandleArray();
177 test_array[1][0] = ScopedHandle();
178 TestArrayWarning<ArrayValidateParams<0, true,
179 ArrayValidateParams<0, false, NoValidateParams> > >(
181 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE);
184 TEST_F(SerializationWarningTest, ArrayOfStrings) {
185 Array<String> test_array(3);
186 for (size_t i = 0; i < test_array.size(); ++i)
187 test_array[i] = "hello";
189 TestArrayWarning<ArrayValidateParams<0, true,
190 ArrayValidateParams<0, false, NoValidateParams> > >(
191 test_array.Pass(), mojo::internal::VALIDATION_ERROR_NONE);
193 test_array = Array<String>(3);
194 TestArrayWarning<ArrayValidateParams<0, false,
195 ArrayValidateParams<0, false, NoValidateParams> > >(
197 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER);
199 test_array = Array<String>(2);
200 TestArrayWarning<ArrayValidateParams<3, true,
201 ArrayValidateParams<0, false, NoValidateParams> > >(
203 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER);