Debian Codegraph @

tests/api/array.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2021 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 /* Array API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = make_sub_pool(NULL);
34 }
35 }
36
37 static void tear_down(void) {
38 if (p != NULL) {
39 destroy_pool(p);
40 p = NULL;
41 }
42 }
43
44 START_TEST (make_array_test) {
45 array_header *list;
46
47 list = make_array(NULL, 0, 0);
48 ck_assert_msg(list == NULL, "Failed to handle null arguments");
49 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
50
51 list = make_array(p, 0, 0);
52 ck_assert_msg(list == NULL, "Failed to handle null arguments");
53 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
54
55 list = make_array(p, 1, 0);
56 ck_assert_msg(list == NULL, "Failed to handle null arguments");
57 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
58
59 list = make_array(p, 0, 1);
60 ck_assert_msg(list != NULL, "Failed to create an array_header: %s",
61 strerror(errno));
62 ck_assert_msg(list->nalloc == 1, "Expected list->nalloc of %u, got %d",
63 1, list->nalloc);
64
65 list = make_array(p, 3, 1);
66 ck_assert_msg(list != NULL, "Failed to create an array_header: %s",
67 strerror(errno));
68
69 ck_assert_msg(list->pool == p, "List pool doesn't given pool; "
70 "expected %p, got %p", p, list->pool);
71 ck_assert_msg(list->elts != NULL, "Expected non-null elements pointer");
72 ck_assert_msg(list->nalloc == 3, "Expected list->nalloc of %u, got %d",
73 3, list->nalloc);
74 ck_assert_msg(list->nelts == 0, "Expected list->nelts of %u, got %d",
75 0, list->nelts);
76 ck_assert_msg(list->elt_size == 1, "Expect list element size of %u, got %lu",
77 1, (unsigned long)list->elt_size);
78 }
79 END_TEST
80
81 START_TEST (push_array_test) {
82 array_header *list;
83 void *res;
84
85 res = push_array(NULL);
86 ck_assert_msg(res == NULL, "Failed to handle null arguments");
87 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL for null args");
88
89 list = make_array(p, 0, 1);
90
91 res = push_array(list);
92 ck_assert_msg(res != NULL, "Failed to allocate new list element");
93 ck_assert_msg(list->nalloc == 1, "Incremented alloc elements needlessly ("
94 "expected %u, got %d)", 1, list->nalloc);
95 ck_assert_msg(list->nelts == 1, "Failed to increment element count "
96 "(expected %u, got %d)", 1, list->nelts);
97
98 res = push_array(list);
99 ck_assert_msg(res != NULL, "Failed to allocate new list element");
100 ck_assert_msg(list->nalloc == 2, "Incremented alloc elements needlessly "
101 "(expected %u, got %d)", 2, list->nalloc);
102 ck_assert_msg(list->nelts == 2, "Failed to increment element count "
103 "(expected %u, got %d)", 2, list->nelts);
104
105 res = push_array(list);
106 ck_assert_msg(res != NULL, "Failed to allocate new list element");
107 ck_assert_msg(list->nalloc == 4, "Incremented alloc elements needlessly "
108 "(expected %u, got %d)", 4, list->nalloc);
109 ck_assert_msg(list->nelts == 3, "Failed to increment element count "
110 "(expected %u, got %d)", 3, list->nelts);
111
112 res = push_array(list);
113 ck_assert_msg(res != NULL, "Failed to allocate new list element");
114 ck_assert_msg(list->nalloc == 4, "Incremented alloc elements needlessly "
115 "(expected %u, got %d)", 4, list->nalloc);
116 ck_assert_msg(list->nelts == 4, "Failed to increment element count "
117 "(expected %u, got %d)", 4, list->nelts);
118 }
119 END_TEST
120
121 START_TEST (array_cat_test) {
122 array_header *src, *dst;
123
124 mark_point();
125
126 /* This should not segfault. */
127 array_cat(NULL, NULL);
128
129 dst = make_array(p, 0, 1);
130 mark_point();
131 array_cat(dst, NULL);
132
133 src = make_array(p, 0, 1);
134 mark_point();
135 array_cat(NULL, src);
136
137 mark_point();
138 array_cat(dst, src);
139
140 ck_assert_msg(dst->nalloc == 1, "Wrong dst alloc count (expected %u, got %d)",
141 1, dst->nalloc);
142 ck_assert_msg(dst->nelts == 0, "Wrong dst item count (expected %u, got %d)",
143 0, dst->nelts);
144
145 push_array(src);
146 array_cat(dst, src);
147
148 ck_assert_msg(dst->nalloc == 1, "Wrong dst alloc count (expected %u, got %d)",
149 1, dst->nalloc);
150 ck_assert_msg(dst->nelts == 1, "Wrong dst item count (expected %u, got %d)",
151 1, dst->nelts);
152
153 push_array(src);
154 push_array(src);
155 push_array(src);
156 array_cat(dst, src);
157
158 ck_assert_msg(dst->nalloc == 8, "Wrong dst alloc count (expected %u, got %d)",
159 8, dst->nalloc);
160 ck_assert_msg(dst->nelts == 5, "Wrong dst item count (expected %u, got %d)",
161 5, dst->nelts);
162 }
163 END_TEST
164
165 START_TEST (array_cat2_test) {
166 array_header *src, *dst;
167 int res;
168
169 mark_point();
170
171 res = array_cat2(NULL, NULL);
172 ck_assert_msg(res < 0, "Failed to handle null arguments");
173 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL, got '%s' (%d)",
174 strerror(errno), errno);
175
176 dst = make_array(p, 0, 1);
177 mark_point();
178 res = array_cat2(dst, NULL);
179 ck_assert_msg(res < 0, "Failed to handle null arguments");
180 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL, got '%s' (%d)",
181 strerror(errno), errno);
182
183 src = make_array(p, 0, 1);
184 mark_point();
185 res = array_cat2(NULL, src);
186 ck_assert_msg(res < 0, "Failed to handle null arguments");
187 ck_assert_msg(errno == EINVAL, "Expected errno EINVAL, got '%s' (%d)",
188 strerror(errno), errno);
189
190 mark_point();
191 res = array_cat2(dst, src);
192 ck_assert_msg(res == 0, "Failed to concatenate arrays: %s", strerror(errno));
193
194 ck_assert_msg(dst->nalloc == 1, "Wrong dst alloc count (expected %u, got %d)",
195 1, dst->nalloc);
196 ck_assert_msg(dst->nelts == 0, "Wrong dst item count (expected %u, got %d)",
197 0, dst->nelts);
198
199 push_array(src);
200 res = array_cat2(dst, src);
201 ck_assert_msg(res == 0, "Failed to concatenate arrays: %s", strerror(errno));
202
203 ck_assert_msg(dst->nalloc == 1, "Wrong dst alloc count (expected %u, got %d)",
204 1, dst->nalloc);
205 ck_assert_msg(dst->nelts == 1, "Wrong dst item count (expected %u, got %d)",
206 1, dst->nelts);
207
208 push_array(src);
209 push_array(src);
210 push_array(src);
211 res = array_cat2(dst, src);
212 ck_assert_msg(res == 0, "Failed to concatenate arrays: %s", strerror(errno));
213
214 ck_assert_msg(dst->nalloc == 8, "Wrong dst alloc count (expected %u, got %d)",
215 8, dst->nalloc);
216 ck_assert_msg(dst->nelts == 5, "Wrong dst item count (expected %u, got %d)",
217 5, dst->nelts);
218 }
219 END_TEST
220
221 START_TEST (clear_array_test) {
222 array_header *list;
223
224 mark_point();
225
226 /* This should not segfault. */
227 clear_array(NULL);
228
229 list = make_array(p, 0, 1);
230 push_array(list);
231 push_array(list);
232
233 ck_assert_msg(list->nalloc == 2, "Wrong list alloc count (expected %u, got %d)",
234 2, list->nalloc);
235 ck_assert_msg(list->nelts == 2, "Wrong list item count (expected %u, got %d)",
236 2, list->nelts);
237
238 clear_array(list);
239
240 ck_assert_msg(list->nalloc == 2, "Wrong list alloc count (expected %u, got %d)",
241 2, list->nalloc);
242 ck_assert_msg(list->nelts == 0, "Wrong list item count (expected %u, got %d)",
243 0, list->nelts);
244 }
245 END_TEST
246
247 START_TEST (copy_array_test) {
248 array_header *list, *src;
249
250 list = copy_array(NULL, NULL);
251 ck_assert_msg(list == NULL, "Failed to handle null arguments");
252 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
253
254 src = make_array(p, 0, 1);
255
256 list = copy_array(NULL, src);
257 ck_assert_msg(list == NULL, "Failed to handle null arguments");
258 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
259
260 list = copy_array(p, NULL);
261 ck_assert_msg(list == NULL, "Failed to handle null arguments");
262 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
263
264 push_array(src);
265
266 list = copy_array(p, src);
267 ck_assert_msg(list != NULL, "Failed to copy list");
268 ck_assert_msg(list->elt_size == src->elt_size,
269 "Copy item size wrong (expected %lu, got %lu)",
270 (unsigned long)src->elt_size, (unsigned long)list->elt_size);
271 ck_assert_msg(list->nalloc == src->nalloc,
272 "Copy nalloc wrong (expected %d, got %d)", src->nalloc, list->nalloc);
273 ck_assert_msg(list->nelts == src->nelts,
274 "Copy nelts wrong (expected %d, got %d)", src->nelts, list->nelts);
275 }
276 END_TEST
277
278 START_TEST (copy_array_str_test) {
279 array_header *list, *src;
280 char *elt, **elts;
281
282 src = make_array(p, 0, sizeof(char *));
283
284 list = copy_array_str(NULL, NULL);
285 ck_assert_msg(list == NULL, "Failed to handle null arguments");
286 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
287
288 list = copy_array_str(NULL, src);
289 ck_assert_msg(list == NULL, "Failed to handle null arguments");
290 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
291
292 list = copy_array_str(p, NULL);
293 ck_assert_msg(list == NULL, "Failed to handle null arguments");
294 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
295
296 *((char **) push_array(src)) = pstrdup(p, "foo");
297 *((char **) push_array(src)) = pstrdup(p, "bar");
298
299 list = copy_array_str(p, src);
300
301 ck_assert_msg(list->elt_size == src->elt_size,
302 "Copy item size wrong (expected %lu, got %lu)",
303 (unsigned long)src->elt_size, (unsigned long)list->elt_size);
304 ck_assert_msg(list->nalloc == src->nalloc,
305 "Copy nalloc wrong (expected %d, got %d)", src->nalloc, list->nalloc);
306 ck_assert_msg(list->nelts == src->nelts,
307 "Copy nelts wrong (expected %d, got %d)", src->nelts, list->nelts);
308
309 elts = list->elts;
310
311 elt = elts[0];
312 ck_assert_msg(strcmp(elt, "foo") == 0,
313 "Improper copy (expected '%s', got '%s')", "foo", elt);
314
315 elt = elts[1];
316 ck_assert_msg(strcmp(elt, "bar") == 0,
317 "Improper copy (expected '%s', got '%s')", "bar", elt);
318 }
319 END_TEST
320
321 START_TEST (copy_array_hdr_test) {
322 array_header *list, *src;
323 int elt, *elts;
324
325 src = make_array(p, 0, sizeof(int));
326
327 list = copy_array_hdr(NULL, NULL);
328 ck_assert_msg(list == NULL, "Failed to handle null arguments");
329 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
330
331 list = copy_array_hdr(NULL, src);
332 ck_assert_msg(list == NULL, "Failed to handle null arguments");
333 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
334
335 list = copy_array_hdr(p, NULL);
336 ck_assert_msg(list == NULL, "Failed to handle null arguments");
337 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
338
339 *((int *) push_array(src)) = -1;
340 *((int *) push_array(src)) = 2;
341 *((int *) push_array(src)) = 2476;
342
343 list = copy_array_hdr(p, src);
344
345 ck_assert_msg(list->elt_size == src->elt_size,
346 "Copy item size wrong (expected %lu, got %lu)",
347 (unsigned long)src->elt_size, (unsigned long)list->elt_size);
348 ck_assert_msg(list->elts == src->elts,
349 "Copy elts wrong (expected %p, got %p)", src->elts, list->elts);
350 ck_assert_msg(list->nelts == src->nelts,
351 "Copy nelts wrong (expected %d, got %d)", src->nelts, list->nelts);
352
353 ck_assert_msg(list->nalloc != src->nalloc,
354 "Copy nalloc wrong (expected %d, got %d)", src->nalloc, list->nalloc);
355 ck_assert_msg(list->nalloc == 3,
356 "Copy nalloc wrong (expected %d, got %d)", 3, list->nalloc);
357
358 elts = list->elts;
359
360 elt = elts[0];
361 ck_assert_msg(elt == -1, "Improper copy (expected %d, got %d)", -1, elt);
362
363 elt = elts[1];
364 ck_assert_msg(elt == 2, "Improper copy (expected %d, got %d)", 2, elt);
365
366 elt = elts[2];
367 ck_assert_msg(elt == 2476, "Improper copy (expected %d, got %d)", 2476, elt);
368 }
369 END_TEST
370
371 START_TEST (append_arrays_test) {
372 array_header *a, *b, *res;
373 int elt, *elts;
374
375 mark_point();
376 a = make_array(p, 0, sizeof(int));
377 b = make_array(p, 0, sizeof(int));
378
379 mark_point();
380 res = append_arrays(NULL, NULL, NULL);
381 ck_assert_msg(res == NULL, "Failed to handle null arguments");
382 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
383
384 mark_point();
385 res = append_arrays(p, NULL, NULL);
386 ck_assert_msg(res == NULL, "Failed to handle null arguments");
387 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
388
389 mark_point();
390 res = append_arrays(NULL, a, NULL);
391 ck_assert_msg(res == NULL, "Failed to handle null arguments");
392 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
393
394 mark_point();
395 res = append_arrays(NULL, NULL, b);
396 ck_assert_msg(res == NULL, "Failed to handle null arguments");
397 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
398
399 mark_point();
400 res = append_arrays(p, a, NULL);
401 ck_assert_msg(res == NULL, "Failed to handle null arguments");
402 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
403
404 mark_point();
405 res = append_arrays(p, NULL, b);
406 ck_assert_msg(res == NULL, "Failed to handle null arguments");
407 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
408
409 mark_point();
410 res = append_arrays(NULL, a, b);
411 ck_assert_msg(res == NULL, "Failed to handle null arguments");
412 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
413
414 *((int *) push_array(a)) = -1;
415 *((int *) push_array(a)) = 2;
416 *((int *) push_array(b)) = 2476;
417 *((int *) push_array(b)) = 4762;
418 *((int *) push_array(b)) = 7642;
419
420 mark_point();
421 res = append_arrays(p, a, b);
422
423 ck_assert_msg(res->elt_size == a->elt_size,
424 "Append item size wrong (expected %lu, got %lu)",
425 (unsigned long)a->elt_size, (unsigned long)res->elt_size);
426 ck_assert_msg(res->nelts == 5,
427 "Append nelts wrong (expected %d, got %d)", 5, res->nelts);
428
429 ck_assert_msg(res->nalloc == 8,
430 "Append nalloc wrong (expected %d, got %d)", 8, res->nalloc);
431
432 elts = res->elts;
433
434 elt = elts[0];
435 ck_assert_msg(elt == -1, "Improper append (expected %d, got %d)", -1, elt);
436
437 elt = elts[1];
438 ck_assert_msg(elt == 2, "Improper append (expected %d, got %d)", 2, elt);
439
440 elt = elts[2];
441 ck_assert_msg(elt == 2476, "Improper append (expected %d, got %d)", 2476, elt);
442
443 elt = elts[3];
444 ck_assert_msg(elt == 4762, "Improper append (expected %d, got %d)", 4762, elt);
445
446 elt = elts[4];
447 ck_assert_msg(elt == 7642, "Improper append (expected %d, got %d)", 7642, elt);
448 }
449 END_TEST
450
451 Suite *tests_get_array_suite(void) {
452 Suite *suite;
453 TCase *testcase;
454
455 suite = suite_create("array");
456
457 testcase = tcase_create("base");
458
459 tcase_add_checked_fixture(testcase, set_up, tear_down);
460
461 tcase_add_test(testcase, make_array_test);
462 tcase_add_test(testcase, push_array_test);
463 tcase_add_test(testcase, array_cat_test);
464 tcase_add_test(testcase, array_cat2_test);
465 tcase_add_test(testcase, clear_array_test);
466 tcase_add_test(testcase, copy_array_test);
467 tcase_add_test(testcase, copy_array_str_test);
468 tcase_add_test(testcase, copy_array_hdr_test);
469 tcase_add_test(testcase, append_arrays_test);
470
471 suite_add_tcase(suite, testcase);
472
473 return suite;
474 }