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/local_data_pipe.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "mojo/edk/system/data_pipe.h"
12 #include "mojo/edk/system/waiter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 const uint32_t kSizeOfOptions =
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions));
23 TEST(LocalDataPipeTest, Creation) {
24 // Create using default options.
26 // Get default options.
27 MojoCreateDataPipeOptions default_options = {0};
30 DataPipe::ValidateCreateOptions(NullUserPointer(), &default_options));
31 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options));
36 // Create using non-default options.
38 const MojoCreateDataPipeOptions options = {
39 kSizeOfOptions, // |struct_size|.
40 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
41 1, // |element_num_bytes|.
42 1000 // |capacity_num_bytes|.
44 MojoCreateDataPipeOptions validated_options = {0};
45 EXPECT_EQ(MOJO_RESULT_OK,
46 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
48 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
53 const MojoCreateDataPipeOptions options = {
54 kSizeOfOptions, // |struct_size|.
55 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
56 4, // |element_num_bytes|.
57 4000 // |capacity_num_bytes|.
59 MojoCreateDataPipeOptions validated_options = {0};
60 EXPECT_EQ(MOJO_RESULT_OK,
61 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
63 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
68 const MojoCreateDataPipeOptions options = {
69 kSizeOfOptions, // |struct_size|.
70 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
71 7, // |element_num_bytes|.
72 7000000 // |capacity_num_bytes|.
74 MojoCreateDataPipeOptions validated_options = {0};
75 EXPECT_EQ(MOJO_RESULT_OK,
76 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
78 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
84 const MojoCreateDataPipeOptions options = {
85 kSizeOfOptions, // |struct_size|.
86 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
87 100, // |element_num_bytes|.
88 0 // |capacity_num_bytes|.
90 MojoCreateDataPipeOptions validated_options = {0};
91 EXPECT_EQ(MOJO_RESULT_OK,
92 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
94 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
100 TEST(LocalDataPipeTest, SimpleReadWrite) {
101 const MojoCreateDataPipeOptions options = {
102 kSizeOfOptions, // |struct_size|.
103 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
104 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
105 1000 * sizeof(int32_t) // |capacity_num_bytes|.
107 MojoCreateDataPipeOptions validated_options = {0};
108 EXPECT_EQ(MOJO_RESULT_OK,
109 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
110 &validated_options));
112 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
114 int32_t elements[10] = {0};
115 uint32_t num_bytes = 0;
117 // Try reading; nothing there yet.
118 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0]));
120 MOJO_RESULT_SHOULD_WAIT,
121 dp->ConsumerReadData(UserPointer<void>(elements),
122 MakeUserPointer(&num_bytes),
126 // Query; nothing there yet.
128 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
129 EXPECT_EQ(0u, num_bytes);
131 // Discard; nothing there yet.
132 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0]));
133 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
134 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false));
136 // Read with invalid |num_bytes|.
137 num_bytes = sizeof(elements[0]) + 1;
139 MOJO_RESULT_INVALID_ARGUMENT,
140 dp->ConsumerReadData(UserPointer<void>(elements),
141 MakeUserPointer(&num_bytes),
145 // Write two elements.
148 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
149 EXPECT_EQ(MOJO_RESULT_OK,
150 dp->ProducerWriteData(UserPointer<const void>(elements),
151 MakeUserPointer(&num_bytes),
153 // It should have written everything (even without "all or none").
154 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes);
158 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
159 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes);
164 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
167 dp->ConsumerReadData(UserPointer<void>(elements),
168 MakeUserPointer(&num_bytes),
171 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
172 EXPECT_EQ(123, elements[0]);
173 EXPECT_EQ(-1, elements[1]);
177 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
178 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
183 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
186 dp->ConsumerReadData(UserPointer<void>(elements),
187 MakeUserPointer(&num_bytes),
190 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
191 EXPECT_EQ(456, elements[0]);
192 EXPECT_EQ(-1, elements[1]);
194 // Query. Still has 1 element remaining.
196 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
197 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
199 // Try to read two elements, with "all or none".
202 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
204 MOJO_RESULT_OUT_OF_RANGE,
205 dp->ConsumerReadData(UserPointer<void>(elements),
206 MakeUserPointer(&num_bytes),
209 EXPECT_EQ(-1, elements[0]);
210 EXPECT_EQ(-1, elements[1]);
212 // Try to read two elements, without "all or none".
215 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
218 dp->ConsumerReadData(UserPointer<void>(elements),
219 MakeUserPointer(&num_bytes),
222 EXPECT_EQ(456, elements[0]);
223 EXPECT_EQ(-1, elements[1]);
227 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
228 EXPECT_EQ(0u, num_bytes);
234 // Note: The "basic" waiting tests test that the "wait states" are correct in
235 // various situations; they don't test that waiters are properly awoken on state
236 // changes. (For that, we need to use multiple threads.)
237 TEST(LocalDataPipeTest, BasicProducerWaiting) {
238 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities
239 // are strict maximums. This is not guaranteed by the API.
241 const MojoCreateDataPipeOptions options = {
242 kSizeOfOptions, // |struct_size|.
243 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
244 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
245 2 * sizeof(int32_t) // |capacity_num_bytes|.
247 MojoCreateDataPipeOptions validated_options = {0};
248 EXPECT_EQ(MOJO_RESULT_OK,
249 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
250 &validated_options));
252 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
254 uint32_t context = 0;
255 HandleSignalsState hss;
259 hss = HandleSignalsState();
261 MOJO_RESULT_FAILED_PRECONDITION,
262 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss));
263 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
264 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
268 hss = HandleSignalsState();
270 MOJO_RESULT_ALREADY_EXISTS,
271 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss));
273 // Write two elements.
274 int32_t elements[2] = {123, 456};
275 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
276 EXPECT_EQ(MOJO_RESULT_OK,
277 dp->ProducerWriteData(UserPointer<const void>(elements),
278 MakeUserPointer(&num_bytes),
280 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
282 // Adding a waiter should now succeed.
286 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr));
287 // And it shouldn't be writable yet.
288 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
289 hss = HandleSignalsState();
290 dp->ProducerRemoveWaiter(&waiter, &hss);
291 EXPECT_EQ(0u, hss.satisfied_signals);
292 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
297 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
300 dp->ConsumerReadData(UserPointer<void>(elements),
301 MakeUserPointer(&num_bytes),
304 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
305 EXPECT_EQ(123, elements[0]);
306 EXPECT_EQ(-1, elements[1]);
312 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56, nullptr));
313 // And it still shouldn't be writable yet.
314 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
315 hss = HandleSignalsState();
316 dp->ProducerRemoveWaiter(&waiter, &hss);
317 EXPECT_EQ(0u, hss.satisfied_signals);
318 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
324 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, nullptr));
329 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
332 dp->ConsumerReadData(UserPointer<void>(elements),
333 MakeUserPointer(&num_bytes),
336 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
337 EXPECT_EQ(123, elements[0]);
338 EXPECT_EQ(-1, elements[1]);
340 // Waiting should now succeed.
341 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context));
342 EXPECT_EQ(78u, context);
343 hss = HandleSignalsState();
344 dp->ProducerRemoveWaiter(&waiter, &hss);
345 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
346 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
348 // Try writing, using a two-phase write.
349 void* buffer = nullptr;
350 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
351 EXPECT_EQ(MOJO_RESULT_OK,
352 dp->ProducerBeginWriteData(
353 MakeUserPointer(&buffer), MakeUserPointer(&num_bytes), false));
355 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
357 static_cast<int32_t*>(buffer)[0] = 789;
358 EXPECT_EQ(MOJO_RESULT_OK,
359 dp->ProducerEndWriteData(
360 static_cast<uint32_t>(1u * sizeof(elements[0]))));
366 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, nullptr));
368 // Read one element, using a two-phase read.
369 const void* read_buffer = nullptr;
373 dp->ConsumerBeginReadData(
374 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), false));
375 EXPECT_TRUE(read_buffer);
376 // Since we only read one element (after having written three in all), the
377 // two-phase read should only allow us to read one. This checks an
378 // implementation detail!
379 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
380 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]);
383 dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0]))));
385 // Waiting should succeed.
386 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context));
387 EXPECT_EQ(90u, context);
388 hss = HandleSignalsState();
389 dp->ProducerRemoveWaiter(&waiter, &hss);
390 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
391 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
393 // Write one element.
395 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
396 EXPECT_EQ(MOJO_RESULT_OK,
397 dp->ProducerWriteData(UserPointer<const void>(elements),
398 MakeUserPointer(&num_bytes),
400 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
406 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, nullptr));
408 // Close the consumer.
411 // It should now be never-writable.
412 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context));
413 EXPECT_EQ(12u, context);
414 hss = HandleSignalsState();
415 dp->ProducerRemoveWaiter(&waiter, &hss);
416 EXPECT_EQ(0u, hss.satisfied_signals);
417 EXPECT_EQ(0u, hss.satisfiable_signals);
422 TEST(LocalDataPipeTest, BasicConsumerWaiting) {
423 const MojoCreateDataPipeOptions options = {
424 kSizeOfOptions, // |struct_size|.
425 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
426 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
427 1000 * sizeof(int32_t) // |capacity_num_bytes|.
429 MojoCreateDataPipeOptions validated_options = {0};
430 EXPECT_EQ(MOJO_RESULT_OK,
431 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
432 &validated_options));
435 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
437 uint32_t context = 0;
438 HandleSignalsState hss;
442 hss = HandleSignalsState();
444 MOJO_RESULT_FAILED_PRECONDITION,
445 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, &hss));
446 EXPECT_EQ(0u, hss.satisfied_signals);
447 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
451 ASSERT_EQ(MOJO_RESULT_OK,
452 dp->ConsumerAddWaiter(
453 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, nullptr));
454 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
455 hss = HandleSignalsState();
456 dp->ConsumerRemoveWaiter(&waiter, &hss);
457 EXPECT_EQ(0u, hss.satisfied_signals);
458 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
460 // Write two elements.
461 int32_t elements[2] = {123, 456};
462 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
463 EXPECT_EQ(MOJO_RESULT_OK,
464 dp->ProducerWriteData(UserPointer<const void>(elements),
465 MakeUserPointer(&num_bytes),
468 // Should already be readable.
470 hss = HandleSignalsState();
472 MOJO_RESULT_ALREADY_EXISTS,
473 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, &hss));
474 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
475 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
477 // Discard one element.
478 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
479 EXPECT_EQ(MOJO_RESULT_OK,
480 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
481 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
483 // Should still be readable.
485 hss = HandleSignalsState();
487 MOJO_RESULT_ALREADY_EXISTS,
488 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss));
489 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
490 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
495 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
498 dp->ConsumerReadData(UserPointer<void>(elements),
499 MakeUserPointer(&num_bytes),
502 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
503 EXPECT_EQ(456, elements[0]);
504 EXPECT_EQ(-1, elements[1]);
506 // Should still be readable.
508 hss = HandleSignalsState();
510 MOJO_RESULT_ALREADY_EXISTS,
511 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78, &hss));
512 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
513 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
518 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
521 dp->ConsumerReadData(UserPointer<void>(elements),
522 MakeUserPointer(&num_bytes),
525 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
526 EXPECT_EQ(456, elements[0]);
527 EXPECT_EQ(-1, elements[1]);
529 // Adding a waiter should now succeed.
531 ASSERT_EQ(MOJO_RESULT_OK,
532 dp->ConsumerAddWaiter(
533 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, nullptr));
535 // Write one element.
538 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
539 EXPECT_EQ(MOJO_RESULT_OK,
540 dp->ProducerWriteData(UserPointer<const void>(elements),
541 MakeUserPointer(&num_bytes),
544 // Waiting should now succeed.
545 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context));
546 EXPECT_EQ(90u, context);
547 hss = HandleSignalsState();
548 dp->ConsumerRemoveWaiter(&waiter, &hss);
549 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
550 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
552 // Close the producer.
555 // Should still be readable.
557 hss = HandleSignalsState();
559 MOJO_RESULT_ALREADY_EXISTS,
560 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss));
561 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
562 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
567 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
570 dp->ConsumerReadData(UserPointer<void>(elements),
571 MakeUserPointer(&num_bytes),
574 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
575 EXPECT_EQ(789, elements[0]);
576 EXPECT_EQ(-1, elements[1]);
578 // Should be never-readable.
580 hss = HandleSignalsState();
582 MOJO_RESULT_FAILED_PRECONDITION,
583 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss));
584 EXPECT_EQ(0u, hss.satisfied_signals);
585 EXPECT_EQ(0u, hss.satisfiable_signals);
590 // Test with two-phase APIs and closing the producer with an active consumer
593 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
595 uint32_t context = 0;
596 HandleSignalsState hss;
598 // Write two elements.
599 int32_t* elements = nullptr;
600 void* buffer = nullptr;
601 // Request room for three (but we'll only write two).
602 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
603 EXPECT_EQ(MOJO_RESULT_OK,
604 dp->ProducerBeginWriteData(
605 MakeUserPointer(&buffer), MakeUserPointer(&num_bytes), true));
607 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0])));
608 elements = static_cast<int32_t*>(buffer);
611 EXPECT_EQ(MOJO_RESULT_OK,
612 dp->ProducerEndWriteData(
613 static_cast<uint32_t>(2u * sizeof(elements[0]))));
615 // Should already be readable.
617 hss = HandleSignalsState();
619 MOJO_RESULT_ALREADY_EXISTS,
620 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss));
621 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
622 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
625 // Request two in all-or-none mode, but only read one.
626 const void* read_buffer = nullptr;
627 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
630 dp->ConsumerBeginReadData(
631 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), true));
632 EXPECT_TRUE(read_buffer);
633 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
634 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer);
635 EXPECT_EQ(123, read_elements[0]);
636 EXPECT_EQ(MOJO_RESULT_OK,
637 dp->ConsumerEndReadData(
638 static_cast<uint32_t>(1u * sizeof(elements[0]))));
640 // Should still be readable.
642 hss = HandleSignalsState();
644 MOJO_RESULT_ALREADY_EXISTS,
645 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34, &hss));
646 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
647 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
650 // Request three, but not in all-or-none mode.
651 read_buffer = nullptr;
652 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0]));
655 dp->ConsumerBeginReadData(
656 MakeUserPointer(&read_buffer), MakeUserPointer(&num_bytes), false));
657 EXPECT_TRUE(read_buffer);
658 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
659 read_elements = static_cast<const int32_t*>(read_buffer);
660 EXPECT_EQ(456, read_elements[0]);
661 EXPECT_EQ(MOJO_RESULT_OK,
662 dp->ConsumerEndReadData(
663 static_cast<uint32_t>(1u * sizeof(elements[0]))));
665 // Adding a waiter should now succeed.
667 ASSERT_EQ(MOJO_RESULT_OK,
668 dp->ConsumerAddWaiter(
669 &waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, nullptr));
671 // Close the producer.
674 // Should be never-readable.
675 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context));
676 EXPECT_EQ(56u, context);
677 hss = HandleSignalsState();
678 dp->ConsumerRemoveWaiter(&waiter, &hss);
679 EXPECT_EQ(0u, hss.satisfied_signals);
680 EXPECT_EQ(0u, hss.satisfiable_signals);
686 // Tests that data pipes aren't writable/readable during two-phase writes/reads.
687 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) {
688 const MojoCreateDataPipeOptions options = {
689 kSizeOfOptions, // |struct_size|.
690 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
691 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
692 1000 * sizeof(int32_t) // |capacity_num_bytes|.
694 MojoCreateDataPipeOptions validated_options = {0};
695 EXPECT_EQ(MOJO_RESULT_OK,
696 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
697 &validated_options));
699 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
701 HandleSignalsState hss;
703 // It should be writable.
705 hss = HandleSignalsState();
707 MOJO_RESULT_ALREADY_EXISTS,
708 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss));
709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
710 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
712 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
713 void* write_ptr = nullptr;
716 dp->ProducerBeginWriteData(
717 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
718 EXPECT_TRUE(write_ptr);
719 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
721 // At this point, it shouldn't be writable.
725 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1, nullptr));
726 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
727 hss = HandleSignalsState();
728 dp->ProducerRemoveWaiter(&waiter, &hss);
729 EXPECT_EQ(0u, hss.satisfied_signals);
730 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
732 // It shouldn't be readable yet either.
736 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2, nullptr));
737 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
738 hss = HandleSignalsState();
739 dp->ConsumerRemoveWaiter(&waiter, &hss);
740 EXPECT_EQ(0u, hss.satisfied_signals);
741 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
743 static_cast<int32_t*>(write_ptr)[0] = 123;
746 dp->ProducerEndWriteData(static_cast<uint32_t>(1u * sizeof(int32_t))));
748 // It should be writable again.
750 hss = HandleSignalsState();
752 MOJO_RESULT_ALREADY_EXISTS,
753 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3, &hss));
754 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
755 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
759 hss = HandleSignalsState();
761 MOJO_RESULT_ALREADY_EXISTS,
762 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4, &hss));
763 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
764 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
766 // Start another two-phase write and check that it's readable even in the
768 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
772 dp->ProducerBeginWriteData(
773 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
774 EXPECT_TRUE(write_ptr);
775 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t)));
777 // It should be readable.
779 hss = HandleSignalsState();
781 MOJO_RESULT_ALREADY_EXISTS,
782 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss));
783 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
784 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
786 // End the two-phase write without writing anything.
787 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
789 // Start a two-phase read.
790 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
791 const void* read_ptr = nullptr;
794 dp->ConsumerBeginReadData(
795 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false));
796 EXPECT_TRUE(read_ptr);
797 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes);
799 // At this point, it should still be writable.
801 hss = HandleSignalsState();
803 MOJO_RESULT_ALREADY_EXISTS,
804 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss));
805 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
806 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
812 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, nullptr));
813 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
814 hss = HandleSignalsState();
815 dp->ConsumerRemoveWaiter(&waiter, &hss);
816 EXPECT_EQ(0u, hss.satisfied_signals);
817 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
819 // End the two-phase read without reading anything.
820 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
822 // It should be readable again.
824 hss = HandleSignalsState();
826 MOJO_RESULT_ALREADY_EXISTS,
827 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8, &hss));
828 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
829 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
835 // Test that a "may discard" data pipe is writable even when it's full.
836 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) {
837 const MojoCreateDataPipeOptions options = {
838 kSizeOfOptions, // |struct_size|.
839 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
840 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
841 1 * sizeof(int32_t) // |capacity_num_bytes|.
843 MojoCreateDataPipeOptions validated_options = {0};
844 EXPECT_EQ(MOJO_RESULT_OK,
845 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
846 &validated_options));
848 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
850 HandleSignalsState hss;
854 hss = HandleSignalsState();
856 MOJO_RESULT_ALREADY_EXISTS,
857 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss));
858 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
859 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
865 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1, nullptr));
866 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
867 hss = HandleSignalsState();
868 dp->ConsumerRemoveWaiter(&waiter, &hss);
869 EXPECT_EQ(0u, hss.satisfied_signals);
870 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
872 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t));
873 int32_t element = 123;
874 EXPECT_EQ(MOJO_RESULT_OK,
875 dp->ProducerWriteData(UserPointer<const void>(&element),
876 MakeUserPointer(&num_bytes),
878 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
880 // Still writable (even though it's full).
882 hss = HandleSignalsState();
884 MOJO_RESULT_ALREADY_EXISTS,
885 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2, &hss));
886 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
887 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
891 hss = HandleSignalsState();
893 MOJO_RESULT_ALREADY_EXISTS,
894 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3, &hss));
895 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
896 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
898 // Overwrite that element.
899 num_bytes = static_cast<uint32_t>(sizeof(int32_t));
901 EXPECT_EQ(MOJO_RESULT_OK,
902 dp->ProducerWriteData(UserPointer<const void>(&element),
903 MakeUserPointer(&num_bytes),
905 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
909 hss = HandleSignalsState();
911 MOJO_RESULT_ALREADY_EXISTS,
912 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4, &hss));
913 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
914 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
916 // And still readable.
918 hss = HandleSignalsState();
920 MOJO_RESULT_ALREADY_EXISTS,
921 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5, &hss));
922 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals);
923 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
925 // Read that element.
926 num_bytes = static_cast<uint32_t>(sizeof(int32_t));
930 dp->ConsumerReadData(UserPointer<void>(&element),
931 MakeUserPointer(&num_bytes),
934 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
935 EXPECT_EQ(456, element);
939 hss = HandleSignalsState();
941 MOJO_RESULT_ALREADY_EXISTS,
942 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6, &hss));
943 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals);
944 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfiable_signals);
946 // No longer readable.
950 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7, nullptr));
951 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, nullptr));
952 hss = HandleSignalsState();
953 dp->ConsumerRemoveWaiter(&waiter, &hss);
954 EXPECT_EQ(0u, hss.satisfied_signals);
955 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfiable_signals);
961 void Seq(int32_t start, size_t count, int32_t* out) {
962 for (size_t i = 0; i < count; i++)
963 out[i] = start + static_cast<int32_t>(i);
966 TEST(LocalDataPipeTest, MayDiscard) {
967 const MojoCreateDataPipeOptions options = {
968 kSizeOfOptions, // |struct_size|.
969 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
970 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
971 10 * sizeof(int32_t) // |capacity_num_bytes|.
973 MojoCreateDataPipeOptions validated_options = {0};
974 EXPECT_EQ(MOJO_RESULT_OK,
975 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
976 &validated_options));
978 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
980 int32_t buffer[100] = {0};
981 uint32_t num_bytes = 0;
983 num_bytes = 20u * sizeof(int32_t);
984 Seq(0, arraysize(buffer), buffer);
985 // Try writing more than capacity. (This test relies on the implementation
986 // enforcing the capacity strictly.)
989 dp->ProducerWriteData(
990 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false));
991 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
993 // Read half of what we wrote.
994 num_bytes = 5u * sizeof(int32_t);
995 memset(buffer, 0xab, sizeof(buffer));
996 EXPECT_EQ(MOJO_RESULT_OK,
997 dp->ConsumerReadData(UserPointer<void>(buffer),
998 MakeUserPointer(&num_bytes),
1001 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1002 int32_t expected_buffer[100];
1003 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1004 Seq(0, 5u, expected_buffer);
1005 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1006 // Internally, a circular buffer would now look like:
1007 // -, -, -, -, -, 5, 6, 7, 8, 9
1009 // Write a bit more than the space that's available.
1010 num_bytes = 8u * sizeof(int32_t);
1011 Seq(100, arraysize(buffer), buffer);
1014 dp->ProducerWriteData(
1015 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false));
1016 EXPECT_EQ(8u * sizeof(int32_t), num_bytes);
1017 // Internally, a circular buffer would now look like:
1018 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9
1020 // Read half of what's available.
1021 num_bytes = 5u * sizeof(int32_t);
1022 memset(buffer, 0xab, sizeof(buffer));
1023 EXPECT_EQ(MOJO_RESULT_OK,
1024 dp->ConsumerReadData(UserPointer<void>(buffer),
1025 MakeUserPointer(&num_bytes),
1028 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1029 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1030 expected_buffer[0] = 8;
1031 expected_buffer[1] = 9;
1032 expected_buffer[2] = 100;
1033 expected_buffer[3] = 101;
1034 expected_buffer[4] = 102;
1035 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1036 // Internally, a circular buffer would now look like:
1037 // -, -, -, 103, 104, 105, 106, 107, -, -
1039 // Write one integer.
1040 num_bytes = 1u * sizeof(int32_t);
1041 Seq(200, arraysize(buffer), buffer);
1044 dp->ProducerWriteData(
1045 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false));
1046 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1047 // Internally, a circular buffer would now look like:
1048 // -, -, -, 103, 104, 105, 106, 107, 200, -
1051 num_bytes = 5u * sizeof(int32_t);
1052 Seq(300, arraysize(buffer), buffer);
1055 dp->ProducerWriteData(
1056 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false));
1057 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1058 // Internally, a circular buffer would now look like:
1059 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300
1062 num_bytes = sizeof(buffer);
1063 memset(buffer, 0xab, sizeof(buffer));
1064 EXPECT_EQ(MOJO_RESULT_OK,
1065 dp->ConsumerReadData(UserPointer<void>(buffer),
1066 MakeUserPointer(&num_bytes),
1069 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1070 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1071 expected_buffer[0] = 104;
1072 expected_buffer[1] = 105;
1073 expected_buffer[2] = 106;
1074 expected_buffer[3] = 107;
1075 expected_buffer[4] = 200;
1076 expected_buffer[5] = 300;
1077 expected_buffer[6] = 301;
1078 expected_buffer[7] = 302;
1079 expected_buffer[8] = 303;
1080 expected_buffer[9] = 304;
1081 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1083 // Test two-phase writes, including in all-or-none mode.
1084 // Note: Again, the following depends on an implementation detail -- namely
1085 // that the write pointer will point at the 5th element of the buffer (and the
1086 // buffer has exactly the capacity requested).
1089 void* write_ptr = nullptr;
1092 dp->ProducerBeginWriteData(
1093 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
1094 EXPECT_TRUE(write_ptr);
1095 EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
1096 Seq(400, 6, static_cast<int32_t*>(write_ptr));
1097 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t)));
1098 // Internally, a circular buffer would now look like:
1099 // -, -, -, -, 400, 401, 402, 403, 404, 405
1101 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none"
1103 num_bytes = 6u * sizeof(int32_t);
1104 write_ptr = nullptr;
1107 dp->ProducerBeginWriteData(
1108 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
1109 EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
1110 static_cast<int32_t*>(write_ptr)[0] = 500;
1111 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t)));
1112 // Internally, a circular buffer would now look like:
1113 // 500, -, -, -, 400, 401, 402, 403, 404, 405
1115 // Requesting a 10-element buffer in all-or-none mode fails at this point.
1116 num_bytes = 10u * sizeof(int32_t);
1117 write_ptr = nullptr;
1119 MOJO_RESULT_OUT_OF_RANGE,
1120 dp->ProducerBeginWriteData(
1121 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1123 // But requesting, say, a 5-element (up to 9, really) buffer should be okay.
1124 // It will discard two elements.
1125 num_bytes = 5u * sizeof(int32_t);
1126 write_ptr = nullptr;
1129 dp->ProducerBeginWriteData(
1130 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1131 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1132 // Only write 4 elements though.
1133 Seq(600, 4, static_cast<int32_t*>(write_ptr));
1134 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t)));
1135 // Internally, a circular buffer would now look like:
1136 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405
1138 // Do this again. Make sure we can get a buffer all the way out to the end of
1139 // the internal buffer.
1140 num_bytes = 5u * sizeof(int32_t);
1141 write_ptr = nullptr;
1144 dp->ProducerBeginWriteData(
1145 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1146 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1147 // Only write 3 elements though.
1148 Seq(700, 3, static_cast<int32_t*>(write_ptr));
1149 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t)));
1150 // Internally, a circular buffer would now look like:
1151 // 500, 600, 601, 602, 603, 700, 701, 702, -, -
1154 num_bytes = sizeof(buffer);
1155 memset(buffer, 0xab, sizeof(buffer));
1156 EXPECT_EQ(MOJO_RESULT_OK,
1157 dp->ConsumerReadData(UserPointer<void>(buffer),
1158 MakeUserPointer(&num_bytes),
1161 EXPECT_EQ(8u * sizeof(int32_t), num_bytes);
1162 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1163 expected_buffer[0] = 500;
1164 expected_buffer[1] = 600;
1165 expected_buffer[2] = 601;
1166 expected_buffer[3] = 602;
1167 expected_buffer[4] = 603;
1168 expected_buffer[5] = 700;
1169 expected_buffer[6] = 701;
1170 expected_buffer[7] = 702;
1171 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1173 dp->ProducerClose();
1174 dp->ConsumerClose();
1177 TEST(LocalDataPipeTest, AllOrNone) {
1178 const MojoCreateDataPipeOptions options = {
1179 kSizeOfOptions, // |struct_size|.
1180 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1181 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1182 10 * sizeof(int32_t) // |capacity_num_bytes|.
1184 MojoCreateDataPipeOptions validated_options = {0};
1185 EXPECT_EQ(MOJO_RESULT_OK,
1186 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1187 &validated_options));
1189 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1191 // Try writing way too much.
1192 uint32_t num_bytes = 20u * sizeof(int32_t);
1193 int32_t buffer[100];
1194 Seq(0, arraysize(buffer), buffer);
1196 MOJO_RESULT_OUT_OF_RANGE,
1197 dp->ProducerWriteData(
1198 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1200 // Should still be empty.
1202 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1203 EXPECT_EQ(0u, num_bytes);
1206 num_bytes = 5u * sizeof(int32_t);
1207 Seq(100, arraysize(buffer), buffer);
1210 dp->ProducerWriteData(
1211 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1212 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1216 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1217 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1220 num_bytes = 6u * sizeof(int32_t);
1221 Seq(200, arraysize(buffer), buffer);
1223 MOJO_RESULT_OUT_OF_RANGE,
1224 dp->ProducerWriteData(
1225 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1227 // Try reading too much.
1228 num_bytes = 11u * sizeof(int32_t);
1229 memset(buffer, 0xab, sizeof(buffer));
1230 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1231 dp->ConsumerReadData(UserPointer<void>(buffer),
1232 MakeUserPointer(&num_bytes),
1235 int32_t expected_buffer[100];
1236 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1237 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1239 // Try discarding too much.
1240 num_bytes = 11u * sizeof(int32_t);
1241 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1242 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1245 num_bytes = 2u * sizeof(int32_t);
1246 Seq(300, arraysize(buffer), buffer);
1249 dp->ProducerWriteData(
1250 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1251 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
1254 num_bytes = 3u * sizeof(int32_t);
1255 Seq(400, arraysize(buffer), buffer);
1258 dp->ProducerWriteData(
1259 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1260 EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
1264 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1265 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1268 num_bytes = 5u * sizeof(int32_t);
1269 memset(buffer, 0xab, sizeof(buffer));
1270 EXPECT_EQ(MOJO_RESULT_OK,
1271 dp->ConsumerReadData(UserPointer<void>(buffer),
1272 MakeUserPointer(&num_bytes),
1275 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1276 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1277 Seq(100, 5, expected_buffer);
1278 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1280 // Try reading too much again.
1281 num_bytes = 6u * sizeof(int32_t);
1282 memset(buffer, 0xab, sizeof(buffer));
1283 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1284 dp->ConsumerReadData(UserPointer<void>(buffer),
1285 MakeUserPointer(&num_bytes),
1288 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1289 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1291 // Try discarding too much again.
1292 num_bytes = 6u * sizeof(int32_t);
1293 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1294 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1296 // Discard a little.
1297 num_bytes = 2u * sizeof(int32_t);
1298 EXPECT_EQ(MOJO_RESULT_OK,
1299 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1300 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
1304 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1305 EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
1307 // Close the producer, then test producer-closed cases.
1308 dp->ProducerClose();
1310 // Try reading too much; "failed precondition" since the producer is closed.
1311 num_bytes = 4u * sizeof(int32_t);
1312 memset(buffer, 0xab, sizeof(buffer));
1313 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1314 dp->ConsumerReadData(UserPointer<void>(buffer),
1315 MakeUserPointer(&num_bytes),
1318 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1319 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1321 // Try discarding too much; "failed precondition" again.
1322 num_bytes = 4u * sizeof(int32_t);
1323 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1324 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1327 num_bytes = 2u * sizeof(int32_t);
1328 memset(buffer, 0xab, sizeof(buffer));
1329 EXPECT_EQ(MOJO_RESULT_OK,
1330 dp->ConsumerReadData(UserPointer<void>(buffer),
1331 MakeUserPointer(&num_bytes),
1334 EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
1335 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1336 Seq(400, 2, expected_buffer);
1337 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1339 // Discard the remaining element.
1340 num_bytes = 1u * sizeof(int32_t);
1341 EXPECT_EQ(MOJO_RESULT_OK,
1342 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1343 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1347 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1348 EXPECT_EQ(0u, num_bytes);
1350 dp->ConsumerClose();
1353 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) {
1354 const MojoCreateDataPipeOptions options = {
1355 kSizeOfOptions, // |struct_size|.
1356 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
1357 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1358 10 * sizeof(int32_t) // |capacity_num_bytes|.
1360 MojoCreateDataPipeOptions validated_options = {0};
1361 EXPECT_EQ(MOJO_RESULT_OK,
1362 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1363 &validated_options));
1365 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1367 // Try writing way too much.
1368 uint32_t num_bytes = 20u * sizeof(int32_t);
1369 int32_t buffer[100];
1370 Seq(0, arraysize(buffer), buffer);
1372 MOJO_RESULT_OUT_OF_RANGE,
1373 dp->ProducerWriteData(
1374 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1376 // Write some stuff.
1377 num_bytes = 5u * sizeof(int32_t);
1378 Seq(100, arraysize(buffer), buffer);
1381 dp->ProducerWriteData(
1382 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1383 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1385 // Write lots of stuff (discarding all but "104").
1386 num_bytes = 9u * sizeof(int32_t);
1387 Seq(200, arraysize(buffer), buffer);
1390 dp->ProducerWriteData(
1391 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1392 EXPECT_EQ(9u * sizeof(int32_t), num_bytes);
1395 num_bytes = 1u * sizeof(int32_t);
1396 memset(buffer, 0xab, sizeof(buffer));
1397 EXPECT_EQ(MOJO_RESULT_OK,
1398 dp->ConsumerReadData(UserPointer<void>(buffer),
1399 MakeUserPointer(&num_bytes),
1402 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1403 int32_t expected_buffer[100];
1404 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1405 expected_buffer[0] = 104;
1406 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1408 // Try reading too many.
1409 num_bytes = 10u * sizeof(int32_t);
1410 memset(buffer, 0xab, sizeof(buffer));
1411 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1412 dp->ConsumerReadData(UserPointer<void>(buffer),
1413 MakeUserPointer(&num_bytes),
1416 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1417 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1419 // Try discarding too many.
1420 num_bytes = 10u * sizeof(int32_t);
1421 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1422 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1425 num_bytes = 4u * sizeof(int32_t);
1426 EXPECT_EQ(MOJO_RESULT_OK,
1427 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1431 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1432 EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1434 // Write as much as possible.
1435 num_bytes = 10u * sizeof(int32_t);
1436 Seq(300, arraysize(buffer), buffer);
1439 dp->ProducerWriteData(
1440 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1441 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1444 num_bytes = 10u * sizeof(int32_t);
1445 memset(buffer, 0xab, sizeof(buffer));
1446 EXPECT_EQ(MOJO_RESULT_OK,
1447 dp->ConsumerReadData(UserPointer<void>(buffer),
1448 MakeUserPointer(&num_bytes),
1451 memset(expected_buffer, 0xab, sizeof(expected_buffer));
1452 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1453 Seq(300, 10, expected_buffer);
1454 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1456 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested
1457 // in LocalDataPipeTest.MayDiscard.
1459 dp->ProducerClose();
1460 dp->ConsumerClose();
1463 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) {
1464 const MojoCreateDataPipeOptions options = {
1465 kSizeOfOptions, // |struct_size|.
1466 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1467 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1468 10 * sizeof(int32_t) // |capacity_num_bytes|.
1470 MojoCreateDataPipeOptions validated_options = {0};
1471 EXPECT_EQ(MOJO_RESULT_OK,
1472 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1473 &validated_options));
1475 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1477 // Try writing way too much (two-phase).
1478 uint32_t num_bytes = 20u * sizeof(int32_t);
1479 void* write_ptr = nullptr;
1481 MOJO_RESULT_OUT_OF_RANGE,
1482 dp->ProducerBeginWriteData(
1483 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1485 // Try writing an amount which isn't a multiple of the element size
1487 static_assert(sizeof(int32_t) > 1u, "Wow! int32_t's have size 1");
1489 write_ptr = nullptr;
1491 MOJO_RESULT_INVALID_ARGUMENT,
1492 dp->ProducerBeginWriteData(
1493 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1495 // Try reading way too much (two-phase).
1496 num_bytes = 20u * sizeof(int32_t);
1497 const void* read_ptr = nullptr;
1498 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1499 dp->ConsumerBeginReadData(
1500 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1502 // Write half (two-phase).
1503 num_bytes = 5u * sizeof(int32_t);
1504 write_ptr = nullptr;
1507 dp->ProducerBeginWriteData(
1508 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1509 // May provide more space than requested.
1510 EXPECT_GE(num_bytes, 5u * sizeof(int32_t));
1511 EXPECT_TRUE(write_ptr);
1512 Seq(0, 5, static_cast<int32_t*>(write_ptr));
1513 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t)));
1515 // Try reading an amount which isn't a multiple of the element size
1519 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1520 dp->ConsumerBeginReadData(
1521 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1523 // Read one (two-phase).
1524 num_bytes = 1u * sizeof(int32_t);
1526 EXPECT_EQ(MOJO_RESULT_OK,
1527 dp->ConsumerBeginReadData(
1528 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1529 EXPECT_GE(num_bytes, 1u * sizeof(int32_t));
1530 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]);
1531 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t)));
1533 // We should have four left, leaving room for six.
1535 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1536 EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
1538 // Assuming a tight circular buffer of the specified capacity, we can't do a
1539 // two-phase write of six now.
1540 num_bytes = 6u * sizeof(int32_t);
1541 write_ptr = nullptr;
1543 MOJO_RESULT_OUT_OF_RANGE,
1544 dp->ProducerBeginWriteData(
1545 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1547 // Write six elements (simple), filling the buffer.
1548 num_bytes = 6u * sizeof(int32_t);
1549 int32_t buffer[100];
1550 Seq(100, 6, buffer);
1553 dp->ProducerWriteData(
1554 UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1555 EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
1559 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1560 EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1562 // But a two-phase read of ten should fail.
1563 num_bytes = 10u * sizeof(int32_t);
1565 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1566 dp->ConsumerBeginReadData(
1567 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1569 // Close the producer.
1570 dp->ProducerClose();
1572 // A two-phase read of nine should work.
1573 num_bytes = 9u * sizeof(int32_t);
1575 EXPECT_EQ(MOJO_RESULT_OK,
1576 dp->ConsumerBeginReadData(
1577 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1578 EXPECT_GE(num_bytes, 9u * sizeof(int32_t));
1579 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]);
1580 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]);
1581 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]);
1582 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]);
1583 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]);
1584 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]);
1585 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]);
1586 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]);
1587 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]);
1588 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t)));
1590 // A two-phase read of two should fail, with "failed precondition".
1591 num_bytes = 2u * sizeof(int32_t);
1593 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1594 dp->ConsumerBeginReadData(
1595 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1597 dp->ConsumerClose();
1600 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads,
1601 // respectively, as much as possible, even if it has to "wrap around" the
1602 // internal circular buffer. (Note that the two-phase write and read do not do
1604 TEST(LocalDataPipeTest, WrapAround) {
1605 unsigned char test_data[1000];
1606 for (size_t i = 0; i < arraysize(test_data); i++)
1607 test_data[i] = static_cast<unsigned char>(i);
1609 const MojoCreateDataPipeOptions options = {
1610 kSizeOfOptions, // |struct_size|.
1611 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1612 1u, // |element_num_bytes|.
1613 100u // |capacity_num_bytes|.
1615 MojoCreateDataPipeOptions validated_options = {0};
1616 EXPECT_EQ(MOJO_RESULT_OK,
1617 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1618 &validated_options));
1619 // This test won't be valid if |ValidateCreateOptions()| decides to give the
1621 ASSERT_EQ(100u, validated_options.capacity_num_bytes);
1623 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1626 uint32_t num_bytes = 20u;
1627 EXPECT_EQ(MOJO_RESULT_OK,
1628 dp->ProducerWriteData(UserPointer<const void>(&test_data[0]),
1629 MakeUserPointer(&num_bytes),
1631 EXPECT_EQ(20u, num_bytes);
1634 unsigned char read_buffer[1000] = {0};
1638 dp->ConsumerReadData(UserPointer<void>(read_buffer),
1639 MakeUserPointer(&num_bytes),
1642 EXPECT_EQ(10u, num_bytes);
1643 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u));
1645 // Check that a two-phase write can now only write (at most) 80 bytes. (This
1646 // checks an implementation detail; this behavior is not guaranteed, but we
1647 // need it for this test.)
1648 void* write_buffer_ptr = nullptr;
1650 EXPECT_EQ(MOJO_RESULT_OK,
1651 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1652 MakeUserPointer(&num_bytes),
1654 EXPECT_TRUE(write_buffer_ptr);
1655 EXPECT_EQ(80u, num_bytes);
1656 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
1658 // Write as much data as we can (using |ProducerWriteData()|). We should write
1661 EXPECT_EQ(MOJO_RESULT_OK,
1662 dp->ProducerWriteData(UserPointer<const void>(&test_data[20]),
1663 MakeUserPointer(&num_bytes),
1665 EXPECT_EQ(90u, num_bytes);
1667 // Check that a two-phase read can now only read (at most) 90 bytes. (This
1668 // checks an implementation detail; this behavior is not guaranteed, but we
1669 // need it for this test.)
1670 const void* read_buffer_ptr = nullptr;
1672 EXPECT_EQ(MOJO_RESULT_OK,
1673 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1674 MakeUserPointer(&num_bytes),
1676 EXPECT_TRUE(read_buffer_ptr);
1677 EXPECT_EQ(90u, num_bytes);
1678 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
1680 // Read as much as possible (using |ConsumerReadData()|). We should read 100
1683 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0]));
1684 memset(read_buffer, 0, num_bytes);
1687 dp->ConsumerReadData(UserPointer<void>(read_buffer),
1688 MakeUserPointer(&num_bytes),
1691 EXPECT_EQ(100u, num_bytes);
1692 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u));
1694 dp->ProducerClose();
1695 dp->ConsumerClose();
1698 // Tests the behavior of closing the producer or consumer with respect to
1699 // writes and reads (simple and two-phase).
1700 TEST(LocalDataPipeTest, CloseWriteRead) {
1701 const char kTestData[] = "hello world";
1702 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData));
1704 const MojoCreateDataPipeOptions options = {
1705 kSizeOfOptions, // |struct_size|.
1706 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1707 1u, // |element_num_bytes|.
1708 1000u // |capacity_num_bytes|.
1710 MojoCreateDataPipeOptions validated_options = {0};
1711 EXPECT_EQ(MOJO_RESULT_OK,
1712 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1713 &validated_options));
1715 // Close producer first, then consumer.
1717 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1719 // Write some data, so we'll have something to read.
1720 uint32_t num_bytes = kTestDataSize;
1721 EXPECT_EQ(MOJO_RESULT_OK,
1722 dp->ProducerWriteData(UserPointer<const void>(kTestData),
1723 MakeUserPointer(&num_bytes),
1725 EXPECT_EQ(kTestDataSize, num_bytes);
1727 // Write it again, so we'll have something left over.
1728 num_bytes = kTestDataSize;
1729 EXPECT_EQ(MOJO_RESULT_OK,
1730 dp->ProducerWriteData(UserPointer<const void>(kTestData),
1731 MakeUserPointer(&num_bytes),
1733 EXPECT_EQ(kTestDataSize, num_bytes);
1735 // Start two-phase write.
1736 void* write_buffer_ptr = nullptr;
1738 EXPECT_EQ(MOJO_RESULT_OK,
1739 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1740 MakeUserPointer(&num_bytes),
1742 EXPECT_TRUE(write_buffer_ptr);
1743 EXPECT_GT(num_bytes, 0u);
1745 // Start two-phase read.
1746 const void* read_buffer_ptr = nullptr;
1748 EXPECT_EQ(MOJO_RESULT_OK,
1749 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1750 MakeUserPointer(&num_bytes),
1752 EXPECT_TRUE(read_buffer_ptr);
1753 EXPECT_EQ(2u * kTestDataSize, num_bytes);
1755 // Close the producer.
1756 dp->ProducerClose();
1758 // The consumer can finish its two-phase read.
1759 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize));
1760 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize));
1762 // And start another.
1763 read_buffer_ptr = nullptr;
1765 EXPECT_EQ(MOJO_RESULT_OK,
1766 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1767 MakeUserPointer(&num_bytes),
1769 EXPECT_TRUE(read_buffer_ptr);
1770 EXPECT_EQ(kTestDataSize, num_bytes);
1772 // Close the consumer, which cancels the two-phase read.
1773 dp->ConsumerClose();
1776 // Close consumer first, then producer.
1778 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1780 // Write some data, so we'll have something to read.
1781 uint32_t num_bytes = kTestDataSize;
1782 EXPECT_EQ(MOJO_RESULT_OK,
1783 dp->ProducerWriteData(UserPointer<const void>(kTestData),
1784 MakeUserPointer(&num_bytes),
1786 EXPECT_EQ(kTestDataSize, num_bytes);
1788 // Start two-phase write.
1789 void* write_buffer_ptr = nullptr;
1791 EXPECT_EQ(MOJO_RESULT_OK,
1792 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1793 MakeUserPointer(&num_bytes),
1795 EXPECT_TRUE(write_buffer_ptr);
1796 ASSERT_GT(num_bytes, kTestDataSize);
1798 // Start two-phase read.
1799 const void* read_buffer_ptr = nullptr;
1801 EXPECT_EQ(MOJO_RESULT_OK,
1802 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1803 MakeUserPointer(&num_bytes),
1805 EXPECT_TRUE(read_buffer_ptr);
1806 EXPECT_EQ(kTestDataSize, num_bytes);
1808 // Close the consumer.
1809 dp->ConsumerClose();
1811 // Actually write some data. (Note: Premature freeing of the buffer would
1812 // probably only be detected under ASAN or similar.)
1813 memcpy(write_buffer_ptr, kTestData, kTestDataSize);
1814 // Note: Even though the consumer has been closed, ending the two-phase
1815 // write will report success.
1816 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(kTestDataSize));
1818 // But trying to write should result in failure.
1819 num_bytes = kTestDataSize;
1820 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1821 dp->ProducerWriteData(UserPointer<const void>(kTestData),
1822 MakeUserPointer(&num_bytes),
1825 // As will trying to start another two-phase write.
1826 write_buffer_ptr = nullptr;
1828 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1829 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1830 MakeUserPointer(&num_bytes),
1833 dp->ProducerClose();
1836 // Test closing the consumer first, then the producer, with an active
1839 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1841 // Start two-phase write.
1842 void* write_buffer_ptr = nullptr;
1843 uint32_t num_bytes = 0u;
1844 EXPECT_EQ(MOJO_RESULT_OK,
1845 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1846 MakeUserPointer(&num_bytes),
1848 EXPECT_TRUE(write_buffer_ptr);
1849 ASSERT_GT(num_bytes, kTestDataSize);
1851 dp->ConsumerClose();
1852 dp->ProducerClose();
1855 // Test closing the producer and then trying to read (with no data).
1857 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1859 // Write some data, so we'll have something to read.
1860 uint32_t num_bytes = kTestDataSize;
1861 EXPECT_EQ(MOJO_RESULT_OK,
1862 dp->ProducerWriteData(UserPointer<const void>(kTestData),
1863 MakeUserPointer(&num_bytes),
1865 EXPECT_EQ(kTestDataSize, num_bytes);
1867 // Close the producer.
1868 dp->ProducerClose();
1872 num_bytes = static_cast<uint32_t>(sizeof(buffer));
1875 dp->ConsumerReadData(UserPointer<void>(buffer),
1876 MakeUserPointer(&num_bytes),
1879 EXPECT_EQ(kTestDataSize, num_bytes);
1880 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize));
1883 memset(buffer, 0, 1000);
1884 num_bytes = static_cast<uint32_t>(sizeof(buffer));
1887 dp->ConsumerReadData(UserPointer<void>(buffer),
1888 MakeUserPointer(&num_bytes),
1891 EXPECT_EQ(kTestDataSize, num_bytes);
1892 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize));
1894 // A second read should fail.
1895 num_bytes = static_cast<uint32_t>(sizeof(buffer));
1897 MOJO_RESULT_FAILED_PRECONDITION,
1898 dp->ConsumerReadData(UserPointer<void>(buffer),
1899 MakeUserPointer(&num_bytes),
1903 // A two-phase read should also fail.
1904 const void* read_buffer_ptr = nullptr;
1906 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1907 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1908 MakeUserPointer(&num_bytes),
1911 // Ditto for discard.
1913 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1914 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false));
1916 dp->ConsumerClose();
1920 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) {
1921 const MojoCreateDataPipeOptions options = {
1922 kSizeOfOptions, // |struct_size|.
1923 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|.
1924 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|.
1925 10 * sizeof(int32_t) // |capacity_num_bytes|.
1927 MojoCreateDataPipeOptions validated_options = {0};
1928 EXPECT_EQ(MOJO_RESULT_OK,
1929 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1930 &validated_options));
1932 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1935 uint32_t num_bytes = 1000u;
1936 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1937 EXPECT_EQ(0u, num_bytes);
1939 // Try "ending" a two-phase write when one isn't active.
1940 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1941 dp->ProducerEndWriteData(1u * sizeof(int32_t)));
1945 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1946 EXPECT_EQ(0u, num_bytes);
1948 // Try ending a two-phase write with an invalid amount (too much).
1950 void* write_ptr = nullptr;
1953 dp->ProducerBeginWriteData(
1954 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
1955 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1956 dp->ProducerEndWriteData(num_bytes +
1957 static_cast<uint32_t>(sizeof(int32_t))));
1959 // But the two-phase write still ended.
1960 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1964 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1965 EXPECT_EQ(0u, num_bytes);
1967 // Try ending a two-phase write with an invalid amount (not a multiple of the
1970 write_ptr = nullptr;
1973 dp->ProducerBeginWriteData(
1974 MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), false));
1975 EXPECT_GE(num_bytes, 1u);
1976 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u));
1978 // But the two-phase write still ended.
1979 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1983 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1984 EXPECT_EQ(0u, num_bytes);
1986 // Now write some data, so we'll be able to try reading.
1987 int32_t element = 123;
1988 num_bytes = 1u * sizeof(int32_t);
1989 EXPECT_EQ(MOJO_RESULT_OK,
1990 dp->ProducerWriteData(UserPointer<const void>(&element),
1991 MakeUserPointer(&num_bytes),
1994 // One element available.
1996 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1997 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1999 // Try "ending" a two-phase read when one isn't active.
2000 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
2001 dp->ConsumerEndReadData(1u * sizeof(int32_t)));
2003 // Still one element available.
2005 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2006 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2008 // Try ending a two-phase read with an invalid amount (too much).
2010 const void* read_ptr = nullptr;
2013 dp->ConsumerBeginReadData(
2014 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false));
2015 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
2016 dp->ConsumerEndReadData(num_bytes +
2017 static_cast<uint32_t>(sizeof(int32_t))));
2019 // Still one element available.
2021 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2022 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2024 // Try ending a two-phase read with an invalid amount (not a multiple of the
2030 dp->ConsumerBeginReadData(
2031 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false));
2032 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2033 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]);
2034 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u));
2036 // Still one element available.
2038 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2039 EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2041 dp->ProducerClose();
2042 dp->ConsumerClose();
2045 // Tests that even with "may discard", the data won't change under a two-phase
2047 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two
2048 // related issues: First, we don't recognize that the data given to
2049 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|,
2050 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the
2051 // |ProducerWriteData()| then changes the data underneath the two-phase read.)
2052 TEST(LocalDataPipeTest, DISABLED_MayDiscardTwoPhaseConsistent) {
2053 const MojoCreateDataPipeOptions options = {
2054 kSizeOfOptions, // |struct_size|.
2055 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|.
2056 1, // |element_num_bytes|.
2057 2 // |capacity_num_bytes|.
2059 MojoCreateDataPipeOptions validated_options = {0};
2060 EXPECT_EQ(MOJO_RESULT_OK,
2061 DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
2062 &validated_options));
2064 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
2066 // Write some elements.
2067 char elements[2] = {'a', 'b'};
2068 uint32_t num_bytes = 2u;
2069 EXPECT_EQ(MOJO_RESULT_OK,
2070 dp->ProducerWriteData(UserPointer<const void>(elements),
2071 MakeUserPointer(&num_bytes),
2073 EXPECT_EQ(2u, num_bytes);
2076 const void* read_ptr = nullptr;
2080 dp->ConsumerBeginReadData(
2081 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false));
2082 EXPECT_EQ(2u, num_bytes);
2083 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]);
2084 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]);
2086 // Try to write some more. But nothing should be discardable right now.
2090 // TODO(vtl): This should be:
2091 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
2092 // dp->ProducerWriteData(elements, &num_bytes, false));
2093 // but we incorrectly think that the bytes being read are discardable. Letting
2094 // this through reveals the significant consequence.
2095 EXPECT_EQ(MOJO_RESULT_OK,
2096 dp->ProducerWriteData(UserPointer<const void>(elements),
2097 MakeUserPointer(&num_bytes),
2100 // Check that our read buffer hasn't changed underneath us.
2101 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]);
2102 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]);
2105 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u));
2107 // Now writing should succeed.
2108 EXPECT_EQ(MOJO_RESULT_OK,
2109 dp->ProducerWriteData(UserPointer<const void>(elements),
2110 MakeUserPointer(&num_bytes),
2113 // And if we read, we should get the new values.
2118 dp->ConsumerBeginReadData(
2119 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), false));
2120 EXPECT_EQ(2u, num_bytes);
2121 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]);
2122 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]);
2125 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u));
2127 dp->ProducerClose();
2128 dp->ConsumerClose();
2132 } // namespace system