Debian Codegraph @

tests/api/table.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 /* Table 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 = make_sub_pool(NULL);
36 }
37 }
38
39 static void tear_down(void) {
40 if (p) {
41 destroy_pool(p);
42 p = NULL;
43 }
44 }
45
46 /* Helper functions */
47
48 static unsigned int b_val_count = 0;
49
50 static int do_cb(const void *key, size_t keysz, const void *value,
51 size_t valuesz, void *user_data) {
52
53 if (*((const char *) value) == 'b') {
54 b_val_count++;
55 }
56
57 return -1;
58 }
59
60 static int do_with_remove_cb(const void *key, size_t keysz, const void *value,
61 size_t valuesz, void *user_data) {
62 pr_table_t *tab;
63
64 tab = user_data;
65
66 if (*((const char *) value) == 'b') {
67 b_val_count++;
68 }
69
70 pr_table_kremove(tab, key, keysz, NULL);
71 return 0;
72 }
73
74 static void table_dump(const char *fmt, ...) {
75 }
76
77 /* Tests */
78
79 START_TEST (table_alloc_test) {
80 pr_table_t *tab;
81
82 tab = pr_table_alloc(NULL, 0);
83 ck_assert_msg(tab == NULL, "Failed to handle null arguments");
84 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
85
86 tab = pr_table_alloc(p, 0);
87 ck_assert_msg(tab != NULL, "Failed to allocate table: %s", strerror(errno));
88 }
89 END_TEST
90
91 START_TEST (table_nalloc_test) {
92 pr_table_t *tab;
93
94 tab = pr_table_nalloc(NULL, 0, 0);
95 ck_assert_msg(tab == NULL, "Failed to handle null arguments");
96 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
97
98 tab = pr_table_nalloc(p, 0, 0);
99 ck_assert_msg(tab == NULL, "Failed to handle zero chains");
100 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
101
102 tab = pr_table_nalloc(p, 0, 1);
103 ck_assert_msg(tab != NULL, "Failed to allocate table: %s", strerror(errno));
104 }
105 END_TEST
106
107 START_TEST (table_add_test) {
108 int res;
109 pr_table_t *tab;
110
111 res = pr_table_add(NULL, NULL, NULL, 0);
112 ck_assert_msg(res == -1, "Failed to handle null arguments");
113 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
114
115 tab = pr_table_alloc(p, 0);
116 ck_assert_msg(tab != NULL, "Failed to allocate table: %s", strerror(errno));
117
118 res = pr_table_add(tab, NULL, NULL, 0);
119 ck_assert_msg(res == -1, "Failed to handle null key");
120 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
121
122 res = pr_table_add(tab, "", NULL, 0);
123 ck_assert_msg(res == 0, "Failed to add null value (len 0) for empty key");
124
125 res = pr_table_add(tab, "", NULL, 1);
126 ck_assert_msg(res == -1, "Failed to handle null value (len 1) for empty key");
127 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
128
129 res = pr_table_add(tab, "", NULL, 0);
130 ck_assert_msg(res == -1, "Failed to handle duplicate (empty) key");
131 ck_assert_msg(errno == EEXIST, "Failed to set errno to EEXIST");
132 }
133 END_TEST
134
135 START_TEST (table_add_dup_test) {
136 int res;
137 pr_table_t *tab;
138
139 res = pr_table_add_dup(NULL, NULL, NULL, 0);
140 ck_assert_msg(res == -1, "Failed to handle null arguments");
141 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
142
143 tab = pr_table_alloc(p, 0);
144 ck_assert_msg(tab != NULL, "Failed to allocate table: %s", strerror(errno));
145
146 res = pr_table_add_dup(tab, NULL, NULL, 0);
147 ck_assert_msg(res == -1, "Failed to handle null key");
148 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
149
150 res = pr_table_add_dup(tab, "", NULL, 0);
151 ck_assert_msg(res == 0, "Failed to add null value (len 0) for empty key");
152
153 res = pr_table_add_dup(tab, "", NULL, 1);
154 ck_assert_msg(res == -1, "Failed to handle null value (len 1) for empty key");
155 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
156
157 res = pr_table_add_dup(tab, "", NULL, 0);
158 ck_assert_msg(res == 0, "Failed to handle empty value: %s", strerror(errno));
159
160 mark_point();
161 res = pr_table_add_dup(tab, "foo", "bar", 0);
162 ck_assert_msg(res == 0, "Failed to add 'foo': %s", strerror(errno));
163 }
164 END_TEST
165
166 START_TEST (table_count_test) {
167 int res, ok;
168 pr_table_t *tab;
169
170 res = pr_table_count(NULL);
171 ck_assert_msg(res == -1, "Failed to handle null arguments");
172 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
173
174 tab = pr_table_alloc(p, 0);
175
176 ok = 0;
177 res = pr_table_count(tab);
178 ck_assert_msg(res == ok, "Expected count %d, got %d", ok, res);
179
180 res = pr_table_add(tab, "foo", NULL, 0);
181 ck_assert_msg(res == 0, "Failed to add item to table: %s", strerror(errno));
182
183 ok = 1;
184 res = pr_table_count(tab);
185 ck_assert_msg(res == ok, "Expected count %d, got %d", ok, res);
186
187 res = pr_table_add(tab, "bar", NULL, 0);
188 ck_assert_msg(res == 0, "Failed to add item to table: %s", strerror(errno));
189
190 ok = 2;
191 res = pr_table_count(tab);
192 ck_assert_msg(res == ok, "Expected count %d, got %d", ok, res);
193 }
194 END_TEST
195
196 START_TEST (table_exists_test) {
197 int res, ok;
198 pr_table_t *tab;
199
200 res = pr_table_exists(NULL, NULL);
201 ck_assert_msg(res == -1, "Failed to handle null arguments");
202 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
203
204 tab = pr_table_alloc(p, PR_TABLE_FL_MULTI_VALUE);
205 ck_assert_msg(tab != NULL, "Failed to allocate table: %s", strerror(errno));
206
207 res = pr_table_exists(tab, NULL);
208 ck_assert_msg(res == -1, "Failed to handle null key");
209 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
210
211 res = pr_table_exists(NULL, "foo");
212 ck_assert_msg(res == -1, "Failed to handle null table");
213 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
214
215 ok = -1;
216 res = pr_table_exists(tab, "foo");
217 ck_assert_msg(res == ok, "Expected value count %d, got %d", ok, res);
218 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
219
220 res = pr_table_add(tab, "foo", "a", 0);
221 ck_assert_msg(res == 0, "Failed to add key to table: %s", strerror(errno));
222
223 ok = 1;
224 res = pr_table_exists(tab, "foo");
225 ck_assert_msg(res == ok, "Expected value count %d, got %d", ok, res);
226
227 res = pr_table_add(tab, "foo", "b", 0);
228 ck_assert_msg(res == 0, "Failed to add key to table: %s", strerror(errno));
229
230 ok = 2;
231 res = pr_table_exists(tab, "foo");
232 ck_assert_msg(res == ok, "Expected value count %d, got %d", ok, res);
233
234 mark_point();
235 res = pr_table_kexists(NULL, NULL, 0);
236 ck_assert_msg(res < 0, "Failed to handle null table");
237 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
238 strerror(errno), errno);
239
240 mark_point();
241 res = pr_table_kexists(tab, NULL, 0);
242 ck_assert_msg(res < 0, "Failed to handle null key_data");
243 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
244 strerror(errno), errno);
245 }
246 END_TEST
247
248 START_TEST (table_empty_test) {
249 int res;
250 pr_table_t *tab;
251
252 res = pr_table_empty(NULL);
253 ck_assert_msg(res == -1, "Failed to handle null table");
254 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
255
256 tab = pr_table_alloc(p, 0);
257
258 res = pr_table_empty(tab);
259 ck_assert_msg(res == 0, "Failed to empty table: %s", strerror(errno));
260
261 res = pr_table_add(tab, "foo", NULL, 0);
262 ck_assert_msg(res == 0, "Failed to add key to table: %s", strerror(errno));
263
264 res = pr_table_count(tab);
265 ck_assert_msg(res == 1, "Expected table item count of 1, got %d", res);
266
267 res = pr_table_empty(tab);
268 ck_assert_msg(res == 0, "Failed to empty table: %s", strerror(errno));
269
270 res = pr_table_count(tab);
271 ck_assert_msg(res == 0, "Expected table item count of 0, got %d", res);
272 }
273 END_TEST
274
275 START_TEST (table_free_test) {
276 int res;
277 pr_table_t *tab;
278
279 res = pr_table_free(NULL);
280 ck_assert_msg(res == -1, "Failed to handle null table");
281 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
282
283 tab = pr_table_alloc(p, 0);
284
285 res = pr_table_free(tab);
286 ck_assert_msg(res == 0, "Failed to free table: %s", strerror(errno));
287
288 tab = pr_table_alloc(p, 0);
289 res = pr_table_add(tab, "foo", "bar", 0);
290 ck_assert_msg(res == 0, "Failed to add item to table: %s", strerror(errno));
291
292 res = pr_table_free(tab);
293 ck_assert_msg(res == -1, "Failed to handle non-empty table");
294 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
295 }
296 END_TEST
297
298 START_TEST (table_get_test) {
299 int ok, xerrno;
300 const void *res;
301 pr_table_t *tab;
302 char *str;
303 size_t sz;
304
305 res = pr_table_get(NULL, NULL, NULL);
306 ck_assert_msg(res == NULL, "Failed to handle null arguments");
307 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
308
309 tab = pr_table_alloc(p, 0);
310
311 res = pr_table_get(tab, NULL, NULL);
312 ck_assert_msg(res == NULL, "Failed to handle null key");
313 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
314
315 ok = pr_table_add(tab, "foo", NULL, 0);
316 ck_assert_msg(ok == 0, "Failed to add null value to table: %s",
317 strerror(errno));
318
319 errno = xerrno = 0;
320 res = pr_table_get(tab, "foo", &sz);
321 xerrno = errno;
322
323 ck_assert_msg(res == NULL, "Failed to lookup null value: %s", strerror(errno));
324 ck_assert_msg(xerrno == 0, "Expected errno 0, got %d (%s)", xerrno,
325 strerror(xerrno));
326
327 ok = pr_table_add(tab, "bar", "baz", 0);
328 ck_assert_msg(ok == 0, "Failed to add 'bar' to table: %s", strerror(errno));
329
330 res = pr_table_get(tab, "bar", &sz);
331 ck_assert_msg(res != NULL, "Failed to lookup value for 'bar': %s",
332 strerror(errno));
333 ck_assert_msg(sz == 4, "Expected result len of 4, got %lu", (unsigned long)sz);
334
335 str = pcalloc(p, sz);
336 memcpy(str, res, sz);
337
338 ck_assert_msg(strcmp(str, "baz") == 0,
339 "Expected value '%s', got '%s'", "baz", str);
340
341 mark_point();
342 res = pr_table_kget(NULL, NULL, 0, NULL);
343 ck_assert_msg(res == NULL, "Failed to handle null table");
344 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
345 strerror(errno), errno);
346 }
347 END_TEST
348
349 static unsigned int cache_key_hash(const void *key, size_t keysz) {
350 return 1;
351 }
352
353 START_TEST (table_get_use_cache_test) {
354 int ok, xerrno;
355 const void *res;
356 pr_table_t *tab;
357 const char *key = "bar";
358 char *str;
359 size_t sz;
360
361 res = pr_table_get(NULL, NULL, NULL);
362 ck_assert_msg(res == NULL, "Failed to handle null arguments");
363 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
364
365 tab = pr_table_alloc(p, PR_TABLE_FL_USE_CACHE);
366
367 /* We use this specific key hash function to ensure that all of the keys we
368 * add to this table end up in the same linked-list chain.
369 */
370
371 ok = pr_table_ctl(tab, PR_TABLE_CTL_SET_KEY_HASH, cache_key_hash);
372 ck_assert_msg(ok == 0, "Failed to set key hash function for table: %s",
373 strerror(errno));
374
375 res = pr_table_get(tab, NULL, NULL);
376 ck_assert_msg(res == NULL, "Failed to handle null key");
377 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
378
379 ok = pr_table_add(tab, key, "baz", 0);
380 ck_assert_msg(ok == 0, "Failed to add 'bar' to table: %s", strerror(errno));
381
382 res = pr_table_get(tab, key, &sz);
383 ck_assert_msg(res != NULL, "Failed to lookup value for 'bar': %s",
384 strerror(errno));
385 ck_assert_msg(sz == 4, "Expected result len of 4, got %lu", (unsigned long)sz);
386
387 str = pcalloc(p, sz);
388 memcpy(str, res, sz);
389
390 ck_assert_msg(strcmp(str, "baz") == 0,
391 "Expected value '%s', got '%s'", "baz", str);
392
393 /* With the USE_CACHE flag on, we should still receive NULL here. */
394 errno = xerrno = 0;
395 res = pr_table_get(tab, key, &sz);
396 ck_assert_msg(res == NULL, "Failed to return null next value: %s",
397 strerror(errno));
398 ck_assert_msg(xerrno == 0, "Expected errno 0, got %d (%s)", xerrno,
399 strerror(xerrno));
400
401 }
402 END_TEST
403
404 START_TEST (table_next_test) {
405 int ok;
406 const char *res;
407 size_t sz = 0;
408 pr_table_t *tab;
409
410 res = pr_table_next(NULL);
411 ck_assert_msg(res == NULL, "Failed to handle null table");
412 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
413
414 tab = pr_table_alloc(p, 0);
415
416 res = pr_table_next(tab);
417 ck_assert_msg(res == NULL, "Failed to handle empty table");
418 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
419
420 ok = pr_table_add(tab, "foo", NULL, 0);
421 ck_assert_msg(ok == 0, "Failed to add 'foo' to table: %s", strerror(errno));
422
423 res = pr_table_next(tab);
424 ck_assert_msg(res != NULL, "Failed to get next key: %s", strerror(errno));
425 ck_assert_msg(strcmp(res, "foo") == 0,
426 "Expected key '%s', got '%s'", "foo", res);
427
428 res = pr_table_next(tab);
429 ck_assert_msg(res == NULL, "Expected no more keys, got '%s'", res);
430
431 pr_table_rewind(tab);
432
433 res = pr_table_knext(tab, &sz);
434 ck_assert_msg(res != NULL, "Failed to get next key: %s", strerror(errno));
435 ck_assert_msg(sz == 4, "Expected 4, got %lu", (unsigned long) sz);
436 ck_assert_msg(strcmp(res, "foo") == 0,
437 "Expected key '%s', got '%s'", "foo", res);
438
439 sz = 0;
440 res = pr_table_knext(tab, &sz);
441 ck_assert_msg(res == NULL, "Expected no more keys, got '%s'", res);
442 }
443 END_TEST
444
445 START_TEST (table_rewind_test) {
446 int res;
447 const char *key;
448 pr_table_t *tab;
449
450 res = pr_table_rewind(NULL);
451 ck_assert_msg(res == -1, "Failed to handle null table");
452 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
453
454 tab = pr_table_alloc(p, 0);
455
456 res = pr_table_rewind(tab);
457 ck_assert_msg(res == 0, "Failed to handle empty table");
458
459 res = pr_table_add(tab, "foo", NULL, 0);
460 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
461
462 key = pr_table_next(tab);
463 ck_assert_msg(key != NULL, "Failed to get next key: %s", strerror(errno));
464 ck_assert_msg(strcmp(key, "foo") == 0,
465 "Expected key '%s', got '%s'", "foo", key);
466
467 key = pr_table_next(tab);
468 ck_assert_msg(key == NULL, "Expected no more keys, got '%s'", key);
469
470 res = pr_table_rewind(tab);
471 ck_assert_msg(res == 0, "Failed to rewind table: %s", strerror(errno));
472
473 key = pr_table_next(tab);
474 ck_assert_msg(key != NULL, "Failed to get next key: %s", strerror(errno));
475 ck_assert_msg(strcmp(key, "foo") == 0,
476 "Expected key '%s', got '%s'", "foo", key);
477
478 key = pr_table_next(tab);
479 ck_assert_msg(key == NULL, "Expected no more keys, got '%s'", key);
480 }
481 END_TEST
482
483 START_TEST (table_remove_test) {
484 int ok;
485 const char *res;
486 char *str;
487 pr_table_t *tab;
488 size_t sz;
489
490 res = pr_table_remove(NULL, NULL, NULL);
491 ck_assert_msg(res == NULL, "Failed to handle null arguments");
492 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
493
494 tab = pr_table_alloc(p, 0);
495
496 res = pr_table_remove(tab, NULL, 0);
497 ck_assert_msg(res == NULL, "Failed to handle null key");
498 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
499
500 res = pr_table_remove(tab, "foo", &sz);
501 ck_assert_msg(res == NULL, "Failed to handle absent value");
502 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
503
504 ok = pr_table_add(tab, "foo", "bar baz", 0);
505 ck_assert_msg(ok == 0, "Failed to add key to table: %s", strerror(errno));
506
507 res = pr_table_remove(tab, "foo", &sz);
508 ck_assert_msg(res != NULL, "Failed to remove 'foo': %s", strerror(errno));
509 ck_assert_msg(sz == 8, "Expected value len of 8, got %lu", (unsigned long)sz);
510
511 str = pcalloc(p, sz);
512 memcpy(str, res, sz);
513
514 ck_assert_msg(strcmp(str, "bar baz") == 0,
515 "Expected value of '%s', got '%s'", "bar baz", str);
516
517 ok = pr_table_count(tab);
518 ck_assert_msg(ok == 0, "Expected table count of 0, got %d", ok);
519
520 res = pr_table_remove(tab, "foo", &sz);
521 ck_assert_msg(res == NULL, "Failed to handle absent value");
522 ck_assert_msg(errno == ENOENT, "Failed to set errno to ENOENT");
523
524 mark_point();
525 res = pr_table_kremove(NULL, NULL, 0, NULL);
526 ck_assert_msg(res == NULL, "Failed to handle null table");
527 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
528 strerror(errno), errno);
529
530 mark_point();
531 res = pr_table_kremove(tab, NULL, 0, NULL);
532 ck_assert_msg(res == NULL, "Failed to handle null key data");
533 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
534 strerror(errno), errno);
535 }
536 END_TEST
537
538 START_TEST (table_set_test) {
539 int res;
540 pr_table_t *tab;
541 const void *v;
542 char *str;
543 size_t sz;
544
545 res = pr_table_set(NULL, NULL, NULL, 0);
546 ck_assert_msg(res == -1, "Failed to handle null arguments");
547 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
548
549 tab = pr_table_alloc(p, 0);
550
551 res = pr_table_set(tab, NULL, NULL, 0);
552 ck_assert_msg(res == -1, "Failed to handle null key");
553 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
554
555 res = pr_table_set(tab, "foo", NULL, 1);
556 ck_assert_msg(res == -1, "Failed to handle null value (len 1)");
557 ck_assert_msg(errno == EINVAL, "Failed to handle null value (len 1)");
558
559 mark_point();
560 res = pr_table_set(tab, "foo", "bar", 1);
561 ck_assert_msg(res < 0, "Failed to handle empty table");
562 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
563 strerror(errno), errno);
564
565 res = pr_table_add(tab, "foo", "bar", 0);
566 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
567
568 res = pr_table_set(tab, "foo", "BAZ", 0);
569 ck_assert_msg(res == 0, "Failed to set 'foo' in table: %s", strerror(errno));
570
571 v = pr_table_get(tab, "foo", &sz);
572 ck_assert_msg(v != NULL, "Failed to retrieve 'foo' from table: %s",
573 strerror(errno));
574 ck_assert_msg(sz == 4, "Expected len 4, got %lu", (unsigned long)sz);
575
576 str = pcalloc(p, sz);
577 memcpy(str, v, sz);
578
579 ck_assert_msg(strcmp(str, "BAZ") == 0,
580 "Expected value of '%s', got '%s'", "BAZ", str);
581 }
582 END_TEST
583
584 START_TEST (table_do_test) {
585 int res;
586 pr_table_t *tab;
587
588 res = pr_table_do(NULL, NULL, NULL, 0);
589 ck_assert_msg(res == -1, "Failed to handle null arguments");
590 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
591
592 tab = pr_table_alloc(p, 0);
593
594 res = pr_table_do(tab, NULL, NULL, 0);
595 ck_assert_msg(res == -1, "Failed to handle null arguments");
596 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
597
598 res = pr_table_do(tab, do_cb, NULL, 0);
599 ck_assert_msg(res == 0, "Failed to handle empty table");
600
601 res = pr_table_add(tab, "foo", "bar", 0);
602 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
603
604 res = pr_table_add(tab, "bar", "baz", 0);
605 ck_assert_msg(res == 0, "Failed to add 'bar' to table: %s", strerror(errno));
606
607 res = pr_table_do(tab, do_cb, NULL, 0);
608 ck_assert_msg(res == -1, "Expected res %d, got %d", -1, res);
609 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
610 ck_assert_msg(b_val_count == 1, "Expected count %u, got %u", 1, b_val_count);
611
612 b_val_count = 0;
613 res = pr_table_do(tab, do_cb, NULL, PR_TABLE_DO_FL_ALL);
614 ck_assert_msg(res == 0, "Failed to do table: %s", strerror(errno));
615 ck_assert_msg(b_val_count == 2, "Expected count %u, got %u", 2, b_val_count);
616 }
617 END_TEST
618
619 START_TEST (table_do_with_remove_test) {
620 int res;
621 pr_table_t *tab;
622
623 tab = pr_table_alloc(p, 0);
624
625 res = pr_table_add(tab, "foo", "bar", 0);
626 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
627
628 res = pr_table_add(tab, "bar", "baz", 0);
629 ck_assert_msg(res == 0, "Failed to add 'bar' to table: %s", strerror(errno));
630
631 b_val_count = 0;
632 res = pr_table_do(tab, do_with_remove_cb, tab, PR_TABLE_DO_FL_ALL);
633 ck_assert_msg(res == 0, "Failed to do table: %s", strerror(errno));
634 ck_assert_msg(b_val_count == 2, "Expected count %u, got %u", 2, b_val_count);
635 }
636 END_TEST
637
638 START_TEST (table_ctl_test) {
639 int res;
640 pr_table_t *tab;
641 unsigned long flags = 0;
642 unsigned int max_ents = 0, nchains = 0;
643
644 res = pr_table_ctl(NULL, 0, NULL);
645 ck_assert_msg(res == -1, "Failed to handle null table");
646 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
647
648 tab = pr_table_alloc(p, 0);
649
650 mark_point();
651 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_ENT_INSERT, NULL);
652 ck_assert_msg(res == 0, "Failed to set entry insert callback: %s",
653 strerror(errno));
654
655 mark_point();
656 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_ENT_REMOVE, NULL);
657 ck_assert_msg(res == 0, "Failed to set entry removal callback: %s",
658 strerror(errno));
659
660 res = pr_table_add(tab, "foo", "bar", 0);
661 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
662
663 mark_point();
664 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_MAX_ENTS, 0);
665 ck_assert_msg(res < 0, "Failed to handle SET_MAX_ENTS smaller than table");
666 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
667 strerror(errno), errno);
668
669 res = pr_table_ctl(tab, 0, NULL);
670 ck_assert_msg(res == -1, "Failed to handle non-empty table");
671 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
672
673 res = pr_table_empty(tab);
674 ck_assert_msg(res == 0, "Failed to empty table: %s", strerror(errno));
675
676 res = pr_table_ctl(tab, 0, NULL);
677 ck_assert_msg(res == -1, "Failed to handle unknown ctl");
678 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
679
680 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_FLAGS, NULL);
681 ck_assert_msg(res == -1, "Failed to handle SET_FLAGS, null args");
682 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
683
684 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_FLAGS, &flags);
685 ck_assert_msg(res == 0, "Failed to handle SET_FLAGS: %s", strerror(errno));
686
687 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_NCHAINS, NULL);
688 ck_assert_msg(res == -1, "Failed to handle SET_NCHAINS, null args");
689 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
690
691 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_NCHAINS, &nchains);
692 ck_assert_msg(res == -1, "Failed to handle SET_NCHAINS, zero args");
693 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
694
695 nchains = 1;
696 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_NCHAINS, &nchains);
697 ck_assert_msg(res == 0, "Failed to handle SET_NCHAINS: %s", strerror(errno));
698
699 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_MAX_ENTS, &max_ents);
700 ck_assert_msg(res == -1, "Failed to handle SET_MAX_ENTS, zero args");
701 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
702
703 /* Add two entries, then try to set MAX_ENTS to one. We should get an
704 * EPERM back for that.
705 */
706 res = pr_table_add(tab, "foo", "bar", 0);
707 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
708
709 res = pr_table_add(tab, "baz", "quxx", 0);
710 ck_assert_msg(res == 0, "Failed to add 'baz' to table: %s", strerror(errno));
711
712 max_ents = 1;
713 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_MAX_ENTS, &max_ents);
714 ck_assert_msg(res == -1, "Failed to handle SET_MAX_ENTS on non-empty table");
715 ck_assert_msg(errno == EPERM, "Failed to set errno to EPERM");
716
717 /* Now empty the table, set the MAX_ENTS to one, then try add two entries. */
718
719 res = pr_table_empty(tab);
720 ck_assert_msg(res == 0, "Failed to empty table: %s", strerror(errno));
721
722 max_ents = 1;
723 res = pr_table_ctl(tab, PR_TABLE_CTL_SET_MAX_ENTS, &max_ents);
724 ck_assert_msg(res == 0, "Failed to handle SET_MAX_ENTS to %d: %s",
725 max_ents, strerror(errno));
726
727 res = pr_table_add(tab, "foo", "bar", 0);
728 ck_assert_msg(res == 0, "Failed to add 'foo' to table: %s", strerror(errno));
729
730 res = pr_table_add(tab, "baz", "quxx", 0);
731 ck_assert_msg(res == -1, "Added second entry unexpectedly");
732 ck_assert_msg(errno == ENOSPC,
733 "Failed to set errno to ENOSPC, received %d (%s)", errno, strerror(errno));
734 }
735 END_TEST
736
737 START_TEST (table_load_test) {
738 pr_table_t *tab = NULL;
739 float load;
740
741 load = pr_table_load(tab);
742 ck_assert_msg(load < 0, "Failed to handle NULL table argument");
743 ck_assert_msg(errno == EINVAL,
744 "Failed to set errno to EINVAL; received %d (%s)", errno, strerror(errno));
745
746 tab = pr_table_alloc(p, 0);
747 load = pr_table_load(tab);
748 ck_assert_msg(load >= 0.0, "Failed to calculate load properly; load = %0.3f",
749 load);
750 }
751 END_TEST
752
753 START_TEST (table_dump_test) {
754 pr_table_t *tab;
755
756 pr_table_dump(NULL, NULL);
757
758 tab = pr_table_alloc(p, 0);
759
760 pr_table_dump(NULL, tab);
761 pr_table_dump(table_dump, NULL);
762 }
763 END_TEST
764
765 START_TEST (table_pcalloc_test) {
766 void *res;
767 pr_table_t *tab;
768
769 res = pr_table_pcalloc(NULL, 0);
770 ck_assert_msg(res == NULL, "Failed to handle null arguments");
771 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
772
773 tab = pr_table_alloc(p, 0);
774
775 res = pr_table_pcalloc(tab, 0);
776 ck_assert_msg(res == NULL, "Failed to handle zero len argument");
777 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
778
779 res = pr_table_pcalloc(NULL, 1);
780 ck_assert_msg(res == NULL, "Failed to handle null table");
781 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
782
783 res = pr_table_pcalloc(tab, 2);
784 ck_assert_msg(res != NULL, "Failed to allocate len 2 from table: %s",
785 strerror(errno));
786 }
787 END_TEST
788
789 Suite *tests_get_table_suite(void) {
790 Suite *suite;
791 TCase *testcase;
792
793 suite = suite_create("table");
794 testcase = tcase_create("base");
795
796 tcase_add_checked_fixture(testcase, set_up, tear_down);
797
798 tcase_add_test(testcase, table_alloc_test);
799 tcase_add_test(testcase, table_nalloc_test);
800 tcase_add_test(testcase, table_add_test);
801 tcase_add_test(testcase, table_add_dup_test);
802 tcase_add_test(testcase, table_count_test);
803 tcase_add_test(testcase, table_exists_test);
804 tcase_add_test(testcase, table_empty_test);
805 tcase_add_test(testcase, table_free_test);
806 tcase_add_test(testcase, table_get_test);
807 tcase_add_test(testcase, table_get_use_cache_test);
808 tcase_add_test(testcase, table_next_test);
809 tcase_add_test(testcase, table_rewind_test);
810 tcase_add_test(testcase, table_remove_test);
811 tcase_add_test(testcase, table_set_test);
812 tcase_add_test(testcase, table_do_test);
813 tcase_add_test(testcase, table_do_with_remove_test);
814 tcase_add_test(testcase, table_ctl_test);
815 tcase_add_test(testcase, table_load_test);
816 tcase_add_test(testcase, table_dump_test);
817 tcase_add_test(testcase, table_pcalloc_test);
818
819 suite_add_tcase(suite, testcase);
820 return suite;
821 }