]>
git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/jail.c
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2022 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #include <pakfire/cgroup.h>
22 #include <pakfire/jail.h>
24 #include "../testsuite.h"
26 static const char* cmd_hello_world
[] = {
27 "/command", "echo", "Hello World!", NULL
,
30 static const char* cmd_exhaust_memory
[] = {
31 "/command", "exhaust-memory", NULL
,
34 static const char* cmd_fork_bomb
[] = {
35 "/command", "fork-bomb", NULL
,
38 static int test_create(const struct test
* t
) {
39 struct pakfire_jail
* jail
= NULL
;
42 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
45 ASSERT_NULL(pakfire_jail_unref(jail
));
47 // Create an interactive jail
48 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, PAKFIRE_JAIL_INTERACTIVE
));
51 ASSERT_NULL(pakfire_jail_unref(jail
));
59 static int test_env(const struct test
* t
) {
60 struct pakfire_jail
* jail
= NULL
;
63 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
65 // Check if the default variables are set
66 ASSERT(pakfire_jail_get_env(jail
, "LANG"));
67 ASSERT(pakfire_jail_get_env(jail
, "TERM"));
69 // Fetch a non-existing environment variable
70 ASSERT_NULL(pakfire_jail_get_env(jail
, "VARIABLE"));
73 ASSERT_SUCCESS(pakfire_jail_set_env(jail
, "VARIABLE", "VALUE"));
75 // Read the value back
76 ASSERT_STRING_EQUALS(pakfire_jail_get_env(jail
, "VARIABLE"), "VALUE");
79 ASSERT_NULL(pakfire_jail_unref(jail
));
87 static int test_exec(const struct test
* t
) {
88 struct pakfire_jail
* jail
= NULL
;
91 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
93 // Try to execute something
94 ASSERT_SUCCESS(pakfire_jail_exec(jail
, cmd_hello_world
, NULL
));
97 ASSERT_NULL(pakfire_jail_unref(jail
));
105 static int test_launch_into_cgroup(const struct test
* t
) {
106 struct pakfire_cgroup
* cgroup
= NULL
;
107 struct pakfire_jail
* jail
= NULL
;
108 int r
= EXIT_FAILURE
;
110 // Create a new cgroup
111 ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup
, t
->pakfire
, "pakfire-test", 0));
114 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
116 // Connect jail to the cgroup
117 ASSERT_SUCCESS(pakfire_jail_set_cgroup(jail
, cgroup
));
120 ASSERT(pakfire_jail_exec(jail
, cmd_hello_world
, NULL
) == 0);
126 pakfire_cgroup_destroy(cgroup
);
127 pakfire_cgroup_unref(cgroup
);
130 pakfire_jail_unref(jail
);
135 static int test_nice(const struct test
* t
) {
136 struct pakfire_jail
* jail
= NULL
;
138 int r
= EXIT_FAILURE
;
140 const char* argv
[] = {
141 "/command", "print-nice", NULL
,
145 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
147 // Set invalid nice levels
148 ASSERT_ERRNO(pakfire_jail_nice(jail
, 100), EINVAL
);
149 ASSERT_ERRNO(pakfire_jail_nice(jail
, -100), EINVAL
);
151 // Set something sane
152 ASSERT_SUCCESS(pakfire_jail_nice(jail
, 5));
154 // Check if the nice level has been set
155 ASSERT_SUCCESS(pakfire_jail_exec(jail
, argv
, &output
));
156 ASSERT_STRING_EQUALS(output
, "5\n");
163 pakfire_jail_unref(jail
);
170 static int test_memory_limit(const struct test
* t
) {
171 struct pakfire_cgroup
* cgroup
= NULL
;
172 struct pakfire_jail
* jail
= NULL
;
173 int r
= EXIT_FAILURE
;
177 ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup
, t
->pakfire
, "pakfire-test", 0));
180 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
182 // Connect jail to the cgroup
183 ASSERT_SUCCESS(pakfire_jail_set_cgroup(jail
, cgroup
));
185 // Set a memory limit of 100 MiB
186 ASSERT_SUCCESS(pakfire_cgroup_set_memory_limit(cgroup
, 100 * 1024 * 1024));
188 // Try to exhaust all memory
189 ASSERT_FAILURE(pakfire_jail_exec(jail
, cmd_exhaust_memory
, NULL
));
191 // A fork bomb should also exhaust all memory
192 ASSERT_FAILURE(pakfire_jail_exec(jail
, cmd_fork_bomb
, NULL
));
199 pakfire_jail_unref(jail
);
201 pakfire_cgroup_destroy(cgroup
);
202 pakfire_cgroup_unref(cgroup
);
208 static int test_pid_limit(const struct test
* t
) {
209 struct pakfire_cgroup
* cgroup
= NULL
;
210 struct pakfire_jail
* jail
= NULL
;
211 int r
= EXIT_FAILURE
;
215 ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup
, t
->pakfire
, "pakfire-test", 0));
218 ASSERT_SUCCESS(pakfire_jail_create(&jail
, t
->pakfire
, 0));
220 // Connect jail to the cgroup
221 ASSERT_SUCCESS(pakfire_jail_set_cgroup(jail
, cgroup
));
223 // Set a PID limit of 100 processes
224 ASSERT_SUCCESS(pakfire_cgroup_set_pid_limit(cgroup
, 100));
226 // Try to fork as many processes as possible
227 ASSERT_FAILURE(pakfire_jail_exec(jail
, cmd_fork_bomb
, NULL
));
234 pakfire_jail_unref(jail
);
236 pakfire_cgroup_destroy(cgroup
);
237 pakfire_cgroup_unref(cgroup
);
243 int main(int argc
, const char* argv
[]) {
244 testsuite_add_test(test_create
);
245 testsuite_add_test(test_env
);
246 testsuite_add_test(test_exec
);
247 testsuite_add_test(test_launch_into_cgroup
);
248 testsuite_add_test(test_nice
);
249 testsuite_add_test(test_memory_limit
);
250 testsuite_add_test(test_pid_limit
);
252 return testsuite_run(argc
, argv
);