amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 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 "base/bind.h" |
| 6 | #include "base/run_loop.h" |
| 7 | #include "base/strings/utf_string_conversions.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 8 | #include "base/task/post_task.h" |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | #include "content/browser/utility_process_host.h" |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 11 | #include "content/public/browser/browser_child_process_observer.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 12 | #include "content/public/browser/browser_task_traits.h" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 13 | #include "content/public/browser/browser_thread.h" |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 14 | #include "content/public/browser/child_process_data.h" |
| 15 | #include "content/public/browser/child_process_termination_info.h" |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 16 | #include "content/public/common/bind_interface_helpers.h" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 17 | #include "content/public/test/content_browser_test.h" |
| 18 | #include "content/public/test/content_browser_test_utils.h" |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 19 | #include "content/public/test/test_service.mojom.h" |
Julie Jeongeun Kim | 29cb9cb7 | 2019-09-17 02:58:35 | [diff] [blame] | 20 | #include "mojo/public/cpp/bindings/remote.h" |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 21 | |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 22 | #if defined(OS_MACOSX) || defined(OS_LINUX) |
| 23 | #include <sys/wait.h> |
| 24 | #endif |
| 25 | |
| 26 | #if defined(OS_WIN) |
| 27 | #include <windows.h> |
| 28 | #endif // OS_WIN |
| 29 | |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 30 | namespace content { |
| 31 | |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | const char kTestProcessName[] = "test_process"; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | class UtilityProcessHostBrowserTest : public BrowserChildProcessObserver, |
| 39 | public ContentBrowserTest { |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 40 | public: |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 41 | void RunUtilityProcess(bool elevated, bool crash) { |
| 42 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 43 | BrowserChildProcessObserver::Add(this); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 44 | has_crashed = false; |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 45 | base::RunLoop run_loop; |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 46 | done_closure_ = |
| 47 | base::BindOnce(&UtilityProcessHostBrowserTest::DoneRunning, |
| 48 | base::Unretained(this), run_loop.QuitClosure(), crash); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 49 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 50 | FROM_HERE, {BrowserThread::IO}, |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 51 | base::BindOnce( |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 52 | &UtilityProcessHostBrowserTest::RunUtilityProcessOnIOThread, |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 53 | base::Unretained(this), elevated, crash)); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 54 | run_loop.Run(); |
| 55 | } |
| 56 | |
| 57 | protected: |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 58 | void DoneRunning(base::OnceClosure quit_closure, bool expect_crashed) { |
| 59 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 60 | BrowserChildProcessObserver::Remove(this); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 61 | EXPECT_EQ(expect_crashed, has_crashed); |
| 62 | std::move(quit_closure).Run(); |
| 63 | } |
| 64 | |
| 65 | void RunUtilityProcessOnIOThread(bool elevated, bool crash) { |
| 66 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Clark DuVall | a6a3233f | 2019-06-12 18:56:02 | [diff] [blame] | 67 | UtilityProcessHost* host = new UtilityProcessHost(); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 68 | host->SetName(base::ASCIIToUTF16("TestProcess")); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 69 | host->SetMetricsName(kTestProcessName); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 70 | #if defined(OS_WIN) |
| 71 | if (elevated) |
Jay Civelli | 0c7f3bfb | 2017-11-07 16:41:22 | [diff] [blame] | 72 | host->SetSandboxType(service_manager::SandboxType:: |
| 73 | SANDBOX_TYPE_NO_SANDBOX_AND_ELEVATED_PRIVILEGES); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 74 | #endif |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 75 | EXPECT_TRUE(host->Start()); |
| 76 | |
Julie Jeongeun Kim | 29cb9cb7 | 2019-09-17 02:58:35 | [diff] [blame] | 77 | BindInterface(host, service_.BindNewPipeAndPassReceiver()); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 78 | if (crash) { |
| 79 | service_->DoCrashImmediately( |
| 80 | base::BindOnce(&UtilityProcessHostBrowserTest::OnSomethingOnIOThread, |
| 81 | base::Unretained(this), crash)); |
| 82 | } else { |
| 83 | service_->DoSomething( |
| 84 | base::BindOnce(&UtilityProcessHostBrowserTest::OnSomethingOnIOThread, |
| 85 | base::Unretained(this), crash)); |
| 86 | } |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 87 | } |
| 88 | |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 89 | void ResetServiceOnIOThread() { |
| 90 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 91 | service_.reset(); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void OnSomethingOnIOThread(bool expect_crash) { |
| 95 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 96 | // If service crashes then this never gets called. |
| 97 | ASSERT_EQ(false, expect_crash); |
| 98 | ResetServiceOnIOThread(); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 99 | base::PostTask(FROM_HERE, {BrowserThread::UI}, std::move(done_closure_)); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 100 | } |
| 101 | |
Julie Jeongeun Kim | 29cb9cb7 | 2019-09-17 02:58:35 | [diff] [blame] | 102 | mojo::Remote<mojom::TestService> service_; |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 103 | base::OnceClosure done_closure_; |
| 104 | |
| 105 | // Access on UI thread. |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 106 | bool has_crashed; |
| 107 | |
| 108 | private: |
| 109 | // content::BrowserChildProcessObserver implementation: |
| 110 | void BrowserChildProcessKilled( |
| 111 | const ChildProcessData& data, |
| 112 | const ChildProcessTerminationInfo& info) override { |
| 113 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 | #if defined(OS_ANDROID) |
| 115 | // Android does not send crash notifications but sends kills. See comment in |
| 116 | // browser_child_process_observer.h. |
| 117 | BrowserChildProcessCrashed(data, info); |
| 118 | #else |
| 119 | FAIL() << "Killed notifications should only happen on Android."; |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | void BrowserChildProcessCrashed( |
| 124 | const ChildProcessData& data, |
| 125 | const ChildProcessTerminationInfo& info) override { |
| 126 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 127 | #if defined(OS_WIN) |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 128 | EXPECT_EQ(EXCEPTION_BREAKPOINT, DWORD{info.exit_code}); |
| 129 | #elif defined(OS_MACOSX) || defined(OS_LINUX) |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 130 | EXPECT_TRUE(WIFSIGNALED(info.exit_code)); |
| 131 | EXPECT_EQ(SIGTRAP, WTERMSIG(info.exit_code)); |
| 132 | #endif |
| 133 | EXPECT_EQ(kTestProcessName, data.metrics_name); |
| 134 | EXPECT_EQ(false, has_crashed); |
| 135 | has_crashed = true; |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 136 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 137 | FROM_HERE, {BrowserThread::IO}, |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 138 | base::BindOnce(&UtilityProcessHostBrowserTest::ResetServiceOnIOThread, |
| 139 | base::Unretained(this))); |
| 140 | std::move(done_closure_).Run(); |
| 141 | } |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 142 | }; |
| 143 | |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 144 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostBrowserTest, LaunchProcess) { |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 145 | RunUtilityProcess(false, false); |
| 146 | } |
| 147 | |
Will Harris | c67f8bc | 2018-10-19 21:04:37 | [diff] [blame] | 148 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostBrowserTest, LaunchProcessAndCrash) { |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 149 | RunUtilityProcess(false, true); |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 150 | } |
| 151 | |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 152 | #if defined(OS_WIN) |
Vasilii Sukhanov | 96369af | 2019-01-31 13:38:33 | [diff] [blame] | 153 | // Times out. crbug.com/927298. |
| 154 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostBrowserTest, |
| 155 | DISABLED_LaunchElevatedProcess) { |
Will Harris | ffe74663 | 2018-06-07 05:16:29 | [diff] [blame] | 156 | RunUtilityProcess(true, false); |
| 157 | } |
| 158 | |
| 159 | // Disabled because currently this causes a WER dialog to appear. |
| 160 | IN_PROC_BROWSER_TEST_F(UtilityProcessHostBrowserTest, |
| 161 | LaunchElevatedProcessAndCrash_DISABLED) { |
| 162 | RunUtilityProcess(true, true); |
amistry | b613891 | 2016-05-31 01:54:12 | [diff] [blame] | 163 | } |
| 164 | #endif |
| 165 | |
amistry | dc1c9106 | 2016-05-12 00:59:52 | [diff] [blame] | 166 | } // namespace content |