test-cgroup-util.c revision e13bb5d2b133f9ae51c0a2d20aa51071c780e9ae
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/***
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering***/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include <assert.h>
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include "util.h"
96115cdfe0241ae9b4e7177cd3874c0a93d00b39Thomas Hindoe Paaboel Andersen#include "cgroup-util.h"
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include "test-helper.h"
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering _cleanup_free_ char *unit = NULL;
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(cg_path_decode_unit(path, &unit) == code);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(streq_ptr(unit, result));
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering}
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void test_path_decode_unit(void) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("getty.service", 0, "getty.service");
4349cd7c1d153c4ffa23cf1cff1644e0afa9bcf0Lennart Poettering check_p_d_u("getty", -EINVAL, NULL);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
8fcde01280adcbd07e8205b91ac52b06305b6208Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering _cleanup_free_ char *unit = NULL;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_path_get_unit(path, &unit) == code);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert_se(streq_ptr(unit, result));
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic void test_path_get_unit(void) {
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *unit = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering assert_se(cg_path_get_user_unit(path, &unit) == code);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering assert_se(streq_ptr(unit, result));
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_user_unit(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
821b2e792159e237a1e5a1ea4bb6ae2e55d64be5Lukas Nykryn check_p_g_u_u("/meh.service", -ENOENT, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidtstatic void check_p_g_s(const char *path, int code, const char *result) {
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt _cleanup_free_ char *s = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_session(path, &s) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(streq_ptr(s, result));
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poetteringstatic void test_path_get_session(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_s("/session-3.scope", 0, "3");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_s("", -ENOENT, 0);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering uid_t uid = 0;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(uid == result);
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_owner_uid(void) {
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_o_u("", -ENOENT, 0);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering}
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poetteringstatic void check_p_g_m_n(const char *path, int code, const char *result) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *m = NULL;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(cg_path_get_machine_name(path, &m) == code);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(streq_ptr(m, result));
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_path_get_machine_name(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_m_n("/user.slice/machine-foobar.scope", 0, "foobar");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt check_p_g_m_n("/machine-foobar.scope", 0, "foobar");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope", 0, "foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope/asjhdkj", 0, "foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering check_p_g_m_n("", -ENOENT, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_get_paths(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *a = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt assert_se(cg_get_root_path(&a) >= 0);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt log_info("Root = %s", a);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_proc(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering struct dirent *de;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering int r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt d = opendir("/proc");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt assert_se(d);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering FOREACH_DIRENT(de, d, break) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL, *slice = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering pid_t pid;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering uid_t uid = (uid_t) -1;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (de->d_type != DT_DIR &&
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering de->d_type != DT_UNKNOWN)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering continue;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
4c1fc3e404d648c70bd2f50ac50aeac6ece8872eDaniel Mack r = parse_pid(de->d_name, &pid);
4c1fc3e404d648c70bd2f50ac50aeac6ece8872eDaniel Mack if (r < 0)
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt continue;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (is_kernel_thread(pid))
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering continue;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_path_shifted(pid, &prefix, &path_shifted);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_owner_uid(pid, &uid);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_session(pid, &session);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_unit(pid, &unit);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering cg_pid_get_user_unit(pid, &user_unit);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering cg_pid_get_machine_name(pid, &machine);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering cg_pid_get_slice(pid, &slice);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n",
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering (unsigned long) pid,
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering path,
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers prefix,
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering path_shifted,
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering (unsigned long) uid,
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering session,
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering unit,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering user_unit,
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering machine,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering slice);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *b;
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering b = cg_escape(s);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering assert_se(b);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(streq(b, r));
cca1dfddd4ce4357113663532696488427cc54e4Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert_se(streq(cg_unescape(b), s));
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_escape(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_escape_one("foobar", "foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_escape_one(".foobar", "_.foobar");
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_escape_one("foobar.service", "foobar.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering test_escape_one("tasks", "_tasks");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering test_escape_one("cpu.service", "_cpu.service");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering test_escape_one("_foobar", "__foobar");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering test_escape_one("", "_");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering test_escape_one("_", "__");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering test_escape_one(".", "_.");
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering}
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidtstatic void test_controller_is_valid(void) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("foobar", false));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert_se(!cg_controller_is_valid("", false));
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering assert_se(!cg_controller_is_valid("name=", true));
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering assert_se(!cg_controller_is_valid("=", false));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(!cg_controller_is_valid("_", false));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering}
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_free_ char *ret = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert_se(streq_ptr(ret, path));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringstatic void test_slice_to_path(void) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_slice_to_path_one("-.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
59512f21d77d984cf1363fb0d1770218c5e17020Kay Sievers test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poetteringint main(void) {
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering test_path_decode_unit();
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering test_path_get_unit();
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_path_get_user_unit();
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_path_get_session();
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_path_get_owner_uid();
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_path_get_machine_name();
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
73b80ec2d999c45ce13f3e034704249d80829f7eLennart Poettering test_proc();
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt TEST_REQ_RUNNING_SYSTEMD(test_escape());
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt test_controller_is_valid();
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering test_slice_to_path();
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering