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 /* check that methods don't crash */
64 sdb_strbuf_destroy(b);
65 sdb_strbuf_skip(b, 0, 0);
66 sdb_strbuf_skip(b, 0, 10);
67 sdb_strbuf_skip(b, 10, 10);
68 sdb_strbuf_clear(b);
70 /* check that methods return an error */
71 fail_unless(sdb_strbuf_vappend(b, "test", ap) < 0,
72 "sdb_strbuf_vappend(NULL) didn't report failure");
73 fail_unless(sdb_strbuf_append(b, "test") < 0,
74 "sdb_strbuf_append(NULL) didn't report failure");
75 fail_unless(sdb_strbuf_vsprintf(b, "test", ap) < 0,
76 "sdb_strbuf_vsprintf(NULL) didn't report failure");
77 fail_unless(sdb_strbuf_sprintf(b, "test") < 0,
78 "sdb_strbuf_sprintf(NULL) didn't report failure");
79 fail_unless(sdb_strbuf_memcpy(b, "test", 4) < 0,
80 "sdb_strbuf_memcpy(NULL) didn't report failure");
81 fail_unless(sdb_strbuf_memappend(b, "test", 4) < 0,
82 "sdb_strbuf_memappend(NULL) didn't report failure");
83 fail_unless(sdb_strbuf_read(b, 0, 32) < 0,
84 "sdb_strbuf_read(NULL) didn't report failure");
85 fail_unless(sdb_strbuf_chomp(b) < 0,
86 "sdb_strbuf_chomp(NULL) didn't report failure");
87 }
88 END_TEST
90 START_TEST(test_empty)
91 {
92 sdb_strbuf_t *b = sdb_strbuf_create(0);
93 const char *data;
94 size_t len;
96 /* check that methods don't crash */
97 sdb_strbuf_skip(b, 1, 1);
98 sdb_strbuf_clear(b);
99 sdb_strbuf_chomp(b);
101 data = sdb_strbuf_string(b);
102 fail_unless(data && (*data == '\0'),
103 "sdb_strbuf_string(<empty>) = '%s'; expected: ''", data);
104 len = sdb_strbuf_len(b);
105 fail_unless(len == 0,
106 "sdb_strbuf_len(<empty>) = %zu; expected: 0", len);
108 sdb_strbuf_destroy(b);
109 }
110 END_TEST
112 START_TEST(test_create)
113 {
114 sdb_strbuf_t *s;
115 size_t len;
117 s = sdb_strbuf_create(0);
118 fail_unless(s != NULL,
119 "sdb_strbuf_create() = NULL; expected strbuf object");
120 len = sdb_strbuf_len(s);
121 fail_unless(len == 0,
122 "sdb_strbuf_create() created buffer with len = %zu; "
123 "expected: 0", len);
124 sdb_strbuf_destroy(s);
126 s = sdb_strbuf_create(128);
127 fail_unless(s != NULL,
128 "sdb_strbuf_create() = NULL; expected strbuf object");
129 len = sdb_strbuf_len(s);
130 /* len still has to be 0 -- there's no content */
131 fail_unless(len == 0,
132 "sdb_strbuf_create() created buffer with len = %zu; "
133 "expected: 0", len);
134 sdb_strbuf_destroy(s);
135 }
136 END_TEST
138 START_TEST(test_append)
139 {
140 ssize_t n, expected;
141 size_t len;
142 const char *test;
144 n = sdb_strbuf_append(buf, "1234567890");
145 fail_unless(n == 10,
146 "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
147 len = sdb_strbuf_len(buf);
148 fail_unless(len == 10,
149 "sdb_strbuf_append() left behind buffer with len = %zu; "
150 "expected: 10", len);
152 n = sdb_strbuf_append(buf, "ABCDE");
153 fail_unless(n == 5,
154 "sdb_strbuf_append() appended %zi bytes; expected: 5", n);
155 len = sdb_strbuf_len(buf);
156 fail_unless(len == 15,
157 "sdb_strbuf_append() left behind buffer with len = %zu; "
158 "expected: 15", len);
160 test = sdb_strbuf_string(buf);
161 fail_unless(test[len] == '\0',
162 "sdb_strbuf_append() did not nil-terminate the string");
163 fail_unless(!strcmp(test, "1234567890ABCDE"),
164 "sdb_strbuf_append() did not correctly concatenate two string; "
165 "got: %s; expected: 1234567890ABCDE", test);
167 n = sdb_strbuf_append(buf, "%zu; %5.4f", len, (double)len / 10.0);
168 expected = /* len */ 2 + /* "; " */ 2 + /* decimal len/10 */ 6;
169 fail_unless(n == expected,
170 "sdb_strbuf_append() appended %zi bytes; expected: %zi",
171 n, expected);
172 len = sdb_strbuf_len(buf);
173 fail_unless(len == 15 + (size_t)expected,
174 "sdb_strbuf_append() left behind buffer with len = %zu; "
175 "expected: %zu", len, 15 + (size_t)expected);
177 test = sdb_strbuf_string(buf);
178 fail_unless(test[len] == '\0',
179 "sdb_strbuf_append() did not nil-terminate the string");
180 fail_unless(!strcmp(test, "1234567890ABCDE15; 1.5000"),
181 "sdb_strbuf_append() did not correctly concatenate two string; "
182 "got: %s; expected: 1234567890ABCDE15; 1.5000", test);
183 }
184 END_TEST
186 START_TEST(test_sprintf)
187 {
188 ssize_t n, expected;
189 size_t len;
190 const char *test;
192 n = sdb_strbuf_sprintf(buf, "1234567890");
193 fail_unless(n == 10,
194 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
195 len = sdb_strbuf_len(buf);
196 fail_unless(len == 10,
197 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
198 "expected: 10", len);
200 n = sdb_strbuf_sprintf(buf, "ABCDE");
201 fail_unless(n == 5,
202 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 5", n);
203 len = sdb_strbuf_len(buf);
204 fail_unless(len == 5,
205 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
206 "expected: 5", len);
208 test = sdb_strbuf_string(buf);
209 fail_unless(test[len] == '\0',
210 "sdb_strbuf_sprintf() did not nil-terminate the string");
211 fail_unless(!strcmp(test, "ABCDE"),
212 "sdb_strbuf_sprintf() did not format string correctly; "
213 "got: %s; expected: ABCDE", test);
215 n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", len, (double)len / 10.0);
216 expected = /* len */ 1 + /* "; " */ 2 + /* decimal len/10 */ 6;
217 fail_unless(n == expected,
218 "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zi",
219 n, expected);
220 len = sdb_strbuf_len(buf);
221 fail_unless(len == (size_t)expected,
222 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
223 "expected: %zu", len, (size_t)expected);
225 test = sdb_strbuf_string(buf);
226 fail_unless(test[len] == '\0',
227 "sdb_strbuf_sprintf() did not nil-terminate the string");
228 fail_unless(!strcmp(test, "5; 0.5000"),
229 "sdb_strbuf_sprintf() did not format string correctly; "
230 "got: %s; expected: 5; 0.5000", test);
231 }
232 END_TEST
234 START_TEST(test_incremental)
235 {
236 ssize_t n;
237 size_t i;
239 sdb_strbuf_destroy(buf);
240 buf = sdb_strbuf_create(1024);
242 /* fill buffer one by one; leave room for nul-byte */
243 for (i = 0; i < 1023; ++i) {
244 n = sdb_strbuf_append(buf, ".");
245 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
246 }
248 /* write another byte; this has to trigger a resize */
249 n = sdb_strbuf_append(buf, ".");
250 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
252 /* write more bytes; this should trigger at least one more resize but
253 * that's an implementation detail */
254 for (i = 0; i < 1024; ++i) {
255 n = sdb_strbuf_append(buf, ".");
256 fail_unless(n == 1, "sdb_strbuf_append() = %zi; expected: 1", n);
257 }
259 n = (ssize_t)sdb_strbuf_len(buf);
260 fail_unless(n == 2048, "sdb_strbuf_len() = %zi; expectd: 2048", n);
261 }
262 END_TEST
264 /* used by test_memcpy and test_memappend */
265 static struct {
266 const char *input;
267 size_t size;
268 } mem_golden_data[] = {
269 { "abc\0\x10\x42", 6 },
270 { "\0\1\2\3\4", 5 },
271 { "\n\n\0\n\n", 5 },
272 { "", 0 },
273 };
275 START_TEST(test_memcpy)
276 {
277 size_t i;
279 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
280 ssize_t n;
281 const char *check;
283 n = sdb_strbuf_memcpy(buf, mem_golden_data[i].input,
284 mem_golden_data[i].size);
285 fail_unless(n >= 0,
286 "sdb_strbuf_memcpy() = %zi; expected: >=0", n);
287 fail_unless((size_t)n == mem_golden_data[i].size,
288 "sdb_strbuf_memcpy() = %zi; expected: %zu",
289 n, mem_golden_data[i].size);
291 n = (ssize_t)sdb_strbuf_len(buf);
292 fail_unless((size_t)n == mem_golden_data[i].size,
293 "sdb_strbuf_len() = %zu (after memcpy); expected: %zu",
294 n, mem_golden_data[i].size);
296 check = sdb_strbuf_string(buf);
297 fail_unless(check != NULL,
298 "sdb_strbuf_string() = NULL (after memcpy); expected: data");
299 fail_unless(check[mem_golden_data[i].size] == '\0',
300 "sdb_strbuf_memcpy() did not nil-terminate the data");
301 fail_unless(!memcmp(check, mem_golden_data[i].input,
302 mem_golden_data[i].size),
303 "sdb_strbuf_memcpy() did not set the buffer correctly");
304 }
305 }
306 END_TEST
308 START_TEST(test_memappend)
309 {
310 size_t i;
312 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
313 ssize_t n;
314 const char *check;
316 size_t total, j;
318 n = sdb_strbuf_memappend(buf, mem_golden_data[i].input,
319 mem_golden_data[i].size);
320 fail_unless(n >= 0,
321 "sdb_strbuf_memappend() = %zi; expected: >=0", n);
322 fail_unless((size_t)n == mem_golden_data[i].size,
323 "sdb_strbuf_memappend() = %zi; expected: %zu",
324 n, mem_golden_data[i].size);
326 check = sdb_strbuf_string(buf);
327 fail_unless(check != NULL,
328 "sdb_strbuf_string() = NULL (after memappend); "
329 "expected: data");
331 n = (ssize_t)sdb_strbuf_len(buf);
332 total = 0;
333 for (j = 0; j <= i; ++j) {
334 fail_unless(total + mem_golden_data[j].size <= (size_t)n,
335 "sdb_strbuf_len() = %zu (after memappend); "
336 "expected: >=%zu", n, total + mem_golden_data[j].size);
338 fail_unless(!memcmp(check + total, mem_golden_data[j].input,
339 mem_golden_data[j].size),
340 "sdb_strbuf_memappend() did not "
341 "set the buffer correctly");
342 total += mem_golden_data[j].size;
343 }
344 fail_unless((size_t)n == total,
345 "sdb_strbuf_len() = %zu (after memappend); expected: %zu",
346 n, total);
348 fail_unless(check[total] == '\0',
349 "sdb_strbuf_memappend() did not nil-terminate the data");
350 }
351 }
352 END_TEST
354 START_TEST(test_chomp)
355 {
356 struct {
357 const char *input;
358 ssize_t expected;
359 const char *expected_string;
360 } golden_data[] = {
361 { NULL, 0, "" },
362 { "\n", 1, "" },
363 { "\n\n", 2, "" },
364 { "12345\n\n\n", 3, "12345" },
365 { "abcd", 0, "abcd" },
366 };
368 size_t i;
370 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
371 ssize_t n;
372 const char *check;
374 if (golden_data[i].input)
375 sdb_strbuf_sprintf(buf, golden_data[i].input);
377 /* empty buffer */
378 n = sdb_strbuf_chomp(buf);
379 fail_unless(n == golden_data[i].expected,
380 "sdb_strbuf_chomp() = %zi; expected: %zi", n,
381 golden_data[i].expected);
383 check = sdb_strbuf_string(buf);
384 fail_unless(!strcmp(check, golden_data[i].expected_string),
385 "sdb_strbuf_chomp() did not correctly remove newlines; "
386 "got string '%s'; expected: '%s'", check,
387 golden_data[i].expected_string);
388 }
389 }
390 END_TEST
392 START_TEST(test_skip)
393 {
394 const char *input = "1234567890";
395 struct {
396 size_t offset;
397 size_t n;
398 const char *expected;
399 size_t expected_len;
400 } golden_data[] = {
401 { 0, 0, "1234567890", 10 },
402 { 0, 1, "234567890", 9 },
403 { 0, 2, "34567890", 8 },
404 { 0, 9, "0", 1 },
405 { 0, 10, "", 0 },
406 { 0, 11, "", 0 },
407 { 0, 100, "", 0 },
408 { 1, 0, "1234567890", 10 },
409 { 1, 1, "134567890", 9 },
410 { 1, 2, "14567890", 8 },
411 { 2, 0, "1234567890", 10 },
412 { 2, 1, "124567890", 9 },
413 { 2, 2, "12567890", 8 },
414 { 2, 3, "1267890", 7 },
415 { 2, 4, "127890", 6 },
416 { 2, 5, "12890", 5 },
417 { 2, 6, "1290", 4 },
418 { 2, 7, "120", 3 },
419 { 2, 8, "12", 2 },
420 { 2, 9, "12", 2 },
421 { 2, 10, "12", 2 },
422 { 8, 1, "123456780", 9 },
423 { 8, 2, "12345678", 8 },
424 { 8, 3, "12345678", 8 },
425 { 9, 1, "123456789", 9 },
426 { 9, 2, "123456789", 9 },
427 { 10, 1, "1234567890", 10 },
428 { 10, 2, "1234567890", 10 },
429 };
431 size_t i;
433 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
434 const char *check;
435 size_t n;
437 sdb_strbuf_sprintf(buf, input);
438 sdb_strbuf_skip(buf, golden_data[i].offset,
439 golden_data[i].n);
441 n = sdb_strbuf_len(buf);
442 fail_unless(n == golden_data[i].expected_len,
443 "sdb_strbuf_len() = %zu (after skip); expected: %zu",
444 n, golden_data[i].expected_len);
446 check = sdb_strbuf_string(buf);
447 fail_unless(!!check,
448 "sdb_strbuf_string() = NULL (after skip); expected: string");
450 fail_unless(check[n] == '\0',
451 "sdb_strbuf_skip() did not nil-terminate the string");
453 fail_unless(! strcmp(golden_data[i].expected, check),
454 "sdb_strbuf_skip('%s', %zu) did not skip correctly; "
455 "got string '%s'; expected: '%s'", input,
456 golden_data[i].n, check, golden_data[i].expected);
457 }
458 }
459 END_TEST
461 START_TEST(test_clear)
462 {
463 const char *data;
464 size_t len;
466 sdb_strbuf_append(buf, "abc");
467 len = sdb_strbuf_len(buf);
468 fail_unless(len != 0,
469 "sdb_strbuf_len() = %zu; expected: != 0", len);
471 sdb_strbuf_clear(buf);
472 len = sdb_strbuf_len(buf);
473 fail_unless(len == 0,
474 "sdb_strbuf_len() = %zu (after clear); expected: 0", len);
476 data = sdb_strbuf_string(buf);
477 fail_unless(*data == '\0',
478 "sdb_strbuf_string() = '%s' (after clear); expected: ''", data);
479 }
480 END_TEST
482 START_TEST(test_string)
483 {
484 struct {
485 const char *input;
486 const char *expected;
487 } golden_data[] = {
488 { NULL, "" },
489 { "a", "a" },
490 { "abcdef", "abcdef" },
491 };
493 size_t i;
495 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
496 const char *check;
498 if (golden_data[i].input)
499 sdb_strbuf_sprintf(buf, golden_data[i].input);
500 check = sdb_strbuf_string(buf);
501 fail_unless(!strcmp(check, golden_data[i].expected),
502 "sdb_strbuf_string() = '%s'; expected: '%s'",
503 check, golden_data[i].expected);
504 }
505 }
506 END_TEST
508 START_TEST(test_len)
509 {
510 struct {
511 const char *input;
512 size_t expected;
513 } golden_data[] = {
514 { NULL, 0 },
515 { "a", 1 },
516 { "12345", 5 },
517 };
519 size_t i;
521 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
522 size_t check;
524 if (golden_data[i].input)
525 sdb_strbuf_sprintf(buf, golden_data[i].input);
526 check = sdb_strbuf_len(buf);
527 fail_unless(check == golden_data[i].expected,
528 "sdb_strbuf_len() = %zu; expected: %zu",
529 check, golden_data[i].expected);
530 }
531 }
532 END_TEST
534 Suite *
535 util_strbuf_suite(void)
536 {
537 Suite *s = suite_create("utils::strbuf");
538 TCase *tc;
540 tc = tcase_create("empty");
541 tcase_add_test(tc, test_null);
542 tcase_add_test(tc, test_empty);
543 suite_add_tcase(s, tc);
545 tc = tcase_create("core");
546 tcase_add_checked_fixture(tc, setup, teardown);
547 tcase_add_test(tc, test_create);
548 tcase_add_test(tc, test_append);
549 tcase_add_test(tc, test_sprintf);
550 tcase_add_test(tc, test_incremental);
551 tcase_add_test(tc, test_memcpy);
552 tcase_add_test(tc, test_memappend);
553 tcase_add_test(tc, test_chomp);
554 tcase_add_test(tc, test_skip);
555 tcase_add_test(tc, test_clear);
556 tcase_add_test(tc, test_string);
557 tcase_add_test(tc, test_len);
558 suite_add_tcase(s, tc);
560 return s;
561 } /* util_strbuf_suite */
563 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */