| 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 <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/synchronization/lock.h" |
| 22 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
| 23 #include "base/test/test_io_thread.h" | 24 #include "base/test/test_io_thread.h" |
| 24 #include "base/threading/simple_thread.h" | 25 #include "base/threading/simple_thread.h" |
| 25 #include "build/build_config.h" // TODO(vtl): Remove this. | 26 #include "build/build_config.h" // TODO(vtl): Remove this. |
| 26 #include "mojo/edk/embedder/platform_channel_pair.h" | 27 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 27 #include "mojo/edk/embedder/platform_handle.h" | 28 #include "mojo/edk/embedder/platform_handle.h" |
| 28 #include "mojo/edk/embedder/scoped_platform_handle.h" | 29 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 29 #include "mojo/edk/system/message_in_transit.h" | 30 #include "mojo/edk/system/message_in_transit.h" |
| 30 #include "mojo/edk/system/mutex.h" | |
| 31 #include "mojo/edk/system/test_utils.h" | 31 #include "mojo/edk/system/test_utils.h" |
| 32 #include "mojo/edk/system/transport_data.h" | 32 #include "mojo/edk/system/transport_data.h" |
| 33 #include "mojo/edk/test/test_utils.h" | 33 #include "mojo/edk/test/test_utils.h" |
| 34 #include "mojo/public/cpp/system/macros.h" | 34 #include "mojo/public/cpp/system/macros.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 36 |
| 37 namespace mojo { | 37 namespace mojo { |
| 38 namespace system { | 38 namespace edk { |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 41 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { |
| 42 std::vector<unsigned char> bytes(num_bytes, 0); | 42 std::vector<unsigned char> bytes(num_bytes, 0); |
| 43 for (size_t i = 0; i < num_bytes; i++) | 43 for (size_t i = 0; i < num_bytes; i++) |
| 44 bytes[i] = static_cast<unsigned char>(i + num_bytes); | 44 bytes[i] = static_cast<unsigned char>(i + num_bytes); |
| 45 return make_scoped_ptr( | 45 return make_scoped_ptr( |
| 46 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, | 46 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, |
| 47 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, | 47 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, |
| 48 num_bytes, bytes.empty() ? nullptr : &bytes[0])); | 48 num_bytes, bytes.empty() ? nullptr : &bytes[0])); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { | 51 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { |
| 52 const unsigned char* b = static_cast<const unsigned char*>(bytes); | 52 const unsigned char* b = static_cast<const unsigned char*>(bytes); |
| 53 for (uint32_t i = 0; i < num_bytes; i++) { | 53 for (uint32_t i = 0; i < num_bytes; i++) { |
| 54 if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 54 if (b[i] != static_cast<unsigned char>(i + num_bytes)) |
| 55 return false; | 55 return false; |
| 56 } | 56 } |
| 57 return true; | 57 return true; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { | 60 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { |
| 61 raw_channel->Init(delegate); | 61 raw_channel->Init(delegate); |
| 62 } | 62 } |
| 63 | 63 |
| 64 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, | 64 bool WriteTestMessageToHandle(const PlatformHandle& handle, |
| 65 uint32_t num_bytes) { | 65 uint32_t num_bytes) { |
| 66 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 66 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); |
| 67 | 67 |
| 68 size_t write_size = 0; | 68 size_t write_size = 0; |
| 69 mojo::test::BlockingWrite(handle, message->main_buffer(), | 69 test::BlockingWrite(handle, message->main_buffer(), |
| 70 message->main_buffer_size(), &write_size); | 70 message->main_buffer_size(), &write_size); |
| 71 return write_size == message->main_buffer_size(); | 71 return write_size == message->main_buffer_size(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // ----------------------------------------------------------------------------- | 74 // ----------------------------------------------------------------------------- |
| 75 | 75 |
| 76 class RawChannelTest : public testing::Test { | 76 class RawChannelTest : public testing::Test { |
| 77 public: | 77 public: |
| 78 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} | 78 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} |
| 79 ~RawChannelTest() override {} | 79 ~RawChannelTest() override {} |
| 80 | 80 |
| 81 void SetUp() override { | 81 void SetUp() override { |
| 82 embedder::PlatformChannelPair channel_pair; | 82 PlatformChannelPair channel_pair; |
| 83 handles[0] = channel_pair.PassServerHandle(); | 83 handles[0] = channel_pair.PassServerHandle(); |
| 84 handles[1] = channel_pair.PassClientHandle(); | 84 handles[1] = channel_pair.PassClientHandle(); |
| 85 io_thread_.Start(); | 85 io_thread_.Start(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void TearDown() override { | 88 void TearDown() override { |
| 89 io_thread_.Stop(); | 89 io_thread_.Stop(); |
| 90 handles[0].reset(); | 90 handles[0].reset(); |
| 91 handles[1].reset(); | 91 handles[1].reset(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 protected: | 94 protected: |
| 95 base::TestIOThread* io_thread() { return &io_thread_; } | 95 base::TestIOThread* io_thread() { return &io_thread_; } |
| 96 | 96 |
| 97 embedder::ScopedPlatformHandle handles[2]; | 97 ScopedPlatformHandle handles[2]; |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 base::TestIOThread io_thread_; | 100 base::TestIOThread io_thread_; |
| 101 | 101 |
| 102 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); | 102 MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannelTest); |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 // RawChannelTest.WriteMessage ------------------------------------------------- | 105 // RawChannelTest.WriteMessage ------------------------------------------------- |
| 106 | 106 |
| 107 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { | 107 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { |
| 108 public: | 108 public: |
| 109 WriteOnlyRawChannelDelegate() {} | 109 WriteOnlyRawChannelDelegate() {} |
| 110 ~WriteOnlyRawChannelDelegate() override {} | 110 ~WriteOnlyRawChannelDelegate() override {} |
| 111 | 111 |
| 112 // |RawChannel::Delegate| implementation: | 112 // |RawChannel::Delegate| implementation: |
| 113 void OnReadMessage( | 113 void OnReadMessage( |
| 114 const MessageInTransit::View& /*message_view*/, | 114 const MessageInTransit::View& /*message_view*/, |
| 115 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { | 115 ScopedPlatformHandleVectorPtr /*platform_handles*/) override { |
| 116 CHECK(false); // Should not get called. | 116 CHECK(false); // Should not get called. |
| 117 } | 117 } |
| 118 void OnError(Error error) override { | 118 void OnError(Error error) override { |
| 119 // We'll get a read (shutdown) error when the connection is closed. | 119 // We'll get a read (shutdown) error when the connection is closed. |
| 120 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 120 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
| 121 } | 121 } |
| 122 | 122 |
| 123 private: | 123 private: |
| 124 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); | 124 MOJO_DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 static const unsigned kMessageReaderSleepMs = 1; | 127 static const unsigned kMessageReaderSleepMs = 1; |
| 128 static const size_t kMessageReaderMaxPollIterations = 3000; | 128 static const size_t kMessageReaderMaxPollIterations = 3000; |
| 129 | 129 |
| 130 class TestMessageReaderAndChecker { | 130 class TestMessageReaderAndChecker { |
| 131 public: | 131 public: |
| 132 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) | 132 explicit TestMessageReaderAndChecker(PlatformHandle handle) |
| 133 : handle_(handle) {} | 133 : handle_(handle) {} |
| 134 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } | 134 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } |
| 135 | 135 |
| 136 bool ReadAndCheckNextMessage(uint32_t expected_size) { | 136 bool ReadAndCheckNextMessage(uint32_t expected_size) { |
| 137 unsigned char buffer[4096]; | 137 unsigned char buffer[4096]; |
| 138 | 138 |
| 139 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { | 139 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { |
| 140 size_t read_size = 0; | 140 size_t read_size = 0; |
| 141 CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer), | 141 CHECK(test::NonBlockingRead(handle_, buffer, sizeof(buffer), &read_size)); |
| 142 &read_size)); | |
| 143 | 142 |
| 144 // Append newly-read data to |bytes_|. | 143 // Append newly-read data to |bytes_|. |
| 145 bytes_.insert(bytes_.end(), buffer, buffer + read_size); | 144 bytes_.insert(bytes_.end(), buffer, buffer + read_size); |
| 146 | 145 |
| 147 // If we have the header.... | 146 // If we have the header.... |
| 148 size_t message_size; | 147 size_t message_size; |
| 149 if (MessageInTransit::GetNextMessageSize( | 148 if (MessageInTransit::GetNextMessageSize( |
| 150 bytes_.empty() ? nullptr : &bytes_[0], bytes_.size(), | 149 bytes_.empty() ? nullptr : &bytes_[0], bytes_.size(), |
| 151 &message_size)) { | 150 &message_size)) { |
| 152 // If we've read the whole message.... | 151 // If we've read the whole message.... |
| (...skipping 23 matching lines...) Expand all Loading... |
| 176 i++; | 175 i++; |
| 177 test::Sleep(test::DeadlineFromMilliseconds(kMessageReaderSleepMs)); | 176 test::Sleep(test::DeadlineFromMilliseconds(kMessageReaderSleepMs)); |
| 178 } | 177 } |
| 179 } | 178 } |
| 180 | 179 |
| 181 LOG(ERROR) << "Too many iterations."; | 180 LOG(ERROR) << "Too many iterations."; |
| 182 return false; | 181 return false; |
| 183 } | 182 } |
| 184 | 183 |
| 185 private: | 184 private: |
| 186 const embedder::PlatformHandle handle_; | 185 const PlatformHandle handle_; |
| 187 | 186 |
| 188 // The start of the received data should always be on a message boundary. | 187 // The start of the received data should always be on a message boundary. |
| 189 std::vector<unsigned char> bytes_; | 188 std::vector<unsigned char> bytes_; |
| 190 | 189 |
| 191 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); | 190 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); |
| 192 }; | 191 }; |
| 193 | 192 |
| 194 // Tests writing (and verifies reading using our own custom reader). | 193 // Tests writing (and verifies reading using our own custom reader). |
| 195 TEST_F(RawChannelTest, WriteMessage) { | 194 TEST_F(RawChannelTest, WriteMessage) { |
| 196 WriteOnlyRawChannelDelegate delegate; | 195 WriteOnlyRawChannelDelegate delegate; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 219 // RawChannelTest.OnReadMessage ------------------------------------------------ | 218 // RawChannelTest.OnReadMessage ------------------------------------------------ |
| 220 | 219 |
| 221 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { | 220 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { |
| 222 public: | 221 public: |
| 223 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} | 222 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} |
| 224 ~ReadCheckerRawChannelDelegate() override {} | 223 ~ReadCheckerRawChannelDelegate() override {} |
| 225 | 224 |
| 226 // |RawChannel::Delegate| implementation (called on the I/O thread): | 225 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 227 void OnReadMessage( | 226 void OnReadMessage( |
| 228 const MessageInTransit::View& message_view, | 227 const MessageInTransit::View& message_view, |
| 229 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 228 ScopedPlatformHandleVectorPtr platform_handles) override { |
| 230 EXPECT_FALSE(platform_handles); | 229 EXPECT_FALSE(platform_handles); |
| 231 | 230 |
| 232 size_t position; | 231 size_t position; |
| 233 size_t expected_size; | 232 size_t expected_size; |
| 234 bool should_signal = false; | 233 bool should_signal = false; |
| 235 { | 234 { |
| 236 MutexLocker locker(&mutex_); | 235 base::AutoLock locker(lock_); |
| 237 CHECK_LT(position_, expected_sizes_.size()); | 236 CHECK_LT(position_, expected_sizes_.size()); |
| 238 position = position_; | 237 position = position_; |
| 239 expected_size = expected_sizes_[position]; | 238 expected_size = expected_sizes_[position]; |
| 240 position_++; | 239 position_++; |
| 241 if (position_ >= expected_sizes_.size()) | 240 if (position_ >= expected_sizes_.size()) |
| 242 should_signal = true; | 241 should_signal = true; |
| 243 } | 242 } |
| 244 | 243 |
| 245 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; | 244 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; |
| 246 if (message_view.num_bytes() == expected_size) { | 245 if (message_view.num_bytes() == expected_size) { |
| 247 EXPECT_TRUE( | 246 EXPECT_TRUE( |
| 248 CheckMessageData(message_view.bytes(), message_view.num_bytes())) | 247 CheckMessageData(message_view.bytes(), message_view.num_bytes())) |
| 249 << position; | 248 << position; |
| 250 } | 249 } |
| 251 | 250 |
| 252 if (should_signal) | 251 if (should_signal) |
| 253 done_event_.Signal(); | 252 done_event_.Signal(); |
| 254 } | 253 } |
| 255 void OnError(Error error) override { | 254 void OnError(Error error) override { |
| 256 // We'll get a read (shutdown) error when the connection is closed. | 255 // We'll get a read (shutdown) error when the connection is closed. |
| 257 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 256 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
| 258 } | 257 } |
| 259 | 258 |
| 260 // Waits for all the messages (of sizes |expected_sizes_|) to be seen. | 259 // Waits for all the messages (of sizes |expected_sizes_|) to be seen. |
| 261 void Wait() { done_event_.Wait(); } | 260 void Wait() { done_event_.Wait(); } |
| 262 | 261 |
| 263 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { | 262 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { |
| 264 MutexLocker locker(&mutex_); | 263 base::AutoLock locker(lock_); |
| 265 CHECK_EQ(position_, expected_sizes_.size()); | 264 CHECK_EQ(position_, expected_sizes_.size()); |
| 266 expected_sizes_ = expected_sizes; | 265 expected_sizes_ = expected_sizes; |
| 267 position_ = 0; | 266 position_ = 0; |
| 268 } | 267 } |
| 269 | 268 |
| 270 private: | 269 private: |
| 271 base::WaitableEvent done_event_; | 270 base::WaitableEvent done_event_; |
| 272 | 271 |
| 273 Mutex mutex_; | 272 base::Lock lock_; // Protects the following members. |
| 274 std::vector<uint32_t> expected_sizes_ MOJO_GUARDED_BY(mutex_); | 273 std::vector<uint32_t> expected_sizes_; |
| 275 size_t position_ MOJO_GUARDED_BY(mutex_); | 274 size_t position_; |
| 276 | 275 |
| 277 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); | 276 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); |
| 278 }; | 277 }; |
| 279 | 278 |
| 280 // Tests reading (writing using our own custom writer). | 279 // Tests reading (writing using our own custom writer). |
| 281 TEST_F(RawChannelTest, OnReadMessage) { | 280 TEST_F(RawChannelTest, OnReadMessage) { |
| 282 ReadCheckerRawChannelDelegate delegate; | 281 ReadCheckerRawChannelDelegate delegate; |
| 283 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); | 282 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); |
| 284 io_thread()->PostTaskAndWait( | 283 io_thread()->PostTaskAndWait( |
| 285 FROM_HERE, | 284 FROM_HERE, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 336 |
| 338 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { | 337 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { |
| 339 public: | 338 public: |
| 340 explicit ReadCountdownRawChannelDelegate(size_t expected_count) | 339 explicit ReadCountdownRawChannelDelegate(size_t expected_count) |
| 341 : done_event_(false, false), expected_count_(expected_count), count_(0) {} | 340 : done_event_(false, false), expected_count_(expected_count), count_(0) {} |
| 342 ~ReadCountdownRawChannelDelegate() override {} | 341 ~ReadCountdownRawChannelDelegate() override {} |
| 343 | 342 |
| 344 // |RawChannel::Delegate| implementation (called on the I/O thread): | 343 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 345 void OnReadMessage( | 344 void OnReadMessage( |
| 346 const MessageInTransit::View& message_view, | 345 const MessageInTransit::View& message_view, |
| 347 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 346 ScopedPlatformHandleVectorPtr platform_handles) override { |
| 348 EXPECT_FALSE(platform_handles); | 347 EXPECT_FALSE(platform_handles); |
| 349 | 348 |
| 350 EXPECT_LT(count_, expected_count_); | 349 EXPECT_LT(count_, expected_count_); |
| 351 count_++; | 350 count_++; |
| 352 | 351 |
| 353 EXPECT_TRUE( | 352 EXPECT_TRUE( |
| 354 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 353 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
| 355 | 354 |
| 356 if (count_ >= expected_count_) | 355 if (count_ >= expected_count_) |
| 357 done_event_.Signal(); | 356 done_event_.Signal(); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 bool should_destroy) | 560 bool should_destroy) |
| 562 : raw_channel_(raw_channel), | 561 : raw_channel_(raw_channel), |
| 563 should_destroy_(should_destroy), | 562 should_destroy_(should_destroy), |
| 564 done_event_(false, false), | 563 done_event_(false, false), |
| 565 did_shutdown_(false) {} | 564 did_shutdown_(false) {} |
| 566 ~ShutdownOnReadMessageRawChannelDelegate() override {} | 565 ~ShutdownOnReadMessageRawChannelDelegate() override {} |
| 567 | 566 |
| 568 // |RawChannel::Delegate| implementation (called on the I/O thread): | 567 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 569 void OnReadMessage( | 568 void OnReadMessage( |
| 570 const MessageInTransit::View& message_view, | 569 const MessageInTransit::View& message_view, |
| 571 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 570 ScopedPlatformHandleVectorPtr platform_handles) override { |
| 572 EXPECT_FALSE(platform_handles); | 571 EXPECT_FALSE(platform_handles); |
| 573 EXPECT_FALSE(did_shutdown_); | 572 EXPECT_FALSE(did_shutdown_); |
| 574 EXPECT_TRUE( | 573 EXPECT_TRUE( |
| 575 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 574 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
| 576 raw_channel_->Shutdown(); | 575 raw_channel_->Shutdown(); |
| 577 if (should_destroy_) | 576 if (should_destroy_) |
| 578 delete raw_channel_; | 577 delete raw_channel_; |
| 579 did_shutdown_ = true; | 578 did_shutdown_ = true; |
| 580 done_event_.Signal(); | 579 done_event_.Signal(); |
| 581 } | 580 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 : raw_channel_(raw_channel), | 636 : raw_channel_(raw_channel), |
| 638 should_destroy_(should_destroy), | 637 should_destroy_(should_destroy), |
| 639 shutdown_on_error_type_(shutdown_on_error_type), | 638 shutdown_on_error_type_(shutdown_on_error_type), |
| 640 done_event_(false, false), | 639 done_event_(false, false), |
| 641 did_shutdown_(false) {} | 640 did_shutdown_(false) {} |
| 642 ~ShutdownOnErrorRawChannelDelegate() override {} | 641 ~ShutdownOnErrorRawChannelDelegate() override {} |
| 643 | 642 |
| 644 // |RawChannel::Delegate| implementation (called on the I/O thread): | 643 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 645 void OnReadMessage( | 644 void OnReadMessage( |
| 646 const MessageInTransit::View& /*message_view*/, | 645 const MessageInTransit::View& /*message_view*/, |
| 647 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { | 646 ScopedPlatformHandleVectorPtr /*platform_handles*/) override { |
| 648 CHECK(false); // Should not get called. | 647 CHECK(false); // Should not get called. |
| 649 } | 648 } |
| 650 void OnError(Error error) override { | 649 void OnError(Error error) override { |
| 651 EXPECT_FALSE(did_shutdown_); | 650 EXPECT_FALSE(did_shutdown_); |
| 652 if (error != shutdown_on_error_type_) | 651 if (error != shutdown_on_error_type_) |
| 653 return; | 652 return; |
| 654 raw_channel_->Shutdown(); | 653 raw_channel_->Shutdown(); |
| 655 if (should_destroy_) | 654 if (should_destroy_) |
| 656 delete raw_channel_; | 655 delete raw_channel_; |
| 657 did_shutdown_ = true; | 656 did_shutdown_ = true; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 | 739 |
| 741 class ReadPlatformHandlesCheckerRawChannelDelegate | 740 class ReadPlatformHandlesCheckerRawChannelDelegate |
| 742 : public RawChannel::Delegate { | 741 : public RawChannel::Delegate { |
| 743 public: | 742 public: |
| 744 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} | 743 ReadPlatformHandlesCheckerRawChannelDelegate() : done_event_(false, false) {} |
| 745 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} | 744 ~ReadPlatformHandlesCheckerRawChannelDelegate() override {} |
| 746 | 745 |
| 747 // |RawChannel::Delegate| implementation (called on the I/O thread): | 746 // |RawChannel::Delegate| implementation (called on the I/O thread): |
| 748 void OnReadMessage( | 747 void OnReadMessage( |
| 749 const MessageInTransit::View& message_view, | 748 const MessageInTransit::View& message_view, |
| 750 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 749 ScopedPlatformHandleVectorPtr platform_handles) override { |
| 751 const char kHello[] = "hello"; | 750 const char kHello[] = "hello"; |
| 752 | 751 |
| 753 EXPECT_EQ(sizeof(kHello), message_view.num_bytes()); | 752 EXPECT_EQ(sizeof(kHello), message_view.num_bytes()); |
| 754 EXPECT_STREQ(kHello, static_cast<const char*>(message_view.bytes())); | 753 EXPECT_STREQ(kHello, static_cast<const char*>(message_view.bytes())); |
| 755 | 754 |
| 756 ASSERT_TRUE(platform_handles); | 755 ASSERT_TRUE(platform_handles); |
| 757 ASSERT_EQ(2u, platform_handles->size()); | 756 ASSERT_EQ(2u, platform_handles->size()); |
| 758 embedder::ScopedPlatformHandle h1(platform_handles->at(0)); | 757 ScopedPlatformHandle h1(platform_handles->at(0)); |
| 759 EXPECT_TRUE(h1.is_valid()); | 758 EXPECT_TRUE(h1.is_valid()); |
| 760 embedder::ScopedPlatformHandle h2(platform_handles->at(1)); | 759 ScopedPlatformHandle h2(platform_handles->at(1)); |
| 761 EXPECT_TRUE(h2.is_valid()); | 760 EXPECT_TRUE(h2.is_valid()); |
| 762 platform_handles->clear(); | 761 platform_handles->clear(); |
| 763 | 762 |
| 764 { | 763 { |
| 765 char buffer[100] = {}; | 764 char buffer[100] = {}; |
| 766 | 765 |
| 767 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); | 766 base::ScopedFILE fp(test::FILEFromPlatformHandle(h1.Pass(), "rb")); |
| 768 EXPECT_TRUE(fp); | 767 EXPECT_TRUE(fp); |
| 769 rewind(fp.get()); | 768 rewind(fp.get()); |
| 770 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); | 769 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
| 771 EXPECT_EQ('1', buffer[0]); | 770 EXPECT_EQ('1', buffer[0]); |
| 772 } | 771 } |
| 773 | 772 |
| 774 { | 773 { |
| 775 char buffer[100] = {}; | 774 char buffer[100] = {}; |
| 776 base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h2.Pass(), "rb")); | 775 base::ScopedFILE fp(test::FILEFromPlatformHandle(h2.Pass(), "rb")); |
| 777 EXPECT_TRUE(fp); | 776 EXPECT_TRUE(fp); |
| 778 rewind(fp.get()); | 777 rewind(fp.get()); |
| 779 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); | 778 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
| 780 EXPECT_EQ('2', buffer[0]); | 779 EXPECT_EQ('2', buffer[0]); |
| 781 } | 780 } |
| 782 | 781 |
| 783 done_event_.Signal(); | 782 done_event_.Signal(); |
| 784 } | 783 } |
| 785 void OnError(Error error) override { | 784 void OnError(Error error) override { |
| 786 // We'll get a read (shutdown) error when the connection is closed. | 785 // We'll get a read (shutdown) error when the connection is closed. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 base::FilePath unused; | 819 base::FilePath unused; |
| 821 base::ScopedFILE fp1( | 820 base::ScopedFILE fp1( |
| 822 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 821 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 823 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); | 822 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); |
| 824 base::ScopedFILE fp2( | 823 base::ScopedFILE fp2( |
| 825 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 824 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 826 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); | 825 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); |
| 827 | 826 |
| 828 { | 827 { |
| 829 const char kHello[] = "hello"; | 828 const char kHello[] = "hello"; |
| 830 embedder::ScopedPlatformHandleVectorPtr platform_handles( | 829 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector()); |
| 831 new embedder::PlatformHandleVector()); | |
| 832 platform_handles->push_back( | 830 platform_handles->push_back( |
| 833 mojo::test::PlatformHandleFromFILE(fp1.Pass()).release()); | 831 test::PlatformHandleFromFILE(fp1.Pass()).release()); |
| 834 platform_handles->push_back( | 832 platform_handles->push_back( |
| 835 mojo::test::PlatformHandleFromFILE(fp2.Pass()).release()); | 833 test::PlatformHandleFromFILE(fp2.Pass()).release()); |
| 836 | 834 |
| 837 scoped_ptr<MessageInTransit> message( | 835 scoped_ptr<MessageInTransit> message( |
| 838 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, | 836 new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT, |
| 839 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, | 837 MessageInTransit::Subtype::ENDPOINT_CLIENT_DATA, |
| 840 sizeof(kHello), kHello)); | 838 sizeof(kHello), kHello)); |
| 841 message->SetTransportData(make_scoped_ptr(new TransportData( | 839 message->SetTransportData(make_scoped_ptr(new TransportData( |
| 842 platform_handles.Pass(), rc_write->GetSerializedPlatformHandleSize()))); | 840 platform_handles.Pass(), rc_write->GetSerializedPlatformHandleSize()))); |
| 843 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); | 841 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); |
| 844 } | 842 } |
| 845 | 843 |
| 846 read_delegate.Wait(); | 844 read_delegate.Wait(); |
| 847 | 845 |
| 848 io_thread()->PostTaskAndWait( | 846 io_thread()->PostTaskAndWait( |
| 849 FROM_HERE, | 847 FROM_HERE, |
| 850 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); | 848 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); |
| 851 io_thread()->PostTaskAndWait( | 849 io_thread()->PostTaskAndWait( |
| 852 FROM_HERE, | 850 FROM_HERE, |
| 853 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); | 851 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); |
| 854 } | 852 } |
| 855 | 853 |
| 856 } // namespace | 854 } // namespace |
| 857 } // namespace system | 855 } // namespace edk |
| 858 } // namespace mojo | 856 } // namespace mojo |
| OLD | NEW |