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