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 #include <utility> |
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" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 190 |
191 // The start of the received data should always be on a message boundary. | 191 // The start of the received data should always be on a message boundary. |
192 std::vector<unsigned char> bytes_; | 192 std::vector<unsigned char> bytes_; |
193 | 193 |
194 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); | 194 MOJO_DISALLOW_COPY_AND_ASSIGN(TestMessageReaderAndChecker); |
195 }; | 195 }; |
196 | 196 |
197 // Tests writing (and verifies reading using our own custom reader). | 197 // Tests writing (and verifies reading using our own custom reader). |
198 TEST_F(RawChannelTest, WriteMessage) { | 198 TEST_F(RawChannelTest, WriteMessage) { |
199 WriteOnlyRawChannelDelegate delegate; | 199 WriteOnlyRawChannelDelegate delegate; |
200 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 200 RawChannel* rc = RawChannel::Create(std::move(handles[0])); |
201 TestMessageReaderAndChecker checker(handles[1].get()); | 201 TestMessageReaderAndChecker checker(handles[1].get()); |
202 internal::g_io_thread_task_runner->PostTask( | 202 internal::g_io_thread_task_runner->PostTask( |
203 FROM_HERE, | 203 FROM_HERE, |
204 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 204 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
205 | 205 |
206 // Write and read, for a variety of sizes. | 206 // Write and read, for a variety of sizes. |
207 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 207 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
208 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); | 208 EXPECT_TRUE(rc->WriteMessage(MakeTestMessage(size))); |
209 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; | 209 EXPECT_TRUE(checker.ReadAndCheckNextMessage(size)) << size; |
210 } | 210 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 base::Lock lock_; // Protects the following members. | 276 base::Lock lock_; // Protects the following members. |
277 std::vector<uint32_t> expected_sizes_; | 277 std::vector<uint32_t> expected_sizes_; |
278 size_t position_; | 278 size_t position_; |
279 | 279 |
280 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); | 280 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); |
281 }; | 281 }; |
282 | 282 |
283 // Tests reading (writing using our own custom writer). | 283 // Tests reading (writing using our own custom writer). |
284 TEST_F(RawChannelTest, OnReadMessage) { | 284 TEST_F(RawChannelTest, OnReadMessage) { |
285 ReadCheckerRawChannelDelegate delegate; | 285 ReadCheckerRawChannelDelegate delegate; |
286 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 286 RawChannel* rc = RawChannel::Create(std::move(handles[0])); |
287 internal::g_io_thread_task_runner->PostTask( | 287 internal::g_io_thread_task_runner->PostTask( |
288 FROM_HERE, | 288 FROM_HERE, |
289 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 289 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
290 | 290 |
291 // Write and read, for a variety of sizes. | 291 // Write and read, for a variety of sizes. |
292 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { | 292 for (uint32_t size = 1; size < 5 * 1000 * 1000; size += size / 2 + 1) { |
293 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); | 293 delegate.SetExpectedSizes(std::vector<uint32_t>(1, size)); |
294 | 294 |
295 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); | 295 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), size)); |
296 | 296 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 size_t count_; | 373 size_t count_; |
374 | 374 |
375 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); | 375 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); |
376 }; | 376 }; |
377 | 377 |
378 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { | 378 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { |
379 static const size_t kNumWriterThreads = 10; | 379 static const size_t kNumWriterThreads = 10; |
380 static const size_t kNumWriteMessagesPerThread = 400; | 380 static const size_t kNumWriteMessagesPerThread = 400; |
381 | 381 |
382 WriteOnlyRawChannelDelegate writer_delegate; | 382 WriteOnlyRawChannelDelegate writer_delegate; |
383 RawChannel* writer_rc = RawChannel::Create(handles[0].Pass()); | 383 RawChannel* writer_rc = RawChannel::Create(std::move(handles[0])); |
384 internal::g_io_thread_task_runner->PostTask( | 384 internal::g_io_thread_task_runner->PostTask( |
385 FROM_HERE, | 385 FROM_HERE, |
386 base::Bind(&InitOnIOThread, writer_rc, | 386 base::Bind(&InitOnIOThread, writer_rc, |
387 base::Unretained(&writer_delegate))); | 387 base::Unretained(&writer_delegate))); |
388 | 388 |
389 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * | 389 ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * |
390 kNumWriteMessagesPerThread); | 390 kNumWriteMessagesPerThread); |
391 RawChannel* reader_rc = RawChannel::Create(handles[1].Pass()); | 391 RawChannel* reader_rc = RawChannel::Create(std::move(handles[1])); |
392 internal::g_io_thread_task_runner->PostTask( | 392 internal::g_io_thread_task_runner->PostTask( |
393 FROM_HERE, | 393 FROM_HERE, |
394 base::Bind(&InitOnIOThread, reader_rc, | 394 base::Bind(&InitOnIOThread, reader_rc, |
395 base::Unretained(&reader_delegate))); | 395 base::Unretained(&reader_delegate))); |
396 | 396 |
397 { | 397 { |
398 ScopedVector<RawChannelWriterThread> writer_threads; | 398 ScopedVector<RawChannelWriterThread> writer_threads; |
399 for (size_t i = 0; i < kNumWriterThreads; i++) { | 399 for (size_t i = 0; i < kNumWriterThreads; i++) { |
400 writer_threads.push_back(new RawChannelWriterThread( | 400 writer_threads.push_back(new RawChannelWriterThread( |
401 writer_rc, kNumWriteMessagesPerThread)); | 401 writer_rc, kNumWriteMessagesPerThread)); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 | 472 |
473 bool expecting_read_error_; | 473 bool expecting_read_error_; |
474 bool expecting_write_error_; | 474 bool expecting_write_error_; |
475 | 475 |
476 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrorRecordingRawChannelDelegate); | 476 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrorRecordingRawChannelDelegate); |
477 }; | 477 }; |
478 | 478 |
479 // Tests (fatal) errors. | 479 // Tests (fatal) errors. |
480 TEST_F(RawChannelTest, OnError) { | 480 TEST_F(RawChannelTest, OnError) { |
481 ErrorRecordingRawChannelDelegate delegate(0, true, true); | 481 ErrorRecordingRawChannelDelegate delegate(0, true, true); |
482 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 482 RawChannel* rc = RawChannel::Create(std::move(handles[0])); |
483 internal::g_io_thread_task_runner->PostTask( | 483 internal::g_io_thread_task_runner->PostTask( |
484 FROM_HERE, | 484 FROM_HERE, |
485 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 485 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
486 FlushIOThread(); | 486 FlushIOThread(); |
487 | 487 |
488 // Close the handle of the other end, which should make writing fail. | 488 // Close the handle of the other end, which should make writing fail. |
489 handles[1].reset(); | 489 handles[1].reset(); |
490 | 490 |
491 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 491 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
492 | 492 |
(...skipping 23 matching lines...) Expand all Loading... |
516 for (size_t i = 0; i < kMessageCount; | 516 for (size_t i = 0; i < kMessageCount; |
517 i++, message_size += message_size / 2 + 1) | 517 i++, message_size += message_size / 2 + 1) |
518 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); | 518 EXPECT_TRUE(WriteTestMessageToHandle(handles[1].get(), message_size)); |
519 | 519 |
520 // Close the other end, which should make writing fail. | 520 // Close the other end, which should make writing fail. |
521 handles[1].reset(); | 521 handles[1].reset(); |
522 | 522 |
523 // Only start up reading here. The system buffer should still contain the | 523 // Only start up reading here. The system buffer should still contain the |
524 // messages that were written. | 524 // messages that were written. |
525 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); | 525 ErrorRecordingRawChannelDelegate delegate(kMessageCount, true, true); |
526 RawChannel* rc = RawChannel::Create(handles[0].Pass()); | 526 RawChannel* rc = RawChannel::Create(std::move(handles[0])); |
527 internal::g_io_thread_task_runner->PostTask( | 527 internal::g_io_thread_task_runner->PostTask( |
528 FROM_HERE, | 528 FROM_HERE, |
529 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); | 529 base::Bind(&InitOnIOThread, rc, base::Unretained(&delegate))); |
530 FlushIOThread(); | 530 FlushIOThread(); |
531 | 531 |
532 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 532 EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); |
533 | 533 |
534 // We should definitely get a write error. | 534 // We should definitely get a write error. |
535 delegate.WaitForWriteError(); | 535 delegate.WaitForWriteError(); |
536 | 536 |
(...skipping 28 matching lines...) Expand all Loading... |
565 ASSERT_EQ(2u, platform_handles->size()); | 565 ASSERT_EQ(2u, platform_handles->size()); |
566 ScopedPlatformHandle h1(platform_handles->at(0)); | 566 ScopedPlatformHandle h1(platform_handles->at(0)); |
567 EXPECT_TRUE(h1.is_valid()); | 567 EXPECT_TRUE(h1.is_valid()); |
568 ScopedPlatformHandle h2(platform_handles->at(1)); | 568 ScopedPlatformHandle h2(platform_handles->at(1)); |
569 EXPECT_TRUE(h2.is_valid()); | 569 EXPECT_TRUE(h2.is_valid()); |
570 platform_handles->clear(); | 570 platform_handles->clear(); |
571 | 571 |
572 { | 572 { |
573 char buffer[100] = {}; | 573 char buffer[100] = {}; |
574 | 574 |
575 base::ScopedFILE fp(test::FILEFromPlatformHandle(h1.Pass(), "rb")); | 575 base::ScopedFILE fp(test::FILEFromPlatformHandle(std::move(h1), "rb")); |
576 EXPECT_TRUE(fp); | 576 EXPECT_TRUE(fp); |
577 rewind(fp.get()); | 577 rewind(fp.get()); |
578 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); | 578 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
579 EXPECT_EQ('1', buffer[0]); | 579 EXPECT_EQ('1', buffer[0]); |
580 } | 580 } |
581 | 581 |
582 { | 582 { |
583 char buffer[100] = {}; | 583 char buffer[100] = {}; |
584 base::ScopedFILE fp(test::FILEFromPlatformHandle(h2.Pass(), "rb")); | 584 base::ScopedFILE fp(test::FILEFromPlatformHandle(std::move(h2), "rb")); |
585 EXPECT_TRUE(fp); | 585 EXPECT_TRUE(fp); |
586 rewind(fp.get()); | 586 rewind(fp.get()); |
587 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); | 587 EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); |
588 EXPECT_EQ('2', buffer[0]); | 588 EXPECT_EQ('2', buffer[0]); |
589 } | 589 } |
590 | 590 |
591 done_event_.Signal(); | 591 done_event_.Signal(); |
592 } | 592 } |
593 void OnError(Error error) override { | 593 void OnError(Error error) override { |
594 // We'll get a read (shutdown) error when the connection is closed. | 594 // We'll get a read (shutdown) error when the connection is closed. |
595 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 595 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
596 } | 596 } |
597 | 597 |
598 void Wait() { done_event_.Wait(); } | 598 void Wait() { done_event_.Wait(); } |
599 | 599 |
600 private: | 600 private: |
601 base::WaitableEvent done_event_; | 601 base::WaitableEvent done_event_; |
602 | 602 |
603 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadPlatformHandlesCheckerRawChannelDelegate); | 603 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadPlatformHandlesCheckerRawChannelDelegate); |
604 }; | 604 }; |
605 | 605 |
606 TEST_F(RawChannelTest, ReadWritePlatformHandles) { | 606 TEST_F(RawChannelTest, ReadWritePlatformHandles) { |
607 base::ScopedTempDir temp_dir; | 607 base::ScopedTempDir temp_dir; |
608 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 608 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
609 | 609 |
610 WriteOnlyRawChannelDelegate write_delegate; | 610 WriteOnlyRawChannelDelegate write_delegate; |
611 RawChannel* rc_write = RawChannel::Create(handles[0].Pass()); | 611 RawChannel* rc_write = RawChannel::Create(std::move(handles[0])); |
612 internal::g_io_thread_task_runner->PostTask( | 612 internal::g_io_thread_task_runner->PostTask( |
613 FROM_HERE, | 613 FROM_HERE, |
614 base::Bind(&InitOnIOThread, rc_write, base::Unretained(&write_delegate))); | 614 base::Bind(&InitOnIOThread, rc_write, base::Unretained(&write_delegate))); |
615 | 615 |
616 ReadPlatformHandlesCheckerRawChannelDelegate read_delegate; | 616 ReadPlatformHandlesCheckerRawChannelDelegate read_delegate; |
617 RawChannel* rc_read = RawChannel::Create(handles[1].Pass()); | 617 RawChannel* rc_read = RawChannel::Create(std::move(handles[1])); |
618 internal::g_io_thread_task_runner->PostTask( | 618 internal::g_io_thread_task_runner->PostTask( |
619 FROM_HERE, | 619 FROM_HERE, |
620 base::Bind(&InitOnIOThread, rc_read, base::Unretained(&read_delegate))); | 620 base::Bind(&InitOnIOThread, rc_read, base::Unretained(&read_delegate))); |
621 | 621 |
622 base::FilePath unused; | 622 base::FilePath unused; |
623 base::ScopedFILE fp1( | 623 base::ScopedFILE fp1( |
624 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 624 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
625 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); | 625 EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); |
626 base::ScopedFILE fp2( | 626 base::ScopedFILE fp2( |
627 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 627 base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
628 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); | 628 EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get())); |
629 | 629 |
630 { | 630 { |
631 const char kHello[] = "hello"; | 631 const char kHello[] = "hello"; |
632 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector()); | 632 ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector()); |
633 platform_handles->push_back( | 633 platform_handles->push_back( |
634 test::PlatformHandleFromFILE(fp1.Pass()).release()); | 634 test::PlatformHandleFromFILE(std::move(fp1)).release()); |
635 platform_handles->push_back( | 635 platform_handles->push_back( |
636 test::PlatformHandleFromFILE(fp2.Pass()).release()); | 636 test::PlatformHandleFromFILE(std::move(fp2)).release()); |
637 | 637 |
638 scoped_ptr<MessageInTransit> message( | 638 scoped_ptr<MessageInTransit> message( |
639 new MessageInTransit(MessageInTransit::Type::MESSAGE, | 639 new MessageInTransit(MessageInTransit::Type::MESSAGE, |
640 sizeof(kHello), kHello)); | 640 sizeof(kHello), kHello)); |
641 message->SetTransportData(make_scoped_ptr(new TransportData( | 641 message->SetTransportData(make_scoped_ptr( |
642 platform_handles.Pass(), rc_write->GetSerializedPlatformHandleSize()))); | 642 new TransportData(std::move(platform_handles), |
643 EXPECT_TRUE(rc_write->WriteMessage(message.Pass())); | 643 rc_write->GetSerializedPlatformHandleSize()))); |
| 644 EXPECT_TRUE(rc_write->WriteMessage(std::move(message))); |
644 } | 645 } |
645 | 646 |
646 read_delegate.Wait(); | 647 read_delegate.Wait(); |
647 | 648 |
648 internal::g_io_thread_task_runner->PostTask( | 649 internal::g_io_thread_task_runner->PostTask( |
649 FROM_HERE, | 650 FROM_HERE, |
650 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); | 651 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read))); |
651 internal::g_io_thread_task_runner->PostTask( | 652 internal::g_io_thread_task_runner->PostTask( |
652 FROM_HERE, | 653 FROM_HERE, |
653 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); | 654 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write))); |
654 } | 655 } |
655 | 656 |
656 } // namespace | 657 } // namespace |
657 } // namespace edk | 658 } // namespace edk |
658 } // namespace mojo | 659 } // namespace mojo |
OLD | NEW |