OLD | NEW |
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 // Serialization warnings are only recorded in debug build. | 5 // Serialization warnings are only recorded in debug build. |
6 #ifndef NDEBUG | 6 #ifndef NDEBUG |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array.h" |
12 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 12 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
14 #include "mojo/public/cpp/bindings/lib/serialization.h" | 14 #include "mojo/public/cpp/bindings/lib/serialization.h" |
15 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 15 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
16 #include "mojo/public/cpp/bindings/string.h" | 16 #include "mojo/public/cpp/bindings/string.h" |
17 #include "mojo/public/cpp/system/message_pipe.h" | 17 #include "mojo/public/cpp/system/message_pipe.h" |
18 #include "mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom
.h" | 18 #include "mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom
.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace test { | 22 namespace test { |
23 namespace { | 23 namespace { |
24 | 24 |
25 using mojo::internal::ArrayValidateParams; | 25 using mojo::internal::ContainerValidateParams; |
26 | 26 |
27 // Creates an array of arrays of handles (2 X 3) for testing. | 27 // Creates an array of arrays of handles (2 X 3) for testing. |
28 Array<Array<ScopedHandle>> CreateTestNestedHandleArray() { | 28 Array<Array<ScopedHandle>> CreateTestNestedHandleArray() { |
29 Array<Array<ScopedHandle>> array(2); | 29 Array<Array<ScopedHandle>> array(2); |
30 for (size_t i = 0; i < array.size(); ++i) { | 30 for (size_t i = 0; i < array.size(); ++i) { |
31 Array<ScopedHandle> nested_array(3); | 31 Array<ScopedHandle> nested_array(3); |
32 for (size_t j = 0; j < nested_array.size(); ++j) { | 32 for (size_t j = 0; j < nested_array.size(); ++j) { |
33 MessagePipe pipe; | 33 MessagePipe pipe; |
34 nested_array[j] = ScopedHandle::From(std::move(pipe.handle1)); | 34 nested_array[j] = ScopedHandle::From(std::move(pipe.handle1)); |
35 } | 35 } |
(...skipping 30 matching lines...) Expand all Loading... |
66 mojo::internal::PrepareToSerialize<TPtr>(obj, &context)); | 66 mojo::internal::PrepareToSerialize<TPtr>(obj, &context)); |
67 typename T::Data_* data; | 67 typename T::Data_* data; |
68 mojo::internal::Serialize<TPtr>(obj, &buf, &data, &context); | 68 mojo::internal::Serialize<TPtr>(obj, &buf, &data, &context); |
69 | 69 |
70 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); | 70 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); |
71 } | 71 } |
72 | 72 |
73 template <typename T> | 73 template <typename T> |
74 void TestArrayWarning(T obj, | 74 void TestArrayWarning(T obj, |
75 mojo::internal::ValidationError expected_warning, | 75 mojo::internal::ValidationError expected_warning, |
76 const ArrayValidateParams* validate_params) { | 76 const ContainerValidateParams* validate_params) { |
77 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE); | 77 warning_observer_.set_last_warning(mojo::internal::VALIDATION_ERROR_NONE); |
78 | 78 |
79 mojo::internal::SerializationContext context; | 79 mojo::internal::SerializationContext context; |
80 mojo::internal::FixedBufferForTesting buf( | 80 mojo::internal::FixedBufferForTesting buf( |
81 mojo::internal::PrepareToSerialize<T>(obj, &context)); | 81 mojo::internal::PrepareToSerialize<T>(obj, &context)); |
82 typename T::Data_* data; | 82 typename T::Data_* data; |
83 mojo::internal::Serialize<T>(obj, &buf, &data, validate_params, &context); | 83 mojo::internal::Serialize<T>(obj, &buf, &data, validate_params, &context); |
84 | 84 |
85 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); | 85 EXPECT_EQ(expected_warning, warning_observer_.last_warning()); |
86 } | 86 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 test_struct->str = "hello world"; | 173 test_struct->str = "hello world"; |
174 | 174 |
175 TestWarning(std::move(test_struct), mojo::internal::VALIDATION_ERROR_NONE); | 175 TestWarning(std::move(test_struct), mojo::internal::VALIDATION_ERROR_NONE); |
176 } | 176 } |
177 | 177 |
178 TEST_F(SerializationWarningTest, ArrayOfArraysOfHandles) { | 178 TEST_F(SerializationWarningTest, ArrayOfArraysOfHandles) { |
179 Array<Array<ScopedHandle>> test_array = CreateTestNestedHandleArray(); | 179 Array<Array<ScopedHandle>> test_array = CreateTestNestedHandleArray(); |
180 test_array[0] = nullptr; | 180 test_array[0] = nullptr; |
181 test_array[1][0] = ScopedHandle(); | 181 test_array[1][0] = ScopedHandle(); |
182 | 182 |
183 ArrayValidateParams validate_params_0( | 183 ContainerValidateParams validate_params_0( |
184 0, true, new ArrayValidateParams(0, true, nullptr)); | 184 0, true, new ContainerValidateParams(0, true, nullptr)); |
185 TestArrayWarning(std::move(test_array), mojo::internal::VALIDATION_ERROR_NONE, | 185 TestArrayWarning(std::move(test_array), mojo::internal::VALIDATION_ERROR_NONE, |
186 &validate_params_0); | 186 &validate_params_0); |
187 | 187 |
188 test_array = CreateTestNestedHandleArray(); | 188 test_array = CreateTestNestedHandleArray(); |
189 test_array[0] = nullptr; | 189 test_array[0] = nullptr; |
190 ArrayValidateParams validate_params_1( | 190 ContainerValidateParams validate_params_1( |
191 0, false, new ArrayValidateParams(0, true, nullptr)); | 191 0, false, new ContainerValidateParams(0, true, nullptr)); |
192 TestArrayWarning(std::move(test_array), | 192 TestArrayWarning(std::move(test_array), |
193 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 193 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
194 &validate_params_1); | 194 &validate_params_1); |
195 | 195 |
196 test_array = CreateTestNestedHandleArray(); | 196 test_array = CreateTestNestedHandleArray(); |
197 test_array[1][0] = ScopedHandle(); | 197 test_array[1][0] = ScopedHandle(); |
198 ArrayValidateParams validate_params_2( | 198 ContainerValidateParams validate_params_2( |
199 0, true, new ArrayValidateParams(0, false, nullptr)); | 199 0, true, new ContainerValidateParams(0, false, nullptr)); |
200 TestArrayWarning(std::move(test_array), | 200 TestArrayWarning(std::move(test_array), |
201 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 201 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
202 &validate_params_2); | 202 &validate_params_2); |
203 } | 203 } |
204 | 204 |
205 TEST_F(SerializationWarningTest, ArrayOfStrings) { | 205 TEST_F(SerializationWarningTest, ArrayOfStrings) { |
206 Array<String> test_array(3); | 206 Array<String> test_array(3); |
207 for (size_t i = 0; i < test_array.size(); ++i) | 207 for (size_t i = 0; i < test_array.size(); ++i) |
208 test_array[i] = "hello"; | 208 test_array[i] = "hello"; |
209 | 209 |
210 ArrayValidateParams validate_params_0( | 210 ContainerValidateParams validate_params_0( |
211 0, true, new ArrayValidateParams(0, false, nullptr)); | 211 0, true, new ContainerValidateParams(0, false, nullptr)); |
212 TestArrayWarning(std::move(test_array), mojo::internal::VALIDATION_ERROR_NONE, | 212 TestArrayWarning(std::move(test_array), mojo::internal::VALIDATION_ERROR_NONE, |
213 &validate_params_0); | 213 &validate_params_0); |
214 | 214 |
215 test_array = Array<String>(3); | 215 test_array = Array<String>(3); |
216 for (size_t i = 0; i < test_array.size(); ++i) | 216 for (size_t i = 0; i < test_array.size(); ++i) |
217 test_array[i] = nullptr; | 217 test_array[i] = nullptr; |
218 ArrayValidateParams validate_params_1( | 218 ContainerValidateParams validate_params_1( |
219 0, false, new ArrayValidateParams(0, false, nullptr)); | 219 0, false, new ContainerValidateParams(0, false, nullptr)); |
220 TestArrayWarning(std::move(test_array), | 220 TestArrayWarning(std::move(test_array), |
221 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 221 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
222 &validate_params_1); | 222 &validate_params_1); |
223 | 223 |
224 test_array = Array<String>(2); | 224 test_array = Array<String>(2); |
225 ArrayValidateParams validate_params_2( | 225 ContainerValidateParams validate_params_2( |
226 3, true, new ArrayValidateParams(0, false, nullptr)); | 226 3, true, new ContainerValidateParams(0, false, nullptr)); |
227 TestArrayWarning(std::move(test_array), | 227 TestArrayWarning(std::move(test_array), |
228 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, | 228 mojo::internal::VALIDATION_ERROR_UNEXPECTED_ARRAY_HEADER, |
229 &validate_params_2); | 229 &validate_params_2); |
230 } | 230 } |
231 | 231 |
232 } // namespace | 232 } // namespace |
233 } // namespace test | 233 } // namespace test |
234 } // namespace mojo | 234 } // namespace mojo |
235 | 235 |
236 #endif | 236 #endif |
OLD | NEW |