1 /*
2 * SysDB - t/unit/utils/strbuf_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "utils/strbuf.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 /*
34 * private data types
35 */
37 static sdb_strbuf_t *buf;
39 static void
40 setup(void)
41 {
42 buf = sdb_strbuf_create(0);
43 fail_unless(buf != NULL,
44 "sdb_strbuf_create() = NULL; expected strbuf object");
45 } /* setup */
47 static void
48 teardown(void)
49 {
50 sdb_strbuf_destroy(buf);
51 buf = NULL;
52 } /* teardown */
54 /*
55 * tests
56 */
58 START_TEST(test_null)
59 {
60 sdb_strbuf_t *b = NULL;
61 va_list ap;
63 size_t check;
65 /* check that methods don't crash */
66 sdb_strbuf_destroy(b);
67 sdb_strbuf_skip(b, 0, 0);
68 sdb_strbuf_skip(b, 0, 10);
69 sdb_strbuf_skip(b, 10, 10);
70 sdb_strbuf_clear(b);
72 /* check that methods return an error */
73 fail_unless(sdb_strbuf_vappend(b, "test", ap) < 0,
74 "sdb_strbuf_vappend(NULL) didn't report failure");
75 fail_unless(sdb_strbuf_append(b, "test") < 0,
76 "sdb_strbuf_append(NULL) didn't report failure");
77 fail_unless(sdb_strbuf_vsprintf(b, "test", ap) < 0,
78 "sdb_strbuf_vsprintf(NULL) didn't report failure");
79 fail_unless(sdb_strbuf_sprintf(b, "test") < 0,
80 "sdb_strbuf_sprintf(NULL) didn't report failure");
81 fail_unless(sdb_strbuf_memcpy(b, "test", 4) < 0,
82 "sdb_strbuf_memcpy(NULL) didn't report failure");
83 fail_unless(sdb_strbuf_memappend(b, "test", 4) < 0,
84 "sdb_strbuf_memappend(NULL) didn't report failure");
85 fail_unless(sdb_strbuf_read(b, 0, 32) < 0,
86 "sdb_strbuf_read(NULL) didn't report failure");
87 fail_unless(sdb_strbuf_chomp(b) < 0,
88 "sdb_strbuf_chomp(NULL) didn't report failure");
90 /* check that methods return no used space */
91 check = sdb_strbuf_len(b);
92 fail_unless(check == 0,
93 "sdb_strbuf_len(NULL) = %zi; expected: 0", check);
94 check = sdb_strbuf_cap(b);
95 fail_unless(check == 0,
96 "sdb_strbuf_cap(NULL) = %zi; expected: 0", check);
97 }
98 END_TEST
100 START_TEST(test_empty)
101 {
102 sdb_strbuf_t *b = sdb_strbuf_create(0);
103 const char *data;
104 size_t check;
106 /* check that methods don't crash */
107 sdb_strbuf_skip(b, 1, 1);
108 sdb_strbuf_clear(b);
109 sdb_strbuf_chomp(b);
111 data = sdb_strbuf_string(b);
112 fail_unless(data && (*data == '\0'),
113 "sdb_strbuf_string(<empty>) = '%s'; expected: ''", data);
114 check = sdb_strbuf_len(b);
115 fail_unless(check == 0,
116 "sdb_strbuf_len(<empty>) = %zu; expected: 0", check);
117 check = sdb_strbuf_cap(b);
118 fail_unless(check == 0,
119 "sdb_strbuf_cap(<empty>) = %zu; expected: 0", check);
121 sdb_strbuf_destroy(b);
122 }
123 END_TEST
125 START_TEST(test_create)
126 {
127 sdb_strbuf_t *s;
128 size_t check;
130 s = sdb_strbuf_create(0);
131 fail_unless(s != NULL,
132 "sdb_strbuf_create() = NULL; expected strbuf object");
133 check = sdb_strbuf_len(s);
134 fail_unless(check == 0,
135 "sdb_strbuf_create() created buffer with len = %zu; "
136 "expected: 0", check);
137 check = sdb_strbuf_cap(s);
138 fail_unless(check == 0,
139 "sdb_strbuf_create() created buffer with cap = %zu; "
140 "expected: 0", check);
141 sdb_strbuf_destroy(s);
143 s = sdb_strbuf_create(128);
144 fail_unless(s != NULL,
145 "sdb_strbuf_create() = NULL; expected strbuf object");
146 check = sdb_strbuf_len(s);
147 /* len still has to be 0 -- there's no content */
148 fail_unless(check == 0,
149 "sdb_strbuf_create() created buffer with len = %zu; "
150 "expected: 0", check);
151 check = sdb_strbuf_cap(s);
152 fail_unless(check == 128,
153 "sdb_strbuf_create() created buffer with cap = %zu; "
154 "expected: 128", check);
155 sdb_strbuf_destroy(s);
156 }
157 END_TEST
159 START_TEST(test_append)
160 {
161 ssize_t n;
162 size_t len, total = 0;
163 const char *test;
165 struct {
166 const char *input;
167 const char *result;
168 } golden_data[] = {
169 { "1234567890", "1234567890" },
170 { "ABCDE", "1234567890ABCDE" },
171 { "", "1234567890ABCDE" },
172 { "-", "1234567890ABCDE-" },
173 /* when adding anything to this array, the last check has to be
174 * updated accordingly */
175 };
177 size_t i;
179 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
180 n = sdb_strbuf_append(buf, "%s", golden_data[i].input);
181 fail_unless((size_t)n == strlen(golden_data[i].input),
182 "sdb_strbuf_append() appended %zi bytes; expected: %zu",
183 n, strlen(golden_data[i].input));
184 total += n;
185 len = sdb_strbuf_len(buf);
186 fail_unless(len == total,
187 "sdb_strbuf_append() left behind buffer with len = %zu; "
188 "expected: %zu", len, total);
190 test = sdb_strbuf_string(buf);
191 fail_unless(test[len] == '\0',
192 "sdb_strbuf_append() did not nil-terminate the string");
194 test = sdb_strbuf_string(buf);
195 fail_unless(!strcmp(test, golden_data[i].result),
196 "sdb_strbuf_append() did not correctly concatenate "
197 "the input; got: %s; expected: %s",
198 test, golden_data[i].result);
199 }
201 n = sdb_strbuf_append(buf, "%zu; %5.4f", (size_t)42, 4.2);
202 fail_unless(n == 10,
203 "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
204 total += n;
205 len = sdb_strbuf_len(buf);
206 fail_unless(len == total,
207 "sdb_strbuf_append() left behind buffer with len = %zu; "
208 "expected: %zu", len, total);
210 test = sdb_strbuf_string(buf);
211 fail_unless(test[len] == '\0',
212 "sdb_strbuf_append() did not nil-terminate the string");
213 fail_unless(!strcmp(test, "1234567890ABCDE-42; 4.2000"),
214 "sdb_strbuf_append() did not correctly concatenate the input; "
215 "got: %s; expected: 1234567890ABCDE-42; 4.2000", test);
216 }
217 END_TEST
219 START_TEST(test_sprintf)
220 {
221 ssize_t n;
222 size_t check;
223 const char *test;
225 const char *golden_data[] = {
226 "1234567890",
227 "ABCDE",
228 "",
229 "-",
230 "123456789012345678901234567890",
231 "--",
232 };
234 size_t i;
236 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
237 n = sdb_strbuf_sprintf(buf, "%s", golden_data[i]);
238 fail_unless((size_t)n == strlen(golden_data[i]),
239 "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zu",
240 n, strlen(golden_data[i]));
241 check = sdb_strbuf_len(buf);
242 fail_unless(check == (size_t)n,
243 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
244 "expected: %zi", check, n);
246 test = sdb_strbuf_string(buf);
247 fail_unless(test[check] == '\0',
248 "sdb_strbuf_sprintf() did not nil-terminate the string");
249 fail_unless(!strcmp(test, golden_data[i]),
250 "sdb_strbuf_sprintf() did not format string correctly; "
251 "got: %s; expected: %s", test, golden_data[i]);
253 check = sdb_strbuf_cap(buf);
254 if (n) /* 3 times the current len is the threshold for shrinking */
255 fail_unless(((size_t)n < check) && (check < (size_t)n * 3),
256 "sdb_strbuf_sprintf() did not resize the buffer "
257 "correctly (got size %zu; expected %zi < <size> < %d)",
258 check, n, n / 3);
259 }
261 n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", (size_t)42, 4.2);
262 fail_unless(n == 10,
263 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
264 check = sdb_strbuf_len(buf);
265 fail_unless(check == 10,
266 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
267 "expected: 10", check);
269 test = sdb_strbuf_string(buf);
270 fail_unless(test[check] == '\0',
271 "sdb_strbuf_sprintf() did not nil-terminate the string");
272 fail_unless(!strcmp(test, "42; 4.2000"),
273 "sdb_strbuf_sprintf() did not format string correctly; "
274 "got: %s; expected: 42; 4.2000", test);
275 }
276 END_TEST
278 START_TEST(test_incremental)
279 {
280 const char *data;
282 ssize_t n;
283 size_t i, check;
285 sdb_strbuf_destroy(buf);
286 buf = sdb_strbuf_create(1024);
288 /* fill buffer one by one; leave room for nul-byte */
289 for (i = 0; i < 1023; ++i) {
290 n = sdb_strbuf_append(buf, ".");
291 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
292 }
294 check = sdb_strbuf_cap(buf);
295 fail_unless(check == 1024,
296 "sdb_strbuf_append() resizes the buffer too early "
297 "(got size %zu; expected: 1024)", check);
299 /* write another byte; this has to trigger a resize */
300 n = sdb_strbuf_append(buf, ".");
301 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
303 check = sdb_strbuf_cap(buf);
304 /* the exact size doesn't matter but is an implementation detail */
305 fail_unless(check > 1024,
306 "sdb_strbuf_append() did not resize the buffer");
308 /* write more bytes; this should trigger at least one more resize but
309 * that's an implementation detail as well */
310 for (i = 0; i < 1024; ++i) {
311 n = sdb_strbuf_append(buf, ".");
312 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
313 }
315 n = (ssize_t)sdb_strbuf_len(buf);
316 fail_unless(n == 2048, "sdb_strbuf_len() = %zi; expectd: 2048", n);
318 data = sdb_strbuf_string(buf);
319 for (i = 0; i < 2048; ++i)
320 fail_unless(data[i] == '.',
321 "After sdb_strbuf_append(), found character %x "
322 "at position %zi; expected %x (.)",
323 (int)data[i], i, '.');
324 fail_unless(data[i] == '\0',
325 "After sdb_strbuf_append(), found character %x at end of string; "
326 "expected '\\0'", (int)data[i]);
327 }
328 END_TEST
330 /* used by test_memcpy and test_memappend */
331 static struct {
332 const char *input;
333 size_t size;
334 } mem_golden_data[] = {
335 { "abc\0\x10\x42", 6 },
336 { "\0\1\2\3\4", 5 },
337 { "\n\n\0\n\n", 5 },
338 { "", 0 },
339 };
341 START_TEST(test_memcpy)
342 {
343 size_t i;
345 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
346 ssize_t n;
347 const char *check;
349 n = sdb_strbuf_memcpy(buf, mem_golden_data[i].input,
350 mem_golden_data[i].size);
351 fail_unless(n >= 0,
352 "sdb_strbuf_memcpy() = %zi; expected: >=0", n);
353 fail_unless((size_t)n == mem_golden_data[i].size,
354 "sdb_strbuf_memcpy() = %zi; expected: %zu",
355 n, mem_golden_data[i].size);
357 n = (ssize_t)sdb_strbuf_len(buf);
358 fail_unless((size_t)n == mem_golden_data[i].size,
359 "sdb_strbuf_len() = %zu (after memcpy); expected: %zu",
360 n, mem_golden_data[i].size);
362 check = sdb_strbuf_string(buf);
363 fail_unless(check != NULL,
364 "sdb_strbuf_string() = NULL (after memcpy); expected: data");
365 fail_unless(check[mem_golden_data[i].size] == '\0',
366 "sdb_strbuf_memcpy() did not nil-terminate the data");
367 fail_unless(!memcmp(check, mem_golden_data[i].input,
368 mem_golden_data[i].size),
369 "sdb_strbuf_memcpy() did not set the buffer correctly");
371 n = (ssize_t)sdb_strbuf_cap(buf);
372 fail_unless((size_t)n > mem_golden_data[i].size,
373 "sdb_strbuf_memcpy() did not resize the buffer correctly "
374 "(got size %zi; expected: > %zu)", n,
375 mem_golden_data[i].size);
376 }
377 }
378 END_TEST
380 START_TEST(test_memappend)
381 {
382 size_t i;
384 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
385 ssize_t n;
386 const char *check;
388 size_t total, j;
390 n = sdb_strbuf_memappend(buf, mem_golden_data[i].input,
391 mem_golden_data[i].size);
392 fail_unless(n >= 0,
393 "sdb_strbuf_memappend() = %zi; expected: >=0", n);
394 fail_unless((size_t)n == mem_golden_data[i].size,
395 "sdb_strbuf_memappend() = %zi; expected: %zu",
396 n, mem_golden_data[i].size);
398 check = sdb_strbuf_string(buf);
399 fail_unless(check != NULL,
400 "sdb_strbuf_string() = NULL (after memappend); "
401 "expected: data");
403 n = (ssize_t)sdb_strbuf_len(buf);
404 total = 0;
405 for (j = 0; j <= i; ++j) {
406 fail_unless(total + mem_golden_data[j].size <= (size_t)n,
407 "sdb_strbuf_len() = %zu (after memappend); "
408 "expected: >=%zu", n, total + mem_golden_data[j].size);
410 fail_unless(!memcmp(check + total, mem_golden_data[j].input,
411 mem_golden_data[j].size),
412 "sdb_strbuf_memappend() did not "
413 "set the buffer correctly");
414 total += mem_golden_data[j].size;
415 }
416 fail_unless((size_t)n == total,
417 "sdb_strbuf_len() = %zu (after memappend); expected: %zu",
418 n, total);
420 fail_unless(check[total] == '\0',
421 "sdb_strbuf_memappend() did not nil-terminate the data");
423 n = (ssize_t)sdb_strbuf_cap(buf);
424 fail_unless((size_t)n > total,
425 "sdb_strbuf_memappend() did not resize the buffer correctly "
426 "(got size %zi; expected: > %zu)", n, total);
427 }
428 }
429 END_TEST
431 START_TEST(test_chomp)
432 {
433 struct {
434 const char *input;
435 ssize_t expected;
436 const char *expected_string;
437 } golden_data[] = {
438 { NULL, 0, "" },
439 { "\n", 1, "" },
440 { "\n\n", 2, "" },
441 { "12345\n\n\n", 3, "12345" },
442 { "abcd", 0, "abcd" },
443 };
445 size_t i;
447 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
448 ssize_t n;
449 const char *check;
451 if (golden_data[i].input)
452 sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
454 /* empty buffer */
455 n = sdb_strbuf_chomp(buf);
456 fail_unless(n == golden_data[i].expected,
457 "sdb_strbuf_chomp() = %zi; expected: %zi", n,
458 golden_data[i].expected);
460 check = sdb_strbuf_string(buf);
461 fail_unless(!strcmp(check, golden_data[i].expected_string),
462 "sdb_strbuf_chomp() did not correctly remove newlines; "
463 "got string '%s'; expected: '%s'", check,
464 golden_data[i].expected_string);
465 }
466 }
467 END_TEST
469 START_TEST(test_skip)
470 {
471 const char *input = "1234567890";
472 struct {
473 size_t offset;
474 size_t n;
475 const char *expected;
476 size_t expected_len;
477 } golden_data[] = {
478 { 0, 0, "1234567890", 10 },
479 { 0, 1, "234567890", 9 },
480 { 0, 2, "34567890", 8 },
481 { 0, 9, "0", 1 },
482 { 0, 10, "", 0 },
483 { 0, 11, "", 0 },
484 { 0, 100, "", 0 },
485 { 1, 0, "1234567890", 10 },
486 { 1, 1, "134567890", 9 },
487 { 1, 2, "14567890", 8 },
488 { 2, 0, "1234567890", 10 },
489 { 2, 1, "124567890", 9 },
490 { 2, 2, "12567890", 8 },
491 { 2, 3, "1267890", 7 },
492 { 2, 4, "127890", 6 },
493 { 2, 5, "12890", 5 },
494 { 2, 6, "1290", 4 },
495 { 2, 7, "120", 3 },
496 { 2, 8, "12", 2 },
497 { 2, 9, "12", 2 },
498 { 2, 10, "12", 2 },
499 { 8, 1, "123456780", 9 },
500 { 8, 2, "12345678", 8 },
501 { 8, 3, "12345678", 8 },
502 { 9, 1, "123456789", 9 },
503 { 9, 2, "123456789", 9 },
504 { 10, 1, "1234567890", 10 },
505 { 10, 2, "1234567890", 10 },
506 };
508 size_t i;
510 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
511 const char *check;
512 size_t n;
514 sdb_strbuf_sprintf(buf, "%s", input);
515 sdb_strbuf_skip(buf, golden_data[i].offset,
516 golden_data[i].n);
518 n = sdb_strbuf_len(buf);
519 fail_unless(n == golden_data[i].expected_len,
520 "sdb_strbuf_len() = %zu (after skip); expected: %zu",
521 n, golden_data[i].expected_len);
523 check = sdb_strbuf_string(buf);
524 fail_unless(!!check,
525 "sdb_strbuf_string() = NULL (after skip); expected: string");
527 fail_unless(check[n] == '\0',
528 "sdb_strbuf_skip() did not nil-terminate the string");
530 fail_unless(! strcmp(golden_data[i].expected, check),
531 "sdb_strbuf_skip('%s', %zu) did not skip correctly; "
532 "got string '%s'; expected: '%s'", input,
533 golden_data[i].n, check, golden_data[i].expected);
534 }
535 }
536 END_TEST
538 START_TEST(test_clear)
539 {
540 const char *data;
541 size_t len;
543 sdb_strbuf_append(buf, "abc");
544 len = sdb_strbuf_len(buf);
545 fail_unless(len != 0,
546 "sdb_strbuf_len() = %zu; expected: != 0", len);
548 sdb_strbuf_clear(buf);
549 len = sdb_strbuf_len(buf);
550 fail_unless(len == 0,
551 "sdb_strbuf_len() = %zu (after clear); expected: 0", len);
553 data = sdb_strbuf_string(buf);
554 fail_unless(*data == '\0',
555 "sdb_strbuf_string() = '%s' (after clear); expected: ''", data);
556 }
557 END_TEST
559 START_TEST(test_string)
560 {
561 struct {
562 const char *input;
563 const char *expected;
564 } golden_data[] = {
565 { NULL, "" },
566 { "a", "a" },
567 { "abcdef", "abcdef" },
568 };
570 size_t i;
572 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
573 const char *check;
575 if (golden_data[i].input)
576 sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
577 check = sdb_strbuf_string(buf);
578 fail_unless(!strcmp(check, golden_data[i].expected),
579 "sdb_strbuf_string() = '%s'; expected: '%s'",
580 check, golden_data[i].expected);
581 }
582 }
583 END_TEST
585 START_TEST(test_len)
586 {
587 struct {
588 const char *input;
589 size_t expected;
590 } golden_data[] = {
591 { NULL, 0 },
592 { "a", 1 },
593 { "12345", 5 },
594 };
596 size_t i;
598 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
599 size_t check;
601 if (golden_data[i].input)
602 sdb_strbuf_sprintf(buf, "%s", golden_data[i].input);
603 check = sdb_strbuf_len(buf);
604 fail_unless(check == golden_data[i].expected,
605 "sdb_strbuf_len() = %zu; expected: %zu",
606 check, golden_data[i].expected);
607 }
608 }
609 END_TEST
611 Suite *
612 util_strbuf_suite(void)
613 {
614 Suite *s = suite_create("utils::strbuf");
615 TCase *tc;
617 tc = tcase_create("empty");
618 tcase_add_test(tc, test_null);
619 tcase_add_test(tc, test_empty);
620 suite_add_tcase(s, tc);
622 tc = tcase_create("core");
623 tcase_add_checked_fixture(tc, setup, teardown);
624 tcase_add_test(tc, test_create);
625 tcase_add_test(tc, test_append);
626 tcase_add_test(tc, test_sprintf);
627 tcase_add_test(tc, test_incremental);
628 tcase_add_test(tc, test_memcpy);
629 tcase_add_test(tc, test_memappend);
630 tcase_add_test(tc, test_chomp);
631 tcase_add_test(tc, test_skip);
632 tcase_add_test(tc, test_clear);
633 tcase_add_test(tc, test_string);
634 tcase_add_test(tc, test_len);
635 suite_add_tcase(s, tc);
637 return s;
638 } /* util_strbuf_suite */
640 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */