[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / lldb / unittests / Utility / ProcessInstanceInfoTest.cpp
bloba3f850cfb9b47e9f87c1f1acc5e9d678fc30d0ca
1 //===-- ProcessInstanceInfoTest.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 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/Process.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private;
14 namespace {
15 /// A very simple resolver which fails for even ids and returns a simple string
16 /// for odd ones.
17 class DummyUserIDResolver : public UserIDResolver {
18 protected:
19 llvm::Optional<std::string> DoGetUserName(id_t uid) override {
20 if (uid % 2)
21 return ("user" + llvm::Twine(uid)).str();
22 return llvm::None;
25 llvm::Optional<std::string> DoGetGroupName(id_t gid) override {
26 if (gid % 2)
27 return ("group" + llvm::Twine(gid)).str();
28 return llvm::None;
31 } // namespace
33 TEST(ProcessInstanceInfo, Dump) {
34 ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47);
35 info.SetUserID(1);
36 info.SetEffectiveUserID(2);
37 info.SetGroupID(3);
38 info.SetEffectiveGroupID(4);
40 DummyUserIDResolver resolver;
41 StreamString s;
42 info.Dump(s, resolver);
43 EXPECT_STREQ(R"( pid = 47
44 name = a.out
45 file = a.out
46 arch = x86_64-pc-linux
47 uid = 1 (user1)
48 gid = 3 (group3)
49 euid = 2 ()
50 egid = 4 ()
51 )",
52 s.GetData());
55 TEST(ProcessInstanceInfo, DumpTable) {
56 ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47);
57 info.SetUserID(1);
58 info.SetEffectiveUserID(2);
59 info.SetGroupID(3);
60 info.SetEffectiveGroupID(4);
62 DummyUserIDResolver resolver;
63 StreamString s;
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);
69 EXPECT_STREQ(
70 R"(PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE ARGUMENTS
71 ====== ====== ========== ========== ========== ========== ============================== ============================
72 47 0 user1 group3 2 4 x86_64-pc-linux
73 )",
74 s.GetData());
77 TEST(ProcessInstanceInfo, DumpTable_invalidUID) {
78 ProcessInstanceInfo info("a.out", ArchSpec("aarch64-unknown-linux-android"), 47);
80 DummyUserIDResolver resolver;
81 StreamString s;
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);
87 EXPECT_STREQ(
88 R"(PID PARENT USER TRIPLE NAME
89 ====== ====== ========== ============================== ============================
90 47 0 aarch64-unknown-linux-android a.out
91 )",
92 s.GetData());
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) {
113 std::string buffer;
114 llvm::raw_string_ostream os(buffer);
116 // Serialize.
117 ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47);
118 info.SetUserID(1);
119 info.SetEffectiveUserID(2);
120 info.SetGroupID(3);
121 info.SetEffectiveGroupID(4);
122 llvm::yaml::Output yout(os);
123 yout << info;
124 os.flush();
126 // Deserialize.
127 ProcessInstanceInfo deserialized;
128 llvm::yaml::Input yin(buffer);
129 yin >> deserialized;
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) {
140 std::string buffer;
141 llvm::raw_string_ostream os(buffer);
143 // Serialize.
144 ProcessInstanceInfo info("a.out", ArchSpec("x86_64-pc-linux"), 47);
145 info.SetUserID(1);
146 info.SetEffectiveUserID(2);
147 info.SetGroupID(3);
148 info.SetEffectiveGroupID(4);
149 ProcessInstanceInfoList list;
150 list.push_back(info);
151 llvm::yaml::Output yout(os);
152 yout << list;
153 os.flush();
155 // Deserialize.
156 ProcessInstanceInfoList deserialized;
157 llvm::yaml::Input yin(buffer);
158 yin >> deserialized;
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());