1 //===-- ProcessInstanceInfoTest.cpp ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/Process.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private
;
15 /// A very simple resolver which fails for even ids and returns a simple string
17 class DummyUserIDResolver
: public UserIDResolver
{
19 llvm::Optional
<std::string
> DoGetUserName(id_t uid
) override
{
21 return ("user" + llvm::Twine(uid
)).str();
25 llvm::Optional
<std::string
> DoGetGroupName(id_t gid
) override
{
27 return ("group" + llvm::Twine(gid
)).str();
33 TEST(ProcessInstanceInfo
, Dump
) {
34 ProcessInstanceInfo
info("a.out", ArchSpec("x86_64-pc-linux"), 47);
36 info
.SetEffectiveUserID(2);
38 info
.SetEffectiveGroupID(4);
40 DummyUserIDResolver resolver
;
42 info
.Dump(s
, resolver
);
43 EXPECT_STREQ(R
"( pid = 47
46 arch = x86_64-pc-linux
55 TEST(ProcessInstanceInfo
, DumpTable
) {
56 ProcessInstanceInfo
info("a.out", ArchSpec("x86_64-pc-linux"), 47);
58 info
.SetEffectiveUserID(2);
60 info
.SetEffectiveGroupID(4);
62 DummyUserIDResolver resolver
;
65 const bool show_args
= false;
66 const bool verbose
= true;
67 ProcessInstanceInfo::DumpTableHeader(s
, show_args
, verbose
);
68 info
.DumpAsTableRow(s
, resolver
, show_args
, verbose
);
70 R
"(PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE ARGUMENTS
71 ====== ====== ========== ========== ========== ========== ============================== ============================
72 47 0 user1 group3 2 4 x86_64-pc-linux
77 TEST(ProcessInstanceInfo
, DumpTable_invalidUID
) {
78 ProcessInstanceInfo
info("a.out", ArchSpec("aarch64-unknown-linux-android"), 47);
80 DummyUserIDResolver resolver
;
83 const bool show_args
= false;
84 const bool verbose
= false;
85 ProcessInstanceInfo::DumpTableHeader(s
, show_args
, verbose
);
86 info
.DumpAsTableRow(s
, resolver
, show_args
, verbose
);
88 R
"(PID PARENT USER TRIPLE NAME
89 ====== ====== ========== ============================== ============================
90 47 0 aarch64-unknown-linux-android a.out
95 TEST(ProcessInstanceInfoMatch
, Name
) {
96 ProcessInstanceInfo info_bar
, info_empty
;
97 info_bar
.GetExecutableFile().SetFile("/foo/bar", FileSpec::Style::posix
);
99 ProcessInstanceInfoMatch match
;
100 match
.SetNameMatchType(NameMatch::Equals
);
101 match
.GetProcessInfo().GetExecutableFile().SetFile("bar",
102 FileSpec::Style::posix
);
104 EXPECT_TRUE(match
.Matches(info_bar
));
105 EXPECT_FALSE(match
.Matches(info_empty
));
107 match
.GetProcessInfo().GetExecutableFile() = FileSpec();
108 EXPECT_TRUE(match
.Matches(info_bar
));
109 EXPECT_TRUE(match
.Matches(info_empty
));
112 TEST(ProcessInstanceInfo
, Yaml
) {
114 llvm::raw_string_ostream
os(buffer
);
117 ProcessInstanceInfo
info("a.out", ArchSpec("x86_64-pc-linux"), 47);
119 info
.SetEffectiveUserID(2);
121 info
.SetEffectiveGroupID(4);
122 llvm::yaml::Output
yout(os
);
127 ProcessInstanceInfo deserialized
;
128 llvm::yaml::Input
yin(buffer
);
131 EXPECT_EQ(deserialized
.GetNameAsStringRef(), info
.GetNameAsStringRef());
132 EXPECT_EQ(deserialized
.GetArchitecture(), info
.GetArchitecture());
133 EXPECT_EQ(deserialized
.GetUserID(), info
.GetUserID());
134 EXPECT_EQ(deserialized
.GetGroupID(), info
.GetGroupID());
135 EXPECT_EQ(deserialized
.GetEffectiveUserID(), info
.GetEffectiveUserID());
136 EXPECT_EQ(deserialized
.GetEffectiveGroupID(), info
.GetEffectiveGroupID());
139 TEST(ProcessInstanceInfoList
, Yaml
) {
141 llvm::raw_string_ostream
os(buffer
);
144 ProcessInstanceInfo
info("a.out", ArchSpec("x86_64-pc-linux"), 47);
146 info
.SetEffectiveUserID(2);
148 info
.SetEffectiveGroupID(4);
149 ProcessInstanceInfoList list
;
150 list
.push_back(info
);
151 llvm::yaml::Output
yout(os
);
156 ProcessInstanceInfoList deserialized
;
157 llvm::yaml::Input
yin(buffer
);
160 ASSERT_EQ(deserialized
.size(), static_cast<size_t>(1));
161 EXPECT_EQ(deserialized
[0].GetNameAsStringRef(), info
.GetNameAsStringRef());
162 EXPECT_EQ(deserialized
[0].GetArchitecture(), info
.GetArchitecture());
163 EXPECT_EQ(deserialized
[0].GetUserID(), info
.GetUserID());
164 EXPECT_EQ(deserialized
[0].GetGroupID(), info
.GetGroupID());
165 EXPECT_EQ(deserialized
[0].GetEffectiveUserID(), info
.GetEffectiveUserID());
166 EXPECT_EQ(deserialized
[0].GetEffectiveGroupID(), info
.GetEffectiveGroupID());