Debian Codegraph @

tests/api/modules.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2022 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 /* Modules API tests */
26
27 #include "tests.h"
28
29 extern module *loaded_modules;
30
31 static pool *p = NULL;
32
33 static void set_up(void) {
34 if (p == NULL) {
35 p = permanent_pool = make_sub_pool(NULL);
36 }
37
38 init_stash();
39 modules_init();
40 }
41
42 static void tear_down(void) {
43 loaded_modules = NULL;
44
45 if (p != NULL) {
46 destroy_pool(p);
47 p = permanent_pool = NULL;
48 }
49 }
50
51 /* Tests */
52
53 static int sess_init_eperm = FALSE;
54
55 static int module_sess_init_cb(void) {
56 if (sess_init_eperm) {
57 sess_init_eperm = FALSE;
58 errno = EPERM;
59 return -1;
60 }
61
62 return 0;
63 }
64
65 START_TEST (module_sess_init_test) {
66 int res;
67 module m;
68
69 res = modules_session_init();
70 ck_assert_msg(res == 0, "Failed to initialize modules: %s", strerror(errno));
71
72 memset(&m, 0, sizeof(m));
73 m.name = "testsuite";
74
75 loaded_modules = &m;
76 res = modules_session_init();
77 ck_assert_msg(res == 0, "Failed to initialize modules: %s", strerror(errno));
78
79 m.sess_init = module_sess_init_cb;
80 res = modules_session_init();
81 ck_assert_msg(res == 0, "Failed to initialize modules: %s", strerror(errno));
82
83 sess_init_eperm = TRUE;
84 res = modules_session_init();
85 ck_assert_msg(res < 0, "Initialized modules unexpectedly");
86 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
87 strerror(errno), errno);
88
89 loaded_modules = NULL;
90 }
91 END_TEST
92
93 START_TEST (module_command_exists_test) {
94 int res;
95
96 res = command_exists(NULL);
97 ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
98 }
99 END_TEST
100
101 START_TEST (module_exists_test) {
102 unsigned char res;
103 module m;
104
105 res = pr_module_exists(NULL);
106 ck_assert_msg(res == FALSE, "Failed to handle null argument");
107 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
108 strerror(errno), errno);
109
110 res = pr_module_exists("mod_foo.c");
111 ck_assert_msg(res == FALSE, "Failed to handle nonexistent module");
112 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
113 strerror(errno), errno);
114
115 memset(&m, 0, sizeof(m));
116 m.name = "bar";
117
118 loaded_modules = &m;
119
120 res = pr_module_exists("mod_foo.c");
121 ck_assert_msg(res == FALSE, "Failed to handle nonexistent module");
122 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
123 strerror(errno), errno);
124
125 res = pr_module_exists("mod_bar.c");
126 ck_assert_msg(res == TRUE, "Failed to detect existing module");
127
128 res = pr_module_exists("mod_BAR.c");
129 ck_assert_msg(res == FALSE, "Failed to handle nonexistent module");
130 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
131 strerror(errno), errno);
132
133 loaded_modules = NULL;
134 }
135 END_TEST
136
137 START_TEST (module_get_test) {
138 module m, *res;
139
140 res = pr_module_get(NULL);
141 ck_assert_msg(res == NULL, "Failed to handle null argument");
142 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
143 strerror(errno), errno);
144
145 res = pr_module_get("mod_foo.c");
146 ck_assert_msg(res == NULL, "Failed to handle nonexistent module");
147 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
148 strerror(errno), errno);
149
150 memset(&m, 0, sizeof(m));
151 m.name = "bar";
152
153 loaded_modules = &m;
154
155 res = pr_module_get("mod_foo.c");
156 ck_assert_msg(res == NULL, "Failed to handle nonexistent module");
157 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
158 strerror(errno), errno);
159
160 res = pr_module_get("mod_bar.c");
161 ck_assert_msg(res != NULL, "Failed to detect existing module");
162 ck_assert_msg(res == &m, "Expected %p, got %p", &m, res);
163
164 res = pr_module_get("mod_BAR.c");
165 ck_assert_msg(res == NULL, "Failed to handle nonexistent module");
166 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
167 strerror(errno), errno);
168
169 loaded_modules = NULL;
170 }
171 END_TEST
172
173 static unsigned int listed = 0;
174 static int module_listf(const char *fmt, ...) {
175 listed++;
176 return 0;
177 }
178
179 START_TEST (module_list_test) {
180 module m, m2;
181
182 mark_point();
183 listed = 0;
184 modules_list2(module_listf, 0);
185 ck_assert_msg(listed > 0, "Expected >0, got %u", listed);
186
187 memset(&m, 0, sizeof(m));
188 m.name = "testsuite";
189 m.module_version = "a.b";
190
191 memset(&m2, 0, sizeof(m2));
192 m2.name = "testsuite2";
193
194 m.next = &m2;
195 loaded_modules = &m;
196
197 mark_point();
198 listed = 0;
199 modules_list2(module_listf, PR_MODULES_LIST_FL_SHOW_STATIC);
200 ck_assert_msg(listed > 0, "Expected >0, got %u", listed);
201
202 mark_point();
203 listed = 0;
204 modules_list2(module_listf, PR_MODULES_LIST_FL_SHOW_VERSION);
205 ck_assert_msg(listed > 0, "Expected >0, got %u", listed);
206
207 mark_point();
208 modules_list(PR_MODULES_LIST_FL_SHOW_STATIC);
209
210 loaded_modules = NULL;
211 }
212 END_TEST
213
214 static int init_cb(void) {
215 errno = EACCES;
216 return -1;
217 }
218
219 START_TEST (module_load_test) {
220 int res;
221 module m;
222
223 res = pr_module_load(NULL);
224 ck_assert_msg(res < 0, "Failed to handle null argument");
225 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
226 strerror(errno), errno);
227
228 memset(&m, 0, sizeof(m));
229
230 res = pr_module_load(&m);
231 ck_assert_msg(res < 0, "Failed to handle null name");
232 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
233 strerror(errno), errno);
234
235 m.name = "foo";
236
237 res = pr_module_load(&m);
238 ck_assert_msg(res < 0, "Failed to handle badly versioned module");
239 ck_assert_msg(errno == EACCES, "Expected EACCES (%d), got %s (%d)", EACCES,
240 strerror(errno), errno);
241
242 m.api_version = PR_MODULE_API_VERSION;
243 m.init = init_cb;
244
245 res = pr_module_load(&m);
246 ck_assert_msg(res < 0, "Failed to handle bad module init callback");
247 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
248 strerror(errno), errno);
249
250 m.init = NULL;
251
252 res = pr_module_load(&m);
253 ck_assert_msg(res == 0, "Failed to load module: %s", strerror(errno));
254
255 res = pr_module_load(&m);
256 ck_assert_msg(res < 0, "Failed to handle duplicate module load");
257 ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
258 strerror(errno), errno);
259 }
260 END_TEST
261
262 START_TEST (module_unload_test) {
263 int res;
264 module m;
265 authtable authtab[] = {
266 { 0, "setpwent", NULL },
267 { 0, NULL, NULL }
268 };
269 cmdtable cmdtab[] = {
270 { CMD, C_RETR, G_READ, NULL, TRUE, FALSE, CL_READ },
271 { HOOK, "foo", G_READ, NULL, FALSE, FALSE },
272 { 0, NULL }
273 };
274 conftable conftab[] = {
275 { "TestSuite", NULL, NULL },
276 { NULL }
277 };
278
279 res = pr_module_unload(NULL);
280 ck_assert_msg(res < 0, "Failed to handle null argument");
281 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
282 strerror(errno), errno);
283
284 memset(&m, 0, sizeof(m));
285
286 res = pr_module_unload(&m);
287 ck_assert_msg(res < 0, "Failed to handle null module name");
288 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
289 strerror(errno), errno);
290
291 m.name = "bar";
292
293 res = pr_module_unload(&m);
294 ck_assert_msg(res < 0, "Failed to handle nonexistent module");
295 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
296 strerror(errno), errno);
297
298 loaded_modules = &m;
299
300 res = pr_module_unload(&m);
301 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
302
303 res = pr_module_unload(&m);
304 ck_assert_msg(res < 0, "Failed to handle nonexistent module");
305 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
306 strerror(errno), errno);
307
308 m.authtable = authtab;
309 m.cmdtable = cmdtab;
310 m.conftable = conftab;
311 loaded_modules = &m;
312
313 res = pr_module_unload(&m);
314 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
315
316 loaded_modules = NULL;
317 }
318 END_TEST
319
320 START_TEST (module_load_authtab_test) {
321 int res;
322 module m;
323 authtable authtab[] = {
324 { 0, "setpwent", NULL },
325 { 0, NULL, NULL }
326 };
327
328 res = pr_module_load_authtab(NULL);
329 ck_assert_msg(res < 0, "Failed to handle null arguments");
330 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
331 strerror(errno), errno);
332
333 memset(&m, 0, sizeof(m));
334
335 res = pr_module_load_authtab(&m);
336 ck_assert_msg(res < 0, "Failed to handle null module name");
337 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
338 strerror(errno), errno);
339
340 m.name = "testsuite";
341 res = pr_module_load_authtab(&m);
342 ck_assert_msg(res == 0, "Failed to load module authtab: %s", strerror(errno));
343
344 pr_module_unload(&m);
345 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
346
347 m.authtable = authtab;
348 res = pr_module_load_authtab(&m);
349 ck_assert_msg(res == 0, "Failed to load module authtab: %s", strerror(errno));
350
351 pr_module_unload(&m);
352 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
353 }
354 END_TEST
355
356 START_TEST (module_load_cmdtab_test) {
357 int res;
358 module m;
359 cmdtable cmdtab[] = {
360 { CMD, C_RETR, G_READ, NULL, TRUE, FALSE, CL_READ },
361 { HOOK, "foo", G_READ, NULL, FALSE, FALSE },
362 { 0, NULL }
363 };
364
365 res = pr_module_load_cmdtab(NULL);
366 ck_assert_msg(res < 0, "Failed to handle null arguments");
367 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
368 strerror(errno), errno);
369
370 memset(&m, 0, sizeof(m));
371
372 res = pr_module_load_cmdtab(&m);
373 ck_assert_msg(res < 0, "Failed to handle null module name");
374 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
375 strerror(errno), errno);
376
377 m.name = "testsuite";
378 res = pr_module_load_cmdtab(&m);
379 ck_assert_msg(res == 0, "Failed to load module cmdtab: %s", strerror(errno));
380
381 pr_module_unload(&m);
382 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
383
384 m.name = "testsuite";
385 m.cmdtable = cmdtab;
386 res = pr_module_load_cmdtab(&m);
387 ck_assert_msg(res == 0, "Failed to load module cmdtab: %s", strerror(errno));
388
389 pr_module_unload(&m);
390 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
391 }
392 END_TEST
393
394 START_TEST (module_load_conftab_test) {
395 int res;
396 module m;
397 conftable conftab[] = {
398 { "TestSuite", NULL, NULL },
399 { NULL }
400 };
401
402 res = pr_module_load_conftab(NULL);
403 ck_assert_msg(res < 0, "Failed to handle null arguments");
404 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
405 strerror(errno), errno);
406
407 memset(&m, 0, sizeof(m));
408
409 res = pr_module_load_conftab(&m);
410 ck_assert_msg(res < 0, "Failed to handle null module name");
411 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
412 strerror(errno), errno);
413
414 m.name = "testsuite";
415 res = pr_module_load_conftab(&m);
416 ck_assert_msg(res == 0, "Failed to load module conftab: %s", strerror(errno));
417
418 pr_module_unload(&m);
419 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
420
421 m.conftable = conftab;
422 res = pr_module_load_conftab(&m);
423 ck_assert_msg(res == 0, "Failed to load module conftab: %s", strerror(errno));
424
425 pr_module_unload(&m);
426 ck_assert_msg(res == 0, "Failed to unload module: %s", strerror(errno));
427 }
428 END_TEST
429
430 static modret_t *call_cb(cmd_rec *cmd) {
431 return PR_HANDLED(cmd);
432 }
433
434 START_TEST (module_call_test) {
435 modret_t *res;
436 module m;
437 cmd_rec *cmd;
438
439 res = pr_module_call(NULL, NULL, NULL);
440 ck_assert_msg(res == NULL, "Failed to handle null arguments");
441 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
442 errno);
443
444 memset(&m, 0, sizeof(m));
445
446 res = pr_module_call(&m, NULL, NULL);
447 ck_assert_msg(res == NULL, "Failed to handle null callback, cmd arguments");
448 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
449 errno);
450
451 res = pr_module_call(NULL, call_cb, NULL);
452 ck_assert_msg(res == NULL, "Failed to handle null module, cmd arguments");
453 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
454 errno);
455
456 cmd = pcalloc(p, sizeof(cmd_rec));
457 cmd->pool = p;
458
459 res = pr_module_call(NULL, NULL, cmd);
460 ck_assert_msg(res == NULL, "Failed to handle null module, callback arguments");
461 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
462 errno);
463
464 res = pr_module_call(&m, call_cb, NULL);
465 ck_assert_msg(res == NULL, "Failed to handle null cmd argument");
466 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
467 errno);
468
469 res = pr_module_call(&m, NULL, cmd);
470 ck_assert_msg(res == NULL, "Failed to handle null callback argument");
471 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
472 errno);
473
474 res = pr_module_call(NULL, call_cb, cmd);
475 ck_assert_msg(res == NULL, "Failed to handle null module argument");
476 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
477 errno);
478
479 res = pr_module_call(&m, call_cb, cmd);
480 ck_assert_msg(res != NULL, "Failed to call function: %s", strerror(errno));
481 ck_assert_msg(MODRET_ISHANDLED(res), "Expected HANDLED result");
482 }
483 END_TEST
484
485 START_TEST (module_create_ret_test) {
486 cmd_rec *cmd;
487 modret_t *mr;
488 char *numeric, *msg;
489
490 mr = mod_create_ret(NULL, 0, NULL, NULL);
491 ck_assert_msg(mr == NULL, "Failed to handle null arguments");
492 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
493 strerror(errno), errno);
494
495 cmd = pr_cmd_alloc(p, 1, "testsuite");
496 mr = mod_create_ret(cmd, 1, NULL, NULL);
497 ck_assert_msg(mr != NULL, "Failed to create modret: %s", strerror(errno));
498 ck_assert_msg(mr->mr_error == 1, "Expected 1, got %d", mr->mr_error);
499 ck_assert_msg(mr->mr_numeric == NULL, "Expected null, got '%s'",
500 mr->mr_numeric);
501 ck_assert_msg(mr->mr_message == NULL, "Expected null, got '%s'",
502 mr->mr_message);
503
504 numeric = "foo";
505 msg = "bar";
506 mr = mod_create_ret(cmd, 1, numeric, msg);
507 ck_assert_msg(mr != NULL, "Failed to create modret: %s", strerror(errno));
508 ck_assert_msg(mr->mr_error == 1, "Expected 1, got %d", mr->mr_error);
509 ck_assert_msg(mr->mr_numeric != NULL, "Expected '%s', got null", numeric);
510 ck_assert_msg(strcmp(mr->mr_numeric, numeric) == 0,
511 "Expected '%s', got '%s'", numeric, mr->mr_numeric);
512 ck_assert_msg(mr->mr_message != NULL, "Expected '%s', got null", msg);
513 ck_assert_msg(strcmp(mr->mr_message, msg) == 0,
514 "Expected '%s', got '%s'", msg, mr->mr_message);
515 }
516 END_TEST
517
518 START_TEST (module_create_error_test) {
519 cmd_rec *cmd;
520 modret_t *mr;
521
522 mr = mod_create_error(NULL, 0);
523 ck_assert_msg(mr == NULL, "Failed to handle null arguments");
524 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
525 strerror(errno), errno);
526
527 cmd = pr_cmd_alloc(p, 1, "testsuite");
528 mr = mod_create_error(cmd, 1);
529 ck_assert_msg(mr != NULL, "Failed to create modret: %s", strerror(errno));
530 ck_assert_msg(mr->mr_error == 1, "Expected 1, got %d", mr->mr_error);
531 }
532 END_TEST
533
534 START_TEST (module_create_data_test) {
535 cmd_rec *cmd;
536 modret_t *mr;
537 int data = 1;
538
539 mr = mod_create_data(NULL, NULL);
540 ck_assert_msg(mr == NULL, "Failed to handle null arguments");
541 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
542 strerror(errno), errno);
543
544 cmd = pr_cmd_alloc(p, 1, "testsuite");
545 mr = mod_create_data(cmd, &data);
546 ck_assert_msg(mr != NULL, "Failed to create modret: %s", strerror(errno));
547 ck_assert_msg(mr->data == &data, "Expected %p, got %p", &data, mr->data);
548 }
549 END_TEST
550
551 Suite *tests_get_modules_suite(void) {
552 Suite *suite;
553 TCase *testcase;
554
555 suite = suite_create("modules");
556
557 testcase = tcase_create("module");
558 tcase_add_checked_fixture(testcase, set_up, tear_down);
559
560 tcase_add_test(testcase, module_sess_init_test);
561 tcase_add_test(testcase, module_command_exists_test);
562 tcase_add_test(testcase, module_exists_test);
563 tcase_add_test(testcase, module_get_test);
564 tcase_add_test(testcase, module_list_test);
565 tcase_add_test(testcase, module_load_test);
566 tcase_add_test(testcase, module_unload_test);
567 tcase_add_test(testcase, module_load_authtab_test);
568 tcase_add_test(testcase, module_load_cmdtab_test);
569 tcase_add_test(testcase, module_load_conftab_test);
570 tcase_add_test(testcase, module_call_test);
571
572 tcase_add_test(testcase, module_create_ret_test);
573 tcase_add_test(testcase, module_create_error_test);
574 tcase_add_test(testcase, module_create_data_test);
575
576 suite_add_tcase(suite, testcase);
577 return suite;
578 }