OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <stdio.h> |
8 | 9 |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" |
| 16 #include "base/files/scoped_temp_dir.h" |
12 #include "base/location.h" | 17 #include "base/location.h" |
13 #include "base/logging.h" | 18 #include "base/logging.h" |
14 #include "base/macros.h" | 19 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
17 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
18 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
19 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
20 #include "base/test/test_io_thread.h" | 25 #include "base/test/test_io_thread.h" |
21 #include "base/threading/platform_thread.h" // For |Sleep()|. | 26 #include "base/threading/platform_thread.h" // For |Sleep()|. |
22 #include "base/threading/simple_thread.h" | 27 #include "base/threading/simple_thread.h" |
23 #include "base/time/time.h" | 28 #include "base/time/time.h" |
24 #include "build/build_config.h" | 29 #include "build/build_config.h" // TODO(vtl): Remove this. |
25 #include "mojo/edk/embedder/platform_channel_pair.h" | 30 #include "mojo/edk/embedder/platform_channel_pair.h" |
26 #include "mojo/edk/embedder/platform_handle.h" | 31 #include "mojo/edk/embedder/platform_handle.h" |
27 #include "mojo/edk/embedder/scoped_platform_handle.h" | 32 #include "mojo/edk/embedder/scoped_platform_handle.h" |
28 #include "mojo/edk/system/message_in_transit.h" | 33 #include "mojo/edk/system/message_in_transit.h" |
29 #include "mojo/edk/system/test_utils.h" | 34 #include "mojo/edk/system/test_utils.h" |
| 35 #include "mojo/edk/system/transport_data.h" |
30 #include "mojo/edk/test/test_utils.h" | 36 #include "mojo/edk/test/test_utils.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
32 | 38 |
33 namespace mojo { | 39 namespace mojo { |
34 namespace system { | 40 namespace system { |
35 namespace { | 41 namespace { |
36 | 42 |
37 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 43 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
38 std::vector<unsigned char> bytes(num_bytes, 0); | 44 std::vector<unsigned char> bytes(num_bytes, 0); |
39 for (size_t i = 0; i < num_bytes; i++) | 45 for (size_t i = 0; i < num_bytes; i++) |
(...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 677 |
672 // Close the handle of the other end, which should stuff fail. | 678 // Close the handle of the other end, which should stuff fail. |
673 handles[1].reset(); | 679 handles[1].reset(); |
674 | 680 |
675 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 681 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
676 | 682 |
677 // Wait for the delegate, which will shut the |RawChannel| down. | 683 // Wait for the delegate, which will shut the |RawChannel| down. |
678 delegate.Wait(); | 684 delegate.Wait(); |
679 } | 685 } |
680 | 686 |
| 687 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- |
| 688 |
| 689 class ReadPlatformHandlesCheckerRawChannelDelegate |
| 690 : public RawChannel::Delegate { |
| 691 public: |
| 692 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} |
| 693 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} |
| 694 |
| 695 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 696 void OnReadMessage( |
| 697 const MessageInTransit::View& message_view, |
| 698 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { |
| 699 const char kHello[] = "hello"; |
| 700 |
| 701 EXPECT_EQ(sizeof(kHello), message_view.num_bytes()); |
| 702 EXPECT_STREQ(kHello, static_cast<const char*>(message_view.bytes())); |
| 703 |
| 704 ASSERT_TRUE(platform_handles); |
| 705 ASSERT_EQ(2u, platform_handles->size()); |
| 706 embedder::ScopedPlatformHandle h1(platform_handles->at(0)); |
| 707 EXPECT_TRUE(h1.is_valid()); |
| 708 embedder::ScopedPlatformHandle h2(platform_handles->at(1)); |
| 709 EXPECT_TRUE(h2.is_valid()); |
| 710 platform_handles->clear(); |
| 711 |
| 712 { |
| 713 char buffer[100] = {}; |
| 714 |
| 715 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); |
| 716 EXPECT_TRUE(fp); |
| 717 rewind(fp.get()); |
| 718 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
| 719 EXPECT_EQ('1', buffer[0]); |
| 720 } |
| 721 |
| 722 { |
| 723 char buffer[100] = {}; |
| 724 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h2.Pass(), "rb")); |
| 725 EXPECT_TRUE(fp); |
| 726 rewind(fp.get()); |
| 727 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
| 728 EXPECT_EQ('2', buffer[0]); |
| 729 } |
| 730 |
| 731 done_event_.Signal(); |
| 732 } |
| 733 void OnError(Error error) override { |
| 734 // We'll get a read (shutdown) error when the connection is closed. |
| 735 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
| 736 } |
| 737 |
| 738 void Wait() { done_event_.Wait(); } |
| 739 |
| 740 private: |
| 741 base::WaitableEvent done_event_; |
| 742 |
| 743 DISALLOW_COPY_AND_ASSIGN(ReadPlatformHandlesCheckerRawChannelDelegate); |
| 744 }; |
| 745 |
| 746 #if defined(OS_POSIX) |
| 747 #define MAYBE_ReadWritePlatformHandles ReadWritePlatformHandles |
| 748 #else |
| 749 // Not yet implemented (on Windows). |
| 750 #define MAYBE_ReadWritePlatformHandles DISABLED_ReadWritePlatformHandles |
| 751 #endif |
| 752 TEST_F(RawChannelTest, MAYBE_ReadWritePlatformHandles) { |
| 753 base::ScopedTempDir temp_dir; |
| 754 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 755 |
| 756 WriteOnlyRawChannelDelegate write_delegate; |
| 757 scoped_ptr<RawChannel> rc_write(RawChannel::Create(handles[0].Pass())); |
| 758 io_thread()->PostTaskAndWait(FROM_HERE, |
| 759 base::Bind(&InitOnIOThread, rc_write.get(), |
| 760 base::Unretained(&write_delegate))); |
| 761 |
| 762 ReadPlatformHandlesCheckerRawChannelDelegate read_delegate; |
| 763 scoped_ptr<RawChannel> rc_read(RawChannel::Create(handles[1].Pass())); |
| 764 io_thread()->PostTaskAndWait(FROM_HERE, |
| 765 base::Bind(&InitOnIOThread, rc_read.get(), |
| 766 base::Unretained(&read_delegate))); |
| 767 |
| 768 base::FilePath unused; |
| 769 base::ScopedFILE fp1( |
| 770 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 771 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); |
| 772 base::ScopedFILE fp2( |
| 773 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 774 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); |
| 775 |
| 776 { |
| 777 const char kHello[] = "hello"; |
| 778 embedder::ScopedPlatformHandleVectorPtr platform_handles( |
| 779 new embedder::PlatformHandleVector()); |
| 780 platform_handles->push_back( |
| 781 mojo::test::PlatformHandleFromFILE(fp1.Pass()).release()); |
| 782 platform_handles->push_back( |
| 783 mojo::test::PlatformHandleFromFILE(fp2.Pass()).release()); |
| 784 |
| 785 scoped_ptr<MessageInTransit> message(new MessageInTransit( |
| 786 MessageInTransit::kTypeEndpoint, MessageInTransit::kSubtypeEndpointData, |
| 787 sizeof(kHello), kHello)); |
| 788 message->SetTransportData( |
| 789 make_scoped_ptr(new TransportData(platform_handles.Pass()))); |
| 790 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); |
| 791 } |
| 792 |
| 793 read_delegate.Wait(); |
| 794 |
| 795 io_thread()->PostTaskAndWait( |
| 796 FROM_HERE, |
| 797 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); |
| 798 io_thread()->PostTaskAndWait( |
| 799 FROM_HERE, |
| 800 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); |
| 801 } |
| 802 |
681 } // namespace | 803 } // namespace |
682 } // namespace system | 804 } // namespace system |
683 } // namespace mojo | 805 } // namespace mojo |
OLD | NEW |