test-cgroup-util.c revision a016b9228f338cb9b380ce7e00826ef462767d98
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering (at your option) any later version.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Lesser General Public License for more details.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering assert_se(cg_path_decode_unit(path, &unit) == code);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_decode_unit(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_d_u("getty@.service/getty@tty2.service", 0, "getty@tty2.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_d_u("getty@.service/getty@tty2.service/xxx", 0, "getty@tty2.service");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty.service", 0, "getty.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(cg_path_get_unit(path, &unit) == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(cg_path_get_user_unit(path, &unit) == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void test_path_get_unit(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service", 0, "getty@tty5.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u("/system/getty@.service/getty@tty5.service/", 0, "getty@tty5.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("sadfdsafsda", -ENOENT, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_get_user_unit(void) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_get_paths(void) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL;
38158b920e772ea3a7cc9dfcf705666ce3aa5ce3Zbigniew Jędrzejewski-Szmek assert_se(cg_get_machine_path("harley", &d) >= 0);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_proc(void) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\n",
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering (unsigned long) pid,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering (unsigned long) uid,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar.service", "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cpu.service", "_cpu.service");
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poetteringstatic void test_controller_is_valid(void) {
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("foobar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("name=", true));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("=", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("_", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path(void) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);