test-cgroup-util.c revision d4fffc4b8beb86e77fd710c1f43913a490ed083a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <assert.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek#include "util.h"
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek#include "cgroup-util.h"
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include "test-helper.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic void check_p_d_u(const char *path, int code, const char *result) {
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek _cleanup_free_ char *unit = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = cg_path_decode_unit(path, &unit);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(r == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(streq_ptr(unit, result));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_decode_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@.service/", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty@.service", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty.service", 0, "getty.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *unit = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = cg_path_get_unit(path, &unit);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(r == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(streq_ptr(unit, result));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *unit = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = cg_path_get_user_unit(path, &unit);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(r == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(streq_ptr(unit, result));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_user_unit(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/meh.service", -ENOENT, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENOENT, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_s(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_session(path, &s) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(streq_ptr(s, result));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_session(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_s("/session-3.scope", 0, "3");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_s("", -ENOENT, 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering uid_t uid = 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(uid == result);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_owner_uid(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_o_u("", -ENOENT, 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void check_p_g_m_n(const char *path, int code, const char *result) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *m = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(cg_path_get_machine_name(path, &m) == code);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(streq_ptr(m, result));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_path_get_machine_name(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering check_p_g_m_n("/user.slice/machine-foobar.scope", 0, "foobar");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering check_p_g_m_n("/machine-foobar.scope", 0, "foobar");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope", 0, "foobar");
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt check_p_g_m_n("/user.slice/user-kuux.slice/machine-foobar.scope/asjhdkj", 0, "foobar");
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering check_p_g_m_n("", -ENOENT, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_get_paths(void) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *a = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(cg_get_root_path(&a) >= 0);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_info("Root = %s", a);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtstatic void test_proc(void) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering struct dirent *de;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering d = opendir("/proc");
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert_se(d);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering FOREACH_DIRENT(de, d, break) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering pid_t pid;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering uid_t uid = (uid_t) -1;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (de->d_type != DT_DIR &&
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering de->d_type != DT_UNKNOWN)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = parse_pid(de->d_name, &pid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (is_kernel_thread(pid))
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt continue;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt cg_pid_get_path_shifted(pid, NULL, &path_shifted);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering cg_pid_get_owner_uid(pid, &uid);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen cg_pid_get_session(pid, &session);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering cg_pid_get_unit(pid, &unit);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering cg_pid_get_user_unit(pid, &user_unit);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering cg_pid_get_machine_name(pid, &machine);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering cg_pid_get_slice(pid, &slice);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen printf("%lu\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n",
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering (unsigned long) pid,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering path,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering path_shifted,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering (unsigned long) uid,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering session,
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen unit,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering user_unit,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering machine,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering slice);
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering _cleanup_free_ char *b;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering b = cg_escape(s);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(b);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq(b, r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq(cg_unescape(b), s));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_escape(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_escape_one("foobar", "foobar");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_escape_one(".foobar", "_.foobar");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_escape_one("foobar.service", "foobar.service");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_escape_one("tasks", "_tasks");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one("cpu.service", "_cpu.service");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one("_foobar", "__foobar");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one("", "_");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one("_", "__");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering test_escape_one(".", "_.");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering}
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poetteringstatic void test_controller_is_valid(void) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering assert_se(cg_controller_is_valid("foobar", false));
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering assert_se(cg_controller_is_valid("foo_bar", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(cg_controller_is_valid("name=foo", true));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("name=", true));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("=", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("cpu,cpuacct", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("_", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("_foobar", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(!cg_controller_is_valid("tatü", false));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *ret = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(streq_ptr(ret, path));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_slice_to_path(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_slice_to_path_one("-.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering 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);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmekint main(void) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_path_decode_unit();
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering test_path_get_unit();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_path_get_user_unit();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_path_get_session();
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_path_get_owner_uid();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_path_get_machine_name();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering test_proc();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek TEST_REQ_RUNNING_SYSTEMD(test_escape());
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering test_controller_is_valid();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek test_slice_to_path();
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering