Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1115)

Side by Side Diff: mojo/system/raw_channel_posix_unittest.cc

Issue 176063002: Reland: Add some handle read/write helpers to mojo/common/test/test_utils.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/mojo.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 // TODO(vtl): Factor out the remaining POSIX-specific bits of this test (once we 5 // TODO(vtl): Factor out the remaining POSIX-specific bits of this test (once we
6 // have a non-POSIX implementation). 6 // have a non-POSIX implementation).
7 7
8 #include "mojo/system/raw_channel.h" 8 #include "mojo/system/raw_channel.h"
9 9
10 #include <fcntl.h>
11 #include <stdint.h>
12 #include <sys/socket.h> 10 #include <sys/socket.h>
13 #include <unistd.h>
14 11
15 #include <vector> 12 #include <vector>
16 13
17 #include "base/basictypes.h" 14 #include "base/basictypes.h"
18 #include "base/bind.h" 15 #include "base/bind.h"
19 #include "base/compiler_specific.h" 16 #include "base/compiler_specific.h"
20 #include "base/location.h" 17 #include "base/location.h"
21 #include "base/logging.h" 18 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/scoped_vector.h" 20 #include "base/memory/scoped_vector.h"
24 #include "base/message_loop/message_loop.h" 21 #include "base/message_loop/message_loop.h"
25 #include "base/posix/eintr_wrapper.h"
26 #include "base/rand_util.h" 22 #include "base/rand_util.h"
27 #include "base/synchronization/lock.h" 23 #include "base/synchronization/lock.h"
28 #include "base/synchronization/waitable_event.h" 24 #include "base/synchronization/waitable_event.h"
29 #include "base/threading/platform_thread.h" // For |Sleep()|. 25 #include "base/threading/platform_thread.h" // For |Sleep()|.
30 #include "base/threading/simple_thread.h" 26 #include "base/threading/simple_thread.h"
31 #include "base/time/time.h" 27 #include "base/time/time.h"
28 #include "mojo/common/test/test_utils.h"
32 #include "mojo/system/embedder/platform_channel_pair.h" 29 #include "mojo/system/embedder/platform_channel_pair.h"
33 #include "mojo/system/embedder/platform_handle.h" 30 #include "mojo/system/embedder/platform_handle.h"
34 #include "mojo/system/embedder/scoped_platform_handle.h" 31 #include "mojo/system/embedder/scoped_platform_handle.h"
35 #include "mojo/system/message_in_transit.h" 32 #include "mojo/system/message_in_transit.h"
36 #include "mojo/system/test_utils.h" 33 #include "mojo/system/test_utils.h"
37 34
38 namespace mojo { 35 namespace mojo {
39 namespace system { 36 namespace system {
40 namespace { 37 namespace {
41 38
(...skipping 18 matching lines...) Expand all
60 } 57 }
61 58
62 void InitOnIOThread(RawChannel* raw_channel) { 59 void InitOnIOThread(RawChannel* raw_channel) {
63 CHECK(raw_channel->Init()); 60 CHECK(raw_channel->Init());
64 } 61 }
65 62
66 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, 63 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle,
67 uint32_t num_bytes) { 64 uint32_t num_bytes) {
68 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); 65 scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes));
69 66
70 ssize_t write_size = HANDLE_EINTR( 67 size_t write_size = 0;
71 write(handle.fd, message->main_buffer(), message->main_buffer_size())); 68 mojo::test::BlockingWrite(
72 bool result = write_size == static_cast<ssize_t>(message->main_buffer_size()); 69 handle, message->main_buffer(), message->main_buffer_size(), &write_size);
73 return result; 70 return write_size == message->main_buffer_size();
74 } 71 }
75 72
76 // ----------------------------------------------------------------------------- 73 // -----------------------------------------------------------------------------
77 74
78 class RawChannelPosixTest : public test::TestWithIOThreadBase { 75 class RawChannelPosixTest : public test::TestWithIOThreadBase {
79 public: 76 public:
80 RawChannelPosixTest() {} 77 RawChannelPosixTest() {}
81 virtual ~RawChannelPosixTest() {} 78 virtual ~RawChannelPosixTest() {}
82 79
83 virtual void SetUp() OVERRIDE { 80 virtual void SetUp() OVERRIDE {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 class TestMessageReaderAndChecker { 124 class TestMessageReaderAndChecker {
128 public: 125 public:
129 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) 126 explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle)
130 : handle_(handle) {} 127 : handle_(handle) {}
131 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } 128 ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); }
132 129
133 bool ReadAndCheckNextMessage(uint32_t expected_size) { 130 bool ReadAndCheckNextMessage(uint32_t expected_size) {
134 unsigned char buffer[4096]; 131 unsigned char buffer[4096];
135 132
136 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { 133 for (size_t i = 0; i < kMessageReaderMaxPollIterations;) {
137 ssize_t read_size = HANDLE_EINTR( 134 size_t read_size = 0;
138 read(handle_.fd, buffer, sizeof(buffer))); 135 CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer),
139 if (read_size < 0) { 136 &read_size));
140 PCHECK(errno == EAGAIN || errno == EWOULDBLOCK);
141 read_size = 0;
142 }
143 137
144 // Append newly-read data to |bytes_|. 138 // Append newly-read data to |bytes_|.
145 bytes_.insert(bytes_.end(), buffer, buffer + read_size); 139 bytes_.insert(bytes_.end(), buffer, buffer + read_size);
146 140
147 // If we have the header.... 141 // If we have the header....
148 size_t message_size; 142 size_t message_size;
149 if (MessageInTransit::GetNextMessageSize(bytes_.data(), bytes_.size(), 143 if (MessageInTransit::GetNextMessageSize(bytes_.data(), bytes_.size(),
150 &message_size)) { 144 &message_size)) {
151 // If we've read the whole message.... 145 // If we've read the whole message....
152 if (bytes_.size() >= message_size) { 146 if (bytes_.size() >= message_size) {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 272
279 base::Lock lock_; // Protects the following members. 273 base::Lock lock_; // Protects the following members.
280 std::vector<uint32_t> expected_sizes_; 274 std::vector<uint32_t> expected_sizes_;
281 size_t position_; 275 size_t position_;
282 276
283 DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); 277 DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate);
284 }; 278 };
285 279
286 // Tests reading (writing using our own custom writer). 280 // Tests reading (writing using our own custom writer).
287 TEST_F(RawChannelPosixTest, OnReadMessage) { 281 TEST_F(RawChannelPosixTest, OnReadMessage) {
288 // We're going to write to |fd(1)|. We'll do so in a blocking manner.
289 PCHECK(fcntl(handles[1].get().fd, F_SETFL, 0) == 0);
290
291 ReadCheckerRawChannelDelegate delegate; 282 ReadCheckerRawChannelDelegate delegate;
292 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(), 283 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
293 &delegate, 284 &delegate,
294 io_thread_message_loop())); 285 io_thread_message_loop()));
295 286
296 test::PostTaskAndWait(io_thread_task_runner(), 287 test::PostTaskAndWait(io_thread_task_runner(),
297 FROM_HERE, 288 FROM_HERE,
298 base::Bind(&InitOnIOThread, rc.get())); 289 base::Bind(&InitOnIOThread, rc.get()));
299 290
300 // Write and read, for a variety of sizes. 291 // Write and read, for a variety of sizes.
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 465
475 DISALLOW_COPY_AND_ASSIGN(FatalErrorRecordingRawChannelDelegate); 466 DISALLOW_COPY_AND_ASSIGN(FatalErrorRecordingRawChannelDelegate);
476 }; 467 };
477 468
478 // Tests fatal errors. 469 // Tests fatal errors.
479 // TODO(vtl): Figure out how to make reading fail reliably. (I'm not convinced 470 // TODO(vtl): Figure out how to make reading fail reliably. (I'm not convinced
480 // that it does.) 471 // that it does.)
481 TEST_F(RawChannelPosixTest, OnFatalError) { 472 TEST_F(RawChannelPosixTest, OnFatalError) {
482 const size_t kMessageCount = 5; 473 const size_t kMessageCount = 5;
483 474
484 // We're going to write to |fd(1)|. We'll do so in a blocking manner.
485 PCHECK(fcntl(handles[1].get().fd, F_SETFL, 0) == 0);
486
487 FatalErrorRecordingRawChannelDelegate delegate(2 * kMessageCount); 475 FatalErrorRecordingRawChannelDelegate delegate(2 * kMessageCount);
488 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(), 476 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass(),
489 &delegate, 477 &delegate,
490 io_thread_message_loop())); 478 io_thread_message_loop()));
491 479
492 test::PostTaskAndWait(io_thread_task_runner(), 480 test::PostTaskAndWait(io_thread_task_runner(),
493 FROM_HERE, 481 FROM_HERE,
494 base::Bind(&InitOnIOThread, rc.get())); 482 base::Bind(&InitOnIOThread, rc.get()));
495 483
496 // Write into the other end a few messages. 484 // Write into the other end a few messages.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 FROM_HERE, 535 FROM_HERE,
548 base::Bind(&RawChannel::Shutdown, 536 base::Bind(&RawChannel::Shutdown,
549 base::Unretained(rc.get()))); 537 base::Unretained(rc.get())));
550 538
551 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); 539 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1)));
552 } 540 }
553 541
554 } // namespace 542 } // namespace
555 } // namespace system 543 } // namespace system
556 } // namespace mojo 544 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/mojo.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698