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"
38 static void test_replacements(void) {
39 #define expect(pattern, repl, expected) \
41 char _cleanup_free_ *t = \
42 unit_name_replace_instance(pattern, repl); \
44 assert(streq(t, expected)); \
47 expect("foo@.service", "waldo", "foo@waldo.service");
48 expect("foo@xyz.service", "waldo", "foo@waldo.service");
49 expect("xyz", "waldo", "xyz");
50 expect("", "waldo", "");
51 expect("foo.service", "waldo", "foo.service");
52 expect(".service", "waldo", ".service");
53 expect("foo@", "waldo", "foo@waldo");
54 expect("@bar", "waldo", "@waldo");
56 puts("-------------------------------------------------");
58 #define expect(path, suffix, expected) \
60 char _cleanup_free_ *k, *t = \
61 unit_name_from_path(path, suffix); \
63 k = unit_name_to_path(t); \
65 assert(streq(k, expected ? expected : path)); \
68 expect("/waldo", ".mount", NULL);
69 expect("/waldo/quuix", ".mount", NULL);
70 expect("/waldo/quuix/", ".mount", "/waldo/quuix");
71 expect("/", ".mount", NULL);
72 expect("///", ".mount", "/");
74 puts("-------------------------------------------------");
76 #define expect(pattern, path, suffix, expected) \
78 char _cleanup_free_ *t = \
79 unit_name_from_path_instance(pattern, path, suffix); \
81 assert(streq(t, expected)); \
84 expect("waldo", "/waldo", ".mount", "waldo@waldo.mount");
85 expect("waldo", "/waldo////quuix////", ".mount", "waldo@waldo-quuix.mount");
86 expect("waldo", "/", ".mount", "waldo@-.mount");
87 expect("wa--ldo", "/--", ".mount", "wa--ldo@\\x2d\\x2d.mount");
89 puts("-------------------------------------------------");
91 #define expect(pattern) \
93 char _cleanup_free_ *k, *t; \
94 assert_se(t = unit_name_mangle(pattern)); \
95 assert_se(k = unit_name_mangle(t)); \
97 assert_se(streq(t, k)); \
102 expect("üxknürz.service");
103 expect("foobar-meh...waldi.service");
104 expect("_____####----.....service");
105 expect("_____##@;;;,,,##----.....service");
106 expect("xxx@@@@/////\\\\\\\\\\yyy.service");
111 static void test_unit_printf(void) {
115 char _cleanup_free_ *mid, *bid, *host, *root_uid;
118 assert_se((mid = specifier_machine_id('m', NULL, NULL)));
119 assert_se((bid = specifier_boot_id('b', NULL, NULL)));
120 assert_se((host = gethostname_malloc()));
122 assert_se((root = getpwnam("root")));
123 assert_se(asprintf(&root_uid, "%d", (int) root->pw_uid) > 0);
125 assert_se(manager_new(SYSTEMD_USER, &m) == 0);
127 #define expect(unit, pattern, expected) \
130 char _cleanup_free_ *t = \
131 unit_full_printf(unit, pattern); \
132 printf("result: %s\n", t); \
133 if ((e = endswith(expected, "*"))) \
134 assert(strncmp(t, e, e-expected)); \
136 assert(streq(t, expected)); \
139 assert_se(setenv("USER", "root", 1) == 0);
140 assert_se(setenv("HOME", "/root", 1) == 0);
142 assert_se(u = unit_new(m, sizeof(Service)));
143 assert_se(unit_add_name(u, "blah.service") == 0);
144 assert_se(unit_add_name(u, "blah.service") == 0);
147 expect(u, "%%", "%");
148 expect(u, "%%s", "%s");
149 expect(u, "%", ""); // REALLY?
152 expect(u, "%n", "blah.service");
153 expect(u, "%N", "blah");
154 expect(u, "%p", "blah");
155 expect(u, "%P", "blah");
158 expect(u, "%u", root->pw_name);
159 expect(u, "%U", root_uid);
160 expect(u, "%h", root->pw_dir);
161 expect(u, "%s", root->pw_shell);
162 expect(u, "%m", mid);
163 expect(u, "%b", bid);
164 expect(u, "%H", host);
165 expect(u, "%t", "/run/user/*");
168 assert_se(u2 = unit_new(m, sizeof(Service)));
169 assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0);
170 assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0);
172 expect(u2, "%n", "blah@foo-foo.service");
173 expect(u2, "%N", "blah@foo-foo");
174 expect(u2, "%p", "blah");
175 expect(u2, "%P", "blah");
176 expect(u2, "%i", "foo-foo");
177 expect(u2, "%I", "foo/foo");
178 expect(u2, "%u", root->pw_name);
179 expect(u2, "%U", root_uid);
180 expect(u2, "%h", root->pw_dir);
181 expect(u2, "%s", root->pw_shell);
182 expect(u2, "%m", mid);
183 expect(u2, "%b", bid);
184 expect(u2, "%H", host);
185 expect(u2, "%t", "/run/user/*");
188 int main(int argc, char* argv[]) {