]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-unit-name.c
core: serialize/deserialize bus subscribers
[thirdparty/systemd.git] / src / test / test-unit-name.c
CommitLineData
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
b0193f1c
LP
8
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.
13
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.
18
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/>.
21***/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
1682ff60
ZJS
26#include <sys/types.h>
27#include <pwd.h>
b0193f1c 28
1682ff60
ZJS
29#include "manager.h"
30#include "unit.h"
b0193f1c 31#include "unit-name.h"
1682ff60
ZJS
32#include "unit-printf.h"
33#include "install.h"
34#include "specifier.h"
b0193f1c 35#include "util.h"
1682ff60 36#include "macro.h"
b0193f1c 37
1682ff60 38static void test_replacements(void) {
3251c0d2
ZJS
39#define expect(pattern, repl, expected) \
40 { \
7fd1b19b 41 _cleanup_free_ char *t = \
3251c0d2
ZJS
42 unit_name_replace_instance(pattern, repl); \
43 puts(t); \
44 assert(streq(t, expected)); \
45 }
46
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");
55
56 puts("-------------------------------------------------");
57#undef expect
58#define expect(path, suffix, expected) \
59 { \
7fd1b19b 60 _cleanup_free_ char *k, *t = \
3251c0d2
ZJS
61 unit_name_from_path(path, suffix); \
62 puts(t); \
63 k = unit_name_to_path(t); \
64 puts(k); \
65 assert(streq(k, expected ? expected : path)); \
66 }
67
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", "/");
73
74 puts("-------------------------------------------------");
75#undef expect
76#define expect(pattern, path, suffix, expected) \
77 { \
7fd1b19b 78 _cleanup_free_ char *t = \
3251c0d2
ZJS
79 unit_name_from_path_instance(pattern, path, suffix); \
80 puts(t); \
81 assert(streq(t, expected)); \
82 }
83
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");
88
89 puts("-------------------------------------------------");
90#undef expect
91#define expect(pattern) \
92 { \
7fd1b19b 93 _cleanup_free_ char *k, *t; \
3251c0d2
ZJS
94 assert_se(t = unit_name_mangle(pattern)); \
95 assert_se(k = unit_name_mangle(t)); \
96 puts(t); \
97 assert_se(streq(t, k)); \
98 }
99
100 expect("/home");
101 expect("/dev/sda");
102 expect("üxknürz.service");
103 expect("foobar-meh...waldi.service");
104 expect("_____####----.....service");
105 expect("_____##@;;;,,,##----.....service");
106 expect("xxx@@@@/////\\\\\\\\\\yyy.service");
b0193f1c 107
1682ff60
ZJS
108#undef expect
109}
110
49e5de64 111static int test_unit_printf(void) {
1682ff60
ZJS
112 Manager *m;
113 Unit *u, *u2;
c5e33bf8 114 int r;
1682ff60 115
7fd1b19b 116 _cleanup_free_ char *mid, *bid, *host, *root_uid;
1682ff60
ZJS
117 struct passwd *root;
118
119 assert_se((mid = specifier_machine_id('m', NULL, NULL)));
120 assert_se((bid = specifier_boot_id('b', NULL, NULL)));
121 assert_se((host = gethostname_malloc()));
122
123 assert_se((root = getpwnam("root")));
124 assert_se(asprintf(&root_uid, "%d", (int) root->pw_uid) > 0);
125
6fa48533 126 r = manager_new(SYSTEMD_USER, false, &m);
c5e33bf8
ZJS
127 if (r == -EPERM) {
128 puts("manager_new: Permission denied. Skipping test.");
49e5de64 129 return EXIT_TEST_SKIP;
c5e33bf8
ZJS
130 }
131 assert(r == 0);
1682ff60
ZJS
132
133#define expect(unit, pattern, expected) \
134 { \
fa3cd739 135 char *e; \
7fd1b19b 136 _cleanup_free_ char *t = \
1682ff60 137 unit_full_printf(unit, pattern); \
c5e33bf8 138 printf("result: %s\nexpect: %s\n", t, expected); \
fa3cd739
ZJS
139 if ((e = endswith(expected, "*"))) \
140 assert(strncmp(t, e, e-expected)); \
141 else \
142 assert(streq(t, expected)); \
1682ff60
ZJS
143 }
144
145 assert_se(setenv("USER", "root", 1) == 0);
146 assert_se(setenv("HOME", "/root", 1) == 0);
147
148 assert_se(u = unit_new(m, sizeof(Service)));
149 assert_se(unit_add_name(u, "blah.service") == 0);
150 assert_se(unit_add_name(u, "blah.service") == 0);
151
152 /* general tests */
153 expect(u, "%%", "%");
154 expect(u, "%%s", "%s");
155 expect(u, "%", ""); // REALLY?
156
157 /* normal unit */
158 expect(u, "%n", "blah.service");
159 expect(u, "%N", "blah");
160 expect(u, "%p", "blah");
161 expect(u, "%P", "blah");
162 expect(u, "%i", "");
163 expect(u, "%I", "");
164 expect(u, "%u", root->pw_name);
165 expect(u, "%U", root_uid);
166 expect(u, "%h", root->pw_dir);
3baed193 167 expect(u, "%s", "/bin/sh");
1682ff60
ZJS
168 expect(u, "%m", mid);
169 expect(u, "%b", bid);
170 expect(u, "%H", host);
fa3cd739 171 expect(u, "%t", "/run/user/*");
1682ff60
ZJS
172
173 /* templated */
174 assert_se(u2 = unit_new(m, sizeof(Service)));
175 assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0);
176 assert_se(unit_add_name(u2, "blah@foo-foo.service") == 0);
177
178 expect(u2, "%n", "blah@foo-foo.service");
179 expect(u2, "%N", "blah@foo-foo");
180 expect(u2, "%p", "blah");
181 expect(u2, "%P", "blah");
182 expect(u2, "%i", "foo-foo");
183 expect(u2, "%I", "foo/foo");
184 expect(u2, "%u", root->pw_name);
185 expect(u2, "%U", root_uid);
186 expect(u2, "%h", root->pw_dir);
3baed193 187 expect(u2, "%s", "/bin/sh");
1682ff60
ZJS
188 expect(u2, "%m", mid);
189 expect(u2, "%b", bid);
190 expect(u2, "%H", host);
fa3cd739 191 expect(u2, "%t", "/run/user/*");
49e5de64
ZJS
192
193 return 0;
1682ff60
ZJS
194}
195
196int main(int argc, char* argv[]) {
197 test_replacements();
49e5de64 198 return test_unit_printf();
b0193f1c 199}