1 // Copyright 2014 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.
5 #include "base/process/process.h"
7 #include "base/process/kill.h"
8 #include "base/test/multiprocess_test.h"
9 #include "base/test/test_timeouts.h"
10 #include "base/threading/platform_thread.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/multiprocess_func_list.h"
17 const int kExpectedStillRunningExitCode
= 0x102;
19 const int kExpectedStillRunningExitCode
= 0;
26 class ProcessTest
: public MultiProcessTest
{
29 TEST_F(ProcessTest
, Create
) {
30 Process
process(SpawnChild("SimpleChildProcess"));
31 ASSERT_TRUE(process
.IsValid());
32 ASSERT_FALSE(process
.is_current());
34 ASSERT_FALSE(process
.IsValid());
37 TEST_F(ProcessTest
, CreateCurrent
) {
38 Process process
= Process::Current();
39 ASSERT_TRUE(process
.IsValid());
40 ASSERT_TRUE(process
.is_current());
42 ASSERT_FALSE(process
.IsValid());
45 TEST_F(ProcessTest
, Move
) {
46 Process
process1(SpawnChild("SimpleChildProcess"));
47 EXPECT_TRUE(process1
.IsValid());
50 EXPECT_FALSE(process2
.IsValid());
52 process2
= process1
.Pass();
53 EXPECT_TRUE(process2
.IsValid());
54 EXPECT_FALSE(process1
.IsValid());
55 EXPECT_FALSE(process2
.is_current());
57 Process process3
= Process::Current();
58 process2
= process3
.Pass();
59 EXPECT_TRUE(process2
.is_current());
60 EXPECT_TRUE(process2
.IsValid());
61 EXPECT_FALSE(process3
.IsValid());
64 TEST_F(ProcessTest
, Duplicate
) {
65 Process
process1(SpawnChild("SimpleChildProcess"));
66 ASSERT_TRUE(process1
.IsValid());
68 Process process2
= process1
.Duplicate();
69 ASSERT_TRUE(process1
.IsValid());
70 ASSERT_TRUE(process2
.IsValid());
71 EXPECT_EQ(process1
.Pid(), process2
.Pid());
72 EXPECT_FALSE(process1
.is_current());
73 EXPECT_FALSE(process2
.is_current());
76 ASSERT_TRUE(process2
.IsValid());
79 TEST_F(ProcessTest
, DuplicateCurrent
) {
80 Process process1
= Process::Current();
81 ASSERT_TRUE(process1
.IsValid());
83 Process process2
= process1
.Duplicate();
84 ASSERT_TRUE(process1
.IsValid());
85 ASSERT_TRUE(process2
.IsValid());
86 EXPECT_EQ(process1
.Pid(), process2
.Pid());
87 EXPECT_TRUE(process1
.is_current());
88 EXPECT_TRUE(process2
.is_current());
91 ASSERT_TRUE(process2
.IsValid());
94 TEST_F(ProcessTest
, DeprecatedGetProcessFromHandle
) {
95 Process
process1(SpawnChild("SimpleChildProcess"));
96 ASSERT_TRUE(process1
.IsValid());
98 Process process2
= Process::DeprecatedGetProcessFromHandle(process1
.Handle());
99 ASSERT_TRUE(process1
.IsValid());
100 ASSERT_TRUE(process2
.IsValid());
101 EXPECT_EQ(process1
.Pid(), process2
.Pid());
102 EXPECT_FALSE(process1
.is_current());
103 EXPECT_FALSE(process2
.is_current());
106 ASSERT_TRUE(process2
.IsValid());
109 MULTIPROCESS_TEST_MAIN(SleepyChildProcess
) {
110 PlatformThread::Sleep(TestTimeouts::action_max_timeout());
114 TEST_F(ProcessTest
, Terminate
) {
115 Process
process(SpawnChild("SleepyChildProcess"));
116 ASSERT_TRUE(process
.IsValid());
118 const int kDummyExitCode
= 42;
119 int exit_code
= kDummyExitCode
;
120 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING
,
121 GetTerminationStatus(process
.Handle(), &exit_code
));
122 EXPECT_EQ(kExpectedStillRunningExitCode
, exit_code
);
124 exit_code
= kDummyExitCode
;
125 int kExpectedExitCode
= 250;
126 process
.Terminate(kExpectedExitCode
, false);
127 process
.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
130 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING
,
131 GetTerminationStatus(process
.Handle(), &exit_code
));
132 #if !defined(OS_POSIX)
133 // The POSIX implementation actually ignores the exit_code.
134 EXPECT_EQ(kExpectedExitCode
, exit_code
);
138 MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess
) {
139 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
143 TEST_F(ProcessTest
, WaitForExit
) {
144 Process
process(SpawnChild("FastSleepyChildProcess"));
145 ASSERT_TRUE(process
.IsValid());
147 const int kDummyExitCode
= 42;
148 int exit_code
= kDummyExitCode
;
149 EXPECT_TRUE(process
.WaitForExit(&exit_code
));
150 EXPECT_EQ(0, exit_code
);
153 TEST_F(ProcessTest
, WaitForExitWithTimeout
) {
154 Process
process(SpawnChild("SleepyChildProcess"));
155 ASSERT_TRUE(process
.IsValid());
157 const int kDummyExitCode
= 42;
158 int exit_code
= kDummyExitCode
;
159 TimeDelta timeout
= TestTimeouts::tiny_timeout();
160 EXPECT_FALSE(process
.WaitForExitWithTimeout(timeout
, &exit_code
));
161 EXPECT_EQ(kDummyExitCode
, exit_code
);
163 process
.Terminate(kDummyExitCode
, false);
166 // Ensure that the priority of a process is restored correctly after
167 // backgrounding and restoring.
168 // Note: a platform may not be willing or able to lower the priority of
169 // a process. The calls to SetProcessBackground should be noops then.
170 TEST_F(ProcessTest
, SetProcessBackgrounded
) {
171 Process
process(SpawnChild("SimpleChildProcess"));
172 int old_priority
= process
.GetPriority();
174 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
175 EXPECT_TRUE(process
.IsProcessBackgrounded());
176 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
177 EXPECT_FALSE(process
.IsProcessBackgrounded());
179 process
.SetProcessBackgrounded(true);
180 process
.SetProcessBackgrounded(false);
182 int new_priority
= process
.GetPriority();
183 EXPECT_EQ(old_priority
, new_priority
);
186 // Same as SetProcessBackgrounded but to this very process. It uses
187 // a different code path at least for Windows.
188 TEST_F(ProcessTest
, SetProcessBackgroundedSelf
) {
189 Process process
= Process::Current();
190 int old_priority
= process
.GetPriority();
192 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
193 EXPECT_TRUE(process
.IsProcessBackgrounded());
194 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
195 EXPECT_FALSE(process
.IsProcessBackgrounded());
197 process
.SetProcessBackgrounded(true);
198 process
.SetProcessBackgrounded(false);
200 int new_priority
= process
.GetPriority();
201 EXPECT_EQ(old_priority
, new_priority
);