| OLD | NEW |
| 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 #include "chrome/browser/process_info_snapshot.h" | 5 #include "chrome/browser/process_info_snapshot.h" |
| 6 | 6 |
| 7 #include <sys/types.h> // For |uid_t| (and |pid_t|). | 7 #include <sys/types.h> // For |uid_t| (and |pid_t|). |
| 8 #include <unistd.h> // For |getpid()|, |getuid()|, etc. | 8 #include <unistd.h> // For |getpid()|, |getuid()|, etc. |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/eintr_wrapper.h" |
| 13 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/logging.h" |
| 14 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 15 | 17 |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 typedef testing::Test ProcessInfoSnapshotMacTest; | 20 typedef testing::Test ProcessInfoSnapshotMacTest; |
| 19 | 21 |
| 20 TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) { | 22 TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) { |
| 21 // Sample process with PID 1, which should exist and presumably belong to | 23 // Sample process with PID 1, which should exist and presumably belong to |
| 22 // root. | 24 // root. |
| 23 std::vector<base::ProcessId> pid_list; | 25 std::vector<base::ProcessId> pid_list; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 ProcessInfoSnapshot::ProcInfoEntry proc_info; | 72 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
| 71 ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info)); | 73 ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info)); |
| 72 EXPECT_EQ(pid, proc_info.pid); | 74 EXPECT_EQ(pid, proc_info.pid); |
| 73 EXPECT_EQ(ppid, proc_info.ppid); | 75 EXPECT_EQ(ppid, proc_info.ppid); |
| 74 EXPECT_EQ(uid, proc_info.uid); | 76 EXPECT_EQ(uid, proc_info.uid); |
| 75 EXPECT_EQ(euid, proc_info.euid); | 77 EXPECT_EQ(euid, proc_info.euid); |
| 76 EXPECT_GE(proc_info.rss, 100u); // Sanity check: we're running, so we | 78 EXPECT_GE(proc_info.rss, 100u); // Sanity check: we're running, so we |
| 77 // should occupy at least 100 kilobytes. | 79 // should occupy at least 100 kilobytes. |
| 78 EXPECT_GE(proc_info.vsize, 1024u); // Sanity check: our |vsize| is presumably | 80 EXPECT_GE(proc_info.vsize, 1024u); // Sanity check: our |vsize| is presumably |
| 79 // at least a megabyte. | 81 // at least a megabyte. |
| 82 EXPECT_GE(proc_info.rshrd, 1024u); // Shared memory should also > 1 MB. |
| 83 EXPECT_GE(proc_info.rprvt, 1024u); // Same with private memory. |
| 80 | 84 |
| 81 // Find our parent. | 85 // Find our parent. |
| 82 ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info)); | 86 ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info)); |
| 83 EXPECT_EQ(ppid, proc_info.pid); | 87 EXPECT_EQ(ppid, proc_info.pid); |
| 84 EXPECT_NE(static_cast<int64>(proc_info.ppid), 0); | 88 EXPECT_NE(static_cast<int64>(proc_info.ppid), 0); |
| 85 EXPECT_EQ(uid, proc_info.uid); // This (and the following) should be true | 89 EXPECT_EQ(uid, proc_info.uid); // This (and the following) should be true |
| 86 EXPECT_EQ(euid, proc_info.euid); // under reasonable circumstances. | 90 EXPECT_EQ(euid, proc_info.euid); // under reasonable circumstances. |
| 87 // Can't say anything definite about its |rss|. | 91 // Can't say anything definite about its |rss|. |
| 88 EXPECT_GT(proc_info.vsize, 0u); // Its |vsize| should be nonzero though. | 92 EXPECT_GT(proc_info.vsize, 0u); // Its |vsize| should be nonzero though. |
| 89 } | 93 } |
| 90 | 94 |
| 95 // To verify that ProcessInfoSnapshot is getting the actual uid and effective |
| 96 // uid, this test runs top. top should have a uid of the caller and effective |
| 97 // uid of 0 (root). |
| 91 TEST_F(ProcessInfoSnapshotMacTest, EffectiveVsRealUserIDTest) { | 98 TEST_F(ProcessInfoSnapshotMacTest, EffectiveVsRealUserIDTest) { |
| 92 // Run top which has a uid of the caller and effective uid of 0. | 99 // Create a pipe to be able to read top's output. |
| 100 int fds[2]; |
| 101 PCHECK(pipe(fds) == 0); |
| 102 base::file_handle_mapping_vector fds_to_remap; |
| 103 fds_to_remap.push_back(std::make_pair(fds[1], 1)); |
| 104 |
| 105 // Hook up top's stderr to the test process' stderr. |
| 106 fds_to_remap.push_back(std::make_pair(fileno(stderr), 2)); |
| 107 |
| 108 std::vector<std::string> argv; |
| 109 argv.push_back("/usr/bin/top"); |
| 110 argv.push_back("-l"); |
| 111 argv.push_back("0"); |
| 112 |
| 93 base::ProcessHandle process_handle; | 113 base::ProcessHandle process_handle; |
| 94 ASSERT_TRUE(base::LaunchApp(CommandLine(FilePath("/usr/bin/top")), | 114 ASSERT_TRUE(base::LaunchApp(argv, fds_to_remap, false, &process_handle)); |
| 95 false, false, &process_handle)); | 115 PCHECK(HANDLE_EINTR(close(fds[1])) == 0); |
| 116 |
| 117 // Wait until there's some output form top. This is an easy way to tell that |
| 118 // the exec() call is done and top is actually running. |
| 119 char buf[1]; |
| 120 PCHECK(HANDLE_EINTR(read(fds[0], buf, 1)) == 1); |
| 96 | 121 |
| 97 std::vector<base::ProcessId> pid_list; | 122 std::vector<base::ProcessId> pid_list; |
| 98 pid_list.push_back(process_handle); | 123 pid_list.push_back(process_handle); |
| 99 ProcessInfoSnapshot snapshot; | 124 ProcessInfoSnapshot snapshot; |
| 100 ASSERT_TRUE(snapshot.Sample(pid_list)); | 125 ASSERT_TRUE(snapshot.Sample(pid_list)); |
| 101 | 126 |
| 102 ProcessInfoSnapshot::ProcInfoEntry proc_info; | 127 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
| 103 ASSERT_TRUE(snapshot.GetProcInfo(process_handle, &proc_info)); | 128 ASSERT_TRUE(snapshot.GetProcInfo(process_handle, &proc_info)); |
| 104 // Effective user ID should be 0 (root). | 129 // Effective user ID should be 0 (root). |
| 105 EXPECT_EQ(proc_info.euid, 0u); | 130 EXPECT_EQ(proc_info.euid, 0u); |
| 106 // Real user ID should match the calling process's user id. | 131 // Real user ID should match the calling process's user id. |
| 107 EXPECT_EQ(proc_info.uid, geteuid()); | 132 EXPECT_EQ(proc_info.uid, geteuid()); |
| 108 | 133 |
| 109 ASSERT_TRUE(base::KillProcess(process_handle, 0, true)); | 134 ASSERT_TRUE(base::KillProcess(process_handle, 0, true)); |
| 135 PCHECK(HANDLE_EINTR(close(fds[0])) == 0); |
| 110 } | 136 } |
| OLD | NEW |