Chromium Code Reviews| 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/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/base_paths.h" | |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 15 #include "base/files/file.h" | 16 #include "base/files/file.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/memory/shared_memory.h" | 19 #include "base/memory/shared_memory.h" |
| 19 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/path_service.h" | |
| 20 #include "base/process/process_handle.h" | 22 #include "base/process/process_handle.h" |
| 23 #include "base/run_loop.h" | |
| 21 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
| 22 #include "base/test/test_timeouts.h" | 25 #include "base/test/test_timeouts.h" |
| 26 #include "mojo/edk/embedder/named_platform_handle.h" | |
| 27 #include "mojo/edk/embedder/named_platform_handle_utils.h" | |
| 23 #include "mojo/edk/embedder/platform_channel_pair.h" | 28 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 24 #include "mojo/edk/embedder/test_embedder.h" | 29 #include "mojo/edk/embedder/test_embedder.h" |
| 25 #include "mojo/edk/system/test_utils.h" | 30 #include "mojo/edk/system/test_utils.h" |
| 26 #include "mojo/edk/test/mojo_test_base.h" | 31 #include "mojo/edk/test/mojo_test_base.h" |
| 32 #include "mojo/edk/test/scoped_ipc_support.h" | |
| 27 #include "mojo/public/c/system/core.h" | 33 #include "mojo/public/c/system/core.h" |
| 28 #include "mojo/public/cpp/system/handle.h" | 34 #include "mojo/public/cpp/system/handle.h" |
| 29 #include "mojo/public/cpp/system/message_pipe.h" | 35 #include "mojo/public/cpp/system/message_pipe.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 37 |
| 32 namespace mojo { | 38 namespace mojo { |
| 33 namespace edk { | 39 namespace edk { |
| 34 namespace { | 40 namespace { |
| 35 | 41 |
| 36 const MojoHandleSignals kSignalReadadableWritable = | 42 const MojoHandleSignals kSignalReadadableWritable = |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 WriteMessage(client_mp, "bye"); | 556 WriteMessage(client_mp, "bye"); |
| 551 } | 557 } |
| 552 | 558 |
| 553 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 559 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 554 | 560 |
| 555 // TODO(vtl): Test immediate write & close. | 561 // TODO(vtl): Test immediate write & close. |
| 556 // TODO(vtl): Test broken-connection cases. | 562 // TODO(vtl): Test broken-connection cases. |
| 557 | 563 |
| 558 #endif // !defined(OS_IOS) | 564 #endif // !defined(OS_IOS) |
| 559 | 565 |
| 566 NamedPlatformHandle GenerateChannelName() { | |
| 567 #if defined(OS_POSIX) | |
| 568 base::FilePath temp_dir; | |
| 569 CHECK(base::PathService::Get(base::DIR_TEMP, &temp_dir)); | |
| 570 return NamedPlatformHandle( | |
| 571 temp_dir.AppendASCII(GenerateRandomToken()).value()); | |
| 572 #else | |
| 573 return NamedPlatformHandle(GenerateRandomToken()); | |
| 574 #endif | |
| 575 } | |
| 576 | |
| 577 TEST_F(EmbedderTest, ClosePendingPeerConnection) { | |
| 578 NamedPlatformHandle named_handle = GenerateChannelName(); | |
| 579 std::string peer_token = GenerateRandomToken(); | |
| 580 ScopedMessagePipeHandle server_pipe = | |
| 581 ConnectToPeerProcess(CreateServerHandle(named_handle), peer_token); | |
| 582 ClosePeerConnection(peer_token); | |
| 583 EXPECT_EQ(MOJO_RESULT_OK, | |
| 584 Wait(server_pipe.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 585 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
| 586 // Closing the physical pipe may take some time so ping-pong between the main | |
| 587 // thread and the IO thread a few times. | |
| 588 base::MessageLoop message_loop; | |
| 589 for (size_t i = 0; i < 10; i++) { | |
|
Ken Rockot(use gerrit already)
2016/11/03 05:02:52
This seems destined to be flaky. Can we not do thi
Sam McNally
2016/11/03 07:13:36
Done.
| |
| 590 base::RunLoop run_loop; | |
| 591 test::GetIoTaskRunner()->PostTaskAndReply( | |
| 592 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); | |
| 593 run_loop.Run(); | |
| 594 } | |
| 595 EXPECT_FALSE(CreateClientHandle(named_handle).is_valid()); | |
| 596 } | |
| 597 | |
| 598 #if !defined(OS_IOS) | |
| 599 | |
| 600 TEST_F(EmbedderTest, ClosePipeToConnectedPeer) { | |
| 601 set_launch_type(LaunchType::PEER); | |
| 602 auto& controller = StartClient("ClosePipeToConnectedPeerClient"); | |
| 603 MojoHandle server_mp = controller.pipe(); | |
| 604 // 1. Write a message to |server_mp| (attaching nothing). | |
| 605 WriteMessage(server_mp, "hello"); | |
| 606 | |
| 607 // 2. Read a message from |server_mp|. | |
| 608 EXPECT_EQ("world!", ReadMessage(server_mp)); | |
| 609 | |
| 610 controller.ClosePeerConnection(); | |
| 611 | |
| 612 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 613 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
| 614 | |
| 615 EXPECT_EQ(0, controller.WaitForShutdown()); | |
| 616 } | |
| 617 | |
| 618 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectedPeerClient, EmbedderTest, | |
| 619 client_mp) { | |
| 620 // 1. Read the first message from |client_mp|. | |
| 621 EXPECT_EQ("hello", ReadMessage(client_mp)); | |
| 622 | |
| 623 // 2. Write a message to |client_mp| (attaching nothing). | |
| 624 WriteMessage(client_mp, "world!"); | |
| 625 | |
| 626 ASSERT_EQ(MOJO_RESULT_OK, | |
| 627 MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 628 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
| 629 } | |
| 630 | |
| 631 TEST_F(EmbedderTest, ClosePipeToConnectingPeer) { | |
| 632 set_launch_type(LaunchType::PEER); | |
| 633 auto& controller = StartClient("ClosePipeToConnectingPeerClient"); | |
| 634 controller.ClosePeerConnection(); | |
| 635 | |
| 636 MojoHandle server_mp = controller.pipe(); | |
| 637 | |
| 638 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(server_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 639 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
| 640 | |
| 641 EXPECT_EQ(0, controller.WaitForShutdown()); | |
| 642 } | |
| 643 | |
| 644 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ClosePipeToConnectingPeerClient, EmbedderTest, | |
| 645 client_mp) { | |
| 646 ASSERT_EQ(MOJO_RESULT_OK, MojoWait(client_mp, MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
| 647 MOJO_DEADLINE_INDEFINITE, nullptr)); | |
| 648 } | |
| 649 | |
| 650 #endif // !defined(OS_IOS) | |
| 651 | |
| 560 } // namespace | 652 } // namespace |
| 561 } // namespace edk | 653 } // namespace edk |
| 562 } // namespace mojo | 654 } // namespace mojo |
| OLD | NEW |