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