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