Debian Codegraph @

tests/api/timers.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2020 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 /* Timers API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static int repeat_cb = FALSE;
32 static unsigned int timer_triggered_count = 0;
33
34 /* Fixtures */
35
36 static void set_up(void) {
37 if (p == NULL) {
38 p = permanent_pool = make_sub_pool(NULL);
39 }
40
41 repeat_cb = FALSE;
42 timer_triggered_count = 0;
43
44 timers_init();
45
46 if (getenv("TEST_VERBOSE") != NULL) {
47 pr_trace_use_stderr(TRUE);
48 pr_trace_set_levels("timers", 1, 20);
49 }
50 }
51
52 static void tear_down(void) {
53 if (getenv("TEST_VERBOSE") != NULL) {
54 pr_trace_use_stderr(FALSE);
55 pr_trace_set_levels("timers", 0, 0);
56 }
57
58 if (p != NULL) {
59 destroy_pool(p);
60 p = permanent_pool = NULL;
61 }
62 }
63
64 /* Helper functions */
65
66 static void timers_handle_signals(void) {
67 if (recvd_signal_flags) {
68 recvd_signal_flags &= ~RECEIVED_SIG_ALRM;
69 handle_alarm();
70 }
71 }
72
73 static int timers_test_cb(CALLBACK_FRAME) {
74 timer_triggered_count++;
75
76 if (repeat_cb) {
77 return 1;
78 }
79
80 return 0;
81 }
82
83 /* Tests */
84
85 START_TEST (timer_add_test) {
86 int res;
87 unsigned int ok = 0;
88
89 res = pr_timer_add(-1, 0, NULL, NULL, NULL);
90 ck_assert_msg(res == -1, "Failed to handle negative seconds");
91 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
92
93 res = pr_timer_add(0, 0, NULL, NULL, NULL);
94 ck_assert_msg(res == -1, "Failed to handle null description");
95 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
96
97 res = pr_timer_add(0, 0, NULL, NULL, "test");
98 ck_assert_msg(res == -1, "Failed to handle zero count");
99 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
100
101 res = pr_timer_add(1, 0, NULL, NULL, "test");
102 ck_assert_msg(res == -1, "Failed to handle null callback");
103 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
104
105 res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
106 ck_assert_msg(res == 0, "Failed to allocate timer: %s", strerror(errno));
107
108 res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
109 ck_assert_msg(res == -1, "Failed to handle duplicate timer: %s",
110 strerror(errno));
111 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
112
113 sleep(2);
114 timers_handle_signals();
115
116 ok = 1;
117 ck_assert_msg(timer_triggered_count == ok ||
118 timer_triggered_count == (ok - 1),
119 "Timer failed to fire (expected count %u, got %u)", ok,
120 timer_triggered_count);
121
122 repeat_cb = TRUE;
123
124 /* Make sure that pr_timer_add() returns an incrementing timer ID,
125 * starting with 1024, if the input timer ID is -1.
126 */
127 res = pr_timer_add(1, -1, NULL, timers_test_cb, "test");
128 ck_assert_msg(res == 1024, "Failed to allocate timer: %s", strerror(errno));
129
130 sleep(1);
131 timers_handle_signals();
132
133 /* Allow for races between timers and testsuite. Aren't timing-based
134 * unit tests fun?
135 */
136
137 ok = 2;
138 ck_assert_msg(timer_triggered_count == ok ||
139 timer_triggered_count == (ok + 1) ||
140 timer_triggered_count == (ok - 1),
141 "Timer failed to fire (expected count %u, got %u)", ok,
142 timer_triggered_count);
143
144 sleep(1);
145 timers_handle_signals();
146
147 ok = 3;
148 ck_assert_msg(timer_triggered_count == ok ||
149 timer_triggered_count == (ok + 1) ||
150 timer_triggered_count == (ok - 1),
151 "Timer failed to fire (expected count %u, got %u)", ok,
152 timer_triggered_count);
153 }
154 END_TEST
155
156 START_TEST (timer_remove_test) {
157 int res;
158
159 res = pr_timer_remove(0, NULL);
160 ck_assert_msg(res == 0, "Failed to remove timer: %s", strerror(errno));
161
162 res = pr_timer_add(1, 0, NULL, timers_test_cb, "test");
163 ck_assert_msg(res == 0, "Failed to add timer (%d): %s", res, strerror(errno));
164
165 res = pr_timer_remove(1, NULL);
166 ck_assert_msg(res == -1, "Failed to return -1 for non-matching timer ID");
167 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
168
169 res = pr_timer_remove(0, NULL);
170 ck_assert_msg(res == 0, "Failed to remove timer (%d): %s", res,
171 strerror(errno));
172
173 ck_assert_msg(timer_triggered_count == 0,
174 "Expected trigger count of 0, got %u", timer_triggered_count);
175 }
176 END_TEST
177
178 START_TEST (timer_remove_multi_test) {
179 int res;
180 module m;
181
182 /* By providing a negative timerno, the return value should be the
183 * dynamically generated timerno, which is greater than or equal to
184 * 1024.
185 */
186 res = pr_timer_add(3, -1, &m, timers_test_cb, "test1");
187 ck_assert_msg(res >= 1024, "Failed to add timer (%d): %s", res,
188 strerror(errno));
189
190 res = pr_timer_add(3, -1, &m, timers_test_cb, "test2");
191 ck_assert_msg(res >= 1024, "Failed to add timer (%d): %s", res,
192 strerror(errno));
193
194 res = pr_timer_add(3, -1, &m, timers_test_cb, "test3");
195 ck_assert_msg(res >= 1024, "Failed to add timer (%d): %s", res,
196 strerror(errno));
197
198 res = pr_timer_remove(-1, &m);
199 ck_assert_msg(res == 3, "Failed to remove timers (%d): %s", res,
200 strerror(errno));
201 }
202 END_TEST
203
204 START_TEST (timer_reset_test) {
205 int res;
206 unsigned int ok = 0;
207
208 mark_point();
209 res = pr_timer_reset(0, NULL);
210 ck_assert_msg(res == -1, "Failed to handle empty timer list");
211 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
212
213 mark_point();
214 res = pr_timer_add(2, 1, NULL, timers_test_cb, "test");
215 ck_assert_msg(res == 1, "Failed to add timer: %s", strerror(errno));
216
217 mark_point();
218 res = pr_timer_reset(2, NULL);
219 ck_assert_msg(res == 0, "Expected timer ID 1, got %d", res);
220
221 sleep(1);
222 timers_handle_signals();
223
224 mark_point();
225 ck_assert_msg(timer_triggered_count == ok,
226 "Timer fired unexpectedly (expected count %u, got %u)", ok,
227 timer_triggered_count);
228
229 mark_point();
230 res = pr_timer_reset(1, NULL);
231 ck_assert_msg(res == 1, "Failed to reset timer");
232
233 sleep(1);
234 timers_handle_signals();
235
236 ck_assert_msg(timer_triggered_count == ok,
237 "Timer fired unexpectedly (expected count %u, got %u)", ok,
238 timer_triggered_count);
239
240 sleep(2);
241 timers_handle_signals();
242
243 ok = 1;
244 ck_assert_msg(timer_triggered_count == ok ||
245 timer_triggered_count == (ok - 1),
246 "Timer failed to fire (expected count %u, got %u)", ok,
247 timer_triggered_count);
248 }
249 END_TEST
250
251 START_TEST (timer_sleep_test) {
252 int res;
253
254 res = pr_timer_sleep(0);
255 ck_assert_msg(res == -1, "Failed to handle sleep len of zero");
256 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
257
258 pr_alarms_block();
259 res = pr_timer_sleep(1);
260 ck_assert_msg(res == -1, "Failed to handle blocked alarms when sleeping");
261 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
262
263 pr_alarms_unblock();
264
265 res = pr_timer_sleep(1);
266 ck_assert_msg(res == 0, "Failed to sleep: %s", strerror(errno));
267 }
268 END_TEST
269
270 START_TEST (timer_usleep_test) {
271 int res;
272
273 res = pr_timer_usleep(0);
274 ck_assert_msg(res == -1, "Failed to handle sleep len of zero");
275 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
276
277 res = pr_timer_usleep(500000);
278 ck_assert_msg(res == 0, "Failed to sleep: %s", strerror(errno));
279 }
280 END_TEST
281
282 Suite *tests_get_timers_suite(void) {
283 Suite *suite;
284 TCase *testcase;
285
286 suite = suite_create("timers");
287 testcase = tcase_create("base");
288
289 tcase_add_checked_fixture(testcase, set_up, tear_down);
290
291 tcase_add_test(testcase, timer_add_test);
292 tcase_add_test(testcase, timer_remove_test);
293 tcase_add_test(testcase, timer_remove_multi_test);
294 tcase_add_test(testcase, timer_reset_test);
295 tcase_add_test(testcase, timer_sleep_test);
296 tcase_add_test(testcase, timer_usleep_test);
297
298 /* Allow a longer timeout on these tests, as they will need a second or
299 * two to actually run through the test itself, plus overhead.
300 */
301 tcase_set_timeout(testcase, 5);
302
303 suite_add_tcase(suite, testcase);
304 return suite;
305 }