Debian Codegraph @

tests/api/class.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2017 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 /* Class API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 /* Fixtures */
32
33 static void set_up(void) {
34 if (p == NULL) {
35 p = permanent_pool = make_sub_pool(NULL);
36 }
37
38 main_server = pcalloc(p, sizeof(server_rec));
39 main_server->pool = p;
40
41 init_class();
42 init_netaddr();
43
44 if (getenv("TEST_VERBOSE") != NULL) {
45 pr_trace_set_levels("class", 1, 20);
46 }
47 }
48
49 static void tear_down(void) {
50 if (getenv("TEST_VERBOSE") != NULL) {
51 pr_trace_set_levels("class", 0, 0);
52 }
53
54 if (p != NULL) {
55 destroy_pool(p);
56 main_server = NULL;
57 p = permanent_pool = NULL;
58 }
59 }
60
61 /* Tests */
62
63 START_TEST (class_open_test) {
64 int res;
65 const char *name;
66
67 res = pr_class_open(NULL, NULL);
68 ck_assert_msg(res == -1, "Failed to handle NULL arguments");
69 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
70
71 res = pr_class_open(p, NULL);
72 ck_assert_msg(res == -1, "Failed to handle NULL name argument");
73 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
74
75 name = "foo";
76 res = pr_class_open(NULL, name);
77 ck_assert_msg(res == -1, "Failed to handle NULL pool argument");
78 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
79
80 res = pr_class_open(p, name);
81 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
82 ck_assert_msg(main_server->config_type == CONF_CLASS,
83 "Expected config_type of %d, got %d", CONF_CLASS, main_server->config_type);
84 }
85 END_TEST
86
87 START_TEST (class_add_acl_test) {
88 pr_netacl_t *acl;
89 int res;
90
91 res = pr_class_add_acl(NULL);
92 ck_assert_msg(res == -1, "Failed to handle NULL argument");
93 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
94
95 acl = pr_netacl_create(p, "all");
96 ck_assert_msg(acl != NULL, "Failed to handle ACL string 'all': %s",
97 strerror(errno));
98
99 res = pr_class_add_acl(acl);
100 ck_assert_msg(res == -1, "Failed to handle unopened class");
101 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
102
103 res = pr_class_open(p, "foo");
104 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
105
106 res = pr_class_add_acl(acl);
107 ck_assert_msg(res == 0, "Failed to add ACL: %s", strerror(errno));
108 }
109 END_TEST
110
111 START_TEST (class_add_note_test) {
112 const char *k = NULL;
113 void *v = NULL;
114 size_t vsz = 0;
115 int res;
116
117 res = pr_class_add_note(k, v, vsz);
118 ck_assert_msg(res == -1, "Failed to handle NULL argument");
119 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
120
121 k = "KEY";
122 v = "VALUE";
123 vsz = 6;
124
125 res = pr_class_add_note(k, v, vsz);
126 ck_assert_msg(res == -1, "Failed to handle unopened class");
127 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
128
129 res = pr_class_open(p, "foo");
130 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
131
132 res = pr_class_add_note(k, v, vsz);
133 ck_assert_msg(res == 0, "Failed to add note: %s", strerror(errno));
134 }
135 END_TEST
136
137 START_TEST (class_close_test) {
138 pr_netacl_t *acl;
139 int res;
140
141 res = pr_class_close();
142 ck_assert_msg(res == 0, "Failed to close nonexistent current class: %s",
143 strerror(errno));
144
145 res = pr_class_open(p, "foo");
146 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
147
148 res = pr_class_close();
149 ck_assert_msg(res == -1, "Failed to close empty class");
150 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
151 ck_assert_msg(main_server->config_type == CONF_ROOT,
152 "Expected config_type of %d, got %d", CONF_ROOT, main_server->config_type);
153
154 res = pr_class_open(p, "foo");
155 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
156
157 acl = pr_netacl_create(p, "all");
158 ck_assert_msg(acl != NULL, "Failed to handle ACL string 'all': %s",
159 strerror(errno));
160
161 res = pr_class_add_acl(acl);
162 ck_assert_msg(res == 0, "Failed to add ACL: %s", strerror(errno));
163
164 res = pr_class_close();
165 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
166 ck_assert_msg(main_server->config_type == CONF_ROOT,
167 "Expected config_type of %d, got %d", CONF_ROOT, main_server->config_type);
168 }
169 END_TEST
170
171 START_TEST (class_set_satisfy_test) {
172 int res;
173
174 res = pr_class_set_satisfy(PR_CLASS_SATISFY_ANY);
175 ck_assert_msg(res == -1, "Failed to handle nonexistent current class");
176 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
177
178 res = pr_class_open(p, "foo");
179 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
180
181 res = pr_class_set_satisfy(PR_CLASS_SATISFY_ANY);
182 ck_assert_msg(res == 0, "Failed to set SATISFY_ANY: %s", strerror(errno));
183
184 res = pr_class_set_satisfy(PR_CLASS_SATISFY_ALL);
185 ck_assert_msg(res == 0, "Failed to set SATISFY_ALL: %s", strerror(errno));
186
187 res = pr_class_set_satisfy(-1);
188 ck_assert_msg(res == -1, "Failed to handle bad satisfy value");
189 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
190 }
191 END_TEST
192
193 START_TEST (class_get_test) {
194 const pr_class_t *class;
195 int res;
196 pr_netacl_t *acl;
197
198 class = pr_class_get(NULL);
199 ck_assert_msg(class == NULL, "Failed to handle empty class list");
200
201 acl = pr_netacl_create(p, "all");
202 ck_assert_msg(acl != NULL, "Failed to handle ACL string 'all': %s",
203 strerror(errno));
204
205 res = pr_class_open(p, "foo");
206 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
207
208 res = pr_class_add_acl(acl);
209 ck_assert_msg(res == 0, "Failed to add ACL: %s", strerror(errno));
210
211 class = pr_class_get(NULL);
212 ck_assert_msg(class == NULL, "Failed to handle unclosed class in list");
213
214 res = pr_class_close();
215 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
216
217 class = pr_class_get(NULL);
218 ck_assert_msg(class != NULL, "Failed to get class in list: %s",
219 strerror(errno));
220 ck_assert_msg(strcmp(class->cls_name, "foo") == 0,
221 "Expected '%s', got '%s'", "foo", class->cls_name);
222
223 class = pr_class_get(class);
224 ck_assert_msg(class == NULL, "Failed to return NULL for end-of-list");
225 }
226 END_TEST
227
228 START_TEST (class_find_test) {
229 const pr_class_t *class;
230 pr_netacl_t *acl;
231 int res;
232
233 class = pr_class_find(NULL);
234 ck_assert_msg(class == NULL, "Failed to handle NULL argument");
235 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
236
237 class = pr_class_find("foo");
238 ck_assert_msg(class == NULL, "Failed to handle empty list");
239 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
240
241 acl = pr_netacl_create(p, "all");
242 ck_assert_msg(acl != NULL, "Failed to handle ACL string 'all': %s",
243 strerror(errno));
244
245 res = pr_class_open(p, "foo");
246 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
247
248 res = pr_class_add_acl(acl);
249 ck_assert_msg(res == 0, "Failed to add ACL: %s", strerror(errno));
250
251 class = pr_class_find("foo");
252 ck_assert_msg(class == NULL, "Failed to handle empty list");
253 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
254
255 res = pr_class_close();
256 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
257
258 class = pr_class_find("foo");
259 ck_assert_msg(class != NULL, "Failed to handle class 'foo': %s",
260 strerror(errno));
261
262 class = pr_class_find("bar");
263 ck_assert_msg(class == NULL, "Failed to handle nonexistent class");
264 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
265 }
266 END_TEST
267
268 START_TEST (class_satisfied_test) {
269 const pr_netaddr_t *addr;
270 const pr_class_t *cls;
271 pr_netacl_t *acl;
272 int res;
273
274 /* Reset the class list. */
275 init_class();
276
277 mark_point();
278 res = pr_class_satisfied(p, NULL, NULL);
279 ck_assert_msg(res < 0, "Failed to handle null class");
280 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
281 strerror(errno), errno);
282
283 res = pr_class_open(p, "localhost");
284 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
285
286 acl = pr_netacl_create(p, "127.0.0.1");
287 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
288
289 res = pr_class_add_acl(acl);
290 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
291
292 res = pr_class_close();
293 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
294
295 cls = pr_class_find("localhost");
296 ck_assert_msg(cls != NULL, "Failed to find class 'localhost': %s",
297 strerror(errno));
298
299 mark_point();
300 res = pr_class_satisfied(p, cls, NULL);
301 ck_assert_msg(res < 0, "Failed to handle null addr");
302 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
303 strerror(errno), errno);
304
305 addr = pr_netaddr_get_addr(p, "localhost", FALSE);
306 ck_assert_msg(addr != NULL, "Failed to get addr: %s", strerror(errno));
307
308 mark_point();
309 res = pr_class_satisfied(p, cls, addr);
310 ck_assert_msg(res == TRUE, "Class not satisfied by address: %s",
311 strerror(errno));
312
313 res = pr_class_open(p, "!localhost");
314 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
315
316 acl = pr_netacl_create(p, "!127.0.0.1");
317 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
318
319 res = pr_class_add_acl(acl);
320 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
321
322 res = pr_class_close();
323 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
324
325 cls = pr_class_find("!localhost");
326 ck_assert_msg(cls != NULL, "Failed to find class '!localhost': %s",
327 strerror(errno));
328
329 mark_point();
330 res = pr_class_satisfied(p, cls, addr);
331 ck_assert_msg(res == FALSE, "Class satisfied unexpectedly by address");
332 }
333 END_TEST
334
335 START_TEST (class_match_addr_test) {
336 const pr_netaddr_t *addr;
337 const pr_class_t *class;
338 pr_netacl_t *acl;
339 int res;
340
341 /* Reset the class list. */
342 init_class();
343
344 mark_point();
345 class = pr_class_match_addr(NULL);
346 ck_assert_msg(class == NULL, "Failed to handle NULL argument");
347 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
348
349 addr = pr_netaddr_get_addr(p, "localhost", FALSE);
350 ck_assert_msg(addr != NULL, "Failed to get addr: %s", strerror(errno));
351
352 class = pr_class_match_addr(addr);
353 ck_assert_msg(class == NULL, "Failed to handle empty class list");
354 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
355
356 res = pr_class_open(p, "localhost");
357 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
358
359 acl = pr_netacl_create(p, "127.0.0.1");
360 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
361
362 res = pr_class_add_acl(acl);
363 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
364
365 res = pr_class_close();
366 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
367
368 res = pr_class_open(p, "!localhost");
369 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
370
371 acl = pr_netacl_create(p, "!127.0.0.1");
372 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
373
374 res = pr_class_add_acl(acl);
375 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
376
377 res = pr_class_close();
378 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
379
380 class = pr_class_match_addr(addr);
381 ck_assert_msg(class != NULL, "Failed to match class for addr: %s",
382 strerror(errno));
383 ck_assert_msg(strcmp(class->cls_name, "localhost") == 0,
384 "Expected '%s', got '%s'", "localhost", class->cls_name);
385
386 /* Reset the class list, add classes in a different order, and try again. */
387 init_class();
388
389 res = pr_class_open(p, "!localhost");
390 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
391
392 acl = pr_netacl_create(p, "!127.0.0.1");
393 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
394
395 res = pr_class_add_acl(acl);
396 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
397
398 res = pr_class_close();
399 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
400
401 res = pr_class_open(p, "localhost");
402 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
403
404 acl = pr_netacl_create(p, "127.0.0.1");
405 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
406
407 res = pr_class_add_acl(acl);
408 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
409
410 res = pr_class_close();
411 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
412
413 class = pr_class_match_addr(addr);
414 ck_assert_msg(class != NULL, "Failed to match class for addr: %s",
415 strerror(errno));
416 ck_assert_msg(strcmp(class->cls_name, "localhost") == 0,
417 "Expected '%s', got '%s'", "localhost", class->cls_name);
418
419 /* Reset the class list, and see what happens when we try to match
420 * the addr against an impossible set of rules.
421 */
422 init_class();
423
424 res = pr_class_open(p, "impossible");
425 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
426
427 acl = pr_netacl_create(p, "!127.0.0.1");
428 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
429
430 res = pr_class_add_acl(acl);
431 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
432
433 acl = pr_netacl_create(p, "127.0.0.1");
434 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
435
436 res = pr_class_add_acl(acl);
437 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
438
439 res = pr_class_set_satisfy(PR_CLASS_SATISFY_ALL);
440 ck_assert_msg(res == 0, "Failed to set satisfy value: %s", strerror(errno));
441
442 res = pr_class_close();
443 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
444
445 class = pr_class_match_addr(addr);
446 ck_assert_msg(class == NULL, "Unexpectedly matched class for addr");
447 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
448
449 /* Reset the class list, add two classes with identical rules, and
450 * verify that the first matching class wins.
451 */
452 init_class();
453
454 res = pr_class_open(p, "first");
455 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
456
457 acl = pr_netacl_create(p, "127.0.0.1");
458 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
459
460 res = pr_class_add_acl(acl);
461 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
462
463 res = pr_class_close();
464 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
465
466 res = pr_class_open(p, "second");
467 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
468
469 acl = pr_netacl_create(p, "127.0.0.1");
470 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
471
472 res = pr_class_add_acl(acl);
473 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
474
475 res = pr_class_close();
476 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
477
478 class = pr_class_match_addr(addr);
479 ck_assert_msg(class != NULL, "Failed to match class for addr: %s",
480 strerror(errno));
481 ck_assert_msg(strcmp(class->cls_name, "first") == 0,
482 "Expected '%s', got '%s'", "first", class->cls_name);
483
484 init_class();
485
486 res = pr_class_open(p, "second");
487 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
488
489 acl = pr_netacl_create(p, "127.0.0.1");
490 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
491
492 res = pr_class_add_acl(acl);
493 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
494
495 res = pr_class_close();
496 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
497
498 res = pr_class_open(p, "first");
499 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
500
501 acl = pr_netacl_create(p, "127.0.0.1");
502 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
503
504 res = pr_class_add_acl(acl);
505 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
506
507 res = pr_class_close();
508 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
509
510 class = pr_class_match_addr(addr);
511 ck_assert_msg(class != NULL, "Failed to match class for addr: %s",
512 strerror(errno));
513 ck_assert_msg(strcmp(class->cls_name, "second") == 0,
514 "Expected '%s', got '%s'", "second", class->cls_name);
515
516 init_class();
517
518 res = pr_class_open(p, "match");
519 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
520
521 acl = pr_netacl_create(p, "127.0.0.1");
522 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
523
524 res = pr_class_add_acl(acl);
525 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
526
527 res = pr_class_set_satisfy(PR_CLASS_SATISFY_ALL);
528 ck_assert_msg(res == 0, "Failed to set satisfy value: %s", strerror(errno));
529
530 res = pr_class_close();
531 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
532
533 class = pr_class_match_addr(addr);
534 ck_assert_msg(class != NULL, "Failed to match class for addr: %s",
535 strerror(errno));
536 ck_assert_msg(strcmp(class->cls_name, "match") == 0,
537 "Expected '%s', got '%s'", "match", class->cls_name);
538
539 }
540 END_TEST
541
542 Suite *tests_get_class_suite(void) {
543 Suite *suite;
544 TCase *testcase;
545
546 suite = suite_create("class");
547
548 testcase = tcase_create("base");
549
550 tcase_add_checked_fixture(testcase, set_up, tear_down);
551
552 tcase_add_test(testcase, class_open_test);
553 tcase_add_test(testcase, class_add_acl_test);
554 tcase_add_test(testcase, class_add_note_test);
555 tcase_add_test(testcase, class_close_test);
556 tcase_add_test(testcase, class_set_satisfy_test);
557 tcase_add_test(testcase, class_get_test);
558 tcase_add_test(testcase, class_find_test);
559 tcase_add_test(testcase, class_satisfied_test);
560 tcase_add_test(testcase, class_match_addr_test);
561
562 suite_add_tcase(suite, testcase);
563
564 return suite;
565 }