OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 EXPECT_TRUE(process.SetProcessBackgrounded(false)); | 346 EXPECT_TRUE(process.SetProcessBackgrounded(false)); |
347 EXPECT_FALSE(process.IsProcessBackgrounded()); | 347 EXPECT_FALSE(process.IsProcessBackgrounded()); |
348 #else | 348 #else |
349 process.SetProcessBackgrounded(true); | 349 process.SetProcessBackgrounded(true); |
350 process.SetProcessBackgrounded(false); | 350 process.SetProcessBackgrounded(false); |
351 #endif | 351 #endif |
352 int new_priority = process.GetPriority(); | 352 int new_priority = process.GetPriority(); |
353 EXPECT_EQ(old_priority, new_priority); | 353 EXPECT_EQ(old_priority, new_priority); |
354 } | 354 } |
355 | 355 |
356 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
357 TEST_F(ProcessUtilTest, GetSystemMemoryInfo) { | |
358 base::SystemMemoryInfoKB info; | |
359 EXPECT_TRUE(base::GetSystemMemoryInfo(&info)); | |
360 | |
361 // Ensure each field received a value. | |
362 EXPECT_GT(info.total, 0); | |
363 EXPECT_GT(info.free, 0); | |
364 EXPECT_GT(info.buffers, 0); | |
365 EXPECT_GT(info.cached, 0); | |
366 EXPECT_GT(info.active_anon, 0); | |
367 EXPECT_GT(info.inactive_anon, 0); | |
368 EXPECT_GT(info.active_file, 0); | |
369 EXPECT_GT(info.inactive_file, 0); | |
370 | |
371 // All the values should be less than the total amount of memory. | |
372 EXPECT_LT(info.free, info.total); | |
373 EXPECT_LT(info.buffers, info.total); | |
374 EXPECT_LT(info.cached, info.total); | |
375 EXPECT_LT(info.active_anon, info.total); | |
376 EXPECT_LT(info.inactive_anon, info.total); | |
377 EXPECT_LT(info.active_file, info.total); | |
378 EXPECT_LT(info.inactive_file, info.total); | |
379 | |
380 #if defined(OS_CHROMEOS) | |
381 // Chrome OS exposes shmem. | |
382 EXPECT_GT(info.shmem, 0); | |
383 EXPECT_LT(info.shmem, info.total); | |
384 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects | |
385 // and gem_size cannot be tested here. | |
386 #endif | |
387 } | |
388 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
389 | |
390 // TODO(estade): if possible, port these 2 tests. | |
391 #if defined(OS_WIN) | 356 #if defined(OS_WIN) |
392 TEST_F(ProcessUtilTest, CalcFreeMemory) { | 357 // TODO(estade): if possible, port this test. |
393 scoped_ptr<base::ProcessMetrics> metrics( | |
394 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); | |
395 ASSERT_TRUE(NULL != metrics.get()); | |
396 | |
397 bool using_tcmalloc = false; | |
398 | |
399 // Detect if we are using tcmalloc | |
400 #if !defined(NO_TCMALLOC) | |
401 const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); | |
402 if (!chrome_allocator || _stricmp(chrome_allocator, "tcmalloc") == 0) | |
403 using_tcmalloc = true; | |
404 #endif | |
405 | |
406 // Typical values here is ~1900 for total and ~1000 for largest. Obviously | |
407 // it depends in what other tests have done to this process. | |
408 base::FreeMBytes free_mem1 = {0}; | |
409 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem1)); | |
410 EXPECT_LT(10u, free_mem1.total); | |
411 EXPECT_LT(10u, free_mem1.largest); | |
412 EXPECT_GT(2048u, free_mem1.total); | |
413 EXPECT_GT(2048u, free_mem1.largest); | |
414 EXPECT_GE(free_mem1.total, free_mem1.largest); | |
415 EXPECT_TRUE(NULL != free_mem1.largest_ptr); | |
416 | |
417 // Allocate 20M and check again. It should have gone down. | |
418 const int kAllocMB = 20; | |
419 scoped_ptr<char[]> alloc(new char[kAllocMB * 1024 * 1024]); | |
420 size_t expected_total = free_mem1.total - kAllocMB; | |
421 size_t expected_largest = free_mem1.largest; | |
422 | |
423 base::FreeMBytes free_mem2 = {0}; | |
424 EXPECT_TRUE(metrics->CalculateFreeMemory(&free_mem2)); | |
425 EXPECT_GE(free_mem2.total, free_mem2.largest); | |
426 // This test is flaky when using tcmalloc, because tcmalloc | |
427 // allocation strategy sometimes results in less than the | |
428 // full drop of 20Mb of free memory. | |
429 if (!using_tcmalloc) | |
430 EXPECT_GE(expected_total, free_mem2.total); | |
431 EXPECT_GE(expected_largest, free_mem2.largest); | |
432 EXPECT_TRUE(NULL != free_mem2.largest_ptr); | |
433 } | |
434 | |
435 TEST_F(ProcessUtilTest, GetAppOutput) { | 358 TEST_F(ProcessUtilTest, GetAppOutput) { |
436 // Let's create a decently long message. | 359 // Let's create a decently long message. |
437 std::string message; | 360 std::string message; |
438 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte | 361 for (int i = 0; i < 1025; i++) { // 1025 so it does not end on a kilo-byte |
439 // boundary. | 362 // boundary. |
440 message += "Hello!"; | 363 message += "Hello!"; |
441 } | 364 } |
442 // cmd.exe's echo always adds a \r\n to its output. | 365 // cmd.exe's echo always adds a \r\n to its output. |
443 std::string expected(message); | 366 std::string expected(message); |
444 expected += "\r\n"; | 367 expected += "\r\n"; |
445 | 368 |
446 FilePath cmd(L"cmd.exe"); | 369 FilePath cmd(L"cmd.exe"); |
447 CommandLine cmd_line(cmd); | 370 CommandLine cmd_line(cmd); |
448 cmd_line.AppendArg("/c"); | 371 cmd_line.AppendArg("/c"); |
449 cmd_line.AppendArg("echo " + message + ""); | 372 cmd_line.AppendArg("echo " + message + ""); |
450 std::string output; | 373 std::string output; |
451 ASSERT_TRUE(base::GetAppOutput(cmd_line, &output)); | 374 ASSERT_TRUE(base::GetAppOutput(cmd_line, &output)); |
452 EXPECT_EQ(expected, output); | 375 EXPECT_EQ(expected, output); |
453 | 376 |
454 // Let's make sure stderr is ignored. | 377 // Let's make sure stderr is ignored. |
455 CommandLine other_cmd_line(cmd); | 378 CommandLine other_cmd_line(cmd); |
456 other_cmd_line.AppendArg("/c"); | 379 other_cmd_line.AppendArg("/c"); |
457 // http://msdn.microsoft.com/library/cc772622.aspx | 380 // http://msdn.microsoft.com/library/cc772622.aspx |
458 cmd_line.AppendArg("echo " + message + " >&2"); | 381 cmd_line.AppendArg("echo " + message + " >&2"); |
459 output.clear(); | 382 output.clear(); |
460 ASSERT_TRUE(base::GetAppOutput(other_cmd_line, &output)); | 383 ASSERT_TRUE(base::GetAppOutput(other_cmd_line, &output)); |
461 EXPECT_EQ("", output); | 384 EXPECT_EQ("", output); |
462 } | 385 } |
463 | 386 |
| 387 // TODO(estade): if possible, port this test. |
464 TEST_F(ProcessUtilTest, LaunchAsUser) { | 388 TEST_F(ProcessUtilTest, LaunchAsUser) { |
465 base::UserTokenHandle token; | 389 base::UserTokenHandle token; |
466 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); | 390 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); |
467 std::wstring cmdline = | 391 std::wstring cmdline = |
468 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); | 392 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); |
469 base::LaunchOptions options; | 393 base::LaunchOptions options; |
470 options.as_user = token; | 394 options.as_user = token; |
471 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); | 395 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); |
472 } | 396 } |
473 | 397 |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 &exit_code)); | 745 &exit_code)); |
822 EXPECT_STREQ("foo\n", output.c_str()); | 746 EXPECT_STREQ("foo\n", output.c_str()); |
823 EXPECT_EQ(exit_code, 2); | 747 EXPECT_EQ(exit_code, 2); |
824 } | 748 } |
825 | 749 |
826 TEST_F(ProcessUtilTest, GetParentProcessId) { | 750 TEST_F(ProcessUtilTest, GetParentProcessId) { |
827 base::ProcessId ppid = base::GetParentProcessId(base::GetCurrentProcId()); | 751 base::ProcessId ppid = base::GetParentProcessId(base::GetCurrentProcId()); |
828 EXPECT_EQ(ppid, getppid()); | 752 EXPECT_EQ(ppid, getppid()); |
829 } | 753 } |
830 | 754 |
831 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
832 TEST_F(ProcessUtilTest, ParseProcStatCPU) { | |
833 // /proc/self/stat for a process running "top". | |
834 const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 " | |
835 "4202496 471 0 0 0 " | |
836 "12 16 0 0 " // <- These are the goods. | |
837 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 " | |
838 "4246868 140733983044336 18446744073709551615 140244213071219 " | |
839 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0"; | |
840 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat)); | |
841 | |
842 // cat /proc/self/stat on a random other machine I have. | |
843 const char kSelfStat[] = "5364 (cat) R 5354 5364 5354 34819 5364 " | |
844 "0 142 0 0 0 " | |
845 "0 0 0 0 " // <- No CPU, apparently. | |
846 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 " | |
847 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; | |
848 | |
849 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); | |
850 } | |
851 | |
852 // Disable on Android because base_unittests runs inside a Dalvik VM that | |
853 // starts and stop threads (crbug.com/175563). | |
854 #if !defined(OS_ANDROID) | |
855 TEST_F(ProcessUtilTest, GetNumberOfThreads) { | |
856 const base::ProcessHandle current = base::GetCurrentProcessHandle(); | |
857 const int initial_threads = base::GetNumberOfThreads(current); | |
858 ASSERT_GT(initial_threads, 0); | |
859 const int kNumAdditionalThreads = 10; | |
860 { | |
861 scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; | |
862 for (int i = 0; i < kNumAdditionalThreads; ++i) { | |
863 my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); | |
864 my_threads[i]->Start(); | |
865 ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); | |
866 } | |
867 } | |
868 // The Thread destructor will stop them. | |
869 ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); | |
870 } | |
871 #endif // !defined(OS_ANDROID) | |
872 | |
873 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
874 | |
875 // TODO(port): port those unit tests. | 755 // TODO(port): port those unit tests. |
876 bool IsProcessDead(base::ProcessHandle child) { | 756 bool IsProcessDead(base::ProcessHandle child) { |
877 // waitpid() will actually reap the process which is exactly NOT what we | 757 // waitpid() will actually reap the process which is exactly NOT what we |
878 // want to test for. The good thing is that if it can't find the process | 758 // want to test for. The good thing is that if it can't find the process |
879 // we'll get a nice value for errno which we can test for. | 759 // we'll get a nice value for errno which we can test for. |
880 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); | 760 const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG)); |
881 return result == -1 && errno == ECHILD; | 761 return result == -1 && errno == ECHILD; |
882 } | 762 } |
883 | 763 |
884 TEST_F(ProcessUtilTest, DelayedTermination) { | 764 TEST_F(ProcessUtilTest, DelayedTermination) { |
(...skipping 26 matching lines...) Expand all Loading... |
911 // Check that process was really killed. | 791 // Check that process was really killed. |
912 EXPECT_TRUE(IsProcessDead(child_process)); | 792 EXPECT_TRUE(IsProcessDead(child_process)); |
913 base::CloseProcessHandle(child_process); | 793 base::CloseProcessHandle(child_process); |
914 } | 794 } |
915 | 795 |
916 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 796 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
917 return 0; | 797 return 0; |
918 } | 798 } |
919 | 799 |
920 #endif // defined(OS_POSIX) | 800 #endif // defined(OS_POSIX) |
OLD | NEW |