1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/process.h"
#include <utility>
#include "base/process/kill.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
namespace {
#if defined(OS_WIN)
const int kExpectedStillRunningExitCode = 0x102;
#else
const int kExpectedStillRunningExitCode = 0;
#endif
#if defined(OS_MACOSX)
// Fake port provider that returns the calling process's
// task port, ignoring its argument.
class FakePortProvider : public base::PortProvider {
mach_port_t TaskForPid(base::ProcessHandle process) const override {
return mach_task_self();
}
};
#endif
} // namespace
namespace base {
class ProcessTest : public MultiProcessTest {
};
TEST_F(ProcessTest, Create) {
Process process(SpawnChild("SimpleChildProcess"));
ASSERT_TRUE(process.IsValid());
ASSERT_FALSE(process.is_current());
process.Close();
ASSERT_FALSE(process.IsValid());
}
TEST_F(ProcessTest, CreateCurrent) {
Process process = Process::Current();
ASSERT_TRUE(process.IsValid());
ASSERT_TRUE(process.is_current());
process.Close();
ASSERT_FALSE(process.IsValid());
}
TEST_F(ProcessTest, Move) {
Process process1(SpawnChild("SimpleChildProcess"));
EXPECT_TRUE(process1.IsValid());
Process process2;
EXPECT_FALSE(process2.IsValid());
process2 = std::move(process1);
EXPECT_TRUE(process2.IsValid());
EXPECT_FALSE(process1.IsValid());
EXPECT_FALSE(process2.is_current());
Process process3 = Process::Current();
process2 = std::move(process3);
EXPECT_TRUE(process2.is_current());
EXPECT_TRUE(process2.IsValid());
EXPECT_FALSE(process3.IsValid());
}
TEST_F(ProcessTest, Duplicate) {
Process process1(SpawnChild("SimpleChildProcess"));
ASSERT_TRUE(process1.IsValid());
Process process2 = process1.Duplicate();
ASSERT_TRUE(process1.IsValid());
ASSERT_TRUE(process2.IsValid());
EXPECT_EQ(process1.Pid(), process2.Pid());
EXPECT_FALSE(process1.is_current());
EXPECT_FALSE(process2.is_current());
process1.Close();
ASSERT_TRUE(process2.IsValid());
}
TEST_F(ProcessTest, DuplicateCurrent) {
Process process1 = Process::Current();
ASSERT_TRUE(process1.IsValid());
Process process2 = process1.Duplicate();
ASSERT_TRUE(process1.IsValid());
ASSERT_TRUE(process2.IsValid());
EXPECT_EQ(process1.Pid(), process2.Pid());
EXPECT_TRUE(process1.is_current());
EXPECT_TRUE(process2.is_current());
process1.Close();
ASSERT_TRUE(process2.IsValid());
}
TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) {
Process process1(SpawnChild("SimpleChildProcess"));
ASSERT_TRUE(process1.IsValid());
Process process2 = Process::DeprecatedGetProcessFromHandle(process1.Handle());
ASSERT_TRUE(process1.IsValid());
ASSERT_TRUE(process2.IsValid());
EXPECT_EQ(process1.Pid(), process2.Pid());
EXPECT_FALSE(process1.is_current());
EXPECT_FALSE(process2.is_current());
process1.Close();
ASSERT_TRUE(process2.IsValid());
}
MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
PlatformThread::Sleep(TestTimeouts::action_max_timeout());
return 0;
}
TEST_F(ProcessTest, Terminate) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
exit_code = kDummyExitCode;
int kExpectedExitCode = 250;
process.Terminate(kExpectedExitCode, false);
process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code);
EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
#if !defined(OS_POSIX)
// The POSIX implementation actually ignores the exit_code.
EXPECT_EQ(kExpectedExitCode, exit_code);
#endif
}
MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
return 0;
}
TEST_F(ProcessTest, WaitForExit) {
Process process(SpawnChild("FastSleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(0, exit_code);
}
TEST_F(ProcessTest, WaitForExitWithTimeout) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode;
TimeDelta timeout = TestTimeouts::tiny_timeout();
EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
EXPECT_EQ(kDummyExitCode, exit_code);
process.Terminate(kDummyExitCode, false);
}
// Ensure that the priority of a process is restored correctly after
// backgrounding and restoring.
// Note: a platform may not be willing or able to lower the priority of
// a process. The calls to SetProcessBackground should be noops then.
TEST_F(ProcessTest, SetProcessBackgrounded) {
if (!Process::CanBackgroundProcesses())
return;
Process process(SpawnChild("SimpleChildProcess"));
int old_priority = process.GetPriority();
#if defined(OS_WIN)
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
#elif defined(OS_MACOSX)
// On the Mac, backgrounding a process requires a port to that process.
// In the browser it's available through the MachBroker class, which is not
// part of base. Additionally, there is an indefinite amount of time between
// spawning a process and receiving its port. Because this test just checks
// the ability to background/foreground a process, we can use the current
// process's port instead.
FakePortProvider provider;
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true));
EXPECT_TRUE(process.IsProcessBackgrounded(&provider));
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false));
EXPECT_FALSE(process.IsProcessBackgrounded(&provider));
#else
process.SetProcessBackgrounded(true);
process.SetProcessBackgrounded(false);
#endif
int new_priority = process.GetPriority();
EXPECT_EQ(old_priority, new_priority);
}
// Same as SetProcessBackgrounded but to this very process. It uses
// a different code path at least for Windows.
TEST_F(ProcessTest, SetProcessBackgroundedSelf) {
if (!Process::CanBackgroundProcesses())
return;
Process process = Process::Current();
int old_priority = process.GetPriority();
#if defined(OS_WIN)
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
#elif defined(OS_MACOSX)
FakePortProvider provider;
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true));
EXPECT_TRUE(process.IsProcessBackgrounded(&provider));
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false));
EXPECT_FALSE(process.IsProcessBackgrounded(&provider));
#else
process.SetProcessBackgrounded(true);
process.SetProcessBackgrounded(false);
#endif
int new_priority = process.GetPriority();
EXPECT_EQ(old_priority, new_priority);
}
#if defined(OS_CHROMEOS)
// Tests that the function IsProcessBackgroundedCGroup() can parse the contents
// of the /proc/<pid>/cgroup file successfully.
TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) {
const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n";
const char kBackgrounded[] =
"2:freezer:/chrome_renderers/to_be_frozen\n"
"1:cpu:/chrome_renderers/background\n";
EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded));
EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded));
}
#endif // defined(OS_CHROMEOS)
} // namespace base
|