Debian Codegraph @

tests/api/trace.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2014-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 /* Trace API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static const char *trace_path = "/tmp/prt-trace.log";
32
33 static void set_up(void) {
34 if (p == NULL) {
35 p = permanent_pool = make_sub_pool(NULL);
36 }
37
38 init_inet();
39 }
40
41 static void tear_down(void) {
42 (void) unlink(trace_path);
43
44 pr_inet_clear();
45 pr_trace_set_options(PR_TRACE_OPT_DEFAULT);
46
47 if (p) {
48 destroy_pool(p);
49 p = permanent_pool = NULL;
50 }
51 }
52
53 #if defined(PR_USE_TRACE)
54 START_TEST (trace_set_levels_test) {
55 int min_level, max_level, res;
56 const char *channel;
57
58 res = pr_trace_set_levels(NULL, 0, 0);
59 ck_assert_msg(res < 0, "Failed to handle null channel, no table");
60 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
61 errno, strerror(errno));
62
63 channel = "foo";
64 min_level = 3;
65 max_level = 1;
66 res = pr_trace_set_levels(channel, min_level, max_level);
67 ck_assert_msg(res < 0, "Failed to handle min level > max level");
68 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
69 errno, strerror(errno));
70
71 min_level = 1;
72 max_level = 2;
73 res = pr_trace_set_levels(channel, min_level, max_level);
74 ck_assert_msg(res == 0, "Failed to handle valid channel and levels: %s",
75 strerror(errno));
76
77 res = pr_trace_set_levels(PR_TRACE_DEFAULT_CHANNEL, 1, 5);
78 ck_assert_msg(res == 0, "Failed to set default channels: %s", strerror(errno));
79
80 res = pr_trace_set_levels(PR_TRACE_DEFAULT_CHANNEL, 0, 0);
81 ck_assert_msg(res == 0, "Failed to set default channels: %s", strerror(errno));
82 }
83 END_TEST
84
85 START_TEST (trace_get_table_test) {
86 pr_table_t *res;
87
88 res = pr_trace_get_table();
89 ck_assert_msg(res == NULL, "Failed to handle uninitialized Trace API");
90 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)",
91 errno, strerror(errno));
92
93 pr_trace_set_levels("foo", 1, 2);
94
95 res = pr_trace_get_table();
96 ck_assert_msg(res != NULL, "Did not get Trace API table as expected: %s",
97 strerror(errno));
98 }
99 END_TEST
100
101 START_TEST (trace_get_max_level_test) {
102 int min_level, max_level, res;
103 const char *channel;
104
105 channel = "foo";
106 min_level = 1;
107 max_level = 2;
108
109 res = pr_trace_get_max_level(NULL);
110 ck_assert_msg(res < 0, "Failed to handle null channel");
111 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
112 errno, strerror(errno));
113
114 res = pr_trace_get_max_level("bar");
115 ck_assert_msg(res < 0, "Failed to handle unset channels/levels");
116 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %d (%s)",
117 errno, strerror(errno));
118
119 res = pr_trace_set_levels(channel, min_level, max_level);
120 ck_assert_msg(res == 0, "Failed to set '%s:%d-%d': %s", channel, min_level,
121 max_level, strerror(errno));
122
123 res = pr_trace_get_max_level("bar");
124 ck_assert_msg(res < 0, "Failed to handle unknown channel");
125 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)",
126 errno, strerror(errno));
127
128 res = pr_trace_get_max_level(channel);
129 ck_assert_msg(res == max_level, "Failed to get level %d for channel '%s': %s",
130 max_level, channel, strerror(errno));
131 }
132 END_TEST
133
134 START_TEST (trace_get_min_level_test) {
135 int min_level, max_level, res;
136 const char *channel;
137
138 channel = "foo";
139 min_level = 1;
140 max_level = 2;
141
142 res = pr_trace_get_min_level(NULL);
143 ck_assert_msg(res < 0, "Failed to handle null channel");
144 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
145 errno, strerror(errno));
146
147 res = pr_trace_get_min_level("bar");
148 ck_assert_msg(res < 0, "Failed to handle unset channels/levels");
149 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %d (%s)",
150 errno, strerror(errno));
151
152 res = pr_trace_set_levels(channel, min_level, max_level);
153 ck_assert_msg(res == 0, "Failed to set '%s:%d-%d': %s", channel, min_level,
154 max_level, strerror(errno));
155
156 res = pr_trace_get_min_level("bar");
157 ck_assert_msg(res < 0, "Failed to handle unknown channel");
158 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)",
159 errno, strerror(errno));
160
161 res = pr_trace_get_min_level(channel);
162 ck_assert_msg(res == min_level, "Failed to get level %d for channel '%s': %s",
163 min_level, channel, strerror(errno));
164 }
165 END_TEST
166
167 START_TEST (trace_get_level_test) {
168 int min_level, max_level, res;
169 const char *channel;
170
171 channel = "foo";
172 min_level = 1;
173 max_level = 2;
174
175 res = pr_trace_get_level(NULL);
176 ck_assert_msg(res < 0, "Failed to handle null channel");
177 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
178 errno, strerror(errno));
179
180 res = pr_trace_get_level("bar");
181 ck_assert_msg(res < 0, "Failed to handle unset channels/levels");
182 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM, got %d (%s)",
183 errno, strerror(errno));
184
185 res = pr_trace_set_levels(channel, min_level, max_level);
186 ck_assert_msg(res == 0, "Failed to set '%s:%d-%d': %s", channel, min_level,
187 max_level, strerror(errno));
188
189 res = pr_trace_get_level("bar");
190 ck_assert_msg(res < 0, "Failed to handle unknown channel");
191 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)",
192 errno, strerror(errno));
193
194 res = pr_trace_get_level(channel);
195 ck_assert_msg(res == max_level, "Failed to get level %d for channel '%s': %s",
196 max_level, channel, strerror(errno));
197 }
198 END_TEST
199
200 START_TEST (trace_parse_levels_test) {
201 int min_level, max_level, res;
202 char *level_str;
203
204 res = pr_trace_parse_levels(NULL, NULL, NULL);
205 ck_assert_msg(res < 0, "Failed to handle null arguments");
206 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
207 errno, strerror(errno));
208
209 res = pr_trace_parse_levels("", &min_level, &max_level);
210 ck_assert_msg(res < 0, "Failed to handle empty string");
211 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
212 errno, strerror(errno));
213
214 level_str = "foo";
215 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
216 ck_assert_msg(res < 0, "Failed to handle invalid levels string");
217 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
218 errno, strerror(errno));
219
220 level_str = pstrdup(p, "-7");
221 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
222 ck_assert_msg(res < 0, "Failed to handle negative levels string");
223 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
224 errno, strerror(errno));
225
226 /* Overflow the int data type, in order to get a negative number without
227 * using the dash.
228 */
229 level_str = pstrdup(p, "2147483653");
230 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
231 ck_assert_msg(res < 0, "Failed to handle negative levels string");
232 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
233 errno, strerror(errno));
234
235 level_str = pstrdup(p, "0");
236 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
237 ck_assert_msg(res == 0, "Failed to handle single level zero string");
238 ck_assert_msg(min_level == 0, "Expected min level 0, got %d", max_level);
239 ck_assert_msg(max_level == 0, "Expected max level 0, got %d", max_level);
240
241 level_str = pstrdup(p, "7");
242 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
243 ck_assert_msg(res == 0, "Failed to handle single level string");
244 ck_assert_msg(min_level == 1, "Expected min level 1, got %d", max_level);
245 ck_assert_msg(max_level == 7, "Expected max level 7, got %d", max_level);
246
247 level_str = pstrdup(p, "abc-def");
248 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
249 ck_assert_msg(res < 0, "Failed to handle invalid levels string");
250 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
251 errno, strerror(errno));
252
253 level_str = pstrdup(p, "1-def");
254 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
255 ck_assert_msg(res < 0, "Failed to handle invalid levels string");
256 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
257 errno, strerror(errno));
258
259 /* Overflow the int data type, in order to get a negative number without
260 * using the dash.
261 */
262 level_str = pstrdup(p, "2147483653-10");
263 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
264 ck_assert_msg(res < 0, "Failed to handle negative levels string");
265 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
266 errno, strerror(errno));
267
268 level_str = pstrdup(p, "10-2147483653");
269 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
270 ck_assert_msg(res < 0, "Failed to handle negative levels string");
271 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
272 errno, strerror(errno));
273
274 level_str = pstrdup(p, "-7-5");
275 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
276 ck_assert_msg(res < 0, "Failed to handle negative levels string");
277 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
278 errno, strerror(errno));
279
280 level_str = pstrdup(p, "0--1");
281 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
282 ck_assert_msg(res < 0, "Failed to handle single level zero string");
283 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
284 errno, strerror(errno));
285
286 level_str = pstrdup(p, "8-7");
287 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
288 ck_assert_msg(res < 0, "Failed to handle max level < min level");
289 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
290 errno, strerror(errno));
291
292 level_str = pstrdup(p, "1-7");
293 res = pr_trace_parse_levels(level_str, &min_level, &max_level);
294 ck_assert_msg(res == 0, "Failed to handle levels string");
295 ck_assert_msg(min_level == 1, "Expected min level 1, got %d", max_level);
296 ck_assert_msg(max_level == 7, "Expected max level 7, got %d", max_level);
297 }
298 END_TEST
299
300 START_TEST (trace_msg_test) {
301 int res;
302 char *channel, msg[16384];
303
304 res = pr_trace_msg(NULL, -1, NULL);
305 ck_assert_msg(res < 0, "Failed to handle null arguments");
306 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
307 strerror(errno), errno);
308
309 channel = "testsuite";
310
311 mark_point();
312 res = pr_trace_msg(channel, -1, NULL);
313 ck_assert_msg(res < 0, "Failed to handle bad level");
314 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
315 strerror(errno), errno);
316
317 mark_point();
318 res = pr_trace_msg(channel, 1, NULL);
319 ck_assert_msg(res < 0, "Failed to handle null message");
320 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
321 strerror(errno), errno);
322
323 mark_point();
324 pr_trace_set_levels(channel, 1, 10);
325
326 mark_point();
327 memset(msg, 'A', sizeof(msg)-1);
328 msg[sizeof(msg)-1] = '\0';
329 pr_trace_msg(channel, 5, "%s", msg);
330
331 mark_point();
332 session.c = pr_inet_create_conn(p, -1, NULL, INPORT_ANY, FALSE);
333 ck_assert_msg(session.c != NULL, "Failed to create conn: %s", strerror(errno));
334 session.c->local_addr = session.c->remote_addr =
335 pr_netaddr_get_addr(p, "127.0.0.1", NULL);
336
337 mark_point();
338 res = pr_trace_set_options(PR_TRACE_OPT_LOG_CONN_IPS|PR_TRACE_OPT_USE_TIMESTAMP_MILLIS);
339 ck_assert_msg(res == 0, "Failed to set options: %s", strerror(errno));
340 pr_trace_msg(channel, 5, "%s", "alef bet vet?");
341
342 res = pr_trace_set_options(0);
343 ck_assert_msg(res == 0, "Failed to set options: %s", strerror(errno));
344 pr_trace_msg(channel, 5, "%s", "alef bet vet?");
345
346 pr_inet_close(p, session.c);
347 session.c = NULL;
348
349 pr_trace_set_levels(channel, 0, 0);
350 }
351 END_TEST
352
353 START_TEST (trace_set_file_test) {
354 int res;
355 const char *path;
356
357 path = "/";
358 res = pr_trace_set_file(path);
359 ck_assert_msg(res < 0, "Failed to handle path '%s'", path);
360 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
361 strerror(errno), errno);
362
363 path = "/tmp";
364 res = pr_trace_set_file(path);
365 ck_assert_msg(res < 0, "Failed to handle path '%s'", path);
366 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
367 strerror(errno), errno);
368
369 mark_point();
370 path = trace_path;
371 res = pr_trace_set_file(path);
372 ck_assert_msg(res == 0, "Failed to set trace file '%s': %s", path,
373 strerror(errno));
374 pr_trace_set_levels("foo", 1, 20);
375
376 mark_point();
377 pr_trace_msg("foo", 1, "bar?");
378 pr_trace_msg("foo", 1, "baz!");
379
380 mark_point();
381 res = pr_trace_set_options(PR_TRACE_OPT_LOG_CONN_IPS|PR_TRACE_OPT_USE_TIMESTAMP_MILLIS);
382 ck_assert_msg(res == 0, "Failed to set options: %s", strerror(errno));
383
384 pr_trace_msg("foo", 1, "quxx?");
385 pr_trace_msg("foo", 1, "QUZZ!");
386
387 res = pr_trace_set_options(PR_TRACE_OPT_DEFAULT);
388 ck_assert_msg(res == 0, "Failed to set default options: %s", strerror(errno));
389
390 pr_trace_set_levels("foo", 0, 0);
391 res = pr_trace_set_file(NULL);
392 ck_assert_msg(res == 0, "Failed to reset trace file: %s", strerror(errno));
393
394 (void) unlink(trace_path);
395 }
396 END_TEST
397
398 START_TEST (trace_restart_test) {
399 pr_trace_set_levels("testsuite", 1, 10);
400 pr_event_generate("core.restart", NULL);
401 }
402 END_TEST
403 #endif /* PR_USE_TRACE */
404
405 Suite *tests_get_trace_suite(void) {
406 Suite *suite;
407 TCase *testcase;
408
409 suite = suite_create("trace");
410 testcase = tcase_create("base");
411
412 tcase_add_checked_fixture(testcase, set_up, tear_down);
413
414 #if defined(PR_USE_TRACE)
415 tcase_add_test(testcase, trace_set_levels_test);
416 tcase_add_test(testcase, trace_get_table_test);
417 tcase_add_test(testcase, trace_get_max_level_test);
418 tcase_add_test(testcase, trace_get_min_level_test);
419 tcase_add_test(testcase, trace_get_level_test);
420 tcase_add_test(testcase, trace_parse_levels_test);
421 tcase_add_test(testcase, trace_msg_test);
422 tcase_add_test(testcase, trace_set_file_test);
423 tcase_add_test(testcase, trace_restart_test);
424 #endif /* PR_USE_TRACE */
425
426 suite_add_tcase(suite, testcase);
427 return suite;
428 }