summaryrefslogtreecommitdiff
path: root/src/tmpfiles/test-offline-passwd.c
blob: 1a961d125bbdde3e250224b343d660ca306cf79a (plain)
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <getopt.h>

#include "offline-passwd.h"
#include "user-util.h"
#include "format-util.h"
#include "tests.h"

static char *arg_root = NULL;

static void test_resolve_one(const char *name) {
        bool relaxed = name || arg_root;

        if (!name)
                name = "root";

        log_info("/* %s(\"%s\") */", __func__, name);

        _cleanup_(hashmap_freep) Hashmap *uid_cache = NULL, *gid_cache = NULL;
        uid_t uid = UID_INVALID;
        gid_t gid = GID_INVALID;
        int r;

        r = name_to_uid_offline(arg_root, name, &uid, &uid_cache);
        log_info_errno(r, "name_to_uid_offline: %s → "UID_FMT": %m", name, uid);
        assert_se(relaxed || r == 0);

        r = name_to_uid_offline(arg_root, name, &uid, &uid_cache);
        log_info_errno(r, "name_to_uid_offline: %s → "UID_FMT": %m", name, uid);
        assert_se(relaxed || r == 0);

        r = name_to_gid_offline(arg_root, name, &gid, &gid_cache);
        log_info_errno(r, "name_to_gid_offline: %s → "GID_FMT": %m", name, gid);
        assert_se(relaxed || r == 0);

        r = name_to_gid_offline(arg_root, name, &gid, &gid_cache);
        log_info_errno(r, "name_to_gid_offline: %s → "GID_FMT": %m", name, gid);
        assert_se(relaxed || r == 0);
}

static int parse_argv(int argc, char *argv[]) {
        static const struct option options[] = {
                { "root",           required_argument,   NULL, 'r' },
                {}
        };

        int c;

        assert(argc >= 0);
        assert(argv);

        while ((c = getopt_long(argc, argv, "r:", options, NULL)) >= 0)
                switch(c) {
                case 'r':
                        arg_root = optarg;
                        break;

                case '?':
                        return -EINVAL;

                default:
                        assert_not_reached("Unhandled option");
                }

        return 0;
}

int main(int argc, char **argv) {
        int r;

        test_setup_logging(LOG_DEBUG);

        r = parse_argv(argc, argv);
        if (r < 0)
                return r;

        if (optind >= argc)
                test_resolve_one(NULL);
        else
                while (optind < argc)
                        test_resolve_one(argv[optind++]);

        return 0;
}