0
Files
src/content/browser/mojo_sandbox_browsertest.cc
Julie Jeongeun Kim 29cb9cb731 Convert TestService to new Mojo types
This CL converts TestServicePtr and TestServiceRequest
to new Mojo types.

It uses Remote, PendingReceiver and Receiver instead of
QueryableDataStorePtr, QueryableDataStoreRequest and Binding.

Bug: 955171
Change-Id: I61f3d6b66ac6f5faffe533800bbe482a2db772ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1803948
Commit-Queue: Julie Kim <jkim@igalia.com>
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: Oksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697068}
2019-09-17 02:58:35 +00:00

97 lines
3.0 KiB
C++

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "content/browser/utility_process_host.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/bind_interface_helpers.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/test_service.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
namespace content {
namespace {
const std::string kTestMessage = "My hovercraft is full of eels!";
class MojoSandboxTest : public ContentBrowserTest {
public:
MojoSandboxTest() {}
void SetUpOnMainThread() override {
base::RunLoop run_loop;
base::PostTaskAndReply(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&MojoSandboxTest::StartUtilityProcessOnIoThread,
base::Unretained(this)),
run_loop.QuitClosure());
run_loop.Run();
}
void TearDownOnMainThread() override {
base::RunLoop run_loop;
base::PostTaskAndReply(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&MojoSandboxTest::StopUtilityProcessOnIoThread,
base::Unretained(this)),
run_loop.QuitClosure());
run_loop.Run();
}
protected:
std::unique_ptr<UtilityProcessHost> host_;
private:
void StartUtilityProcessOnIoThread() {
host_.reset(new UtilityProcessHost());
host_->SetMetricsName("mojo_sandbox_test_process");
ASSERT_TRUE(host_->Start());
}
void StopUtilityProcessOnIoThread() { host_.reset(); }
DISALLOW_COPY_AND_ASSIGN(MojoSandboxTest);
};
IN_PROC_BROWSER_TEST_F(MojoSandboxTest, SubprocessSharedBuffer) {
// Ensures that a shared buffer can be created within a sandboxed process.
mojo::Remote<mojom::TestService> test_service;
BindInterface(host_.get(), test_service.BindNewPipeAndPassReceiver());
bool got_response = false;
base::RunLoop run_loop;
test_service.set_disconnect_handler(run_loop.QuitClosure());
test_service->CreateSharedBuffer(
kTestMessage,
base::BindOnce(
[](const base::Closure& quit_closure, bool* got_response,
mojo::ScopedSharedBufferHandle buffer) {
ASSERT_TRUE(buffer.is_valid());
mojo::ScopedSharedBufferMapping mapping =
buffer->Map(kTestMessage.size());
ASSERT_TRUE(mapping);
std::string contents(static_cast<const char*>(mapping.get()),
kTestMessage.size());
EXPECT_EQ(kTestMessage, contents);
*got_response = true;
quit_closure.Run();
},
run_loop.QuitClosure(), &got_response));
run_loop.Run();
EXPECT_TRUE(got_response);
}
} // namespace
} // namespace content