Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / mojo / edk / system / local_data_pipe_unittest.cc
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.
4
5 #include "mojo/edk/system/local_data_pipe.h"
6
7 #include <string.h>
8
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"
14
15 namespace mojo {
16 namespace system {
17 namespace {
18
19 const uint32_t kSizeOfOptions =
20     static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions));
21
22 // Validate options.
23 TEST(LocalDataPipeTest, Creation) {
24   // Create using default options.
25   {
26     // Get default options.
27     MojoCreateDataPipeOptions default_options = {0};
28     EXPECT_EQ(
29         MOJO_RESULT_OK,
30         DataPipe::ValidateCreateOptions(NullUserPointer(), &default_options));
31     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options));
32     dp->ProducerClose();
33     dp->ConsumerClose();
34   }
35
36   // Create using non-default options.
37   {
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|.
43     };
44     MojoCreateDataPipeOptions validated_options = {0};
45     EXPECT_EQ(MOJO_RESULT_OK,
46               DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
47                                               &validated_options));
48     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
49     dp->ProducerClose();
50     dp->ConsumerClose();
51   }
52   {
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|.
58     };
59     MojoCreateDataPipeOptions validated_options = {0};
60     EXPECT_EQ(MOJO_RESULT_OK,
61               DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
62                                               &validated_options));
63     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
64     dp->ProducerClose();
65     dp->ConsumerClose();
66   }
67   {
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|.
73     };
74     MojoCreateDataPipeOptions validated_options = {0};
75     EXPECT_EQ(MOJO_RESULT_OK,
76               DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
77                                               &validated_options));
78     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
79     dp->ProducerClose();
80     dp->ConsumerClose();
81   }
82   // Default capacity.
83   {
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|.
89     };
90     MojoCreateDataPipeOptions validated_options = {0};
91     EXPECT_EQ(MOJO_RESULT_OK,
92               DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
93                                               &validated_options));
94     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
95     dp->ProducerClose();
96     dp->ConsumerClose();
97   }
98 }
99
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|.
106   };
107   MojoCreateDataPipeOptions validated_options = {0};
108   EXPECT_EQ(MOJO_RESULT_OK,
109             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
110                                             &validated_options));
111
112   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
113
114   int32_t elements[10] = {0};
115   uint32_t num_bytes = 0;
116
117   // Try reading; nothing there yet.
118   num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0]));
119   EXPECT_EQ(
120       MOJO_RESULT_SHOULD_WAIT,
121       dp->ConsumerReadData(UserPointer<void>(elements),
122                            MakeUserPointer(&num_bytes),
123                            false,
124                            false));
125
126   // Query; nothing there yet.
127   num_bytes = 0;
128   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
129   EXPECT_EQ(0u, num_bytes);
130
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));
135
136   // Read with invalid |num_bytes|.
137   num_bytes = sizeof(elements[0]) + 1;
138   EXPECT_EQ(
139       MOJO_RESULT_INVALID_ARGUMENT,
140       dp->ConsumerReadData(UserPointer<void>(elements),
141                            MakeUserPointer(&num_bytes),
142                            false,
143                            false));
144
145   // Write two elements.
146   elements[0] = 123;
147   elements[1] = 456;
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),
152                                   false));
153   // It should have written everything (even without "all or none").
154   EXPECT_EQ(2u * sizeof(elements[0]), num_bytes);
155
156   // Query.
157   num_bytes = 0;
158   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
159   EXPECT_EQ(2 * sizeof(elements[0]), num_bytes);
160
161   // Read one element.
162   elements[0] = -1;
163   elements[1] = -1;
164   num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
165   EXPECT_EQ(
166       MOJO_RESULT_OK,
167       dp->ConsumerReadData(UserPointer<void>(elements),
168                            MakeUserPointer(&num_bytes),
169                            false,
170                            false));
171   EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
172   EXPECT_EQ(123, elements[0]);
173   EXPECT_EQ(-1, elements[1]);
174
175   // Query.
176   num_bytes = 0;
177   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
178   EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
179
180   // Peek one element.
181   elements[0] = -1;
182   elements[1] = -1;
183   num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
184   EXPECT_EQ(
185       MOJO_RESULT_OK,
186       dp->ConsumerReadData(UserPointer<void>(elements),
187                            MakeUserPointer(&num_bytes),
188                            false,
189                            true));
190   EXPECT_EQ(1u * sizeof(elements[0]), num_bytes);
191   EXPECT_EQ(456, elements[0]);
192   EXPECT_EQ(-1, elements[1]);
193
194   // Query. Still has 1 element remaining.
195   num_bytes = 0;
196   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
197   EXPECT_EQ(1 * sizeof(elements[0]), num_bytes);
198
199   // Try to read two elements, with "all or none".
200   elements[0] = -1;
201   elements[1] = -1;
202   num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
203   EXPECT_EQ(
204       MOJO_RESULT_OUT_OF_RANGE,
205       dp->ConsumerReadData(UserPointer<void>(elements),
206                            MakeUserPointer(&num_bytes),
207                            true,
208                            false));
209   EXPECT_EQ(-1, elements[0]);
210   EXPECT_EQ(-1, elements[1]);
211
212   // Try to read two elements, without "all or none".
213   elements[0] = -1;
214   elements[1] = -1;
215   num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0]));
216   EXPECT_EQ(
217       MOJO_RESULT_OK,
218       dp->ConsumerReadData(UserPointer<void>(elements),
219                            MakeUserPointer(&num_bytes),
220                            false,
221                            false));
222   EXPECT_EQ(456, elements[0]);
223   EXPECT_EQ(-1, elements[1]);
224
225   // Query.
226   num_bytes = 0;
227   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
228   EXPECT_EQ(0u, num_bytes);
229
230   dp->ProducerClose();
231   dp->ConsumerClose();
232 }
233
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.
240
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|.
246   };
247   MojoCreateDataPipeOptions validated_options = {0};
248   EXPECT_EQ(MOJO_RESULT_OK,
249             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
250                                             &validated_options));
251
252   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
253   Waiter waiter;
254   uint32_t context = 0;
255   HandleSignalsState hss;
256
257   // Never readable.
258   waiter.Init();
259   hss = HandleSignalsState();
260   EXPECT_EQ(
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);
265
266   // Already writable.
267   waiter.Init();
268   hss = HandleSignalsState();
269   EXPECT_EQ(
270       MOJO_RESULT_ALREADY_EXISTS,
271       dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34, &hss));
272
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),
279                                   true));
280   EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes);
281
282   // Adding a waiter should now succeed.
283   waiter.Init();
284   ASSERT_EQ(
285       MOJO_RESULT_OK,
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);
293
294   // Peek one element.
295   elements[0] = -1;
296   elements[1] = -1;
297   num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
298   EXPECT_EQ(
299       MOJO_RESULT_OK,
300       dp->ConsumerReadData(UserPointer<void>(elements),
301                            MakeUserPointer(&num_bytes),
302                            true,
303                            true));
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]);
307
308   // Add a waiter.
309   waiter.Init();
310   ASSERT_EQ(
311       MOJO_RESULT_OK,
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);
319
320   // Do it again.
321   waiter.Init();
322   ASSERT_EQ(
323       MOJO_RESULT_OK,
324       dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78, nullptr));
325
326   // Read one element.
327   elements[0] = -1;
328   elements[1] = -1;
329   num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
330   EXPECT_EQ(
331       MOJO_RESULT_OK,
332       dp->ConsumerReadData(UserPointer<void>(elements),
333                            MakeUserPointer(&num_bytes),
334                            true,
335                            false));
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]);
339
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);
347
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));
354   EXPECT_TRUE(buffer);
355   EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
356
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]))));
361
362   // Add a waiter.
363   waiter.Init();
364   ASSERT_EQ(
365       MOJO_RESULT_OK,
366       dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90, nullptr));
367
368   // Read one element, using a two-phase read.
369   const void* read_buffer = nullptr;
370   num_bytes = 0u;
371   EXPECT_EQ(
372       MOJO_RESULT_OK,
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]);
381   EXPECT_EQ(
382       MOJO_RESULT_OK,
383       dp->ConsumerEndReadData(static_cast<uint32_t>(1u * sizeof(elements[0]))));
384
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);
392
393   // Write one element.
394   elements[0] = 123;
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),
399                                   false));
400   EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes);
401
402   // Add a waiter.
403   waiter.Init();
404   ASSERT_EQ(
405       MOJO_RESULT_OK,
406       dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, nullptr));
407
408   // Close the consumer.
409   dp->ConsumerClose();
410
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);
418
419   dp->ProducerClose();
420 }
421
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|.
428   };
429   MojoCreateDataPipeOptions validated_options = {0};
430   EXPECT_EQ(MOJO_RESULT_OK,
431             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
432                                             &validated_options));
433
434   {
435     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
436     Waiter waiter;
437     uint32_t context = 0;
438     HandleSignalsState hss;
439
440     // Never writable.
441     waiter.Init();
442     hss = HandleSignalsState();
443     EXPECT_EQ(
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);
448
449     // Not yet readable.
450     waiter.Init();
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);
459
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),
466                                     true));
467
468     // Should already be readable.
469     waiter.Init();
470     hss = HandleSignalsState();
471     EXPECT_EQ(
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);
476
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);
482
483     // Should still be readable.
484     waiter.Init();
485     hss = HandleSignalsState();
486     EXPECT_EQ(
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);
491
492     // Peek one element.
493     elements[0] = -1;
494     elements[1] = -1;
495     num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
496     EXPECT_EQ(
497         MOJO_RESULT_OK,
498         dp->ConsumerReadData(UserPointer<void>(elements),
499                              MakeUserPointer(&num_bytes),
500                              true,
501                              true));
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]);
505
506     // Should still be readable.
507     waiter.Init();
508     hss = HandleSignalsState();
509     EXPECT_EQ(
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);
514
515     // Read one element.
516     elements[0] = -1;
517     elements[1] = -1;
518     num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
519     EXPECT_EQ(
520         MOJO_RESULT_OK,
521         dp->ConsumerReadData(UserPointer<void>(elements),
522                              MakeUserPointer(&num_bytes),
523                              true,
524                              false));
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]);
528
529     // Adding a waiter should now succeed.
530     waiter.Init();
531     ASSERT_EQ(MOJO_RESULT_OK,
532               dp->ConsumerAddWaiter(
533                   &waiter, MOJO_HANDLE_SIGNAL_READABLE, 90, nullptr));
534
535     // Write one element.
536     elements[0] = 789;
537     elements[1] = -1;
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),
542                                     true));
543
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);
551
552     // Close the producer.
553     dp->ProducerClose();
554
555     // Should still be readable.
556     waiter.Init();
557     hss = HandleSignalsState();
558     EXPECT_EQ(
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);
563
564     // Read one element.
565     elements[0] = -1;
566     elements[1] = -1;
567     num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0]));
568     EXPECT_EQ(
569         MOJO_RESULT_OK,
570         dp->ConsumerReadData(UserPointer<void>(elements),
571                              MakeUserPointer(&num_bytes),
572                              true,
573                              false));
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]);
577
578     // Should be never-readable.
579     waiter.Init();
580     hss = HandleSignalsState();
581     EXPECT_EQ(
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);
586
587     dp->ConsumerClose();
588   }
589
590   // Test with two-phase APIs and closing the producer with an active consumer
591   // waiter.
592   {
593     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
594     Waiter waiter;
595     uint32_t context = 0;
596     HandleSignalsState hss;
597
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));
606     EXPECT_TRUE(buffer);
607     EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0])));
608     elements = static_cast<int32_t*>(buffer);
609     elements[0] = 123;
610     elements[1] = 456;
611     EXPECT_EQ(MOJO_RESULT_OK,
612               dp->ProducerEndWriteData(
613                   static_cast<uint32_t>(2u * sizeof(elements[0]))));
614
615     // Should already be readable.
616     waiter.Init();
617     hss = HandleSignalsState();
618     EXPECT_EQ(
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);
623
624     // Read one element.
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]));
628     EXPECT_EQ(
629         MOJO_RESULT_OK,
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]))));
639
640     // Should still be readable.
641     waiter.Init();
642     hss = HandleSignalsState();
643     EXPECT_EQ(
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);
648
649     // Read one element.
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]));
653     EXPECT_EQ(
654         MOJO_RESULT_OK,
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]))));
664
665     // Adding a waiter should now succeed.
666     waiter.Init();
667     ASSERT_EQ(MOJO_RESULT_OK,
668               dp->ConsumerAddWaiter(
669                   &waiter, MOJO_HANDLE_SIGNAL_READABLE, 56, nullptr));
670
671     // Close the producer.
672     dp->ProducerClose();
673
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);
681
682     dp->ConsumerClose();
683   }
684 }
685
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|.
693   };
694   MojoCreateDataPipeOptions validated_options = {0};
695   EXPECT_EQ(MOJO_RESULT_OK,
696             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
697                                             &validated_options));
698
699   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
700   Waiter waiter;
701   HandleSignalsState hss;
702
703   // It should be writable.
704   waiter.Init();
705   hss = HandleSignalsState();
706   EXPECT_EQ(
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);
711
712   uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
713   void* write_ptr = nullptr;
714   EXPECT_EQ(
715       MOJO_RESULT_OK,
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)));
720
721   // At this point, it shouldn't be writable.
722   waiter.Init();
723   ASSERT_EQ(
724       MOJO_RESULT_OK,
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);
731
732   // It shouldn't be readable yet either.
733   waiter.Init();
734   ASSERT_EQ(
735       MOJO_RESULT_OK,
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);
742
743   static_cast<int32_t*>(write_ptr)[0] = 123;
744   EXPECT_EQ(
745       MOJO_RESULT_OK,
746       dp->ProducerEndWriteData(static_cast<uint32_t>(1u * sizeof(int32_t))));
747
748   // It should be writable again.
749   waiter.Init();
750   hss = HandleSignalsState();
751   EXPECT_EQ(
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);
756
757   // And readable.
758   waiter.Init();
759   hss = HandleSignalsState();
760   EXPECT_EQ(
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);
765
766   // Start another two-phase write and check that it's readable even in the
767   // middle of it.
768   num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
769   write_ptr = nullptr;
770   EXPECT_EQ(
771       MOJO_RESULT_OK,
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)));
776
777   // It should be readable.
778   waiter.Init();
779   hss = HandleSignalsState();
780   EXPECT_EQ(
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);
785
786   // End the two-phase write without writing anything.
787   EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
788
789   // Start a two-phase read.
790   num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t));
791   const void* read_ptr = nullptr;
792   EXPECT_EQ(
793       MOJO_RESULT_OK,
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);
798
799   // At this point, it should still be writable.
800   waiter.Init();
801   hss = HandleSignalsState();
802   EXPECT_EQ(
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);
807
808   // But not readable.
809   waiter.Init();
810   ASSERT_EQ(
811       MOJO_RESULT_OK,
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);
818
819   // End the two-phase read without reading anything.
820   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
821
822   // It should be readable again.
823   waiter.Init();
824   hss = HandleSignalsState();
825   EXPECT_EQ(
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);
830
831   dp->ProducerClose();
832   dp->ConsumerClose();
833 }
834
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|.
842   };
843   MojoCreateDataPipeOptions validated_options = {0};
844   EXPECT_EQ(MOJO_RESULT_OK,
845             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
846                                             &validated_options));
847
848   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
849   Waiter waiter;
850   HandleSignalsState hss;
851
852   // Writable.
853   waiter.Init();
854   hss = HandleSignalsState();
855   EXPECT_EQ(
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);
860
861   // Not readable.
862   waiter.Init();
863   ASSERT_EQ(
864       MOJO_RESULT_OK,
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);
871
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),
877                                   false));
878   EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
879
880   // Still writable (even though it's full).
881   waiter.Init();
882   hss = HandleSignalsState();
883   EXPECT_EQ(
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);
888
889   // Now readable.
890   waiter.Init();
891   hss = HandleSignalsState();
892   EXPECT_EQ(
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);
897
898   // Overwrite that element.
899   num_bytes = static_cast<uint32_t>(sizeof(int32_t));
900   element = 456;
901   EXPECT_EQ(MOJO_RESULT_OK,
902             dp->ProducerWriteData(UserPointer<const void>(&element),
903                                   MakeUserPointer(&num_bytes),
904                                   false));
905   EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
906
907   // Still writable.
908   waiter.Init();
909   hss = HandleSignalsState();
910   EXPECT_EQ(
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);
915
916   // And still readable.
917   waiter.Init();
918   hss = HandleSignalsState();
919   EXPECT_EQ(
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);
924
925   // Read that element.
926   num_bytes = static_cast<uint32_t>(sizeof(int32_t));
927   element = 0;
928   EXPECT_EQ(
929       MOJO_RESULT_OK,
930       dp->ConsumerReadData(UserPointer<void>(&element),
931                            MakeUserPointer(&num_bytes),
932                            false,
933                            false));
934   EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes);
935   EXPECT_EQ(456, element);
936
937   // Still writable.
938   waiter.Init();
939   hss = HandleSignalsState();
940   EXPECT_EQ(
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);
945
946   // No longer readable.
947   waiter.Init();
948   ASSERT_EQ(
949       MOJO_RESULT_OK,
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);
956
957   dp->ProducerClose();
958   dp->ConsumerClose();
959 }
960
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);
964 }
965
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|.
972   };
973   MojoCreateDataPipeOptions validated_options = {0};
974   EXPECT_EQ(MOJO_RESULT_OK,
975             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
976                                             &validated_options));
977
978   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
979
980   int32_t buffer[100] = {0};
981   uint32_t num_bytes = 0;
982
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.)
987   EXPECT_EQ(
988       MOJO_RESULT_OK,
989       dp->ProducerWriteData(
990           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), false));
991   EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
992
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),
999                                  false,
1000                                  false));
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
1008
1009   // Write a bit more than the space that's available.
1010   num_bytes = 8u * sizeof(int32_t);
1011   Seq(100, arraysize(buffer), buffer);
1012   EXPECT_EQ(
1013       MOJO_RESULT_OK,
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
1019
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),
1026                                  false,
1027                                  false));
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, -, -
1038
1039   // Write one integer.
1040   num_bytes = 1u * sizeof(int32_t);
1041   Seq(200, arraysize(buffer), buffer);
1042   EXPECT_EQ(
1043       MOJO_RESULT_OK,
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, -
1049
1050   // Write five more.
1051   num_bytes = 5u * sizeof(int32_t);
1052   Seq(300, arraysize(buffer), buffer);
1053   EXPECT_EQ(
1054       MOJO_RESULT_OK,
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
1060
1061   // Read it all.
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),
1067                                  false,
1068                                  false));
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)));
1082
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).
1087
1088   num_bytes = 0u;
1089   void* write_ptr = nullptr;
1090   EXPECT_EQ(
1091       MOJO_RESULT_OK,
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
1100
1101   // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none"
1102   // mode.
1103   num_bytes = 6u * sizeof(int32_t);
1104   write_ptr = nullptr;
1105   EXPECT_EQ(
1106       MOJO_RESULT_OK,
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
1114
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;
1118   EXPECT_EQ(
1119       MOJO_RESULT_OUT_OF_RANGE,
1120       dp->ProducerBeginWriteData(
1121           MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1122
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;
1127   EXPECT_EQ(
1128       MOJO_RESULT_OK,
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
1137
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;
1142   EXPECT_EQ(
1143       MOJO_RESULT_OK,
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, -, -
1152
1153   // Read everything.
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),
1159                                  false,
1160                                  false));
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)));
1172
1173   dp->ProducerClose();
1174   dp->ConsumerClose();
1175 }
1176
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|.
1183   };
1184   MojoCreateDataPipeOptions validated_options = {0};
1185   EXPECT_EQ(MOJO_RESULT_OK,
1186             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1187                                             &validated_options));
1188
1189   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1190
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);
1195   EXPECT_EQ(
1196       MOJO_RESULT_OUT_OF_RANGE,
1197       dp->ProducerWriteData(
1198           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1199
1200   // Should still be empty.
1201   num_bytes = ~0u;
1202   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1203   EXPECT_EQ(0u, num_bytes);
1204
1205   // Write some data.
1206   num_bytes = 5u * sizeof(int32_t);
1207   Seq(100, arraysize(buffer), buffer);
1208   EXPECT_EQ(
1209       MOJO_RESULT_OK,
1210       dp->ProducerWriteData(
1211           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1212   EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1213
1214   // Half full.
1215   num_bytes = 0u;
1216   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1217   EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1218
1219   // Too much.
1220   num_bytes = 6u * sizeof(int32_t);
1221   Seq(200, arraysize(buffer), buffer);
1222   EXPECT_EQ(
1223       MOJO_RESULT_OUT_OF_RANGE,
1224       dp->ProducerWriteData(
1225           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1226
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),
1233                                  true,
1234                                  false));
1235   int32_t expected_buffer[100];
1236   memset(expected_buffer, 0xab, sizeof(expected_buffer));
1237   EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1238
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));
1243
1244   // Just a little.
1245   num_bytes = 2u * sizeof(int32_t);
1246   Seq(300, arraysize(buffer), buffer);
1247   EXPECT_EQ(
1248       MOJO_RESULT_OK,
1249       dp->ProducerWriteData(
1250           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1251   EXPECT_EQ(2u * sizeof(int32_t), num_bytes);
1252
1253   // Just right.
1254   num_bytes = 3u * sizeof(int32_t);
1255   Seq(400, arraysize(buffer), buffer);
1256   EXPECT_EQ(
1257       MOJO_RESULT_OK,
1258       dp->ProducerWriteData(
1259           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1260   EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
1261
1262   // Exactly full.
1263   num_bytes = 0u;
1264   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1265   EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1266
1267   // Read half.
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),
1273                                  true,
1274                                  false));
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)));
1279
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),
1286                                  true,
1287                                  false));
1288   memset(expected_buffer, 0xab, sizeof(expected_buffer));
1289   EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1290
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));
1295
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);
1301
1302   // Three left.
1303   num_bytes = 0u;
1304   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1305   EXPECT_EQ(3u * sizeof(int32_t), num_bytes);
1306
1307   // Close the producer, then test producer-closed cases.
1308   dp->ProducerClose();
1309
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),
1316                                  true,
1317                                  false));
1318   memset(expected_buffer, 0xab, sizeof(expected_buffer));
1319   EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1320
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));
1325
1326   // Read a little.
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),
1332                                  true,
1333                                  false));
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)));
1338
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);
1344
1345   // Empty again.
1346   num_bytes = ~0u;
1347   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1348   EXPECT_EQ(0u, num_bytes);
1349
1350   dp->ConsumerClose();
1351 }
1352
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|.
1359   };
1360   MojoCreateDataPipeOptions validated_options = {0};
1361   EXPECT_EQ(MOJO_RESULT_OK,
1362             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1363                                             &validated_options));
1364
1365   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1366
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);
1371   EXPECT_EQ(
1372       MOJO_RESULT_OUT_OF_RANGE,
1373       dp->ProducerWriteData(
1374           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1375
1376   // Write some stuff.
1377   num_bytes = 5u * sizeof(int32_t);
1378   Seq(100, arraysize(buffer), buffer);
1379   EXPECT_EQ(
1380       MOJO_RESULT_OK,
1381       dp->ProducerWriteData(
1382           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1383   EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1384
1385   // Write lots of stuff (discarding all but "104").
1386   num_bytes = 9u * sizeof(int32_t);
1387   Seq(200, arraysize(buffer), buffer);
1388   EXPECT_EQ(
1389       MOJO_RESULT_OK,
1390       dp->ProducerWriteData(
1391           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1392   EXPECT_EQ(9u * sizeof(int32_t), num_bytes);
1393
1394   // Read one.
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),
1400                                  true,
1401                                  false));
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)));
1407
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),
1414                                  true,
1415                                  false));
1416   memset(expected_buffer, 0xab, sizeof(expected_buffer));
1417   EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer)));
1418
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));
1423
1424   // Discard a bunch.
1425   num_bytes = 4u * sizeof(int32_t);
1426   EXPECT_EQ(MOJO_RESULT_OK,
1427             dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), true));
1428
1429   // Half full.
1430   num_bytes = 0u;
1431   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1432   EXPECT_EQ(5u * sizeof(int32_t), num_bytes);
1433
1434   // Write as much as possible.
1435   num_bytes = 10u * sizeof(int32_t);
1436   Seq(300, arraysize(buffer), buffer);
1437   EXPECT_EQ(
1438       MOJO_RESULT_OK,
1439       dp->ProducerWriteData(
1440           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1441   EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1442
1443   // Read everything.
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),
1449                                  true,
1450                                  false));
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)));
1455
1456   // Note: All-or-none two-phase writes on a "may discard" data pipe are tested
1457   // in LocalDataPipeTest.MayDiscard.
1458
1459   dp->ProducerClose();
1460   dp->ConsumerClose();
1461 }
1462
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|.
1469   };
1470   MojoCreateDataPipeOptions validated_options = {0};
1471   EXPECT_EQ(MOJO_RESULT_OK,
1472             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1473                                             &validated_options));
1474
1475   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1476
1477   // Try writing way too much (two-phase).
1478   uint32_t num_bytes = 20u * sizeof(int32_t);
1479   void* write_ptr = nullptr;
1480   EXPECT_EQ(
1481       MOJO_RESULT_OUT_OF_RANGE,
1482       dp->ProducerBeginWriteData(
1483           MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1484
1485   // Try writing an amount which isn't a multiple of the element size
1486   // (two-phase).
1487   static_assert(sizeof(int32_t) > 1u, "Wow! int32_t's have size 1");
1488   num_bytes = 1u;
1489   write_ptr = nullptr;
1490   EXPECT_EQ(
1491       MOJO_RESULT_INVALID_ARGUMENT,
1492       dp->ProducerBeginWriteData(
1493           MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1494
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));
1501
1502   // Write half (two-phase).
1503   num_bytes = 5u * sizeof(int32_t);
1504   write_ptr = nullptr;
1505   EXPECT_EQ(
1506       MOJO_RESULT_OK,
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)));
1514
1515   // Try reading an amount which isn't a multiple of the element size
1516   // (two-phase).
1517   num_bytes = 1u;
1518   read_ptr = nullptr;
1519   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
1520             dp->ConsumerBeginReadData(
1521                 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1522
1523   // Read one (two-phase).
1524   num_bytes = 1u * sizeof(int32_t);
1525   read_ptr = nullptr;
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)));
1532
1533   // We should have four left, leaving room for six.
1534   num_bytes = 0u;
1535   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1536   EXPECT_EQ(4u * sizeof(int32_t), num_bytes);
1537
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;
1542   EXPECT_EQ(
1543       MOJO_RESULT_OUT_OF_RANGE,
1544       dp->ProducerBeginWriteData(
1545           MakeUserPointer(&write_ptr), MakeUserPointer(&num_bytes), true));
1546
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);
1551   EXPECT_EQ(
1552       MOJO_RESULT_OK,
1553       dp->ProducerWriteData(
1554           UserPointer<const void>(buffer), MakeUserPointer(&num_bytes), true));
1555   EXPECT_EQ(6u * sizeof(int32_t), num_bytes);
1556
1557   // We have ten.
1558   num_bytes = 0u;
1559   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1560   EXPECT_EQ(10u * sizeof(int32_t), num_bytes);
1561
1562   // But a two-phase read of ten should fail.
1563   num_bytes = 10u * sizeof(int32_t);
1564   read_ptr = nullptr;
1565   EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
1566             dp->ConsumerBeginReadData(
1567                 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1568
1569   // Close the producer.
1570   dp->ProducerClose();
1571
1572   // A two-phase read of nine should work.
1573   num_bytes = 9u * sizeof(int32_t);
1574   read_ptr = nullptr;
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)));
1589
1590   // A two-phase read of two should fail, with "failed precondition".
1591   num_bytes = 2u * sizeof(int32_t);
1592   read_ptr = nullptr;
1593   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1594             dp->ConsumerBeginReadData(
1595                 MakeUserPointer(&read_ptr), MakeUserPointer(&num_bytes), true));
1596
1597   dp->ConsumerClose();
1598 }
1599
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
1603 // this.)
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);
1608
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|.
1614   };
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
1620   // pipe more space.
1621   ASSERT_EQ(100u, validated_options.capacity_num_bytes);
1622
1623   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1624
1625   // Write 20 bytes.
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),
1630                                   false));
1631   EXPECT_EQ(20u, num_bytes);
1632
1633   // Read 10 bytes.
1634   unsigned char read_buffer[1000] = {0};
1635   num_bytes = 10u;
1636   EXPECT_EQ(
1637       MOJO_RESULT_OK,
1638       dp->ConsumerReadData(UserPointer<void>(read_buffer),
1639                            MakeUserPointer(&num_bytes),
1640                            false,
1641                            false));
1642   EXPECT_EQ(10u, num_bytes);
1643   EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u));
1644
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;
1649   num_bytes = 0u;
1650   EXPECT_EQ(MOJO_RESULT_OK,
1651             dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1652                                        MakeUserPointer(&num_bytes),
1653                                        false));
1654   EXPECT_TRUE(write_buffer_ptr);
1655   EXPECT_EQ(80u, num_bytes);
1656   EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u));
1657
1658   // Write as much data as we can (using |ProducerWriteData()|). We should write
1659   // 90 bytes.
1660   num_bytes = 200u;
1661   EXPECT_EQ(MOJO_RESULT_OK,
1662             dp->ProducerWriteData(UserPointer<const void>(&test_data[20]),
1663                                   MakeUserPointer(&num_bytes),
1664                                   false));
1665   EXPECT_EQ(90u, num_bytes);
1666
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;
1671   num_bytes = 0u;
1672   EXPECT_EQ(MOJO_RESULT_OK,
1673             dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1674                                       MakeUserPointer(&num_bytes),
1675                                       false));
1676   EXPECT_TRUE(read_buffer_ptr);
1677   EXPECT_EQ(90u, num_bytes);
1678   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u));
1679
1680   // Read as much as possible (using |ConsumerReadData()|). We should read 100
1681   // bytes.
1682   num_bytes =
1683       static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0]));
1684   memset(read_buffer, 0, num_bytes);
1685   EXPECT_EQ(
1686       MOJO_RESULT_OK,
1687       dp->ConsumerReadData(UserPointer<void>(read_buffer),
1688                            MakeUserPointer(&num_bytes),
1689                            false,
1690                            false));
1691   EXPECT_EQ(100u, num_bytes);
1692   EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u));
1693
1694   dp->ProducerClose();
1695   dp->ConsumerClose();
1696 }
1697
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));
1703
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|.
1709   };
1710   MojoCreateDataPipeOptions validated_options = {0};
1711   EXPECT_EQ(MOJO_RESULT_OK,
1712             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1713                                             &validated_options));
1714
1715   // Close producer first, then consumer.
1716   {
1717     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1718
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),
1724                                     false));
1725     EXPECT_EQ(kTestDataSize, num_bytes);
1726
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),
1732                                     false));
1733     EXPECT_EQ(kTestDataSize, num_bytes);
1734
1735     // Start two-phase write.
1736     void* write_buffer_ptr = nullptr;
1737     num_bytes = 0u;
1738     EXPECT_EQ(MOJO_RESULT_OK,
1739               dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1740                                          MakeUserPointer(&num_bytes),
1741                                          false));
1742     EXPECT_TRUE(write_buffer_ptr);
1743     EXPECT_GT(num_bytes, 0u);
1744
1745     // Start two-phase read.
1746     const void* read_buffer_ptr = nullptr;
1747     num_bytes = 0u;
1748     EXPECT_EQ(MOJO_RESULT_OK,
1749               dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1750                                         MakeUserPointer(&num_bytes),
1751                                         false));
1752     EXPECT_TRUE(read_buffer_ptr);
1753     EXPECT_EQ(2u * kTestDataSize, num_bytes);
1754
1755     // Close the producer.
1756     dp->ProducerClose();
1757
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));
1761
1762     // And start another.
1763     read_buffer_ptr = nullptr;
1764     num_bytes = 0u;
1765     EXPECT_EQ(MOJO_RESULT_OK,
1766               dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1767                                         MakeUserPointer(&num_bytes),
1768                                         false));
1769     EXPECT_TRUE(read_buffer_ptr);
1770     EXPECT_EQ(kTestDataSize, num_bytes);
1771
1772     // Close the consumer, which cancels the two-phase read.
1773     dp->ConsumerClose();
1774   }
1775
1776   // Close consumer first, then producer.
1777   {
1778     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1779
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),
1785                                     false));
1786     EXPECT_EQ(kTestDataSize, num_bytes);
1787
1788     // Start two-phase write.
1789     void* write_buffer_ptr = nullptr;
1790     num_bytes = 0u;
1791     EXPECT_EQ(MOJO_RESULT_OK,
1792               dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1793                                          MakeUserPointer(&num_bytes),
1794                                          false));
1795     EXPECT_TRUE(write_buffer_ptr);
1796     ASSERT_GT(num_bytes, kTestDataSize);
1797
1798     // Start two-phase read.
1799     const void* read_buffer_ptr = nullptr;
1800     num_bytes = 0u;
1801     EXPECT_EQ(MOJO_RESULT_OK,
1802               dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1803                                         MakeUserPointer(&num_bytes),
1804                                         false));
1805     EXPECT_TRUE(read_buffer_ptr);
1806     EXPECT_EQ(kTestDataSize, num_bytes);
1807
1808     // Close the consumer.
1809     dp->ConsumerClose();
1810
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));
1817
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),
1823                                     false));
1824
1825     // As will trying to start another two-phase write.
1826     write_buffer_ptr = nullptr;
1827     num_bytes = 0u;
1828     EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1829               dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr),
1830                                          MakeUserPointer(&num_bytes),
1831                                          false));
1832
1833     dp->ProducerClose();
1834   }
1835
1836   // Test closing the consumer first, then the producer, with an active
1837   // two-phase write.
1838   {
1839     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1840
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),
1847                                          false));
1848     EXPECT_TRUE(write_buffer_ptr);
1849     ASSERT_GT(num_bytes, kTestDataSize);
1850
1851     dp->ConsumerClose();
1852     dp->ProducerClose();
1853   }
1854
1855   // Test closing the producer and then trying to read (with no data).
1856   {
1857     scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1858
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),
1864                                     false));
1865     EXPECT_EQ(kTestDataSize, num_bytes);
1866
1867     // Close the producer.
1868     dp->ProducerClose();
1869
1870     // Peek that data.
1871     char buffer[1000];
1872     num_bytes = static_cast<uint32_t>(sizeof(buffer));
1873     EXPECT_EQ(
1874         MOJO_RESULT_OK,
1875         dp->ConsumerReadData(UserPointer<void>(buffer),
1876                              MakeUserPointer(&num_bytes),
1877                              false,
1878                              true));
1879     EXPECT_EQ(kTestDataSize, num_bytes);
1880     EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize));
1881
1882     // Read that data.
1883     memset(buffer, 0, 1000);
1884     num_bytes = static_cast<uint32_t>(sizeof(buffer));
1885     EXPECT_EQ(
1886         MOJO_RESULT_OK,
1887         dp->ConsumerReadData(UserPointer<void>(buffer),
1888                              MakeUserPointer(&num_bytes),
1889                              false,
1890                              false));
1891     EXPECT_EQ(kTestDataSize, num_bytes);
1892     EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize));
1893
1894     // A second read should fail.
1895     num_bytes = static_cast<uint32_t>(sizeof(buffer));
1896     EXPECT_EQ(
1897         MOJO_RESULT_FAILED_PRECONDITION,
1898         dp->ConsumerReadData(UserPointer<void>(buffer),
1899                              MakeUserPointer(&num_bytes),
1900                              false,
1901                              false));
1902
1903     // A two-phase read should also fail.
1904     const void* read_buffer_ptr = nullptr;
1905     num_bytes = 0u;
1906     EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1907               dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr),
1908                                         MakeUserPointer(&num_bytes),
1909                                         false));
1910
1911     // Ditto for discard.
1912     num_bytes = 10u;
1913     EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1914               dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false));
1915
1916     dp->ConsumerClose();
1917   }
1918 }
1919
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|.
1926   };
1927   MojoCreateDataPipeOptions validated_options = {0};
1928   EXPECT_EQ(MOJO_RESULT_OK,
1929             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
1930                                             &validated_options));
1931
1932   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
1933
1934   // No data.
1935   uint32_t num_bytes = 1000u;
1936   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1937   EXPECT_EQ(0u, num_bytes);
1938
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)));
1942
1943   // Still no data.
1944   num_bytes = 1000u;
1945   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1946   EXPECT_EQ(0u, num_bytes);
1947
1948   // Try ending a two-phase write with an invalid amount (too much).
1949   num_bytes = 0u;
1950   void* write_ptr = nullptr;
1951   EXPECT_EQ(
1952       MOJO_RESULT_OK,
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))));
1958
1959   // But the two-phase write still ended.
1960   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1961
1962   // Still no data.
1963   num_bytes = 1000u;
1964   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1965   EXPECT_EQ(0u, num_bytes);
1966
1967   // Try ending a two-phase write with an invalid amount (not a multiple of the
1968   // element size).
1969   num_bytes = 0u;
1970   write_ptr = nullptr;
1971   EXPECT_EQ(
1972       MOJO_RESULT_OK,
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));
1977
1978   // But the two-phase write still ended.
1979   EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u));
1980
1981   // Still no data.
1982   num_bytes = 1000u;
1983   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1984   EXPECT_EQ(0u, num_bytes);
1985
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),
1992                                   false));
1993
1994   // One element available.
1995   num_bytes = 0u;
1996   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
1997   EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
1998
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)));
2002
2003   // Still one element available.
2004   num_bytes = 0u;
2005   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2006   EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2007
2008   // Try ending a two-phase read with an invalid amount (too much).
2009   num_bytes = 0u;
2010   const void* read_ptr = nullptr;
2011   EXPECT_EQ(
2012       MOJO_RESULT_OK,
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))));
2018
2019   // Still one element available.
2020   num_bytes = 0u;
2021   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2022   EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2023
2024   // Try ending a two-phase read with an invalid amount (not a multiple of the
2025   // element size).
2026   num_bytes = 0u;
2027   read_ptr = nullptr;
2028   EXPECT_EQ(
2029       MOJO_RESULT_OK,
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));
2035
2036   // Still one element available.
2037   num_bytes = 0u;
2038   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes)));
2039   EXPECT_EQ(1u * sizeof(int32_t), num_bytes);
2040
2041   dp->ProducerClose();
2042   dp->ConsumerClose();
2043 }
2044
2045 // Tests that even with "may discard", the data won't change under a two-phase
2046 // read.
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|.
2058   };
2059   MojoCreateDataPipeOptions validated_options = {0};
2060   EXPECT_EQ(MOJO_RESULT_OK,
2061             DataPipe::ValidateCreateOptions(MakeUserPointer(&options),
2062                                             &validated_options));
2063
2064   scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options));
2065
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),
2072                                   false));
2073   EXPECT_EQ(2u, num_bytes);
2074
2075   // Begin reading.
2076   const void* read_ptr = nullptr;
2077   num_bytes = 2u;
2078   EXPECT_EQ(
2079       MOJO_RESULT_OK,
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]);
2085
2086   // Try to write some more. But nothing should be discardable right now.
2087   elements[0] = 'x';
2088   elements[1] = 'y';
2089   num_bytes = 2u;
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),
2098                                   false));
2099
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]);
2103
2104   // End reading.
2105   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u));
2106
2107   // Now writing should succeed.
2108   EXPECT_EQ(MOJO_RESULT_OK,
2109             dp->ProducerWriteData(UserPointer<const void>(elements),
2110                                   MakeUserPointer(&num_bytes),
2111                                   false));
2112
2113   // And if we read, we should get the new values.
2114   read_ptr = nullptr;
2115   num_bytes = 2u;
2116   EXPECT_EQ(
2117       MOJO_RESULT_OK,
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]);
2123
2124   // End reading.
2125   EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u));
2126
2127   dp->ProducerClose();
2128   dp->ConsumerClose();
2129 }
2130
2131 }  // namespace
2132 }  // namespace system
2133 }  // namespace mojo