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 "ipc/ipc_mojo_bootstrap.h" | 5 #include "ipc/ipc_mojo_bootstrap.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "ipc/ipc.mojom.h" | 16 #include "ipc/ipc.mojom.h" |
| 17 #include "ipc/ipc_test_base.h" | 17 #include "ipc/ipc_test_base.h" |
| 18 #include "mojo/edk/embedder/embedder.h" | 18 #include "mojo/edk/embedder/embedder.h" |
| 19 #include "mojo/edk/test/mojo_test_base.h" | 19 #include "mojo/edk/test/mojo_test_base.h" |
| 20 #include "mojo/edk/test/multiprocess_test_helper.h" | 20 #include "mojo/edk/test/multiprocess_test_helper.h" |
| 21 #include "mojo/public/cpp/bindings/associated_binding.h" | |
| 21 | 22 |
| 22 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
| 23 #include "base/file_descriptor_posix.h" | 24 #include "base/file_descriptor_posix.h" |
| 24 #endif | 25 #endif |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 29 constexpr int32_t kTestPid = 42; | |
| 30 | |
| 31 class PeerPidReceiver : public IPC::mojom::Channel { | |
| 32 public: | |
| 33 PeerPidReceiver(IPC::mojom::ChannelAssociatedRequest request, | |
| 34 const base::Closure& on_peer_pid_set) | |
|
yzshen1
2017/02/07 00:06:26
nit: wrong indent.
Ken Rockot(use gerrit already)
2017/02/07 01:25:32
done
| |
| 35 : binding_(this, std::move(request)), on_peer_pid_set_(on_peer_pid_set) {} | |
| 36 ~PeerPidReceiver() override {} | |
| 37 | |
| 38 // mojom::Channel: | |
| 39 void SetPeerPid(int32_t pid) override { | |
| 40 peer_pid_ = pid; | |
| 41 on_peer_pid_set_.Run(); | |
| 42 } | |
| 43 | |
| 44 void Receive( | |
| 45 const std::vector<uint8_t>& data, | |
| 46 base::Optional<std::vector<IPC::mojom::SerializedHandlePtr>> | |
| 47 handles) override { | |
| 48 } | |
| 49 | |
| 50 void GetAssociatedInterface( | |
| 51 const std::string& name, | |
| 52 IPC::mojom::GenericInterfaceAssociatedRequest request) override { | |
| 53 } | |
| 54 | |
| 55 int32_t peer_pid() const { return peer_pid_; } | |
| 56 | |
| 57 private: | |
| 58 mojo::AssociatedBinding<IPC::mojom::Channel> binding_; | |
| 59 const base::Closure on_peer_pid_set_; | |
| 60 int32_t peer_pid_ = -1; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(PeerPidReceiver); | |
| 63 }; | |
| 64 | |
| 28 class IPCMojoBootstrapTest : public testing::Test { | 65 class IPCMojoBootstrapTest : public testing::Test { |
| 29 protected: | 66 protected: |
| 30 mojo::edk::test::MultiprocessTestHelper helper_; | 67 mojo::edk::test::MultiprocessTestHelper helper_; |
| 31 }; | 68 }; |
| 32 | 69 |
| 33 class TestingDelegate : public IPC::MojoBootstrap::Delegate { | |
| 34 public: | |
| 35 explicit TestingDelegate(const base::Closure& quit_callback) | |
| 36 : passed_(false), quit_callback_(quit_callback) {} | |
| 37 | |
| 38 void OnPipesAvailable( | |
| 39 IPC::mojom::ChannelAssociatedPtr sender, | |
| 40 IPC::mojom::ChannelAssociatedRequest receiver) override; | |
| 41 | |
| 42 bool passed() const { return passed_; } | |
| 43 | |
| 44 private: | |
| 45 bool passed_; | |
| 46 const base::Closure quit_callback_; | |
| 47 }; | |
| 48 | |
| 49 void TestingDelegate::OnPipesAvailable( | |
| 50 IPC::mojom::ChannelAssociatedPtr sender, | |
| 51 IPC::mojom::ChannelAssociatedRequest receiver) { | |
| 52 passed_ = true; | |
| 53 quit_callback_.Run(); | |
| 54 } | |
| 55 | |
| 56 TEST_F(IPCMojoBootstrapTest, Connect) { | 70 TEST_F(IPCMojoBootstrapTest, Connect) { |
| 57 base::MessageLoop message_loop; | 71 base::MessageLoop message_loop; |
| 58 base::RunLoop run_loop; | |
| 59 TestingDelegate delegate(run_loop.QuitClosure()); | |
| 60 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( | 72 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
| 61 helper_.StartChild("IPCMojoBootstrapTestClient"), | 73 helper_.StartChild("IPCMojoBootstrapTestClient"), |
| 62 IPC::Channel::MODE_SERVER, &delegate, | 74 IPC::Channel::MODE_SERVER, base::ThreadTaskRunnerHandle::Get()); |
| 63 base::ThreadTaskRunnerHandle::Get()); | |
| 64 | 75 |
| 65 bootstrap->Connect(); | 76 IPC::mojom::ChannelAssociatedPtr sender; |
| 77 IPC::mojom::ChannelAssociatedRequest receiver; | |
| 78 bootstrap->Connect(&sender, &receiver); | |
| 79 sender->SetPeerPid(kTestPid); | |
| 80 | |
| 81 base::RunLoop run_loop; | |
| 82 PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); | |
| 66 run_loop.Run(); | 83 run_loop.Run(); |
| 67 | 84 |
| 68 EXPECT_TRUE(delegate.passed()); | 85 EXPECT_EQ(kTestPid, impl.peer_pid()); |
| 86 | |
| 69 EXPECT_TRUE(helper_.WaitForChildTestShutdown()); | 87 EXPECT_TRUE(helper_.WaitForChildTestShutdown()); |
| 70 } | 88 } |
| 71 | 89 |
| 72 // A long running process that connects to us. | 90 // A long running process that connects to us. |
| 73 MULTIPROCESS_TEST_MAIN_WITH_SETUP( | 91 MULTIPROCESS_TEST_MAIN_WITH_SETUP( |
| 74 IPCMojoBootstrapTestClientTestChildMain, | 92 IPCMojoBootstrapTestClientTestChildMain, |
| 75 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { | 93 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { |
| 76 base::MessageLoop message_loop; | 94 base::MessageLoop message_loop; |
| 77 base::RunLoop run_loop; | |
| 78 TestingDelegate delegate(run_loop.QuitClosure()); | |
| 79 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( | 95 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
| 80 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), | 96 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), |
| 81 IPC::Channel::MODE_CLIENT, &delegate, | 97 IPC::Channel::MODE_CLIENT, base::ThreadTaskRunnerHandle::Get()); |
| 82 base::ThreadTaskRunnerHandle::Get()); | |
| 83 | 98 |
| 84 bootstrap->Connect(); | 99 IPC::mojom::ChannelAssociatedPtr sender; |
| 100 IPC::mojom::ChannelAssociatedRequest receiver; | |
| 101 bootstrap->Connect(&sender, &receiver); | |
| 102 sender->SetPeerPid(kTestPid); | |
|
yzshen1
2017/02/07 00:06:26
Does it make sense to use a different ID than the
Ken Rockot(use gerrit already)
2017/02/07 01:25:32
done
| |
| 85 | 103 |
| 104 base::RunLoop run_loop; | |
| 105 PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); | |
| 86 run_loop.Run(); | 106 run_loop.Run(); |
| 87 | 107 |
| 88 return delegate.passed() ? 0 : 1; | 108 EXPECT_EQ(kTestPid, impl.peer_pid()); |
| 109 | |
| 110 return 0; | |
| 89 } | 111 } |
| 90 | 112 |
| 91 } // namespace | 113 } // namespace |
| OLD | NEW |