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"
18 const int kExpectedStillRunningExitCode
= 0x102;
20 const int kExpectedStillRunningExitCode
= 0;
27 class ProcessTest
: public MultiProcessTest
{
30 TEST_F(ProcessTest
, Create
) {
31 Process
process(SpawnChild("SimpleChildProcess"));
32 ASSERT_TRUE(process
.IsValid());
33 ASSERT_FALSE(process
.is_current());
35 ASSERT_FALSE(process
.IsValid());
38 TEST_F(ProcessTest
, CreateCurrent
) {
39 Process process
= Process::Current();
40 ASSERT_TRUE(process
.IsValid());
41 ASSERT_TRUE(process
.is_current());
43 ASSERT_FALSE(process
.IsValid());
46 TEST_F(ProcessTest
, Move
) {
47 Process
process1(SpawnChild("SimpleChildProcess"));
48 EXPECT_TRUE(process1
.IsValid());
51 EXPECT_FALSE(process2
.IsValid());
53 process2
= process1
.Pass();
54 EXPECT_TRUE(process2
.IsValid());
55 EXPECT_FALSE(process1
.IsValid());
56 EXPECT_FALSE(process2
.is_current());
58 Process process3
= Process::Current();
59 process2
= process3
.Pass();
60 EXPECT_TRUE(process2
.is_current());
61 EXPECT_TRUE(process2
.IsValid());
62 EXPECT_FALSE(process3
.IsValid());
65 TEST_F(ProcessTest
, Duplicate
) {
66 Process
process1(SpawnChild("SimpleChildProcess"));
67 ASSERT_TRUE(process1
.IsValid());
69 Process process2
= process1
.Duplicate();
70 ASSERT_TRUE(process1
.IsValid());
71 ASSERT_TRUE(process2
.IsValid());
72 EXPECT_EQ(process1
.pid(), process2
.pid());
73 EXPECT_FALSE(process1
.is_current());
74 EXPECT_FALSE(process2
.is_current());
77 ASSERT_TRUE(process2
.IsValid());
80 TEST_F(ProcessTest
, DuplicateCurrent
) {
81 Process process1
= Process::Current();
82 ASSERT_TRUE(process1
.IsValid());
84 Process process2
= process1
.Duplicate();
85 ASSERT_TRUE(process1
.IsValid());
86 ASSERT_TRUE(process2
.IsValid());
87 EXPECT_EQ(process1
.pid(), process2
.pid());
88 EXPECT_TRUE(process1
.is_current());
89 EXPECT_TRUE(process2
.is_current());
92 ASSERT_TRUE(process2
.IsValid());
95 MULTIPROCESS_TEST_MAIN(SleepyChildProcess
) {
96 PlatformThread::Sleep(TestTimeouts::action_max_timeout());
100 TEST_F(ProcessTest
, Terminate
) {
101 Process
process(SpawnChild("SleepyChildProcess"));
102 ASSERT_TRUE(process
.IsValid());
104 const int kDummyExitCode
= 42;
105 int exit_code
= kDummyExitCode
;
106 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING
,
107 GetTerminationStatus(process
.Handle(), &exit_code
));
108 EXPECT_EQ(kExpectedStillRunningExitCode
, exit_code
);
110 exit_code
= kDummyExitCode
;
111 int kExpectedExitCode
= 250;
112 process
.Terminate(kExpectedExitCode
);
113 WaitForSingleProcess(process
.Handle(), TestTimeouts::action_max_timeout());
115 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING
,
116 GetTerminationStatus(process
.Handle(), &exit_code
));
117 #if !defined(OS_POSIX)
118 // The POSIX implementation actually ignores the exit_code.
119 EXPECT_EQ(kExpectedExitCode
, exit_code
);
123 // Ensure that the priority of a process is restored correctly after
124 // backgrounding and restoring.
125 // Note: a platform may not be willing or able to lower the priority of
126 // a process. The calls to SetProcessBackground should be noops then.
127 TEST_F(ProcessTest
, SetProcessBackgrounded
) {
128 Process
process(SpawnChild("SimpleChildProcess"));
129 int old_priority
= process
.GetPriority();
131 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
132 EXPECT_TRUE(process
.IsProcessBackgrounded());
133 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
134 EXPECT_FALSE(process
.IsProcessBackgrounded());
136 process
.SetProcessBackgrounded(true);
137 process
.SetProcessBackgrounded(false);
139 int new_priority
= process
.GetPriority();
140 EXPECT_EQ(old_priority
, new_priority
);
143 // Same as SetProcessBackgrounded but to this very process. It uses
144 // a different code path at least for Windows.
145 TEST_F(ProcessTest
, SetProcessBackgroundedSelf
) {
146 Process process
= Process::Current();
147 int old_priority
= process
.GetPriority();
149 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
150 EXPECT_TRUE(process
.IsProcessBackgrounded());
151 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
152 EXPECT_FALSE(process
.IsProcessBackgrounded());
154 process
.SetProcessBackgrounded(true);
155 process
.SetProcessBackgrounded(false);
157 int new_priority
= process
.GetPriority();
158 EXPECT_EQ(old_priority
, new_priority
);