]>
Commit | Line | Data |
---|---|---|
3334e9c0 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2022 Pakfire development team # | |
5 | # # | |
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. # | |
10 | # # | |
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. # | |
15 | # # | |
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/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
6962faab | 21 | #include <pakfire/cgroup.h> |
3334e9c0 MT |
22 | #include <pakfire/jail.h> |
23 | ||
24 | #include "../testsuite.h" | |
25 | ||
2e664540 MT |
26 | static const char* cmd_hello_world[] = { |
27 | "/command", "echo", "Hello World!", NULL, | |
3334e9c0 MT |
28 | }; |
29 | ||
30 | static int test_create(const struct test* t) { | |
31 | struct pakfire_jail* jail = NULL; | |
32 | ||
33 | // Create a new jail | |
d639929b | 34 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); |
3334e9c0 MT |
35 | |
36 | // Destroy it | |
37 | ASSERT_NULL(pakfire_jail_unref(jail)); | |
38 | ||
00ba1d9a MT |
39 | // Create an interactive jail |
40 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, PAKFIRE_JAIL_INTERACTIVE)); | |
41 | ||
42 | // Destroy it again | |
43 | ASSERT_NULL(pakfire_jail_unref(jail)); | |
44 | ||
3334e9c0 MT |
45 | return EXIT_SUCCESS; |
46 | ||
47 | FAIL: | |
48 | return EXIT_FAILURE; | |
49 | } | |
50 | ||
32d5f21d MT |
51 | static int test_env(const struct test* t) { |
52 | struct pakfire_jail* jail = NULL; | |
53 | ||
54 | // Create a new jail | |
d639929b | 55 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); |
32d5f21d | 56 | |
d5bc8fe0 MT |
57 | // Check if the default variables are set |
58 | ASSERT(pakfire_jail_get_env(jail, "LANG")); | |
59 | ASSERT(pakfire_jail_get_env(jail, "TERM")); | |
60 | ||
32d5f21d MT |
61 | // Fetch a non-existing environment variable |
62 | ASSERT_NULL(pakfire_jail_get_env(jail, "VARIABLE")); | |
63 | ||
64 | // Set a variable | |
65 | ASSERT_SUCCESS(pakfire_jail_set_env(jail, "VARIABLE", "VALUE")); | |
66 | ||
67 | // Read the value back | |
68 | ASSERT_STRING_EQUALS(pakfire_jail_get_env(jail, "VARIABLE"), "VALUE"); | |
69 | ||
70 | // Destroy it | |
71 | ASSERT_NULL(pakfire_jail_unref(jail)); | |
72 | ||
73 | return EXIT_SUCCESS; | |
74 | ||
75 | FAIL: | |
76 | return EXIT_FAILURE; | |
77 | } | |
78 | ||
0bd84dc1 MT |
79 | static int test_exec(const struct test* t) { |
80 | struct pakfire_jail* jail = NULL; | |
81 | ||
0bd84dc1 MT |
82 | // Create a new jail |
83 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); | |
84 | ||
85 | // Try to execute something | |
2e664540 | 86 | ASSERT_SUCCESS(pakfire_jail_exec(jail, cmd_hello_world, NULL)); |
0bd84dc1 MT |
87 | |
88 | // Destroy it | |
89 | ASSERT_NULL(pakfire_jail_unref(jail)); | |
90 | ||
91 | return EXIT_SUCCESS; | |
92 | ||
93 | FAIL: | |
94 | return EXIT_FAILURE; | |
95 | } | |
32d5f21d | 96 | |
6962faab MT |
97 | static int test_launch_into_cgroup(const struct test* t) { |
98 | struct pakfire_cgroup* cgroup = NULL; | |
99 | struct pakfire_jail* jail = NULL; | |
100 | int r = EXIT_FAILURE; | |
101 | ||
102 | // Create a new cgroup | |
103 | ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup, t->pakfire, "pakfire-test", 0)); | |
104 | ||
105 | // Create a new jail | |
106 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); | |
107 | ||
108 | // Connect jail to the cgroup | |
109 | ASSERT_SUCCESS(pakfire_jail_set_cgroup(jail, cgroup)); | |
110 | ||
111 | // Run command | |
2e664540 | 112 | ASSERT(pakfire_jail_exec(jail, cmd_hello_world, NULL) == 0); |
6962faab MT |
113 | |
114 | r = EXIT_SUCCESS; | |
115 | ||
116 | FAIL: | |
117 | if (cgroup) { | |
118 | pakfire_cgroup_destroy(cgroup); | |
119 | pakfire_cgroup_unref(cgroup); | |
120 | } | |
121 | if (jail) | |
122 | pakfire_jail_unref(jail); | |
123 | ||
124 | return r; | |
125 | } | |
126 | ||
17841bb8 MT |
127 | static int test_nice(const struct test* t) { |
128 | struct pakfire_jail* jail = NULL; | |
12b9b39f | 129 | char* output = NULL; |
17841bb8 MT |
130 | int r = EXIT_FAILURE; |
131 | ||
132 | const char* argv[] = { | |
133 | "/command", "print-nice", NULL, | |
134 | }; | |
135 | ||
17841bb8 MT |
136 | // Create a new jail |
137 | ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0)); | |
138 | ||
139 | // Set invalid nice levels | |
140 | ASSERT_ERRNO(pakfire_jail_nice(jail, 100), EINVAL); | |
141 | ASSERT_ERRNO(pakfire_jail_nice(jail, -100), EINVAL); | |
142 | ||
143 | // Set something sane | |
144 | ASSERT_SUCCESS(pakfire_jail_nice(jail, 5)); | |
145 | ||
12b9b39f | 146 | // Check if the nice level has been set |
17841bb8 | 147 | ASSERT_SUCCESS(pakfire_jail_exec(jail, argv, &output)); |
12b9b39f | 148 | ASSERT_STRING_EQUALS(output, "5\n"); |
17841bb8 MT |
149 | |
150 | // Success | |
151 | r = EXIT_SUCCESS; | |
152 | ||
153 | FAIL: | |
154 | if (jail) | |
155 | pakfire_jail_unref(jail); | |
12b9b39f | 156 | if (output) |
17841bb8 | 157 | free(output); |
17841bb8 MT |
158 | |
159 | return r; | |
160 | } | |
161 | ||
b0ead88b | 162 | int main(int argc, const char* argv[]) { |
3334e9c0 | 163 | testsuite_add_test(test_create); |
32d5f21d | 164 | testsuite_add_test(test_env); |
0bd84dc1 | 165 | testsuite_add_test(test_exec); |
6962faab | 166 | testsuite_add_test(test_launch_into_cgroup); |
17841bb8 | 167 | testsuite_add_test(test_nice); |
3334e9c0 | 168 | |
b0ead88b | 169 | return testsuite_run(argc, argv); |
3334e9c0 | 170 | } |