]>
git.proxmox.com Git - systemd.git/blob - src/test/test-unit-name.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
31 #include "unit-name.h"
32 #include "unit-printf.h"
34 #include "specifier.h"
37 #include "test-helper.h"
39 static void test_replacements(void) {
40 #define expect(pattern, repl, expected) \
42 _cleanup_free_ char *t = \
43 unit_name_replace_instance(pattern, repl); \
45 assert(streq(t, expected)); \
48 expect("foo@.service", "waldo", "foo@waldo.service");
49 expect("foo@xyz.service", "waldo", "foo@waldo.service");
50 expect("xyz", "waldo", "xyz");
51 expect("", "waldo", "");
52 expect("foo.service", "waldo", "foo.service");
53 expect(".service", "waldo", ".service");
54 expect("foo@", "waldo", "foo@waldo");
55 expect("@bar", "waldo", "@waldo");
57 puts("-------------------------------------------------");
59 #define expect(path, suffix, expected) \
61 _cleanup_free_ char *k, *t = \
62 unit_name_from_path(path, suffix); \
64 k = unit_name_to_path(t); \
66 assert(streq(k, expected ? expected : path)); \
69 expect("/waldo", ".mount", NULL
);
70 expect("/waldo/quuix", ".mount", NULL
);
71 expect("/waldo/quuix/", ".mount", "/waldo/quuix");
72 expect("/", ".mount", NULL
);
73 expect("///", ".mount", "/");
75 puts("-------------------------------------------------");
77 #define expect(pattern, path, suffix, expected) \
79 _cleanup_free_ char *t = \
80 unit_name_from_path_instance(pattern, path, suffix); \
82 assert(streq(t, expected)); \
85 expect("waldo", "/waldo", ".mount", "waldo@waldo.mount");
86 expect("waldo", "/waldo////quuix////", ".mount", "waldo@waldo-quuix.mount");
87 expect("waldo", "/", ".mount", "waldo@-.mount");
88 expect("wa--ldo", "/--", ".mount", "wa--ldo@\\x2d\\x2d.mount");
90 puts("-------------------------------------------------");
92 #define expect(pattern) \
94 _cleanup_free_ char *k, *t; \
95 assert_se(t = unit_name_mangle(pattern)); \
96 assert_se(k = unit_name_mangle(t)); \
98 assert_se(streq(t, k)); \
103 expect("üxknürz.service");
104 expect("foobar-meh...waldi.service");
105 expect("_____####----.....service");
106 expect("_____##@;;;,,,##----.....service");
107 expect("xxx@@@@/////\\\\\\\\\\yyy.service");
112 static int test_unit_printf(void) {
117 _cleanup_free_
char *mid
, *bid
, *host
, *root_uid
;
120 assert_se(specifier_machine_id('m', NULL
, NULL
, &mid
) >= 0 && mid
);
121 assert_se(specifier_boot_id('b', NULL
, NULL
, &bid
) >= 0 && bid
);
122 assert_se((host
= gethostname_malloc()));
124 assert_se((root
= getpwnam("root")));
125 assert_se(asprintf(&root_uid
, "%d", (int) root
->pw_uid
) > 0);
127 r
= manager_new(SYSTEMD_USER
, false, &m
);
128 if (r
== -EPERM
|| r
== -EACCES
) {
129 puts("manager_new: Permission denied. Skipping test.");
130 return EXIT_TEST_SKIP
;
134 #define expect(unit, pattern, expected) \
137 _cleanup_free_ char *t; \
138 assert_se(unit_full_printf(unit, pattern, &t) >= 0); \
139 printf("result: %s\nexpect: %s\n", t, expected); \
140 if ((e = endswith(expected, "*"))) \
141 assert(strncmp(t, e, e-expected)); \
143 assert(streq(t, expected)); \
146 assert_se(setenv("USER", "root", 1) == 0);
147 assert_se(setenv("HOME", "/root", 1) == 0);
149 assert_se(u
= unit_new(m
, sizeof(Service
)));
150 assert_se(unit_add_name(u
, "blah.service") == 0);
151 assert_se(unit_add_name(u
, "blah.service") == 0);
154 expect(u
, "%%", "%");
155 expect(u
, "%%s", "%s");
156 expect(u
, "%", ""); // REALLY?
159 expect(u
, "%n", "blah.service");
160 expect(u
, "%N", "blah");
161 expect(u
, "%p", "blah");
162 expect(u
, "%P", "blah");
165 expect(u
, "%u", root
->pw_name
);
166 expect(u
, "%U", root_uid
);
167 expect(u
, "%h", root
->pw_dir
);
168 expect(u
, "%s", "/bin/sh");
169 expect(u
, "%m", mid
);
170 expect(u
, "%b", bid
);
171 expect(u
, "%H", host
);
172 expect(u
, "%t", "/run/user/*");
175 assert_se(u2
= unit_new(m
, sizeof(Service
)));
176 assert_se(unit_add_name(u2
, "blah@foo-foo.service") == 0);
177 assert_se(unit_add_name(u2
, "blah@foo-foo.service") == 0);
179 expect(u2
, "%n", "blah@foo-foo.service");
180 expect(u2
, "%N", "blah@foo-foo");
181 expect(u2
, "%p", "blah");
182 expect(u2
, "%P", "blah");
183 expect(u2
, "%i", "foo-foo");
184 expect(u2
, "%I", "foo/foo");
185 expect(u2
, "%u", root
->pw_name
);
186 expect(u2
, "%U", root_uid
);
187 expect(u2
, "%h", root
->pw_dir
);
188 expect(u2
, "%s", "/bin/sh");
189 expect(u2
, "%m", mid
);
190 expect(u2
, "%b", bid
);
191 expect(u2
, "%H", host
);
192 expect(u2
, "%t", "/run/user/*");
199 int main(int argc
, char* argv
[]) {
202 TEST_REQ_RUNNING_SYSTEMD(rc
= test_unit_printf());