]>
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 |
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" |
143bfdaf | 37 | #include "test-helper.h" |
b0193f1c | 38 | |
1682ff60 | 39 | static void test_replacements(void) { |
3251c0d2 ZJS |
40 | #define expect(pattern, repl, expected) \ |
41 | { \ | |
7fd1b19b | 42 | _cleanup_free_ char *t = \ |
3251c0d2 ZJS |
43 | unit_name_replace_instance(pattern, repl); \ |
44 | puts(t); \ | |
45 | assert(streq(t, expected)); \ | |
46 | } | |
47 | ||
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"); | |
56 | ||
57 | puts("-------------------------------------------------"); | |
58 | #undef expect | |
59 | #define expect(path, suffix, expected) \ | |
60 | { \ | |
7fd1b19b | 61 | _cleanup_free_ char *k, *t = \ |
3251c0d2 ZJS |
62 | unit_name_from_path(path, suffix); \ |
63 | puts(t); \ | |
64 | k = unit_name_to_path(t); \ | |
65 | puts(k); \ | |
66 | assert(streq(k, expected ? expected : path)); \ | |
67 | } | |
68 | ||
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", "/"); | |
74 | ||
75 | puts("-------------------------------------------------"); | |
76 | #undef expect | |
77 | #define expect(pattern, path, suffix, expected) \ | |
78 | { \ | |
7fd1b19b | 79 | _cleanup_free_ char *t = \ |
3251c0d2 ZJS |
80 | unit_name_from_path_instance(pattern, path, suffix); \ |
81 | puts(t); \ | |
82 | assert(streq(t, expected)); \ | |
83 | } | |
84 | ||
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"); | |
89 | ||
90 | puts("-------------------------------------------------"); | |
91 | #undef expect | |
92 | #define expect(pattern) \ | |
93 | { \ | |
7fd1b19b | 94 | _cleanup_free_ char *k, *t; \ |
3251c0d2 ZJS |
95 | assert_se(t = unit_name_mangle(pattern)); \ |
96 | assert_se(k = unit_name_mangle(t)); \ | |
97 | puts(t); \ | |
98 | assert_se(streq(t, k)); \ | |
99 | } | |
100 | ||
101 | expect("/home"); | |
102 | expect("/dev/sda"); | |
103 | expect("üxknürz.service"); | |
104 | expect("foobar-meh...waldi.service"); | |
105 | expect("_____####----.....service"); | |
106 | expect("_____##@;;;,,,##----.....service"); | |
107 | expect("xxx@@@@/////\\\\\\\\\\yyy.service"); | |
b0193f1c | 108 | |
1682ff60 ZJS |
109 | #undef expect |
110 | } | |
111 | ||
49e5de64 | 112 | static int test_unit_printf(void) { |
1682ff60 ZJS |
113 | Manager *m; |
114 | Unit *u, *u2; | |
c5e33bf8 | 115 | int r; |
1682ff60 | 116 | |
7fd1b19b | 117 | _cleanup_free_ char *mid, *bid, *host, *root_uid; |
1682ff60 ZJS |
118 | struct passwd *root; |
119 | ||
120 | assert_se((mid = specifier_machine_id('m', NULL, NULL))); | |
121 | assert_se((bid = specifier_boot_id('b', NULL, NULL))); | |
122 | assert_se((host = gethostname_malloc())); | |
123 | ||
124 | assert_se((root = getpwnam("root"))); | |
125 | assert_se(asprintf(&root_uid, "%d", (int) root->pw_uid) > 0); | |
126 | ||
6fa48533 | 127 | r = manager_new(SYSTEMD_USER, false, &m); |
f04ca8c2 | 128 | if (r == -EPERM || r == -EACCES) { |
c5e33bf8 | 129 | puts("manager_new: Permission denied. Skipping test."); |
49e5de64 | 130 | return EXIT_TEST_SKIP; |
c5e33bf8 ZJS |
131 | } |
132 | assert(r == 0); | |
1682ff60 ZJS |
133 | |
134 | #define expect(unit, pattern, expected) \ | |
135 | { \ | |
fa3cd739 | 136 | char *e; \ |
7fd1b19b | 137 | _cleanup_free_ char *t = \ |
1682ff60 | 138 | unit_full_printf(unit, pattern); \ |
c5e33bf8 | 139 | printf("result: %s\nexpect: %s\n", t, expected); \ |
fa3cd739 ZJS |
140 | if ((e = endswith(expected, "*"))) \ |
141 | assert(strncmp(t, e, e-expected)); \ | |
142 | else \ | |
143 | assert(streq(t, expected)); \ | |
1682ff60 ZJS |
144 | } |
145 | ||
146 | assert_se(setenv("USER", "root", 1) == 0); | |
147 | assert_se(setenv("HOME", "/root", 1) == 0); | |
148 | ||
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); | |
152 | ||
153 | /* general tests */ | |
154 | expect(u, "%%", "%"); | |
155 | expect(u, "%%s", "%s"); | |
156 | expect(u, "%", ""); // REALLY? | |
157 | ||
158 | /* normal unit */ | |
159 | expect(u, "%n", "blah.service"); | |
160 | expect(u, "%N", "blah"); | |
161 | expect(u, "%p", "blah"); | |
162 | expect(u, "%P", "blah"); | |
163 | expect(u, "%i", ""); | |
164 | expect(u, "%I", ""); | |
165 | expect(u, "%u", root->pw_name); | |
166 | expect(u, "%U", root_uid); | |
167 | expect(u, "%h", root->pw_dir); | |
3baed193 | 168 | expect(u, "%s", "/bin/sh"); |
1682ff60 ZJS |
169 | expect(u, "%m", mid); |
170 | expect(u, "%b", bid); | |
171 | expect(u, "%H", host); | |
fa3cd739 | 172 | expect(u, "%t", "/run/user/*"); |
1682ff60 ZJS |
173 | |
174 | /* templated */ | |
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); | |
178 | ||
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); | |
3baed193 | 188 | expect(u2, "%s", "/bin/sh"); |
1682ff60 ZJS |
189 | expect(u2, "%m", mid); |
190 | expect(u2, "%b", bid); | |
191 | expect(u2, "%H", host); | |
fa3cd739 | 192 | expect(u2, "%t", "/run/user/*"); |
49e5de64 | 193 | |
b463b813 ZJS |
194 | manager_free(m); |
195 | ||
49e5de64 | 196 | return 0; |
1682ff60 ZJS |
197 | } |
198 | ||
199 | int main(int argc, char* argv[]) { | |
143bfdaf | 200 | int rc = 0; |
1682ff60 | 201 | test_replacements(); |
143bfdaf HHPF |
202 | TEST_REQ_RUNNING_SYSTEMD(rc = test_unit_printf()); |
203 | return rc; | |
b0193f1c | 204 | } |