OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Serialization warnings are only recorded in debug build. | |
6 #ifndef NDEBUG | |
7 | |
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" | |
18 | |
19 namespace mojo { | |
20 namespace test { | |
21 namespace { | |
22 | |
23 using mojo::internal::ArrayValidateParams; | |
24 using mojo::internal::NoValidateParams; | |
25 | |
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) { | |
32 MessagePipe pipe; | |
33 nested_array[j] = ScopedHandle::From(pipe.handle1.Pass()); | |
34 } | |
35 array[i] = nested_array.Pass(); | |
36 } | |
37 | |
38 return array.Pass(); | |
39 } | |
40 | |
41 class SerializationWarningTest : public testing::Test { | |
42 public: | |
43 ~SerializationWarningTest() override {} | |
44 | |
45 protected: | |
46 template <typename T> | |
47 void TestWarning(StructPtr<T> obj, | |
48 mojo::internal::ValidationError expected_warning) { | |
49 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE); | |
50 | |
51 mojo::internal::FixedBuffer buf(GetSerializedSize_(obj)); | |
52 typename T::Data_* data; | |
53 Serialize_(obj.Pass(), &buf, &data); | |
54 | |
55 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); | |
56 } | |
57 | |
58 template <typename ValidateParams, typename T> | |
59 void TestArrayWarning(T obj, | |
60 mojo::internal::ValidationError expected_warning) { | |
61 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE); | |
62 | |
63 mojo::internal::FixedBuffer buf(GetSerializedSize_(obj)); | |
64 typename T::Data_* data; | |
65 SerializeArray_<ValidateParams>(obj.Pass(), &buf, &data); | |
66 | |
67 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); | |
68 } | |
69 | |
70 mojo::internal::SerializationWarningObserverForTesting warning_observer_; | |
71 Environment env_; | |
72 }; | |
73 | |
74 TEST_F(SerializationWarningTest, HandleInStruct) { | |
75 Struct2Ptr test_struct(Struct2::New()); | |
76 EXPECT_FALSE(test_struct->hdl.is_valid()); | |
77 | |
78 TestWarning(test_struct.Pass(), | |
79 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE); | |
80 | |
81 test_struct = Struct2::New(); | |
82 MessagePipe pipe; | |
83 test_struct->hdl = ScopedHandle::From(pipe.handle1.Pass()); | |
84 | |
85 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
86 } | |
87 | |
88 TEST_F(SerializationWarningTest, StructInStruct) { | |
89 Struct3Ptr test_struct(Struct3::New()); | |
90 EXPECT_TRUE(!test_struct->struct_1); | |
91 | |
92 TestWarning(test_struct.Pass(), | |
93 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
94 | |
95 test_struct = Struct3::New(); | |
96 test_struct->struct_1 = Struct1::New(); | |
97 | |
98 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
99 } | |
100 | |
101 TEST_F(SerializationWarningTest, ArrayOfStructsInStruct) { | |
102 Struct4Ptr test_struct(Struct4::New()); | |
103 EXPECT_TRUE(!test_struct->data); | |
104 | |
105 TestWarning(test_struct.Pass(), | |
106 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
107 | |
108 test_struct = Struct4::New(); | |
109 test_struct->data.resize(1); | |
110 | |
111 TestWarning(test_struct.Pass(), | |
112 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
113 | |
114 test_struct = Struct4::New(); | |
115 test_struct->data.resize(0); | |
116 | |
117 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
118 | |
119 test_struct = Struct4::New(); | |
120 test_struct->data.resize(1); | |
121 test_struct->data[0] = Struct1::New(); | |
122 | |
123 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
124 } | |
125 | |
126 TEST_F(SerializationWarningTest, FixedArrayOfStructsInStruct) { | |
127 Struct5Ptr test_struct(Struct5::New()); | |
128 EXPECT_TRUE(!test_struct->pair); | |
129 | |
130 TestWarning(test_struct.Pass(), | |
131 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
132 | |
133 test_struct = Struct5::New(); | |
134 test_struct->pair.resize(1); | |
135 test_struct->pair[0] = Struct1::New(); | |
136 | |
137 TestWarning(test_struct.Pass(), | |
138 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER); | |
139 | |
140 test_struct = Struct5::New(); | |
141 test_struct->pair.resize(2); | |
142 test_struct->pair[0] = Struct1::New(); | |
143 test_struct->pair[1] = Struct1::New(); | |
144 | |
145 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
146 } | |
147 | |
148 TEST_F(SerializationWarningTest, StringInStruct) { | |
149 Struct6Ptr test_struct(Struct6::New()); | |
150 EXPECT_TRUE(!test_struct->str); | |
151 | |
152 TestWarning(test_struct.Pass(), | |
153 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
154 | |
155 test_struct = Struct6::New(); | |
156 test_struct->str = "hello world"; | |
157 | |
158 TestWarning(test_struct.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
159 } | |
160 | |
161 TEST_F(SerializationWarningTest, ArrayOfArraysOfHandles) { | |
162 Array<Array<ScopedHandle>> test_array = CreateTestNestedHandleArray(); | |
163 test_array[0] = Array<ScopedHandle>(); | |
164 test_array[1][0] = ScopedHandle(); | |
165 | |
166 TestArrayWarning< | |
167 ArrayValidateParams<0, | |
168 true, | |
169 ArrayValidateParams<0, true, NoValidateParams>>>( | |
170 test_array.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
171 | |
172 test_array = CreateTestNestedHandleArray(); | |
173 test_array[0] = Array<ScopedHandle>(); | |
174 TestArrayWarning< | |
175 ArrayValidateParams<0, | |
176 false, | |
177 ArrayValidateParams<0, true, NoValidateParams>>>( | |
178 test_array.Pass(), | |
179 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
180 | |
181 test_array = CreateTestNestedHandleArray(); | |
182 test_array[1][0] = ScopedHandle(); | |
183 TestArrayWarning< | |
184 ArrayValidateParams<0, | |
185 true, | |
186 ArrayValidateParams<0, false, NoValidateParams>>>( | |
187 test_array.Pass(), | |
188 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE); | |
189 } | |
190 | |
191 TEST_F(SerializationWarningTest, ArrayOfStrings) { | |
192 Array<String> test_array(3); | |
193 for (size_t i = 0; i < test_array.size(); ++i) | |
194 test_array[i] = "hello"; | |
195 | |
196 TestArrayWarning< | |
197 ArrayValidateParams<0, | |
198 true, | |
199 ArrayValidateParams<0, false, NoValidateParams>>>( | |
200 test_array.Pass(), mojo::internal::VALIDATION_ERROR_NONE); | |
201 | |
202 test_array = Array<String>(3); | |
203 TestArrayWarning< | |
204 ArrayValidateParams<0, | |
205 false, | |
206 ArrayValidateParams<0, false, NoValidateParams>>>( | |
207 test_array.Pass(), | |
208 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER); | |
209 | |
210 test_array = Array<String>(2); | |
211 TestArrayWarning< | |
212 ArrayValidateParams<3, | |
213 true, | |
214 ArrayValidateParams<0, false, NoValidateParams>>>( | |
215 test_array.Pass(), | |
216 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER); | |
217 } | |
218 | |
219 } // namespace | |
220 } // namespace test | |
221 } // namespace mojo | |
222 | |
223 #endif | |
OLD | NEW |