1 /*
2 * SysDB - t/core/data_test.c
3 * Copyright (C) 2014 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 "core/data.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 START_TEST(test_data)
34 {
35 sdb_data_t d1, d2;
36 int check;
38 d2.type = SDB_TYPE_INTEGER;
39 d2.data.integer = 4711;
40 check = sdb_data_copy(&d1, &d2);
41 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
42 fail_unless(d1.type == d2.type,
43 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
44 d1.type, d2.type);
45 fail_unless(d1.data.integer == d2.data.integer,
46 "sdb_data_copy() didn't copy integer data: got: %d; expected: %d",
47 d1.data.integer, d2.data.integer);
49 d2.type = SDB_TYPE_DECIMAL;
50 d2.data.decimal = 47.11;
51 check = sdb_data_copy(&d1, &d2);
52 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
53 fail_unless(d1.type == d2.type,
54 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
55 d1.type, d2.type);
56 fail_unless(d1.data.decimal == d2.data.decimal,
57 "sdb_data_copy() didn't copy decimal data: got: %f; expected: %f",
58 d1.data.decimal, d2.data.decimal);
60 d2.type = SDB_TYPE_STRING;
61 d2.data.string = "some string";
62 check = sdb_data_copy(&d1, &d2);
63 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
64 fail_unless(d1.type == d2.type,
65 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
66 d1.type, d2.type);
67 fail_unless(!strcmp(d1.data.string, d2.data.string),
68 "sdb_data_copy() didn't copy string data: got: %s; expected: %s",
69 d1.data.string, d2.data.string);
71 sdb_data_free_datum(&d1);
72 fail_unless(d1.data.string == NULL,
73 "sdb_data_free_datum() didn't free string data");
75 d2.type = SDB_TYPE_DATETIME;
76 d2.data.datetime = 4711;
77 check = sdb_data_copy(&d1, &d2);
78 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
79 fail_unless(d1.type == d2.type,
80 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
81 d1.type, d2.type);
82 fail_unless(d1.data.datetime == d2.data.datetime,
83 "sdb_data_copy() didn't copy datetime data: got: %d; expected: %d",
84 d1.data.datetime, d2.data.datetime);
86 d2.type = SDB_TYPE_BINARY;
87 d2.data.binary.datum = (unsigned char *)"some string";
88 d2.data.binary.length = strlen((const char *)d2.data.binary.datum);
89 check = sdb_data_copy(&d1, &d2);
90 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
91 fail_unless(d1.type == d2.type,
92 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
93 d1.type, d2.type);
94 fail_unless(d1.data.binary.length == d2.data.binary.length,
95 "sdb_data_copy() didn't copy length; got: %d; expected: 5d",
96 d1.data.binary.length, d2.data.binary.length);
97 fail_unless(!memcmp(d1.data.binary.datum, d2.data.binary.datum,
98 d2.data.binary.length),
99 "sdb_data_copy() didn't copy binary data: got: %s; expected: %s",
100 d1.data.string, d2.data.string);
102 sdb_data_free_datum(&d1);
103 fail_unless(d1.data.binary.length == 0,
104 "sdb_data_free_datum() didn't reset binary datum length");
105 fail_unless(d1.data.binary.datum == NULL,
106 "sdb_data_free_datum() didn't free binary datum");
107 }
108 END_TEST
110 START_TEST(test_format)
111 {
112 struct {
113 sdb_data_t datum;
114 const char *expected;
115 } golden_data[] = {
116 {
117 { SDB_TYPE_INTEGER, { .integer = 4711 } },
118 "4711",
119 },
120 {
121 { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
122 "0x1p+16",
123 },
124 {
125 { SDB_TYPE_STRING, { .string = NULL } },
126 "\"NULL\"",
127 },
128 {
129 { SDB_TYPE_STRING, { .string = "this is a test" } },
130 "\"this is a test\"",
131 },
132 {
133 { SDB_TYPE_STRING, { .string = "special \\ \" characters" } },
134 "\"special \\\\ \\\" characters\"",
135 },
136 {
137 { SDB_TYPE_DATETIME, { .datetime= 471147114711471100 } },
138 "\"1984-12-06 02:11:54 +0000\"",
139 },
140 {
141 { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
142 "\"\"",
143 },
144 {
145 {
146 SDB_TYPE_BINARY,
147 { .binary = { 12, (unsigned char *)"binary\0crap\x42" } },
148 },
149 "\"\\x62\\x69\\x6e\\x61\\x72\\x79\\x0\\x63\\x72\\x61\\x70\\x42\"",
150 },
151 };
153 size_t i;
155 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
156 sdb_data_t *datum = &golden_data[i].datum;
157 char buf[sdb_data_strlen(datum) + 2];
158 int check;
160 memset(buf, (int)'A', sizeof(buf));
162 check = sdb_data_format(datum, buf, sizeof(buf) - 1,
163 SDB_DOUBLE_QUOTED);
164 fail_unless(check > 0,
165 "sdb_data_format(type=%s) = %d; expected: >0",
166 SDB_TYPE_TO_STRING(datum->type), check);
167 fail_unless(! strcmp(buf, golden_data[i].expected),
168 "sdb_data_format(type=%s) used wrong format: %s; expected: %s",
169 SDB_TYPE_TO_STRING(datum->type), buf, golden_data[i].expected);
171 fail_unless((size_t)check <= sizeof(buf) - 2,
172 "sdb_data_format(type=%s) wrote %d bytes; "
173 "expected <= %zu based on sdb_data_strlen()",
174 SDB_TYPE_TO_STRING(datum->type), check, sizeof(buf) - 2);
176 fail_unless(buf[sizeof(buf) - 2] == '\0',
177 "sdb_data_format(type=%s) did not nul-terminate the buffer",
178 SDB_TYPE_TO_STRING(datum->type));
179 fail_unless(buf[sizeof(buf) - 1] == 'A',
180 "sdb_data_format(type=%s) wrote past the end of the buffer",
181 SDB_TYPE_TO_STRING(datum->type));
182 }
183 }
184 END_TEST
186 Suite *
187 core_data_suite(void)
188 {
189 Suite *s = suite_create("core::data");
190 TCase *tc;
192 tc = tcase_create("core");
193 tcase_add_test(tc, test_data);
194 tcase_add_test(tc, test_format);
195 suite_add_tcase(s, tc);
197 return s;
198 } /* core_data_suite */
200 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */