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