1//===-- UserIDResolverTest.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/Utility/UserIDResolver.h"
10#include "gmock/gmock.h"
11#include <optional>
12
13using namespace lldb_private;
14using namespace testing;
15
16namespace {
17class TestUserIDResolver : public UserIDResolver {
18public:
19 MOCK_METHOD1(DoGetUserName, std::optional<std::string>(id_t uid));
20 MOCK_METHOD1(DoGetGroupName, std::optional<std::string>(id_t gid));
21};
22} // namespace
23
24TEST(UserIDResolver, GetUserName) {
25 StrictMock<TestUserIDResolver> r;
26 llvm::StringRef user47("foo");
27 EXPECT_CALL(r, DoGetUserName(47)).Times(n: 1).WillOnce(once_action: Return(value: user47.str()));
28 EXPECT_CALL(r, DoGetUserName(42)).Times(n: 1).WillOnce(once_action: Return(value: std::nullopt));
29
30 // Call functions twice to make sure the caching works.
31 EXPECT_EQ(user47, r.GetUserName(47));
32 EXPECT_EQ(user47, r.GetUserName(47));
33 EXPECT_EQ(std::nullopt, r.GetUserName(42));
34 EXPECT_EQ(std::nullopt, r.GetUserName(42));
35}
36
37TEST(UserIDResolver, GetGroupName) {
38 StrictMock<TestUserIDResolver> r;
39 llvm::StringRef group47("foo");
40 EXPECT_CALL(r, DoGetGroupName(47)).Times(n: 1).WillOnce(once_action: Return(value: group47.str()));
41 EXPECT_CALL(r, DoGetGroupName(42)).Times(n: 1).WillOnce(once_action: Return(value: std::nullopt));
42
43 // Call functions twice to make sure the caching works.
44 EXPECT_EQ(group47, r.GetGroupName(47));
45 EXPECT_EQ(group47, r.GetGroupName(47));
46 EXPECT_EQ(std::nullopt, r.GetGroupName(42));
47 EXPECT_EQ(std::nullopt, r.GetGroupName(42));
48}
49

source code of lldb/unittests/Utility/UserIDResolverTest.cpp