]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cgroup-util.c
util: make sure result of hostname_cleanup() passes hostname_is_valid()
[thirdparty/systemd.git] / src / test / test-cgroup-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23
24 #include "util.h"
25 #include "cgroup-util.h"
26
27 static void check_c_t_u(const char *path, int code, const char *result) {
28 _cleanup_free_ char *unit = NULL;
29
30 assert_se(cg_cgroup_to_unit(path, &unit) == code);
31 assert_se(streq_ptr(unit, result));
32 }
33
34 static void test_cgroup_to_unit(void) {
35 check_c_t_u("getty@.service/tty2", 0, "getty@tty2.service");
36 check_c_t_u("getty@.service/tty2/xxx", 0, "getty@tty2.service");
37 check_c_t_u("getty@.service/", -EINVAL, NULL);
38 check_c_t_u("getty@.service", -EINVAL, NULL);
39 check_c_t_u("getty.service", 0, "getty.service");
40 check_c_t_u("getty", -EINVAL, NULL);
41 }
42
43 static void check_p_g_u(const char *path, int code, const char *result) {
44 _cleanup_free_ char *unit = NULL;
45
46 assert_se(cg_path_get_unit(path, &unit) == code);
47 assert_se(streq_ptr(unit, result));
48 }
49
50 static void check_p_g_u_u(const char *path, int code, const char *result) {
51 _cleanup_free_ char *unit = NULL;
52
53 assert_se(cg_path_get_user_unit(path, &unit) == code);
54 assert_se(streq_ptr(unit, result));
55 }
56
57 static void test_path_get_unit(void) {
58 check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
59 check_p_g_u("/system/getty@.service/tty5", 0, "getty@tty5.service");
60 check_p_g_u("/system/getty@.service/tty5/aaa/bbb", 0, "getty@tty5.service");
61 check_p_g_u("/system/getty@.service/tty5/", 0, "getty@tty5.service");
62 check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
63 check_p_g_u("sadfdsafsda", -ENOENT, NULL);
64 check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
65
66 check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service");
67 check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service");
68 check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service");
69 check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL);
70 check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL);
71 check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/pie/pa/po", 0, "foobar@pie.service");
72 }
73
74 int main(void) {
75 test_cgroup_to_unit();
76 test_path_get_unit();
77
78 return 0;
79 }