1 | //===-- HostTest.cpp ------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Host/Host.h" |
10 | #include "lldb/Host/FileSystem.h" |
11 | #include "lldb/Host/HostInfo.h" |
12 | #include "lldb/Utility/ProcessInfo.h" |
13 | #include "gtest/gtest.h" |
14 | |
15 | using namespace lldb_private; |
16 | |
17 | namespace { |
18 | class HostTest : public testing::Test { |
19 | public: |
20 | static void SetUpTestCase() { |
21 | FileSystem::Initialize(); |
22 | HostInfo::Initialize(); |
23 | } |
24 | static void TearDownTestCase() { |
25 | HostInfo::Terminate(); |
26 | FileSystem::Terminate(); |
27 | } |
28 | }; |
29 | } // namespace |
30 | |
31 | TEST_F(HostTest, GetProcessInfo) { |
32 | llvm::Triple triple = HostInfo::GetTargetTriple(); |
33 | |
34 | ASSERT_TRUE( |
35 | (triple.getOS() == llvm::Triple::OSType::Linux) || |
36 | (triple.hasEnvironment() && |
37 | triple.getEnvironment() == llvm::Triple::EnvironmentType::Android)); |
38 | |
39 | ProcessInstanceInfo Info; |
40 | ASSERT_FALSE(Host::GetProcessInfo(0, Info)); |
41 | |
42 | ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
43 | |
44 | ASSERT_TRUE(Info.ProcessIDIsValid()); |
45 | EXPECT_EQ(lldb::pid_t(getpid()), Info.GetProcessID()); |
46 | |
47 | ASSERT_TRUE(Info.ParentProcessIDIsValid()); |
48 | EXPECT_EQ(lldb::pid_t(getppid()), Info.GetParentProcessID()); |
49 | |
50 | ASSERT_TRUE(Info.ProcessGroupIDIsValid()); |
51 | EXPECT_EQ(lldb::pid_t(getpgrp()), Info.GetProcessGroupID()); |
52 | |
53 | ASSERT_TRUE(Info.ProcessSessionIDIsValid()); |
54 | EXPECT_EQ(lldb::pid_t(getsid(getpid())), Info.GetProcessSessionID()); |
55 | |
56 | ASSERT_TRUE(Info.EffectiveUserIDIsValid()); |
57 | EXPECT_EQ(geteuid(), Info.GetEffectiveUserID()); |
58 | |
59 | ASSERT_TRUE(Info.EffectiveGroupIDIsValid()); |
60 | EXPECT_EQ(getegid(), Info.GetEffectiveGroupID()); |
61 | |
62 | ASSERT_TRUE(Info.UserIDIsValid()); |
63 | EXPECT_EQ(geteuid(), Info.GetUserID()); |
64 | |
65 | ASSERT_TRUE(Info.GroupIDIsValid()); |
66 | EXPECT_EQ(getegid(), Info.GetGroupID()); |
67 | |
68 | EXPECT_TRUE(Info.GetArchitecture().IsValid()); |
69 | EXPECT_EQ(HostInfo::GetArchitecture(HostInfo::eArchKindDefault), |
70 | Info.GetArchitecture()); |
71 | // Test timings |
72 | /* |
73 | * This is flaky in the buildbots on all archs |
74 | ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
75 | ProcessInstanceInfo::timespec user_time = Info.GetUserTime(); |
76 | static volatile unsigned u = 0; |
77 | for (unsigned i = 0; i < 10'000'000; i++) { |
78 | u = i; |
79 | } |
80 | ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info)); |
81 | ProcessInstanceInfo::timespec next_user_time = Info.GetUserTime(); |
82 | ASSERT_TRUE(user_time.tv_sec < next_user_time.tv_sec || |
83 | user_time.tv_usec < next_user_time.tv_usec); |
84 | */ |
85 | } |
86 | |