| 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 "content/public/browser/utility_process_mojo_client.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/test/content_browser_test.h" |
| 16 #include "content/public/test/test_mojo_service.mojom.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 // Test fixture used to make different Mojo calls to the utility process. |
| 21 class UtilityProcessMojoClientBrowserTest : public ContentBrowserTest { |
| 22 public: |
| 23 void StartMojoService(bool disable_sandbox) { |
| 24 mojo_client_.reset(new UtilityProcessMojoClient<mojom::TestMojoService>( |
| 25 base::ASCIIToUTF16("TestMojoProcess"), |
| 26 base::Bind(&UtilityProcessMojoClientBrowserTest::OnConnectionError, |
| 27 base::Unretained(this)))); |
| 28 |
| 29 // This test case needs to have the sandbox disabled. |
| 30 if (disable_sandbox) |
| 31 mojo_client_->set_disable_sandbox(); |
| 32 mojo_client_->Start(); |
| 33 } |
| 34 |
| 35 // Called when a response is received from a call to DoSomething() or |
| 36 // DoTerminateProcess(). |
| 37 void OnResponseReceived() { |
| 38 response_received_ = true; |
| 39 done_closure_.Run(); |
| 40 } |
| 41 |
| 42 // Called when a connection error happen between the test and the utility |
| 43 // process. |
| 44 void OnConnectionError() { |
| 45 error_happened_ = true; |
| 46 done_closure_.Run(); |
| 47 } |
| 48 |
| 49 // Called when a responsed is received from a call to CreateFolder(). |
| 50 void OnCreateFolderFinished(bool succeeded) { |
| 51 response_received_ = true; |
| 52 sandbox_succeeded_ = succeeded; |
| 53 done_closure_.Run(); |
| 54 } |
| 55 |
| 56 protected: |
| 57 std::unique_ptr<UtilityProcessMojoClient<mojom::TestMojoService>> |
| 58 mojo_client_; |
| 59 base::Closure done_closure_; |
| 60 |
| 61 // The test result that is compared to the current test case. |
| 62 bool response_received_ = false; |
| 63 bool error_happened_ = false; |
| 64 bool sandbox_succeeded_ = false; |
| 65 }; |
| 66 |
| 67 // Successful call through the Mojo service with response back. |
| 68 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, CallService) { |
| 69 base::RunLoop run_loop; |
| 70 done_closure_ = run_loop.QuitClosure(); |
| 71 |
| 72 StartMojoService(false); |
| 73 |
| 74 mojo_client_->service()->DoSomething( |
| 75 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, |
| 76 base::Unretained(this))); |
| 77 |
| 78 run_loop.Run(); |
| 79 EXPECT_TRUE(response_received_); |
| 80 EXPECT_FALSE(error_happened_); |
| 81 } |
| 82 |
| 83 // Call the Mojo service but the utility process terminates before getting |
| 84 // the result back. |
| 85 // TODO(pmonette): Re-enable when crbug.com/618206 is fixed. |
| 86 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, |
| 87 DISABLED_ConnectionError) { |
| 88 base::RunLoop run_loop; |
| 89 done_closure_ = run_loop.QuitClosure(); |
| 90 |
| 91 StartMojoService(false); |
| 92 |
| 93 mojo_client_->service()->DoTerminateProcess( |
| 94 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, |
| 95 base::Unretained(this))); |
| 96 |
| 97 run_loop.Run(); |
| 98 EXPECT_FALSE(response_received_); |
| 99 EXPECT_TRUE(error_happened_); |
| 100 } |
| 101 |
| 102 // Android doesn't support non-sandboxed utility processes. |
| 103 #if !defined(OS_ANDROID) |
| 104 // Call a function that fails because the sandbox is still enabled. |
| 105 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxFailure) { |
| 106 base::RunLoop run_loop; |
| 107 done_closure_ = run_loop.QuitClosure(); |
| 108 |
| 109 StartMojoService(false); |
| 110 |
| 111 mojo_client_->service()->CreateFolder( |
| 112 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, |
| 113 base::Unretained(this))); |
| 114 |
| 115 run_loop.Run(); |
| 116 EXPECT_TRUE(response_received_); |
| 117 // If the sandbox is disabled by the command line, this will succeed. |
| 118 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) |
| 119 EXPECT_FALSE(sandbox_succeeded_); |
| 120 EXPECT_FALSE(error_happened_); |
| 121 } |
| 122 |
| 123 // Call a function that succeeds only when the sandbox is disabled. |
| 124 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxSuccess) { |
| 125 base::RunLoop run_loop; |
| 126 done_closure_ = run_loop.QuitClosure(); |
| 127 |
| 128 StartMojoService(true); |
| 129 |
| 130 mojo_client_->service()->CreateFolder( |
| 131 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, |
| 132 base::Unretained(this))); |
| 133 |
| 134 run_loop.Run(); |
| 135 EXPECT_TRUE(response_received_); |
| 136 EXPECT_TRUE(sandbox_succeeded_); |
| 137 EXPECT_FALSE(error_happened_); |
| 138 } |
| 139 #endif |
| 140 |
| 141 } // namespace content |
| OLD | NEW |