| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| 11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/multiprocess_func_list.h" | 15 #include "testing/multiprocess_func_list.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 const int kExpectedStillRunningExitCode = 0x102; | 20 const int kExpectedStillRunningExitCode = 0x102; |
| 21 #else | 21 #else |
| 22 const int kExpectedStillRunningExitCode = 0; | 22 const int kExpectedStillRunningExitCode = 0; |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 #if defined(OS_MACOSX) |
| 26 // Fake port provider that returns the calling process's |
| 27 // task port, ignoring its argument. |
| 28 class FakePortProvider : public base::PortProvider { |
| 29 mach_port_t TaskForPid(base::ProcessHandle process) const override { |
| 30 return mach_task_self(); |
| 31 } |
| 32 }; |
| 33 #endif |
| 34 |
| 25 } // namespace | 35 } // namespace |
| 26 | 36 |
| 27 namespace base { | 37 namespace base { |
| 28 | 38 |
| 29 class ProcessTest : public MultiProcessTest { | 39 class ProcessTest : public MultiProcessTest { |
| 30 }; | 40 }; |
| 31 | 41 |
| 32 TEST_F(ProcessTest, Create) { | 42 TEST_F(ProcessTest, Create) { |
| 33 Process process(SpawnChild("SimpleChildProcess")); | 43 Process process(SpawnChild("SimpleChildProcess")); |
| 34 ASSERT_TRUE(process.IsValid()); | 44 ASSERT_TRUE(process.IsValid()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 EXPECT_EQ(kDummyExitCode, exit_code); | 174 EXPECT_EQ(kDummyExitCode, exit_code); |
| 165 | 175 |
| 166 process.Terminate(kDummyExitCode, false); | 176 process.Terminate(kDummyExitCode, false); |
| 167 } | 177 } |
| 168 | 178 |
| 169 // Ensure that the priority of a process is restored correctly after | 179 // Ensure that the priority of a process is restored correctly after |
| 170 // backgrounding and restoring. | 180 // backgrounding and restoring. |
| 171 // Note: a platform may not be willing or able to lower the priority of | 181 // Note: a platform may not be willing or able to lower the priority of |
| 172 // a process. The calls to SetProcessBackground should be noops then. | 182 // a process. The calls to SetProcessBackground should be noops then. |
| 173 TEST_F(ProcessTest, SetProcessBackgrounded) { | 183 TEST_F(ProcessTest, SetProcessBackgrounded) { |
| 184 if (!Process::CanBackgroundProcesses()) |
| 185 return; |
| 174 Process process(SpawnChild("SimpleChildProcess")); | 186 Process process(SpawnChild("SimpleChildProcess")); |
| 175 int old_priority = process.GetPriority(); | 187 int old_priority = process.GetPriority(); |
| 176 #if defined(OS_WIN) | 188 #if defined(OS_WIN) |
| 177 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 189 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 178 EXPECT_TRUE(process.IsProcessBackgrounded()); | 190 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 179 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 191 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 180 EXPECT_FALSE(process.IsProcessBackgrounded()); | 192 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 193 #elif defined(OS_MACOSX) |
| 194 // On the Mac, backgrounding a process requires a port to that process. |
| 195 // In the browser it's available through the MachBroker class, which is not |
| 196 // part of base. Additionally, there is an indefinite amount of time between |
| 197 // spawning a process and receiving its port. Because this test just checks |
| 198 // the ability to background/foreground a process, we can use the current |
| 199 // process's port instead. |
| 200 FakePortProvider provider; |
| 201 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true)); |
| 202 EXPECT_TRUE(process.IsProcessBackgrounded(&provider)); |
| 203 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false)); |
| 204 EXPECT_FALSE(process.IsProcessBackgrounded(&provider)); |
| 205 |
| 181 #else | 206 #else |
| 182 if (process.CanBackgroundProcesses()) { | 207 process.SetProcessBackgrounded(true); |
| 183 process.SetProcessBackgrounded(true); | 208 process.SetProcessBackgrounded(false); |
| 184 process.SetProcessBackgrounded(false); | |
| 185 } | |
| 186 #endif | 209 #endif |
| 187 int new_priority = process.GetPriority(); | 210 int new_priority = process.GetPriority(); |
| 188 EXPECT_EQ(old_priority, new_priority); | 211 EXPECT_EQ(old_priority, new_priority); |
| 189 } | 212 } |
| 190 | 213 |
| 191 // Same as SetProcessBackgrounded but to this very process. It uses | 214 // Same as SetProcessBackgrounded but to this very process. It uses |
| 192 // a different code path at least for Windows. | 215 // a different code path at least for Windows. |
| 193 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { | 216 TEST_F(ProcessTest, SetProcessBackgroundedSelf) { |
| 217 if (!Process::CanBackgroundProcesses()) |
| 218 return; |
| 194 Process process = Process::Current(); | 219 Process process = Process::Current(); |
| 195 int old_priority = process.GetPriority(); | 220 int old_priority = process.GetPriority(); |
| 196 #if defined(OS_WIN) | 221 #if defined(OS_WIN) |
| 197 EXPECT_TRUE(process.SetProcessBackgrounded(true)); | 222 EXPECT_TRUE(process.SetProcessBackgrounded(true)); |
| 198 EXPECT_TRUE(process.IsProcessBackgrounded()); | 223 EXPECT_TRUE(process.IsProcessBackgrounded()); |
| 199 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 224 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
| 200 EXPECT_FALSE(process.IsProcessBackgrounded()); | 225 EXPECT_FALSE(process.IsProcessBackgrounded()); |
| 226 #elif defined(OS_MACOSX) |
| 227 FakePortProvider provider; |
| 228 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true)); |
| 229 EXPECT_TRUE(process.IsProcessBackgrounded(&provider)); |
| 230 EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false)); |
| 231 EXPECT_FALSE(process.IsProcessBackgrounded(&provider)); |
| 201 #else | 232 #else |
| 202 process.SetProcessBackgrounded(true); | 233 process.SetProcessBackgrounded(true); |
| 203 process.SetProcessBackgrounded(false); | 234 process.SetProcessBackgrounded(false); |
| 204 #endif | 235 #endif |
| 205 int new_priority = process.GetPriority(); | 236 int new_priority = process.GetPriority(); |
| 206 EXPECT_EQ(old_priority, new_priority); | 237 EXPECT_EQ(old_priority, new_priority); |
| 207 } | 238 } |
| 208 | 239 |
| 209 #if defined(OS_CHROMEOS) | 240 #if defined(OS_CHROMEOS) |
| 210 | 241 |
| 211 // Tests that the function IsProcessBackgroundedCGroup() can parse the contents | 242 // Tests that the function IsProcessBackgroundedCGroup() can parse the contents |
| 212 // of the /proc/<pid>/cgroup file successfully. | 243 // of the /proc/<pid>/cgroup file successfully. |
| 213 TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) { | 244 TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) { |
| 214 const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n"; | 245 const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n"; |
| 215 const char kBackgrounded[] = | 246 const char kBackgrounded[] = |
| 216 "2:freezer:/chrome_renderers/to_be_frozen\n" | 247 "2:freezer:/chrome_renderers/to_be_frozen\n" |
| 217 "1:cpu:/chrome_renderers/background\n"; | 248 "1:cpu:/chrome_renderers/background\n"; |
| 218 | 249 |
| 219 EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded)); | 250 EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded)); |
| 220 EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded)); | 251 EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded)); |
| 221 } | 252 } |
| 222 | 253 |
| 223 #endif // defined(OS_CHROMEOS) | 254 #endif // defined(OS_CHROMEOS) |
| 224 | 255 |
| 225 } // namespace base | 256 } // namespace base |
| OLD | NEW |