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 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Target/Process.h" |
10 | #include "gtest/gtest.h" |
11 | #include <optional> |
12 | |
13 | using namespace lldb_private; |
14 | |
15 | namespace { |
16 | /// A very simple resolver which fails for even ids and returns a simple string |
17 | /// for odd ones. |
18 | class DummyUserIDResolver : public UserIDResolver { |
19 | protected: |
20 | std::optional<std::string> DoGetUserName(id_t uid) override { |
21 | if (uid % 2) |
22 | return ("user" + llvm::Twine(uid)).str(); |
23 | return std::nullopt; |
24 | } |
25 | |
26 | std::optional<std::string> DoGetGroupName(id_t gid) override { |
27 | if (gid % 2) |
28 | return ("group" + llvm::Twine(gid)).str(); |
29 | return std::nullopt; |
30 | } |
31 | }; |
32 | } // namespace |
33 | |
34 | TEST(ProcessInstanceInfo, Dump) { |
35 | ProcessInstanceInfo info("a.out" , ArchSpec("x86_64-pc-linux" ), 47); |
36 | info.SetUserID(1); |
37 | info.SetEffectiveUserID(2); |
38 | info.SetGroupID(3); |
39 | info.SetEffectiveGroupID(4); |
40 | |
41 | DummyUserIDResolver resolver; |
42 | StreamString s; |
43 | info.Dump(s, resolver); |
44 | EXPECT_STREQ(R"( pid = 47 |
45 | name = a.out |
46 | file = a.out |
47 | arch = x86_64-pc-linux |
48 | uid = 1 (user1) |
49 | gid = 3 (group3) |
50 | euid = 2 () |
51 | egid = 4 () |
52 | )" , |
53 | s.GetData()); |
54 | } |
55 | |
56 | TEST(ProcessInstanceInfo, DumpTable) { |
57 | ProcessInstanceInfo info("a.out" , ArchSpec("x86_64-pc-linux" ), 47); |
58 | info.SetUserID(1); |
59 | info.SetEffectiveUserID(2); |
60 | info.SetGroupID(3); |
61 | info.SetEffectiveGroupID(4); |
62 | |
63 | DummyUserIDResolver resolver; |
64 | StreamString s; |
65 | |
66 | const bool show_args = false; |
67 | const bool verbose = true; |
68 | ProcessInstanceInfo::DumpTableHeader(s, show_args, verbose); |
69 | info.DumpAsTableRow(s, resolver, show_args, verbose); |
70 | EXPECT_STREQ( |
71 | R"(PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE ARGUMENTS |
72 | ====== ====== ========== ========== ========== ========== ============================== ============================ |
73 | 47 0 user1 group3 2 4 x86_64-pc-linux |
74 | )" , |
75 | s.GetData()); |
76 | } |
77 | |
78 | TEST(ProcessInstanceInfo, DumpTable_invalidUID) { |
79 | ProcessInstanceInfo info("a.out" , ArchSpec("aarch64-unknown-linux-android" ), 47); |
80 | |
81 | DummyUserIDResolver resolver; |
82 | StreamString s; |
83 | |
84 | const bool show_args = false; |
85 | const bool verbose = false; |
86 | ProcessInstanceInfo::DumpTableHeader(s, show_args, verbose); |
87 | info.DumpAsTableRow(s, resolver, show_args, verbose); |
88 | EXPECT_STREQ( |
89 | R"(PID PARENT USER TRIPLE NAME |
90 | ====== ====== ========== ============================== ============================ |
91 | 47 0 aarch64-unknown-linux-android a.out |
92 | )" , |
93 | s.GetData()); |
94 | } |
95 | |
96 | TEST(ProcessInstanceInfoMatch, Name) { |
97 | ProcessInstanceInfo info_bar, info_empty; |
98 | info_bar.GetExecutableFile().SetFile(path: "/foo/bar" , style: FileSpec::Style::posix); |
99 | |
100 | ProcessInstanceInfoMatch match; |
101 | match.SetNameMatchType(NameMatch::Equals); |
102 | match.GetProcessInfo().GetExecutableFile().SetFile(path: "bar" , |
103 | style: FileSpec::Style::posix); |
104 | |
105 | EXPECT_TRUE(match.Matches(info_bar)); |
106 | EXPECT_FALSE(match.Matches(info_empty)); |
107 | |
108 | match.GetProcessInfo().GetExecutableFile() = FileSpec(); |
109 | EXPECT_TRUE(match.Matches(info_bar)); |
110 | EXPECT_TRUE(match.Matches(info_empty)); |
111 | } |
112 | |