]> git.ipfire.org Git - people/ms/pakfire.git/blob - tests/libpakfire/jail.c
jail: Execute command
[people/ms/pakfire.git] / tests / libpakfire / jail.c
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
21 #include <pakfire/jail.h>
22
23 #include "../testsuite.h"
24
25 static const char* cmd[2] = {
26 "/usr/bin/does-not-exist",
27 };
28
29 static int test_create(const struct test* t) {
30 struct pakfire_jail* jail = NULL;
31
32 // Create a new jail
33 ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0));
34
35 // Destroy it
36 ASSERT_NULL(pakfire_jail_unref(jail));
37
38 // Create an interactive jail
39 ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, PAKFIRE_JAIL_INTERACTIVE));
40
41 // Destroy it again
42 ASSERT_NULL(pakfire_jail_unref(jail));
43
44 return EXIT_SUCCESS;
45
46 FAIL:
47 return EXIT_FAILURE;
48 }
49
50 static int test_env(const struct test* t) {
51 struct pakfire_jail* jail = NULL;
52
53 // Create a new jail
54 ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0));
55
56 // Check if the default variables are set
57 ASSERT(pakfire_jail_get_env(jail, "LANG"));
58 ASSERT(pakfire_jail_get_env(jail, "TERM"));
59
60 // Fetch a non-existing environment variable
61 ASSERT_NULL(pakfire_jail_get_env(jail, "VARIABLE"));
62
63 // Set a variable
64 ASSERT_SUCCESS(pakfire_jail_set_env(jail, "VARIABLE", "VALUE"));
65
66 // Read the value back
67 ASSERT_STRING_EQUALS(pakfire_jail_get_env(jail, "VARIABLE"), "VALUE");
68
69 // Destroy it
70 ASSERT_NULL(pakfire_jail_unref(jail));
71
72 return EXIT_SUCCESS;
73
74 FAIL:
75 return EXIT_FAILURE;
76 }
77
78 static int test_exec(const struct test* t) {
79 struct pakfire_jail* jail = NULL;
80
81 const char* argv[] = {
82 "/does-not-exist",
83 NULL,
84 };
85
86 // Create a new jail
87 ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0));
88
89 // Try to execute something
90 ASSERT(pakfire_jail_exec(jail, argv) == 127);
91
92 // Destroy it
93 ASSERT_NULL(pakfire_jail_unref(jail));
94
95 return EXIT_SUCCESS;
96
97 FAIL:
98 return EXIT_FAILURE;
99 }
100
101 int main(int argc, char** argv) {
102 testsuite_add_test(test_create);
103 testsuite_add_test(test_env);
104 testsuite_add_test(test_exec);
105
106 return testsuite_run();
107 }