| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // This file contains tests that are shared between different implementations of | 5 //#include "mojo/edk/system/data_pipe_impl.h" |
| 6 // |DataPipeImpl|. | |
| 7 | |
| 8 #include "mojo/edk/system/data_pipe_impl.h" | |
| 9 | 6 |
| 10 #include <stdint.h> | 7 #include <stdint.h> |
| 11 | 8 |
| 12 #include "base/bind.h" | 9 #include "base/bind.h" |
| 13 #include "base/location.h" | 10 #include "base/location.h" |
| 14 #include "base/logging.h" | 11 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 17 #include "base/test/test_io_thread.h" | 14 #include "base/test/test_io_thread.h" |
| 18 #include "mojo/edk/embedder/platform_channel_pair.h" | 15 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 19 #include "mojo/edk/embedder/simple_platform_support.h" | 16 #include "mojo/edk/embedder/simple_platform_support.h" |
| 20 #include "mojo/edk/system/channel.h" | 17 //#include "mojo/edk/system/channel.h" |
| 21 #include "mojo/edk/system/channel_endpoint.h" | 18 //#include "mojo/edk/system/channel_endpoint.h" |
| 22 #include "mojo/edk/system/data_pipe.h" | 19 //#include "mojo/edk/system/data_pipe.h" |
| 23 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" | 20 //#include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
| 24 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" | 21 //#include "mojo/edk/system/data_pipe_producer_dispatcher.h" |
| 25 #include "mojo/edk/system/memory.h" | 22 #include "mojo/edk/system/memory.h" |
| 26 #include "mojo/edk/system/message_pipe.h" | 23 //#include "mojo/edk/system/message_pipe.h" |
| 27 #include "mojo/edk/system/raw_channel.h" | 24 //#include "mojo/edk/system/raw_channel.h" |
| 28 #include "mojo/edk/system/test_utils.h" | 25 #include "mojo/edk/system/test_utils.h" |
| 29 #include "mojo/edk/system/waiter.h" | 26 #include "mojo/edk/system/waiter.h" |
| 27 #include "mojo/edk/test/scoped_ipc_support.h" |
| 28 #include "mojo/public/c/system/data_pipe.h" |
| 29 #include "mojo/public/c/system/functions.h" |
| 30 #include "mojo/public/cpp/system/macros.h" | 30 #include "mojo/public/cpp/system/macros.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 32 |
| 33 namespace mojo { | 33 namespace mojo { |
| 34 namespace system { | 34 namespace system { |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const MojoHandleSignals kAllSignals = MOJO_HANDLE_SIGNAL_READABLE | | 37 const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | |
| 38 MOJO_HANDLE_SIGNAL_WRITABLE | | 38 MOJO_HANDLE_SIGNAL_WRITABLE | |
| 39 MOJO_HANDLE_SIGNAL_PEER_CLOSED; | 39 MOJO_HANDLE_SIGNAL_PEER_CLOSED; |
| 40 const uint32_t kSizeOfOptions = | 40 const uint32_t kSizeOfOptions = |
| 41 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 41 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
| 42 | 42 |
| 43 // In various places, we have to poll (since, e.g., we can't yet wait for a | 43 // In various places, we have to poll (since, e.g., we can't yet wait for a |
| 44 // certain amount of data to be available). This is the maximum number of | 44 // certain amount of data to be available). This is the maximum number of |
| 45 // iterations (separated by a short sleep). | 45 // iterations (separated by a short sleep). |
| 46 // TODO(vtl): Get rid of this. | 46 // TODO(vtl): Get rid of this. |
| 47 const size_t kMaxPoll = 100; | 47 const size_t kMaxPoll = 100; |
| 48 | 48 /* |
| 49 // DataPipeImplTestHelper ------------------------------------------------------ | 49 // DataPipeImplTestHelper ------------------------------------------------------ |
| 50 | 50 |
| 51 class DataPipeImplTestHelper { | 51 class DataPipeImplTestHelper { |
| 52 public: | 52 public: |
| 53 virtual ~DataPipeImplTestHelper() {} | 53 virtual ~DataPipeImplTestHelper() {} |
| 54 | 54 |
| 55 virtual void SetUp() = 0; | 55 virtual void SetUp() = 0; |
| 56 virtual void TearDown() = 0; | 56 virtual void TearDown() = 0; |
| 57 | 57 |
| 58 virtual void Create(const MojoCreateDataPipeOptions& validated_options) = 0; | 58 virtual void Create(const MojoCreateDataPipeOptions& validated_options) = 0; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 277 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 278 transport.End(); | 278 transport.End(); |
| 279 } | 279 } |
| 280 uint32_t context = 0; | 280 uint32_t context = 0; |
| 281 ASSERT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionDeadline(), &context)); | 281 ASSERT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionDeadline(), &context)); |
| 282 EXPECT_EQ(987u, context); | 282 EXPECT_EQ(987u, context); |
| 283 HandleSignalsState hss = HandleSignalsState(); | 283 HandleSignalsState hss = HandleSignalsState(); |
| 284 message_pipe(dest_i)->RemoveAwakable(0, &waiter, &hss); | 284 message_pipe(dest_i)->RemoveAwakable(0, &waiter, &hss); |
| 285 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, | 285 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 286 hss.satisfied_signals); | 286 hss.satisfied_signals); |
| 287 EXPECT_EQ(kAllSignals, hss.satisfiable_signals); | 287 EXPECT_EQ(kSignalAll, hss.satisfiable_signals); |
| 288 char read_buffer[100] = {}; | 288 char read_buffer[100] = {}; |
| 289 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); | 289 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
| 290 DispatcherVector read_dispatchers; | 290 DispatcherVector read_dispatchers; |
| 291 uint32_t read_num_dispatchers = 10; // Maximum to get. | 291 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 292 ASSERT_EQ(MOJO_RESULT_OK, | 292 ASSERT_EQ(MOJO_RESULT_OK, |
| 293 message_pipe(dest_i)->ReadMessage( | 293 message_pipe(dest_i)->ReadMessage( |
| 294 0, UserPointer<void>(read_buffer), | 294 0, UserPointer<void>(read_buffer), |
| 295 MakeUserPointer(&read_buffer_size), &read_dispatchers, | 295 MakeUserPointer(&read_buffer_size), &read_dispatchers, |
| 296 &read_num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE)); | 296 &read_num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE)); |
| 297 EXPECT_EQ(0u, static_cast<size_t>(read_buffer_size)); | 297 EXPECT_EQ(0u, static_cast<size_t>(read_buffer_size)); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 &test_options[0])); | 586 &test_options[0])); |
| 587 | 587 |
| 588 for (size_t i = 0; i < MOJO_ARRAYSIZE(test_options); i++) { | 588 for (size_t i = 0; i < MOJO_ARRAYSIZE(test_options); i++) { |
| 589 this->Create(test_options[i]); | 589 this->Create(test_options[i]); |
| 590 this->DoTransfer(); | 590 this->DoTransfer(); |
| 591 this->ProducerClose(); | 591 this->ProducerClose(); |
| 592 this->ConsumerClose(); | 592 this->ConsumerClose(); |
| 593 this->Reset(); | 593 this->Reset(); |
| 594 } | 594 } |
| 595 } | 595 } |
| 596 */ |
| 596 | 597 |
| 597 TYPED_TEST(DataPipeImplTest, SimpleReadWrite) { | 598 class DataPipeTest : public testing::Test { |
| 599 public: |
| 600 DataPipeTest() |
| 601 : test_io_thread_(base::TestIOThread::kAutoStart) {} |
| 602 ~DataPipeTest() override {} |
| 603 |
| 604 protected: |
| 605 base::TestIOThread& test_io_thread() { return test_io_thread_; } |
| 606 scoped_refptr<base::TaskRunner> test_io_task_runner() { |
| 607 return test_io_thread_.task_runner(); |
| 608 } |
| 609 |
| 610 private: |
| 611 base::MessageLoop message_loop_; |
| 612 base::TestIOThread test_io_thread_; |
| 613 |
| 614 MOJO_DISALLOW_COPY_AND_ASSIGN(DataPipeTest); |
| 615 }; |
| 616 |
| 617 //TYPED_TEST(DataPipeImplTest, SimpleReadWrite) { |
| 618 TEST_F(DataPipeTest, SimpleReadWrite) { |
| 598 const MojoCreateDataPipeOptions options = { | 619 const MojoCreateDataPipeOptions options = { |
| 599 kSizeOfOptions, // |struct_size|. | 620 kSizeOfOptions, // |struct_size|. |
| 600 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 621 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 601 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 622 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 602 1000 * sizeof(int32_t) // |capacity_num_bytes|. | 623 1000 * sizeof(int32_t) // |capacity_num_bytes|. |
| 603 }; | 624 }; |
| 625 |
| 626 mojo::test::ScopedIPCSupport ipc_support(test_io_task_runner()); |
| 627 |
| 628 MojoHandle producer_handle, consumer_handle; |
| 629 ASSERT_EQ(MOJO_RESULT_OK, |
| 630 MojoCreateDataPipe(&options, &producer_handle, &consumer_handle)); |
| 631 |
| 632 // We can write to a data pipe handle immediately. |
| 633 int32_t elements[10] = {}; |
| 634 uint32_t num_bytes = 0; |
| 635 |
| 636 num_bytes = |
| 637 static_cast<uint32_t>(MOJO_ARRAYSIZE(elements) * sizeof(elements[0])); |
| 638 |
| 639 elements[0] = 123; |
| 640 elements[1] = 456; |
| 641 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
| 642 EXPECT_EQ( |
| 643 MOJO_RESULT_OK, |
| 644 MojoWriteData(producer_handle, &elements[0], &num_bytes, |
| 645 MOJO_WRITE_DATA_FLAG_NONE)); |
| 646 |
| 647 // Now wait for the other side to become readable. |
| 648 MojoHandleSignalsState state; |
| 649 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(consumer_handle, |
| 650 MOJO_HANDLE_SIGNAL_READABLE, |
| 651 MOJO_DEADLINE_INDEFINITE, &state)); |
| 652 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, state.satisfied_signals); |
| 653 |
| 654 elements[0] = -1; |
| 655 elements[1] = -1; |
| 656 EXPECT_EQ(MOJO_RESULT_OK, |
| 657 MojoReadData(consumer_handle, &elements[0], &num_bytes, |
| 658 MOJO_READ_DATA_FLAG_NONE)); |
| 659 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
| 660 EXPECT_EQ(elements[0], 123); |
| 661 EXPECT_EQ(elements[1], 456); |
| 662 |
| 663 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer_handle)); |
| 664 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer_handle)); |
| 665 } |
| 666 |
| 667 /* |
| 604 this->Create(options); | 668 this->Create(options); |
| 605 this->DoTransfer(); | 669 this->DoTransfer(); |
| 606 | 670 |
| 607 Waiter waiter; | 671 Waiter waiter; |
| 608 HandleSignalsState hss; | 672 HandleSignalsState hss; |
| 609 uint32_t context; | 673 uint32_t context; |
| 610 | 674 |
| 611 int32_t elements[10] = {}; | 675 int32_t elements[10] = {}; |
| 612 uint32_t num_bytes = 0; | 676 uint32_t num_bytes = 0; |
| 613 | 677 |
| (...skipping 1026 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1640 | 1704 |
| 1641 // Empty again. | 1705 // Empty again. |
| 1642 num_bytes = ~0u; | 1706 num_bytes = ~0u; |
| 1643 EXPECT_EQ(MOJO_RESULT_OK, | 1707 EXPECT_EQ(MOJO_RESULT_OK, |
| 1644 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 1708 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
| 1645 EXPECT_EQ(0u, num_bytes); | 1709 EXPECT_EQ(0u, num_bytes); |
| 1646 | 1710 |
| 1647 this->ConsumerClose(); | 1711 this->ConsumerClose(); |
| 1648 } | 1712 } |
| 1649 | 1713 |
| 1650 TYPED_TEST(DataPipeImplTest, TwoPhaseAllOrNone) { | 1714 TYPED_TEST(DataPipeImplTest, DISABLED_TwoPhaseAllOrNone) { |
| 1651 const MojoCreateDataPipeOptions options = { | 1715 const MojoCreateDataPipeOptions options = { |
| 1652 kSizeOfOptions, // |struct_size|. | 1716 kSizeOfOptions, // |struct_size|. |
| 1653 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. | 1717 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 1654 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. | 1718 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. |
| 1655 10 * sizeof(int32_t) // |capacity_num_bytes|. | 1719 10 * sizeof(int32_t) // |capacity_num_bytes|. |
| 1656 }; | 1720 }; |
| 1657 this->Create(options); | 1721 this->Create(options); |
| 1658 this->DoTransfer(); | 1722 this->DoTransfer(); |
| 1659 | 1723 |
| 1660 Waiter waiter; | 1724 Waiter waiter; |
| (...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2386 | 2450 |
| 2387 // Still one element available. | 2451 // Still one element available. |
| 2388 num_bytes = 0u; | 2452 num_bytes = 0u; |
| 2389 EXPECT_EQ(MOJO_RESULT_OK, | 2453 EXPECT_EQ(MOJO_RESULT_OK, |
| 2390 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); | 2454 this->ConsumerQueryData(MakeUserPointer(&num_bytes))); |
| 2391 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 2455 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
| 2392 | 2456 |
| 2393 this->ProducerClose(); | 2457 this->ProducerClose(); |
| 2394 this->ConsumerClose(); | 2458 this->ConsumerClose(); |
| 2395 } | 2459 } |
| 2396 | 2460 */ |
| 2397 } // namespace | 2461 } // namespace |
| 2398 } // namespace system | 2462 } // namespace system |
| 2399 } // namespace mojo | 2463 } // namespace mojo |
| OLD | NEW |