| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/process/process.h" | |
| 6 | |
| 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" | |
| 13 | |
| 14 #if defined(OS_MACOSX) | |
| 15 #include <mach/mach.h> | |
| 16 #endif // OS_MACOSX | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 #if defined(OS_WIN) | |
| 21 const int kExpectedStillRunningExitCode = 0x102; | |
| 22 #else | |
| 23 const int kExpectedStillRunningExitCode = 0; | |
| 24 #endif | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 namespace base { | |
| 29 | |
| 30 class ProcessTest : public MultiProcessTest { | |
| 31 }; | |
| 32 | |
| 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()); | |
| 39 } | |
| 40 | |
| 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()); | |
| 47 } | |
| 48 | |
| 49 TEST_F(ProcessTest, Move) { | |
| 50 Process process1(SpawnChild("SimpleChildProcess")); | |
| 51 EXPECT_TRUE(process1.IsValid()); | |
| 52 | |
| 53 Process process2; | |
| 54 EXPECT_FALSE(process2.IsValid()); | |
| 55 | |
| 56 process2 = process1.Pass(); | |
| 57 EXPECT_TRUE(process2.IsValid()); | |
| 58 EXPECT_FALSE(process1.IsValid()); | |
| 59 EXPECT_FALSE(process2.is_current()); | |
| 60 | |
| 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()); | |
| 66 } | |
| 67 | |
| 68 TEST_F(ProcessTest, Duplicate) { | |
| 69 Process process1(SpawnChild("SimpleChildProcess")); | |
| 70 ASSERT_TRUE(process1.IsValid()); | |
| 71 | |
| 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()); | |
| 78 | |
| 79 process1.Close(); | |
| 80 ASSERT_TRUE(process2.IsValid()); | |
| 81 } | |
| 82 | |
| 83 TEST_F(ProcessTest, DuplicateCurrent) { | |
| 84 Process process1 = Process::Current(); | |
| 85 ASSERT_TRUE(process1.IsValid()); | |
| 86 | |
| 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()); | |
| 93 | |
| 94 process1.Close(); | |
| 95 ASSERT_TRUE(process2.IsValid()); | |
| 96 } | |
| 97 | |
| 98 TEST_F(ProcessTest, DeprecatedGetProcessFromHandle) { | |
| 99 Process process1(SpawnChild("SimpleChildProcess")); | |
| 100 ASSERT_TRUE(process1.IsValid()); | |
| 101 | |
| 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()); | |
| 108 | |
| 109 process1.Close(); | |
| 110 ASSERT_TRUE(process2.IsValid()); | |
| 111 } | |
| 112 | |
| 113 MULTIPROCESS_TEST_MAIN(SleepyChildProcess) { | |
| 114 PlatformThread::Sleep(TestTimeouts::action_max_timeout()); | |
| 115 return 0; | |
| 116 } | |
| 117 | |
| 118 TEST_F(ProcessTest, Terminate) { | |
| 119 Process process(SpawnChild("SleepyChildProcess")); | |
| 120 ASSERT_TRUE(process.IsValid()); | |
| 121 | |
| 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); | |
| 127 | |
| 128 exit_code = kDummyExitCode; | |
| 129 int kExpectedExitCode = 250; | |
| 130 process.Terminate(kExpectedExitCode, false); | |
| 131 process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
| 132 &exit_code); | |
| 133 | |
| 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 | |
| 140 } | |
| 141 | |
| 142 MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) { | |
| 143 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10); | |
| 144 return 0; | |
| 145 } | |
| 146 | |
| 147 TEST_F(ProcessTest, WaitForExit) { | |
| 148 Process process(SpawnChild("FastSleepyChildProcess")); | |
| 149 ASSERT_TRUE(process.IsValid()); | |
| 150 | |
| 151 const int kDummyExitCode = 42; | |
| 152 int exit_code = kDummyExitCode; | |
| 153 EXPECT_TRUE(process.WaitForExit(&exit_code)); | |
| 154 EXPECT_EQ(0, exit_code); | |
| 155 } | |
| 156 | |
| 157 TEST_F(ProcessTest, WaitForExitWithTimeout) { | |
| 158 Process process(SpawnChild("SleepyChildProcess")); | |
| 159 ASSERT_TRUE(process.IsValid()); | |
| 160 | |
| 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); | |
| 166 | |
| 167 process.Terminate(kDummyExitCode, false); | |
| 168 } | |
| 169 | |
| 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); | |
| 200 } | |
| 201 | |
| 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); | |
| 224 } | |
| 225 | |
| 226 } // namespace base | |
| OLD | NEW |