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 #include "chrome/browser/process_info_snapshot.h" | 5 #include "chrome/browser/process_info_snapshot.h" |
6 | 6 |
| 7 #include <stdint.h> |
7 #include <sys/types.h> // For |uid_t| (and |pid_t|). | 8 #include <sys/types.h> // For |uid_t| (and |pid_t|). |
8 #include <unistd.h> // For |getpid()|, |getuid()|, etc. | 9 #include <unistd.h> // For |getpid()|, |getuid()|, etc. |
9 | 10 |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/mac/mac_util.h" | 16 #include "base/mac/mac_util.h" |
16 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
17 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
18 #include "base/process/process.h" | 19 #include "base/process/process.h" |
19 #include "base/process/process_metrics.h" | 20 #include "base/process/process_metrics.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 typedef testing::Test ProcessInfoSnapshotMacTest; | 23 typedef testing::Test ProcessInfoSnapshotMacTest; |
23 | 24 |
24 TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) { | 25 TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) { |
25 // Sample process with PID 1, which should exist and presumably belong to | 26 // Sample process with PID 1, which should exist and presumably belong to |
26 // root. | 27 // root. |
27 std::vector<base::ProcessId> pid_list; | 28 std::vector<base::ProcessId> pid_list; |
28 pid_list.push_back(1); | 29 pid_list.push_back(1); |
29 ProcessInfoSnapshot snapshot; | 30 ProcessInfoSnapshot snapshot; |
30 ASSERT_TRUE(snapshot.Sample(pid_list)); | 31 ASSERT_TRUE(snapshot.Sample(pid_list)); |
31 | 32 |
32 ProcessInfoSnapshot::ProcInfoEntry proc_info; | 33 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
33 ASSERT_TRUE(snapshot.GetProcInfo(1, &proc_info)); | 34 ASSERT_TRUE(snapshot.GetProcInfo(1, &proc_info)); |
34 EXPECT_EQ(1, static_cast<int64>(proc_info.pid)); | 35 EXPECT_EQ(1, static_cast<int64_t>(proc_info.pid)); |
35 EXPECT_EQ(0, static_cast<int64>(proc_info.ppid)); | 36 EXPECT_EQ(0, static_cast<int64_t>(proc_info.ppid)); |
36 EXPECT_EQ(0, static_cast<int64>(proc_info.uid)); | 37 EXPECT_EQ(0, static_cast<int64_t>(proc_info.uid)); |
37 EXPECT_EQ(0, static_cast<int64>(proc_info.euid)); | 38 EXPECT_EQ(0, static_cast<int64_t>(proc_info.euid)); |
38 EXPECT_GE(proc_info.rss, 0u); | 39 EXPECT_GE(proc_info.rss, 0u); |
39 EXPECT_GT(proc_info.vsize, 0u); | 40 EXPECT_GT(proc_info.vsize, 0u); |
40 | 41 |
41 // Try out the |Get...OfPID()|, but don't examine the results, since they | 42 // Try out the |Get...OfPID()|, but don't examine the results, since they |
42 // depend on how we map |ProcInfoEntry| to |...KBytes|. | 43 // depend on how we map |ProcInfoEntry| to |...KBytes|. |
43 base::CommittedKBytes usage; | 44 base::CommittedKBytes usage; |
44 EXPECT_TRUE(snapshot.GetCommittedKBytesOfPID(1, &usage)); | 45 EXPECT_TRUE(snapshot.GetCommittedKBytesOfPID(1, &usage)); |
45 base::WorkingSetKBytes ws_usage; | 46 base::WorkingSetKBytes ws_usage; |
46 EXPECT_TRUE(snapshot.GetWorkingSetKBytesOfPID(1, &ws_usage)); | 47 EXPECT_TRUE(snapshot.GetWorkingSetKBytesOfPID(1, &ws_usage)); |
47 | 48 |
48 // Make sure it hasn't picked up some other PID (say, 2). | 49 // Make sure it hasn't picked up some other PID (say, 2). |
49 EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info)); | 50 EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info)); |
50 | 51 |
51 // Make sure PID 2 still isn't there (in case I mess up my use of std::map). | 52 // Make sure PID 2 still isn't there (in case I mess up my use of std::map). |
52 EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info)); | 53 EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info)); |
53 | 54 |
54 // Test |Reset()|. | 55 // Test |Reset()|. |
55 snapshot.Reset(); | 56 snapshot.Reset(); |
56 EXPECT_FALSE(snapshot.GetProcInfo(1, &proc_info)); | 57 EXPECT_FALSE(snapshot.GetProcInfo(1, &proc_info)); |
57 } | 58 } |
58 | 59 |
59 TEST_F(ProcessInfoSnapshotMacTest, FindPidSelfTest) { | 60 TEST_F(ProcessInfoSnapshotMacTest, FindPidSelfTest) { |
60 // Sample this process and its parent. | 61 // Sample this process and its parent. |
61 base::ProcessId pid = static_cast<base::ProcessId>(getpid()); | 62 base::ProcessId pid = static_cast<base::ProcessId>(getpid()); |
62 base::ProcessId ppid = static_cast<base::ProcessId>(getppid()); | 63 base::ProcessId ppid = static_cast<base::ProcessId>(getppid()); |
63 uid_t uid = getuid(); | 64 uid_t uid = getuid(); |
64 uid_t euid = geteuid(); | 65 uid_t euid = geteuid(); |
65 EXPECT_NE(static_cast<int64>(ppid), 0); | 66 EXPECT_NE(static_cast<int64_t>(ppid), 0); |
66 | 67 |
67 std::vector<base::ProcessId> pid_list; | 68 std::vector<base::ProcessId> pid_list; |
68 pid_list.push_back(pid); | 69 pid_list.push_back(pid); |
69 pid_list.push_back(ppid); | 70 pid_list.push_back(ppid); |
70 ProcessInfoSnapshot snapshot; | 71 ProcessInfoSnapshot snapshot; |
71 ASSERT_TRUE(snapshot.Sample(pid_list)); | 72 ASSERT_TRUE(snapshot.Sample(pid_list)); |
72 | 73 |
73 // Find our process. | 74 // Find our process. |
74 ProcessInfoSnapshot::ProcInfoEntry proc_info; | 75 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
75 ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info)); | 76 ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info)); |
(...skipping 11 matching lines...) Expand all Loading... |
87 if (!base::mac::IsOSMavericksOrLater()) { | 88 if (!base::mac::IsOSMavericksOrLater()) { |
88 // Shared memory should also > 1 MB. | 89 // Shared memory should also > 1 MB. |
89 EXPECT_GE(proc_info.rshrd, 1024u); | 90 EXPECT_GE(proc_info.rshrd, 1024u); |
90 // Same with private memory. | 91 // Same with private memory. |
91 EXPECT_GE(proc_info.rprvt, 1024u); | 92 EXPECT_GE(proc_info.rprvt, 1024u); |
92 } | 93 } |
93 | 94 |
94 // Find our parent. | 95 // Find our parent. |
95 ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info)); | 96 ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info)); |
96 EXPECT_EQ(ppid, proc_info.pid); | 97 EXPECT_EQ(ppid, proc_info.pid); |
97 EXPECT_NE(static_cast<int64>(proc_info.ppid), 0); | 98 EXPECT_NE(static_cast<int64_t>(proc_info.ppid), 0); |
98 EXPECT_EQ(uid, proc_info.uid); // This (and the following) should be true | 99 EXPECT_EQ(uid, proc_info.uid); // This (and the following) should be true |
99 EXPECT_EQ(euid, proc_info.euid); // under reasonable circumstances. | 100 EXPECT_EQ(euid, proc_info.euid); // under reasonable circumstances. |
100 // Can't say anything definite about its |rss|. | 101 // Can't say anything definite about its |rss|. |
101 EXPECT_GT(proc_info.vsize, 0u); // Its |vsize| should be nonzero though. | 102 EXPECT_GT(proc_info.vsize, 0u); // Its |vsize| should be nonzero though. |
102 } | 103 } |
103 | 104 |
104 // To verify that ProcessInfoSnapshot is getting the actual uid and effective | 105 // To verify that ProcessInfoSnapshot is getting the actual uid and effective |
105 // uid, this test runs top. top should have a uid of the caller and effective | 106 // uid, this test runs top. top should have a uid of the caller and effective |
106 // uid of 0 (root). | 107 // uid of 0 (root). |
107 TEST_F(ProcessInfoSnapshotMacTest, EffectiveVsRealUserIDTest) { | 108 TEST_F(ProcessInfoSnapshotMacTest, EffectiveVsRealUserIDTest) { |
(...skipping 30 matching lines...) Expand all Loading... |
138 ProcessInfoSnapshot::ProcInfoEntry proc_info; | 139 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
139 ASSERT_TRUE(snapshot.GetProcInfo(process.Pid(), &proc_info)); | 140 ASSERT_TRUE(snapshot.GetProcInfo(process.Pid(), &proc_info)); |
140 // Effective user ID should be 0 (root). | 141 // Effective user ID should be 0 (root). |
141 EXPECT_EQ(proc_info.euid, 0u); | 142 EXPECT_EQ(proc_info.euid, 0u); |
142 // Real user ID should match the calling process's user id. | 143 // Real user ID should match the calling process's user id. |
143 EXPECT_EQ(proc_info.uid, geteuid()); | 144 EXPECT_EQ(proc_info.uid, geteuid()); |
144 | 145 |
145 ASSERT_TRUE(process.Terminate(0, true)); | 146 ASSERT_TRUE(process.Terminate(0, true)); |
146 PCHECK(IGNORE_EINTR(close(fds[0])) == 0); | 147 PCHECK(IGNORE_EINTR(close(fds[0])) == 0); |
147 } | 148 } |
OLD | NEW |