d0516b306b483cc4fe32c5cd502463014c9eff22
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 char *string_values[] = { "foo", "abcd" };
54 struct {
55 sdb_data_t datum;
56 ssize_t expected_len;
57 char *expected;
58 } golden_data[] = {
59 {
60 { SDB_TYPE_NULL, { .integer = 0 } },
61 4, "\0\0\0\0",
62 },
63 {
64 { SDB_TYPE_INTEGER, { .integer = 4711 } },
65 12, INT_TYPE "\0\0\0\0\0\0\x12\x67",
66 },
67 {
68 { SDB_TYPE_DECIMAL, { .integer = 4711 } },
69 -1, NULL, /* not supported yet */
70 },
71 {
72 { SDB_TYPE_STRING, { .string = "some string" } },
73 /* length includes the null byte */
74 20, STRING_TYPE "\0\0\0\xc" "some string\0",
75 },
76 {
77 { SDB_TYPE_DATETIME, { .datetime = 1418923804000000 } },
78 12, DATETIME_TYPE "\x0\x5\xa\x80\xf1\x4c\xff\x0",
79 },
80 {
81 { SDB_TYPE_BINARY, { .binary = {
82 4, (unsigned char *)"\x42\x0\xa\x1b" } } },
83 12, BINARY_TYPE "\0\0\0\x4" "\x42\x0\xa\x1b",
84 },
85 {
86 { SDB_TYPE_REGEX, { .re = { "dummy", dummy_re } } },
87 -1, NULL, /* not supported */
88 },
89 {
90 { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = {
91 3, int_values } } },
92 32, INT_ARRAY "\0\0\0\x3" "\0\0\0\0\0\0\0\x2f"
93 "\0\0\0\0\0\0\0\xb" "\0\0\0\0\0\0\0\x17"
94 },
95 {
96 { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = {
97 2, string_values } } },
98 25, STRING_ARRAY "\0\0\0\x2" "\0\0\0\x4" "foo\0"
99 "\0\0\0\x5" "abcd\0"
100 },
101 };
103 size_t i;
105 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
106 ssize_t len = sdb_proto_marshal_data(NULL, 0, &golden_data[i].datum);
107 char buf[len > 0 ? len : 1];
108 char v[sdb_data_strlen(&golden_data[i].datum)];
110 if (sdb_data_format(&golden_data[i].datum, v, sizeof(v), 0) < 0)
111 snprintf(v, sizeof(v), "<ERR>");
113 fail_unless(len == golden_data[i].expected_len,
114 "sdb_proto_marshal_data(NULL, 0, %s) = %zi; expected: %zi",
115 v, len, golden_data[i].expected_len);
117 if (len < 0)
118 continue;
120 len = sdb_proto_marshal_data(buf, sizeof(buf), &golden_data[i].datum);
121 fail_unless(len == golden_data[i].expected_len,
122 "sdb_proto_marshal_data(<buf>, <size>, %s) = %zi; expected: %zi",
123 v, len, golden_data[i].expected_len);
124 if (memcmp(buf, golden_data[i].expected, len) != 0) {
125 size_t pos;
126 for (pos = 0; pos < (size_t)len; ++pos)
127 if (buf[pos] != golden_data[i].expected[pos])
128 break;
129 fail("sdb_proto_marshal_data(%s) -> \"%s\"; expected: \"%s\" "
130 "(bytes %zu differ: '%x' != '%x')",
131 v, buf, golden_data[i].expected,
132 pos, (int)buf[pos], (int)golden_data[i].expected[pos]);
133 }
134 }
135 }
136 END_TEST
138 Suite *
139 util_proto_suite(void)
140 {
141 Suite *s = suite_create("utils::proto");
142 TCase *tc;
144 tc = tcase_create("core");
145 tcase_add_test(tc, test_marshal_data);
146 suite_add_tcase(s, tc);
148 return s;
149 } /* util_proto_suite */
151 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */