morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 5 | #include "ipc/ipc_mojo_bootstrap.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 8 | #include <memory> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 9 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 10 | #include "base/base_paths.h" |
| 11 | #include "base/files/file.h" |
| 12 | #include "base/message_loop/message_loop.h" |
rockot | cf1d7d0 | 2016-11-22 05:27:27 | [diff] [blame^] | 13 | #include "base/run_loop.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 14 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 15 | #include "build/build_config.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 16 | #include "ipc/ipc.mojom.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 17 | #include "ipc/ipc_test_base.h" |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 18 | #include "mojo/edk/embedder/embedder.h" |
| 19 | #include "mojo/edk/test/mojo_test_base.h" |
| 20 | #include "mojo/edk/test/multiprocess_test_helper.h" |
| 21 | #include "mojo/edk/test/scoped_ipc_support.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 22 | |
| 23 | #if defined(OS_POSIX) |
| 24 | #include "base/file_descriptor_posix.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace { |
| 28 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 29 | class IPCMojoBootstrapTest : public testing::Test { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 30 | protected: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 31 | mojo::edk::test::MultiprocessTestHelper helper_; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | class TestingDelegate : public IPC::MojoBootstrap::Delegate { |
| 35 | public: |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 36 | explicit TestingDelegate(const base::Closure& quit_callback) |
| 37 | : passed_(false), quit_callback_(quit_callback) {} |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 38 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 39 | void OnPipesAvailable( |
| 40 | IPC::mojom::ChannelAssociatedPtr sender, |
| 41 | IPC::mojom::ChannelAssociatedRequest receiver) override; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 42 | |
| 43 | bool passed() const { return passed_; } |
| 44 | |
| 45 | private: |
| 46 | bool passed_; |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 47 | const base::Closure quit_callback_; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 48 | }; |
| 49 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 50 | void TestingDelegate::OnPipesAvailable( |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 51 | IPC::mojom::ChannelAssociatedPtr sender, |
| 52 | IPC::mojom::ChannelAssociatedRequest receiver) { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 53 | passed_ = true; |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 54 | quit_callback_.Run(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 55 | } |
| 56 | |
amistry | 6de2ee4f | 2016-05-05 05:12:09 | [diff] [blame] | 57 | TEST_F(IPCMojoBootstrapTest, Connect) { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 58 | base::MessageLoop message_loop; |
| 59 | base::RunLoop run_loop; |
| 60 | TestingDelegate delegate(run_loop.QuitClosure()); |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 61 | std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 62 | helper_.StartChild("IPCMojoBootstrapTestClient"), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 63 | IPC::Channel::MODE_SERVER, &delegate, |
| 64 | base::ThreadTaskRunnerHandle::Get()); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 65 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 66 | bootstrap->Connect(); |
| 67 | run_loop.Run(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 68 | |
| 69 | EXPECT_TRUE(delegate.passed()); |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 70 | EXPECT_TRUE(helper_.WaitForChildTestShutdown()); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 71 | } |
| 72 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 73 | // A long running process that connects to us. |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 74 | MULTIPROCESS_TEST_MAIN_WITH_SETUP( |
| 75 | IPCMojoBootstrapTestClientTestChildMain, |
| 76 | ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) { |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 77 | base::MessageLoop message_loop; |
| 78 | base::RunLoop run_loop; |
| 79 | TestingDelegate delegate(run_loop.QuitClosure()); |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 80 | std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create( |
sammc | b0a39f8 | 2016-08-10 06:29:54 | [diff] [blame] | 81 | std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe), |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 82 | IPC::Channel::MODE_CLIENT, &delegate, |
| 83 | base::ThreadTaskRunnerHandle::Get()); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 84 | |
| 85 | bootstrap->Connect(); |
| 86 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 87 | run_loop.Run(); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 88 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 89 | return delegate.passed() ? 0 : 1; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace |