1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "format-util.h"
5 #include "libcrypt-util.h"
6 #include "log.h"
7 #include "macro.h"
8 #include "memory-util.h"
9 #include "path-util.h"
10 #include "string-util.h"
11 #include "tests.h"
12 #include "user-util.h"
13
test_uid_to_name_one(uid_t uid,const char * name)14 static void test_uid_to_name_one(uid_t uid, const char *name) {
15 _cleanup_free_ char *t = NULL;
16
17 log_info("/* %s("UID_FMT", \"%s\") */", __func__, uid, name);
18
19 assert_se(t = uid_to_name(uid));
20 if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
21 log_info("(skipping detailed tests because nobody is not synthesized)");
22 return;
23 }
24 assert_se(streq_ptr(t, name));
25 }
26
TEST(uid_to_name)27 TEST(uid_to_name) {
28 test_uid_to_name_one(0, "root");
29 test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME);
30 test_uid_to_name_one(0xFFFF, "65535");
31 test_uid_to_name_one(0xFFFFFFFF, "4294967295");
32 }
33
test_gid_to_name_one(gid_t gid,const char * name)34 static void test_gid_to_name_one(gid_t gid, const char *name) {
35 _cleanup_free_ char *t = NULL;
36
37 log_info("/* %s("GID_FMT", \"%s\") */", __func__, gid, name);
38
39 assert_se(t = gid_to_name(gid));
40 if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
41 log_info("(skipping detailed tests because nobody is not synthesized)");
42 return;
43 }
44 assert_se(streq_ptr(t, name));
45 }
46
TEST(gid_to_name)47 TEST(gid_to_name) {
48 test_gid_to_name_one(0, "root");
49 test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME);
50 test_gid_to_name_one(TTY_GID, "tty");
51 test_gid_to_name_one(0xFFFF, "65535");
52 test_gid_to_name_one(0xFFFFFFFF, "4294967295");
53 }
54
TEST(parse_uid)55 TEST(parse_uid) {
56 int r;
57 uid_t uid;
58
59 r = parse_uid("0", &uid);
60 assert_se(r == 0);
61 assert_se(uid == 0);
62
63 r = parse_uid("1", &uid);
64 assert_se(r == 0);
65 assert_se(uid == 1);
66
67 r = parse_uid("01", &uid);
68 assert_se(r == -EINVAL);
69 assert_se(uid == 1);
70
71 r = parse_uid("001", &uid);
72 assert_se(r == -EINVAL);
73 assert_se(uid == 1);
74
75 r = parse_uid("100", &uid);
76 assert_se(r == 0);
77 assert_se(uid == 100);
78
79 r = parse_uid("65535", &uid);
80 assert_se(r == -ENXIO);
81 assert_se(uid == 100);
82
83 r = parse_uid("0x1234", &uid);
84 assert_se(r == -EINVAL);
85 assert_se(uid == 100);
86
87 r = parse_uid("0o1234", &uid);
88 assert_se(r == -EINVAL);
89 assert_se(uid == 100);
90
91 r = parse_uid("0b1234", &uid);
92 assert_se(r == -EINVAL);
93 assert_se(uid == 100);
94
95 r = parse_uid("+1234", &uid);
96 assert_se(r == -EINVAL);
97 assert_se(uid == 100);
98
99 r = parse_uid("-1234", &uid);
100 assert_se(r == -EINVAL);
101 assert_se(uid == 100);
102
103 r = parse_uid(" 1234", &uid);
104 assert_se(r == -EINVAL);
105 assert_se(uid == 100);
106
107 r = parse_uid("01234", &uid);
108 assert_se(r == -EINVAL);
109 assert_se(uid == 100);
110
111 r = parse_uid("001234", &uid);
112 assert_se(r == -EINVAL);
113 assert_se(uid == 100);
114
115 r = parse_uid("0001234", &uid);
116 assert_se(r == -EINVAL);
117 assert_se(uid == 100);
118
119 r = parse_uid("-0", &uid);
120 assert_se(r == -EINVAL);
121 assert_se(uid == 100);
122
123 r = parse_uid("+0", &uid);
124 assert_se(r == -EINVAL);
125 assert_se(uid == 100);
126
127 r = parse_uid("00", &uid);
128 assert_se(r == -EINVAL);
129 assert_se(uid == 100);
130
131 r = parse_uid("000", &uid);
132 assert_se(r == -EINVAL);
133 assert_se(uid == 100);
134
135 r = parse_uid("asdsdas", &uid);
136 assert_se(r == -EINVAL);
137 assert_se(uid == 100);
138 }
139
TEST(uid_ptr)140 TEST(uid_ptr) {
141 assert_se(UID_TO_PTR(0) != NULL);
142 assert_se(UID_TO_PTR(1000) != NULL);
143
144 assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
145 assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
146 }
147
TEST(valid_user_group_name_relaxed)148 TEST(valid_user_group_name_relaxed) {
149 assert_se(!valid_user_group_name(NULL, VALID_USER_RELAX));
150 assert_se(!valid_user_group_name("", VALID_USER_RELAX));
151 assert_se(!valid_user_group_name("1", VALID_USER_RELAX));
152 assert_se(!valid_user_group_name("65535", VALID_USER_RELAX));
153 assert_se(!valid_user_group_name("-1", VALID_USER_RELAX));
154 assert_se(!valid_user_group_name("foo\nbar", VALID_USER_RELAX));
155 assert_se(!valid_user_group_name("0123456789012345678901234567890123456789", VALID_USER_RELAX));
156 assert_se(!valid_user_group_name("aaa:bbb", VALID_USER_RELAX|VALID_USER_ALLOW_NUMERIC));
157 assert_se(!valid_user_group_name(".aaa:bbb", VALID_USER_RELAX|VALID_USER_ALLOW_NUMERIC));
158 assert_se(!valid_user_group_name(".", VALID_USER_RELAX));
159 assert_se(!valid_user_group_name("..", VALID_USER_RELAX));
160
161 assert_se(valid_user_group_name("root", VALID_USER_RELAX));
162 assert_se(valid_user_group_name("lennart", VALID_USER_RELAX));
163 assert_se(valid_user_group_name("LENNART", VALID_USER_RELAX));
164 assert_se(valid_user_group_name("_kkk", VALID_USER_RELAX));
165 assert_se(valid_user_group_name("kkk-", VALID_USER_RELAX));
166 assert_se(valid_user_group_name("kk-k", VALID_USER_RELAX));
167 assert_se(valid_user_group_name("eff.eff", VALID_USER_RELAX));
168 assert_se(valid_user_group_name("eff.", VALID_USER_RELAX));
169 assert_se(valid_user_group_name("-kkk", VALID_USER_RELAX));
170 assert_se(valid_user_group_name("rööt", VALID_USER_RELAX));
171 assert_se(valid_user_group_name(".eff", VALID_USER_RELAX));
172 assert_se(valid_user_group_name(".1", VALID_USER_RELAX));
173 assert_se(valid_user_group_name(".65535", VALID_USER_RELAX));
174 assert_se(valid_user_group_name(".-1", VALID_USER_RELAX));
175 assert_se(valid_user_group_name(".-kkk", VALID_USER_RELAX));
176 assert_se(valid_user_group_name(".rööt", VALID_USER_RELAX));
177 assert_se(valid_user_group_name("...", VALID_USER_RELAX));
178
179 assert_se(valid_user_group_name("some5", VALID_USER_RELAX));
180 assert_se(valid_user_group_name("5some", VALID_USER_RELAX));
181 assert_se(valid_user_group_name("INNER5NUMBER", VALID_USER_RELAX));
182
183 assert_se(valid_user_group_name("piff.paff@ad.domain.example", VALID_USER_RELAX));
184 assert_se(valid_user_group_name("Dāvis", VALID_USER_RELAX));
185 }
186
TEST(valid_user_group_name)187 TEST(valid_user_group_name) {
188 assert_se(!valid_user_group_name(NULL, 0));
189 assert_se(!valid_user_group_name("", 0));
190 assert_se(!valid_user_group_name("1", 0));
191 assert_se(!valid_user_group_name("65535", 0));
192 assert_se(!valid_user_group_name("-1", 0));
193 assert_se(!valid_user_group_name("-kkk", 0));
194 assert_se(!valid_user_group_name("rööt", 0));
195 assert_se(!valid_user_group_name(".", 0));
196 assert_se(!valid_user_group_name(".eff", 0));
197 assert_se(!valid_user_group_name("foo\nbar", 0));
198 assert_se(!valid_user_group_name("0123456789012345678901234567890123456789", 0));
199 assert_se(!valid_user_group_name("aaa:bbb", VALID_USER_ALLOW_NUMERIC));
200 assert_se(!valid_user_group_name(".", 0));
201 assert_se(!valid_user_group_name("..", 0));
202 assert_se(!valid_user_group_name("...", 0));
203 assert_se(!valid_user_group_name(".1", 0));
204 assert_se(!valid_user_group_name(".65535", 0));
205 assert_se(!valid_user_group_name(".-1", 0));
206 assert_se(!valid_user_group_name(".-kkk", 0));
207 assert_se(!valid_user_group_name(".rööt", 0));
208 assert_se(!valid_user_group_name(".aaa:bbb", VALID_USER_ALLOW_NUMERIC));
209
210 assert_se(valid_user_group_name("root", 0));
211 assert_se(valid_user_group_name("lennart", 0));
212 assert_se(valid_user_group_name("LENNART", 0));
213 assert_se(valid_user_group_name("_kkk", 0));
214 assert_se(valid_user_group_name("kkk-", 0));
215 assert_se(valid_user_group_name("kk-k", 0));
216 assert_se(!valid_user_group_name("eff.eff", 0));
217 assert_se(!valid_user_group_name("eff.", 0));
218
219 assert_se(valid_user_group_name("some5", 0));
220 assert_se(!valid_user_group_name("5some", 0));
221 assert_se(valid_user_group_name("INNER5NUMBER", 0));
222
223 assert_se(!valid_user_group_name("piff.paff@ad.domain.example", 0));
224 assert_se(!valid_user_group_name("Dāvis", 0));
225 }
226
TEST(valid_user_group_name_or_numeric_relaxed)227 TEST(valid_user_group_name_or_numeric_relaxed) {
228 assert_se(!valid_user_group_name(NULL, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
229 assert_se(!valid_user_group_name("", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
230 assert_se(valid_user_group_name("0", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
231 assert_se(valid_user_group_name("1", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
232 assert_se(valid_user_group_name("65534", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
233 assert_se(!valid_user_group_name("65535", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
234 assert_se(valid_user_group_name("65536", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
235 assert_se(!valid_user_group_name("-1", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
236 assert_se(!valid_user_group_name("foo\nbar", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
237 assert_se(!valid_user_group_name("0123456789012345678901234567890123456789", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
238 assert_se(!valid_user_group_name("aaa:bbb", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
239 assert_se(!valid_user_group_name(".", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
240 assert_se(!valid_user_group_name("..", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
241
242 assert_se(valid_user_group_name("root", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
243 assert_se(valid_user_group_name("lennart", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
244 assert_se(valid_user_group_name("LENNART", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
245 assert_se(valid_user_group_name("_kkk", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
246 assert_se(valid_user_group_name("kkk-", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
247 assert_se(valid_user_group_name("kk-k", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
248 assert_se(valid_user_group_name("-kkk", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
249 assert_se(valid_user_group_name("rööt", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
250 assert_se(valid_user_group_name(".eff", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
251 assert_se(valid_user_group_name("eff.eff", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
252 assert_se(valid_user_group_name("eff.", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
253 assert_se(valid_user_group_name("...", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
254
255 assert_se(valid_user_group_name("some5", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
256 assert_se(valid_user_group_name("5some", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
257 assert_se(valid_user_group_name("INNER5NUMBER", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
258
259 assert_se(valid_user_group_name("piff.paff@ad.domain.example", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
260 assert_se(valid_user_group_name("Dāvis", VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX));
261 }
262
TEST(valid_user_group_name_or_numeric)263 TEST(valid_user_group_name_or_numeric) {
264 assert_se(!valid_user_group_name(NULL, VALID_USER_ALLOW_NUMERIC));
265 assert_se(!valid_user_group_name("", VALID_USER_ALLOW_NUMERIC));
266 assert_se(valid_user_group_name("0", VALID_USER_ALLOW_NUMERIC));
267 assert_se(valid_user_group_name("1", VALID_USER_ALLOW_NUMERIC));
268 assert_se(valid_user_group_name("65534", VALID_USER_ALLOW_NUMERIC));
269 assert_se(!valid_user_group_name("65535", VALID_USER_ALLOW_NUMERIC));
270 assert_se(valid_user_group_name("65536", VALID_USER_ALLOW_NUMERIC));
271 assert_se(!valid_user_group_name("-1", VALID_USER_ALLOW_NUMERIC));
272 assert_se(!valid_user_group_name("-kkk", VALID_USER_ALLOW_NUMERIC));
273 assert_se(!valid_user_group_name("rööt", VALID_USER_ALLOW_NUMERIC));
274 assert_se(!valid_user_group_name(".", VALID_USER_ALLOW_NUMERIC));
275 assert_se(!valid_user_group_name("..", VALID_USER_ALLOW_NUMERIC));
276 assert_se(!valid_user_group_name("...", VALID_USER_ALLOW_NUMERIC));
277 assert_se(!valid_user_group_name(".eff", VALID_USER_ALLOW_NUMERIC));
278 assert_se(!valid_user_group_name("eff.eff", VALID_USER_ALLOW_NUMERIC));
279 assert_se(!valid_user_group_name("eff.", VALID_USER_ALLOW_NUMERIC));
280 assert_se(!valid_user_group_name("foo\nbar", VALID_USER_ALLOW_NUMERIC));
281 assert_se(!valid_user_group_name("0123456789012345678901234567890123456789", VALID_USER_ALLOW_NUMERIC));
282 assert_se(!valid_user_group_name("aaa:bbb", VALID_USER_ALLOW_NUMERIC));
283
284 assert_se(valid_user_group_name("root", VALID_USER_ALLOW_NUMERIC));
285 assert_se(valid_user_group_name("lennart", VALID_USER_ALLOW_NUMERIC));
286 assert_se(valid_user_group_name("LENNART", VALID_USER_ALLOW_NUMERIC));
287 assert_se(valid_user_group_name("_kkk", VALID_USER_ALLOW_NUMERIC));
288 assert_se(valid_user_group_name("kkk-", VALID_USER_ALLOW_NUMERIC));
289 assert_se(valid_user_group_name("kk-k", VALID_USER_ALLOW_NUMERIC));
290
291 assert_se(valid_user_group_name("some5", VALID_USER_ALLOW_NUMERIC));
292 assert_se(!valid_user_group_name("5some", VALID_USER_ALLOW_NUMERIC));
293 assert_se(valid_user_group_name("INNER5NUMBER", VALID_USER_ALLOW_NUMERIC));
294
295 assert_se(!valid_user_group_name("piff.paff@ad.domain.example", VALID_USER_ALLOW_NUMERIC));
296 assert_se(!valid_user_group_name("Dāvis", VALID_USER_ALLOW_NUMERIC));
297 }
298
TEST(valid_gecos)299 TEST(valid_gecos) {
300 assert_se(!valid_gecos(NULL));
301 assert_se(valid_gecos(""));
302 assert_se(valid_gecos("test"));
303 assert_se(valid_gecos("Ümläüt"));
304 assert_se(!valid_gecos("In\nvalid"));
305 assert_se(!valid_gecos("In:valid"));
306 }
307
TEST(valid_home)308 TEST(valid_home) {
309 assert_se(!valid_home(NULL));
310 assert_se(!valid_home(""));
311 assert_se(!valid_home("."));
312 assert_se(!valid_home("/home/.."));
313 assert_se(!valid_home("/home/../"));
314 assert_se(!valid_home("/home\n/foo"));
315 assert_se(!valid_home("./piep"));
316 assert_se(!valid_home("piep"));
317 assert_se(!valid_home("/home/user:lennart"));
318
319 assert_se(valid_home("/"));
320 assert_se(valid_home("/home"));
321 assert_se(valid_home("/home/foo"));
322 }
323
test_get_user_creds_one(const char * id,const char * name,uid_t uid,gid_t gid,const char * home,const char * shell)324 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) {
325 const char *rhome = NULL;
326 const char *rshell = NULL;
327 uid_t ruid = UID_INVALID;
328 gid_t rgid = GID_INVALID;
329 int r;
330
331 log_info("/* %s(\"%s\", \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\") */",
332 __func__, id, name, uid, gid, home, shell);
333
334 r = get_user_creds(&id, &ruid, &rgid, &rhome, &rshell, 0);
335 log_info_errno(r, "got \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\": %m",
336 id, ruid, rgid, strnull(rhome), strnull(rshell));
337 if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
338 log_info("(skipping detailed tests because nobody is not synthesized)");
339 return;
340 }
341 assert_se(r == 0);
342 assert_se(streq_ptr(id, name));
343 assert_se(ruid == uid);
344 assert_se(rgid == gid);
345 assert_se(path_equal(rhome, home));
346 assert_se(path_equal(rshell, shell));
347 }
348
TEST(get_user_creds)349 TEST(get_user_creds) {
350 test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh");
351 test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh");
352 test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
353 test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", NOLOGIN);
354 }
355
test_get_group_creds_one(const char * id,const char * name,gid_t gid)356 static void test_get_group_creds_one(const char *id, const char *name, gid_t gid) {
357 gid_t rgid = GID_INVALID;
358 int r;
359
360 log_info("/* %s(\"%s\", \"%s\", "GID_FMT") */", __func__, id, name, gid);
361
362 r = get_group_creds(&id, &rgid, 0);
363 log_info_errno(r, "got \"%s\", "GID_FMT": %m", id, rgid);
364 if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
365 log_info("(skipping detailed tests because nobody is not synthesized)");
366 return;
367 }
368 assert_se(r == 0);
369 assert_se(streq_ptr(id, name));
370 assert_se(rgid == gid);
371 }
372
TEST(get_group_creds)373 TEST(get_group_creds) {
374 test_get_group_creds_one("root", "root", 0);
375 test_get_group_creds_one("0", "root", 0);
376 test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY);
377 test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY);
378 }
379
TEST(make_salt)380 TEST(make_salt) {
381 _cleanup_free_ char *s, *t;
382
383 assert_se(make_salt(&s) == 0);
384 log_info("got %s", s);
385
386 assert_se(make_salt(&t) == 0);
387 log_info("got %s", t);
388
389 assert_se(!streq(s, t));
390 }
391
TEST(in_gid)392 TEST(in_gid) {
393 assert_se(in_gid(getgid()) >= 0);
394 assert_se(in_gid(getegid()) >= 0);
395 assert_se(in_gid(GID_INVALID) < 0);
396 assert_se(in_gid(TTY_GID) == 0); /* The TTY gid is for owning ttys, it would be really really weird if we were in it. */
397 }
398
TEST(gid_lists_ops)399 TEST(gid_lists_ops) {
400 static const gid_t l1[] = { 5, 10, 15, 20, 25};
401 static const gid_t l2[] = { 1, 2, 3, 15, 20, 25};
402 static const gid_t l3[] = { 5, 10, 15, 20, 25, 26, 27};
403 static const gid_t l4[] = { 25, 26, 20, 15, 5, 27, 10};
404
405 static const gid_t result1[] = {1, 2, 3, 5, 10, 15, 20, 25, 26, 27};
406 static const gid_t result2[] = {5, 10, 15, 20, 25, 26, 27};
407
408 _cleanup_free_ gid_t *gids = NULL;
409 _cleanup_free_ gid_t *res1 = NULL;
410 _cleanup_free_ gid_t *res2 = NULL;
411 _cleanup_free_ gid_t *res3 = NULL;
412 _cleanup_free_ gid_t *res4 = NULL;
413 int nresult;
414
415 nresult = merge_gid_lists(l2, ELEMENTSOF(l2), l3, ELEMENTSOF(l3), &res1);
416 assert_se(nresult >= 0);
417 assert_se(memcmp_nn(res1, nresult, result1, ELEMENTSOF(result1)) == 0);
418
419 nresult = merge_gid_lists(NULL, 0, l2, ELEMENTSOF(l2), &res2);
420 assert_se(nresult >= 0);
421 assert_se(memcmp_nn(res2, nresult, l2, ELEMENTSOF(l2)) == 0);
422
423 nresult = merge_gid_lists(l1, ELEMENTSOF(l1), l1, ELEMENTSOF(l1), &res3);
424 assert_se(nresult >= 0);
425 assert_se(memcmp_nn(l1, ELEMENTSOF(l1), res3, nresult) == 0);
426
427 nresult = merge_gid_lists(l1, ELEMENTSOF(l1), l4, ELEMENTSOF(l4), &res4);
428 assert_se(nresult >= 0);
429 assert_se(memcmp_nn(result2, ELEMENTSOF(result2), res4, nresult) == 0);
430
431 nresult = getgroups_alloc(&gids);
432 assert_se(nresult >= 0 || nresult == -EINVAL || nresult == -ENOMEM);
433 assert_se(gids);
434 }
435
TEST(parse_uid_range)436 TEST(parse_uid_range) {
437 uid_t a = 4711, b = 4711;
438
439 assert_se(parse_uid_range("", &a, &b) == -EINVAL && a == 4711 && b == 4711);
440 assert_se(parse_uid_range(" ", &a, &b) == -EINVAL && a == 4711 && b == 4711);
441 assert_se(parse_uid_range("x", &a, &b) == -EINVAL && a == 4711 && b == 4711);
442
443 assert_se(parse_uid_range("0", &a, &b) >= 0 && a == 0 && b == 0);
444 assert_se(parse_uid_range("1", &a, &b) >= 0 && a == 1 && b == 1);
445 assert_se(parse_uid_range("2-2", &a, &b) >= 0 && a == 2 && b == 2);
446 assert_se(parse_uid_range("3-3", &a, &b) >= 0 && a == 3 && b == 3);
447 assert_se(parse_uid_range("4-5", &a, &b) >= 0 && a == 4 && b == 5);
448
449 assert_se(parse_uid_range("7-6", &a, &b) == -EINVAL && a == 4 && b == 5);
450 assert_se(parse_uid_range("-1", &a, &b) == -EINVAL && a == 4 && b == 5);
451 assert_se(parse_uid_range("01", &a, &b) == -EINVAL && a == 4 && b == 5);
452 assert_se(parse_uid_range("001", &a, &b) == -EINVAL && a == 4 && b == 5);
453 assert_se(parse_uid_range("+1", &a, &b) == -EINVAL && a == 4 && b == 5);
454 assert_se(parse_uid_range("1--1", &a, &b) == -EINVAL && a == 4 && b == 5);
455 assert_se(parse_uid_range(" 1", &a, &b) == -EINVAL && a == 4 && b == 5);
456 assert_se(parse_uid_range(" 1-2", &a, &b) == -EINVAL && a == 4 && b == 5);
457 assert_se(parse_uid_range("1 -2", &a, &b) == -EINVAL && a == 4 && b == 5);
458 assert_se(parse_uid_range("1- 2", &a, &b) == -EINVAL && a == 4 && b == 5);
459 assert_se(parse_uid_range("1-2 ", &a, &b) == -EINVAL && a == 4 && b == 5);
460 assert_se(parse_uid_range("01-2", &a, &b) == -EINVAL && a == 4 && b == 5);
461 assert_se(parse_uid_range("1-02", &a, &b) == -EINVAL && a == 4 && b == 5);
462 assert_se(parse_uid_range("001-2", &a, &b) == -EINVAL && a == 4 && b == 5);
463 assert_se(parse_uid_range("1-002", &a, &b) == -EINVAL && a == 4 && b == 5);
464 assert_se(parse_uid_range(" 01", &a, &b) == -EINVAL && a == 4 && b == 5);
465 }
466
test_mangle_gecos_one(const char * input,const char * expected)467 static void test_mangle_gecos_one(const char *input, const char *expected) {
468 _cleanup_free_ char *p = NULL;
469
470 assert_se(p = mangle_gecos(input));
471 assert_se(streq(p, expected));
472 assert_se(valid_gecos(p));
473 }
474
TEST(mangle_gecos)475 TEST(mangle_gecos) {
476 test_mangle_gecos_one("", "");
477 test_mangle_gecos_one("root", "root");
478 test_mangle_gecos_one("wuff\nwuff", "wuff wuff");
479 test_mangle_gecos_one("wuff:wuff", "wuff wuff");
480 test_mangle_gecos_one("wuff\r\n:wuff", "wuff wuff");
481 test_mangle_gecos_one("\n--wüff-wäff-wöff::", " --wüff-wäff-wöff ");
482 test_mangle_gecos_one("\xc3\x28", " (");
483 test_mangle_gecos_one("\xe2\x28\xa1", " ( ");
484 }
485
486 DEFINE_TEST_MAIN(LOG_INFO);
487