Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: base/process_util_unittest.cc

Issue 1689012: Move common code into process_util.cc (Closed)
Patch Set: More fixes Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/process_util_posix.cc ('k') | base/process_util_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #define _CRT_SECURE_NO_WARNINGS 5 #define _CRT_SECURE_NO_WARNINGS
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/multiprocess_test.h" 12 #include "base/multiprocess_test.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/platform_thread.h" 14 #include "base/platform_thread.h"
15 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/scoped_ptr.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 #if defined(OS_LINUX) 19 #if defined(OS_LINUX)
19 #include <dlfcn.h> 20 #include <dlfcn.h>
20 #include <errno.h> 21 #include <errno.h>
21 #include <malloc.h> 22 #include <malloc.h>
22 #include <glib.h> 23 #include <glib.h>
23 #endif 24 #endif
24 #if defined(OS_POSIX) 25 #if defined(OS_POSIX)
25 #include <fcntl.h> 26 #include <fcntl.h>
26 #include <sys/resource.h> 27 #include <sys/resource.h>
27 #include <sys/socket.h> 28 #include <sys/socket.h>
28 #endif 29 #endif
29 #if defined(OS_WIN) 30 #if defined(OS_WIN)
30 #include <windows.h> 31 #include <windows.h>
31 #endif 32 #endif
32 #if defined(OS_MACOSX) 33 #if defined(OS_MACOSX)
33 #include "base/process_util_unittest_mac.h" 34 #include "base/process_util_unittest_mac.h"
34 #endif 35 #endif
35 36
36 namespace base { 37 namespace {
38
39 #if defined(OS_WIN)
40 const wchar_t* const kProcessName = L"base_unittests.exe";
41 #else
42 const wchar_t* const kProcessName = L"base_unittests";
43 #endif // defined(OS_WIN)
44
45 } // namespace
37 46
38 class ProcessUtilTest : public MultiProcessTest { 47 class ProcessUtilTest : public MultiProcessTest {
39 #if defined(OS_POSIX) 48 #if defined(OS_POSIX)
40 public: 49 public:
41 // Spawn a child process that counts how many file descriptors are open. 50 // Spawn a child process that counts how many file descriptors are open.
42 int CountOpenFDsInChild(); 51 int CountOpenFDsInChild();
43 #endif 52 #endif
44 }; 53 };
45 54
46 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { 55 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) {
47 return 0; 56 return 0;
48 } 57 }
49 58
50 TEST_F(ProcessUtilTest, SpawnChild) { 59 TEST_F(ProcessUtilTest, SpawnChild) {
51 ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess"); 60 base::ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess");
52 61
53 ASSERT_NE(base::kNullProcessHandle, handle); 62 ASSERT_NE(base::kNullProcessHandle, handle);
54 EXPECT_TRUE(WaitForSingleProcess(handle, 5000)); 63 EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000));
55 base::CloseProcessHandle(handle); 64 base::CloseProcessHandle(handle);
56 } 65 }
57 66
58 MULTIPROCESS_TEST_MAIN(SlowChildProcess) { 67 MULTIPROCESS_TEST_MAIN(SlowChildProcess) {
59 // Sleep until file "SlowChildProcess.die" is created. 68 // Sleep until file "SlowChildProcess.die" is created.
60 FILE *fp; 69 FILE *fp;
61 do { 70 do {
62 PlatformThread::Sleep(100); 71 PlatformThread::Sleep(100);
63 fp = fopen("SlowChildProcess.die", "r"); 72 fp = fopen("SlowChildProcess.die", "r");
64 } while (!fp); 73 } while (!fp);
65 fclose(fp); 74 fclose(fp);
66 remove("SlowChildProcess.die"); 75 remove("SlowChildProcess.die");
67 exit(0); 76 exit(0);
68 return 0; 77 return 0;
69 } 78 }
70 79
71 TEST_F(ProcessUtilTest, KillSlowChild) { 80 TEST_F(ProcessUtilTest, KillSlowChild) {
72 remove("SlowChildProcess.die"); 81 remove("SlowChildProcess.die");
73 ProcessHandle handle = this->SpawnChild(L"SlowChildProcess"); 82 base::ProcessHandle handle = this->SpawnChild(L"SlowChildProcess");
74 ASSERT_NE(base::kNullProcessHandle, handle); 83 ASSERT_NE(base::kNullProcessHandle, handle);
75 FILE *fp = fopen("SlowChildProcess.die", "w"); 84 FILE *fp = fopen("SlowChildProcess.die", "w");
76 fclose(fp); 85 fclose(fp);
77 EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000)); 86 EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000));
78 base::CloseProcessHandle(handle); 87 base::CloseProcessHandle(handle);
79 } 88 }
80 89
81 TEST_F(ProcessUtilTest, DidProcessCrash) { 90 TEST_F(ProcessUtilTest, DidProcessCrash) {
82 remove("SlowChildProcess.die"); 91 remove("SlowChildProcess.die");
83 ProcessHandle handle = this->SpawnChild(L"SlowChildProcess"); 92 base::ProcessHandle handle = this->SpawnChild(L"SlowChildProcess");
84 ASSERT_NE(base::kNullProcessHandle, handle); 93 ASSERT_NE(base::kNullProcessHandle, handle);
85 94
86 bool child_exited = true; 95 bool child_exited = true;
87 EXPECT_FALSE(base::DidProcessCrash(&child_exited, handle)); 96 EXPECT_FALSE(base::DidProcessCrash(&child_exited, handle));
88 EXPECT_FALSE(child_exited); 97 EXPECT_FALSE(child_exited);
89 98
90 FILE *fp = fopen("SlowChildProcess.die", "w"); 99 FILE *fp = fopen("SlowChildProcess.die", "w");
91 fclose(fp); 100 fclose(fp);
92 EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000)); 101 EXPECT_TRUE(base::WaitForSingleProcess(handle, 5000));
93 102
94 EXPECT_FALSE(base::DidProcessCrash(&child_exited, handle)); 103 EXPECT_FALSE(base::DidProcessCrash(&child_exited, handle));
95
96 base::CloseProcessHandle(handle); 104 base::CloseProcessHandle(handle);
97 } 105 }
98 106
99 // Ensure that the priority of a process is restored correctly after 107 // Ensure that the priority of a process is restored correctly after
100 // backgrounding and restoring. 108 // backgrounding and restoring.
101 // Note: a platform may not be willing or able to lower the priority of 109 // Note: a platform may not be willing or able to lower the priority of
102 // a process. The calls to SetProcessBackground should be noops then. 110 // a process. The calls to SetProcessBackground should be noops then.
103 TEST_F(ProcessUtilTest, SetProcessBackgrounded) { 111 TEST_F(ProcessUtilTest, SetProcessBackgrounded) {
104 ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess"); 112 base::ProcessHandle handle = this->SpawnChild(L"SimpleChildProcess");
105 Process process(handle); 113 base::Process process(handle);
106 int old_priority = process.GetPriority(); 114 int old_priority = process.GetPriority();
107 process.SetProcessBackgrounded(true); 115 process.SetProcessBackgrounded(true);
108 process.SetProcessBackgrounded(false); 116 process.SetProcessBackgrounded(false);
109 int new_priority = process.GetPriority(); 117 int new_priority = process.GetPriority();
110 EXPECT_EQ(old_priority, new_priority); 118 EXPECT_EQ(old_priority, new_priority);
111 } 119 }
112 120
113 // TODO(estade): if possible, port these 2 tests. 121 // TODO(estade): if possible, port these 2 tests.
114 #if defined(OS_WIN) 122 #if defined(OS_WIN)
115 TEST_F(ProcessUtilTest, EnableLFH) { 123 TEST_F(ProcessUtilTest, EnableLFH) {
116 ASSERT_TRUE(EnableLowFragmentationHeap()); 124 ASSERT_TRUE(base::EnableLowFragmentationHeap());
117 if (IsDebuggerPresent()) { 125 if (IsDebuggerPresent()) {
118 // Under these conditions, LFH can't be enabled. There's no point to test 126 // Under these conditions, LFH can't be enabled. There's no point to test
119 // anything. 127 // anything.
120 const char* no_debug_env = getenv("_NO_DEBUG_HEAP"); 128 const char* no_debug_env = getenv("_NO_DEBUG_HEAP");
121 if (!no_debug_env || strcmp(no_debug_env, "1")) 129 if (!no_debug_env || strcmp(no_debug_env, "1"))
122 return; 130 return;
123 } 131 }
124 HANDLE heaps[1024] = { 0 }; 132 HANDLE heaps[1024] = { 0 };
125 unsigned number_heaps = GetProcessHeaps(1024, heaps); 133 unsigned number_heaps = GetProcessHeaps(1024, heaps);
126 EXPECT_GT(number_heaps, 0u); 134 EXPECT_GT(number_heaps, 0u);
(...skipping 10 matching lines...) Expand all
137 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not 145 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not
138 // supported on the LFH. 146 // supported on the LFH.
139 147
140 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag. 148 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag.
141 EXPECT_LE(flag, 2u); 149 EXPECT_LE(flag, 2u);
142 EXPECT_NE(flag, 1u); 150 EXPECT_NE(flag, 1u);
143 } 151 }
144 } 152 }
145 153
146 TEST_F(ProcessUtilTest, CalcFreeMemory) { 154 TEST_F(ProcessUtilTest, CalcFreeMemory) {
147 ProcessMetrics* metrics = 155 scoped_ptr<base::ProcessMetrics> metrics(
148 ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()); 156 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()));
149 ASSERT_TRUE(NULL != metrics); 157 ASSERT_TRUE(NULL != metrics.get());
150 158
151 // Typical values here is ~1900 for total and ~1000 for largest. Obviously 159 // Typical values here is ~1900 for total and ~1000 for largest. Obviously
152 // it depends in what other tests have done to this process. 160 // it depends in what other tests have done to this process.
153 FreeMBytes free_mem1 = {0}; 161 base::FreeMBytes free_mem1 = {0};
154 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); 162 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1));
155 EXPECT_LT(10u, free_mem1.total); 163 EXPECT_LT(10u, free_mem1.total);
156 EXPECT_LT(10u, free_mem1.largest); 164 EXPECT_LT(10u, free_mem1.largest);
157 EXPECT_GT(2048u, free_mem1.total); 165 EXPECT_GT(2048u, free_mem1.total);
158 EXPECT_GT(2048u, free_mem1.largest); 166 EXPECT_GT(2048u, free_mem1.largest);
159 EXPECT_GE(free_mem1.total, free_mem1.largest); 167 EXPECT_GE(free_mem1.total, free_mem1.largest);
160 EXPECT_TRUE(NULL != free_mem1.largest_ptr); 168 EXPECT_TRUE(NULL != free_mem1.largest_ptr);
161 169
162 // Allocate 20M and check again. It should have gone down. 170 // Allocate 20M and check again. It should have gone down.
163 const int kAllocMB = 20; 171 const int kAllocMB = 20;
164 char* alloc = new char[kAllocMB * 1024 * 1024]; 172 scoped_array<char> alloc(new char[kAllocMB * 1024 * 1024]);
165 EXPECT_TRUE(NULL != alloc);
166
167 size_t expected_total = free_mem1.total - kAllocMB; 173 size_t expected_total = free_mem1.total - kAllocMB;
168 size_t expected_largest = free_mem1.largest; 174 size_t expected_largest = free_mem1.largest;
169 175
170 FreeMBytes free_mem2 = {0}; 176 base::FreeMBytes free_mem2 = {0};
171 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); 177 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2));
172 EXPECT_GE(free_mem2.total, free_mem2.largest); 178 EXPECT_GE(free_mem2.total, free_mem2.largest);
173 EXPECT_GE(expected_total, free_mem2.total); 179 EXPECT_GE(expected_total, free_mem2.total);
174 EXPECT_GE(expected_largest, free_mem2.largest); 180 EXPECT_GE(expected_largest, free_mem2.largest);
175 EXPECT_TRUE(NULL != free_mem2.largest_ptr); 181 EXPECT_TRUE(NULL != free_mem2.largest_ptr);
176
177 delete[] alloc;
178 delete metrics;
179 } 182 }
180 183
181 TEST_F(ProcessUtilTest, GetAppOutput) { 184 TEST_F(ProcessUtilTest, GetAppOutput) {
182 // Let's create a decently long message. 185 // Let's create a decently long message.
183 std::string message; 186 std::string message;
184 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte 187 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte
185 // boundary. 188 // boundary.
186 message += "Hello!"; 189 message += "Hello!";
187 } 190 }
188 191
(...skipping 25 matching lines...) Expand all
214 base::UserTokenHandle token; 217 base::UserTokenHandle token;
215 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); 218 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token));
216 std::wstring cmdline = 219 std::wstring cmdline =
217 this->MakeCmdLine(L"SimpleChildProcess", false).command_line_string(); 220 this->MakeCmdLine(L"SimpleChildProcess", false).command_line_string();
218 EXPECT_TRUE(base::LaunchAppAsUser(token, cmdline, false, NULL)); 221 EXPECT_TRUE(base::LaunchAppAsUser(token, cmdline, false, NULL));
219 } 222 }
220 223
221 #endif // defined(OS_WIN) 224 #endif // defined(OS_WIN)
222 225
223 #if defined(OS_POSIX) 226 #if defined(OS_POSIX)
227
228 namespace {
229
224 // Returns the maximum number of files that a process can have open. 230 // Returns the maximum number of files that a process can have open.
225 // Returns 0 on error. 231 // Returns 0 on error.
226 int GetMaxFilesOpenInProcess() { 232 int GetMaxFilesOpenInProcess() {
227 struct rlimit rlim; 233 struct rlimit rlim;
228 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { 234 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
229 return 0; 235 return 0;
230 } 236 }
231 237
232 // rlim_t is a uint64 - clip to maxint. We do this since FD #s are ints 238 // rlim_t is a uint64 - clip to maxint. We do this since FD #s are ints
233 // which are all 32 bits on the supported platforms. 239 // which are all 32 bits on the supported platforms.
234 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max()); 240 rlim_t max_int = static_cast<rlim_t>(std::numeric_limits<int32>::max());
235 if (rlim.rlim_cur > max_int) { 241 if (rlim.rlim_cur > max_int) {
236 return max_int; 242 return max_int;
237 } 243 }
238 244
239 return rlim.rlim_cur; 245 return rlim.rlim_cur;
240 } 246 }
241 247
242 const int kChildPipe = 20; // FD # for write end of pipe in child process. 248 const int kChildPipe = 20; // FD # for write end of pipe in child process.
249
250 } // namespace
251
243 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { 252 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) {
244 // This child process counts the number of open FDs, it then writes that 253 // This child process counts the number of open FDs, it then writes that
245 // number out to a pipe connected to the parent. 254 // number out to a pipe connected to the parent.
246 int num_open_files = 0; 255 int num_open_files = 0;
247 int write_pipe = kChildPipe; 256 int write_pipe = kChildPipe;
248 int max_files = GetMaxFilesOpenInProcess(); 257 int max_files = GetMaxFilesOpenInProcess();
249 for (int i = STDERR_FILENO + 1; i < max_files; i++) { 258 for (int i = STDERR_FILENO + 1; i < max_files; i++) {
250 if (i != kChildPipe) { 259 if (i != kChildPipe) {
251 int fd; 260 int fd;
252 if ((fd = HANDLE_EINTR(dup(i))) != -1) { 261 if ((fd = HANDLE_EINTR(dup(i))) != -1) {
(...skipping 10 matching lines...) Expand all
263 DPCHECK(ret == 0); 272 DPCHECK(ret == 0);
264 273
265 return 0; 274 return 0;
266 } 275 }
267 276
268 int ProcessUtilTest::CountOpenFDsInChild() { 277 int ProcessUtilTest::CountOpenFDsInChild() {
269 int fds[2]; 278 int fds[2];
270 if (pipe(fds) < 0) 279 if (pipe(fds) < 0)
271 NOTREACHED(); 280 NOTREACHED();
272 281
273 file_handle_mapping_vector fd_mapping_vec; 282 base::file_handle_mapping_vector fd_mapping_vec;
274 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe)); 283 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe));
275 ProcessHandle handle = this->SpawnChild(L"ProcessUtilsLeakFDChildProcess", 284 base::ProcessHandle handle = this->SpawnChild(
276 fd_mapping_vec, 285 L"ProcessUtilsLeakFDChildProcess", fd_mapping_vec, false);
277 false);
278 CHECK(handle); 286 CHECK(handle);
279 int ret = HANDLE_EINTR(close(fds[1])); 287 int ret = HANDLE_EINTR(close(fds[1]));
280 DPCHECK(ret == 0); 288 DPCHECK(ret == 0);
281 289
282 // Read number of open files in client process from pipe; 290 // Read number of open files in client process from pipe;
283 int num_open_files = -1; 291 int num_open_files = -1;
284 ssize_t bytes_read = 292 ssize_t bytes_read =
285 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files))); 293 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files)));
286 CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); 294 CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files)));
287 295
288 CHECK(WaitForSingleProcess(handle, 1000)); 296 CHECK(base::WaitForSingleProcess(handle, 1000));
289 base::CloseProcessHandle(handle); 297 base::CloseProcessHandle(handle);
290 ret = HANDLE_EINTR(close(fds[0])); 298 ret = HANDLE_EINTR(close(fds[0]));
291 DPCHECK(ret == 0); 299 DPCHECK(ret == 0);
292 300
293 return num_open_files; 301 return num_open_files;
294 } 302 }
295 303
296 TEST_F(ProcessUtilTest, FDRemapping) { 304 TEST_F(ProcessUtilTest, FDRemapping) {
297 int fds_before = CountOpenFDsInChild(); 305 int fds_before = CountOpenFDsInChild();
298 306
299 // open some dummy fds to make sure they don't propogate over to the 307 // open some dummy fds to make sure they don't propagate over to the
300 // child process. 308 // child process.
301 int dev_null = open("/dev/null", O_RDONLY); 309 int dev_null = open("/dev/null", O_RDONLY);
302 int sockets[2]; 310 int sockets[2];
303 socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); 311 socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
304 312
305 int fds_after = CountOpenFDsInChild(); 313 int fds_after = CountOpenFDsInChild();
306 314
307 ASSERT_EQ(fds_after, fds_before); 315 ASSERT_EQ(fds_after, fds_before);
308 316
309 int ret; 317 int ret;
310 ret = HANDLE_EINTR(close(sockets[0])); 318 ret = HANDLE_EINTR(close(sockets[0]));
311 DPCHECK(ret == 0); 319 DPCHECK(ret == 0);
312 ret = HANDLE_EINTR(close(sockets[1])); 320 ret = HANDLE_EINTR(close(sockets[1]));
313 DPCHECK(ret == 0); 321 DPCHECK(ret == 0);
314 ret = HANDLE_EINTR(close(dev_null)); 322 ret = HANDLE_EINTR(close(dev_null));
315 DPCHECK(ret == 0); 323 DPCHECK(ret == 0);
316 } 324 }
317 325
318 static std::string TestLaunchApp(const base::environment_vector& env_changes) { 326 namespace {
327
328 std::string TestLaunchApp(const base::environment_vector& env_changes) {
319 std::vector<std::string> args; 329 std::vector<std::string> args;
320 base::file_handle_mapping_vector fds_to_remap; 330 base::file_handle_mapping_vector fds_to_remap;
321 ProcessHandle handle; 331 base::ProcessHandle handle;
322 332
323 args.push_back("bash"); 333 args.push_back("bash");
324 args.push_back("-c"); 334 args.push_back("-c");
325 args.push_back("echo $BASE_TEST"); 335 args.push_back("echo $BASE_TEST");
326 336
327 int fds[2]; 337 int fds[2];
328 PCHECK(pipe(fds) == 0); 338 PCHECK(pipe(fds) == 0);
329 339
330 fds_to_remap.push_back(std::make_pair(fds[1], 1)); 340 fds_to_remap.push_back(std::make_pair(fds[1], 1));
331 EXPECT_TRUE(LaunchApp(args, env_changes, fds_to_remap, 341 EXPECT_TRUE(base::LaunchApp(args, env_changes, fds_to_remap,
332 true /* wait for exit */, &handle)); 342 true /* wait for exit */, &handle));
333 PCHECK(close(fds[1]) == 0); 343 PCHECK(close(fds[1]) == 0);
334 344
335 char buf[512]; 345 char buf[512];
336 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); 346 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf)));
337 PCHECK(n > 0); 347 PCHECK(n > 0);
338 return std::string(buf, n); 348 return std::string(buf, n);
339 } 349 }
340 350
341 static const char kLargeString[] = 351 const char kLargeString[] =
342 "0123456789012345678901234567890123456789012345678901234567890123456789" 352 "0123456789012345678901234567890123456789012345678901234567890123456789"
343 "0123456789012345678901234567890123456789012345678901234567890123456789" 353 "0123456789012345678901234567890123456789012345678901234567890123456789"
344 "0123456789012345678901234567890123456789012345678901234567890123456789" 354 "0123456789012345678901234567890123456789012345678901234567890123456789"
345 "0123456789012345678901234567890123456789012345678901234567890123456789" 355 "0123456789012345678901234567890123456789012345678901234567890123456789"
346 "0123456789012345678901234567890123456789012345678901234567890123456789" 356 "0123456789012345678901234567890123456789012345678901234567890123456789"
347 "0123456789012345678901234567890123456789012345678901234567890123456789" 357 "0123456789012345678901234567890123456789012345678901234567890123456789"
348 "0123456789012345678901234567890123456789012345678901234567890123456789"; 358 "0123456789012345678901234567890123456789012345678901234567890123456789";
349 359
360 } // namespace
361
350 TEST_F(ProcessUtilTest, LaunchApp) { 362 TEST_F(ProcessUtilTest, LaunchApp) {
351 base::environment_vector env_changes; 363 base::environment_vector env_changes;
352 364
353 env_changes.push_back(std::make_pair(std::string("BASE_TEST"), 365 env_changes.push_back(std::make_pair(std::string("BASE_TEST"),
354 std::string("bar"))); 366 std::string("bar")));
355 EXPECT_EQ("bar\n", TestLaunchApp(env_changes)); 367 EXPECT_EQ("bar\n", TestLaunchApp(env_changes));
356 env_changes.clear(); 368 env_changes.clear();
357 369
358 EXPECT_EQ(0, setenv("BASE_TEST", "testing", 1 /* override */)); 370 EXPECT_EQ(0, setenv("BASE_TEST", "testing", 1 /* override */));
359 EXPECT_EQ("testing\n", TestLaunchApp(env_changes)); 371 EXPECT_EQ("testing\n", TestLaunchApp(env_changes));
360 372
361 env_changes.push_back(std::make_pair(std::string("BASE_TEST"), 373 env_changes.push_back(std::make_pair(std::string("BASE_TEST"),
362 std::string(""))); 374 std::string("")));
363 EXPECT_EQ("\n", TestLaunchApp(env_changes)); 375 EXPECT_EQ("\n", TestLaunchApp(env_changes));
364 376
365 env_changes[0].second = "foo"; 377 env_changes[0].second = "foo";
366 EXPECT_EQ("foo\n", TestLaunchApp(env_changes)); 378 EXPECT_EQ("foo\n", TestLaunchApp(env_changes));
367 379
368 env_changes.clear(); 380 env_changes.clear();
369 EXPECT_EQ(0, setenv("BASE_TEST", kLargeString, 1 /* override */)); 381 EXPECT_EQ(0, setenv("BASE_TEST", kLargeString, 1 /* override */));
370 EXPECT_EQ(std::string(kLargeString) + "\n", TestLaunchApp(env_changes)); 382 EXPECT_EQ(std::string(kLargeString) + "\n", TestLaunchApp(env_changes));
371 383
372 env_changes.push_back(std::make_pair(std::string("BASE_TEST"), 384 env_changes.push_back(std::make_pair(std::string("BASE_TEST"),
373 std::string("wibble"))); 385 std::string("wibble")));
374 EXPECT_EQ("wibble\n", TestLaunchApp(env_changes)); 386 EXPECT_EQ("wibble\n", TestLaunchApp(env_changes));
375 } 387 }
376 388
377 TEST_F(ProcessUtilTest, AlterEnvironment) { 389 TEST_F(ProcessUtilTest, AlterEnvironment) {
378 static const char* empty[] = { NULL }; 390 const char* const empty[] = { NULL };
379 static const char* a2[] = { "A=2", NULL }; 391 const char* const a2[] = { "A=2", NULL };
380 base::environment_vector changes; 392 base::environment_vector changes;
381 char** e; 393 char** e;
382 394
383 e = AlterEnvironment(changes, empty); 395 e = base::AlterEnvironment(changes, empty);
384 EXPECT_TRUE(e[0] == NULL); 396 EXPECT_TRUE(e[0] == NULL);
385 delete[] e; 397 delete[] e;
386 398
387 changes.push_back(std::make_pair(std::string("A"), std::string("1"))); 399 changes.push_back(std::make_pair(std::string("A"), std::string("1")));
388 e = AlterEnvironment(changes, empty); 400 e = base::AlterEnvironment(changes, empty);
389 EXPECT_EQ(std::string("A=1"), e[0]); 401 EXPECT_EQ(std::string("A=1"), e[0]);
390 EXPECT_TRUE(e[1] == NULL); 402 EXPECT_TRUE(e[1] == NULL);
391 delete[] e; 403 delete[] e;
392 404
393 changes.clear(); 405 changes.clear();
394 changes.push_back(std::make_pair(std::string("A"), std::string(""))); 406 changes.push_back(std::make_pair(std::string("A"), std::string("")));
395 e = AlterEnvironment(changes, empty); 407 e = base::AlterEnvironment(changes, empty);
396 EXPECT_TRUE(e[0] == NULL); 408 EXPECT_TRUE(e[0] == NULL);
397 delete[] e; 409 delete[] e;
398 410
399 changes.clear(); 411 changes.clear();
400 e = AlterEnvironment(changes, a2); 412 e = base::AlterEnvironment(changes, a2);
401 EXPECT_EQ(std::string("A=2"), e[0]); 413 EXPECT_EQ(std::string("A=2"), e[0]);
402 EXPECT_TRUE(e[1] == NULL); 414 EXPECT_TRUE(e[1] == NULL);
403 delete[] e; 415 delete[] e;
404 416
405 changes.clear(); 417 changes.clear();
406 changes.push_back(std::make_pair(std::string("A"), std::string("1"))); 418 changes.push_back(std::make_pair(std::string("A"), std::string("1")));
407 e = AlterEnvironment(changes, a2); 419 e = base::AlterEnvironment(changes, a2);
408 EXPECT_EQ(std::string("A=1"), e[0]); 420 EXPECT_EQ(std::string("A=1"), e[0]);
409 EXPECT_TRUE(e[1] == NULL); 421 EXPECT_TRUE(e[1] == NULL);
410 delete[] e; 422 delete[] e;
411 423
412 changes.clear(); 424 changes.clear();
413 changes.push_back(std::make_pair(std::string("A"), std::string(""))); 425 changes.push_back(std::make_pair(std::string("A"), std::string("")));
414 e = AlterEnvironment(changes, a2); 426 e = base::AlterEnvironment(changes, a2);
415 EXPECT_TRUE(e[0] == NULL); 427 EXPECT_TRUE(e[0] == NULL);
416 delete[] e; 428 delete[] e;
417 } 429 }
418 430
419 TEST_F(ProcessUtilTest, GetAppOutput) { 431 TEST_F(ProcessUtilTest, GetAppOutput) {
420 std::string output; 432 std::string output;
421 EXPECT_TRUE(GetAppOutput(CommandLine(FilePath("true")), &output)); 433 EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output));
422 EXPECT_STREQ("", output.c_str()); 434 EXPECT_STREQ("", output.c_str());
423 435
424 EXPECT_FALSE(GetAppOutput(CommandLine(FilePath("false")), &output)); 436 EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath("false")), &output));
425 437
426 std::vector<std::string> argv; 438 std::vector<std::string> argv;
427 argv.push_back("/bin/echo"); 439 argv.push_back("/bin/echo");
428 argv.push_back("-n"); 440 argv.push_back("-n");
429 argv.push_back("foobar42"); 441 argv.push_back("foobar42");
430 EXPECT_TRUE(GetAppOutput(CommandLine(argv), &output)); 442 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
431 EXPECT_STREQ("foobar42", output.c_str()); 443 EXPECT_STREQ("foobar42", output.c_str());
432 } 444 }
433 445
434 TEST_F(ProcessUtilTest, GetAppOutputRestricted) { 446 TEST_F(ProcessUtilTest, GetAppOutputRestricted) {
435 // Unfortunately, since we can't rely on the path, we need to know where 447 // Unfortunately, since we can't rely on the path, we need to know where
436 // everything is. So let's use /bin/sh, which is on every POSIX system, and 448 // everything is. So let's use /bin/sh, which is on every POSIX system, and
437 // its built-ins. 449 // its built-ins.
438 std::vector<std::string> argv; 450 std::vector<std::string> argv;
439 argv.push_back("/bin/sh"); // argv[0] 451 argv.push_back("/bin/sh"); // argv[0]
440 argv.push_back("-c"); // argv[1] 452 argv.push_back("-c"); // argv[1]
441 453
442 // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of 454 // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of
443 // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we 455 // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we
444 // need absolute paths). 456 // need absolute paths).
445 argv.push_back("exit 0"); // argv[2]; equivalent to "true" 457 argv.push_back("exit 0"); // argv[2]; equivalent to "true"
446 std::string output = "abc"; 458 std::string output = "abc";
447 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 100)); 459 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
448 EXPECT_STREQ("", output.c_str()); 460 EXPECT_STREQ("", output.c_str());
449 461
450 // On failure, should not touch |output|. As above, but for |false|. 462 // On failure, should not touch |output|. As above, but for |false|.
451 argv[2] = "exit 1"; // equivalent to "false" 463 argv[2] = "exit 1"; // equivalent to "false"
452 output = "abc"; 464 output = "abc";
453 EXPECT_FALSE(GetAppOutputRestricted(CommandLine(argv), 465 EXPECT_FALSE(base::GetAppOutputRestricted(CommandLine(argv),
454 &output, 100)); 466 &output, 100));
455 EXPECT_STREQ("abc", output.c_str()); 467 EXPECT_STREQ("abc", output.c_str());
456 468
457 // Amount of output exactly equal to space allowed. 469 // Amount of output exactly equal to space allowed.
458 argv[2] = "echo 123456789"; // (the sh built-in doesn't take "-n") 470 argv[2] = "echo 123456789"; // (the sh built-in doesn't take "-n")
459 output.clear(); 471 output.clear();
460 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 10)); 472 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
461 EXPECT_STREQ("123456789\n", output.c_str()); 473 EXPECT_STREQ("123456789\n", output.c_str());
462 474
463 // Amount of output greater than space allowed. 475 // Amount of output greater than space allowed.
464 output.clear(); 476 output.clear();
465 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 5)); 477 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 5));
466 EXPECT_STREQ("12345", output.c_str()); 478 EXPECT_STREQ("12345", output.c_str());
467 479
468 // Amount of output less than space allowed. 480 // Amount of output less than space allowed.
469 output.clear(); 481 output.clear();
470 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 15)); 482 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 15));
471 EXPECT_STREQ("123456789\n", output.c_str()); 483 EXPECT_STREQ("123456789\n", output.c_str());
472 484
473 // Zero space allowed. 485 // Zero space allowed.
474 output = "abc"; 486 output = "abc";
475 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 0)); 487 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 0));
476 EXPECT_STREQ("", output.c_str()); 488 EXPECT_STREQ("", output.c_str());
477 } 489 }
478 490
479 TEST_F(ProcessUtilTest, GetAppOutputRestrictedNoZombies) { 491 TEST_F(ProcessUtilTest, GetAppOutputRestrictedNoZombies) {
480 std::vector<std::string> argv; 492 std::vector<std::string> argv;
481 argv.push_back("/bin/sh"); // argv[0] 493 argv.push_back("/bin/sh"); // argv[0]
482 argv.push_back("-c"); // argv[1] 494 argv.push_back("-c"); // argv[1]
483 argv.push_back("echo 123456789012345678901234567890"); // argv[2] 495 argv.push_back("echo 123456789012345678901234567890"); // argv[2]
484 496
485 // Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS 497 // Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS
486 // 10.5) times with an output buffer big enough to capture all output. 498 // 10.5) times with an output buffer big enough to capture all output.
487 for (int i = 0; i < 300; i++) { 499 for (int i = 0; i < 300; i++) {
488 std::string output; 500 std::string output;
489 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 100)); 501 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 100));
490 EXPECT_STREQ("123456789012345678901234567890\n", output.c_str()); 502 EXPECT_STREQ("123456789012345678901234567890\n", output.c_str());
491 } 503 }
492 504
493 // Ditto, but with an output buffer too small to capture all output. 505 // Ditto, but with an output buffer too small to capture all output.
494 for (int i = 0; i < 300; i++) { 506 for (int i = 0; i < 300; i++) {
495 std::string output; 507 std::string output;
496 EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 10)); 508 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
497 EXPECT_STREQ("1234567890", output.c_str()); 509 EXPECT_STREQ("1234567890", output.c_str());
498 } 510 }
499 } 511 }
500 512
501 #if defined(OS_LINUX) 513 #if defined(OS_LINUX)
502 TEST_F(ProcessUtilTest, GetParentProcessId) { 514 TEST_F(ProcessUtilTest, GetParentProcessId) {
503 base::ProcessId ppid = GetParentProcessId(GetCurrentProcId()); 515 base::ProcessId ppid = base::GetParentProcessId(base::GetCurrentProcId());
504 EXPECT_EQ(ppid, getppid()); 516 EXPECT_EQ(ppid, getppid());
505 } 517 }
506 518
507 TEST_F(ProcessUtilTest, ParseProcStatCPU) { 519 TEST_F(ProcessUtilTest, ParseProcStatCPU) {
508 // /proc/self/stat for a process running "top". 520 // /proc/self/stat for a process running "top".
509 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " 521 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
510 "4202496 471 0 0 0 " 522 "4202496 471 0 0 0 "
511 "12 16 0 0 " // <- These are the goods. 523 "12 16 0 0 " // <- These are the goods.
512 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " 524 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
513 "4246868 140733983044336 18446744073709551615 140244213071219 " 525 "4246868 140733983044336 18446744073709551615 140244213071219 "
514 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; 526 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
515 EXPECT_EQ(12 + 16, ParseProcStatCPU(kTopStat)); 527 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat));
516 528
517 // cat /proc/self/stat on a random other machine I have. 529 // cat /proc/self/stat on a random other machine I have.
518 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " 530 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
519 "0 142 0 0 0 " 531 "0 142 0 0 0 "
520 "0 0 0 0 " // <- No CPU, apparently. 532 "0 0 0 0 " // <- No CPU, apparently.
521 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " 533 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
522 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; 534 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
523 535
524 EXPECT_EQ(0, ParseProcStatCPU(kSelfStat)); 536 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
525 } 537 }
526 #endif 538 #endif
527 539
528 #endif // defined(OS_POSIX) 540 #endif // defined(OS_POSIX)
529 541
530 // TODO(vandebo) make this work on Windows too. 542 // TODO(vandebo) make this work on Windows too.
531 #if !defined(OS_WIN) 543 #if !defined(OS_WIN)
532 544
533 #if defined(USE_TCMALLOC) 545 #if defined(USE_TCMALLOC)
534 extern "C" { 546 extern "C" {
535 int tc_set_new_mode(int mode); 547 int tc_set_new_mode(int mode);
536 } 548 }
537 #endif // defined(USE_TCMALLOC) 549 #endif // defined(USE_TCMALLOC)
538 550
539 class OutOfMemoryTest : public testing::Test { 551 class OutOfMemoryTest : public testing::Test {
540 public: 552 public:
541 OutOfMemoryTest() 553 OutOfMemoryTest()
542 : value_(NULL), 554 : value_(NULL),
543 // Make test size as large as possible minus a few pages so 555 // Make test size as large as possible minus a few pages so
544 // that alignment or other rounding doesn't make it wrap. 556 // that alignment or other rounding doesn't make it wrap.
545 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), 557 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024),
546 signed_test_size_(std::numeric_limits<ssize_t>::max()) { 558 signed_test_size_(std::numeric_limits<ssize_t>::max()) {
547 } 559 }
548 560
549 virtual void SetUp() { 561 virtual void SetUp() {
550 // Must call EnableTerminationOnOutOfMemory() because that is called from 562 // Must call EnableTerminationOnOutOfMemory() because that is called from
551 // chrome's main function and therefore hasn't been called yet. 563 // chrome's main function and therefore hasn't been called yet.
552 EnableTerminationOnOutOfMemory(); 564 base::EnableTerminationOnOutOfMemory();
553 #if defined(USE_TCMALLOC) 565 #if defined(USE_TCMALLOC)
554 tc_set_new_mode(1); 566 tc_set_new_mode(1);
555 } 567 }
556 568
557 virtual void TearDown() { 569 virtual void TearDown() {
558 tc_set_new_mode(0); 570 tc_set_new_mode(0);
559 #endif // defined(USE_TCMALLOC) 571 #endif // defined(USE_TCMALLOC)
560 } 572 }
561 573
562 void* value_; 574 void* value_;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 } 627 }
616 #endif // OS_LINUX 628 #endif // OS_LINUX
617 629
618 #if defined(OS_MACOSX) 630 #if defined(OS_MACOSX)
619 631
620 // Since these allocation functions take a signed size, it's possible that 632 // Since these allocation functions take a signed size, it's possible that
621 // calling them just once won't be enough to exhaust memory. 633 // calling them just once won't be enough to exhaust memory.
622 634
623 TEST_F(OutOfMemoryTest, CFAllocatorSystemDefault) { 635 TEST_F(OutOfMemoryTest, CFAllocatorSystemDefault) {
624 ASSERT_DEATH(while ((value_ = 636 ASSERT_DEATH(while ((value_ =
625 AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}, ""); 637 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}, "");
626 } 638 }
627 639
628 TEST_F(OutOfMemoryTest, CFAllocatorMalloc) { 640 TEST_F(OutOfMemoryTest, CFAllocatorMalloc) {
629 ASSERT_DEATH(while ((value_ = 641 ASSERT_DEATH(while ((value_ =
630 AllocateViaCFAllocatorMalloc(signed_test_size_))) {}, ""); 642 base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {}, "");
631 } 643 }
632 644
633 TEST_F(OutOfMemoryTest, CFAllocatorMallocZone) { 645 TEST_F(OutOfMemoryTest, CFAllocatorMallocZone) {
634 ASSERT_DEATH(while ((value_ = 646 ASSERT_DEATH(while ((value_ =
635 AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}, ""); 647 base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}, "");
636 } 648 }
637 649
638 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) { 650 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) {
639 ASSERT_DEATH(while ((value_ = 651 ASSERT_DEATH(while ((value_ =
640 AllocatePsychoticallyBigObjCObject())) {}, ""); 652 base::AllocatePsychoticallyBigObjCObject())) {}, "");
641 } 653 }
642 654
643 #endif // OS_MACOSX 655 #endif // OS_MACOSX
644 656
645 #endif // !defined(OS_WIN) 657 #endif // !defined(OS_WIN)
646
647 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util_posix.cc ('k') | base/process_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698