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"
12 #include "mojo/edk/system/configuration.h"
13 #include "testing/gtest/include/gtest/gtest.h"
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
) {
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
),
76 RevalidateCreateOptions(validated_options
);
77 CheckDefaultCapacity(validated_options
);
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
= {
91 offsetof(MojoCreateDataPipeOptions
, element_num_bytes
),
94 MojoCreateDataPipeOptions validated_options
= {};
95 EXPECT_EQ(MOJO_RESULT_OK
,
96 DataPipe::ValidateCreateOptions(MakeUserPointer(&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|.
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
),
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
);
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|.
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
),
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
);
148 MojoCreateDataPipeOptions options
= {
149 kSizeOfCreateOptions
, // |struct_size|.
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
),
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
= {
170 offsetof(MojoCreateDataPipeOptions
, capacity_num_bytes
),
172 size
// |element_num_bytes|.
174 MojoCreateDataPipeOptions validated_options
= {};
175 EXPECT_EQ(MOJO_RESULT_OK
,
176 DataPipe::ValidateCreateOptions(MakeUserPointer(&options
),
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
);
188 for (uint32_t size
= 100; size
<= 100 * 1000; size
*= 10) {
189 // Capacity of 1000 elements.
191 MojoCreateDataPipeOptions options
= {
192 kSizeOfCreateOptions
, // |struct_size|.
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
),
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
);
212 MojoCreateDataPipeOptions options
= {
213 kSizeOfCreateOptions
, // |struct_size|.
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
),
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
= {
234 offsetof(MojoCreateDataPipeOptions
, capacity_num_bytes
),
236 size
// |element_num_bytes|.
238 MojoCreateDataPipeOptions validated_options
= {};
239 EXPECT_EQ(MOJO_RESULT_OK
,
240 DataPipe::ValidateCreateOptions(MakeUserPointer(&options
),
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
= {
258 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
, // |flags|.
259 1, // |element_num_bytes|.
260 0 // |capacity_num_bytes|.
262 MojoCreateDataPipeOptions unused
;
264 MOJO_RESULT_INVALID_ARGUMENT
,
265 DataPipe::ValidateCreateOptions(MakeUserPointer(&options
), &unused
));
270 MojoCreateDataPipeOptions options
= {
271 kSizeOfCreateOptions
, // |struct_size|.
273 1, // |element_num_bytes|.
274 0 // |capacity_num_bytes|.
276 MojoCreateDataPipeOptions unused
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
368 MOJO_RESULT_RESOURCE_EXHAUSTED
,
369 DataPipe::ValidateCreateOptions(MakeUserPointer(&options
), &unused
));
374 } // namespace system