OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include <stdint.h> |
| 6 #include <memory> |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "content/browser/utility_process_host_impl.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/utility_process_host_client.h" |
| 15 #include "content/public/test/content_browser_test.h" |
| 16 #include "content/public/test/test_mojo_service.mojom.h" |
| 17 #include "services/shell/public/cpp/interface_provider.h" |
| 18 |
| 19 namespace content { |
| 20 namespace { |
| 21 |
| 22 const std::string kTestMessage = "My hovercraft is full of eels!"; |
| 23 |
| 24 class MojoSandboxTest : public ContentBrowserTest { |
| 25 public: |
| 26 MojoSandboxTest() {} |
| 27 |
| 28 void SetUpOnMainThread() override { |
| 29 base::RunLoop run_loop; |
| 30 BrowserThread::PostTaskAndReply( |
| 31 BrowserThread::IO, FROM_HERE, |
| 32 base::Bind(&MojoSandboxTest::StartUtilityProcessOnIoThread, |
| 33 base::Unretained(this)), |
| 34 run_loop.QuitClosure()); |
| 35 run_loop.Run(); |
| 36 } |
| 37 |
| 38 void TearDownOnMainThread() override { |
| 39 base::RunLoop run_loop; |
| 40 BrowserThread::PostTaskAndReply( |
| 41 BrowserThread::IO, FROM_HERE, |
| 42 base::Bind(&MojoSandboxTest::StopUtilityProcessOnIoThread, |
| 43 base::Unretained(this)), |
| 44 run_loop.QuitClosure()); |
| 45 run_loop.Run(); |
| 46 } |
| 47 |
| 48 protected: |
| 49 std::unique_ptr<UtilityProcessHostImpl> host_; |
| 50 |
| 51 private: |
| 52 void StartUtilityProcessOnIoThread() { |
| 53 host_.reset(new UtilityProcessHostImpl(nullptr, nullptr)); |
| 54 ASSERT_TRUE(host_->Start()); |
| 55 } |
| 56 |
| 57 void StopUtilityProcessOnIoThread() { host_.reset(); } |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(MojoSandboxTest); |
| 60 }; |
| 61 |
| 62 IN_PROC_BROWSER_TEST_F(MojoSandboxTest, SubprocessSharedBuffer) { |
| 63 // Ensures that a shared buffer can be created within a sandboxed process. |
| 64 |
| 65 mojom::TestMojoServicePtr test_service; |
| 66 host_->GetRemoteInterfaces()->GetInterface(&test_service); |
| 67 |
| 68 bool got_response = false; |
| 69 base::RunLoop run_loop; |
| 70 test_service.set_connection_error_handler(run_loop.QuitClosure()); |
| 71 test_service->CreateSharedBuffer( |
| 72 kTestMessage, |
| 73 base::Bind( |
| 74 [](const base::Closure& quit_closure, bool* got_response, |
| 75 mojo::ScopedSharedBufferHandle buffer) { |
| 76 ASSERT_TRUE(buffer.is_valid()); |
| 77 mojo::ScopedSharedBufferMapping mapping = |
| 78 buffer->Map(kTestMessage.size()); |
| 79 ASSERT_TRUE(mapping); |
| 80 std::string contents(static_cast<const char*>(mapping.get()), |
| 81 kTestMessage.size()); |
| 82 EXPECT_EQ(kTestMessage, contents); |
| 83 *got_response = true; |
| 84 quit_closure.Run(); |
| 85 }, |
| 86 run_loop.QuitClosure(), &got_response)); |
| 87 run_loop.Run(); |
| 88 EXPECT_TRUE(got_response); |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 } // namespace content |
OLD | NEW |