9f24c81e43cf1b054be3e45c1c391cbca6636900
1 /*
2 * SysDB - t/unit/utils/proto_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 "utils/proto.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
32 #include <stdio.h>
33 #include <string.h>
35 START_TEST(test_marshal_data)
36 {
37 #define INT_TYPE "\0\0\0\1"
38 #define DECIMAL_TYPE "\0\0\0\2"
39 #define STRING_TYPE "\0\0\0\3"
40 #define DATETIME_TYPE "\0\0\0\4"
41 #define BINARY_TYPE "\0\0\0\5"
42 #define REGEX_TYPE "\0\0\0\6"
44 #define NULL_ARRAY "\0\0\1\0"
45 #define INT_ARRAY "\0\0\1\1"
46 #define DECIMAL_ARRAY "\0\0\1\2"
47 #define STRING_ARRAY "\0\0\1\3"
48 #define DATETIME_ARRAY "\0\0\1\4"
49 #define BINARY_ARRAY "\0\0\1\5"
50 #define REGEX_ARRAY "\0\0\1\6"
52 regex_t dummy_re;
53 int64_t int_values[] = { 47, 11, 23 };
54 double dec_values[] = { 47.11, .5 };
55 char *string_values[] = { "foo", "abcd" };
56 sdb_time_t datetime_values[] = { 4711, 1234567890123456789L };
57 struct {
58 size_t length;
59 unsigned char *datum;
60 } binary_values[] = {
61 { 3, (unsigned char *)"\x1\x2\x3" },
62 { 4, (unsigned char *)"\x42\x0\xa\x1b" },
63 };
64 struct {
65 char *raw;
66 regex_t regex;
67 } regex_values[] = {
68 { "dummy regex", dummy_re },
69 };
71 struct {
72 sdb_data_t datum;
73 ssize_t expected_len;
74 char *expected;
75 } golden_data[] = {
76 {
77 { SDB_TYPE_NULL, { .integer = 0 } },
78 4, "\0\0\0\0",
79 },
80 {
81 { SDB_TYPE_INTEGER, { .integer = 4711 } },
82 12, INT_TYPE "\0\0\0\0\0\0\x12\x67",
83 },
84 {
85 { SDB_TYPE_DECIMAL, { .decimal = 3.141592653e130 } },
86 12, DECIMAL_TYPE "\x5b\x6\xa9\x40\x66\x1e\x10\x4",
87 },
88 {
89 { SDB_TYPE_STRING, { .string = "some string" } },
90 /* length includes the null byte */
91 20, STRING_TYPE "\0\0\0\xc" "some string\0",
92 },
93 {
94 { SDB_TYPE_DATETIME, { .datetime = 1418923804000000 } },
95 12, DATETIME_TYPE "\x0\x5\xa\x80\xf1\x4c\xff\x0",
96 },
97 {
98 { SDB_TYPE_BINARY, { .binary = {
99 4, (unsigned char *)"\x42\x0\xa\x1b" } } },
100 12, BINARY_TYPE "\0\0\0\x4" "\x42\x0\xa\x1b",
101 },
102 {
103 { SDB_TYPE_REGEX, { .re = { "dummy", dummy_re } } },
104 14, REGEX_TYPE "\0\0\0\x6" "dummy\0",
105 },
106 {
107 { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = {
108 3, int_values } } },
109 32, INT_ARRAY "\0\0\0\x3" "\0\0\0\0\0\0\0\x2f"
110 "\0\0\0\0\0\0\0\xb" "\0\0\0\0\0\0\0\x17"
111 },
112 {
113 { SDB_TYPE_DECIMAL | SDB_TYPE_ARRAY, { .array = {
114 2, dec_values } } },
115 24, DECIMAL_ARRAY "\0\0\0\x2" "\x40\x47\x8e\x14\x7a\xe1\x47\xae"
116 "\x3f\xe0\0\0\0\0\0\0"
117 },
118 {
119 { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = {
120 2, string_values } } },
121 25, STRING_ARRAY "\0\0\0\x2" "\0\0\0\x4" "foo\0"
122 "\0\0\0\x5" "abcd\0"
123 },
124 {
125 { SDB_TYPE_DATETIME | SDB_TYPE_ARRAY, { .array = {
126 2, datetime_values } } },
127 24, DATETIME_ARRAY "\0\0\0\x2" "\0\0\0\0\0\0\x12\x67"
128 "\x11\x22\x10\xf4\x7d\xe9\x81\x15"
129 },
130 {
131 { SDB_TYPE_BINARY | SDB_TYPE_ARRAY, { .array = {
132 2, binary_values } } },
133 23, BINARY_ARRAY "\0\0\0\x2" "\0\0\0\x3" "\x1\x2\x3"
134 "\0\0\0\4" "\x42\x0\xa\x1b"
135 },
136 {
137 { SDB_TYPE_REGEX | SDB_TYPE_ARRAY, { .array = {
138 1, regex_values } } },
139 24, REGEX_ARRAY "\0\0\0\1" "\0\0\0\xc" "dummy regex\0"
140 },
141 };
143 size_t i;
145 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
146 ssize_t len = sdb_proto_marshal_data(NULL, 0, &golden_data[i].datum);
147 char buf[len > 0 ? len : 1];
148 char v[sdb_data_strlen(&golden_data[i].datum)];
150 if (sdb_data_format(&golden_data[i].datum, v, sizeof(v), 0) < 0)
151 snprintf(v, sizeof(v), "<ERR>");
153 fail_unless(len == golden_data[i].expected_len,
154 "sdb_proto_marshal_data(NULL, 0, %s) = %zi; expected: %zi",
155 v, len, golden_data[i].expected_len);
157 if (len < 0)
158 continue;
160 len = sdb_proto_marshal_data(buf, sizeof(buf), &golden_data[i].datum);
161 fail_unless(len == golden_data[i].expected_len,
162 "sdb_proto_marshal_data(<buf>, <size>, %s) = %zi; expected: %zi",
163 v, len, golden_data[i].expected_len);
164 if (memcmp(buf, golden_data[i].expected, len) != 0) {
165 size_t pos;
166 for (pos = 0; pos < (size_t)len; ++pos)
167 if (buf[pos] != golden_data[i].expected[pos])
168 break;
169 fail("sdb_proto_marshal_data(%s) -> \"%s\"; expected: \"%s\" "
170 "(bytes %zu differ: '%x' != '%x')",
171 v, buf, golden_data[i].expected,
172 pos, (int)buf[pos], (int)golden_data[i].expected[pos]);
173 }
174 }
175 }
176 END_TEST
178 Suite *
179 util_proto_suite(void)
180 {
181 Suite *s = suite_create("utils::proto");
182 TCase *tc;
184 tc = tcase_create("core");
185 tcase_add_test(tc, test_marshal_data);
186 suite_add_tcase(s, tc);
188 return s;
189 } /* util_proto_suite */
191 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */