Debian Codegraph @

src/test/test-specifier.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include "sd-id128.h"
5
6 #include "alloc-util.h"
7 #include "format-util.h"
8 #include "log.h"
9 #include "process-util.h"
10 #include "runtime-scope.h"
11 #include "specifier.h"
12 #include "stdio-util.h"
13 #include "strv.h"
14 #include "tests.h"
15
16 static void test_specifier_escape_one(const char *a, const char *b) {
17 _cleanup_free_ char *x = NULL;
18
19 x = specifier_escape(a);
20 ASSERT_STREQ(x, b);
21 }
22
23 TEST(specifier_escape) {
24 test_specifier_escape_one(NULL, NULL);
25 test_specifier_escape_one("", "");
26 test_specifier_escape_one("%", "%%");
27 test_specifier_escape_one("foo bar", "foo bar");
28 test_specifier_escape_one("foo%bar", "foo%%bar");
29 test_specifier_escape_one("%%%%%", "%%%%%%%%%%");
30 }
31
32 static void test_specifier_escape_strv_one(char **a, char **b) {
33 _cleanup_strv_free_ char **x = NULL;
34
35 assert_se(specifier_escape_strv(a, &x) >= 0);
36 assert_se(strv_equal(x, b));
37 }
38
39 TEST(specifier_escape_strv) {
40 test_specifier_escape_strv_one(NULL, NULL);
41 test_specifier_escape_strv_one(STRV_MAKE(NULL), STRV_MAKE(NULL));
42 test_specifier_escape_strv_one(STRV_MAKE(""), STRV_MAKE(""));
43 test_specifier_escape_strv_one(STRV_MAKE("foo"), STRV_MAKE("foo"));
44 test_specifier_escape_strv_one(STRV_MAKE("%"), STRV_MAKE("%%"));
45 test_specifier_escape_strv_one(STRV_MAKE("foo", "%", "foo%", "%foo", "foo%foo", "quux", "%%%"),
46 STRV_MAKE("foo", "%%", "foo%%", "%%foo", "foo%%foo", "quux", "%%%%%%"));
47 }
48
49 /* Any specifier functions which don't need an argument. */
50 static const Specifier specifier_table[] = {
51 COMMON_SYSTEM_SPECIFIERS,
52
53 COMMON_CREDS_SPECIFIERS(RUNTIME_SCOPE_USER),
54 { 'h', specifier_user_home, NULL },
55
56 COMMON_TMP_SPECIFIERS,
57 {}
58 };
59
60 TEST(specifier_printf) {
61 static const Specifier table[] = {
62 { 'X', specifier_string, (char*) "AAAA" },
63 { 'Y', specifier_string, (char*) "BBBB" },
64 { 'e', specifier_string, NULL },
65 COMMON_SYSTEM_SPECIFIERS,
66 {}
67 };
68
69 _cleanup_free_ char *w = NULL;
70 int r;
71
72 r = specifier_printf("xxx a=%X b=%Y e=%e yyy", SIZE_MAX, table, NULL, NULL, &w);
73 assert_se(r >= 0);
74 assert_se(w);
75
76 puts(w);
77 ASSERT_STREQ(w, "xxx a=AAAA b=BBBB e= yyy");
78
79 free(w);
80 r = specifier_printf("boot=%b, host=%H, pretty=%q, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w);
81 assert_se(r >= 0);
82 assert_se(w);
83 puts(w);
84
85 w = mfree(w);
86 specifier_printf("os=%o, os-version=%w, build=%B, variant=%W, empty=%e%e%e", SIZE_MAX, table, NULL, NULL, &w);
87 if (w)
88 puts(w);
89 }
90
91 TEST(specifier_real_path) {
92 static const Specifier table[] = {
93 { 'p', specifier_string, "/dev/fd" },
94 { 'y', specifier_real_path, "/dev/fd" },
95 { 'Y', specifier_real_directory, "/dev/fd" },
96 { 'w', specifier_real_path, "/dev/tty" },
97 { 'W', specifier_real_directory, "/dev/tty" },
98 {}
99 };
100
101 _cleanup_free_ char *w = NULL, *expected = NULL;
102 int r;
103
104 r = specifier_printf("p=%p y=%y Y=%Y w=%w W=%W", SIZE_MAX, table, NULL, NULL, &w);
105 if (r < 0) {
106 ASSERT_ERROR(r, ENOENT);
107 return (void) log_tests_skipped_errno(r, "/dev/fd and/or /dev/tty do not exist");
108 }
109
110 ASSERT_OK(asprintf(&expected,
111 "p=/dev/fd y=/proc/"PID_FMT"/fd Y=/proc/"PID_FMT" w=/dev/tty W=/dev",
112 getpid_cached(), getpid_cached()));
113 ASSERT_STREQ(w, expected);
114 }
115
116 TEST(specifier_real_path_missing_file) {
117 static const Specifier table[] = {
118 { 'p', specifier_string, "/dev/-no-such-file--" },
119 { 'y', specifier_real_path, "/dev/-no-such-file--" },
120 { 'Y', specifier_real_directory, "/dev/-no-such-file--" },
121 {}
122 };
123
124 _cleanup_free_ char *w = NULL;
125 int r;
126
127 r = specifier_printf("p=%p y=%y", SIZE_MAX, table, NULL, NULL, &w);
128 assert_se(r == -ENOENT);
129
130 r = specifier_printf("p=%p Y=%Y", SIZE_MAX, table, NULL, NULL, &w);
131 assert_se(r == -ENOENT);
132 }
133
134 TEST(specifiers) {
135 int r;
136
137 for (const Specifier *s = specifier_table; s->specifier; s++) {
138 char spec[3];
139 _cleanup_free_ char *resolved = NULL;
140
141 xsprintf(spec, "%%%c", s->specifier);
142
143 r = specifier_printf(spec, SIZE_MAX, specifier_table, NULL, NULL, &resolved);
144 if (s->specifier == 'A' && r == -EUNATCH) /* os-release might be missing in build chroots */
145 continue;
146 if (s->specifier == 'm' && IN_SET(r, -EUNATCH, -ENOMEDIUM, -ENOPKG)) /* machine-id might be missing in build chroots */
147 continue;
148 assert_se(r >= 0);
149
150 log_info("%%%c → %s", s->specifier, resolved);
151 }
152 }
153
154 /* Bunch of specifiers that are not part of the common lists */
155 TEST(specifiers_assorted) {
156 const sd_id128_t id = SD_ID128_ALLF;
157 const uint64_t llu = UINT64_MAX;
158 const Specifier table[] = {
159 /* Used in src/repart/repart.c */
160 { 'a', specifier_uuid, &id },
161 { 'b', specifier_uint64, &llu },
162 {}
163 };
164
165 for (const Specifier *s = table; s->specifier; s++) {
166 char spec[3];
167 _cleanup_free_ char *resolved = NULL;
168 int r;
169
170 xsprintf(spec, "%%%c", s->specifier);
171
172 r = specifier_printf(spec, SIZE_MAX, table, NULL, NULL, &resolved);
173 assert_se(r >= 0);
174
175 log_info("%%%c → %s", s->specifier, resolved);
176 }
177 }
178
179 TEST(specifiers_missing_data_ok) {
180 _cleanup_free_ char *resolved = NULL;
181
182 assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0);
183 assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) >= 0);
184 ASSERT_STREQ(resolved, "-----");
185
186 assert_se(setenv("SYSTEMD_OS_RELEASE", "/nosuchfileordirectory", 1) == 0);
187 assert_se(specifier_printf("%A-%B-%M-%o-%w-%W", SIZE_MAX, specifier_table, NULL, NULL, &resolved) == -EUNATCH);
188 ASSERT_STREQ(resolved, "-----");
189
190 assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
191 }
192
193 DEFINE_TEST_MAIN(LOG_DEBUG);