OLD | NEW |
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 POSIX-specific bits of this test (once we have a | 5 // TODO(vtl): Factor out the POSIX-specific bits of this test (once we have a |
6 // non-POSIX implementation). | 6 // 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> | 10 #include <fcntl.h> |
(...skipping 27 matching lines...) Expand all Loading... |
38 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
39 | 39 |
40 namespace mojo { | 40 namespace mojo { |
41 namespace system { | 41 namespace system { |
42 namespace { | 42 namespace { |
43 | 43 |
44 MessageInTransit* MakeTestMessage(uint32_t num_bytes) { | 44 MessageInTransit* MakeTestMessage(uint32_t num_bytes) { |
45 std::vector<unsigned char> bytes(num_bytes, 0); | 45 std::vector<unsigned char> bytes(num_bytes, 0); |
46 for (size_t i = 0; i < num_bytes; i++) | 46 for (size_t i = 0; i < num_bytes; i++) |
47 bytes[i] = static_cast<unsigned char>(i + num_bytes); | 47 bytes[i] = static_cast<unsigned char>(i + num_bytes); |
48 return MessageInTransit::Create(bytes.data(), num_bytes); | 48 return MessageInTransit::Create( |
| 49 MessageInTransit::kTypeMessagePipeEndpoint, |
| 50 MessageInTransit::kSubtypeMessagePipeEndpointData, |
| 51 bytes.data(), num_bytes); |
49 } | 52 } |
50 | 53 |
51 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { | 54 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { |
52 const unsigned char* b = static_cast<const unsigned char*>(bytes); | 55 const unsigned char* b = static_cast<const unsigned char*>(bytes); |
53 for (uint32_t i = 0; i < num_bytes; i++) { | 56 for (uint32_t i = 0; i < num_bytes; i++) { |
54 if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 57 if (b[i] != static_cast<unsigned char>(i + num_bytes)) |
55 return false; | 58 return false; |
56 } | 59 } |
57 return true; | 60 return true; |
58 } | 61 } |
59 | 62 |
| 63 void InitOnIOThread(RawChannel* raw_channel) { |
| 64 CHECK(raw_channel->Init()); |
| 65 } |
| 66 |
60 // ----------------------------------------------------------------------------- | 67 // ----------------------------------------------------------------------------- |
61 | 68 |
62 class RawChannelPosixTest : public testing::Test { | 69 class RawChannelPosixTest : public testing::Test { |
63 public: | 70 public: |
64 RawChannelPosixTest() : io_thread_("io_thread") { | 71 RawChannelPosixTest() : io_thread_("io_thread") { |
65 fds_[0] = -1; | 72 fds_[0] = -1; |
66 fds_[1] = -1; | 73 fds_[1] = -1; |
67 } | 74 } |
68 | 75 |
69 virtual ~RawChannelPosixTest() { | 76 virtual ~RawChannelPosixTest() { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), | 210 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), |
204 &delegate, | 211 &delegate, |
205 io_thread_message_loop())); | 212 io_thread_message_loop())); |
206 // |RawChannel::Create()| takes ownership of the FD. | 213 // |RawChannel::Create()| takes ownership of the FD. |
207 clear_fd(0); | 214 clear_fd(0); |
208 | 215 |
209 TestMessageReaderAndChecker checker(fd(1)); | 216 TestMessageReaderAndChecker checker(fd(1)); |
210 | 217 |
211 test::PostTaskAndWait(io_thread_task_runner(), | 218 test::PostTaskAndWait(io_thread_task_runner(), |
212 FROM_HERE, | 219 FROM_HERE, |
213 base::Bind(&RawChannel::Init, | 220 base::Bind(&InitOnIOThread, rc.get())); |
214 base::Unretained(rc.get()))); | |
215 | 221 |
216 // Write and read, for a variety of sizes. | 222 // Write and read, for a variety of sizes. |
217 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 223 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
218 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); | 224 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); |
219 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; | 225 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; |
220 } | 226 } |
221 | 227 |
222 // Write/queue and read afterwards, for a variety of sizes. | 228 // Write/queue and read afterwards, for a variety of sizes. |
223 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) | 229 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) |
224 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); | 230 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 303 |
298 ReadCheckerRawChannelDelegate delegate; | 304 ReadCheckerRawChannelDelegate delegate; |
299 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), | 305 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), |
300 &delegate, | 306 &delegate, |
301 io_thread_message_loop())); | 307 io_thread_message_loop())); |
302 // |RawChannel::Create()| takes ownership of the FD. | 308 // |RawChannel::Create()| takes ownership of the FD. |
303 clear_fd(0); | 309 clear_fd(0); |
304 | 310 |
305 test::PostTaskAndWait(io_thread_task_runner(), | 311 test::PostTaskAndWait(io_thread_task_runner(), |
306 FROM_HERE, | 312 FROM_HERE, |
307 base::Bind(&RawChannel::Init, | 313 base::Bind(&InitOnIOThread, rc.get())); |
308 base::Unretained(rc.get()))); | |
309 | 314 |
310 // Write and read, for a variety of sizes. | 315 // Write and read, for a variety of sizes. |
311 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 316 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
312 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); | 317 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); |
313 MessageInTransit* message = MakeTestMessage(size); | 318 MessageInTransit* message = MakeTestMessage(size); |
314 EXPECT_EQ(static_cast<ssize_t>(message->size_with_header_and_padding()), | 319 EXPECT_EQ(static_cast<ssize_t>(message->size_with_header_and_padding()), |
315 write(fd(1), message, message->size_with_header_and_padding())); | 320 write(fd(1), message, message->size_with_header_and_padding())); |
316 message->Destroy(); | 321 message->Destroy(); |
317 delegate.Wait(); | 322 delegate.Wait(); |
318 } | 323 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 WriteOnlyRawChannelDelegate writer_delegate; | 414 WriteOnlyRawChannelDelegate writer_delegate; |
410 scoped_ptr<RawChannel> writer_rc( | 415 scoped_ptr<RawChannel> writer_rc( |
411 RawChannel::Create(PlatformChannelHandle(fd(0)), | 416 RawChannel::Create(PlatformChannelHandle(fd(0)), |
412 &writer_delegate, | 417 &writer_delegate, |
413 io_thread_message_loop())); | 418 io_thread_message_loop())); |
414 // |RawChannel::Create()| takes ownership of the FD. | 419 // |RawChannel::Create()| takes ownership of the FD. |
415 clear_fd(0); | 420 clear_fd(0); |
416 | 421 |
417 test::PostTaskAndWait(io_thread_task_runner(), | 422 test::PostTaskAndWait(io_thread_task_runner(), |
418 FROM_HERE, | 423 FROM_HERE, |
419 base::Bind(&RawChannel::Init, | 424 base::Bind(&InitOnIOThread, writer_rc.get())); |
420 base::Unretained(writer_rc.get()))); | |
421 | 425 |
422 ReadCountdownRawChannelDelegate reader_delegate( | 426 ReadCountdownRawChannelDelegate reader_delegate( |
423 kNumWriterThreads * kNumWriteMessagesPerThread); | 427 kNumWriterThreads * kNumWriteMessagesPerThread); |
424 scoped_ptr<RawChannel> reader_rc( | 428 scoped_ptr<RawChannel> reader_rc( |
425 RawChannel::Create(PlatformChannelHandle(fd(1)), | 429 RawChannel::Create(PlatformChannelHandle(fd(1)), |
426 &reader_delegate, | 430 &reader_delegate, |
427 io_thread_message_loop())); | 431 io_thread_message_loop())); |
428 // |RawChannel::Create()| takes ownership of the FD. | 432 // |RawChannel::Create()| takes ownership of the FD. |
429 clear_fd(1); | 433 clear_fd(1); |
430 | 434 |
431 test::PostTaskAndWait(io_thread_task_runner(), | 435 test::PostTaskAndWait(io_thread_task_runner(), |
432 FROM_HERE, | 436 FROM_HERE, |
433 base::Bind(&RawChannel::Init, | 437 base::Bind(&InitOnIOThread, reader_rc.get())); |
434 base::Unretained(reader_rc.get()))); | |
435 | 438 |
436 { | 439 { |
437 ScopedVector<RawChannelWriterThread> writer_threads; | 440 ScopedVector<RawChannelWriterThread> writer_threads; |
438 for (size_t i = 0; i < kNumWriterThreads; i++) { | 441 for (size_t i = 0; i < kNumWriterThreads; i++) { |
439 writer_threads.push_back(new RawChannelWriterThread( | 442 writer_threads.push_back(new RawChannelWriterThread( |
440 writer_rc.get(), kNumWriteMessagesPerThread)); | 443 writer_rc.get(), kNumWriteMessagesPerThread)); |
441 } | 444 } |
442 for (size_t i = 0; i < writer_threads.size(); i++) | 445 for (size_t i = 0; i < writer_threads.size(); i++) |
443 writer_threads[i]->Start(); | 446 writer_threads[i]->Start(); |
444 } // Joins all the writer threads. | 447 } // Joins all the writer threads. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 TEST_F(RawChannelPosixTest, OnFatalError) { | 506 TEST_F(RawChannelPosixTest, OnFatalError) { |
504 FatalErrorRecordingRawChannelDelegate delegate; | 507 FatalErrorRecordingRawChannelDelegate delegate; |
505 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), | 508 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), |
506 &delegate, | 509 &delegate, |
507 io_thread_message_loop())); | 510 io_thread_message_loop())); |
508 // |RawChannel::Create()| takes ownership of the FD. | 511 // |RawChannel::Create()| takes ownership of the FD. |
509 clear_fd(0); | 512 clear_fd(0); |
510 | 513 |
511 test::PostTaskAndWait(io_thread_task_runner(), | 514 test::PostTaskAndWait(io_thread_task_runner(), |
512 FROM_HERE, | 515 FROM_HERE, |
513 base::Bind(&RawChannel::Init, | 516 base::Bind(&InitOnIOThread, rc.get())); |
514 base::Unretained(rc.get()))); | |
515 | 517 |
516 // Close the other end, which should make writing fail. | 518 // Close the other end, which should make writing fail. |
517 CHECK_EQ(close(fd(1)), 0); | 519 CHECK_EQ(close(fd(1)), 0); |
518 clear_fd(1); | 520 clear_fd(1); |
519 | 521 |
520 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 522 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
521 | 523 |
522 // TODO(vtl): In theory, it's conceivable that closing the other end might | 524 // TODO(vtl): In theory, it's conceivable that closing the other end might |
523 // lead to read failing. In practice, it doesn't seem to. | 525 // lead to read failing. In practice, it doesn't seem to. |
524 EXPECT_EQ(RawChannel::Delegate::FATAL_ERROR_FAILED_WRITE, | 526 EXPECT_EQ(RawChannel::Delegate::FATAL_ERROR_FAILED_WRITE, |
(...skipping 13 matching lines...) Expand all Loading... |
538 TEST_F(RawChannelPosixTest, WriteMessageAfterShutdown) { | 540 TEST_F(RawChannelPosixTest, WriteMessageAfterShutdown) { |
539 WriteOnlyRawChannelDelegate delegate; | 541 WriteOnlyRawChannelDelegate delegate; |
540 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), | 542 scoped_ptr<RawChannel> rc(RawChannel::Create(PlatformChannelHandle(fd(0)), |
541 &delegate, | 543 &delegate, |
542 io_thread_message_loop())); | 544 io_thread_message_loop())); |
543 // |RawChannel::Create()| takes ownership of the FD. | 545 // |RawChannel::Create()| takes ownership of the FD. |
544 clear_fd(0); | 546 clear_fd(0); |
545 | 547 |
546 test::PostTaskAndWait(io_thread_task_runner(), | 548 test::PostTaskAndWait(io_thread_task_runner(), |
547 FROM_HERE, | 549 FROM_HERE, |
548 base::Bind(&RawChannel::Init, | 550 base::Bind(&InitOnIOThread, rc.get())); |
549 base::Unretained(rc.get()))); | |
550 test::PostTaskAndWait(io_thread_task_runner(), | 551 test::PostTaskAndWait(io_thread_task_runner(), |
551 FROM_HERE, | 552 FROM_HERE, |
552 base::Bind(&RawChannel::Shutdown, | 553 base::Bind(&RawChannel::Shutdown, |
553 base::Unretained(rc.get()))); | 554 base::Unretained(rc.get()))); |
554 | 555 |
555 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 556 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
556 } | 557 } |
557 | 558 |
558 } // namespace | 559 } // namespace |
559 } // namespace system | 560 } // namespace system |
560 } // namespace mojo | 561 } // namespace mojo |
OLD | NEW |