OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/posix/process_info.h" |
| 16 |
| 17 #include <time.h> |
| 18 #include <unistd.h> |
| 19 |
| 20 #include <set> |
| 21 #include <string> |
| 22 #include <vector> |
| 23 |
| 24 #include "base/basictypes.h" |
| 25 #include "build/build_config.h" |
| 26 #include "gtest/gtest.h" |
| 27 #include "util/test/errors.h" |
| 28 |
| 29 #if defined(OS_MACOSX) |
| 30 #include <crt_externs.h> |
| 31 #endif |
| 32 |
| 33 namespace crashpad { |
| 34 namespace test { |
| 35 namespace { |
| 36 |
| 37 void TestSelfProcess(const ProcessInfo& process_info) { |
| 38 EXPECT_EQ(getpid(), process_info.ProcessID()); |
| 39 EXPECT_EQ(getppid(), process_info.ParentProcessID()); |
| 40 |
| 41 // There’s no system call to obtain the saved set-user ID or saved set-group |
| 42 // ID in an easy way. Normally, they are the same as the effective user ID and |
| 43 // effective group ID, so just check against those. |
| 44 EXPECT_EQ(getuid(), process_info.RealUserID()); |
| 45 const uid_t euid = geteuid(); |
| 46 EXPECT_EQ(euid, process_info.EffectiveUserID()); |
| 47 EXPECT_EQ(euid, process_info.SavedUserID()); |
| 48 const gid_t gid = getgid(); |
| 49 EXPECT_EQ(gid, process_info.RealGroupID()); |
| 50 const gid_t egid = getegid(); |
| 51 EXPECT_EQ(egid, process_info.EffectiveGroupID()); |
| 52 EXPECT_EQ(egid, process_info.SavedGroupID()); |
| 53 |
| 54 // Test SupplementaryGroups(). |
| 55 int group_count = getgroups(0, nullptr); |
| 56 ASSERT_GE(group_count, 0) << ErrnoMessage("getgroups"); |
| 57 |
| 58 std::vector<gid_t> group_vector(group_count); |
| 59 if (group_count > 0) { |
| 60 group_count = getgroups(group_vector.size(), &group_vector[0]); |
| 61 ASSERT_GE(group_count, 0) << ErrnoMessage("getgroups"); |
| 62 ASSERT_EQ(group_vector.size(), implicit_cast<size_t>(group_count)); |
| 63 } |
| 64 |
| 65 std::set<gid_t> group_set(group_vector.begin(), group_vector.end()); |
| 66 EXPECT_EQ(group_set, process_info.SupplementaryGroups()); |
| 67 |
| 68 // Test AllGroups(), which is SupplementaryGroups() plus the real, effective, |
| 69 // and saved set-group IDs. The effective and saved set-group IDs are expected |
| 70 // to be identical (see above). |
| 71 group_set.insert(gid); |
| 72 group_set.insert(egid); |
| 73 |
| 74 EXPECT_EQ(group_set, process_info.AllGroups()); |
| 75 |
| 76 // The test executable isn’t expected to change privileges. |
| 77 EXPECT_FALSE(process_info.DidChangePrivileges()); |
| 78 |
| 79 #if defined(ARCH_CPU_64_BITS) |
| 80 EXPECT_TRUE(process_info.Is64Bit()); |
| 81 #else |
| 82 EXPECT_FALSE(process_info.Is64Bit()); |
| 83 #endif |
| 84 |
| 85 // Test StartTime(). This program must have started at some time in the past. |
| 86 timeval start_time; |
| 87 process_info.StartTime(&start_time); |
| 88 time_t now; |
| 89 time(&now); |
| 90 EXPECT_LE(start_time.tv_sec, now); |
| 91 |
| 92 std::vector<std::string> argv; |
| 93 ASSERT_TRUE(process_info.Arguments(&argv)); |
| 94 |
| 95 // gtest argv processing scrambles argv, but it leaves argc and argv[0] |
| 96 // intact, so test those. |
| 97 |
| 98 #if defined(OS_MACOSX) |
| 99 int expect_argc = *_NSGetArgc(); |
| 100 char** expect_argv = *_NSGetArgv(); |
| 101 #else |
| 102 #error Obtain expect_argc and expect_argv correctly on your system. |
| 103 #endif |
| 104 |
| 105 int argc = implicit_cast<int>(argv.size()); |
| 106 EXPECT_EQ(expect_argc, argc); |
| 107 |
| 108 ASSERT_GE(expect_argc, 1); |
| 109 ASSERT_GE(argc, 1); |
| 110 |
| 111 EXPECT_EQ(std::string(expect_argv[0]), argv[0]); |
| 112 } |
| 113 |
| 114 |
| 115 TEST(ProcessInfo, Self) { |
| 116 ProcessInfo process_info; |
| 117 ASSERT_TRUE(process_info.Initialize(getpid())); |
| 118 TestSelfProcess(process_info); |
| 119 } |
| 120 |
| 121 #if defined(OS_MACOSX) |
| 122 TEST(ProcessInfo, SelfTask) { |
| 123 ProcessInfo process_info; |
| 124 ASSERT_TRUE(process_info.InitializeFromTask(mach_task_self())); |
| 125 TestSelfProcess(process_info); |
| 126 } |
| 127 #endif |
| 128 |
| 129 TEST(ProcessInfo, Pid1) { |
| 130 // PID 1 is expected to be init or the system’s equivalent. This tests reading |
| 131 // information about another process. |
| 132 ProcessInfo process_info; |
| 133 ASSERT_TRUE(process_info.Initialize(1)); |
| 134 |
| 135 EXPECT_EQ(implicit_cast<pid_t>(1), process_info.ProcessID()); |
| 136 EXPECT_EQ(implicit_cast<pid_t>(0), process_info.ParentProcessID()); |
| 137 EXPECT_EQ(implicit_cast<uid_t>(0), process_info.RealUserID()); |
| 138 EXPECT_EQ(implicit_cast<uid_t>(0), process_info.EffectiveUserID()); |
| 139 EXPECT_EQ(implicit_cast<uid_t>(0), process_info.SavedUserID()); |
| 140 EXPECT_EQ(implicit_cast<gid_t>(0), process_info.RealGroupID()); |
| 141 EXPECT_EQ(implicit_cast<gid_t>(0), process_info.EffectiveGroupID()); |
| 142 EXPECT_EQ(implicit_cast<gid_t>(0), process_info.SavedGroupID()); |
| 143 EXPECT_FALSE(process_info.AllGroups().empty()); |
| 144 } |
| 145 |
| 146 } // namespace |
| 147 } // namespace test |
| 148 } // namespace crashpad |
OLD | NEW |