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/system/raw_channel.h" | 5 #include "mojo/system/raw_channel.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 return write_size == message->main_buffer_size(); | 68 return write_size == message->main_buffer_size(); |
69 } | 69 } |
70 | 70 |
71 // ----------------------------------------------------------------------------- | 71 // ----------------------------------------------------------------------------- |
72 | 72 |
73 class RawChannelTest : public testing::Test { | 73 class RawChannelTest : public testing::Test { |
74 public: | 74 public: |
75 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} | 75 RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} |
76 virtual ~RawChannelTest() {} | 76 virtual ~RawChannelTest() {} |
77 | 77 |
78 virtual void SetUp() OVERRIDE { | 78 virtual void SetUp() override { |
79 embedder::PlatformChannelPair channel_pair; | 79 embedder::PlatformChannelPair channel_pair; |
80 handles[0] = channel_pair.PassServerHandle(); | 80 handles[0] = channel_pair.PassServerHandle(); |
81 handles[1] = channel_pair.PassClientHandle(); | 81 handles[1] = channel_pair.PassClientHandle(); |
82 io_thread_.Start(); | 82 io_thread_.Start(); |
83 } | 83 } |
84 | 84 |
85 virtual void TearDown() OVERRIDE { | 85 virtual void TearDown() override { |
86 io_thread_.Stop(); | 86 io_thread_.Stop(); |
87 handles[0].reset(); | 87 handles[0].reset(); |
88 handles[1].reset(); | 88 handles[1].reset(); |
89 } | 89 } |
90 | 90 |
91 protected: | 91 protected: |
92 base::TestIOThread* io_thread() { return &io_thread_; } | 92 base::TestIOThread* io_thread() { return &io_thread_; } |
93 | 93 |
94 embedder::ScopedPlatformHandle handles[2]; | 94 embedder::ScopedPlatformHandle handles[2]; |
95 | 95 |
96 private: | 96 private: |
97 base::TestIOThread io_thread_; | 97 base::TestIOThread io_thread_; |
98 | 98 |
99 DISALLOW_COPY_AND_ASSIGN(RawChannelTest); | 99 DISALLOW_COPY_AND_ASSIGN(RawChannelTest); |
100 }; | 100 }; |
101 | 101 |
102 // RawChannelTest.WriteMessage ------------------------------------------------- | 102 // RawChannelTest.WriteMessage ------------------------------------------------- |
103 | 103 |
104 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { | 104 class WriteOnlyRawChannelDelegate : public RawChannel::Delegate { |
105 public: | 105 public: |
106 WriteOnlyRawChannelDelegate() {} | 106 WriteOnlyRawChannelDelegate() {} |
107 virtual ~WriteOnlyRawChannelDelegate() {} | 107 virtual ~WriteOnlyRawChannelDelegate() {} |
108 | 108 |
109 // |RawChannel::Delegate| implementation: | 109 // |RawChannel::Delegate| implementation: |
110 virtual void OnReadMessage( | 110 virtual void OnReadMessage( |
111 const MessageInTransit::View& /*message_view*/, | 111 const MessageInTransit::View& /*message_view*/, |
112 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) OVERRIDE { | 112 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { |
113 CHECK(false); // Should not get called. | 113 CHECK(false); // Should not get called. |
114 } | 114 } |
115 virtual void OnError(Error error) OVERRIDE { | 115 virtual void OnError(Error error) override { |
116 // We'll get a read (shutdown) error when the connection is closed. | 116 // We'll get a read (shutdown) error when the connection is closed. |
117 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 117 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
118 } | 118 } |
119 | 119 |
120 private: | 120 private: |
121 DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); | 121 DISALLOW_COPY_AND_ASSIGN(WriteOnlyRawChannelDelegate); |
122 }; | 122 }; |
123 | 123 |
124 static const int64_t kMessageReaderSleepMs = 1; | 124 static const int64_t kMessageReaderSleepMs = 1; |
125 static const size_t kMessageReaderMaxPollIterations = 3000; | 125 static const size_t kMessageReaderMaxPollIterations = 3000; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 // RawChannelTest.OnReadMessage ------------------------------------------------ | 218 // RawChannelTest.OnReadMessage ------------------------------------------------ |
219 | 219 |
220 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { | 220 class ReadCheckerRawChannelDelegate : public RawChannel::Delegate { |
221 public: | 221 public: |
222 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} | 222 ReadCheckerRawChannelDelegate() : done_event_(false, false), position_(0) {} |
223 virtual ~ReadCheckerRawChannelDelegate() {} | 223 virtual ~ReadCheckerRawChannelDelegate() {} |
224 | 224 |
225 // |RawChannel::Delegate| implementation (called on the I/O thread): | 225 // |RawChannel::Delegate| implementation (called on the I/O thread): |
226 virtual void OnReadMessage( | 226 virtual void OnReadMessage( |
227 const MessageInTransit::View& message_view, | 227 const MessageInTransit::View& message_view, |
228 embedder::ScopedPlatformHandleVectorPtr platform_handles) OVERRIDE { | 228 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { |
229 EXPECT_FALSE(platform_handles); | 229 EXPECT_FALSE(platform_handles); |
230 | 230 |
231 size_t position; | 231 size_t position; |
232 size_t expected_size; | 232 size_t expected_size; |
233 bool should_signal = false; | 233 bool should_signal = false; |
234 { | 234 { |
235 base::AutoLock locker(lock_); | 235 base::AutoLock locker(lock_); |
236 CHECK_LT(position_, expected_sizes_.size()); | 236 CHECK_LT(position_, expected_sizes_.size()); |
237 position = position_; | 237 position = position_; |
238 expected_size = expected_sizes_[position]; | 238 expected_size = expected_sizes_[position]; |
239 position_++; | 239 position_++; |
240 if (position_ >= expected_sizes_.size()) | 240 if (position_ >= expected_sizes_.size()) |
241 should_signal = true; | 241 should_signal = true; |
242 } | 242 } |
243 | 243 |
244 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; | 244 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; |
245 if (message_view.num_bytes() == expected_size) { | 245 if (message_view.num_bytes() == expected_size) { |
246 EXPECT_TRUE( | 246 EXPECT_TRUE( |
247 CheckMessageData(message_view.bytes(), message_view.num_bytes())) | 247 CheckMessageData(message_view.bytes(), message_view.num_bytes())) |
248 << position; | 248 << position; |
249 } | 249 } |
250 | 250 |
251 if (should_signal) | 251 if (should_signal) |
252 done_event_.Signal(); | 252 done_event_.Signal(); |
253 } | 253 } |
254 virtual void OnError(Error error) OVERRIDE { | 254 virtual void OnError(Error error) override { |
255 // 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. |
256 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 256 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
257 } | 257 } |
258 | 258 |
259 // 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. |
260 void Wait() { done_event_.Wait(); } | 260 void Wait() { done_event_.Wait(); } |
261 | 261 |
262 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { | 262 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { |
263 base::AutoLock locker(lock_); | 263 base::AutoLock locker(lock_); |
264 CHECK_EQ(position_, expected_sizes_.size()); | 264 CHECK_EQ(position_, expected_sizes_.size()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 class RawChannelWriterThread : public base::SimpleThread { | 312 class RawChannelWriterThread : public base::SimpleThread { |
313 public: | 313 public: |
314 RawChannelWriterThread(RawChannel* raw_channel, size_t write_count) | 314 RawChannelWriterThread(RawChannel* raw_channel, size_t write_count) |
315 : base::SimpleThread("raw_channel_writer_thread"), | 315 : base::SimpleThread("raw_channel_writer_thread"), |
316 raw_channel_(raw_channel), | 316 raw_channel_(raw_channel), |
317 left_to_write_(write_count) {} | 317 left_to_write_(write_count) {} |
318 | 318 |
319 virtual ~RawChannelWriterThread() { Join(); } | 319 virtual ~RawChannelWriterThread() { Join(); } |
320 | 320 |
321 private: | 321 private: |
322 virtual void Run() OVERRIDE { | 322 virtual void Run() override { |
323 static const int kMaxRandomMessageSize = 25000; | 323 static const int kMaxRandomMessageSize = 25000; |
324 | 324 |
325 while (left_to_write_-- > 0) { | 325 while (left_to_write_-- > 0) { |
326 EXPECT_TRUE(raw_channel_->WriteMessage(MakeTestMessage( | 326 EXPECT_TRUE(raw_channel_->WriteMessage(MakeTestMessage( |
327 static_cast<uint32_t>(base::RandInt(1, kMaxRandomMessageSize))))); | 327 static_cast<uint32_t>(base::RandInt(1, kMaxRandomMessageSize))))); |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 RawChannel* const raw_channel_; | 331 RawChannel* const raw_channel_; |
332 size_t left_to_write_; | 332 size_t left_to_write_; |
333 | 333 |
334 DISALLOW_COPY_AND_ASSIGN(RawChannelWriterThread); | 334 DISALLOW_COPY_AND_ASSIGN(RawChannelWriterThread); |
335 }; | 335 }; |
336 | 336 |
337 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { | 337 class ReadCountdownRawChannelDelegate : public RawChannel::Delegate { |
338 public: | 338 public: |
339 explicit ReadCountdownRawChannelDelegate(size_t expected_count) | 339 explicit ReadCountdownRawChannelDelegate(size_t expected_count) |
340 : done_event_(false, false), expected_count_(expected_count), count_(0) {} | 340 : done_event_(false, false), expected_count_(expected_count), count_(0) {} |
341 virtual ~ReadCountdownRawChannelDelegate() {} | 341 virtual ~ReadCountdownRawChannelDelegate() {} |
342 | 342 |
343 // |RawChannel::Delegate| implementation (called on the I/O thread): | 343 // |RawChannel::Delegate| implementation (called on the I/O thread): |
344 virtual void OnReadMessage( | 344 virtual void OnReadMessage( |
345 const MessageInTransit::View& message_view, | 345 const MessageInTransit::View& message_view, |
346 embedder::ScopedPlatformHandleVectorPtr platform_handles) OVERRIDE { | 346 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { |
347 EXPECT_FALSE(platform_handles); | 347 EXPECT_FALSE(platform_handles); |
348 | 348 |
349 EXPECT_LT(count_, expected_count_); | 349 EXPECT_LT(count_, expected_count_); |
350 count_++; | 350 count_++; |
351 | 351 |
352 EXPECT_TRUE( | 352 EXPECT_TRUE( |
353 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 353 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
354 | 354 |
355 if (count_ >= expected_count_) | 355 if (count_ >= expected_count_) |
356 done_event_.Signal(); | 356 done_event_.Signal(); |
357 } | 357 } |
358 virtual void OnError(Error error) OVERRIDE { | 358 virtual void OnError(Error error) override { |
359 // We'll get a read (shutdown) error when the connection is closed. | 359 // We'll get a read (shutdown) error when the connection is closed. |
360 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 360 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
361 } | 361 } |
362 | 362 |
363 // Waits for all the messages to have been seen. | 363 // Waits for all the messages to have been seen. |
364 void Wait() { done_event_.Wait(); } | 364 void Wait() { done_event_.Wait(); } |
365 | 365 |
366 private: | 366 private: |
367 base::WaitableEvent done_event_; | 367 base::WaitableEvent done_event_; |
368 size_t expected_count_; | 368 size_t expected_count_; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 bool expect_read_error, | 425 bool expect_read_error, |
426 bool expect_write_error) | 426 bool expect_write_error) |
427 : ReadCountdownRawChannelDelegate(expected_read_count), | 427 : ReadCountdownRawChannelDelegate(expected_read_count), |
428 got_read_error_event_(false, false), | 428 got_read_error_event_(false, false), |
429 got_write_error_event_(false, false), | 429 got_write_error_event_(false, false), |
430 expecting_read_error_(expect_read_error), | 430 expecting_read_error_(expect_read_error), |
431 expecting_write_error_(expect_write_error) {} | 431 expecting_write_error_(expect_write_error) {} |
432 | 432 |
433 virtual ~ErrorRecordingRawChannelDelegate() {} | 433 virtual ~ErrorRecordingRawChannelDelegate() {} |
434 | 434 |
435 virtual void OnError(Error error) OVERRIDE { | 435 virtual void OnError(Error error) override { |
436 switch (error) { | 436 switch (error) { |
437 case ERROR_READ_SHUTDOWN: | 437 case ERROR_READ_SHUTDOWN: |
438 ASSERT_TRUE(expecting_read_error_); | 438 ASSERT_TRUE(expecting_read_error_); |
439 expecting_read_error_ = false; | 439 expecting_read_error_ = false; |
440 got_read_error_event_.Signal(); | 440 got_read_error_event_.Signal(); |
441 break; | 441 break; |
442 case ERROR_READ_BROKEN: | 442 case ERROR_READ_BROKEN: |
443 // TODO(vtl): Test broken connections. | 443 // TODO(vtl): Test broken connections. |
444 CHECK(false); | 444 CHECK(false); |
445 break; | 445 break; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 public: | 560 public: |
561 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel) | 561 explicit ShutdownOnReadMessageRawChannelDelegate(RawChannel* raw_channel) |
562 : raw_channel_(raw_channel), | 562 : raw_channel_(raw_channel), |
563 done_event_(false, false), | 563 done_event_(false, false), |
564 did_shutdown_(false) {} | 564 did_shutdown_(false) {} |
565 virtual ~ShutdownOnReadMessageRawChannelDelegate() {} | 565 virtual ~ShutdownOnReadMessageRawChannelDelegate() {} |
566 | 566 |
567 // |RawChannel::Delegate| implementation (called on the I/O thread): | 567 // |RawChannel::Delegate| implementation (called on the I/O thread): |
568 virtual void OnReadMessage( | 568 virtual void OnReadMessage( |
569 const MessageInTransit::View& message_view, | 569 const MessageInTransit::View& message_view, |
570 embedder::ScopedPlatformHandleVectorPtr platform_handles) OVERRIDE { | 570 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { |
571 EXPECT_FALSE(platform_handles); | 571 EXPECT_FALSE(platform_handles); |
572 EXPECT_FALSE(did_shutdown_); | 572 EXPECT_FALSE(did_shutdown_); |
573 EXPECT_TRUE( | 573 EXPECT_TRUE( |
574 CheckMessageData(message_view.bytes(), message_view.num_bytes())); | 574 CheckMessageData(message_view.bytes(), message_view.num_bytes())); |
575 raw_channel_->Shutdown(); | 575 raw_channel_->Shutdown(); |
576 did_shutdown_ = true; | 576 did_shutdown_ = true; |
577 done_event_.Signal(); | 577 done_event_.Signal(); |
578 } | 578 } |
579 virtual void OnError(Error /*error*/) OVERRIDE { | 579 virtual void OnError(Error /*error*/) override { |
580 CHECK(false); // Should not get called. | 580 CHECK(false); // Should not get called. |
581 } | 581 } |
582 | 582 |
583 // Waits for shutdown. | 583 // Waits for shutdown. |
584 void Wait() { | 584 void Wait() { |
585 done_event_.Wait(); | 585 done_event_.Wait(); |
586 EXPECT_TRUE(did_shutdown_); | 586 EXPECT_TRUE(did_shutdown_); |
587 } | 587 } |
588 | 588 |
589 private: | 589 private: |
(...skipping 27 matching lines...) Expand all Loading... |
617 Error shutdown_on_error_type) | 617 Error shutdown_on_error_type) |
618 : raw_channel_(raw_channel), | 618 : raw_channel_(raw_channel), |
619 shutdown_on_error_type_(shutdown_on_error_type), | 619 shutdown_on_error_type_(shutdown_on_error_type), |
620 done_event_(false, false), | 620 done_event_(false, false), |
621 did_shutdown_(false) {} | 621 did_shutdown_(false) {} |
622 virtual ~ShutdownOnErrorRawChannelDelegate() {} | 622 virtual ~ShutdownOnErrorRawChannelDelegate() {} |
623 | 623 |
624 // |RawChannel::Delegate| implementation (called on the I/O thread): | 624 // |RawChannel::Delegate| implementation (called on the I/O thread): |
625 virtual void OnReadMessage( | 625 virtual void OnReadMessage( |
626 const MessageInTransit::View& /*message_view*/, | 626 const MessageInTransit::View& /*message_view*/, |
627 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) OVERRIDE { | 627 embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override { |
628 CHECK(false); // Should not get called. | 628 CHECK(false); // Should not get called. |
629 } | 629 } |
630 virtual void OnError(Error error) OVERRIDE { | 630 virtual void OnError(Error error) override { |
631 EXPECT_FALSE(did_shutdown_); | 631 EXPECT_FALSE(did_shutdown_); |
632 if (error != shutdown_on_error_type_) | 632 if (error != shutdown_on_error_type_) |
633 return; | 633 return; |
634 raw_channel_->Shutdown(); | 634 raw_channel_->Shutdown(); |
635 did_shutdown_ = true; | 635 did_shutdown_ = true; |
636 done_event_.Signal(); | 636 done_event_.Signal(); |
637 } | 637 } |
638 | 638 |
639 // Waits for shutdown. | 639 // Waits for shutdown. |
640 void Wait() { | 640 void Wait() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 679 |
680 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 680 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
681 | 681 |
682 // Wait for the delegate, which will shut the |RawChannel| down. | 682 // Wait for the delegate, which will shut the |RawChannel| down. |
683 delegate.Wait(); | 683 delegate.Wait(); |
684 } | 684 } |
685 | 685 |
686 } // namespace | 686 } // namespace |
687 } // namespace system | 687 } // namespace system |
688 } // namespace mojo | 688 } // namespace mojo |
OLD | NEW |