OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/system/local_data_pipe.h" | 5 #include "mojo/edk/system/local_data_pipe_impl.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "mojo/edk/system/data_pipe.h" | 11 #include "mojo/edk/system/data_pipe.h" |
12 #include "mojo/edk/system/waiter.h" | 12 #include "mojo/edk/system/waiter.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace system { | 16 namespace system { |
17 namespace { | 17 namespace { |
18 | 18 |
19 const uint32_t kSizeOfOptions = | 19 const uint32_t kSizeOfOptions = |
20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
21 | 21 |
22 // Validate options. | 22 // Validate options. |
23 TEST(LocalDataPipeTest, Creation) { | 23 TEST(LocalDataPipeImplTest, Creation) { |
24 // Create using default options. | 24 // Create using default options. |
25 { | 25 { |
26 // Get default options. | 26 // Get default options. |
27 MojoCreateDataPipeOptions default_options = {0}; | 27 MojoCreateDataPipeOptions default_options = {0}; |
28 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 28 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
29 NullUserPointer(), &default_options)); | 29 NullUserPointer(), &default_options)); |
30 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); | 30 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(default_options)); |
31 dp->ProducerClose(); | 31 dp->ProducerClose(); |
32 dp->ConsumerClose(); | 32 dp->ConsumerClose(); |
33 } | 33 } |
34 | 34 |
35 // Create using non-default options. | 35 // Create using non-default options. |
36 { | 36 { |
37 const MojoCreateDataPipeOptions options = { | 37 const MojoCreateDataPipeOptions options = { |
38 kSizeOfOptions, // |struct_size|. | 38 kSizeOfOptions, // |struct_size|. |
39 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 39 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
40 1, // |element_num_bytes|. | 40 1, // |element_num_bytes|. |
41 1000 // |capacity_num_bytes|. | 41 1000 // |capacity_num_bytes|. |
42 }; | 42 }; |
43 MojoCreateDataPipeOptions validated_options = {0}; | 43 MojoCreateDataPipeOptions validated_options = {0}; |
44 EXPECT_EQ(MOJO_RESULT_OK, | 44 EXPECT_EQ(MOJO_RESULT_OK, |
45 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), | 45 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
46 &validated_options)); | 46 &validated_options)); |
47 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 47 scoped_refptr<LocalDataPipeImpl> dp( |
| 48 new LocalDataPipeImpl(validated_options)); |
48 dp->ProducerClose(); | 49 dp->ProducerClose(); |
49 dp->ConsumerClose(); | 50 dp->ConsumerClose(); |
50 } | 51 } |
51 { | 52 { |
52 const MojoCreateDataPipeOptions options = { | 53 const MojoCreateDataPipeOptions options = { |
53 kSizeOfOptions, // |struct_size|. | 54 kSizeOfOptions, // |struct_size|. |
54 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 55 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
55 4, // |element_num_bytes|. | 56 4, // |element_num_bytes|. |
56 4000 // |capacity_num_bytes|. | 57 4000 // |capacity_num_bytes|. |
57 }; | 58 }; |
58 MojoCreateDataPipeOptions validated_options = {0}; | 59 MojoCreateDataPipeOptions validated_options = {0}; |
59 EXPECT_EQ(MOJO_RESULT_OK, | 60 EXPECT_EQ(MOJO_RESULT_OK, |
60 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), | 61 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
61 &validated_options)); | 62 &validated_options)); |
62 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 63 scoped_refptr<LocalDataPipeImpl> dp( |
| 64 new LocalDataPipeImpl(validated_options)); |
63 dp->ProducerClose(); | 65 dp->ProducerClose(); |
64 dp->ConsumerClose(); | 66 dp->ConsumerClose(); |
65 } | 67 } |
66 { | 68 { |
67 const MojoCreateDataPipeOptions options = { | 69 const MojoCreateDataPipeOptions options = { |
68 kSizeOfOptions, // |struct_size|. | 70 kSizeOfOptions, // |struct_size|. |
69 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 71 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
70 7, // |element_num_bytes|. | 72 7, // |element_num_bytes|. |
71 7000000 // |capacity_num_bytes|. | 73 7000000 // |capacity_num_bytes|. |
72 }; | 74 }; |
73 MojoCreateDataPipeOptions validated_options = {0}; | 75 MojoCreateDataPipeOptions validated_options = {0}; |
74 EXPECT_EQ(MOJO_RESULT_OK, | 76 EXPECT_EQ(MOJO_RESULT_OK, |
75 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), | 77 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
76 &validated_options)); | 78 &validated_options)); |
77 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 79 scoped_refptr<LocalDataPipeImpl> dp( |
| 80 new LocalDataPipeImpl(validated_options)); |
78 dp->ProducerClose(); | 81 dp->ProducerClose(); |
79 dp->ConsumerClose(); | 82 dp->ConsumerClose(); |
80 } | 83 } |
81 // Default capacity. | 84 // Default capacity. |
82 { | 85 { |
83 const MojoCreateDataPipeOptions options = { | 86 const MojoCreateDataPipeOptions options = { |
84 kSizeOfOptions, // |struct_size|. | 87 kSizeOfOptions, // |struct_size|. |
85 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 88 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
86 100, // |element_num_bytes|. | 89 100, // |element_num_bytes|. |
87 0 // |capacity_num_bytes|. | 90 0 // |capacity_num_bytes|. |
88 }; | 91 }; |
89 MojoCreateDataPipeOptions validated_options = {0}; | 92 MojoCreateDataPipeOptions validated_options = {0}; |
90 EXPECT_EQ(MOJO_RESULT_OK, | 93 EXPECT_EQ(MOJO_RESULT_OK, |
91 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), | 94 DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
92 &validated_options)); | 95 &validated_options)); |
93 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 96 scoped_refptr<LocalDataPipeImpl> dp( |
| 97 new LocalDataPipeImpl(validated_options)); |
94 dp->ProducerClose(); | 98 dp->ProducerClose(); |
95 dp->ConsumerClose(); | 99 dp->ConsumerClose(); |
96 } | 100 } |
97 } | 101 } |
98 | 102 |
99 TEST(LocalDataPipeTest, SimpleReadWrite) { | 103 TEST(LocalDataPipeImplTest, SimpleReadWrite) { |
100 const MojoCreateDataPipeOptions options = { | 104 const MojoCreateDataPipeOptions options = { |
101 kSizeOfOptions, // |struct_size|. | 105 kSizeOfOptions, // |struct_size|. |
102 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 106 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
103 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 107 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
104 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 108 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
105 }; | 109 }; |
106 MojoCreateDataPipeOptions validated_options = {0}; | 110 MojoCreateDataPipeOptions validated_options = {0}; |
107 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 111 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
108 MakeUserPointer(&options), &validated_options)); | 112 MakeUserPointer(&options), &validated_options)); |
109 | 113 |
110 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 114 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
111 | 115 |
112 int32_t elements[10] = {0}; | 116 int32_t elements[10] = {0}; |
113 uint32_t num_bytes = 0; | 117 uint32_t num_bytes = 0; |
114 | 118 |
115 // Try reading; nothing there yet. | 119 // Try reading; nothing there yet. |
116 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); | 120 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); |
117 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 121 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
118 dp->ConsumerReadData(UserPointer<void>(elements), | 122 dp->ConsumerReadData(UserPointer<void>(elements), |
119 MakeUserPointer(&num_bytes), false, false)); | 123 MakeUserPointer(&num_bytes), false, false)); |
120 | 124 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 210 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
207 EXPECT_EQ(0u, num_bytes); | 211 EXPECT_EQ(0u, num_bytes); |
208 | 212 |
209 dp->ProducerClose(); | 213 dp->ProducerClose(); |
210 dp->ConsumerClose(); | 214 dp->ConsumerClose(); |
211 } | 215 } |
212 | 216 |
213 // Note: The "basic" waiting tests test that the "wait states" are correct in | 217 // Note: The "basic" waiting tests test that the "wait states" are correct in |
214 // various situations; they don't test that waiters are properly awoken on state | 218 // various situations; they don't test that waiters are properly awoken on state |
215 // changes. (For that, we need to use multiple threads.) | 219 // changes. (For that, we need to use multiple threads.) |
216 TEST(LocalDataPipeTest, BasicProducerWaiting) { | 220 TEST(LocalDataPipeImplTest, BasicProducerWaiting) { |
217 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities | 221 // Note: We take advantage of the fact that for |LocalDataPipeImpl|, |
218 // are strict maximums. This is not guaranteed by the API. | 222 // capacities are strict maximums. This is not guaranteed by the API. |
219 | 223 |
220 const MojoCreateDataPipeOptions options = { | 224 const MojoCreateDataPipeOptions options = { |
221 kSizeOfOptions, // |struct_size|. | 225 kSizeOfOptions, // |struct_size|. |
222 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 226 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
223 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 227 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
224 2 * sizeof(int32_t) // |capacity_num_bytes|. | 228 2 * sizeof(int32_t) // |capacity_num_bytes|. |
225 }; | 229 }; |
226 MojoCreateDataPipeOptions validated_options = {0}; | 230 MojoCreateDataPipeOptions validated_options = {0}; |
227 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 231 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
228 MakeUserPointer(&options), &validated_options)); | 232 MakeUserPointer(&options), &validated_options)); |
229 | 233 |
230 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 234 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
231 Waiter waiter; | 235 Waiter waiter; |
232 uint32_t context = 0; | 236 uint32_t context = 0; |
233 HandleSignalsState hss; | 237 HandleSignalsState hss; |
234 | 238 |
235 // Never readable. | 239 // Never readable. |
236 waiter.Init(); | 240 waiter.Init(); |
237 hss = HandleSignalsState(); | 241 hss = HandleSignalsState(); |
238 EXPECT_EQ( | 242 EXPECT_EQ( |
239 MOJO_RESULT_FAILED_PRECONDITION, | 243 MOJO_RESULT_FAILED_PRECONDITION, |
240 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); | 244 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12, &hss)); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); | 389 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); |
386 EXPECT_EQ(12u, context); | 390 EXPECT_EQ(12u, context); |
387 hss = HandleSignalsState(); | 391 hss = HandleSignalsState(); |
388 dp->ProducerRemoveAwakable(&waiter, &hss); | 392 dp->ProducerRemoveAwakable(&waiter, &hss); |
389 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 393 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
390 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 394 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
391 | 395 |
392 dp->ProducerClose(); | 396 dp->ProducerClose(); |
393 } | 397 } |
394 | 398 |
395 TEST(LocalDataPipeTest, PeerClosedWaiting) { | 399 TEST(LocalDataPipeImplTest, PeerClosedWaiting) { |
396 const MojoCreateDataPipeOptions options = { | 400 const MojoCreateDataPipeOptions options = { |
397 kSizeOfOptions, // |struct_size|. | 401 kSizeOfOptions, // |struct_size|. |
398 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 402 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
399 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 403 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
400 2 * sizeof(int32_t) // |capacity_num_bytes|. | 404 2 * sizeof(int32_t) // |capacity_num_bytes|. |
401 }; | 405 }; |
402 MojoCreateDataPipeOptions validated_options = {0}; | 406 MojoCreateDataPipeOptions validated_options = {0}; |
403 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 407 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
404 MakeUserPointer(&options), &validated_options)); | 408 MakeUserPointer(&options), &validated_options)); |
405 | 409 |
406 Waiter waiter; | 410 Waiter waiter; |
407 HandleSignalsState hss; | 411 HandleSignalsState hss; |
408 | 412 |
409 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on producer. | 413 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on producer. |
410 { | 414 { |
411 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 415 scoped_refptr<LocalDataPipeImpl> dp( |
| 416 new LocalDataPipeImpl(validated_options)); |
412 // Add a waiter. | 417 // Add a waiter. |
413 waiter.Init(); | 418 waiter.Init(); |
414 ASSERT_EQ(MOJO_RESULT_OK, | 419 ASSERT_EQ(MOJO_RESULT_OK, |
415 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 420 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
416 12, nullptr)); | 421 12, nullptr)); |
417 | 422 |
418 // Close the consumer. | 423 // Close the consumer. |
419 dp->ConsumerClose(); | 424 dp->ConsumerClose(); |
420 | 425 |
421 // It should be signaled. | 426 // It should be signaled. |
422 uint32_t context = 0; | 427 uint32_t context = 0; |
423 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 428 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
424 EXPECT_EQ(12u, context); | 429 EXPECT_EQ(12u, context); |
425 hss = HandleSignalsState(); | 430 hss = HandleSignalsState(); |
426 dp->ProducerRemoveAwakable(&waiter, &hss); | 431 dp->ProducerRemoveAwakable(&waiter, &hss); |
427 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 432 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
428 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 433 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
429 | 434 |
430 dp->ProducerClose(); | 435 dp->ProducerClose(); |
431 } | 436 } |
432 | 437 |
433 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on consumer. | 438 // Check MOJO_HANDLE_SIGNAL_PEER_CLOSED on consumer. |
434 { | 439 { |
435 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 440 scoped_refptr<LocalDataPipeImpl> dp( |
| 441 new LocalDataPipeImpl(validated_options)); |
436 // Add a waiter. | 442 // Add a waiter. |
437 waiter.Init(); | 443 waiter.Init(); |
438 ASSERT_EQ(MOJO_RESULT_OK, | 444 ASSERT_EQ(MOJO_RESULT_OK, |
439 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 445 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
440 12, nullptr)); | 446 12, nullptr)); |
441 | 447 |
442 // Close the producer. | 448 // Close the producer. |
443 dp->ProducerClose(); | 449 dp->ProducerClose(); |
444 | 450 |
445 // It should be signaled. | 451 // It should be signaled. |
446 uint32_t context = 0; | 452 uint32_t context = 0; |
447 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 453 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
448 EXPECT_EQ(12u, context); | 454 EXPECT_EQ(12u, context); |
449 hss = HandleSignalsState(); | 455 hss = HandleSignalsState(); |
450 dp->ConsumerRemoveAwakable(&waiter, &hss); | 456 dp->ConsumerRemoveAwakable(&waiter, &hss); |
451 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 457 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
452 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 458 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
453 | 459 |
454 dp->ConsumerClose(); | 460 dp->ConsumerClose(); |
455 } | 461 } |
456 } | 462 } |
457 | 463 |
458 TEST(LocalDataPipeTest, BasicConsumerWaiting) { | 464 TEST(LocalDataPipeImplTest, BasicConsumerWaiting) { |
459 const MojoCreateDataPipeOptions options = { | 465 const MojoCreateDataPipeOptions options = { |
460 kSizeOfOptions, // |struct_size|. | 466 kSizeOfOptions, // |struct_size|. |
461 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 467 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
462 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 468 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
463 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 469 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
464 }; | 470 }; |
465 MojoCreateDataPipeOptions validated_options = {0}; | 471 MojoCreateDataPipeOptions validated_options = {0}; |
466 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 472 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
467 MakeUserPointer(&options), &validated_options)); | 473 MakeUserPointer(&options), &validated_options)); |
468 | 474 |
469 { | 475 { |
470 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 476 scoped_refptr<LocalDataPipeImpl> dp( |
| 477 new LocalDataPipeImpl(validated_options)); |
471 Waiter waiter; | 478 Waiter waiter; |
472 uint32_t context = 0; | 479 uint32_t context = 0; |
473 HandleSignalsState hss; | 480 HandleSignalsState hss; |
474 | 481 |
475 // Never writable. | 482 // Never writable. |
476 waiter.Init(); | 483 waiter.Init(); |
477 hss = HandleSignalsState(); | 484 hss = HandleSignalsState(); |
478 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 485 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
479 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, | 486 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12, |
480 &hss)); | 487 &hss)); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 &hss)); | 622 &hss)); |
616 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 623 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
617 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 624 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
618 | 625 |
619 dp->ConsumerClose(); | 626 dp->ConsumerClose(); |
620 } | 627 } |
621 | 628 |
622 // Test with two-phase APIs and closing the producer with an active consumer | 629 // Test with two-phase APIs and closing the producer with an active consumer |
623 // waiter. | 630 // waiter. |
624 { | 631 { |
625 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 632 scoped_refptr<LocalDataPipeImpl> dp( |
| 633 new LocalDataPipeImpl(validated_options)); |
626 Waiter waiter; | 634 Waiter waiter; |
627 uint32_t context = 0; | 635 uint32_t context = 0; |
628 HandleSignalsState hss; | 636 HandleSignalsState hss; |
629 | 637 |
630 // Write two elements. | 638 // Write two elements. |
631 int32_t* elements = nullptr; | 639 int32_t* elements = nullptr; |
632 void* buffer = nullptr; | 640 void* buffer = nullptr; |
633 // Request room for three (but we'll only write two). | 641 // Request room for three (but we'll only write two). |
634 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 642 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
635 EXPECT_EQ(MOJO_RESULT_OK, | 643 EXPECT_EQ(MOJO_RESULT_OK, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 hss = HandleSignalsState(); | 714 hss = HandleSignalsState(); |
707 dp->ConsumerRemoveAwakable(&waiter, &hss); | 715 dp->ConsumerRemoveAwakable(&waiter, &hss); |
708 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | 716 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); |
709 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | 717 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); |
710 | 718 |
711 dp->ConsumerClose(); | 719 dp->ConsumerClose(); |
712 } | 720 } |
713 } | 721 } |
714 | 722 |
715 // Tests that data pipes aren't writable/readable during two-phase writes/reads. | 723 // Tests that data pipes aren't writable/readable during two-phase writes/reads. |
716 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { | 724 TEST(LocalDataPipeImplTest, BasicTwoPhaseWaiting) { |
717 const MojoCreateDataPipeOptions options = { | 725 const MojoCreateDataPipeOptions options = { |
718 kSizeOfOptions, // |struct_size|. | 726 kSizeOfOptions, // |struct_size|. |
719 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 727 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
720 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 728 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
721 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 729 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
722 }; | 730 }; |
723 MojoCreateDataPipeOptions validated_options = {0}; | 731 MojoCreateDataPipeOptions validated_options = {0}; |
724 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 732 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
725 MakeUserPointer(&options), &validated_options)); | 733 MakeUserPointer(&options), &validated_options)); |
726 | 734 |
727 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 735 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
728 Waiter waiter; | 736 Waiter waiter; |
729 HandleSignalsState hss; | 737 HandleSignalsState hss; |
730 | 738 |
731 // It should be writable. | 739 // It should be writable. |
732 waiter.Init(); | 740 waiter.Init(); |
733 hss = HandleSignalsState(); | 741 hss = HandleSignalsState(); |
734 EXPECT_EQ( | 742 EXPECT_EQ( |
735 MOJO_RESULT_ALREADY_EXISTS, | 743 MOJO_RESULT_ALREADY_EXISTS, |
736 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 744 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
737 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 745 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8, &hss)); | 867 dp->ConsumerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8, &hss)); |
860 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); | 868 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, hss.satisfied_signals); |
861 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 869 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
862 hss.satisfiable_signals); | 870 hss.satisfiable_signals); |
863 | 871 |
864 dp->ProducerClose(); | 872 dp->ProducerClose(); |
865 dp->ConsumerClose(); | 873 dp->ConsumerClose(); |
866 } | 874 } |
867 | 875 |
868 // Test that a "may discard" data pipe is writable even when it's full. | 876 // Test that a "may discard" data pipe is writable even when it's full. |
869 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { | 877 TEST(LocalDataPipeImplTest, BasicMayDiscardWaiting) { |
870 const MojoCreateDataPipeOptions options = { | 878 const MojoCreateDataPipeOptions options = { |
871 kSizeOfOptions, // |struct_size|. | 879 kSizeOfOptions, // |struct_size|. |
872 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 880 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
873 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 881 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
874 1 * sizeof(int32_t) // |capacity_num_bytes|. | 882 1 * sizeof(int32_t) // |capacity_num_bytes|. |
875 }; | 883 }; |
876 MojoCreateDataPipeOptions validated_options = {0}; | 884 MojoCreateDataPipeOptions validated_options = {0}; |
877 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 885 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
878 MakeUserPointer(&options), &validated_options)); | 886 MakeUserPointer(&options), &validated_options)); |
879 | 887 |
880 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 888 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
881 Waiter waiter; | 889 Waiter waiter; |
882 HandleSignalsState hss; | 890 HandleSignalsState hss; |
883 | 891 |
884 // Writable. | 892 // Writable. |
885 waiter.Init(); | 893 waiter.Init(); |
886 hss = HandleSignalsState(); | 894 hss = HandleSignalsState(); |
887 EXPECT_EQ( | 895 EXPECT_EQ( |
888 MOJO_RESULT_ALREADY_EXISTS, | 896 MOJO_RESULT_ALREADY_EXISTS, |
889 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 897 dp->ProducerAddAwakable(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
890 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); | 898 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, hss.satisfied_signals); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 | 999 |
992 dp->ProducerClose(); | 1000 dp->ProducerClose(); |
993 dp->ConsumerClose(); | 1001 dp->ConsumerClose(); |
994 } | 1002 } |
995 | 1003 |
996 void Seq(int32_t start, size_t count, int32_t* out) { | 1004 void Seq(int32_t start, size_t count, int32_t* out) { |
997 for (size_t i = 0; i < count; i++) | 1005 for (size_t i = 0; i < count; i++) |
998 out[i] = start + static_cast<int32_t>(i); | 1006 out[i] = start + static_cast<int32_t>(i); |
999 } | 1007 } |
1000 | 1008 |
1001 TEST(LocalDataPipeTest, MayDiscard) { | 1009 TEST(LocalDataPipeImplTest, MayDiscard) { |
1002 const MojoCreateDataPipeOptions options = { | 1010 const MojoCreateDataPipeOptions options = { |
1003 kSizeOfOptions, // |struct_size|. | 1011 kSizeOfOptions, // |struct_size|. |
1004 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 1012 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
1005 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1013 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1006 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1014 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1007 }; | 1015 }; |
1008 MojoCreateDataPipeOptions validated_options = {0}; | 1016 MojoCreateDataPipeOptions validated_options = {0}; |
1009 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1017 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1010 MakeUserPointer(&options), &validated_options)); | 1018 MakeUserPointer(&options), &validated_options)); |
1011 | 1019 |
1012 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1020 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1013 | 1021 |
1014 int32_t buffer[100] = {0}; | 1022 int32_t buffer[100] = {0}; |
1015 uint32_t num_bytes = 0; | 1023 uint32_t num_bytes = 0; |
1016 | 1024 |
1017 num_bytes = 20u * sizeof(int32_t); | 1025 num_bytes = 20u * sizeof(int32_t); |
1018 Seq(0, arraysize(buffer), buffer); | 1026 Seq(0, arraysize(buffer), buffer); |
1019 // Try writing more than capacity. (This test relies on the implementation | 1027 // Try writing more than capacity. (This test relies on the implementation |
1020 // enforcing the capacity strictly.) | 1028 // enforcing the capacity strictly.) |
1021 EXPECT_EQ(MOJO_RESULT_OK, | 1029 EXPECT_EQ(MOJO_RESULT_OK, |
1022 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1030 dp->ProducerWriteData(UserPointer<const void>(buffer), |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1184 expected_buffer[4] = 603; | 1192 expected_buffer[4] = 603; |
1185 expected_buffer[5] = 700; | 1193 expected_buffer[5] = 700; |
1186 expected_buffer[6] = 701; | 1194 expected_buffer[6] = 701; |
1187 expected_buffer[7] = 702; | 1195 expected_buffer[7] = 702; |
1188 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1196 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1189 | 1197 |
1190 dp->ProducerClose(); | 1198 dp->ProducerClose(); |
1191 dp->ConsumerClose(); | 1199 dp->ConsumerClose(); |
1192 } | 1200 } |
1193 | 1201 |
1194 TEST(LocalDataPipeTest, AllOrNone) { | 1202 TEST(LocalDataPipeImplTest, AllOrNone) { |
1195 const MojoCreateDataPipeOptions options = { | 1203 const MojoCreateDataPipeOptions options = { |
1196 kSizeOfOptions, // |struct_size|. | 1204 kSizeOfOptions, // |struct_size|. |
1197 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1205 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1198 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1206 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1199 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1207 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1200 }; | 1208 }; |
1201 MojoCreateDataPipeOptions validated_options = {0}; | 1209 MojoCreateDataPipeOptions validated_options = {0}; |
1202 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1210 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1203 MakeUserPointer(&options), &validated_options)); | 1211 MakeUserPointer(&options), &validated_options)); |
1204 | 1212 |
1205 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1213 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1206 | 1214 |
1207 // Try writing way too much. | 1215 // Try writing way too much. |
1208 uint32_t num_bytes = 20u * sizeof(int32_t); | 1216 uint32_t num_bytes = 20u * sizeof(int32_t); |
1209 int32_t buffer[100]; | 1217 int32_t buffer[100]; |
1210 Seq(0, arraysize(buffer), buffer); | 1218 Seq(0, arraysize(buffer), buffer); |
1211 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1219 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1212 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1220 dp->ProducerWriteData(UserPointer<const void>(buffer), |
1213 MakeUserPointer(&num_bytes), true)); | 1221 MakeUserPointer(&num_bytes), true)); |
1214 | 1222 |
1215 // Should still be empty. | 1223 // Should still be empty. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1344 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1352 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1345 | 1353 |
1346 // Empty again. | 1354 // Empty again. |
1347 num_bytes = ~0u; | 1355 num_bytes = ~0u; |
1348 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1356 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1349 EXPECT_EQ(0u, num_bytes); | 1357 EXPECT_EQ(0u, num_bytes); |
1350 | 1358 |
1351 dp->ConsumerClose(); | 1359 dp->ConsumerClose(); |
1352 } | 1360 } |
1353 | 1361 |
1354 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) { | 1362 TEST(LocalDataPipeImplTest, AllOrNoneMayDiscard) { |
1355 const MojoCreateDataPipeOptions options = { | 1363 const MojoCreateDataPipeOptions options = { |
1356 kSizeOfOptions, // |struct_size|. | 1364 kSizeOfOptions, // |struct_size|. |
1357 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 1365 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
1358 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1366 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1359 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1367 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1360 }; | 1368 }; |
1361 MojoCreateDataPipeOptions validated_options = {0}; | 1369 MojoCreateDataPipeOptions validated_options = {0}; |
1362 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1370 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1363 MakeUserPointer(&options), &validated_options)); | 1371 MakeUserPointer(&options), &validated_options)); |
1364 | 1372 |
1365 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1373 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1366 | 1374 |
1367 // Try writing way too much. | 1375 // Try writing way too much. |
1368 uint32_t num_bytes = 20u * sizeof(int32_t); | 1376 uint32_t num_bytes = 20u * sizeof(int32_t); |
1369 int32_t buffer[100]; | 1377 int32_t buffer[100]; |
1370 Seq(0, arraysize(buffer), buffer); | 1378 Seq(0, arraysize(buffer), buffer); |
1371 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1379 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1372 dp->ProducerWriteData(UserPointer<const void>(buffer), | 1380 dp->ProducerWriteData(UserPointer<const void>(buffer), |
1373 MakeUserPointer(&num_bytes), true)); | 1381 MakeUserPointer(&num_bytes), true)); |
1374 | 1382 |
1375 // Write some stuff. | 1383 // Write some stuff. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 memset(buffer, 0xab, sizeof(buffer)); | 1445 memset(buffer, 0xab, sizeof(buffer)); |
1438 EXPECT_EQ(MOJO_RESULT_OK, | 1446 EXPECT_EQ(MOJO_RESULT_OK, |
1439 dp->ConsumerReadData(UserPointer<void>(buffer), | 1447 dp->ConsumerReadData(UserPointer<void>(buffer), |
1440 MakeUserPointer(&num_bytes), true, false)); | 1448 MakeUserPointer(&num_bytes), true, false)); |
1441 memset(expected_buffer, 0xab, sizeof(expected_buffer)); | 1449 memset(expected_buffer, 0xab, sizeof(expected_buffer)); |
1442 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1450 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1443 Seq(300, 10, expected_buffer); | 1451 Seq(300, 10, expected_buffer); |
1444 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 1452 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
1445 | 1453 |
1446 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested | 1454 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested |
1447 // in LocalDataPipeTest.MayDiscard. | 1455 // in LocalDataPipeImplTest.MayDiscard. |
1448 | 1456 |
1449 dp->ProducerClose(); | 1457 dp->ProducerClose(); |
1450 dp->ConsumerClose(); | 1458 dp->ConsumerClose(); |
1451 } | 1459 } |
1452 | 1460 |
1453 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) { | 1461 TEST(LocalDataPipeImplTest, TwoPhaseAllOrNone) { |
1454 const MojoCreateDataPipeOptions options = { | 1462 const MojoCreateDataPipeOptions options = { |
1455 kSizeOfOptions, // |struct_size|. | 1463 kSizeOfOptions, // |struct_size|. |
1456 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1464 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1457 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1465 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1458 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1466 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1459 }; | 1467 }; |
1460 MojoCreateDataPipeOptions validated_options = {0}; | 1468 MojoCreateDataPipeOptions validated_options = {0}; |
1461 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1469 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1462 MakeUserPointer(&options), &validated_options)); | 1470 MakeUserPointer(&options), &validated_options)); |
1463 | 1471 |
1464 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1472 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1465 | 1473 |
1466 // Try writing way too much (two-phase). | 1474 // Try writing way too much (two-phase). |
1467 uint32_t num_bytes = 20u * sizeof(int32_t); | 1475 uint32_t num_bytes = 20u * sizeof(int32_t); |
1468 void* write_ptr = nullptr; | 1476 void* write_ptr = nullptr; |
1469 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1477 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1470 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1478 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
1471 MakeUserPointer(&num_bytes), true)); | 1479 MakeUserPointer(&num_bytes), true)); |
1472 | 1480 |
1473 // Try writing an amount which isn't a multiple of the element size | 1481 // Try writing an amount which isn't a multiple of the element size |
1474 // (two-phase). | 1482 // (two-phase). |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1578 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), | 1586 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
1579 MakeUserPointer(&num_bytes), true)); | 1587 MakeUserPointer(&num_bytes), true)); |
1580 | 1588 |
1581 dp->ConsumerClose(); | 1589 dp->ConsumerClose(); |
1582 } | 1590 } |
1583 | 1591 |
1584 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, | 1592 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, |
1585 // respectively, as much as possible, even if it has to "wrap around" the | 1593 // respectively, as much as possible, even if it has to "wrap around" the |
1586 // internal circular buffer. (Note that the two-phase write and read do not do | 1594 // internal circular buffer. (Note that the two-phase write and read do not do |
1587 // this.) | 1595 // this.) |
1588 TEST(LocalDataPipeTest, WrapAround) { | 1596 TEST(LocalDataPipeImplTest, WrapAround) { |
1589 unsigned char test_data[1000]; | 1597 unsigned char test_data[1000]; |
1590 for (size_t i = 0; i < arraysize(test_data); i++) | 1598 for (size_t i = 0; i < arraysize(test_data); i++) |
1591 test_data[i] = static_cast<unsigned char>(i); | 1599 test_data[i] = static_cast<unsigned char>(i); |
1592 | 1600 |
1593 const MojoCreateDataPipeOptions options = { | 1601 const MojoCreateDataPipeOptions options = { |
1594 kSizeOfOptions, // |struct_size|. | 1602 kSizeOfOptions, // |struct_size|. |
1595 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1603 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1596 1u, // |element_num_bytes|. | 1604 1u, // |element_num_bytes|. |
1597 100u // |capacity_num_bytes|. | 1605 100u // |capacity_num_bytes|. |
1598 }; | 1606 }; |
1599 MojoCreateDataPipeOptions validated_options = {0}; | 1607 MojoCreateDataPipeOptions validated_options = {0}; |
1600 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1608 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1601 MakeUserPointer(&options), &validated_options)); | 1609 MakeUserPointer(&options), &validated_options)); |
1602 // This test won't be valid if |ValidateCreateOptions()| decides to give the | 1610 // This test won't be valid if |ValidateCreateOptions()| decides to give the |
1603 // pipe more space. | 1611 // pipe more space. |
1604 ASSERT_EQ(100u, validated_options.capacity_num_bytes); | 1612 ASSERT_EQ(100u, validated_options.capacity_num_bytes); |
1605 | 1613 |
1606 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1614 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1607 | 1615 |
1608 // Write 20 bytes. | 1616 // Write 20 bytes. |
1609 uint32_t num_bytes = 20u; | 1617 uint32_t num_bytes = 20u; |
1610 EXPECT_EQ(MOJO_RESULT_OK, | 1618 EXPECT_EQ(MOJO_RESULT_OK, |
1611 dp->ProducerWriteData(UserPointer<const void>(&test_data[0]), | 1619 dp->ProducerWriteData(UserPointer<const void>(&test_data[0]), |
1612 MakeUserPointer(&num_bytes), false)); | 1620 MakeUserPointer(&num_bytes), false)); |
1613 EXPECT_EQ(20u, num_bytes); | 1621 EXPECT_EQ(20u, num_bytes); |
1614 | 1622 |
1615 // Read 10 bytes. | 1623 // Read 10 bytes. |
1616 unsigned char read_buffer[1000] = {0}; | 1624 unsigned char read_buffer[1000] = {0}; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1663 MakeUserPointer(&num_bytes), false, false)); | 1671 MakeUserPointer(&num_bytes), false, false)); |
1664 EXPECT_EQ(100u, num_bytes); | 1672 EXPECT_EQ(100u, num_bytes); |
1665 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u)); | 1673 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u)); |
1666 | 1674 |
1667 dp->ProducerClose(); | 1675 dp->ProducerClose(); |
1668 dp->ConsumerClose(); | 1676 dp->ConsumerClose(); |
1669 } | 1677 } |
1670 | 1678 |
1671 // Tests the behavior of closing the producer or consumer with respect to | 1679 // Tests the behavior of closing the producer or consumer with respect to |
1672 // writes and reads (simple and two-phase). | 1680 // writes and reads (simple and two-phase). |
1673 TEST(LocalDataPipeTest, CloseWriteRead) { | 1681 TEST(LocalDataPipeImplTest, CloseWriteRead) { |
1674 const char kTestData[] = "hello world"; | 1682 const char kTestData[] = "hello world"; |
1675 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); | 1683 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); |
1676 | 1684 |
1677 const MojoCreateDataPipeOptions options = { | 1685 const MojoCreateDataPipeOptions options = { |
1678 kSizeOfOptions, // |struct_size|. | 1686 kSizeOfOptions, // |struct_size|. |
1679 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1687 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1680 1u, // |element_num_bytes|. | 1688 1u, // |element_num_bytes|. |
1681 1000u // |capacity_num_bytes|. | 1689 1000u // |capacity_num_bytes|. |
1682 }; | 1690 }; |
1683 MojoCreateDataPipeOptions validated_options = {0}; | 1691 MojoCreateDataPipeOptions validated_options = {0}; |
1684 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1692 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1685 MakeUserPointer(&options), &validated_options)); | 1693 MakeUserPointer(&options), &validated_options)); |
1686 | 1694 |
1687 // Close producer first, then consumer. | 1695 // Close producer first, then consumer. |
1688 { | 1696 { |
1689 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1697 scoped_refptr<LocalDataPipeImpl> dp( |
| 1698 new LocalDataPipeImpl(validated_options)); |
1690 | 1699 |
1691 // Write some data, so we'll have something to read. | 1700 // Write some data, so we'll have something to read. |
1692 uint32_t num_bytes = kTestDataSize; | 1701 uint32_t num_bytes = kTestDataSize; |
1693 EXPECT_EQ(MOJO_RESULT_OK, | 1702 EXPECT_EQ(MOJO_RESULT_OK, |
1694 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1703 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1695 MakeUserPointer(&num_bytes), false)); | 1704 MakeUserPointer(&num_bytes), false)); |
1696 EXPECT_EQ(kTestDataSize, num_bytes); | 1705 EXPECT_EQ(kTestDataSize, num_bytes); |
1697 | 1706 |
1698 // Write it again, so we'll have something left over. | 1707 // Write it again, so we'll have something left over. |
1699 num_bytes = kTestDataSize; | 1708 num_bytes = kTestDataSize; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1735 MakeUserPointer(&num_bytes), false)); | 1744 MakeUserPointer(&num_bytes), false)); |
1736 EXPECT_TRUE(read_buffer_ptr); | 1745 EXPECT_TRUE(read_buffer_ptr); |
1737 EXPECT_EQ(kTestDataSize, num_bytes); | 1746 EXPECT_EQ(kTestDataSize, num_bytes); |
1738 | 1747 |
1739 // Close the consumer, which cancels the two-phase read. | 1748 // Close the consumer, which cancels the two-phase read. |
1740 dp->ConsumerClose(); | 1749 dp->ConsumerClose(); |
1741 } | 1750 } |
1742 | 1751 |
1743 // Close consumer first, then producer. | 1752 // Close consumer first, then producer. |
1744 { | 1753 { |
1745 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1754 scoped_refptr<LocalDataPipeImpl> dp( |
| 1755 new LocalDataPipeImpl(validated_options)); |
1746 | 1756 |
1747 // Write some data, so we'll have something to read. | 1757 // Write some data, so we'll have something to read. |
1748 uint32_t num_bytes = kTestDataSize; | 1758 uint32_t num_bytes = kTestDataSize; |
1749 EXPECT_EQ(MOJO_RESULT_OK, | 1759 EXPECT_EQ(MOJO_RESULT_OK, |
1750 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1760 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1751 MakeUserPointer(&num_bytes), false)); | 1761 MakeUserPointer(&num_bytes), false)); |
1752 EXPECT_EQ(kTestDataSize, num_bytes); | 1762 EXPECT_EQ(kTestDataSize, num_bytes); |
1753 | 1763 |
1754 // Start two-phase write. | 1764 // Start two-phase write. |
1755 void* write_buffer_ptr = nullptr; | 1765 void* write_buffer_ptr = nullptr; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1791 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1801 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1792 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1802 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1793 MakeUserPointer(&num_bytes), false)); | 1803 MakeUserPointer(&num_bytes), false)); |
1794 | 1804 |
1795 dp->ProducerClose(); | 1805 dp->ProducerClose(); |
1796 } | 1806 } |
1797 | 1807 |
1798 // Test closing the consumer first, then the producer, with an active | 1808 // Test closing the consumer first, then the producer, with an active |
1799 // two-phase write. | 1809 // two-phase write. |
1800 { | 1810 { |
1801 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1811 scoped_refptr<LocalDataPipeImpl> dp( |
| 1812 new LocalDataPipeImpl(validated_options)); |
1802 | 1813 |
1803 // Start two-phase write. | 1814 // Start two-phase write. |
1804 void* write_buffer_ptr = nullptr; | 1815 void* write_buffer_ptr = nullptr; |
1805 uint32_t num_bytes = 0u; | 1816 uint32_t num_bytes = 0u; |
1806 EXPECT_EQ(MOJO_RESULT_OK, | 1817 EXPECT_EQ(MOJO_RESULT_OK, |
1807 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1818 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1808 MakeUserPointer(&num_bytes), false)); | 1819 MakeUserPointer(&num_bytes), false)); |
1809 EXPECT_TRUE(write_buffer_ptr); | 1820 EXPECT_TRUE(write_buffer_ptr); |
1810 ASSERT_GT(num_bytes, kTestDataSize); | 1821 ASSERT_GT(num_bytes, kTestDataSize); |
1811 | 1822 |
1812 dp->ConsumerClose(); | 1823 dp->ConsumerClose(); |
1813 dp->ProducerClose(); | 1824 dp->ProducerClose(); |
1814 } | 1825 } |
1815 | 1826 |
1816 // Test closing the producer and then trying to read (with no data). | 1827 // Test closing the producer and then trying to read (with no data). |
1817 { | 1828 { |
1818 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1829 scoped_refptr<LocalDataPipeImpl> dp( |
| 1830 new LocalDataPipeImpl(validated_options)); |
1819 | 1831 |
1820 // Write some data, so we'll have something to read. | 1832 // Write some data, so we'll have something to read. |
1821 uint32_t num_bytes = kTestDataSize; | 1833 uint32_t num_bytes = kTestDataSize; |
1822 EXPECT_EQ(MOJO_RESULT_OK, | 1834 EXPECT_EQ(MOJO_RESULT_OK, |
1823 dp->ProducerWriteData(UserPointer<const void>(kTestData), | 1835 dp->ProducerWriteData(UserPointer<const void>(kTestData), |
1824 MakeUserPointer(&num_bytes), false)); | 1836 MakeUserPointer(&num_bytes), false)); |
1825 EXPECT_EQ(kTestDataSize, num_bytes); | 1837 EXPECT_EQ(kTestDataSize, num_bytes); |
1826 | 1838 |
1827 // Close the producer. | 1839 // Close the producer. |
1828 dp->ProducerClose(); | 1840 dp->ProducerClose(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1860 | 1872 |
1861 // Ditto for discard. | 1873 // Ditto for discard. |
1862 num_bytes = 10u; | 1874 num_bytes = 10u; |
1863 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1875 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1864 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); | 1876 dp->ConsumerDiscardData(MakeUserPointer(&num_bytes), false)); |
1865 | 1877 |
1866 dp->ConsumerClose(); | 1878 dp->ConsumerClose(); |
1867 } | 1879 } |
1868 } | 1880 } |
1869 | 1881 |
1870 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) { | 1882 TEST(LocalDataPipeImplTest, TwoPhaseMoreInvalidArguments) { |
1871 const MojoCreateDataPipeOptions options = { | 1883 const MojoCreateDataPipeOptions options = { |
1872 kSizeOfOptions, // |struct_size|. | 1884 kSizeOfOptions, // |struct_size|. |
1873 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1885 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
1874 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1886 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
1875 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1887 10 * sizeof(int32_t) // |capacity_num_bytes|. |
1876 }; | 1888 }; |
1877 MojoCreateDataPipeOptions validated_options = {0}; | 1889 MojoCreateDataPipeOptions validated_options = {0}; |
1878 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 1890 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
1879 MakeUserPointer(&options), &validated_options)); | 1891 MakeUserPointer(&options), &validated_options)); |
1880 | 1892 |
1881 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1893 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
1882 | 1894 |
1883 // No data. | 1895 // No data. |
1884 uint32_t num_bytes = 1000u; | 1896 uint32_t num_bytes = 1000u; |
1885 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1897 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
1886 EXPECT_EQ(0u, num_bytes); | 1898 EXPECT_EQ(0u, num_bytes); |
1887 | 1899 |
1888 // Try "ending" a two-phase write when one isn't active. | 1900 // Try "ending" a two-phase write when one isn't active. |
1889 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1901 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1890 dp->ProducerEndWriteData(1u * sizeof(int32_t))); | 1902 dp->ProducerEndWriteData(1u * sizeof(int32_t))); |
1891 | 1903 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1986 dp->ConsumerClose(); | 1998 dp->ConsumerClose(); |
1987 } | 1999 } |
1988 | 2000 |
1989 // Tests that even with "may discard", the data won't change under a two-phase | 2001 // Tests that even with "may discard", the data won't change under a two-phase |
1990 // read. | 2002 // read. |
1991 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two | 2003 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two |
1992 // related issues: First, we don't recognize that the data given to | 2004 // related issues: First, we don't recognize that the data given to |
1993 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|, | 2005 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|, |
1994 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the | 2006 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the |
1995 // |ProducerWriteData()| then changes the data underneath the two-phase read.) | 2007 // |ProducerWriteData()| then changes the data underneath the two-phase read.) |
1996 TEST(LocalDataPipeTest, DISABLED_MayDiscardTwoPhaseConsistent) { | 2008 TEST(LocalDataPipeImplTest, DISABLED_MayDiscardTwoPhaseConsistent) { |
1997 const MojoCreateDataPipeOptions options = { | 2009 const MojoCreateDataPipeOptions options = { |
1998 kSizeOfOptions, // |struct_size|. | 2010 kSizeOfOptions, // |struct_size|. |
1999 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. | 2011 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. |
2000 1, // |element_num_bytes|. | 2012 1, // |element_num_bytes|. |
2001 2 // |capacity_num_bytes|. | 2013 2 // |capacity_num_bytes|. |
2002 }; | 2014 }; |
2003 MojoCreateDataPipeOptions validated_options = {0}; | 2015 MojoCreateDataPipeOptions validated_options = {0}; |
2004 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( | 2016 EXPECT_EQ(MOJO_RESULT_OK, DataPipe::ValidateCreateOptions( |
2005 MakeUserPointer(&options), &validated_options)); | 2017 MakeUserPointer(&options), &validated_options)); |
2006 | 2018 |
2007 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 2019 scoped_refptr<LocalDataPipeImpl> dp(new LocalDataPipeImpl(validated_options)); |
2008 | 2020 |
2009 // Write some elements. | 2021 // Write some elements. |
2010 char elements[2] = {'a', 'b'}; | 2022 char elements[2] = {'a', 'b'}; |
2011 uint32_t num_bytes = 2u; | 2023 uint32_t num_bytes = 2u; |
2012 EXPECT_EQ(MOJO_RESULT_OK, | 2024 EXPECT_EQ(MOJO_RESULT_OK, |
2013 dp->ProducerWriteData(UserPointer<const void>(elements), | 2025 dp->ProducerWriteData(UserPointer<const void>(elements), |
2014 MakeUserPointer(&num_bytes), false)); | 2026 MakeUserPointer(&num_bytes), false)); |
2015 EXPECT_EQ(2u, num_bytes); | 2027 EXPECT_EQ(2u, num_bytes); |
2016 | 2028 |
2017 // Begin reading. | 2029 // Begin reading. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2062 // End reading. | 2074 // End reading. |
2063 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 2075 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
2064 | 2076 |
2065 dp->ProducerClose(); | 2077 dp->ProducerClose(); |
2066 dp->ConsumerClose(); | 2078 dp->ConsumerClose(); |
2067 } | 2079 } |
2068 | 2080 |
2069 } // namespace | 2081 } // namespace |
2070 } // namespace system | 2082 } // namespace system |
2071 } // namespace mojo | 2083 } // namespace mojo |
OLD | NEW |