Debian Codegraph @

tests/api/rlimit.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2015-2018 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 /* RLimit API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = make_sub_pool(NULL);
34 }
35 }
36
37 static void tear_down(void) {
38 if (p) {
39 destroy_pool(p);
40 p = NULL;
41 }
42 }
43
44 START_TEST (rlimit_core_test) {
45 int res;
46 rlim_t curr_rlim = 0, max_rlim = 0 ;
47
48 res = pr_rlimit_get_core(NULL, NULL);
49 ck_assert_msg(res == -1, "Failed to handle null arguments");
50 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
51 strerror(errno), errno);
52
53 res = pr_rlimit_get_core(&curr_rlim, &max_rlim);
54 ck_assert_msg(res == 0, "Failed to get core resource limits: %s",
55 strerror(errno));
56
57 curr_rlim = max_rlim = -1;
58 res = pr_rlimit_set_core(curr_rlim, max_rlim);
59
60 /* Note that some platforms will NOT fail a setrlimit(2) command if the
61 * arguments are negative. Hence this conditional check.
62 */
63 if (res < 0) {
64 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
65 strerror(errno), errno);
66 }
67 }
68 END_TEST
69
70 START_TEST (rlimit_cpu_test) {
71 int res;
72 rlim_t curr_rlim = 0, max_rlim = 0 ;
73
74 res = pr_rlimit_get_cpu(NULL, NULL);
75 ck_assert_msg(res == -1, "Failed to handle null arguments");
76 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
77 strerror(errno), errno);
78
79 res = pr_rlimit_get_cpu(&curr_rlim, &max_rlim);
80 ck_assert_msg(res == 0, "Failed to get CPU resource limits: %s",
81 strerror(errno));
82
83 curr_rlim = max_rlim = -1;
84 res = pr_rlimit_set_cpu(curr_rlim, max_rlim);
85
86 /* Note that some platforms will NOT fail a setrlimit(2) command if the
87 * arguments are negative. Hence this conditional check.
88 */
89 if (res < 0) {
90 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
91 strerror(errno), errno);
92 }
93 }
94 END_TEST
95
96 START_TEST (rlimit_files_test) {
97 int res;
98 rlim_t curr_rlim = 0, max_rlim = 0 ;
99
100 res = pr_rlimit_get_files(NULL, NULL);
101 ck_assert_msg(res == -1, "Failed to handle null arguments");
102 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
103 strerror(errno), errno);
104
105 res = pr_rlimit_get_files(&curr_rlim, &max_rlim);
106 ck_assert_msg(res == 0, "Failed to get file resource limits: %s",
107 strerror(errno));
108
109 curr_rlim = max_rlim = -1;
110 res = pr_rlimit_set_files(curr_rlim, max_rlim);
111
112 /* Note that some platforms will NOT fail a setrlimit(2) command if the
113 * arguments are negative. Hence this conditional check.
114 */
115 if (res < 0) {
116 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
117 strerror(errno), errno);
118 }
119 }
120 END_TEST
121
122 START_TEST (rlimit_memory_test) {
123 int res;
124 rlim_t curr_rlim = 0, max_rlim = 0 ;
125
126 res = pr_rlimit_get_memory(NULL, NULL);
127 ck_assert_msg(res == -1, "Failed to handle null arguments");
128 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
129 strerror(errno), errno);
130
131 res = pr_rlimit_get_memory(&curr_rlim, &max_rlim);
132 ck_assert_msg(res == 0, "Failed to get memory resource limits: %s",
133 strerror(errno));
134
135 curr_rlim = max_rlim = -1;
136 res = pr_rlimit_set_memory(curr_rlim, max_rlim);
137
138 /* Note that some platforms will NOT fail a setrlimit(2) command if the
139 * arguments are negative. Hence this conditional check.
140 */
141 if (res < 0) {
142 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
143 strerror(errno), errno);
144 }
145 }
146 END_TEST
147
148 #ifdef RLIMIT_NPROC
149 START_TEST (rlimit_nproc_test) {
150 int res;
151 rlim_t curr_rlim = 0, max_rlim = 0 ;
152
153 res = pr_rlimit_get_nproc(NULL, NULL);
154 ck_assert_msg(res == -1, "Failed to handle null arguments");
155 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)",
156 strerror(errno), errno);
157
158 res = pr_rlimit_get_nproc(&curr_rlim, &max_rlim);
159 ck_assert_msg(res == 0, "Failed to get nproc resource limits: %s",
160 strerror(errno));
161
162 curr_rlim = max_rlim = -1;
163 res = pr_rlimit_set_nproc(curr_rlim, max_rlim);
164
165 /* Note that some platforms will NOT fail a setrlimit(2) command if the
166 * arguments are negative. Hence this conditional check.
167 */
168 if (res < 0) {
169 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %s (%d)",
170 strerror(errno), errno);
171 }
172 }
173 END_TEST
174 #endif /* RLIMIT_NPROC */
175
176 Suite *tests_get_rlimit_suite(void) {
177 Suite *suite;
178 TCase *testcase;
179
180 suite = suite_create("rlimit");
181 testcase = tcase_create("base");
182
183 tcase_add_checked_fixture(testcase, set_up, tear_down);
184
185 tcase_add_test(testcase, rlimit_core_test);
186 tcase_add_test(testcase, rlimit_cpu_test);
187 tcase_add_test(testcase, rlimit_files_test);
188 tcase_add_test(testcase, rlimit_memory_test);
189 #ifdef RLIMIT_NPROC
190 tcase_add_test(testcase, rlimit_nproc_test);
191 #endif /* RLIMIT_NPROC */
192
193 suite_add_tcase(suite, testcase);
194 return suite;
195 }