Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: components/browser_watcher/exit_code_watcher_win_unittest.cc

Issue 807513003: Switch ExitCodeWatcher to base::Process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/browser_watcher/exit_code_watcher_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 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 "components/browser_watcher/exit_code_watcher_win.h" 5 #include "components/browser_watcher/exit_code_watcher_win.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/process/kill.h" 8 #include "base/process/process.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
13 #include "base/test/multiprocess_test.h" 13 #include "base/test/multiprocess_test.h"
14 #include "base/test/test_reg_util_win.h" 14 #include "base/test/test_reg_util_win.h"
15 #include "base/threading/platform_thread.h" 15 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "base/win/scoped_handle.h" 17 #include "base/win/scoped_handle.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/multiprocess_func_list.h" 19 #include "testing/multiprocess_func_list.h"
20 20
21 namespace browser_watcher { 21 namespace browser_watcher {
22 22
23 namespace { 23 namespace {
24 24
25 const base::char16 kRegistryPath[] = L"Software\\BrowserWatcherTest"; 25 const base::char16 kRegistryPath[] = L"Software\\BrowserWatcherTest";
26 26
27 MULTIPROCESS_TEST_MAIN(Sleeper) { 27 MULTIPROCESS_TEST_MAIN(Sleeper) {
28 // Sleep forever - the test harness will kill this process to give it an 28 // Sleep forever - the test harness will kill this process to give it an
29 // exit code. 29 // exit code.
30 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(INFINITE)); 30 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(INFINITE));
31 return 1; 31 return 1;
32 } 32 }
33 33
34 class ScopedSleeperProcess { 34 class ScopedSleeperProcess {
35 public: 35 public:
36 ScopedSleeperProcess() : 36 ScopedSleeperProcess() : is_killed_(false) {
37 process_(base::kNullProcessHandle),
38 process_id_(base::kNullProcessId),
39 is_killed_(false) {
40 } 37 }
41 38
42 ~ScopedSleeperProcess() { 39 ~ScopedSleeperProcess() {
43 if (process_ != base::kNullProcessHandle) { 40 if (process_.IsValid()) {
44 base::KillProcess(process_, -1, true); 41 process_.Terminate(-1);
45 base::CloseProcessHandle(process_); 42 int exit_code = 0;
43 EXPECT_TRUE(process_.WaitForExit(&exit_code));
46 } 44 }
47 } 45 }
48 46
49 void Launch() { 47 void Launch() {
50 ASSERT_EQ(base::kNullProcessHandle, process_); 48 ASSERT_FALSE(process_.IsValid());
51 49
52 base::CommandLine cmd_line(base::GetMultiProcessTestChildBaseCommandLine()); 50 base::CommandLine cmd_line(base::GetMultiProcessTestChildBaseCommandLine());
53 base::LaunchOptions options; 51 base::LaunchOptions options;
54 options.start_hidden = true; 52 options.start_hidden = true;
55 process_ = base::SpawnMultiProcessTestChild("Sleeper", cmd_line, options); 53 process_ = base::Process(
56 process_id_ = base::GetProcId(process_); 54 base::SpawnMultiProcessTestChild("Sleeper", cmd_line, options));
57 ASSERT_NE(base::kNullProcessHandle, process_); 55 ASSERT_TRUE(process_.IsValid());
58 } 56 }
59 57
60 void Kill(int exit_code, bool wait) { 58 void Kill(int exit_code, bool wait) {
61 ASSERT_NE(process_, base::kNullProcessHandle); 59 ASSERT_TRUE(process_.IsValid());
62 ASSERT_FALSE(is_killed_); 60 ASSERT_FALSE(is_killed_);
63 ASSERT_TRUE(base::KillProcess(process_, exit_code, wait)); 61 process_.Terminate(exit_code);
62 int seen_exit_code = 0;
63 EXPECT_TRUE(process_.WaitForExit(&seen_exit_code));
64 EXPECT_EQ(exit_code, seen_exit_code);
64 is_killed_ = true; 65 is_killed_ = true;
65 } 66 }
66 67
67 void GetNewHandle(base::ProcessHandle* output) { 68 void GetNewHandle(base::ProcessHandle* output) {
68 ASSERT_NE(process_, base::kNullProcessHandle); 69 ASSERT_TRUE(process_.IsValid());
69 70
70 ASSERT_TRUE(DuplicateHandle(::GetCurrentProcess(), 71 ASSERT_TRUE(DuplicateHandle(::GetCurrentProcess(),
71 process_, 72 process_.Handle(),
72 ::GetCurrentProcess(), 73 ::GetCurrentProcess(),
73 output, 74 output,
74 0, 75 0,
75 FALSE, 76 FALSE,
76 DUPLICATE_SAME_ACCESS)); 77 DUPLICATE_SAME_ACCESS));
77 } 78 }
78 79
79 base::ProcessHandle process() const { return process_; } 80 const base::Process& process() const { return process_; }
80 base::ProcessId process_id() const { return process_id_; }
81 81
82 private: 82 private:
83 base::ProcessHandle process_; 83 base::Process process_;
84 base::ProcessId process_id_;
85 bool is_killed_; 84 bool is_killed_;
86 }; 85 };
87 86
88 class BrowserWatcherTest : public testing::Test { 87 class BrowserWatcherTest : public testing::Test {
89 public: 88 public:
90 typedef testing::Test Super; 89 typedef testing::Test Super;
91 90
92 static const int kExitCode = 0xCAFEBABE; 91 static const int kExitCode = 0xCAFEBABE;
93 92
94 BrowserWatcherTest() : 93 BrowserWatcherTest() :
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 182
184 // Open a handle to this process with sufficient access for the watcher. 183 // Open a handle to this process with sufficient access for the watcher.
185 ASSERT_NO_FATAL_FAILURE( 184 ASSERT_NO_FATAL_FAILURE(
186 OpenSelfWithAccess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION)); 185 OpenSelfWithAccess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION));
187 186
188 // A process handle with sufficient access should succeed init. 187 // A process handle with sufficient access should succeed init.
189 cmd_line_.AppendSwitchASCII(ExitCodeWatcher::kParenthHandleSwitch, 188 cmd_line_.AppendSwitchASCII(ExitCodeWatcher::kParenthHandleSwitch,
190 base::StringPrintf("%d", process_)); 189 base::StringPrintf("%d", process_));
191 EXPECT_TRUE(watcher.ParseArguments(cmd_line_)); 190 EXPECT_TRUE(watcher.ParseArguments(cmd_line_));
192 191
193 ASSERT_EQ(process_, watcher.process()); 192 ASSERT_EQ(process_, watcher.process().Handle());
194 193
195 // The watcher takes ownership of the handle, make sure it's not 194 // The watcher takes ownership of the handle, make sure it's not
196 // double-closed. 195 // double-closed.
197 process_ = base::kNullProcessHandle; 196 process_ = base::kNullProcessHandle;
198 } 197 }
199 198
200 TEST_F(BrowserWatcherTest, ExitCodeWatcherOnExitedProcess) { 199 TEST_F(BrowserWatcherTest, ExitCodeWatcherOnExitedProcess) {
201 ScopedSleeperProcess sleeper; 200 ScopedSleeperProcess sleeper;
202 ASSERT_NO_FATAL_FAILURE(sleeper.Launch()); 201 ASSERT_NO_FATAL_FAILURE(sleeper.Launch());
203 202
204 // Create a new handle to the sleeper process. This handle will leak in 203 // Create a new handle to the sleeper process. This handle will leak in
205 // the case this test fails. A ScopedHandle cannot be used here, as the 204 // the case this test fails. A ScopedHandle cannot be used here, as the
206 // ownership would momentarily be held by two of them, which is disallowed. 205 // ownership would momentarily be held by two of them, which is disallowed.
207 base::ProcessHandle sleeper_handle; 206 base::ProcessHandle sleeper_handle = base::kNullProcessHandle;
208 sleeper.GetNewHandle(&sleeper_handle); 207 sleeper.GetNewHandle(&sleeper_handle);
209 208
210 ExitCodeWatcher watcher(kRegistryPath); 209 ExitCodeWatcher watcher(kRegistryPath);
211 210
212 cmd_line_.AppendSwitchASCII(ExitCodeWatcher::kParenthHandleSwitch, 211 cmd_line_.AppendSwitchASCII(ExitCodeWatcher::kParenthHandleSwitch,
213 base::StringPrintf("%d", sleeper_handle)); 212 base::StringPrintf("%d", sleeper_handle));
214 EXPECT_TRUE(watcher.ParseArguments(cmd_line_)); 213 EXPECT_TRUE(watcher.ParseArguments(cmd_line_));
215 ASSERT_EQ(sleeper_handle, watcher.process()); 214 ASSERT_EQ(sleeper_handle, watcher.process().Handle());
216 215
217 // Verify that the watcher wrote a sentinel for the process. 216 // Verify that the watcher wrote a sentinel for the process.
218 VerifyWroteExitCode(sleeper.process_id(), STILL_ACTIVE); 217 VerifyWroteExitCode(sleeper.process().pid(), STILL_ACTIVE);
219 218
220 // Kill the sleeper, and make sure it's exited before we continue. 219 // Kill the sleeper, and make sure it's exited before we continue.
221 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true)); 220 ASSERT_NO_FATAL_FAILURE(sleeper.Kill(kExitCode, true));
222 221
223 watcher.WaitForExit(); 222 watcher.WaitForExit();
224 223
225 VerifyWroteExitCode(sleeper.process_id(), kExitCode); 224 VerifyWroteExitCode(sleeper.process().pid(), kExitCode);
226 } 225 }
227 226
228 } // namespace browser_watcher 227 } // namespace browser_watcher
OLDNEW
« no previous file with comments | « components/browser_watcher/exit_code_watcher_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698