chiark / gitweb /
bus: use GREEDY_REALLOC() when allocating message queues
[elogind.git] / src / test / test-unit-name.c
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
7   Copyright 2013 Zbigniew Jędrzejewski-Szmek
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>
26 #include <sys/types.h>
27 #include <pwd.h>
28
29 #include "manager.h"
30 #include "unit.h"
31 #include "unit-name.h"
32 #include "unit-printf.h"
33 #include "install.h"
34 #include "specifier.h"
35 #include "util.h"
36 #include "macro.h"
37 #include "test-helper.h"
38
39 static void test_replacements(void) {
40 #define expect(pattern, repl, expected)                            \
41         {                                                          \
42                 _cleanup_free_ char *t =                           \
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         {                                                          \
61                 _cleanup_free_ char *k, *t =                       \
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         {                                                               \
79                 _cleanup_free_ char *t =                                \
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         {                                                               \
94                 _cleanup_free_ char *k, *t;                             \
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");
108
109 #undef expect
110 }
111
112 static int test_unit_printf(void) {
113         Manager *m;
114         Unit *u, *u2;
115         int r;
116
117         _cleanup_free_ char *mid, *bid, *host, *root_uid;
118         struct passwd *root;
119
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()));
123
124         assert_se((root = getpwnam("root")));
125         assert_se(asprintf(&root_uid, "%d", (int) root->pw_uid) > 0);
126
127         r = manager_new(SYSTEMD_USER, &m);
128         if (r == -EPERM || r == -EACCES || r == -EADDRINUSE) {
129                 puts("manager_new: Permission denied. Skipping test.");
130                 return EXIT_TEST_SKIP;
131         }
132         assert(r == 0);
133
134 #define expect(unit, pattern, expected)                                 \
135         {                                                               \
136                 char *e;                                                \
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));              \
142                 else                                                    \
143                         assert(streq(t, expected));                     \
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);
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/*");
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);
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/*");
193
194         manager_free(m);
195
196         return 0;
197 }
198
199 int main(int argc, char* argv[]) {
200         int rc = 0;
201         test_replacements();
202         TEST_REQ_RUNNING_SYSTEMD(rc = test_unit_printf());
203         return rc;
204 }