More explicit thread checking in SafeBrowsingDatabase.
[chromium-blink-merge.git] / mojo / edk / system / data_pipe_unittest.cc
blobad02222ddb1c19ed33087fd445510abff5ce2190
1 // Copyright 2013 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.
5 #include "mojo/edk/system/data_pipe.h"
7 #include <stddef.h>
8 #include <stdint.h>
10 #include <limits>
12 #include "mojo/edk/system/configuration.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace mojo {
16 namespace system {
17 namespace {
19 const uint32_t kSizeOfCreateOptions =
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions));
22 // Does a cursory sanity check of |validated_options|. Calls
23 // |ValidateCreateOptions()| on already-validated options. The validated options
24 // should be valid, and the revalidated copy should be the same.
25 void RevalidateCreateOptions(
26 const MojoCreateDataPipeOptions& validated_options) {
27 EXPECT_EQ(kSizeOfCreateOptions, validated_options.struct_size);
28 // Nothing to check for flags.
29 EXPECT_GT(validated_options.element_num_bytes, 0u);
30 EXPECT_GT(validated_options.capacity_num_bytes, 0u);
31 EXPECT_EQ(0u, validated_options.capacity_num_bytes %
32 validated_options.element_num_bytes);
34 MojoCreateDataPipeOptions revalidated_options = {};
35 EXPECT_EQ(MOJO_RESULT_OK,
36 DataPipe::ValidateCreateOptions(MakeUserPointer(&validated_options),
37 &revalidated_options));
38 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size);
39 EXPECT_EQ(validated_options.element_num_bytes,
40 revalidated_options.element_num_bytes);
41 EXPECT_EQ(validated_options.capacity_num_bytes,
42 revalidated_options.capacity_num_bytes);
43 EXPECT_EQ(validated_options.flags, revalidated_options.flags);
46 // Checks that a default-computed capacity is correct. (Does not duplicate the
47 // checks done by |RevalidateCreateOptions()|.)
48 void CheckDefaultCapacity(const MojoCreateDataPipeOptions& validated_options) {
49 EXPECT_LE(validated_options.capacity_num_bytes,
50 GetConfiguration().default_data_pipe_capacity_bytes);
51 EXPECT_GT(validated_options.capacity_num_bytes +
52 validated_options.element_num_bytes,
53 GetConfiguration().default_data_pipe_capacity_bytes);
56 // Tests valid inputs to |ValidateCreateOptions()|.
57 TEST(DataPipeTest, ValidateCreateOptionsValid) {
58 // Default options.
60 MojoCreateDataPipeOptions validated_options = {};
61 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions(
62 NullUserPointer(), &validated_options));
63 RevalidateCreateOptions(validated_options);
64 CheckDefaultCapacity(validated_options);
67 // Size member, but nothing beyond.
69 MojoCreateDataPipeOptions options = {
70 offsetof(MojoCreateDataPipeOptions, flags) // |struct_size|.
72 MojoCreateDataPipeOptions validated_options = {};
73 EXPECT_EQ(MOJO_RESULT_OK,
74 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
75 &validated_options));
76 RevalidateCreateOptions(validated_options);
77 CheckDefaultCapacity(validated_options);
80 // Different flags.
81 MojoCreateDataPipeOptionsFlags flags_values[] = {
82 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
83 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD};
84 for (size_t i = 0; i < arraysize(flags_values); i++) {
85 const MojoCreateDataPipeOptionsFlags flags = flags_values[i];
87 // Flags member, but nothing beyond.
89 MojoCreateDataPipeOptions options = {
90 // |struct_size|.
91 offsetof(MojoCreateDataPipeOptions, element_num_bytes),
92 flags // |flags|.
94 MojoCreateDataPipeOptions validated_options = {};
95 EXPECT_EQ(MOJO_RESULT_OK,
96 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
97 &validated_options));
98 RevalidateCreateOptions(validated_options);
99 EXPECT_EQ(options.flags, validated_options.flags);
100 CheckDefaultCapacity(validated_options);
103 // Different capacities (size 1).
104 for (uint32_t capacity = 1; capacity <= 100 * 1000 * 1000; capacity *= 10) {
105 MojoCreateDataPipeOptions options = {
106 kSizeOfCreateOptions, // |struct_size|.
107 flags, // |flags|.
108 1, // |element_num_bytes|.
109 capacity // |capacity_num_bytes|.
111 MojoCreateDataPipeOptions validated_options = {};
112 EXPECT_EQ(MOJO_RESULT_OK,
113 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
114 &validated_options))
115 << capacity;
116 RevalidateCreateOptions(validated_options);
117 EXPECT_EQ(options.flags, validated_options.flags);
118 EXPECT_EQ(options.element_num_bytes, validated_options.element_num_bytes);
119 EXPECT_EQ(options.capacity_num_bytes,
120 validated_options.capacity_num_bytes);
123 // Small sizes.
124 for (uint32_t size = 1; size < 100; size++) {
125 // Different capacities.
126 for (uint32_t elements = 1; elements <= 1000 * 1000; elements *= 10) {
127 MojoCreateDataPipeOptions options = {
128 kSizeOfCreateOptions, // |struct_size|.
129 flags, // |flags|.
130 size, // |element_num_bytes|.
131 size * elements // |capacity_num_bytes|.
133 MojoCreateDataPipeOptions validated_options = {};
134 EXPECT_EQ(MOJO_RESULT_OK,
135 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
136 &validated_options))
137 << size << ", " << elements;
138 RevalidateCreateOptions(validated_options);
139 EXPECT_EQ(options.flags, validated_options.flags);
140 EXPECT_EQ(options.element_num_bytes,
141 validated_options.element_num_bytes);
142 EXPECT_EQ(options.capacity_num_bytes,
143 validated_options.capacity_num_bytes);
146 // Default capacity.
148 MojoCreateDataPipeOptions options = {
149 kSizeOfCreateOptions, // |struct_size|.
150 flags, // |flags|.
151 size, // |element_num_bytes|.
152 0 // |capacity_num_bytes|.
154 MojoCreateDataPipeOptions validated_options = {};
155 EXPECT_EQ(MOJO_RESULT_OK,
156 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
157 &validated_options))
158 << size;
159 RevalidateCreateOptions(validated_options);
160 EXPECT_EQ(options.flags, validated_options.flags);
161 EXPECT_EQ(options.element_num_bytes,
162 validated_options.element_num_bytes);
163 CheckDefaultCapacity(validated_options);
166 // No capacity field.
168 MojoCreateDataPipeOptions options = {
169 // |struct_size|.
170 offsetof(MojoCreateDataPipeOptions, capacity_num_bytes),
171 flags, // |flags|.
172 size // |element_num_bytes|.
174 MojoCreateDataPipeOptions validated_options = {};
175 EXPECT_EQ(MOJO_RESULT_OK,
176 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
177 &validated_options))
178 << size;
179 RevalidateCreateOptions(validated_options);
180 EXPECT_EQ(options.flags, validated_options.flags);
181 EXPECT_EQ(options.element_num_bytes,
182 validated_options.element_num_bytes);
183 CheckDefaultCapacity(validated_options);
187 // Larger sizes.
188 for (uint32_t size = 100; size <= 100 * 1000; size *= 10) {
189 // Capacity of 1000 elements.
191 MojoCreateDataPipeOptions options = {
192 kSizeOfCreateOptions, // |struct_size|.
193 flags, // |flags|.
194 size, // |element_num_bytes|.
195 1000 * size // |capacity_num_bytes|.
197 MojoCreateDataPipeOptions validated_options = {};
198 EXPECT_EQ(MOJO_RESULT_OK,
199 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
200 &validated_options))
201 << size;
202 RevalidateCreateOptions(validated_options);
203 EXPECT_EQ(options.flags, validated_options.flags);
204 EXPECT_EQ(options.element_num_bytes,
205 validated_options.element_num_bytes);
206 EXPECT_EQ(options.capacity_num_bytes,
207 validated_options.capacity_num_bytes);
210 // Default capacity.
212 MojoCreateDataPipeOptions options = {
213 kSizeOfCreateOptions, // |struct_size|.
214 flags, // |flags|.
215 size, // |element_num_bytes|.
216 0 // |capacity_num_bytes|.
218 MojoCreateDataPipeOptions validated_options = {};
219 EXPECT_EQ(MOJO_RESULT_OK,
220 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
221 &validated_options))
222 << size;
223 RevalidateCreateOptions(validated_options);
224 EXPECT_EQ(options.flags, validated_options.flags);
225 EXPECT_EQ(options.element_num_bytes,
226 validated_options.element_num_bytes);
227 CheckDefaultCapacity(validated_options);
230 // No capacity field.
232 MojoCreateDataPipeOptions options = {
233 // |struct_size|.
234 offsetof(MojoCreateDataPipeOptions, capacity_num_bytes),
235 flags, // |flags|.
236 size // |element_num_bytes|.
238 MojoCreateDataPipeOptions validated_options = {};
239 EXPECT_EQ(MOJO_RESULT_OK,
240 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
241 &validated_options))
242 << size;
243 RevalidateCreateOptions(validated_options);
244 EXPECT_EQ(options.flags, validated_options.flags);
245 EXPECT_EQ(options.element_num_bytes,
246 validated_options.element_num_bytes);
247 CheckDefaultCapacity(validated_options);
253 TEST(DataPipeTest, ValidateCreateOptionsInvalid) {
254 // Invalid |struct_size|.
256 MojoCreateDataPipeOptions options = {
257 1, // |struct_size|.
258 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
259 1, // |element_num_bytes|.
260 0 // |capacity_num_bytes|.
262 MojoCreateDataPipeOptions unused;
263 EXPECT_EQ(
264 MOJO_RESULT_INVALID_ARGUMENT,
265 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
268 // Unknown |flags|.
270 MojoCreateDataPipeOptions options = {
271 kSizeOfCreateOptions, // |struct_size|.
272 ~0u, // |flags|.
273 1, // |element_num_bytes|.
274 0 // |capacity_num_bytes|.
276 MojoCreateDataPipeOptions unused;
277 EXPECT_EQ(
278 MOJO_RESULT_UNIMPLEMENTED,
279 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
282 // Invalid |element_num_bytes|.
284 MojoCreateDataPipeOptions options = {
285 kSizeOfCreateOptions, // |struct_size|.
286 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
287 0, // |element_num_bytes|.
288 1000 // |capacity_num_bytes|.
290 MojoCreateDataPipeOptions unused;
291 EXPECT_EQ(
292 MOJO_RESULT_INVALID_ARGUMENT,
293 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
295 // |element_num_bytes| too big.
297 MojoCreateDataPipeOptions options = {
298 kSizeOfCreateOptions, // |struct_size|.
299 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
300 std::numeric_limits<uint32_t>::max(), // |element_num_bytes|.
301 std::numeric_limits<uint32_t>::max() // |capacity_num_bytes|.
303 MojoCreateDataPipeOptions unused;
304 EXPECT_EQ(
305 MOJO_RESULT_RESOURCE_EXHAUSTED,
306 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
309 MojoCreateDataPipeOptions options = {
310 kSizeOfCreateOptions, // |struct_size|.
311 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
312 std::numeric_limits<uint32_t>::max() - 1000, // |element_num_bytes|.
313 std::numeric_limits<uint32_t>::max() - 1000 // |capacity_num_bytes|.
315 MojoCreateDataPipeOptions unused;
316 EXPECT_EQ(
317 MOJO_RESULT_RESOURCE_EXHAUSTED,
318 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
321 // Invalid |capacity_num_bytes|.
323 MojoCreateDataPipeOptions options = {
324 kSizeOfCreateOptions, // |struct_size|.
325 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
326 2, // |element_num_bytes|.
327 1 // |capacity_num_bytes|.
329 MojoCreateDataPipeOptions unused;
330 EXPECT_EQ(
331 MOJO_RESULT_INVALID_ARGUMENT,
332 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
335 MojoCreateDataPipeOptions options = {
336 kSizeOfCreateOptions, // |struct_size|.
337 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
338 2, // |element_num_bytes|.
339 111 // |capacity_num_bytes|.
341 MojoCreateDataPipeOptions unused;
342 EXPECT_EQ(
343 MOJO_RESULT_INVALID_ARGUMENT,
344 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
347 MojoCreateDataPipeOptions options = {
348 kSizeOfCreateOptions, // |struct_size|.
349 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
350 5, // |element_num_bytes|.
351 104 // |capacity_num_bytes|.
353 MojoCreateDataPipeOptions unused;
354 EXPECT_EQ(
355 MOJO_RESULT_INVALID_ARGUMENT,
356 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
358 // |capacity_num_bytes| too big.
360 MojoCreateDataPipeOptions options = {
361 kSizeOfCreateOptions, // |struct_size|.
362 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
363 8, // |element_num_bytes|.
364 0xffff0000 // |capacity_num_bytes|.
366 MojoCreateDataPipeOptions unused;
367 EXPECT_EQ(
368 MOJO_RESULT_RESOURCE_EXHAUSTED,
369 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), &unused));
373 } // namespace
374 } // namespace system
375 } // namespace mojo