test-cgroup-util.c revision 6c03089c32c251d823173bda4d809a9e643219f0
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/***
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering This file is part of systemd.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering systemd is free software; you can redistribute it and/or modify it
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering under the terms of the GNU Lesser General Public License as published by
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (at your option) any later version.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering systemd is distributed in the hope that it will be useful, but
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering Lesser General Public License for more details.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering You should have received a copy of the GNU Lesser General Public License
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering***/
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <assert.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include "util.h"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include "cgroup-util.h"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void check_c_t_u(const char *path, int code, const char *result) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_free_ char *unit = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(cg_cgroup_to_unit(path, &unit) == code);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(streq_ptr(unit, result));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void test_cgroup_to_unit(void) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty@.service/tty2", 0, "getty@tty2.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty@.service/tty2/xxx", 0, "getty@tty2.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty@.service/", -EINVAL, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty@.service", -EINVAL, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty.service", 0, "getty.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_c_t_u("getty", -EINVAL, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_free_ char *unit = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(cg_path_get_unit(path, &unit) == code);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(streq_ptr(unit, result));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_free_ char *unit = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(cg_path_get_user_unit(path, &unit) == code);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert_se(streq_ptr(unit, result));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void test_path_get_unit(void) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/foobar.service/sdfdsaf", 0, "foobar.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/getty@.service/tty5", 0, "getty@tty5.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/getty@.service/tty5/aaa/bbb", 0, "getty@tty5.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/getty@.service/tty5/", 0, "getty@tty5.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/getty@tty6.service/tty5", 0, "getty@tty6.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("sadfdsafsda", -ENOENT, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u("/system/getty####@tty6.service/tty5", -EINVAL, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service", 0, "foobar.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo", 0, "foobar.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar.service/waldo/uuuux", 0, "foobar.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/waldo/waldo/uuuux", -EINVAL, NULL);
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering check_p_g_u_u("/user/lennart/2/foobar.service", -ENOENT, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering check_p_g_u_u("/user/lennart/2/systemd-21548/foobar@.service/pie/pa/po", 0, "foobar@pie.service");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint main(void) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering test_cgroup_to_unit();
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering test_path_get_unit();
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering