9e96b984a2a764d5d322fc60e1065e2ee9cb2b6b
1 /*
2 * SysDB - t/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_empty)
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);
66 sdb_strbuf_skip(b, 10);
68 /* check that methods return an error */
69 fail_unless(sdb_strbuf_vappend(b, "test", ap) < 0,
70 "sdb_strbuf_vappend(NULL) didn't report failure");
71 fail_unless(sdb_strbuf_append(b, "test") < 0,
72 "sdb_strbuf_append(NULL) didn't report failure");
73 fail_unless(sdb_strbuf_vsprintf(b, "test", ap) < 0,
74 "sdb_strbuf_vsprintf(NULL) didn't report failure");
75 fail_unless(sdb_strbuf_sprintf(b, "test") < 0,
76 "sdb_strbuf_sprintf(NULL) didn't report failure");
77 fail_unless(sdb_strbuf_memcpy(b, "test", 4) < 0,
78 "sdb_strbuf_memcpy(NULL) didn't report failure");
79 fail_unless(sdb_strbuf_memappend(b, "test", 4) < 0,
80 "sdb_strbuf_memappend(NULL) didn't report failure");
81 fail_unless(sdb_strbuf_read(b, 0, 32) < 0,
82 "sdb_strbuf_read(NULL) didn't report failure");
83 fail_unless(sdb_strbuf_chomp(b) < 0,
84 "sdb_strbuf_chomp(NULL) didn't report failure");
85 }
86 END_TEST
88 START_TEST(test_sdb_strbuf_create)
89 {
90 sdb_strbuf_t *s;
91 size_t len;
93 s = sdb_strbuf_create(0);
94 fail_unless(s != NULL,
95 "sdb_strbuf_create() = NULL; expected strbuf object");
96 len = sdb_strbuf_len(s);
97 fail_unless(len == 0,
98 "sdb_strbuf_create() created buffer with len = %zu; "
99 "expected: 0", len);
100 sdb_strbuf_destroy(s);
102 s = sdb_strbuf_create(128);
103 fail_unless(s != NULL,
104 "sdb_strbuf_create() = NULL; expected strbuf object");
105 len = sdb_strbuf_len(s);
106 /* len still has to be 0 -- there's no content */
107 fail_unless(len == 0,
108 "sdb_strbuf_create() created buffer with len = %zu; "
109 "expected: 0", len);
110 sdb_strbuf_destroy(s);
111 }
112 END_TEST
114 START_TEST(test_sdb_strbuf_append)
115 {
116 ssize_t n, expected;
117 size_t len;
118 const char *test;
120 n = sdb_strbuf_append(buf, "1234567890");
121 fail_unless(n == 10,
122 "sdb_strbuf_append() appended %zi bytes; expected: 10", n);
123 len = sdb_strbuf_len(buf);
124 fail_unless(len == 10,
125 "sdb_strbuf_append() left behind buffer with len = %zu; "
126 "expected: 10", len);
128 n = sdb_strbuf_append(buf, "ABCDE");
129 fail_unless(n == 5,
130 "sdb_strbuf_append() appended %zi bytes; expected: 5", n);
131 len = sdb_strbuf_len(buf);
132 fail_unless(len == 15,
133 "sdb_strbuf_append() left behind buffer with len = %zu; "
134 "expected: 15", len);
136 test = sdb_strbuf_string(buf);
137 fail_unless(test[len] == '\0',
138 "sdb_strbuf_append() did not nil-terminate the string");
139 fail_unless(!strcmp(test, "1234567890ABCDE"),
140 "sdb_strbuf_append() did not correctly concatenate two string; "
141 "got: %s; expected: 1234567890ABCDE", test);
143 n = sdb_strbuf_append(buf, "%zu; %5.4f", len, (double)len / 10.0);
144 expected = /* len */ 2 + /* "; " */ 2 + /* decimal len/10 */ 6;
145 fail_unless(n == expected,
146 "sdb_strbuf_append() appended %zi bytes; expected: %zi",
147 n, expected);
148 len = sdb_strbuf_len(buf);
149 fail_unless(len == 15 + (size_t)expected,
150 "sdb_strbuf_append() left behind buffer with len = %zu; "
151 "expected: %zu", len, 15 + (size_t)expected);
153 test = sdb_strbuf_string(buf);
154 fail_unless(test[len] == '\0',
155 "sdb_strbuf_append() did not nil-terminate the string");
156 fail_unless(!strcmp(test, "1234567890ABCDE15; 1.5000"),
157 "sdb_strbuf_append() did not correctly concatenate two string; "
158 "got: %s; expected: 1234567890ABCDE15; 1.5000", test);
159 }
160 END_TEST
162 START_TEST(test_sdb_strbuf_sprintf)
163 {
164 ssize_t n, expected;
165 size_t len;
166 const char *test;
168 n = sdb_strbuf_sprintf(buf, "1234567890");
169 fail_unless(n == 10,
170 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 10", n);
171 len = sdb_strbuf_len(buf);
172 fail_unless(len == 10,
173 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
174 "expected: 10", len);
176 n = sdb_strbuf_sprintf(buf, "ABCDE");
177 fail_unless(n == 5,
178 "sdb_strbuf_sprintf() wrote %zi bytes; expected: 5", n);
179 len = sdb_strbuf_len(buf);
180 fail_unless(len == 5,
181 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
182 "expected: 5", len);
184 test = sdb_strbuf_string(buf);
185 fail_unless(test[len] == '\0',
186 "sdb_strbuf_sprintf() did not nil-terminate the string");
187 fail_unless(!strcmp(test, "ABCDE"),
188 "sdb_strbuf_sprintf() did not format string correctly; "
189 "got: %s; expected: ABCDE", test);
191 n = sdb_strbuf_sprintf(buf, "%zu; %5.4f", len, (double)len / 10.0);
192 expected = /* len */ 1 + /* "; " */ 2 + /* decimal len/10 */ 6;
193 fail_unless(n == expected,
194 "sdb_strbuf_sprintf() wrote %zi bytes; expected: %zi",
195 n, expected);
196 len = sdb_strbuf_len(buf);
197 fail_unless(len == (size_t)expected,
198 "sdb_strbuf_sprintf() left behind buffer with len = %zu; "
199 "expected: %zu", len, (size_t)expected);
201 test = sdb_strbuf_string(buf);
202 fail_unless(test[len] == '\0',
203 "sdb_strbuf_sprintf() did not nil-terminate the string");
204 fail_unless(!strcmp(test, "5; 0.5000"),
205 "sdb_strbuf_sprintf() did not format string correctly; "
206 "got: %s; expected: 5; 0.5000", test);
207 }
208 END_TEST
210 static struct {
211 const char *input;
212 size_t size;
213 } mem_golden_data[] = {
214 { "abc\0\x10\x42", 6 },
215 { "\0\1\2\3\4", 5 },
216 { "\n\n\0\n\n", 5 },
217 { "", 0 },
218 };
220 START_TEST(test_sdb_strbuf_memcpy)
221 {
222 size_t i;
224 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
225 ssize_t n;
226 const char *check;
228 n = sdb_strbuf_memcpy(buf, mem_golden_data[i].input,
229 mem_golden_data[i].size);
230 fail_unless(n >= 0,
231 "sdb_strbuf_memcpy() = %zi; expected: >=0", n);
232 fail_unless((size_t)n == mem_golden_data[i].size,
233 "sdb_strbuf_memcpy() = %zi; expected: %zu",
234 n, mem_golden_data[i].size);
236 n = (ssize_t)sdb_strbuf_len(buf);
237 fail_unless((size_t)n == mem_golden_data[i].size,
238 "sdb_strbuf_len() = %zu (after memcpy); expected: %zu",
239 n, mem_golden_data[i].size);
241 check = sdb_strbuf_string(buf);
242 fail_unless(check != NULL,
243 "sdb_strbuf_string() = NULL (after memcpy); expected: data");
244 fail_unless(check[mem_golden_data[i].size] == '\0',
245 "sdb_strbuf_memcpy() did not nil-terminate the data");
246 fail_unless(!memcmp(check, mem_golden_data[i].input,
247 mem_golden_data[i].size),
248 "sdb_strbuf_memcpy() did not set the buffer correctly");
249 }
250 }
251 END_TEST
253 START_TEST(test_sdb_strbuf_memappend)
254 {
255 size_t i;
257 for (i = 0; i < SDB_STATIC_ARRAY_LEN(mem_golden_data); ++i) {
258 ssize_t n;
259 const char *check;
261 size_t total, j;
263 n = sdb_strbuf_memappend(buf, mem_golden_data[i].input,
264 mem_golden_data[i].size);
265 fail_unless(n >= 0,
266 "sdb_strbuf_memappend() = %zi; expected: >=0", n);
267 fail_unless((size_t)n == mem_golden_data[i].size,
268 "sdb_strbuf_memappend() = %zi; expected: %zu",
269 n, mem_golden_data[i].size);
271 check = sdb_strbuf_string(buf);
272 fail_unless(check != NULL,
273 "sdb_strbuf_string() = NULL (after memappend); "
274 "expected: data");
276 n = (ssize_t)sdb_strbuf_len(buf);
277 total = 0;
278 for (j = 0; j <= i; ++j) {
279 fail_unless(total + mem_golden_data[j].size <= (size_t)n,
280 "sdb_strbuf_len() = %zu (after memappend); "
281 "expected: >=%zu", n, total + mem_golden_data[j].size);
283 fail_unless(!memcmp(check + total, mem_golden_data[j].input,
284 mem_golden_data[j].size),
285 "sdb_strbuf_memappend() did not "
286 "set the buffer correctly");
287 total += mem_golden_data[j].size;
288 }
289 fail_unless((size_t)n == total,
290 "sdb_strbuf_len() = %zu (after memappend); expected: %zu",
291 n, total);
293 fail_unless(check[total] == '\0',
294 "sdb_strbuf_memappend() did not nil-terminate the data");
295 }
296 }
297 END_TEST
299 static struct {
300 const char *input;
301 ssize_t expected;
302 const char *expected_string;
303 } chomp_golden_data[] = {
304 { NULL, 0, "" },
305 { "\n", 1, "" },
306 { "\n\n", 2, "" },
307 { "12345\n\n\n", 3, "12345" },
308 { "abcd", 0, "abcd" },
309 };
311 START_TEST(test_sdb_strbuf_chomp)
312 {
313 size_t i;
315 for (i = 0; i < SDB_STATIC_ARRAY_LEN(chomp_golden_data); ++i) {
316 ssize_t n;
317 const char *check;
319 if (chomp_golden_data[i].input)
320 sdb_strbuf_sprintf(buf, chomp_golden_data[i].input);
322 /* empty buffer */
323 n = sdb_strbuf_chomp(buf);
324 fail_unless(n == chomp_golden_data[i].expected,
325 "sdb_strbuf_chomp() = %zi; expected: %zi", n,
326 chomp_golden_data[i].expected);
328 check = sdb_strbuf_string(buf);
329 fail_unless(!strcmp(check, chomp_golden_data[i].expected_string),
330 "sdb_strbuf_chomp() did not correctly remove newlines; "
331 "got string '%s'; expected: '%s'", check,
332 chomp_golden_data[i].expected_string);
333 }
334 }
335 END_TEST
337 /* input is "1234567890" */
338 static struct {
339 size_t n;
340 const char *expected;
341 size_t expected_len;
342 } skip_golden_data[] = {
343 { 0, "1234567890", 10 },
344 { 1, "234567890", 9 },
345 { 2, "34567890", 8 },
346 { 9, "0", 1 },
347 { 10, "", 0 },
348 { 11, "", 0 },
349 { 100, "", 0 },
350 };
352 START_TEST(test_sdb_strbuf_skip)
353 {
354 const char *input = "1234567890";
355 size_t i;
357 for (i = 0; i < SDB_STATIC_ARRAY_LEN(skip_golden_data); ++i) {
358 const char *check;
359 size_t n;
361 sdb_strbuf_sprintf(buf, input);
362 sdb_strbuf_skip(buf, skip_golden_data[i].n);
364 n = sdb_strbuf_len(buf);
365 fail_unless(n == skip_golden_data[i].expected_len,
366 "sdb_strbuf_len() = %zu (after skip); expected: %zu",
367 n, skip_golden_data[i].expected_len);
369 check = sdb_strbuf_string(buf);
370 fail_unless(!!check,
371 "sdb_strbuf_string() = NULL (after skip); expected: string");
373 fail_unless(check[n] == '\0',
374 "sdb_strbuf_skip() did not nil-terminate the string");
376 fail_unless(! strcmp(skip_golden_data[i].expected, check),
377 "sdb_strbuf_skip('%s', %zu) did not skip correctly; "
378 "got string '%s'; expected: '%s'", input,
379 skip_golden_data[i].n, check, skip_golden_data[i].expected);
380 }
381 }
382 END_TEST
384 static struct {
385 const char *input;
386 const char *expected;
387 } string_golden_data[] = {
388 { NULL, "" },
389 { "a", "a" },
390 { "abcdef", "abcdef" },
391 };
393 START_TEST(test_sdb_strbuf_string)
394 {
395 size_t i;
397 for (i = 0; i < SDB_STATIC_ARRAY_LEN(string_golden_data); ++i) {
398 const char *check;
400 if (string_golden_data[i].input)
401 sdb_strbuf_sprintf(buf, string_golden_data[i].input);
402 check = sdb_strbuf_string(buf);
403 fail_unless(!strcmp(check, string_golden_data[i].expected),
404 "sdb_strbuf_string() = '%s'; expected: '%s'",
405 check, string_golden_data[i].expected);
406 }
407 }
408 END_TEST
410 static struct {
411 const char *input;
412 size_t expected;
413 } len_golden_data[] = {
414 { NULL, 0 },
415 { "a", 1 },
416 { "12345", 5 },
417 };
419 START_TEST(test_sdb_strbuf_len)
420 {
421 size_t i;
423 for (i = 0; i < SDB_STATIC_ARRAY_LEN(len_golden_data); ++i) {
424 size_t check;
426 if (len_golden_data[i].input)
427 sdb_strbuf_sprintf(buf, len_golden_data[i].input);
428 check = sdb_strbuf_len(buf);
429 fail_unless(check == len_golden_data[i].expected,
430 "sdb_strbuf_len() = %zu; expected: %zu",
431 check, len_golden_data[i].expected);
432 }
433 }
434 END_TEST
436 Suite *
437 util_strbuf_suite(void)
438 {
439 Suite *s = suite_create("utils::strbuf");
440 TCase *tc;
442 tc = tcase_create("empty");
443 tcase_add_test(tc, test_empty);
444 suite_add_tcase(s, tc);
446 tc = tcase_create("core");
447 tcase_add_checked_fixture(tc, setup, teardown);
448 tcase_add_test(tc, test_sdb_strbuf_create);
449 tcase_add_test(tc, test_sdb_strbuf_append);
450 tcase_add_test(tc, test_sdb_strbuf_sprintf);
451 tcase_add_test(tc, test_sdb_strbuf_memcpy);
452 tcase_add_test(tc, test_sdb_strbuf_memappend);
453 tcase_add_test(tc, test_sdb_strbuf_chomp);
454 tcase_add_test(tc, test_sdb_strbuf_skip);
455 tcase_add_test(tc, test_sdb_strbuf_string);
456 tcase_add_test(tc, test_sdb_strbuf_len);
457 suite_add_tcase(s, tc);
459 return s;
460 } /* util_strbuf_suite */
462 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */