xref: /aosp_15_r20/external/libchrome/base/process/process_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <utility>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include "base/at_exit.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/process/kill.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/test/multiprocess_test.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/test/test_timeouts.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_local.h"
15*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
16*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
17*635a8641SAndroid Build Coastguard Worker #include "testing/multiprocess_func_list.h"
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker namespace {
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
22*635a8641SAndroid Build Coastguard Worker const int kExpectedStillRunningExitCode = 0x102;
23*635a8641SAndroid Build Coastguard Worker #else
24*635a8641SAndroid Build Coastguard Worker const int kExpectedStillRunningExitCode = 0;
25*635a8641SAndroid Build Coastguard Worker #endif
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
28*635a8641SAndroid Build Coastguard Worker // Fake port provider that returns the calling process's
29*635a8641SAndroid Build Coastguard Worker // task port, ignoring its argument.
30*635a8641SAndroid Build Coastguard Worker class FakePortProvider : public base::PortProvider {
TaskForPid(base::ProcessHandle process) const31*635a8641SAndroid Build Coastguard Worker   mach_port_t TaskForPid(base::ProcessHandle process) const override {
32*635a8641SAndroid Build Coastguard Worker     return mach_task_self();
33*635a8641SAndroid Build Coastguard Worker   }
34*635a8641SAndroid Build Coastguard Worker };
35*635a8641SAndroid Build Coastguard Worker #endif
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker }  // namespace
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker namespace base {
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker class ProcessTest : public MultiProcessTest {
42*635a8641SAndroid Build Coastguard Worker };
43*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,Create)44*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, Create) {
45*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("SimpleChildProcess"));
46*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
47*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(process.is_current());
48*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(process.Pid(), kNullProcessId);
49*635a8641SAndroid Build Coastguard Worker   process.Close();
50*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(process.IsValid());
51*635a8641SAndroid Build Coastguard Worker }
52*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,CreateCurrent)53*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, CreateCurrent) {
54*635a8641SAndroid Build Coastguard Worker   Process process = Process::Current();
55*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
56*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.is_current());
57*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(process.Pid(), kNullProcessId);
58*635a8641SAndroid Build Coastguard Worker   process.Close();
59*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(process.IsValid());
60*635a8641SAndroid Build Coastguard Worker }
61*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,Move)62*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, Move) {
63*635a8641SAndroid Build Coastguard Worker   Process process1(SpawnChild("SimpleChildProcess"));
64*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process1.IsValid());
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker   Process process2;
67*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process2.IsValid());
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker   process2 = std::move(process1);
70*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process2.IsValid());
71*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process1.IsValid());
72*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process2.is_current());
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker   Process process3 = Process::Current();
75*635a8641SAndroid Build Coastguard Worker   process2 = std::move(process3);
76*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process2.is_current());
77*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process2.IsValid());
78*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process3.IsValid());
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,Duplicate)81*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, Duplicate) {
82*635a8641SAndroid Build Coastguard Worker   Process process1(SpawnChild("SimpleChildProcess"));
83*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker   Process process2 = process1.Duplicate();
86*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
87*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
88*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(process1.Pid(), process2.Pid());
89*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process1.is_current());
90*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process2.is_current());
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   process1.Close();
93*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
94*635a8641SAndroid Build Coastguard Worker }
95*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,DuplicateCurrent)96*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, DuplicateCurrent) {
97*635a8641SAndroid Build Coastguard Worker   Process process1 = Process::Current();
98*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   Process process2 = process1.Duplicate();
101*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
102*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
103*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(process1.Pid(), process2.Pid());
104*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process1.is_current());
105*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process2.is_current());
106*635a8641SAndroid Build Coastguard Worker 
107*635a8641SAndroid Build Coastguard Worker   process1.Close();
108*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
109*635a8641SAndroid Build Coastguard Worker }
110*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,DeprecatedGetProcessFromHandle)111*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) {
112*635a8641SAndroid Build Coastguard Worker   Process process1(SpawnChild("SimpleChildProcess"));
113*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
114*635a8641SAndroid Build Coastguard Worker 
115*635a8641SAndroid Build Coastguard Worker   Process process2 = Process::DeprecatedGetProcessFromHandle(process1.Handle());
116*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process1.IsValid());
117*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
118*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(process1.Pid(), process2.Pid());
119*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process1.is_current());
120*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process2.is_current());
121*635a8641SAndroid Build Coastguard Worker 
122*635a8641SAndroid Build Coastguard Worker   process1.Close();
123*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process2.IsValid());
124*635a8641SAndroid Build Coastguard Worker }
125*635a8641SAndroid Build Coastguard Worker 
MULTIPROCESS_TEST_MAIN(SleepyChildProcess)126*635a8641SAndroid Build Coastguard Worker MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
127*635a8641SAndroid Build Coastguard Worker   PlatformThread::Sleep(TestTimeouts::action_max_timeout());
128*635a8641SAndroid Build Coastguard Worker   return 0;
129*635a8641SAndroid Build Coastguard Worker }
130*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,Terminate)131*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, Terminate) {
132*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("SleepyChildProcess"));
133*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
134*635a8641SAndroid Build Coastguard Worker 
135*635a8641SAndroid Build Coastguard Worker   const int kDummyExitCode = 42;
136*635a8641SAndroid Build Coastguard Worker   int exit_code = kDummyExitCode;
137*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
138*635a8641SAndroid Build Coastguard Worker             GetTerminationStatus(process.Handle(), &exit_code));
139*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker   exit_code = kDummyExitCode;
142*635a8641SAndroid Build Coastguard Worker   int kExpectedExitCode = 250;
143*635a8641SAndroid Build Coastguard Worker   process.Terminate(kExpectedExitCode, false);
144*635a8641SAndroid Build Coastguard Worker   process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
145*635a8641SAndroid Build Coastguard Worker                                  &exit_code);
146*635a8641SAndroid Build Coastguard Worker 
147*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
148*635a8641SAndroid Build Coastguard Worker             GetTerminationStatus(process.Handle(), &exit_code));
149*635a8641SAndroid Build Coastguard Worker #if !defined(OS_POSIX) && !defined(OS_FUCHSIA)
150*635a8641SAndroid Build Coastguard Worker   // The POSIX & Fuchsia implementations actually ignore the exit_code.
151*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(kExpectedExitCode, exit_code);
152*635a8641SAndroid Build Coastguard Worker #endif
153*635a8641SAndroid Build Coastguard Worker }
154*635a8641SAndroid Build Coastguard Worker 
AtExitHandler(void *)155*635a8641SAndroid Build Coastguard Worker void AtExitHandler(void*) {
156*635a8641SAndroid Build Coastguard Worker   // At-exit handler should not be called at
157*635a8641SAndroid Build Coastguard Worker   // Process::TerminateCurrentProcessImmediately.
158*635a8641SAndroid Build Coastguard Worker   DCHECK(false);
159*635a8641SAndroid Build Coastguard Worker }
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker class ThreadLocalObject {
~ThreadLocalObject()162*635a8641SAndroid Build Coastguard Worker   ~ThreadLocalObject() {
163*635a8641SAndroid Build Coastguard Worker     // Thread-local storage should not be destructed at
164*635a8641SAndroid Build Coastguard Worker     // Process::TerminateCurrentProcessImmediately.
165*635a8641SAndroid Build Coastguard Worker     DCHECK(false);
166*635a8641SAndroid Build Coastguard Worker   }
167*635a8641SAndroid Build Coastguard Worker };
168*635a8641SAndroid Build Coastguard Worker 
MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode0)169*635a8641SAndroid Build Coastguard Worker MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode0) {
170*635a8641SAndroid Build Coastguard Worker   base::ThreadLocalPointer<ThreadLocalObject> object;
171*635a8641SAndroid Build Coastguard Worker   base::AtExitManager::RegisterCallback(&AtExitHandler, nullptr);
172*635a8641SAndroid Build Coastguard Worker   Process::TerminateCurrentProcessImmediately(0);
173*635a8641SAndroid Build Coastguard Worker }
174*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,TerminateCurrentProcessImmediatelyWithZeroExitCode)175*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, TerminateCurrentProcessImmediatelyWithZeroExitCode) {
176*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("TerminateCurrentProcessImmediatelyWithCode0"));
177*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
178*635a8641SAndroid Build Coastguard Worker   int exit_code = 42;
179*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
180*635a8641SAndroid Build Coastguard Worker                                              &exit_code));
181*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0, exit_code);
182*635a8641SAndroid Build Coastguard Worker }
183*635a8641SAndroid Build Coastguard Worker 
MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode250)184*635a8641SAndroid Build Coastguard Worker MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode250) {
185*635a8641SAndroid Build Coastguard Worker   Process::TerminateCurrentProcessImmediately(250);
186*635a8641SAndroid Build Coastguard Worker }
187*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,TerminateCurrentProcessImmediatelyWithNonZeroExitCode)188*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, TerminateCurrentProcessImmediatelyWithNonZeroExitCode) {
189*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("TerminateCurrentProcessImmediatelyWithCode250"));
190*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
191*635a8641SAndroid Build Coastguard Worker   int exit_code = 42;
192*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
193*635a8641SAndroid Build Coastguard Worker                                              &exit_code));
194*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(250, exit_code);
195*635a8641SAndroid Build Coastguard Worker }
196*635a8641SAndroid Build Coastguard Worker 
MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess)197*635a8641SAndroid Build Coastguard Worker MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
198*635a8641SAndroid Build Coastguard Worker   PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
199*635a8641SAndroid Build Coastguard Worker   return 0;
200*635a8641SAndroid Build Coastguard Worker }
201*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,WaitForExit)202*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, WaitForExit) {
203*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("FastSleepyChildProcess"));
204*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
205*635a8641SAndroid Build Coastguard Worker 
206*635a8641SAndroid Build Coastguard Worker   const int kDummyExitCode = 42;
207*635a8641SAndroid Build Coastguard Worker   int exit_code = kDummyExitCode;
208*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.WaitForExit(&exit_code));
209*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0, exit_code);
210*635a8641SAndroid Build Coastguard Worker }
211*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,WaitForExitWithTimeout)212*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, WaitForExitWithTimeout) {
213*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("SleepyChildProcess"));
214*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(process.IsValid());
215*635a8641SAndroid Build Coastguard Worker 
216*635a8641SAndroid Build Coastguard Worker   const int kDummyExitCode = 42;
217*635a8641SAndroid Build Coastguard Worker   int exit_code = kDummyExitCode;
218*635a8641SAndroid Build Coastguard Worker   TimeDelta timeout = TestTimeouts::tiny_timeout();
219*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
220*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(kDummyExitCode, exit_code);
221*635a8641SAndroid Build Coastguard Worker 
222*635a8641SAndroid Build Coastguard Worker   process.Terminate(kDummyExitCode, false);
223*635a8641SAndroid Build Coastguard Worker }
224*635a8641SAndroid Build Coastguard Worker 
225*635a8641SAndroid Build Coastguard Worker // Ensure that the priority of a process is restored correctly after
226*635a8641SAndroid Build Coastguard Worker // backgrounding and restoring.
227*635a8641SAndroid Build Coastguard Worker // Note: a platform may not be willing or able to lower the priority of
228*635a8641SAndroid Build Coastguard Worker // a process. The calls to SetProcessBackground should be noops then.
TEST_F(ProcessTest,SetProcessBackgrounded)229*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, SetProcessBackgrounded) {
230*635a8641SAndroid Build Coastguard Worker   if (!Process::CanBackgroundProcesses())
231*635a8641SAndroid Build Coastguard Worker     return;
232*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("SimpleChildProcess"));
233*635a8641SAndroid Build Coastguard Worker   int old_priority = process.GetPriority();
234*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
235*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(true));
236*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.IsProcessBackgrounded());
237*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(false));
238*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.IsProcessBackgrounded());
239*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
240*635a8641SAndroid Build Coastguard Worker   // On the Mac, backgrounding a process requires a port to that process.
241*635a8641SAndroid Build Coastguard Worker   // In the browser it's available through the MachBroker class, which is not
242*635a8641SAndroid Build Coastguard Worker   // part of base. Additionally, there is an indefinite amount of time between
243*635a8641SAndroid Build Coastguard Worker   // spawning a process and receiving its port. Because this test just checks
244*635a8641SAndroid Build Coastguard Worker   // the ability to background/foreground a process, we can use the current
245*635a8641SAndroid Build Coastguard Worker   // process's port instead.
246*635a8641SAndroid Build Coastguard Worker   FakePortProvider provider;
247*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true));
248*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.IsProcessBackgrounded(&provider));
249*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false));
250*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.IsProcessBackgrounded(&provider));
251*635a8641SAndroid Build Coastguard Worker 
252*635a8641SAndroid Build Coastguard Worker #else
253*635a8641SAndroid Build Coastguard Worker   process.SetProcessBackgrounded(true);
254*635a8641SAndroid Build Coastguard Worker   process.SetProcessBackgrounded(false);
255*635a8641SAndroid Build Coastguard Worker #endif
256*635a8641SAndroid Build Coastguard Worker   int new_priority = process.GetPriority();
257*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(old_priority, new_priority);
258*635a8641SAndroid Build Coastguard Worker }
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker // Same as SetProcessBackgrounded but to this very process. It uses
261*635a8641SAndroid Build Coastguard Worker // a different code path at least for Windows.
TEST_F(ProcessTest,SetProcessBackgroundedSelf)262*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, SetProcessBackgroundedSelf) {
263*635a8641SAndroid Build Coastguard Worker   if (!Process::CanBackgroundProcesses())
264*635a8641SAndroid Build Coastguard Worker     return;
265*635a8641SAndroid Build Coastguard Worker   Process process = Process::Current();
266*635a8641SAndroid Build Coastguard Worker   int old_priority = process.GetPriority();
267*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
268*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(true));
269*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.IsProcessBackgrounded());
270*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(false));
271*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.IsProcessBackgrounded());
272*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX)
273*635a8641SAndroid Build Coastguard Worker   FakePortProvider provider;
274*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true));
275*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.IsProcessBackgrounded(&provider));
276*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false));
277*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.IsProcessBackgrounded(&provider));
278*635a8641SAndroid Build Coastguard Worker #else
279*635a8641SAndroid Build Coastguard Worker   process.SetProcessBackgrounded(true);
280*635a8641SAndroid Build Coastguard Worker   process.SetProcessBackgrounded(false);
281*635a8641SAndroid Build Coastguard Worker #endif
282*635a8641SAndroid Build Coastguard Worker   int new_priority = process.GetPriority();
283*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(old_priority, new_priority);
284*635a8641SAndroid Build Coastguard Worker }
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker // Consumers can use WaitForExitWithTimeout(base::TimeDelta(), nullptr) to check
287*635a8641SAndroid Build Coastguard Worker // whether the process is still running. This may not be safe because of the
288*635a8641SAndroid Build Coastguard Worker // potential reusing of the process id. So we won't export Process::IsRunning()
289*635a8641SAndroid Build Coastguard Worker // on all platforms. But for the controllable scenario in the test cases, the
290*635a8641SAndroid Build Coastguard Worker // behavior should be guaranteed.
TEST_F(ProcessTest,CurrentProcessIsRunning)291*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, CurrentProcessIsRunning) {
292*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(Process::Current().WaitForExitWithTimeout(
293*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(), nullptr));
294*635a8641SAndroid Build Coastguard Worker }
295*635a8641SAndroid Build Coastguard Worker 
296*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX)
297*635a8641SAndroid Build Coastguard Worker // On Mac OSX, we can detect whether a non-child process is running.
TEST_F(ProcessTest,PredefinedProcessIsRunning)298*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, PredefinedProcessIsRunning) {
299*635a8641SAndroid Build Coastguard Worker   // Process 1 is the /sbin/launchd, it should be always running.
300*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(Process::Open(1).WaitForExitWithTimeout(
301*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(), nullptr));
302*635a8641SAndroid Build Coastguard Worker }
303*635a8641SAndroid Build Coastguard Worker #endif
304*635a8641SAndroid Build Coastguard Worker 
TEST_F(ProcessTest,ChildProcessIsRunning)305*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, ChildProcessIsRunning) {
306*635a8641SAndroid Build Coastguard Worker   Process process(SpawnChild("SleepyChildProcess"));
307*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(process.WaitForExitWithTimeout(
308*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(), nullptr));
309*635a8641SAndroid Build Coastguard Worker   process.Terminate(0, true);
310*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(process.WaitForExitWithTimeout(
311*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(), nullptr));
312*635a8641SAndroid Build Coastguard Worker }
313*635a8641SAndroid Build Coastguard Worker 
314*635a8641SAndroid Build Coastguard Worker #if defined(OS_CHROMEOS)
315*635a8641SAndroid Build Coastguard Worker 
316*635a8641SAndroid Build Coastguard Worker // Tests that the function IsProcessBackgroundedCGroup() can parse the contents
317*635a8641SAndroid Build Coastguard Worker // of the /proc/<pid>/cgroup file successfully.
TEST_F(ProcessTest,TestIsProcessBackgroundedCGroup)318*635a8641SAndroid Build Coastguard Worker TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) {
319*635a8641SAndroid Build Coastguard Worker   const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n";
320*635a8641SAndroid Build Coastguard Worker   const char kBackgrounded[] =
321*635a8641SAndroid Build Coastguard Worker       "2:freezer:/chrome_renderers/to_be_frozen\n"
322*635a8641SAndroid Build Coastguard Worker       "1:cpu:/chrome_renderers/background\n";
323*635a8641SAndroid Build Coastguard Worker 
324*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded));
325*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded));
326*635a8641SAndroid Build Coastguard Worker }
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_CHROMEOS)
329*635a8641SAndroid Build Coastguard Worker 
330*635a8641SAndroid Build Coastguard Worker }  // namespace base
331