1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "offline-passwd.h"
6 #include "user-util.h"
7 #include "format-util.h"
8 #include "tests.h"
9
10 static char *arg_root = NULL;
11
test_resolve_one(const char * name)12 static void test_resolve_one(const char *name) {
13 bool relaxed = name || arg_root;
14
15 if (!name)
16 name = "root";
17
18 log_info("/* %s(\"%s\") */", __func__, name);
19
20 _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL;
21 uid_t uid = UID_INVALID;
22 gid_t gid = GID_INVALID;
23 int r;
24
25 r = name_to_uid_offline(arg_root, name, &uid, &uid_cache);
26 log_info_errno(r, "name_to_uid_offline: %s → "UID_FMT": %m", name, uid);
27 assert_se(relaxed || r == 0);
28
29 r = name_to_uid_offline(arg_root, name, &uid, &uid_cache);
30 log_info_errno(r, "name_to_uid_offline: %s → "UID_FMT": %m", name, uid);
31 assert_se(relaxed || r == 0);
32
33 r = name_to_gid_offline(arg_root, name, &gid, &gid_cache);
34 log_info_errno(r, "name_to_gid_offline: %s → "GID_FMT": %m", name, gid);
35 assert_se(relaxed || r == 0);
36
37 r = name_to_gid_offline(arg_root, name, &gid, &gid_cache);
38 log_info_errno(r, "name_to_gid_offline: %s → "GID_FMT": %m", name, gid);
39 assert_se(relaxed || r == 0);
40 }
41
parse_argv(int argc,char * argv[])42 static int parse_argv(int argc, char *argv[]) {
43 static const struct option options[] = {
44 { "root", required_argument, NULL, 'r' },
45 {}
46 };
47
48 int c;
49
50 assert_se(argc >= 0);
51 assert_se(argv);
52
53 while ((c = getopt_long(argc, argv, "r:", options, NULL)) >= 0)
54 switch (c) {
55 case 'r':
56 arg_root = optarg;
57 break;
58
59 case '?':
60 return -EINVAL;
61
62 default:
63 assert_not_reached();
64 }
65
66 return 0;
67 }
68
main(int argc,char ** argv)69 int main(int argc, char **argv) {
70 int r;
71
72 test_setup_logging(LOG_DEBUG);
73
74 r = parse_argv(argc, argv);
75 if (r < 0)
76 return r;
77
78 if (optind >= argc)
79 test_resolve_one(NULL);
80 else
81 while (optind < argc)
82 test_resolve_one(argv[optind++]);
83
84 return 0;
85 }
86