]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2012 Lennart Poettering | |
5 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
e842803a | 6 | Copyright 2014 Ronny Chevalier |
663996b3 MS |
7 | |
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
db2df898 | 22 | #include <pwd.h> |
663996b3 MS |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
663996b3 | 26 | |
db2df898 | 27 | #include "alloc-util.h" |
4c89c718 | 28 | #include "glob-util.h" |
db2df898 MP |
29 | #include "hostname-util.h" |
30 | #include "macro.h" | |
663996b3 | 31 | #include "manager.h" |
db2df898 MP |
32 | #include "path-util.h" |
33 | #include "specifier.h" | |
34 | #include "string-util.h" | |
35 | #include "test-helper.h" | |
663996b3 MS |
36 | #include "unit-name.h" |
37 | #include "unit-printf.h" | |
db2df898 MP |
38 | #include "unit.h" |
39 | #include "user-util.h" | |
663996b3 | 40 | #include "util.h" |
e3bff60a MP |
41 | |
42 | static void test_unit_name_is_valid(void) { | |
43 | assert_se(unit_name_is_valid("foo.service", UNIT_NAME_ANY)); | |
44 | assert_se(unit_name_is_valid("foo.service", UNIT_NAME_PLAIN)); | |
45 | assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_INSTANCE)); | |
46 | assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_TEMPLATE)); | |
47 | assert_se(!unit_name_is_valid("foo.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)); | |
48 | ||
49 | assert_se(unit_name_is_valid("foo@bar.service", UNIT_NAME_ANY)); | |
50 | assert_se(!unit_name_is_valid("foo@bar.service", UNIT_NAME_PLAIN)); | |
51 | assert_se(unit_name_is_valid("foo@bar.service", UNIT_NAME_INSTANCE)); | |
52 | assert_se(!unit_name_is_valid("foo@bar.service", UNIT_NAME_TEMPLATE)); | |
53 | assert_se(unit_name_is_valid("foo@bar.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)); | |
54 | ||
55 | assert_se(unit_name_is_valid("foo@.service", UNIT_NAME_ANY)); | |
56 | assert_se(!unit_name_is_valid("foo@.service", UNIT_NAME_PLAIN)); | |
57 | assert_se(!unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE)); | |
58 | assert_se(unit_name_is_valid("foo@.service", UNIT_NAME_TEMPLATE)); | |
59 | assert_se(unit_name_is_valid("foo@.service", UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)); | |
60 | ||
61 | assert_se(!unit_name_is_valid(".service", UNIT_NAME_ANY)); | |
62 | assert_se(!unit_name_is_valid("", UNIT_NAME_ANY)); | |
63 | assert_se(!unit_name_is_valid("foo.waldo", UNIT_NAME_ANY)); | |
64 | assert_se(!unit_name_is_valid("@.service", UNIT_NAME_ANY)); | |
65 | assert_se(!unit_name_is_valid("@piep.service", UNIT_NAME_ANY)); | |
66 | } | |
663996b3 | 67 | |
4c89c718 | 68 | static void test_unit_name_replace_instance_one(const char *pattern, const char *repl, const char *expected, int ret) { |
e3bff60a MP |
69 | _cleanup_free_ char *t = NULL; |
70 | assert_se(unit_name_replace_instance(pattern, repl, &t) == ret); | |
71 | puts(strna(t)); | |
72 | assert_se(streq_ptr(t, expected)); | |
73 | } | |
663996b3 | 74 | |
4c89c718 | 75 | static void test_unit_name_replace_instance(void) { |
663996b3 | 76 | puts("-------------------------------------------------"); |
4c89c718 MP |
77 | test_unit_name_replace_instance_one("foo@.service", "waldo", "foo@waldo.service", 0); |
78 | test_unit_name_replace_instance_one("foo@xyz.service", "waldo", "foo@waldo.service", 0); | |
79 | test_unit_name_replace_instance_one("xyz", "waldo", NULL, -EINVAL); | |
80 | test_unit_name_replace_instance_one("", "waldo", NULL, -EINVAL); | |
81 | test_unit_name_replace_instance_one("foo.service", "waldo", NULL, -EINVAL); | |
82 | test_unit_name_replace_instance_one(".service", "waldo", NULL, -EINVAL); | |
83 | test_unit_name_replace_instance_one("foo@", "waldo", NULL, -EINVAL); | |
84 | test_unit_name_replace_instance_one("@bar", "waldo", NULL, -EINVAL); | |
e3bff60a | 85 | } |
663996b3 | 86 | |
4c89c718 | 87 | static void test_unit_name_from_path_one(const char *path, const char *suffix, const char *expected, int ret) { |
e3bff60a | 88 | _cleanup_free_ char *t = NULL; |
663996b3 | 89 | |
e3bff60a MP |
90 | assert_se(unit_name_from_path(path, suffix, &t) == ret); |
91 | puts(strna(t)); | |
92 | assert_se(streq_ptr(t, expected)); | |
663996b3 | 93 | |
e3bff60a MP |
94 | if (t) { |
95 | _cleanup_free_ char *k = NULL; | |
96 | assert_se(unit_name_to_path(t, &k) == 0); | |
97 | puts(strna(k)); | |
98 | assert_se(path_equal(k, isempty(path) ? "/" : path)); | |
99 | } | |
100 | } | |
663996b3 | 101 | |
4c89c718 | 102 | static void test_unit_name_from_path(void) { |
663996b3 | 103 | puts("-------------------------------------------------"); |
4c89c718 MP |
104 | test_unit_name_from_path_one("/waldo", ".mount", "waldo.mount", 0); |
105 | test_unit_name_from_path_one("/waldo/quuix", ".mount", "waldo-quuix.mount", 0); | |
106 | test_unit_name_from_path_one("/waldo/quuix/", ".mount", "waldo-quuix.mount", 0); | |
107 | test_unit_name_from_path_one("", ".mount", "-.mount", 0); | |
108 | test_unit_name_from_path_one("/", ".mount", "-.mount", 0); | |
109 | test_unit_name_from_path_one("///", ".mount", "-.mount", 0); | |
110 | test_unit_name_from_path_one("/foo/../bar", ".mount", NULL, -EINVAL); | |
111 | test_unit_name_from_path_one("/foo/./bar", ".mount", NULL, -EINVAL); | |
e3bff60a MP |
112 | } |
113 | ||
4c89c718 | 114 | static void test_unit_name_from_path_instance_one(const char *pattern, const char *path, const char *suffix, const char *expected, int ret) { |
e3bff60a MP |
115 | _cleanup_free_ char *t = NULL; |
116 | ||
117 | assert_se(unit_name_from_path_instance(pattern, path, suffix, &t) == ret); | |
118 | puts(strna(t)); | |
119 | assert_se(streq_ptr(t, expected)); | |
120 | ||
121 | if (t) { | |
122 | _cleanup_free_ char *k = NULL, *v = NULL; | |
123 | ||
124 | assert_se(unit_name_to_instance(t, &k) > 0); | |
125 | assert_se(unit_name_path_unescape(k, &v) == 0); | |
126 | assert_se(path_equal(v, isempty(path) ? "/" : path)); | |
663996b3 | 127 | } |
e3bff60a | 128 | } |
663996b3 | 129 | |
4c89c718 | 130 | static void test_unit_name_from_path_instance(void) { |
e3bff60a | 131 | puts("-------------------------------------------------"); |
663996b3 | 132 | |
4c89c718 MP |
133 | test_unit_name_from_path_instance_one("waldo", "/waldo", ".mount", "waldo@waldo.mount", 0); |
134 | test_unit_name_from_path_instance_one("waldo", "/waldo////quuix////", ".mount", "waldo@waldo-quuix.mount", 0); | |
135 | test_unit_name_from_path_instance_one("waldo", "/", ".mount", "waldo@-.mount", 0); | |
136 | test_unit_name_from_path_instance_one("waldo", "", ".mount", "waldo@-.mount", 0); | |
137 | test_unit_name_from_path_instance_one("waldo", "///", ".mount", "waldo@-.mount", 0); | |
138 | test_unit_name_from_path_instance_one("waldo", "..", ".mount", NULL, -EINVAL); | |
139 | test_unit_name_from_path_instance_one("waldo", "/foo", ".waldi", NULL, -EINVAL); | |
140 | test_unit_name_from_path_instance_one("wa--ldo", "/--", ".mount", "wa--ldo@\\x2d\\x2d.mount", 0); | |
e3bff60a MP |
141 | } |
142 | ||
4c89c718 | 143 | static void test_unit_name_to_path_one(const char *unit, const char *path, int ret) { |
e3bff60a MP |
144 | _cleanup_free_ char *p = NULL; |
145 | ||
146 | assert_se(unit_name_to_path(unit, &p) == ret); | |
147 | assert_se(streq_ptr(path, p)); | |
148 | } | |
149 | ||
4c89c718 MP |
150 | static void test_unit_name_to_path(void) { |
151 | test_unit_name_to_path_one("home.mount", "/home", 0); | |
152 | test_unit_name_to_path_one("home-lennart.mount", "/home/lennart", 0); | |
153 | test_unit_name_to_path_one("home-lennart-.mount", NULL, -EINVAL); | |
154 | test_unit_name_to_path_one("-home-lennart.mount", NULL, -EINVAL); | |
155 | test_unit_name_to_path_one("-home--lennart.mount", NULL, -EINVAL); | |
156 | test_unit_name_to_path_one("home-..-lennart.mount", NULL, -EINVAL); | |
157 | test_unit_name_to_path_one("", NULL, -EINVAL); | |
158 | test_unit_name_to_path_one("home/foo", NULL, -EINVAL); | |
e3bff60a MP |
159 | } |
160 | ||
4c89c718 | 161 | static void test_unit_name_mangle_one(UnitNameMangle allow_globs, const char *pattern, const char *expect, int ret) { |
e3bff60a MP |
162 | _cleanup_free_ char *t = NULL; |
163 | ||
4c89c718 | 164 | assert_se(unit_name_mangle(pattern, allow_globs, &t) == ret); |
e3bff60a MP |
165 | puts(strna(t)); |
166 | assert_se(streq_ptr(t, expect)); | |
167 | ||
168 | if (t) { | |
169 | _cleanup_free_ char *k = NULL; | |
170 | ||
4c89c718 MP |
171 | assert_se(unit_name_is_valid(t, UNIT_NAME_ANY) || |
172 | (allow_globs == UNIT_NAME_GLOB && string_is_glob(t))); | |
e3bff60a | 173 | |
4c89c718 | 174 | assert_se(unit_name_mangle(t, allow_globs, &k) == 0); |
e3bff60a MP |
175 | assert_se(streq_ptr(t, k)); |
176 | } | |
177 | } | |
178 | ||
4c89c718 | 179 | static void test_unit_name_mangle(void) { |
e3bff60a | 180 | puts("-------------------------------------------------"); |
4c89c718 MP |
181 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "foo.service", "foo.service", 0); |
182 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "/home", "home.mount", 1); | |
183 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "/dev/sda", "dev-sda.device", 1); | |
184 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "üxknürz.service", "\\xc3\\xbcxkn\\xc3\\xbcrz.service", 1); | |
185 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "foobar-meh...waldi.service", "foobar-meh...waldi.service", 0); | |
186 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "_____####----.....service", "_____\\x23\\x23\\x23\\x23----.....service", 1); | |
187 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "_____##@;;;,,,##----.....service", "_____\\x23\\x23@\\x3b\\x3b\\x3b\\x2c\\x2c\\x2c\\x23\\x23----.....service", 1); | |
188 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "xxx@@@@/////\\\\\\\\\\yyy.service", "xxx@@@@-----\\\\\\\\\\yyy.service", 1); | |
189 | test_unit_name_mangle_one(UNIT_NAME_NOGLOB, "", NULL, -EINVAL); | |
190 | ||
191 | test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo.service", "foo.service", 0); | |
192 | test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo", "foo.service", 1); | |
193 | test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo*", "foo*", 0); | |
194 | test_unit_name_mangle_one(UNIT_NAME_GLOB, "ü*", "\\xc3\\xbc*", 1); | |
663996b3 MS |
195 | } |
196 | ||
197 | static int test_unit_printf(void) { | |
60f067b4 | 198 | Manager *m = NULL; |
663996b3 MS |
199 | Unit *u, *u2; |
200 | int r; | |
201 | ||
db2df898 | 202 | _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *uid = NULL, *user = NULL, *shell = NULL, *home = NULL; |
663996b3 | 203 | |
14228c0d MB |
204 | assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid); |
205 | assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid); | |
db2df898 MP |
206 | assert_se(host = gethostname_malloc()); |
207 | assert_se(user = getusername_malloc()); | |
208 | assert_se(asprintf(&uid, UID_FMT, getuid())); | |
209 | assert_se(get_home_dir(&home) >= 0); | |
210 | assert_se(get_shell(&shell) >= 0); | |
663996b3 | 211 | |
e3bff60a | 212 | r = manager_new(MANAGER_USER, true, &m); |
60f067b4 | 213 | if (r == -EPERM || r == -EACCES || r == -EADDRINUSE) { |
663996b3 MS |
214 | puts("manager_new: Permission denied. Skipping test."); |
215 | return EXIT_TEST_SKIP; | |
216 | } | |
5eef597e | 217 | assert_se(r == 0); |
663996b3 MS |
218 | |
219 | #define expect(unit, pattern, expected) \ | |
220 | { \ | |
221 | char *e; \ | |
f47781d8 | 222 | _cleanup_free_ char *t = NULL; \ |
14228c0d | 223 | assert_se(unit_full_printf(unit, pattern, &t) >= 0); \ |
663996b3 MS |
224 | printf("result: %s\nexpect: %s\n", t, expected); \ |
225 | if ((e = endswith(expected, "*"))) \ | |
5eef597e | 226 | assert_se(strncmp(t, e, e-expected)); \ |
663996b3 | 227 | else \ |
5eef597e | 228 | assert_se(streq(t, expected)); \ |
663996b3 MS |
229 | } |
230 | ||
e842803a | 231 | assert_se(setenv("XDG_RUNTIME_DIR", "/run/user/1/", 1) == 0); |
663996b3 MS |
232 | |
233 | assert_se(u = unit_new(m, sizeof(Service))); | |
234 | assert_se(unit_add_name(u, "blah.service") == 0); | |
235 | assert_se(unit_add_name(u, "blah.service") == 0); | |
236 | ||
237 | /* general tests */ | |
238 | expect(u, "%%", "%"); | |
239 | expect(u, "%%s", "%s"); | |
240 | expect(u, "%", ""); // REALLY? | |
241 | ||
242 | /* normal unit */ | |
243 | expect(u, "%n", "blah.service"); | |
e735f4d4 | 244 | expect(u, "%f", "/blah"); |
663996b3 MS |
245 | expect(u, "%N", "blah"); |
246 | expect(u, "%p", "blah"); | |
247 | expect(u, "%P", "blah"); | |
248 | expect(u, "%i", ""); | |
db2df898 MP |
249 | expect(u, "%u", user); |
250 | expect(u, "%U", uid); | |
251 | expect(u, "%h", home); | |
663996b3 MS |
252 | expect(u, "%m", mid); |
253 | expect(u, "%b", bid); | |
254 | expect(u, "%H", host); | |
255 | expect(u, "%t", "/run/user/*"); | |
256 | ||
257 | /* templated */ | |
258 | assert_se(u2 = unit_new(m, sizeof(Service))); | |
259 | assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0); | |
260 | assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0); | |
261 | ||
262 | expect(u2, "%n", "blah@foo-foo.service"); | |
263 | expect(u2, "%N", "blah@foo-foo"); | |
e735f4d4 | 264 | expect(u2, "%f", "/foo/foo"); |
663996b3 MS |
265 | expect(u2, "%p", "blah"); |
266 | expect(u2, "%P", "blah"); | |
267 | expect(u2, "%i", "foo-foo"); | |
268 | expect(u2, "%I", "foo/foo"); | |
db2df898 MP |
269 | expect(u2, "%u", user); |
270 | expect(u2, "%U", uid); | |
271 | expect(u2, "%h", home); | |
663996b3 MS |
272 | expect(u2, "%m", mid); |
273 | expect(u2, "%b", bid); | |
274 | expect(u2, "%H", host); | |
275 | expect(u2, "%t", "/run/user/*"); | |
276 | ||
14228c0d | 277 | manager_free(m); |
e735f4d4 | 278 | #undef expect |
14228c0d | 279 | |
663996b3 MS |
280 | return 0; |
281 | } | |
282 | ||
e842803a MB |
283 | static void test_unit_instance_is_valid(void) { |
284 | assert_se(unit_instance_is_valid("fooBar")); | |
285 | assert_se(unit_instance_is_valid("foo-bar")); | |
286 | assert_se(unit_instance_is_valid("foo.stUff")); | |
287 | assert_se(unit_instance_is_valid("fOo123.stuff")); | |
288 | assert_se(unit_instance_is_valid("@f_oo123.Stuff")); | |
289 | ||
290 | assert_se(!unit_instance_is_valid("$¢£")); | |
291 | assert_se(!unit_instance_is_valid("")); | |
292 | assert_se(!unit_instance_is_valid("foo bar")); | |
293 | assert_se(!unit_instance_is_valid("foo/bar")); | |
294 | } | |
295 | ||
296 | static void test_unit_prefix_is_valid(void) { | |
297 | assert_se(unit_prefix_is_valid("fooBar")); | |
298 | assert_se(unit_prefix_is_valid("foo-bar")); | |
299 | assert_se(unit_prefix_is_valid("foo.stUff")); | |
300 | assert_se(unit_prefix_is_valid("fOo123.stuff")); | |
301 | assert_se(unit_prefix_is_valid("foo123.Stuff")); | |
302 | ||
303 | assert_se(!unit_prefix_is_valid("$¢£")); | |
304 | assert_se(!unit_prefix_is_valid("")); | |
305 | assert_se(!unit_prefix_is_valid("foo bar")); | |
306 | assert_se(!unit_prefix_is_valid("foo/bar")); | |
307 | assert_se(!unit_prefix_is_valid("@foo-bar")); | |
308 | } | |
309 | ||
310 | static void test_unit_name_change_suffix(void) { | |
e3bff60a | 311 | char *t; |
e842803a | 312 | |
e3bff60a MP |
313 | assert_se(unit_name_change_suffix("foo.mount", ".service", &t) == 0); |
314 | assert_se(streq(t, "foo.service")); | |
315 | free(t); | |
e842803a | 316 | |
e3bff60a MP |
317 | assert_se(unit_name_change_suffix("foo@stuff.service", ".socket", &t) == 0); |
318 | assert_se(streq(t, "foo@stuff.socket")); | |
319 | free(t); | |
e842803a MB |
320 | } |
321 | ||
322 | static void test_unit_name_build(void) { | |
e3bff60a | 323 | char *t; |
e842803a | 324 | |
e3bff60a MP |
325 | assert_se(unit_name_build("foo", "bar", ".service", &t) == 0); |
326 | assert_se(streq(t, "foo@bar.service")); | |
327 | free(t); | |
e842803a | 328 | |
e3bff60a MP |
329 | assert_se(unit_name_build("fo0-stUff_b", "bar", ".mount", &t) == 0); |
330 | assert_se(streq(t, "fo0-stUff_b@bar.mount")); | |
331 | free(t); | |
e842803a | 332 | |
e3bff60a MP |
333 | assert_se(unit_name_build("foo", NULL, ".service", &t) == 0); |
334 | assert_se(streq(t, "foo.service")); | |
335 | free(t); | |
e842803a MB |
336 | } |
337 | ||
e3bff60a MP |
338 | static void test_slice_name_is_valid(void) { |
339 | assert_se(slice_name_is_valid("-.slice")); | |
340 | assert_se(slice_name_is_valid("foo.slice")); | |
341 | assert_se(slice_name_is_valid("foo-bar.slice")); | |
342 | assert_se(slice_name_is_valid("foo-bar-baz.slice")); | |
343 | assert_se(!slice_name_is_valid("-foo-bar-baz.slice")); | |
344 | assert_se(!slice_name_is_valid("foo-bar-baz-.slice")); | |
345 | assert_se(!slice_name_is_valid("-foo-bar-baz-.slice")); | |
346 | assert_se(!slice_name_is_valid("foo-bar--baz.slice")); | |
347 | assert_se(!slice_name_is_valid("foo--bar--baz.slice")); | |
348 | assert_se(!slice_name_is_valid(".slice")); | |
349 | assert_se(!slice_name_is_valid("")); | |
350 | assert_se(!slice_name_is_valid("foo.service")); | |
e842803a MB |
351 | } |
352 | ||
353 | static void test_build_subslice(void) { | |
354 | char *a; | |
355 | char *b; | |
356 | ||
e3bff60a MP |
357 | assert_se(slice_build_subslice("-.slice", "foo", &a) >= 0); |
358 | assert_se(slice_build_subslice(a, "bar", &b) >= 0); | |
e842803a | 359 | free(a); |
e3bff60a | 360 | assert_se(slice_build_subslice(b, "barfoo", &a) >= 0); |
e842803a | 361 | free(b); |
e3bff60a | 362 | assert_se(slice_build_subslice(a, "foobar", &b) >= 0); |
e842803a MB |
363 | free(a); |
364 | assert_se(streq(b, "foo-bar-barfoo-foobar.slice")); | |
365 | free(b); | |
366 | ||
e3bff60a MP |
367 | assert_se(slice_build_subslice("foo.service", "bar", &a) < 0); |
368 | assert_se(slice_build_subslice("foo", "bar", &a) < 0); | |
369 | } | |
370 | ||
371 | static void test_build_parent_slice_one(const char *name, const char *expect, int ret) { | |
372 | _cleanup_free_ char *s = NULL; | |
373 | ||
374 | assert_se(slice_build_parent_slice(name, &s) == ret); | |
375 | assert_se(streq_ptr(s, expect)); | |
376 | } | |
377 | ||
378 | static void test_build_parent_slice(void) { | |
379 | test_build_parent_slice_one("-.slice", NULL, 0); | |
380 | test_build_parent_slice_one("foo.slice", "-.slice", 1); | |
381 | test_build_parent_slice_one("foo-bar.slice", "foo.slice", 1); | |
382 | test_build_parent_slice_one("foo-bar-baz.slice", "foo-bar.slice", 1); | |
383 | test_build_parent_slice_one("foo-bar--baz.slice", NULL, -EINVAL); | |
384 | test_build_parent_slice_one("-foo-bar.slice", NULL, -EINVAL); | |
385 | test_build_parent_slice_one("foo-bar-.slice", NULL, -EINVAL); | |
386 | test_build_parent_slice_one("foo-bar.service", NULL, -EINVAL); | |
387 | test_build_parent_slice_one(".slice", NULL, -EINVAL); | |
e842803a MB |
388 | } |
389 | ||
390 | static void test_unit_name_to_instance(void) { | |
391 | char *instance; | |
392 | int r; | |
393 | ||
394 | r = unit_name_to_instance("foo@bar.service", &instance); | |
395 | assert_se(r >= 0); | |
396 | assert_se(streq(instance, "bar")); | |
397 | free(instance); | |
398 | ||
e735f4d4 MP |
399 | r = unit_name_to_instance("foo@.service", &instance); |
400 | assert_se(r >= 0); | |
401 | assert_se(streq(instance, "")); | |
402 | free(instance); | |
403 | ||
e3bff60a | 404 | r = unit_name_to_instance("fo0-stUff_b@b.service", &instance); |
e842803a MB |
405 | assert_se(r >= 0); |
406 | assert_se(streq(instance, "b")); | |
407 | free(instance); | |
408 | ||
e3bff60a MP |
409 | r = unit_name_to_instance("foo.service", &instance); |
410 | assert_se(r == 0); | |
e842803a MB |
411 | assert_se(!instance); |
412 | ||
413 | r = unit_name_to_instance("fooj@unk", &instance); | |
414 | assert_se(r < 0); | |
e735f4d4 MP |
415 | |
416 | r = unit_name_to_instance("foo@", &instance); | |
417 | assert_se(r < 0); | |
e842803a MB |
418 | } |
419 | ||
420 | static void test_unit_name_escape(void) { | |
421 | _cleanup_free_ char *r; | |
422 | ||
423 | r = unit_name_escape("ab+-c.a/bc@foo.service"); | |
424 | assert_se(r); | |
425 | assert_se(streq(r, "ab\\x2b\\x2dc.a-bc\\x40foo.service")); | |
426 | } | |
427 | ||
e3bff60a MP |
428 | |
429 | static void test_u_n_t_one(const char *name, const char *expected, int ret) { | |
430 | _cleanup_free_ char *f = NULL; | |
431 | ||
432 | assert_se(unit_name_template(name, &f) == ret); | |
433 | printf("got: %s, expected: %s\n", strna(f), strna(expected)); | |
434 | assert_se(streq_ptr(f, expected)); | |
435 | } | |
436 | ||
e735f4d4 | 437 | static void test_unit_name_template(void) { |
e3bff60a MP |
438 | test_u_n_t_one("foo@bar.service", "foo@.service", 0); |
439 | test_u_n_t_one("foo.mount", NULL, -EINVAL); | |
e735f4d4 MP |
440 | } |
441 | ||
e3bff60a MP |
442 | static void test_unit_name_path_unescape_one(const char *name, const char *path, int ret) { |
443 | _cleanup_free_ char *p = NULL; | |
444 | ||
445 | assert_se(unit_name_path_unescape(name, &p) == ret); | |
446 | assert_se(streq_ptr(path, p)); | |
447 | } | |
e735f4d4 | 448 | |
e3bff60a MP |
449 | static void test_unit_name_path_unescape(void) { |
450 | ||
451 | test_unit_name_path_unescape_one("foo", "/foo", 0); | |
452 | test_unit_name_path_unescape_one("foo-bar", "/foo/bar", 0); | |
453 | test_unit_name_path_unescape_one("foo-.bar", "/foo/.bar", 0); | |
454 | test_unit_name_path_unescape_one("foo-bar-baz", "/foo/bar/baz", 0); | |
455 | test_unit_name_path_unescape_one("-", "/", 0); | |
456 | test_unit_name_path_unescape_one("--", NULL, -EINVAL); | |
457 | test_unit_name_path_unescape_one("-foo-bar", NULL, -EINVAL); | |
458 | test_unit_name_path_unescape_one("foo--bar", NULL, -EINVAL); | |
459 | test_unit_name_path_unescape_one("foo-bar-", NULL, -EINVAL); | |
460 | test_unit_name_path_unescape_one(".-bar", NULL, -EINVAL); | |
461 | test_unit_name_path_unescape_one("foo-..", NULL, -EINVAL); | |
462 | test_unit_name_path_unescape_one("", NULL, -EINVAL); | |
e735f4d4 MP |
463 | } |
464 | ||
663996b3 | 465 | int main(int argc, char* argv[]) { |
14228c0d | 466 | int rc = 0; |
e3bff60a | 467 | test_unit_name_is_valid(); |
4c89c718 MP |
468 | test_unit_name_replace_instance(); |
469 | test_unit_name_from_path(); | |
470 | test_unit_name_from_path_instance(); | |
471 | test_unit_name_mangle(); | |
472 | test_unit_name_to_path(); | |
14228c0d | 473 | TEST_REQ_RUNNING_SYSTEMD(rc = test_unit_printf()); |
e842803a MB |
474 | test_unit_instance_is_valid(); |
475 | test_unit_prefix_is_valid(); | |
476 | test_unit_name_change_suffix(); | |
477 | test_unit_name_build(); | |
e3bff60a | 478 | test_slice_name_is_valid(); |
e842803a | 479 | test_build_subslice(); |
e3bff60a | 480 | test_build_parent_slice(); |
e842803a MB |
481 | test_unit_name_to_instance(); |
482 | test_unit_name_escape(); | |
e735f4d4 | 483 | test_unit_name_template(); |
e3bff60a | 484 | test_unit_name_path_unescape(); |
e842803a | 485 | |
14228c0d | 486 | return rc; |
663996b3 | 487 | } |