blob: b0a4709fcf8cfd68274dced68427f12fb41177f3 [file] [log] [blame]
morrita54f6f80c2014-09-23 21:16:001// 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
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_mojo_bootstrap.h"
morrita54f6f80c2014-09-23 21:16:006
avi246998d82015-12-22 02:39:047#include <stdint.h>
danakj03de39b22016-04-23 04:21:098#include <memory>
avi246998d82015-12-22 02:39:049
morrita54f6f80c2014-09-23 21:16:0010#include "base/base_paths.h"
11#include "base/files/file.h"
12#include "base/message_loop/message_loop.h"
rockotcf1d7d02016-11-22 05:27:2713#include "base/run_loop.h"
gabf08ccc02016-05-11 18:51:1114#include "base/threading/thread_task_runner_handle.h"
avi246998d82015-12-22 02:39:0415#include "build/build_config.h"
amistryd4aa70d2016-06-23 07:52:3716#include "ipc/ipc.mojom.h"
morrita54f6f80c2014-09-23 21:16:0017#include "ipc/ipc_test_base.h"
sammce4d0abd2016-03-07 22:38:0418#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"
morrita54f6f80c2014-09-23 21:16:0022
23#if defined(OS_POSIX)
24#include "base/file_descriptor_posix.h"
25#endif
26
27namespace {
28
sammc57ed9f982016-03-10 06:28:3529class IPCMojoBootstrapTest : public testing::Test {
morrita54f6f80c2014-09-23 21:16:0030 protected:
sammc57ed9f982016-03-10 06:28:3531 mojo::edk::test::MultiprocessTestHelper helper_;
morrita54f6f80c2014-09-23 21:16:0032};
33
34class TestingDelegate : public IPC::MojoBootstrap::Delegate {
35 public:
sammce4d0abd2016-03-07 22:38:0436 explicit TestingDelegate(const base::Closure& quit_callback)
37 : passed_(false), quit_callback_(quit_callback) {}
morrita54f6f80c2014-09-23 21:16:0038
rockot0e4de5f2016-07-22 21:18:0739 void OnPipesAvailable(
40 IPC::mojom::ChannelAssociatedPtr sender,
41 IPC::mojom::ChannelAssociatedRequest receiver) override;
morrita54f6f80c2014-09-23 21:16:0042
43 bool passed() const { return passed_; }
44
45 private:
46 bool passed_;
sammce4d0abd2016-03-07 22:38:0447 const base::Closure quit_callback_;
morrita54f6f80c2014-09-23 21:16:0048};
49
sammce4d0abd2016-03-07 22:38:0450void TestingDelegate::OnPipesAvailable(
rockot0e4de5f2016-07-22 21:18:0751 IPC::mojom::ChannelAssociatedPtr sender,
52 IPC::mojom::ChannelAssociatedRequest receiver) {
morrita54f6f80c2014-09-23 21:16:0053 passed_ = true;
sammce4d0abd2016-03-07 22:38:0454 quit_callback_.Run();
morrita54f6f80c2014-09-23 21:16:0055}
56
amistry6de2ee4f2016-05-05 05:12:0957TEST_F(IPCMojoBootstrapTest, Connect) {
sammce4d0abd2016-03-07 22:38:0458 base::MessageLoop message_loop;
59 base::RunLoop run_loop;
60 TestingDelegate delegate(run_loop.QuitClosure());
danakj03de39b22016-04-23 04:21:0961 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create(
sammc57ed9f982016-03-10 06:28:3562 helper_.StartChild("IPCMojoBootstrapTestClient"),
rockot0e4de5f2016-07-22 21:18:0763 IPC::Channel::MODE_SERVER, &delegate,
64 base::ThreadTaskRunnerHandle::Get());
morrita54f6f80c2014-09-23 21:16:0065
sammce4d0abd2016-03-07 22:38:0466 bootstrap->Connect();
67 run_loop.Run();
morrita54f6f80c2014-09-23 21:16:0068
69 EXPECT_TRUE(delegate.passed());
sammc57ed9f982016-03-10 06:28:3570 EXPECT_TRUE(helper_.WaitForChildTestShutdown());
morrita54f6f80c2014-09-23 21:16:0071}
72
sammce4d0abd2016-03-07 22:38:0473// A long running process that connects to us.
sammc57ed9f982016-03-10 06:28:3574MULTIPROCESS_TEST_MAIN_WITH_SETUP(
75 IPCMojoBootstrapTestClientTestChildMain,
76 ::mojo::edk::test::MultiprocessTestHelper::ChildSetup) {
sammce4d0abd2016-03-07 22:38:0477 base::MessageLoop message_loop;
78 base::RunLoop run_loop;
79 TestingDelegate delegate(run_loop.QuitClosure());
danakj03de39b22016-04-23 04:21:0980 std::unique_ptr<IPC::MojoBootstrap> bootstrap = IPC::MojoBootstrap::Create(
sammcb0a39f82016-08-10 06:29:5481 std::move(mojo::edk::test::MultiprocessTestHelper::primordial_pipe),
rockot0e4de5f2016-07-22 21:18:0782 IPC::Channel::MODE_CLIENT, &delegate,
83 base::ThreadTaskRunnerHandle::Get());
morrita54f6f80c2014-09-23 21:16:0084
85 bootstrap->Connect();
86
sammce4d0abd2016-03-07 22:38:0487 run_loop.Run();
morrita54f6f80c2014-09-23 21:16:0088
sammc57ed9f982016-03-10 06:28:3589 return delegate.passed() ? 0 : 1;
morrita54f6f80c2014-09-23 21:16:0090}
91
92} // namespace