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 kTestServerPid = 42; |
| 30 constexpr int32_t kTestClientPid = 4242; |
| 31 |
| 32 class PeerPidReceiver : public IPC::mojom::Channel { |
| 33 public: |
| 34 PeerPidReceiver(IPC::mojom::ChannelAssociatedRequest request, |
| 35 const base::Closure& on_peer_pid_set) |
| 36 : binding_(this, std::move(request)), on_peer_pid_set_(on_peer_pid_set) {} |
| 37 ~PeerPidReceiver() override {} |
| 38 |
| 39 // mojom::Channel: |
| 40 void SetPeerPid(int32_t pid) override { |
| 41 peer_pid_ = pid; |
| 42 on_peer_pid_set_.Run(); |
| 43 } |
| 44 |
| 45 void Receive(const std::vector<uint8_t>& data, |
| 46 base::Optional<std::vector<IPC::mojom::SerializedHandlePtr>> |
| 47 handles) override {} |
| 48 |
| 49 void GetAssociatedInterface( |
| 50 const std::string& name, |
| 51 IPC::mojom::GenericInterfaceAssociatedRequest request) override {} |
| 52 |
| 53 int32_t peer_pid() const { return peer_pid_; } |
| 54 |
| 55 private: |
| 56 mojo::AssociatedBinding<IPC::mojom::Channel> binding_; |
| 57 const base::Closure on_peer_pid_set_; |
| 58 int32_t peer_pid_ = -1; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(PeerPidReceiver); |
| 61 }; |
| 62 |
28 class IPCMojoBootstrapTest : public testing::Test { | 63 class IPCMojoBootstrapTest : public testing::Test { |
29 protected: | 64 protected: |
30 mojo::edk::test::MultiprocessTestHelper helper_; | 65 mojo::edk::test::MultiprocessTestHelper helper_; |
31 }; | 66 }; |
32 | 67 |
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) { | 68 TEST_F(IPCMojoBootstrapTest, Connect) { |
57 base::MessageLoop message_loop; | 69 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( | 70 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
61 helper_.StartChild("IPCMojoBootstrapTestClient"), | 71 helper_.StartChild("IPCMojoBootstrapTestClient"), |
62 IPC::Channel::MODE_SERVER, &delegate, | 72 IPC::Channel::MODE_SERVER, base::ThreadTaskRunnerHandle::Get()); |
63 base::ThreadTaskRunnerHandle::Get()); | |
64 | 73 |
65 bootstrap->Connect(); | 74 IPC::mojom::ChannelAssociatedPtr sender; |
| 75 IPC::mojom::ChannelAssociatedRequest receiver; |
| 76 bootstrap->Connect(&sender, &receiver); |
| 77 sender->SetPeerPid(kTestServerPid); |
| 78 |
| 79 base::RunLoop run_loop; |
| 80 PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); |
66 run_loop.Run(); | 81 run_loop.Run(); |
67 | 82 |
68 EXPECT_TRUE(delegate.passed()); | 83 EXPECT_EQ(kTestClientPid, impl.peer_pid()); |
| 84 |
69 EXPECT_TRUE(helper_.WaitForChildTestShutdown()); | 85 EXPECT_TRUE(helper_.WaitForChildTestShutdown()); |
70 } | 86 } |
71 | 87 |
72 // A long running process that connects to us. | 88 // A long running process that connects to us. |
73 MULTIPROCESS_TEST_MAIN_WITH_SETUP( | 89 MULTIPROCESS_TEST_MAIN_WITH_SETUP( |
74 IPCMojoBootstrapTestClientTestChildMain, | 90 IPCMojoBootstrapTestClientTestChildMain, |
75 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { | 91 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { |
76 base::MessageLoop message_loop; | 92 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( | 93 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
80 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), | 94 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), |
81 IPC::Channel::MODE_CLIENT, &delegate, | 95 IPC::Channel::MODE_CLIENT, base::ThreadTaskRunnerHandle::Get()); |
82 base::ThreadTaskRunnerHandle::Get()); | |
83 | 96 |
84 bootstrap->Connect(); | 97 IPC::mojom::ChannelAssociatedPtr sender; |
| 98 IPC::mojom::ChannelAssociatedRequest receiver; |
| 99 bootstrap->Connect(&sender, &receiver); |
| 100 sender->SetPeerPid(kTestClientPid); |
85 | 101 |
| 102 base::RunLoop run_loop; |
| 103 PeerPidReceiver impl(std::move(receiver), run_loop.QuitClosure()); |
86 run_loop.Run(); | 104 run_loop.Run(); |
87 | 105 |
88 return delegate.passed() ? 0 : 1; | 106 EXPECT_EQ(kTestServerPid, impl.peer_pid()); |
| 107 |
| 108 return 0; |
89 } | 109 } |
90 | 110 |
91 } // namespace | 111 } // namespace |
OLD | NEW |