test-cgroup-util.c revision 0b452006de98294d1690f045f6ea2f7f6630ec3b
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering/***
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering This file is part of systemd.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
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
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
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/>.
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering***/
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek#include "cgroup-util.h"
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther#include "test-helper.h"
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalier#include "formats-util.h"
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier#include "process-util.h"
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void check_p_d_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_decode_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
7027ff61a34a12487712b382a061c654acc3a679Lennart Poetteringstatic void test_path_decode_unit(void) {
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("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");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering check_p_d_u("getty", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("getty/waldo", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_d_u("_cpu.service", 0, "cpu.service");
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_get_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_unit(void) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("sadfdsafsda", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -EINVAL, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poetteringstatic void check_p_g_u_u(const char *path, int code, const char *result) {
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering _cleanup_free_ char *unit = NULL;
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek int r;
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek r = cg_path_get_user_unit(path, &unit);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek assert_se(r == code);
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering assert_se(streq_ptr(unit, result));
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_user_unit(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_u_u("/meh.service", -ENOENT, NULL);
d7bd3de0654669e65b9642c248c5fa6d1d9a9f61Lennart Poettering check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
320814811417146cfa1e416f69f1101eed630c36Luke Shumaker check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
d4fffc4b8beb86e77fd710c1f43913a490ed083aZbigniew Jędrzejewski-Szmek check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENOENT, NULL);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering}
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_s(const char *path, int code, const char *result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *s = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_session(path, &s) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(streq_ptr(s, result));
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_session(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_s("/session-3.scope", 0, "3");
a13ee4c792cb5738c3dd13b2a4bb1a828e7994fdZbigniew Jędrzejewski-Szmek check_p_g_s("/session-.scope", -ENOENT, NULL);
a13ee4c792cb5738c3dd13b2a4bb1a828e7994fdZbigniew Jędrzejewski-Szmek check_p_g_s("", -ENOENT, NULL);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void check_p_g_o_u(const char *path, int code, uid_t result) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering uid_t uid = 0;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_path_get_owner_uid(path, &uid) == code);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(uid == result);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_path_get_owner_uid(void) {
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
374ec6abf31ada6ca554cc8ea99b282373fac010Lennart Poettering check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering check_p_g_o_u("", -ENOENT, 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringstatic void test_get_paths(void) {
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering _cleanup_free_ char *a = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering assert_se(cg_get_root_path(&a) >= 0);
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering log_info("Root = %s", a);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering}
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poetteringstatic void test_proc(void) {
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering _cleanup_closedir_ DIR *d = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering struct dirent *de;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering int r;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering d = opendir("/proc");
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering assert_se(d);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering FOREACH_DIRENT(de, d, break) {
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering pid_t pid;
fed1e721fd0c81e60c77120539f34e16c2585634Lennart Poettering uid_t uid = UID_INVALID;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (de->d_type != DT_DIR &&
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering de->d_type != DT_UNKNOWN)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering continue;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering r = parse_pid(de->d_name, &pid);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering if (r < 0)
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering continue;
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (is_kernel_thread(pid))
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering continue;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering cg_pid_get_path_shifted(pid, NULL, &path_shifted);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering cg_pid_get_owner_uid(pid, &uid);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_session(pid, &session);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_unit(pid, &unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_user_unit(pid, &user_unit);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering cg_pid_get_machine_name(pid, &machine);
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering cg_pid_get_slice(pid, &slice);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek pid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering path_shifted,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek uid,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering session,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering unit,
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering user_unit,
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering machine,
1021b21bc6f8dd522b46116e8598b17f9f93f1b7Lennart Poettering slice);
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering }
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering}
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape_one(const char *s, const char *r) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering _cleanup_free_ char *b;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering b = cg_escape(s);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(b);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(streq(b, r));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(streq(cg_unescape(b), s));
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic void test_escape(void) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar", "foobar");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one(".foobar", "_.foobar");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("foobar.service", "foobar.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("cgroup.service", "_cgroup.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("tasks", "_tasks");
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
e13bb5d2b133f9ae51c0a2d20aa51071c780e9aeKay Sievers test_escape_one("cpu.service", "_cpu.service");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering test_escape_one("_foobar", "__foobar");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one("", "_");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one("_", "__");
a0ab566574303be1ca12cdb334f284cfd407caa5Lennart Poettering test_escape_one(".", "_.");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
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));
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering}
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path_one(const char *unit, const char *path, int error) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering _cleanup_free_ char *ret = NULL;
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(cg_slice_to_path(unit, &ret) == error);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering assert_se(streq_ptr(ret, path));
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poetteringstatic void test_slice_to_path(void) {
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
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);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart 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);
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering}
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering const char *s = NULL;
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering assert_se(cg_shift_path(raw, root, &s) >= 0);
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering assert_se(streq(s, shifted));
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering}
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poetteringstatic void test_shift_path(void) {
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "", "/foobar/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/foobar", "/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path_one("/foobar/waldo", "/fuckfuck", "/foobar/waldo");
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering}
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmekint main(void) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test_path_decode_unit();
6c03089c32c251d823173bda4d809a9e643219f0Lennart Poettering test_path_get_unit();
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering test_path_get_user_unit();
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test_path_get_session();
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering test_path_get_owner_uid();
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
aff38e74bd776471f15ba54b305a24b0251eb865Lennart Poettering test_proc();
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther TEST_REQ_RUNNING_SYSTEMD(test_escape());
78edb35ab4f4227485cb9ec816b43c37e0d5e62aLennart Poettering test_controller_is_valid();
a016b9228f338cb9b380ce7e00826ef462767d98Lennart Poettering test_slice_to_path();
751bc6ac79320bc16e63e8c1bbb713c30a3b7bc9Lennart Poettering test_shift_path();
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek return 0;
96cde13ace6406582688028f3df5668a172ba628Zbigniew Jędrzejewski-Szmek}