Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "alloc-util.h"
4 : #include "format-util.h"
5 : #include "log.h"
6 : #include "macro.h"
7 : #include "path-util.h"
8 : #include "string-util.h"
9 : #include "user-util.h"
10 :
11 4 : static void test_uid_to_name_one(uid_t uid, const char *name) {
12 4 : _cleanup_free_ char *t = NULL;
13 :
14 4 : log_info("/* %s("UID_FMT", \"%s\") */", __func__, uid, name);
15 :
16 4 : assert_se(t = uid_to_name(uid));
17 4 : if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
18 1 : log_info("(skipping detailed tests because nobody is not synthesized)");
19 1 : return;
20 : }
21 3 : assert_se(streq_ptr(t, name));
22 : }
23 :
24 5 : static void test_gid_to_name_one(gid_t gid, const char *name) {
25 5 : _cleanup_free_ char *t = NULL;
26 :
27 5 : log_info("/* %s("GID_FMT", \"%s\") */", __func__, gid, name);
28 :
29 5 : assert_se(t = gid_to_name(gid));
30 5 : if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
31 1 : log_info("(skipping detailed tests because nobody is not synthesized)");
32 1 : return;
33 : }
34 4 : assert_se(streq_ptr(t, name));
35 : }
36 :
37 1 : static void test_parse_uid(void) {
38 : int r;
39 : uid_t uid;
40 :
41 1 : log_info("/* %s */", __func__);
42 :
43 1 : r = parse_uid("100", &uid);
44 1 : assert_se(r == 0);
45 1 : assert_se(uid == 100);
46 :
47 1 : r = parse_uid("65535", &uid);
48 1 : assert_se(r == -ENXIO);
49 :
50 1 : r = parse_uid("asdsdas", &uid);
51 1 : assert_se(r == -EINVAL);
52 1 : }
53 :
54 1 : static void test_uid_ptr(void) {
55 1 : log_info("/* %s */", __func__);
56 :
57 : assert_se(UID_TO_PTR(0) != NULL);
58 : assert_se(UID_TO_PTR(1000) != NULL);
59 :
60 : assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
61 : assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
62 1 : }
63 :
64 1 : static void test_valid_user_group_name(void) {
65 1 : log_info("/* %s */", __func__);
66 :
67 1 : assert_se(!valid_user_group_name(NULL));
68 1 : assert_se(!valid_user_group_name(""));
69 1 : assert_se(!valid_user_group_name("1"));
70 1 : assert_se(!valid_user_group_name("65535"));
71 1 : assert_se(!valid_user_group_name("-1"));
72 1 : assert_se(!valid_user_group_name("-kkk"));
73 1 : assert_se(!valid_user_group_name("rööt"));
74 1 : assert_se(!valid_user_group_name("."));
75 1 : assert_se(!valid_user_group_name("eff.eff"));
76 1 : assert_se(!valid_user_group_name("foo\nbar"));
77 1 : assert_se(!valid_user_group_name("0123456789012345678901234567890123456789"));
78 1 : assert_se(!valid_user_group_name_or_id("aaa:bbb"));
79 :
80 1 : assert_se(valid_user_group_name("root"));
81 1 : assert_se(valid_user_group_name("lennart"));
82 1 : assert_se(valid_user_group_name("LENNART"));
83 1 : assert_se(valid_user_group_name("_kkk"));
84 1 : assert_se(valid_user_group_name("kkk-"));
85 1 : assert_se(valid_user_group_name("kk-k"));
86 :
87 1 : assert_se(valid_user_group_name("some5"));
88 1 : assert_se(!valid_user_group_name("5some"));
89 1 : assert_se(valid_user_group_name("INNER5NUMBER"));
90 1 : }
91 :
92 1 : static void test_valid_user_group_name_or_id(void) {
93 1 : log_info("/* %s */", __func__);
94 :
95 1 : assert_se(!valid_user_group_name_or_id(NULL));
96 1 : assert_se(!valid_user_group_name_or_id(""));
97 1 : assert_se(valid_user_group_name_or_id("0"));
98 1 : assert_se(valid_user_group_name_or_id("1"));
99 1 : assert_se(valid_user_group_name_or_id("65534"));
100 1 : assert_se(!valid_user_group_name_or_id("65535"));
101 1 : assert_se(valid_user_group_name_or_id("65536"));
102 1 : assert_se(!valid_user_group_name_or_id("-1"));
103 1 : assert_se(!valid_user_group_name_or_id("-kkk"));
104 1 : assert_se(!valid_user_group_name_or_id("rööt"));
105 1 : assert_se(!valid_user_group_name_or_id("."));
106 1 : assert_se(!valid_user_group_name_or_id("eff.eff"));
107 1 : assert_se(!valid_user_group_name_or_id("foo\nbar"));
108 1 : assert_se(!valid_user_group_name_or_id("0123456789012345678901234567890123456789"));
109 1 : assert_se(!valid_user_group_name_or_id("aaa:bbb"));
110 :
111 1 : assert_se(valid_user_group_name_or_id("root"));
112 1 : assert_se(valid_user_group_name_or_id("lennart"));
113 1 : assert_se(valid_user_group_name_or_id("LENNART"));
114 1 : assert_se(valid_user_group_name_or_id("_kkk"));
115 1 : assert_se(valid_user_group_name_or_id("kkk-"));
116 1 : assert_se(valid_user_group_name_or_id("kk-k"));
117 :
118 1 : assert_se(valid_user_group_name_or_id("some5"));
119 1 : assert_se(!valid_user_group_name_or_id("5some"));
120 1 : assert_se(valid_user_group_name_or_id("INNER5NUMBER"));
121 1 : }
122 :
123 1 : static void test_valid_gecos(void) {
124 1 : log_info("/* %s */", __func__);
125 :
126 1 : assert_se(!valid_gecos(NULL));
127 1 : assert_se(valid_gecos(""));
128 1 : assert_se(valid_gecos("test"));
129 1 : assert_se(valid_gecos("Ümläüt"));
130 1 : assert_se(!valid_gecos("In\nvalid"));
131 1 : assert_se(!valid_gecos("In:valid"));
132 1 : }
133 :
134 1 : static void test_valid_home(void) {
135 1 : log_info("/* %s */", __func__);
136 :
137 1 : assert_se(!valid_home(NULL));
138 1 : assert_se(!valid_home(""));
139 1 : assert_se(!valid_home("."));
140 1 : assert_se(!valid_home("/home/.."));
141 1 : assert_se(!valid_home("/home/../"));
142 1 : assert_se(!valid_home("/home\n/foo"));
143 1 : assert_se(!valid_home("./piep"));
144 1 : assert_se(!valid_home("piep"));
145 1 : assert_se(!valid_home("/home/user:lennart"));
146 :
147 1 : assert_se(valid_home("/"));
148 1 : assert_se(valid_home("/home"));
149 1 : assert_se(valid_home("/home/foo"));
150 1 : }
151 :
152 4 : static void test_get_user_creds_one(const char *id, const char *name, uid_t uid, gid_t gid, const char *home, const char *shell) {
153 4 : const char *rhome = NULL;
154 4 : const char *rshell = NULL;
155 4 : uid_t ruid = UID_INVALID;
156 4 : gid_t rgid = GID_INVALID;
157 : int r;
158 :
159 4 : log_info("/* %s(\"%s\", \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\") */",
160 : __func__, id, name, uid, gid, home, shell);
161 :
162 4 : r = get_user_creds(&id, &ruid, &rgid, &rhome, &rshell, 0);
163 4 : log_info_errno(r, "got \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\": %m",
164 : id, ruid, rgid, strnull(rhome), strnull(rshell));
165 4 : if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
166 2 : log_info("(skipping detailed tests because nobody is not synthesized)");
167 2 : return;
168 : }
169 2 : assert_se(r == 0);
170 2 : assert_se(streq_ptr(id, name));
171 2 : assert_se(ruid == uid);
172 2 : assert_se(rgid == gid);
173 2 : assert_se(path_equal(rhome, home));
174 2 : assert_se(path_equal(rshell, shell));
175 : }
176 :
177 4 : static void test_get_group_creds_one(const char *id, const char *name, gid_t gid) {
178 4 : gid_t rgid = GID_INVALID;
179 : int r;
180 :
181 4 : log_info("/* %s(\"%s\", \"%s\", "GID_FMT") */", __func__, id, name, gid);
182 :
183 4 : r = get_group_creds(&id, &rgid, 0);
184 4 : log_info_errno(r, "got \"%s\", "GID_FMT": %m", id, rgid);
185 4 : if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
186 2 : log_info("(skipping detailed tests because nobody is not synthesized)");
187 2 : return;
188 : }
189 2 : assert_se(r == 0);
190 2 : assert_se(streq_ptr(id, name));
191 2 : assert_se(rgid == gid);
192 : }
193 :
194 1 : static void test_make_salt(void) {
195 1 : log_info("/* %s */", __func__);
196 :
197 1 : _cleanup_free_ char *s, *t;
198 :
199 1 : assert_se(make_salt(&s) == 0);
200 1 : log_info("got %s", s);
201 :
202 1 : assert_se(make_salt(&t) == 0);
203 1 : log_info("got %s", t);
204 :
205 1 : assert(!streq(s, t));
206 1 : }
207 :
208 1 : int main(int argc, char *argv[]) {
209 1 : test_uid_to_name_one(0, "root");
210 1 : test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME);
211 1 : test_uid_to_name_one(0xFFFF, "65535");
212 1 : test_uid_to_name_one(0xFFFFFFFF, "4294967295");
213 :
214 1 : test_gid_to_name_one(0, "root");
215 1 : test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME);
216 1 : test_gid_to_name_one(TTY_GID, "tty");
217 1 : test_gid_to_name_one(0xFFFF, "65535");
218 1 : test_gid_to_name_one(0xFFFFFFFF, "4294967295");
219 :
220 1 : test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh");
221 1 : test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh");
222 1 : test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
223 1 : test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
224 :
225 1 : test_get_group_creds_one("root", "root", 0);
226 1 : test_get_group_creds_one("0", "root", 0);
227 1 : test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY);
228 1 : test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY);
229 :
230 1 : test_parse_uid();
231 1 : test_uid_ptr();
232 :
233 1 : test_valid_user_group_name();
234 1 : test_valid_user_group_name_or_id();
235 1 : test_valid_gecos();
236 1 : test_valid_home();
237 :
238 1 : test_make_salt();
239 :
240 1 : return 0;
241 : }
|