| 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 #include <stdio.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "mojo/edk/system/test/test_io_thread.h" | 24 #include "mojo/edk/system/test/test_io_thread.h" |
| 25 #include "mojo/edk/system/transport_data.h" | 25 #include "mojo/edk/system/transport_data.h" |
| 26 #include "mojo/edk/test/test_utils.h" | 26 #include "mojo/edk/test/test_utils.h" |
| 27 #include "mojo/edk/util/make_unique.h" | 27 #include "mojo/edk/util/make_unique.h" |
| 28 #include "mojo/edk/util/mutex.h" | 28 #include "mojo/edk/util/mutex.h" |
| 29 #include "mojo/edk/util/scoped_file.h" | 29 #include "mojo/edk/util/scoped_file.h" |
| 30 #include "mojo/edk/util/waitable_event.h" | 30 #include "mojo/edk/util/waitable_event.h" |
| 31 #include "mojo/public/cpp/system/macros.h" | 31 #include "mojo/public/cpp/system/macros.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 33 |
| 34 using mojo::embedder::ScopedPlatformHandle; |
| 34 using mojo::util::AutoResetWaitableEvent; | 35 using mojo::util::AutoResetWaitableEvent; |
| 36 using mojo::util::MakeUnique; |
| 35 using mojo::util::Mutex; | 37 using mojo::util::Mutex; |
| 36 using mojo::util::MutexLocker; | 38 using mojo::util::MutexLocker; |
| 37 | 39 |
| 38 namespace mojo { | 40 namespace mojo { |
| 39 namespace system { | 41 namespace system { |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 44 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
| 43 std::vector<unsigned char> bytes(num_bytes, 0); | 45 std::vector<unsigned char> bytes(num_bytes, 0); |
| 44 for (size_t i = 0; i < num_bytes; i++) | 46 for (size_t i = 0; i < num_bytes; i++) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 90 |
| 89 void TearDown() override { | 91 void TearDown() override { |
| 90 io_thread_.Stop(); | 92 io_thread_.Stop(); |
| 91 handles[0].reset(); | 93 handles[0].reset(); |
| 92 handles[1].reset(); | 94 handles[1].reset(); |
| 93 } | 95 } |
| 94 | 96 |
| 95 protected: | 97 protected: |
| 96 test::TestIOThread* io_thread() { return &io_thread_; } | 98 test::TestIOThread* io_thread() { return &io_thread_; } |
| 97 | 99 |
| 98 embedder::ScopedPlatformHandle handles[2]; | 100 ScopedPlatformHandle handles[2]; |
| 99 | 101 |
| 100 private: | 102 private: |
| 101 test::TestIOThread io_thread_; | 103 test::TestIOThread io_thread_; |
| 102 | 104 |
| 103 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); | 105 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); |
| 104 }; | 106 }; |
| 105 | 107 |
| 106 // RawChannelTest.WriteMessage ------------------------------------------------- | 108 // RawChannelTest.WriteMessage ------------------------------------------------- |
| 107 | 109 |
| 108 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { | 110 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { |
| 109 public: | 111 public: |
| 110 WriteOnlyRawChannelDelegate() {} | 112 WriteOnlyRawChannelDelegate() {} |
| 111 ~WriteOnlyRawChannelDelegate() override {} | 113 ~WriteOnlyRawChannelDelegate() override {} |
| 112 | 114 |
| 113 // |RawChannel::Delegate| implementation: | 115 // |RawChannel::Delegate| implementation: |
| 114 void OnReadMessage( | 116 void OnReadMessage(const MessageInTransit::View& /*message_view*/, |
| 115 const MessageInTransit::View& /*message_view*/, | 117 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
| 116 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { | 118 /*platform_handles*/) override { |
| 117 CHECK(false); // Should not get called. | 119 CHECK(false); // Should not get called. |
| 118 } | 120 } |
| 119 void OnError(Error error) override { | 121 void OnError(Error error) override { |
| 120 // We'll get a read (shutdown) error when the connection is closed. | 122 // We'll get a read (shutdown) error when the connection is closed. |
| 121 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 123 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
| 122 } | 124 } |
| 123 | 125 |
| 124 private: | 126 private: |
| 125 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); | 127 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); |
| 126 }; | 128 }; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 219 } |
| 218 | 220 |
| 219 // RawChannelTest.OnReadMessage ------------------------------------------------ | 221 // RawChannelTest.OnReadMessage ------------------------------------------------ |
| 220 | 222 |
| 221 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { | 223 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { |
| 222 public: | 224 public: |
| 223 ReadCheckerRawChannelDelegate() : position_(0) {} | 225 ReadCheckerRawChannelDelegate() : position_(0) {} |
| 224 ~ReadCheckerRawChannelDelegate() override {} | 226 ~ReadCheckerRawChannelDelegate() override {} |
| 225 | 227 |
| 226 // |RawChannel::Delegate| implementation (called on the I/O thread): | 228 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 227 void OnReadMessage( | 229 void OnReadMessage(const MessageInTransit::View& message_view, |
| 228 const MessageInTransit::View& message_view, | 230 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
| 229 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 231 platform_handles) override { |
| 230 EXPECT_FALSE(platform_handles); | 232 EXPECT_FALSE(platform_handles); |
| 231 | 233 |
| 232 size_t position; | 234 size_t position; |
| 233 size_t expected_size; | 235 size_t expected_size; |
| 234 bool should_signal = false; | 236 bool should_signal = false; |
| 235 { | 237 { |
| 236 MutexLocker locker(&mutex_); | 238 MutexLocker locker(&mutex_); |
| 237 CHECK_LT(position_, expected_sizes_.size()); | 239 CHECK_LT(position_, expected_sizes_.size()); |
| 238 position = position_; | 240 position = position_; |
| 239 expected_size = expected_sizes_[position]; | 241 expected_size = expected_sizes_[position]; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelWriterThread); | 334 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelWriterThread); |
| 333 }; | 335 }; |
| 334 | 336 |
| 335 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { | 337 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { |
| 336 public: | 338 public: |
| 337 explicit ReadCountdownRawChannelDelegate(size_t expected_count) | 339 explicit ReadCountdownRawChannelDelegate(size_t expected_count) |
| 338 : expected_count_(expected_count), count_(0) {} | 340 : expected_count_(expected_count), count_(0) {} |
| 339 ~ReadCountdownRawChannelDelegate() override {} | 341 ~ReadCountdownRawChannelDelegate() override {} |
| 340 | 342 |
| 341 // |RawChannel::Delegate| implementation (called on the I/O thread): | 343 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 342 void OnReadMessage( | 344 void OnReadMessage(const MessageInTransit::View& message_view, |
| 343 const MessageInTransit::View& message_view, | 345 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
| 344 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 346 platform_handles) override { |
| 345 EXPECT_FALSE(platform_handles); | 347 EXPECT_FALSE(platform_handles); |
| 346 | 348 |
| 347 EXPECT_LT(count_, expected_count_); | 349 EXPECT_LT(count_, expected_count_); |
| 348 count_++; | 350 count_++; |
| 349 | 351 |
| 350 EXPECT_TRUE( | 352 EXPECT_TRUE( |
| 351 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 353 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
| 352 | 354 |
| 353 if (count_ >= expected_count_) | 355 if (count_ >= expected_count_) |
| 354 done_event_.Signal(); | 356 done_event_.Signal(); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate { | 548 class ShutdownOnReadMessageRawChannelDelegate : public RawChannel::Delegate { |
| 547 public: | 549 public: |
| 548 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel, | 550 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel, |
| 549 bool should_destroy) | 551 bool should_destroy) |
| 550 : raw_channel_(raw_channel), | 552 : raw_channel_(raw_channel), |
| 551 should_destroy_(should_destroy), | 553 should_destroy_(should_destroy), |
| 552 did_shutdown_(false) {} | 554 did_shutdown_(false) {} |
| 553 ~ShutdownOnReadMessageRawChannelDelegate() override {} | 555 ~ShutdownOnReadMessageRawChannelDelegate() override {} |
| 554 | 556 |
| 555 // |RawChannel::Delegate| implementation (called on the I/O thread): | 557 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 556 void OnReadMessage( | 558 void OnReadMessage(const MessageInTransit::View& message_view, |
| 557 const MessageInTransit::View& message_view, | 559 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
| 558 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 560 platform_handles) override { |
| 559 EXPECT_FALSE(platform_handles); | 561 EXPECT_FALSE(platform_handles); |
| 560 EXPECT_FALSE(did_shutdown_); | 562 EXPECT_FALSE(did_shutdown_); |
| 561 EXPECT_TRUE( | 563 EXPECT_TRUE( |
| 562 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 564 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
| 563 raw_channel_->Shutdown(); | 565 raw_channel_->Shutdown(); |
| 564 if (should_destroy_) | 566 if (should_destroy_) |
| 565 delete raw_channel_; | 567 delete raw_channel_; |
| 566 did_shutdown_ = true; | 568 did_shutdown_ = true; |
| 567 done_event_.Signal(); | 569 done_event_.Signal(); |
| 568 } | 570 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 Error shutdown_on_error_type) | 624 Error shutdown_on_error_type) |
| 623 : raw_channel_(raw_channel), | 625 : raw_channel_(raw_channel), |
| 624 should_destroy_(should_destroy), | 626 should_destroy_(should_destroy), |
| 625 shutdown_on_error_type_(shutdown_on_error_type), | 627 shutdown_on_error_type_(shutdown_on_error_type), |
| 626 did_shutdown_(false) {} | 628 did_shutdown_(false) {} |
| 627 ~ShutdownOnErrorRawChannelDelegate() override {} | 629 ~ShutdownOnErrorRawChannelDelegate() override {} |
| 628 | 630 |
| 629 // |RawChannel::Delegate| implementation (called on the I/O thread): | 631 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 630 void OnReadMessage( | 632 void OnReadMessage( |
| 631 const MessageInTransit::View& /*message_view*/, | 633 const MessageInTransit::View& /*message_view*/, |
| 632 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { | 634 std::unique_ptr<std::vector<ScopedPlatformHandle>> /*platform_handles*/) |
| 635 override { |
| 633 CHECK(false); // Should not get called. | 636 CHECK(false); // Should not get called. |
| 634 } | 637 } |
| 635 void OnError(Error error) override { | 638 void OnError(Error error) override { |
| 636 EXPECT_FALSE(did_shutdown_); | 639 EXPECT_FALSE(did_shutdown_); |
| 637 if (error != shutdown_on_error_type_) | 640 if (error != shutdown_on_error_type_) |
| 638 return; | 641 return; |
| 639 raw_channel_->Shutdown(); | 642 raw_channel_->Shutdown(); |
| 640 if (should_destroy_) | 643 if (should_destroy_) |
| 641 delete raw_channel_; | 644 delete raw_channel_; |
| 642 did_shutdown_ = true; | 645 did_shutdown_ = true; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 | 724 |
| 722 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- | 725 // RawChannelTest.ReadWritePlatformHandles ------------------------------------- |
| 723 | 726 |
| 724 class ReadPlatformHandlesCheckerRawChannelDelegate | 727 class ReadPlatformHandlesCheckerRawChannelDelegate |
| 725 : public RawChannel::Delegate { | 728 : public RawChannel::Delegate { |
| 726 public: | 729 public: |
| 727 ReadPlatformHandlesCheckerRawChannelDelegate() {} | 730 ReadPlatformHandlesCheckerRawChannelDelegate() {} |
| 728 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} | 731 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} |
| 729 | 732 |
| 730 // |RawChannel::Delegate| implementation (called on the I/O thread): | 733 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 731 void OnReadMessage( | 734 void OnReadMessage(const MessageInTransit::View& message_view, |
| 732 const MessageInTransit::View& message_view, | 735 std::unique_ptr<std::vector<ScopedPlatformHandle>> |
| 733 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 736 platform_handles) override { |
| 734 const char kHello[] = "hello"; | 737 const char kHello[] = "hello"; |
| 735 | 738 |
| 736 EXPECT_EQ(sizeof(kHello), message_view.num_bytes()); | 739 EXPECT_EQ(sizeof(kHello), message_view.num_bytes()); |
| 737 EXPECT_STREQ(kHello, static_cast<const char*>(message_view.bytes())); | 740 EXPECT_STREQ(kHello, static_cast<const char*>(message_view.bytes())); |
| 738 | 741 |
| 739 ASSERT_TRUE(platform_handles); | 742 ASSERT_TRUE(platform_handles); |
| 740 ASSERT_EQ(2u, platform_handles->size()); | 743 ASSERT_EQ(2u, platform_handles->size()); |
| 741 embedder::ScopedPlatformHandle h1(platform_handles->at(0)); | 744 ScopedPlatformHandle h1(std::move(platform_handles->at(0))); |
| 742 EXPECT_TRUE(h1.is_valid()); | 745 EXPECT_TRUE(h1.is_valid()); |
| 743 embedder::ScopedPlatformHandle h2(platform_handles->at(1)); | 746 ScopedPlatformHandle h2(std::move(platform_handles->at(1))); |
| 744 EXPECT_TRUE(h2.is_valid()); | 747 EXPECT_TRUE(h2.is_valid()); |
| 745 platform_handles->clear(); | 748 platform_handles->clear(); |
| 746 | 749 |
| 747 { | 750 { |
| 748 char buffer[100] = {}; | 751 char buffer[100] = {}; |
| 749 | 752 |
| 750 util::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); | 753 util::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); |
| 751 EXPECT_TRUE(fp); | 754 EXPECT_TRUE(fp); |
| 752 rewind(fp.get()); | 755 rewind(fp.get()); |
| 753 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); | 756 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 io_thread()->PostTaskAndWait(base::Bind(&InitOnIOThread, rc_read.get(), | 794 io_thread()->PostTaskAndWait(base::Bind(&InitOnIOThread, rc_read.get(), |
| 792 base::Unretained(&read_delegate))); | 795 base::Unretained(&read_delegate))); |
| 793 | 796 |
| 794 util::ScopedFILE fp1(test_dir.CreateFile()); | 797 util::ScopedFILE fp1(test_dir.CreateFile()); |
| 795 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); | 798 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); |
| 796 util::ScopedFILE fp2(test_dir.CreateFile()); | 799 util::ScopedFILE fp2(test_dir.CreateFile()); |
| 797 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); | 800 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); |
| 798 | 801 |
| 799 { | 802 { |
| 800 const char kHello[] = "hello"; | 803 const char kHello[] = "hello"; |
| 801 embedder::ScopedPlatformHandleVectorPtr platform_handles( | 804 auto platform_handles = MakeUnique<std::vector<ScopedPlatformHandle>>(); |
| 802 new embedder::PlatformHandleVector()); | |
| 803 platform_handles->push_back( | 805 platform_handles->push_back( |
| 804 mojo::test::PlatformHandleFromFILE(std::move(fp1)).release()); | 806 mojo::test::PlatformHandleFromFILE(std::move(fp1))); |
| 805 platform_handles->push_back( | 807 platform_handles->push_back( |
| 806 mojo::test::PlatformHandleFromFILE(std::move(fp2)).release()); | 808 mojo::test::PlatformHandleFromFILE(std::move(fp2))); |
| 807 | 809 |
| 808 std::unique_ptr<MessageInTransit> message( | 810 std::unique_ptr<MessageInTransit> message( |
| 809 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, | 811 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, |
| 810 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, | 812 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, |
| 811 sizeof(kHello), kHello)); | 813 sizeof(kHello), kHello)); |
| 812 message->SetTransportData(util::MakeUnique<TransportData>( | 814 message->SetTransportData(util::MakeUnique<TransportData>( |
| 813 std::move(platform_handles), | 815 std::move(platform_handles), |
| 814 rc_write->GetSerializedPlatformHandleSize())); | 816 rc_write->GetSerializedPlatformHandleSize())); |
| 815 EXPECT_TRUE(rc_write->WriteMessage(std::move(message))); | 817 EXPECT_TRUE(rc_write->WriteMessage(std::move(message))); |
| 816 } | 818 } |
| 817 | 819 |
| 818 read_delegate.Wait(); | 820 read_delegate.Wait(); |
| 819 | 821 |
| 820 io_thread()->PostTaskAndWait( | 822 io_thread()->PostTaskAndWait( |
| 821 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); | 823 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); |
| 822 io_thread()->PostTaskAndWait( | 824 io_thread()->PostTaskAndWait( |
| 823 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); | 825 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); |
| 824 } | 826 } |
| 825 | 827 |
| 826 } // namespace | 828 } // namespace |
| 827 } // namespace system | 829 } // namespace system |
| 828 } // namespace mojo | 830 } // namespace mojo |
| OLD | NEW |