1ec0615fa33fd7a7f29c2796a9c2c369e92afebe
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"
43 #define NULL_ARRAY "\0\0\1\0"
44 #define INT_ARRAY "\0\0\1\1"
45 #define DECIMAL_ARRAY "\0\0\1\2"
46 #define STRING_ARRAY "\0\0\1\3"
47 #define DATETIME_ARRAY "\0\0\1\4"
48 #define BINARY_ARRAY "\0\0\1\5"
50 regex_t dummy_re;
51 int64_t int_values[] = { 47, 11, 23 };
52 double dec_values[] = { 47.11, .5 };
53 char *string_values[] = { "foo", "abcd" };
55 struct {
56 sdb_data_t datum;
57 ssize_t expected_len;
58 char *expected;
59 } golden_data[] = {
60 {
61 { SDB_TYPE_NULL, { .integer = 0 } },
62 4, "\0\0\0\0",
63 },
64 {
65 { SDB_TYPE_INTEGER, { .integer = 4711 } },
66 12, INT_TYPE "\0\0\0\0\0\0\x12\x67",
67 },
68 {
69 { SDB_TYPE_DECIMAL, { .decimal = 3.141592653e130 } },
70 12, DECIMAL_TYPE "\x5b\x6\xa9\x40\x66\x1e\x10\x4",
71 },
72 {
73 { SDB_TYPE_STRING, { .string = "some string" } },
74 /* length includes the null byte */
75 20, STRING_TYPE "\0\0\0\xc" "some string\0",
76 },
77 {
78 { SDB_TYPE_DATETIME, { .datetime = 1418923804000000 } },
79 12, DATETIME_TYPE "\x0\x5\xa\x80\xf1\x4c\xff\x0",
80 },
81 {
82 { SDB_TYPE_BINARY, { .binary = {
83 4, (unsigned char *)"\x42\x0\xa\x1b" } } },
84 12, BINARY_TYPE "\0\0\0\x4" "\x42\x0\xa\x1b",
85 },
86 {
87 { SDB_TYPE_REGEX, { .re = { "dummy", dummy_re } } },
88 -1, NULL, /* not supported */
89 },
90 {
91 { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = {
92 3, int_values } } },
93 32, INT_ARRAY "\0\0\0\x3" "\0\0\0\0\0\0\0\x2f"
94 "\0\0\0\0\0\0\0\xb" "\0\0\0\0\0\0\0\x17"
95 },
96 {
97 { SDB_TYPE_DECIMAL | SDB_TYPE_ARRAY, { .array = {
98 2, dec_values } } },
99 24, DECIMAL_ARRAY "\0\0\0\x2" "\x40\x47\x8e\x14\x7a\xe1\x47\xae"
100 "\x3f\xe0\0\0\0\0\0\0"
101 },
102 {
103 { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = {
104 2, string_values } } },
105 25, STRING_ARRAY "\0\0\0\x2" "\0\0\0\x4" "foo\0"
106 "\0\0\0\x5" "abcd\0"
107 },
108 };
110 size_t i;
112 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
113 ssize_t len = sdb_proto_marshal_data(NULL, 0, &golden_data[i].datum);
114 char buf[len > 0 ? len : 1];
115 char v[sdb_data_strlen(&golden_data[i].datum)];
117 if (sdb_data_format(&golden_data[i].datum, v, sizeof(v), 0) < 0)
118 snprintf(v, sizeof(v), "<ERR>");
120 fail_unless(len == golden_data[i].expected_len,
121 "sdb_proto_marshal_data(NULL, 0, %s) = %zi; expected: %zi",
122 v, len, golden_data[i].expected_len);
124 if (len < 0)
125 continue;
127 len = sdb_proto_marshal_data(buf, sizeof(buf), &golden_data[i].datum);
128 fail_unless(len == golden_data[i].expected_len,
129 "sdb_proto_marshal_data(<buf>, <size>, %s) = %zi; expected: %zi",
130 v, len, golden_data[i].expected_len);
131 if (memcmp(buf, golden_data[i].expected, len) != 0) {
132 size_t pos;
133 for (pos = 0; pos < (size_t)len; ++pos)
134 if (buf[pos] != golden_data[i].expected[pos])
135 break;
136 fail("sdb_proto_marshal_data(%s) -> \"%s\"; expected: \"%s\" "
137 "(bytes %zu differ: '%x' != '%x')",
138 v, buf, golden_data[i].expected,
139 pos, (int)buf[pos], (int)golden_data[i].expected[pos]);
140 }
141 }
142 }
143 END_TEST
145 Suite *
146 util_proto_suite(void)
147 {
148 Suite *s = suite_create("utils::proto");
149 TCase *tc;
151 tc = tcase_create("core");
152 tcase_add_test(tc, test_marshal_data);
153 suite_add_tcase(s, tc);
155 return s;
156 } /* util_proto_suite */
158 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */