summaryrefslogtreecommitdiff
path: root/chromium/content/browser/mojo_sandbox_browsertest.cc
blob: f3eddd9ad57eb1a230d70278e5d0217a283b0b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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 "content/browser/utility_process_host_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/utility_process_host_client.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/test_service.mojom.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;
    BrowserThread::PostTaskAndReply(
        BrowserThread::IO, FROM_HERE,
        base::Bind(&MojoSandboxTest::StartUtilityProcessOnIoThread,
                   base::Unretained(this)),
        run_loop.QuitClosure());
    run_loop.Run();
  }

  void TearDownOnMainThread() override {
    base::RunLoop run_loop;
    BrowserThread::PostTaskAndReply(
        BrowserThread::IO, FROM_HERE,
        base::Bind(&MojoSandboxTest::StopUtilityProcessOnIoThread,
                   base::Unretained(this)),
        run_loop.QuitClosure());
    run_loop.Run();
  }

 protected:
  std::unique_ptr<UtilityProcessHostImpl> host_;

 private:
  void StartUtilityProcessOnIoThread() {
    host_.reset(new UtilityProcessHostImpl(nullptr, nullptr));
    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.

  mojom::TestServicePtr test_service;
  host_->GetRemoteInterfaces()->GetInterface(&test_service);

  bool got_response = false;
  base::RunLoop run_loop;
  test_service.set_connection_error_handler(run_loop.QuitClosure());
  test_service->CreateSharedBuffer(
      kTestMessage,
      base::Bind(
          [](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