| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/browser/utility_process_mojo_client.h" | 5 #include "content/public/browser/utility_process_mojo_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/test/content_browser_test.h" | 15 #include "content/public/test/content_browser_test.h" |
| 16 #include "content/public/test/test_service.mojom.h" | 16 #include "content/public/test/test_service.mojom.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 // Test fixture used to make different Mojo calls to the utility process. | 20 // Test fixture used to make different Mojo calls to the utility process. |
| 21 class UtilityProcessMojoClientBrowserTest : public ContentBrowserTest { | 21 class UtilityProcessMojoClientBrowserTest : public ContentBrowserTest { |
| 22 public: | 22 public: |
| 23 void StartMojoService(bool disable_sandbox, bool run_elevated = false) { | 23 enum RunOption { |
| 24 SANDBOXED, UNSANDBOXED, ELEVATED |
| 25 }; |
| 26 |
| 27 void StartMojoService(RunOption option = SANDBOXED) { |
| 24 mojo_client_.reset(new UtilityProcessMojoClient<mojom::TestService>( | 28 mojo_client_.reset(new UtilityProcessMojoClient<mojom::TestService>( |
| 25 base::ASCIIToUTF16("TestMojoProcess"))); | 29 base::ASCIIToUTF16("TestUtilityProcessMojoClient"))); |
| 26 | 30 |
| 27 mojo_client_->set_error_callback( | 31 mojo_client_->set_error_callback( |
| 28 base::Bind(&UtilityProcessMojoClientBrowserTest::OnConnectionError, | 32 base::Bind(&UtilityProcessMojoClientBrowserTest::OnConnectionError, |
| 29 base::Unretained(this))); | 33 base::Unretained(this))); |
| 30 | 34 |
| 31 // This test case needs to have the sandbox disabled. | 35 // This test case needs to have the sandbox disabled. |
| 32 if (disable_sandbox) | 36 if (option == UNSANDBOXED) |
| 33 mojo_client_->set_disable_sandbox(); | 37 mojo_client_->set_disable_sandbox(); |
| 34 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 35 // This test case needs utility process privilege elevation. | 39 // This test case needs utility process UAC privilege elevation. |
| 36 if (run_elevated) { | 40 if (option == ELEVATED) |
| 37 CHECK(disable_sandbox); | |
| 38 mojo_client_->set_run_elevated(); | 41 mojo_client_->set_run_elevated(); |
| 39 } | |
| 40 #endif // defined(OS_WIN) | 42 #endif // defined(OS_WIN) |
| 41 | 43 |
| 42 mojo_client_->Start(); | 44 mojo_client_->Start(); |
| 43 } | 45 } |
| 44 | 46 |
| 45 // Called when a response is received from a call to DoSomething() or | 47 // Called when a response is received from a call to DoSomething() or |
| 46 // DoTerminateProcess(). | 48 // DoTerminateProcess(). |
| 47 void OnResponseReceived() { | 49 void OnResponseReceived() { |
| 48 response_received_ = true; | 50 response_received_ = true; |
| 49 done_closure_.Run(); | 51 done_closure_.Run(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 bool response_received_ = false; | 74 bool response_received_ = false; |
| 73 bool error_happened_ = false; | 75 bool error_happened_ = false; |
| 74 bool sandbox_succeeded_ = false; | 76 bool sandbox_succeeded_ = false; |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 // Successful call through the Mojo service with response back. | 79 // Successful call through the Mojo service with response back. |
| 78 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, CallService) { | 80 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, CallService) { |
| 79 base::RunLoop run_loop; | 81 base::RunLoop run_loop; |
| 80 done_closure_ = run_loop.QuitClosure(); | 82 done_closure_ = run_loop.QuitClosure(); |
| 81 | 83 |
| 82 StartMojoService(false); | 84 StartMojoService(); |
| 83 | 85 |
| 84 mojo_client_->service()->DoSomething( | 86 mojo_client_->service()->DoSomething( |
| 85 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, | 87 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, |
| 86 base::Unretained(this))); | 88 base::Unretained(this))); |
| 87 | 89 |
| 88 run_loop.Run(); | 90 run_loop.Run(); |
| 89 EXPECT_TRUE(response_received_); | 91 EXPECT_TRUE(response_received_); |
| 90 EXPECT_FALSE(error_happened_); | 92 EXPECT_FALSE(error_happened_); |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Call the Mojo service but the utility process terminates before getting | 95 // Call the Mojo service but the utility process terminates before getting |
| 94 // the result back. | 96 // the result back. |
| 95 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, ConnectionError) { | 97 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, ConnectionError) { |
| 96 base::RunLoop run_loop; | 98 base::RunLoop run_loop; |
| 97 done_closure_ = run_loop.QuitClosure(); | 99 done_closure_ = run_loop.QuitClosure(); |
| 98 | 100 |
| 99 StartMojoService(false); | 101 StartMojoService(); |
| 100 | 102 |
| 101 mojo_client_->service()->DoTerminateProcess( | 103 mojo_client_->service()->DoTerminateProcess( |
| 102 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, | 104 base::Bind(&UtilityProcessMojoClientBrowserTest::OnResponseReceived, |
| 103 base::Unretained(this))); | 105 base::Unretained(this))); |
| 104 | 106 |
| 105 run_loop.Run(); | 107 run_loop.Run(); |
| 106 EXPECT_FALSE(response_received_); | 108 EXPECT_FALSE(response_received_); |
| 107 EXPECT_TRUE(error_happened_); | 109 EXPECT_TRUE(error_happened_); |
| 108 } | 110 } |
| 109 | 111 |
| 110 // Android doesn't support non-sandboxed utility processes. | 112 // Android doesn't support non-sandboxed utility processes. |
| 111 #if !defined(OS_ANDROID) | 113 #if !defined(OS_ANDROID) |
| 112 // Call a function that fails because the sandbox is still enabled. | 114 // Call a function that fails because the sandbox is still enabled. |
| 113 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxFailure) { | 115 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxFailure) { |
| 114 base::RunLoop run_loop; | 116 base::RunLoop run_loop; |
| 115 done_closure_ = run_loop.QuitClosure(); | 117 done_closure_ = run_loop.QuitClosure(); |
| 116 | 118 |
| 117 StartMojoService(false); | 119 StartMojoService(); |
| 118 | 120 |
| 119 mojo_client_->service()->CreateFolder( | 121 mojo_client_->service()->CreateFolder( |
| 120 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, | 122 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, |
| 121 base::Unretained(this))); | 123 base::Unretained(this))); |
| 122 | 124 |
| 123 run_loop.Run(); | 125 run_loop.Run(); |
| 124 EXPECT_TRUE(response_received_); | 126 EXPECT_TRUE(response_received_); |
| 125 // If the sandbox is disabled by the command line, this will succeed. | 127 // If the sandbox is disabled by the command line, this will succeed. |
| 126 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) | 128 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) |
| 127 EXPECT_FALSE(sandbox_succeeded_); | 129 EXPECT_FALSE(sandbox_succeeded_); |
| 128 EXPECT_FALSE(error_happened_); | 130 EXPECT_FALSE(error_happened_); |
| 129 } | 131 } |
| 130 | 132 |
| 131 // Call a function that succeeds only when the sandbox is disabled. | 133 // Call a function that succeeds only when the sandbox is disabled. |
| 132 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxSuccess) { | 134 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, SandboxSuccess) { |
| 133 base::RunLoop run_loop; | 135 base::RunLoop run_loop; |
| 134 done_closure_ = run_loop.QuitClosure(); | 136 done_closure_ = run_loop.QuitClosure(); |
| 135 | 137 |
| 136 StartMojoService(true); | 138 StartMojoService(UNSANDBOXED); |
| 137 | 139 |
| 138 mojo_client_->service()->CreateFolder( | 140 mojo_client_->service()->CreateFolder( |
| 139 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, | 141 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, |
| 140 base::Unretained(this))); | 142 base::Unretained(this))); |
| 141 | 143 |
| 142 run_loop.Run(); | 144 run_loop.Run(); |
| 143 EXPECT_TRUE(response_received_); | 145 EXPECT_TRUE(response_received_); |
| 144 EXPECT_TRUE(sandbox_succeeded_); | 146 EXPECT_TRUE(sandbox_succeeded_); |
| 145 EXPECT_FALSE(error_happened_); | 147 EXPECT_FALSE(error_happened_); |
| 146 } | 148 } |
| 147 #endif // !defined(OS_ANDROID) | 149 #endif // !defined(OS_ANDROID) |
| 148 | 150 |
| 149 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
| 150 // Call a function that succeeds with process elevation. Elevation is only | 152 // Call a function that succeeds with process elevation. Elevation is only |
| 151 // available on WIN, and is used to permit UAC prompts for operations that | 153 // available on WIN, and is used to permit UAC prompts for operations that |
| 152 // need user approval before proceeding. | 154 // need user approval before proceeding. |
| 153 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, ElevatedSuccess) { | 155 IN_PROC_BROWSER_TEST_F(UtilityProcessMojoClientBrowserTest, ElevatedSuccess) { |
| 154 base::RunLoop run_loop; | 156 base::RunLoop run_loop; |
| 155 done_closure_ = run_loop.QuitClosure(); | 157 done_closure_ = run_loop.QuitClosure(); |
| 156 | 158 |
| 157 bool elevated_utility_process = true; | 159 StartMojoService(ELEVATED); |
| 158 StartMojoService(true, elevated_utility_process); | |
| 159 | 160 |
| 160 mojo_client_->service()->CreateFolder( | 161 mojo_client_->service()->CreateFolder( |
| 161 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, | 162 base::Bind(&UtilityProcessMojoClientBrowserTest::OnCreateFolderFinished, |
| 162 base::Unretained(this))); | 163 base::Unretained(this))); |
| 163 | 164 |
| 164 run_loop.Run(); | 165 run_loop.Run(); |
| 165 EXPECT_TRUE(response_received_); | 166 EXPECT_TRUE(response_received_); |
| 166 EXPECT_TRUE(sandbox_succeeded_); | 167 EXPECT_TRUE(sandbox_succeeded_); |
| 167 EXPECT_FALSE(error_happened_); | 168 EXPECT_FALSE(error_happened_); |
| 168 } | 169 } |
| 169 #endif // defined(OS_WIN) | 170 #endif // defined(OS_WIN) |
| 170 | 171 |
| 171 } // namespace content | 172 } // namespace content |
| OLD | NEW |