This source file includes following definitions.
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/process_info_snapshot.h"
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/process/process_metrics.h"
#include "testing/gtest/include/gtest/gtest.h"
typedef testing::Test ProcessInfoSnapshotMacTest;
TEST_F(ProcessInfoSnapshotMacTest, FindPidOneTest) {
std::vector<base::ProcessId> pid_list;
pid_list.push_back(1);
ProcessInfoSnapshot snapshot;
ASSERT_TRUE(snapshot.Sample(pid_list));
ProcessInfoSnapshot::ProcInfoEntry proc_info;
ASSERT_TRUE(snapshot.GetProcInfo(1, &proc_info));
EXPECT_EQ(1, static_cast<int64>(proc_info.pid));
EXPECT_EQ(0, static_cast<int64>(proc_info.ppid));
EXPECT_EQ(0, static_cast<int64>(proc_info.uid));
EXPECT_EQ(0, static_cast<int64>(proc_info.euid));
EXPECT_GE(proc_info.rss, 0u);
EXPECT_GT(proc_info.vsize, 0u);
base::CommittedKBytes usage;
EXPECT_TRUE(snapshot.GetCommittedKBytesOfPID(1, &usage));
base::WorkingSetKBytes ws_usage;
EXPECT_TRUE(snapshot.GetWorkingSetKBytesOfPID(1, &ws_usage));
EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info));
EXPECT_FALSE(snapshot.GetProcInfo(2, &proc_info));
snapshot.Reset();
EXPECT_FALSE(snapshot.GetProcInfo(1, &proc_info));
}
TEST_F(ProcessInfoSnapshotMacTest, FindPidSelfTest) {
base::ProcessId pid = static_cast<base::ProcessId>(getpid());
base::ProcessId ppid = static_cast<base::ProcessId>(getppid());
uid_t uid = getuid();
uid_t euid = geteuid();
EXPECT_NE(static_cast<int64>(ppid), 0);
std::vector<base::ProcessId> pid_list;
pid_list.push_back(pid);
pid_list.push_back(ppid);
ProcessInfoSnapshot snapshot;
ASSERT_TRUE(snapshot.Sample(pid_list));
ProcessInfoSnapshot::ProcInfoEntry proc_info;
ASSERT_TRUE(snapshot.GetProcInfo(pid, &proc_info));
EXPECT_EQ(pid, proc_info.pid);
EXPECT_EQ(ppid, proc_info.ppid);
EXPECT_EQ(uid, proc_info.uid);
EXPECT_EQ(euid, proc_info.euid);
EXPECT_GE(proc_info.rss, 100u);
EXPECT_GE(proc_info.vsize, 1024u);
EXPECT_GE(proc_info.rshrd, 1024u);
EXPECT_GE(proc_info.rprvt, 1024u);
ASSERT_TRUE(snapshot.GetProcInfo(ppid, &proc_info));
EXPECT_EQ(ppid, proc_info.pid);
EXPECT_NE(static_cast<int64>(proc_info.ppid), 0);
EXPECT_EQ(uid, proc_info.uid);
EXPECT_EQ(euid, proc_info.euid);
EXPECT_GT(proc_info.vsize, 0u);
}
TEST_F(ProcessInfoSnapshotMacTest, EffectiveVsRealUserIDTest) {
int fds[2];
PCHECK(pipe(fds) == 0);
base::FileHandleMappingVector fds_to_remap;
fds_to_remap.push_back(std::make_pair(fds[1], 1));
fds_to_remap.push_back(std::make_pair(fileno(stderr), 2));
std::vector<std::string> argv;
argv.push_back("/usr/bin/top");
argv.push_back("-l");
argv.push_back("0");
base::ProcessHandle process_handle;
base::LaunchOptions options;
options.fds_to_remap = &fds_to_remap;
ASSERT_TRUE(base::LaunchProcess(argv, options, &process_handle));
PCHECK(IGNORE_EINTR(close(fds[1])) == 0);
char buf[1];
PCHECK(HANDLE_EINTR(read(fds[0], buf, 1)) == 1);
std::vector<base::ProcessId> pid_list;
pid_list.push_back(process_handle);
ProcessInfoSnapshot snapshot;
ASSERT_TRUE(snapshot.Sample(pid_list));
ProcessInfoSnapshot::ProcInfoEntry proc_info;
ASSERT_TRUE(snapshot.GetProcInfo(process_handle, &proc_info));
EXPECT_EQ(proc_info.euid, 0u);
EXPECT_EQ(proc_info.uid, geteuid());
ASSERT_TRUE(base::KillProcess(process_handle, 0, true));
PCHECK(IGNORE_EINTR(close(fds[0])) == 0);
}