]>
Commit | Line | Data |
---|---|---|
e82f30d1 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e82f30d1 LP |
2 | |
3 | #include "alloc-util.h" | |
4 | #include "log.h" | |
5 | #include "specifier.h" | |
b6c93a5a | 6 | #include "stdio-util.h" |
e82f30d1 LP |
7 | #include "string-util.h" |
8 | #include "strv.h" | |
6d7c4033 | 9 | #include "tests.h" |
e82f30d1 LP |
10 | |
11 | static void test_specifier_escape_one(const char *a, const char *b) { | |
12 | _cleanup_free_ char *x = NULL; | |
13 | ||
14 | x = specifier_escape(a); | |
15 | assert_se(streq_ptr(x, b)); | |
16 | } | |
17 | ||
18 | static void test_specifier_escape(void) { | |
b6c93a5a ZJS |
19 | log_info("/* %s */", __func__); |
20 | ||
e82f30d1 LP |
21 | test_specifier_escape_one(NULL, NULL); |
22 | test_specifier_escape_one("", ""); | |
23 | test_specifier_escape_one("%", "%%"); | |
24 | test_specifier_escape_one("foo bar", "foo bar"); | |
25 | test_specifier_escape_one("foo%bar", "foo%%bar"); | |
26 | test_specifier_escape_one("%%%%%", "%%%%%%%%%%"); | |
27 | } | |
28 | ||
29 | static void test_specifier_escape_strv_one(char **a, char **b) { | |
30 | _cleanup_strv_free_ char **x = NULL; | |
31 | ||
32 | assert_se(specifier_escape_strv(a, &x) >= 0); | |
33 | assert_se(strv_equal(x, b)); | |
34 | } | |
35 | ||
36 | static void test_specifier_escape_strv(void) { | |
b6c93a5a ZJS |
37 | log_info("/* %s */", __func__); |
38 | ||
e82f30d1 LP |
39 | test_specifier_escape_strv_one(NULL, NULL); |
40 | test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL)); | |
41 | test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE("")); | |
42 | test_specifier_escape_strv_one(STRV_MAKE("foo"), STRV_MAKE("foo")); | |
43 | test_specifier_escape_strv_one(STRV_MAKE("%"), STRV_MAKE("%%")); | |
b6c93a5a ZJS |
44 | test_specifier_escape_strv_one(STRV_MAKE("foo", "%", "foo%", "%foo", "foo%foo", "quux", "%%%"), |
45 | STRV_MAKE("foo", "%%", "foo%%", "%%foo", "foo%%foo", "quux", "%%%%%%")); | |
46 | } | |
47 | ||
48 | /* Any specifier functions which don't need an argument. */ | |
49 | static const Specifier specifier_table[] = { | |
50 | { 'm', specifier_machine_id, NULL }, | |
51 | { 'b', specifier_boot_id, NULL }, | |
52 | { 'H', specifier_host_name, NULL }, | |
53 | { 'l', specifier_short_host_name, NULL }, | |
54 | { 'v', specifier_kernel_release, NULL }, | |
55 | { 'a', specifier_architecture, NULL }, | |
56 | { 'o', specifier_os_id, NULL }, | |
57 | { 'w', specifier_os_version_id, NULL }, | |
58 | { 'B', specifier_os_build_id, NULL }, | |
59 | { 'W', specifier_os_variant_id, NULL }, | |
60 | ||
61 | { 'g', specifier_group_name, NULL }, | |
62 | { 'G', specifier_group_id, NULL }, | |
63 | { 'U', specifier_user_id, NULL }, | |
64 | { 'u', specifier_user_name, NULL }, | |
65 | { 'h', specifier_user_home, NULL }, | |
66 | ||
67 | { 'T', specifier_tmp_dir, NULL }, | |
68 | { 'V', specifier_var_tmp_dir, NULL }, | |
69 | {} | |
70 | }; | |
71 | ||
72 | static void test_specifiers(void) { | |
73 | log_info("/* %s */", __func__); | |
74 | ||
75 | for (const Specifier *s = specifier_table; s->specifier; s++) { | |
76 | char spec[3]; | |
77 | _cleanup_free_ char *resolved = NULL; | |
78 | ||
79 | xsprintf(spec, "%%%c", s->specifier); | |
80 | ||
81 | assert_se(specifier_printf(spec, specifier_table, NULL, &resolved) >= 0); | |
82 | ||
83 | log_info("%%%c → %s", s->specifier, resolved); | |
84 | } | |
e82f30d1 LP |
85 | } |
86 | ||
87 | int main(int argc, char *argv[]) { | |
6d7c4033 | 88 | test_setup_logging(LOG_DEBUG); |
e82f30d1 LP |
89 | |
90 | test_specifier_escape(); | |
91 | test_specifier_escape_strv(); | |
b6c93a5a | 92 | test_specifiers(); |
e82f30d1 LP |
93 | |
94 | return 0; | |
95 | } |