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