Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / process / process_unittest.cc
blobe094c032f3bb07e4e7530ac3276e2e77726629e0
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"
14 #if defined(OS_MACOSX)
15 #include <mach/mach.h>
16 #endif // OS_MACOSX
18 namespace {
20 #if defined(OS_WIN)
21 const int kExpectedStillRunningExitCode = 0x102;
22 #else
23 const int kExpectedStillRunningExitCode = 0;
24 #endif
26 } // namespace
28 namespace base {
30 class ProcessTest : public MultiProcessTest {
33 TEST_F(ProcessTest, Create) {
34 Process process(SpawnChild("SimpleChildProcess"));
35 ASSERT_TRUE(process.IsValid());
36 ASSERT_FALSE(process.is_current());
37 process.Close();
38 ASSERT_FALSE(process.IsValid());
41 TEST_F(ProcessTest, CreateCurrent) {
42 Process process = Process::Current();
43 ASSERT_TRUE(process.IsValid());
44 ASSERT_TRUE(process.is_current());
45 process.Close();
46 ASSERT_FALSE(process.IsValid());
49 TEST_F(ProcessTest, Move) {
50 Process process1(SpawnChild("SimpleChildProcess"));
51 EXPECT_TRUE(process1.IsValid());
53 Process process2;
54 EXPECT_FALSE(process2.IsValid());
56 process2 = process1.Pass();
57 EXPECT_TRUE(process2.IsValid());
58 EXPECT_FALSE(process1.IsValid());
59 EXPECT_FALSE(process2.is_current());
61 Process process3 = Process::Current();
62 process2 = process3.Pass();
63 EXPECT_TRUE(process2.is_current());
64 EXPECT_TRUE(process2.IsValid());
65 EXPECT_FALSE(process3.IsValid());
68 TEST_F(ProcessTest, Duplicate) {
69 Process process1(SpawnChild("SimpleChildProcess"));
70 ASSERT_TRUE(process1.IsValid());
72 Process process2 = process1.Duplicate();
73 ASSERT_TRUE(process1.IsValid());
74 ASSERT_TRUE(process2.IsValid());
75 EXPECT_EQ(process1.Pid(), process2.Pid());
76 EXPECT_FALSE(process1.is_current());
77 EXPECT_FALSE(process2.is_current());
79 process1.Close();
80 ASSERT_TRUE(process2.IsValid());
83 TEST_F(ProcessTest, DuplicateCurrent) {
84 Process process1 = Process::Current();
85 ASSERT_TRUE(process1.IsValid());
87 Process process2 = process1.Duplicate();
88 ASSERT_TRUE(process1.IsValid());
89 ASSERT_TRUE(process2.IsValid());
90 EXPECT_EQ(process1.Pid(), process2.Pid());
91 EXPECT_TRUE(process1.is_current());
92 EXPECT_TRUE(process2.is_current());
94 process1.Close();
95 ASSERT_TRUE(process2.IsValid());
98 TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) {
99 Process process1(SpawnChild("SimpleChildProcess"));
100 ASSERT_TRUE(process1.IsValid());
102 Process process2 = Process::DeprecatedGetProcessFromHandle(process1.Handle());
103 ASSERT_TRUE(process1.IsValid());
104 ASSERT_TRUE(process2.IsValid());
105 EXPECT_EQ(process1.Pid(), process2.Pid());
106 EXPECT_FALSE(process1.is_current());
107 EXPECT_FALSE(process2.is_current());
109 process1.Close();
110 ASSERT_TRUE(process2.IsValid());
113 MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
114 PlatformThread::Sleep(TestTimeouts::action_max_timeout());
115 return 0;
118 TEST_F(ProcessTest, Terminate) {
119 Process process(SpawnChild("SleepyChildProcess"));
120 ASSERT_TRUE(process.IsValid());
122 const int kDummyExitCode = 42;
123 int exit_code = kDummyExitCode;
124 EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
125 GetTerminationStatus(process.Handle(), &exit_code));
126 EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
128 exit_code = kDummyExitCode;
129 int kExpectedExitCode = 250;
130 process.Terminate(kExpectedExitCode, false);
131 process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
132 &exit_code);
134 EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
135 GetTerminationStatus(process.Handle(), &exit_code));
136 #if !defined(OS_POSIX)
137 // The POSIX implementation actually ignores the exit_code.
138 EXPECT_EQ(kExpectedExitCode, exit_code);
139 #endif
142 MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
143 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
144 return 0;
147 TEST_F(ProcessTest, WaitForExit) {
148 Process process(SpawnChild("FastSleepyChildProcess"));
149 ASSERT_TRUE(process.IsValid());
151 const int kDummyExitCode = 42;
152 int exit_code = kDummyExitCode;
153 EXPECT_TRUE(process.WaitForExit(&exit_code));
154 EXPECT_EQ(0, exit_code);
157 TEST_F(ProcessTest, WaitForExitWithTimeout) {
158 Process process(SpawnChild("SleepyChildProcess"));
159 ASSERT_TRUE(process.IsValid());
161 const int kDummyExitCode = 42;
162 int exit_code = kDummyExitCode;
163 TimeDelta timeout = TestTimeouts::tiny_timeout();
164 EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
165 EXPECT_EQ(kDummyExitCode, exit_code);
167 process.Terminate(kDummyExitCode, false);
170 // Ensure that the priority of a process is restored correctly after
171 // backgrounding and restoring.
172 // Note: a platform may not be willing or able to lower the priority of
173 // a process. The calls to SetProcessBackground should be noops then.
174 TEST_F(ProcessTest, SetProcessBackgrounded) {
175 Process process(SpawnChild("SimpleChildProcess"));
176 int old_priority = process.GetPriority();
177 #if defined(OS_MACOSX)
178 // On the Mac, backgrounding a process requires a port to that process.
179 // In the browser it's available through the MachBroker class, which is not
180 // part of base. Additionally, there is an indefinite amount of time between
181 // spawning a process and receiving its port. Because this test just checks
182 // the ability to background/foreground a process, we can use the current
183 // process's port instead.
184 mach_port_t process_port = mach_task_self();
185 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, true));
186 EXPECT_TRUE(process.IsProcessBackgrounded(process_port));
187 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, false));
188 EXPECT_FALSE(process.IsProcessBackgrounded(process_port));
189 #elif defined(OS_WIN)
190 EXPECT_TRUE(process.SetProcessBackgrounded(true));
191 EXPECT_TRUE(process.IsProcessBackgrounded());
192 EXPECT_TRUE(process.SetProcessBackgrounded(false));
193 EXPECT_FALSE(process.IsProcessBackgrounded());
194 #else
195 process.SetProcessBackgrounded(true);
196 process.SetProcessBackgrounded(false);
197 #endif
198 int new_priority = process.GetPriority();
199 EXPECT_EQ(old_priority, new_priority);
202 // Same as SetProcessBackgrounded but to this very process. It uses
203 // a different code path at least for Windows.
204 TEST_F(ProcessTest, SetProcessBackgroundedSelf) {
205 Process process = Process::Current();
206 int old_priority = process.GetPriority();
207 #if defined(OS_MACOSX)
208 mach_port_t process_port = mach_task_self();
209 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, true));
210 EXPECT_TRUE(process.IsProcessBackgrounded(process_port));
211 EXPECT_TRUE(process.SetProcessBackgrounded(process_port, false));
212 EXPECT_FALSE(process.IsProcessBackgrounded(process_port));
213 #elif defined(OS_WIN)
214 EXPECT_TRUE(process.SetProcessBackgrounded(true));
215 EXPECT_TRUE(process.IsProcessBackgrounded());
216 EXPECT_TRUE(process.SetProcessBackgrounded(false));
217 EXPECT_FALSE(process.IsProcessBackgrounded());
218 #else
219 process.SetProcessBackgrounded(true);
220 process.SetProcessBackgrounded(false);
221 #endif
222 int new_priority = process.GetPriority();
223 EXPECT_EQ(old_priority, new_priority);
226 } // namespace base