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