| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "mojo/edk/embedder/platform_channel_pair.h" | 16 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 17 #include "mojo/edk/embedder/platform_handle.h" | 17 #include "mojo/edk/platform/platform_handle.h" |
| 18 #include "mojo/edk/embedder/scoped_platform_handle.h" | 18 #include "mojo/edk/platform/scoped_platform_handle.h" |
| 19 #include "mojo/edk/system/message_in_transit.h" | 19 #include "mojo/edk/system/message_in_transit.h" |
| 20 #include "mojo/edk/system/test/random.h" | 20 #include "mojo/edk/system/test/random.h" |
| 21 #include "mojo/edk/system/test/scoped_test_dir.h" | 21 #include "mojo/edk/system/test/scoped_test_dir.h" |
| 22 #include "mojo/edk/system/test/simple_test_thread.h" | 22 #include "mojo/edk/system/test/simple_test_thread.h" |
| 23 #include "mojo/edk/system/test/sleep.h" | 23 #include "mojo/edk/system/test/sleep.h" |
| 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::platform::PlatformHandle; |
| 35 using mojo::platform::ScopedPlatformHandle; |
| 35 using mojo::util::AutoResetWaitableEvent; | 36 using mojo::util::AutoResetWaitableEvent; |
| 36 using mojo::util::MakeUnique; | 37 using mojo::util::MakeUnique; |
| 37 using mojo::util::Mutex; | 38 using mojo::util::Mutex; |
| 38 using mojo::util::MutexLocker; | 39 using mojo::util::MutexLocker; |
| 39 | 40 |
| 40 namespace mojo { | 41 namespace mojo { |
| 41 namespace system { | 42 namespace system { |
| 42 namespace { | 43 namespace { |
| 43 | 44 |
| 44 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 45 std::unique_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 57 if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 58 if (b[i] != static_cast<unsigned char>(i + num_bytes)) |
| 58 return false; | 59 return false; |
| 59 } | 60 } |
| 60 return true; | 61 return true; |
| 61 } | 62 } |
| 62 | 63 |
| 63 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { | 64 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { |
| 64 raw_channel->Init(delegate); | 65 raw_channel->Init(delegate); |
| 65 } | 66 } |
| 66 | 67 |
| 67 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, | 68 bool WriteTestMessageToHandle(const PlatformHandle& handle, |
| 68 uint32_t num_bytes) { | 69 uint32_t num_bytes) { |
| 69 std::unique_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 70 std::unique_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); |
| 70 | 71 |
| 71 size_t write_size = 0; | 72 size_t write_size = 0; |
| 72 mojo::test::BlockingWrite(handle, message->main_buffer(), | 73 mojo::test::BlockingWrite(handle, message->main_buffer(), |
| 73 message->main_buffer_size(), &write_size); | 74 message->main_buffer_size(), &write_size); |
| 74 return write_size == message->main_buffer_size(); | 75 return write_size == message->main_buffer_size(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 // ----------------------------------------------------------------------------- | 78 // ----------------------------------------------------------------------------- |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 126 |
| 126 private: | 127 private: |
| 127 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); | 128 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 static const unsigned kMessageReaderSleepMs = 1; | 131 static const unsigned kMessageReaderSleepMs = 1; |
| 131 static const size_t kMessageReaderMaxPollIterations = 3000; | 132 static const size_t kMessageReaderMaxPollIterations = 3000; |
| 132 | 133 |
| 133 class TestMessageReaderAndChecker { | 134 class TestMessageReaderAndChecker { |
| 134 public: | 135 public: |
| 135 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) | 136 explicit TestMessageReaderAndChecker(PlatformHandle handle) |
| 136 : handle_(handle) {} | 137 : handle_(handle) {} |
| 137 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } | 138 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } |
| 138 | 139 |
| 139 bool ReadAndCheckNextMessage(uint32_t expected_size) { | 140 bool ReadAndCheckNextMessage(uint32_t expected_size) { |
| 140 unsigned char buffer[4096]; | 141 unsigned char buffer[4096]; |
| 141 | 142 |
| 142 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { | 143 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { |
| 143 size_t read_size = 0; | 144 size_t read_size = 0; |
| 144 CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer), | 145 CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer), |
| 145 &read_size)); | 146 &read_size)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 i++; | 180 i++; |
| 180 test::SleepMilliseconds(kMessageReaderSleepMs); | 181 test::SleepMilliseconds(kMessageReaderSleepMs); |
| 181 } | 182 } |
| 182 } | 183 } |
| 183 | 184 |
| 184 LOG(ERROR) << "Too many iterations."; | 185 LOG(ERROR) << "Too many iterations."; |
| 185 return false; | 186 return false; |
| 186 } | 187 } |
| 187 | 188 |
| 188 private: | 189 private: |
| 189 const embedder::PlatformHandle handle_; | 190 const PlatformHandle handle_; |
| 190 | 191 |
| 191 // The start of the received data should always be on a message boundary. | 192 // The start of the received data should always be on a message boundary. |
| 192 std::vector<unsigned char> bytes_; | 193 std::vector<unsigned char> bytes_; |
| 193 | 194 |
| 194 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); | 195 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); |
| 195 }; | 196 }; |
| 196 | 197 |
| 197 // Tests writing (and verifies reading using our own custom reader). | 198 // Tests writing (and verifies reading using our own custom reader). |
| 198 TEST_F(RawChannelTest, WriteMessage) { | 199 TEST_F(RawChannelTest, WriteMessage) { |
| 199 WriteOnlyRawChannelDelegate delegate; | 200 WriteOnlyRawChannelDelegate delegate; |
| (...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 | 822 |
| 822 io_thread()->PostTaskAndWait( | 823 io_thread()->PostTaskAndWait( |
| 823 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); | 824 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); |
| 824 io_thread()->PostTaskAndWait( | 825 io_thread()->PostTaskAndWait( |
| 825 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); | 826 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); |
| 826 } | 827 } |
| 827 | 828 |
| 828 } // namespace | 829 } // namespace |
| 829 } // namespace system | 830 } // namespace system |
| 830 } // namespace mojo | 831 } // namespace mojo |
| OLD | NEW |