brintos

brintos / llvm-project-archived public Read only

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