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