File: modules.c

package info (click to toggle)
proftpd-dfsg 1.3.3a-6squeeze7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,868 kB
  • ctags: 10,217
  • sloc: ansic: 111,549; perl: 94,506; sh: 17,265; makefile: 1,986
file content (509 lines) | stat: -rw-r--r-- 17,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * ProFTPD - FTP server testsuite
 * Copyright (c) 2008 The ProFTPD Project team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exemption, The ProFTPD Project team and other respective
 * copyright holders give permission to link this program with OpenSSL, and
 * distribute the resulting executable, without including the source code for
 * OpenSSL in the source distribution.
 */

/* Modules API tests
 * $Id: modules.c,v 1.1 2008/10/06 18:16:50 castaglia Exp $
 */

#include "tests.h"

static pool *p = NULL;

static void set_up(void) {
  if (p == NULL) {
    p = permanent_pool = make_sub_pool(NULL);
  }

  init_stash();
  modules_init();
}

static void tear_down(void) {
  if (p) {
    destroy_pool(p);
    p = NULL;
    permanent_pool = NULL;
  } 
}

START_TEST (stash_add_symbol_test) {
  int res;
  conftable conftab;
  cmdtable cmdtab, hooktab;
  authtable authtab;
  
  res = pr_stash_add_symbol(0, NULL);
  fail_unless(res == -1, "Failed to handle null arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_stash_add_symbol(0, "Foo");
  fail_unless(res == -1, "Failed to handle bad type");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&conftab, 0, sizeof(conftab));
  res = pr_stash_add_symbol(PR_SYM_CONF, &conftab);
  fail_unless(res == -1, "Failed to handle null conf name");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM (got %d)",
    errno);

  memset(&cmdtab, 0, sizeof(cmdtab));
  res = pr_stash_add_symbol(PR_SYM_CMD, &cmdtab);
  fail_unless(res == -1, "Failed to handle null cmd name");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM (got %d)",
    errno);

  memset(&authtab, 0, sizeof(authtab));
  res = pr_stash_add_symbol(PR_SYM_AUTH, &authtab);
  fail_unless(res == -1, "Failed to handle null auth name");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM (got %d)",
    errno);

  memset(&hooktab, 0, sizeof(hooktab));
  res = pr_stash_add_symbol(PR_SYM_HOOK, &hooktab);
  fail_unless(res == -1, "Failed to handle null hook name");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM (got %d)",
    errno);

  memset(&conftab, 0, sizeof(conftab));
  conftab.directive = pstrdup(p, "Foo");
  res = pr_stash_add_symbol(PR_SYM_CONF, &conftab);
  fail_unless(res == 0, "Failed to add CONF symbol: %s", strerror(errno));

  memset(&cmdtab, 0, sizeof(cmdtab));
  cmdtab.command = pstrdup(p, "Foo");
  res = pr_stash_add_symbol(PR_SYM_CMD, &cmdtab);
  fail_unless(res == 0, "Failed to add CMD symbol: %s", strerror(errno));

  memset(&authtab, 0, sizeof(authtab));
  authtab.name = pstrdup(p, "Foo");
  res = pr_stash_add_symbol(PR_SYM_AUTH, &authtab);
  fail_unless(res == 0, "Failed to add AUTH symbol: %s", strerror(errno));

  memset(&hooktab, 0, sizeof(hooktab));
  hooktab.command = pstrdup(p, "Foo");
  res = pr_stash_add_symbol(PR_SYM_HOOK, &hooktab);
  fail_unless(res == 0, "Failed to add HOOK symbol: %s", strerror(errno));
}
END_TEST

START_TEST (stash_get_symbol_test) {
  int res;
  void *sym;
  conftable conftab;
  cmdtable cmdtab, hooktab;
  authtable authtab;

  sym = pr_stash_get_symbol(0, NULL, NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle null arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  sym = pr_stash_get_symbol(0, "foo", NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle bad type argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  sym = pr_stash_get_symbol(PR_SYM_CONF, "foo", NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle nonexistent CONF symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  sym = pr_stash_get_symbol(PR_SYM_CMD, "foo", NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle nonexistent CMD symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  sym = pr_stash_get_symbol(PR_SYM_AUTH, "foo", NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle nonexistent AUTH symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  sym = pr_stash_get_symbol(PR_SYM_HOOK, "foo", NULL, NULL);
  fail_unless(sym == NULL, "Failed to handle nonexistent HOOK symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&conftab, 0, sizeof(conftab));
  conftab.directive = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_CONF, &conftab);
  fail_unless(res == 0, "Failed to add CONF symbol: %s", strerror(errno));

  sym = pr_stash_get_symbol(PR_SYM_CONF, "foo", NULL, NULL);
  fail_unless(sym != NULL, "Failed to get CONF symbol: %s", strerror(errno));
  fail_unless(sym == &conftab, "Expected %p, got %p", &conftab, sym);

  sym = pr_stash_get_symbol(PR_SYM_CONF, "foo", sym, NULL);
  fail_unless(sym == NULL, "Unexpectedly found CONF symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&cmdtab, 0, sizeof(cmdtab));
  cmdtab.command = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_CMD, &cmdtab);
  fail_unless(res == 0, "Failed to add CMD symbol: %s", strerror(errno));

  sym = pr_stash_get_symbol(PR_SYM_CMD, "foo", NULL, NULL);
  fail_unless(sym != NULL, "Failed to get CMD symbol: %s", strerror(errno));
  fail_unless(sym == &cmdtab, "Expected %p, got %p", &cmdtab, sym);

  sym = pr_stash_get_symbol(PR_SYM_CMD, "foo", sym, NULL);
  fail_unless(sym == NULL, "Unexpectedly found CMD symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&authtab, 0, sizeof(authtab));
  authtab.name = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_AUTH, &authtab);
  fail_unless(res == 0, "Failed to add AUTH symbol: %s", strerror(errno));

  sym = pr_stash_get_symbol(PR_SYM_AUTH, "foo", NULL, NULL);
  fail_unless(sym != NULL, "Failed to get AUTH symbol: %s", strerror(errno));
  fail_unless(sym == &authtab, "Expected %p, got %p", &authtab, sym);

  sym = pr_stash_get_symbol(PR_SYM_AUTH, "foo", sym, NULL);
  fail_unless(sym == NULL, "Unexpectedly found AUTH symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&hooktab, 0, sizeof(hooktab));
  hooktab.command = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_HOOK, &hooktab);
  fail_unless(res == 0, "Failed to add HOOK symbol: %s", strerror(errno));

  sym = pr_stash_get_symbol(PR_SYM_HOOK, "foo", NULL, NULL);
  fail_unless(sym != NULL, "Failed to get HOOK symbol: %s", strerror(errno));
  fail_unless(sym == &hooktab, "Expected %p, got %p", &hooktab, sym);

  sym = pr_stash_get_symbol(PR_SYM_HOOK, "foo", sym, NULL);
  fail_unless(sym == NULL, "Unexpectedly found HOOK symbol");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);
}
END_TEST

START_TEST (stash_remove_symbol_test) {
  int res;
  conftable conftab;
  cmdtable cmdtab, hooktab;
  authtable authtab;

  res = pr_stash_remove_symbol(0, NULL, NULL);
  fail_unless(res == -1, "Failed to handle null arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_stash_remove_symbol(0, "foo", NULL);
  fail_unless(res == -1, "Failed to handle bad symbol type");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_stash_remove_symbol(PR_SYM_CONF, "foo", NULL);
  fail_unless(res == 0, "Expected %d, got %d", 0, res);

  memset(&conftab, 0, sizeof(conftab));
  conftab.directive = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_CONF, &conftab);
  fail_unless(res == 0, "Failed to add CONF symbol: %s", strerror(errno));

  res = pr_stash_add_symbol(PR_SYM_CONF, &conftab);
  fail_unless(res == 0, "Failed to add CONF symbol: %s", strerror(errno));

  res = pr_stash_remove_symbol(PR_SYM_CONF, "foo", NULL);
  fail_unless(res == 2, "Expected %d, got %d", 2, res);

  memset(&cmdtab, 0, sizeof(cmdtab));
  cmdtab.command = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_CMD, &cmdtab);
  fail_unless(res == 0, "Failed to add CMD symbol: %s", strerror(errno));

  res = pr_stash_add_symbol(PR_SYM_CMD, &cmdtab);
  fail_unless(res == 0, "Failed to add CMD symbol: %s", strerror(errno));

  res = pr_stash_remove_symbol(PR_SYM_CMD, "foo", NULL);
  fail_unless(res == 2, "Expected %d, got %d", 2, res);

  memset(&authtab, 0, sizeof(authtab));
  authtab.name = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_AUTH, &authtab);
  fail_unless(res == 0, "Failed to add AUTH symbol: %s", strerror(errno));

  res = pr_stash_add_symbol(PR_SYM_AUTH, &authtab);
  fail_unless(res == 0, "Failed to add AUTH symbol: %s", strerror(errno));

  res = pr_stash_remove_symbol(PR_SYM_AUTH, "foo", NULL);
  fail_unless(res == 2, "Expected %d, got %d", 2, res);

  memset(&hooktab, 0, sizeof(hooktab));
  hooktab.command = pstrdup(p, "foo");
  res = pr_stash_add_symbol(PR_SYM_HOOK, &hooktab);
  fail_unless(res == 0, "Failed to add HOOK symbol: %s", strerror(errno));

  res = pr_stash_add_symbol(PR_SYM_HOOK, &hooktab);
  fail_unless(res == 0, "Failed to add HOOK symbol: %s", strerror(errno));

  res = pr_stash_remove_symbol(PR_SYM_HOOK, "foo", NULL);
  fail_unless(res == 2, "Expected %d, got %d", 2, res);
}
END_TEST

START_TEST (module_exists_test) {
  unsigned char res;
  module m;

  res = pr_module_exists(NULL);
  fail_unless(res == FALSE, "Failed to handle null argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_exists("mod_foo.c");
  fail_unless(res == FALSE, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&m, 0, sizeof(m));
  m.name = "bar";

  loaded_modules = &m;

  res = pr_module_exists("mod_foo.c");
  fail_unless(res == FALSE, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  res = pr_module_exists("mod_bar.c");
  fail_unless(res == TRUE, "Failed to detect existing module");

  res = pr_module_exists("mod_BAR.c");
  fail_unless(res == FALSE, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);
}
END_TEST

START_TEST (module_get_test) {
  module m, *res;

  res = pr_module_get(NULL);
  fail_unless(res == NULL, "Failed to handle null argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_get("mod_foo.c");
  fail_unless(res == NULL, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  memset(&m, 0, sizeof(m));
  m.name = "bar";

  loaded_modules = &m;

  res = pr_module_get("mod_foo.c");
  fail_unless(res == NULL, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  res = pr_module_get("mod_bar.c");
  fail_unless(res != NULL, "Failed to detect existing module");
  fail_unless(res == &m, "Expected %p, got %p", &m, res);

  res = pr_module_get("mod_BAR.c");
  fail_unless(res == NULL, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);
}
END_TEST

static int init_cb(void) {
  errno = EACCES;
  return -1;
}

START_TEST (module_load_test) {
  int res;
  module m;

  res = pr_module_load(NULL);
  fail_unless(res == -1, "Failed to handle null argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  memset(&m, 0, sizeof(m));

  res = pr_module_load(&m);
  fail_unless(res == -1, "Failed to handle null name");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  m.name = "foo";

  res = pr_module_load(&m);
  fail_unless(res == -1, "Failed to handle badly versioned module");
  fail_unless(errno == EACCES, "Failed to set errno to EACCES (got %d)",
    errno);

  m.api_version = PR_MODULE_API_VERSION;
  m.init = init_cb;

  res = pr_module_load(&m);
  fail_unless(res == -1, "Failed to handle bad module init callback");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM (got %d)",
    errno);

  m.init = NULL;

  res = pr_module_load(&m);
  fail_unless(res == 0, "Failed to load module: %s", strerror(errno));

  res = pr_module_load(&m);
  fail_unless(res == -1, "Failed to handle duplicate module load");
  fail_unless(errno == EEXIST, "Failed to set errno to EEXIST (got %d)",
    errno);
}
END_TEST

START_TEST (module_unload_test) {
  int res;
  module m;

  res = pr_module_unload(NULL);
  fail_unless(res == -1, "Failed to handle null argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  memset(&m, 0, sizeof(m));

  res = pr_module_unload(&m);
  fail_unless(res == -1, "Failed to handle null module name");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  m.name = "bar";

  res = pr_module_unload(&m);
  fail_unless(res == -1, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);

  loaded_modules = &m;

  res = pr_module_unload(&m);
  fail_unless(res == 0, "Failed to unload module: %s", strerror(errno));

  res = pr_module_unload(&m);
  fail_unless(res == -1, "Failed to handle nonexistent module");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT (got %d)",
    errno);
}
END_TEST

static modret_t *call_cb(cmd_rec *cmd) {
  return PR_HANDLED(cmd);
}

START_TEST (module_call_test) {
  modret_t *res;
  module m;
  cmd_rec *cmd;

  res = pr_module_call(NULL, NULL, NULL);
  fail_unless(res == NULL, "Failed to handle null arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  memset(&m, 0, sizeof(m));

  res = pr_module_call(&m, NULL, NULL);
  fail_unless(res == NULL, "Failed to handle null callback, cmd arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_call(NULL, call_cb, NULL);
  fail_unless(res == NULL, "Failed to handle null module, cmd arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  cmd = pcalloc(p, sizeof(cmd_rec));
  cmd->pool = p;

  res = pr_module_call(NULL, NULL, cmd);
  fail_unless(res == NULL, "Failed to handle null module, callback arguments");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_call(&m, call_cb, NULL);
  fail_unless(res == NULL, "Failed to handle null cmd argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_call(&m, NULL, cmd);
  fail_unless(res == NULL, "Failed to handle null callback argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_call(NULL, call_cb, cmd);
  fail_unless(res == NULL, "Failed to handle null module argument");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL (got %d)",
    errno);

  res = pr_module_call(&m, call_cb, cmd);
  fail_unless(res != NULL, "Failed to call function: %s", strerror(errno));
  fail_unless(MODRET_ISHANDLED(res), "Expected HANDLED result");
}
END_TEST

Suite *tests_get_modules_suite(void) {
  Suite *suite;
  TCase *testcase;

  suite = suite_create("modules");

  testcase = tcase_create("stash");
  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, stash_add_symbol_test);
  tcase_add_test(testcase, stash_get_symbol_test);
  tcase_add_test(testcase, stash_remove_symbol_test);

  suite_add_tcase(suite, testcase);

  testcase = tcase_create("module");
  tcase_add_checked_fixture(testcase, set_up, tear_down);

  tcase_add_test(testcase, module_exists_test);
  tcase_add_test(testcase, module_get_test);
  tcase_add_test(testcase, module_load_test);
  tcase_add_test(testcase, module_unload_test);
  tcase_add_test(testcase, module_call_test);

  suite_add_tcase(suite, testcase);

  /* XXX At some point, unit tests for the mod_create_*() functions
   * should be written.
   */

  return suite;
}