1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/Vector.h>
#include <pwd.h>
struct PasswdEntry {
DeprecatedString name;
uid_t uid {};
};
static Vector<PasswdEntry> get_all_passwd_entries()
{
Vector<PasswdEntry> entries;
setpwent();
while (auto* pwd = getpwent()) {
entries.append(PasswdEntry {
.name = pwd->pw_name,
.uid = pwd->pw_uid,
});
}
return entries;
}
TEST_CASE(getpwuid_r)
{
// Verify that all known passwd entries can be found with getpwuid_r()
for (auto const& entry : get_all_passwd_entries()) {
struct passwd pwd_buffer = {};
char buffer[4096] = {};
struct passwd* result = nullptr;
auto rc = getpwuid_r(entry.uid, &pwd_buffer, buffer, sizeof(buffer), &result);
EXPECT_EQ(rc, 0);
EXPECT_EQ(entry.uid, result->pw_uid);
EXPECT_EQ(entry.name, result->pw_name);
}
// Verify that a bogus UID can't be found with getpwuid_r()
{
struct passwd pwd_buffer = {};
char buffer[4096] = {};
struct passwd* result = nullptr;
auto rc = getpwuid_r(99991999, &pwd_buffer, buffer, sizeof(buffer), &result);
EXPECT_EQ(rc, ENOENT);
EXPECT_EQ(result, nullptr);
}
// Verify that two calls to getpwuid_r() don't clobber each other.
{
struct passwd pwd_buffer1 = {};
char buffer1[4096] = {};
struct passwd* result1 = nullptr;
auto rc1 = getpwuid_r(0, &pwd_buffer1, buffer1, sizeof(buffer1), &result1);
EXPECT_EQ(rc1, 0);
EXPECT_NE(result1, nullptr);
EXPECT_EQ(result1, &pwd_buffer1);
struct passwd pwd_buffer2 = {};
char buffer2[4096] = {};
struct passwd* result2 = nullptr;
auto rc2 = getpwuid_r(0, &pwd_buffer2, buffer2, sizeof(buffer2), &result2);
EXPECT_EQ(rc2, 0);
EXPECT_NE(result2, nullptr);
EXPECT_EQ(result2, &pwd_buffer2);
EXPECT_NE(result1, result2);
}
}
TEST_CASE(getpwnam_r)
{
// Verify that all known passwd entries can be found with getpwnam_r()
for (auto const& entry : get_all_passwd_entries()) {
struct passwd pwd_buffer = {};
char buffer[4096] = {};
struct passwd* result = nullptr;
auto rc = getpwnam_r(entry.name.characters(), &pwd_buffer, buffer, sizeof(buffer), &result);
EXPECT_EQ(rc, 0);
EXPECT_EQ(entry.uid, result->pw_uid);
EXPECT_EQ(entry.name, result->pw_name);
}
// Verify that a bogus name can't be found with getpwnam_r()
{
struct passwd pwd_buffer = {};
char buffer[4096] = {};
struct passwd* result = nullptr;
auto rc = getpwnam_r("99991999", &pwd_buffer, buffer, sizeof(buffer), &result);
EXPECT_EQ(rc, ENOENT);
EXPECT_EQ(result, nullptr);
}
// Verify that two calls to getpwnam_r() don't clobber each other.
{
struct passwd pwd_buffer1 = {};
char buffer1[4096] = {};
struct passwd* result1 = nullptr;
auto rc1 = getpwnam_r("root", &pwd_buffer1, buffer1, sizeof(buffer1), &result1);
EXPECT_EQ(rc1, 0);
EXPECT_NE(result1, nullptr);
EXPECT_EQ(result1, &pwd_buffer1);
struct passwd pwd_buffer2 = {};
char buffer2[4096] = {};
struct passwd* result2 = nullptr;
auto rc2 = getpwnam_r("root", &pwd_buffer2, buffer2, sizeof(buffer2), &result2);
EXPECT_EQ(rc2, 0);
EXPECT_NE(result2, nullptr);
EXPECT_EQ(result2, &pwd_buffer2);
EXPECT_NE(result1, result2);
}
}
|