Debian Codegraph @

tests/api/privs.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2015-2016 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Privs API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static uid_t privs_uid = (uid_t) -1;
32 static gid_t privs_gid = (gid_t) -1;
33
34 static void set_up(void) {
35 if (p == NULL) {
36 p = permanent_pool = make_sub_pool(NULL);
37 }
38
39 init_privs();
40 privs_uid = getuid();
41 privs_gid = getgid();
42
43 if (getenv("TEST_VERBOSE") != NULL) {
44 pr_trace_set_levels("privs", 1, 20);
45 }
46 }
47
48 static void tear_down(void) {
49 if (getenv("TEST_VERBOSE") != NULL) {
50 pr_trace_set_levels("privs", 0, 0);
51 }
52
53 if (p) {
54 destroy_pool(p);
55 p = permanent_pool = NULL;
56 }
57 }
58
59 START_TEST (privs_set_nonroot_daemon_test) {
60 int nonroot, res;
61
62 res = set_nonroot_daemon(-1);
63 ck_assert_msg(res < 0, "Failed to handle non-Boolean parameter");
64 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
65 strerror(errno), errno);
66
67 nonroot = set_nonroot_daemon(TRUE);
68 ck_assert_msg(nonroot == FALSE || nonroot == TRUE, "Expected true/false, got %d",
69 nonroot);
70 set_nonroot_daemon(nonroot);
71 }
72 END_TEST
73
74 START_TEST (privs_setup_test) {
75 int nonroot, res;
76
77 if (privs_uid != 0) {
78 res = pr_privs_setup(privs_uid, privs_gid, __FILE__, __LINE__);
79 ck_assert_msg(res == 0, "Failed to setup privs: %s", strerror(errno));
80 ck_assert_msg(session.uid == privs_uid, "Expected %lu, got %lu",
81 (unsigned long) privs_uid, (unsigned long) session.uid);
82 ck_assert_msg(session.gid == privs_gid, "Expected %lu, got %lu",
83 (unsigned long) privs_gid, (unsigned long) session.gid);
84
85 nonroot = set_nonroot_daemon(FALSE);
86
87 res = pr_privs_setup(privs_uid, privs_gid, __FILE__, __LINE__);
88 ck_assert_msg(res == 0, "Failed to setup privs: %s", strerror(errno));
89 ck_assert_msg(session.uid == privs_uid, "Expected %lu, got %lu",
90 (unsigned long) privs_uid, (unsigned long) session.uid);
91 ck_assert_msg(session.gid == privs_gid, "Expected %lu, got %lu",
92 (unsigned long) privs_gid, (unsigned long) session.gid);
93
94 set_nonroot_daemon(nonroot);
95 }
96 }
97 END_TEST
98
99 START_TEST (privs_root_test) {
100 int nonroot, res;
101
102 if (privs_uid != 0) {
103 res = pr_privs_root(__FILE__, __LINE__);
104 ck_assert_msg(res == 0, "Failed to set root privs: %s", strerror(errno));
105
106 nonroot = set_nonroot_daemon(FALSE);
107
108 res = pr_privs_root(__FILE__, __LINE__);
109 ck_assert_msg(res == 0, "Failed to set root privs: %s", strerror(errno));
110
111 set_nonroot_daemon(nonroot);
112 }
113 }
114 END_TEST
115
116 START_TEST (privs_user_test) {
117 int nonroot, res;
118
119 if (privs_uid != 0) {
120 res = pr_privs_user(__FILE__, __LINE__);
121 ck_assert_msg(res == 0, "Failed to set user privs: %s", strerror(errno));
122
123 nonroot = set_nonroot_daemon(FALSE);
124
125 res = pr_privs_user(__FILE__, __LINE__);
126 ck_assert_msg(res == 0, "Failed to set user privs: %s", strerror(errno));
127
128 set_nonroot_daemon(nonroot);
129 }
130 }
131 END_TEST
132
133 START_TEST (privs_relinquish_test) {
134 int nonroot, res;
135
136 if (privs_uid != 0) {
137 res = pr_privs_relinquish(__FILE__, __LINE__);
138 ck_assert_msg(res == 0, "Failed to relinquish privs: %s", strerror(errno));
139
140 nonroot = set_nonroot_daemon(FALSE);
141
142 res = pr_privs_relinquish(__FILE__, __LINE__);
143 ck_assert_msg(res == 0, "Failed to relinquish privs: %s", strerror(errno));
144
145 set_nonroot_daemon(nonroot);
146 }
147 }
148 END_TEST
149
150 START_TEST (privs_revoke_test) {
151 int nonroot, res;
152
153 if (privs_uid != 0) {
154 res = pr_privs_revoke(__FILE__, __LINE__);
155 ck_assert_msg(res == 0, "Failed to revoke privs: %s", strerror(errno));
156
157 nonroot = set_nonroot_daemon(FALSE);
158
159 res = pr_privs_revoke(__FILE__, __LINE__);
160 ck_assert_msg(res == 0, "Failed to revoke privs: %s", strerror(errno));
161
162 set_nonroot_daemon(nonroot);
163 }
164 }
165 END_TEST
166
167 Suite *tests_get_privs_suite(void) {
168 Suite *suite;
169 TCase *testcase;
170
171 suite = suite_create("privs");
172
173 testcase = tcase_create("base");
174 tcase_add_checked_fixture(testcase, set_up, tear_down);
175
176 tcase_add_test(testcase, privs_set_nonroot_daemon_test);
177 tcase_add_test(testcase, privs_setup_test);
178 tcase_add_test(testcase, privs_root_test);
179 tcase_add_test(testcase, privs_user_test);
180 tcase_add_test(testcase, privs_relinquish_test);
181 tcase_add_test(testcase, privs_revoke_test);
182
183 suite_add_tcase(suite, testcase);
184 return suite;
185 }