Code

proto: Don't include a string's length in the wire format.
[sysdb.git] / t / unit / utils / proto_test.c
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                         16, STRING_TYPE "some string\0",
91                 },
92                 {
93                         { SDB_TYPE_DATETIME, { .datetime = 1418923804000000 } },
94                         12, DATETIME_TYPE "\x0\x5\xa\x80\xf1\x4c\xff\x0",
95                 },
96                 {
97                         { SDB_TYPE_BINARY, { .binary = {
98                                 4, (unsigned char *)"\x42\x0\xa\x1b" } } },
99                         12, BINARY_TYPE "\0\0\0\x4" "\x42\x0\xa\x1b",
100                 },
101                 {
102                         { SDB_TYPE_REGEX, { .re = { "dummy", dummy_re } } },
103                         10, REGEX_TYPE "dummy\0",
104                 },
105                 {
106                         { SDB_TYPE_INTEGER | SDB_TYPE_ARRAY, { .array = {
107                                 3, int_values } } },
108                         32, INT_ARRAY "\0\0\0\x3" "\0\0\0\0\0\0\0\x2f"
109                                 "\0\0\0\0\0\0\0\xb" "\0\0\0\0\0\0\0\x17"
110                 },
111                 {
112                         { SDB_TYPE_DECIMAL | SDB_TYPE_ARRAY, { .array = {
113                                 2, dec_values } } },
114                         24, DECIMAL_ARRAY "\0\0\0\x2" "\x40\x47\x8e\x14\x7a\xe1\x47\xae"
115                                 "\x3f\xe0\0\0\0\0\0\0"
116                 },
117                 {
118                         { SDB_TYPE_STRING | SDB_TYPE_ARRAY, { .array = {
119                                 2, string_values } } },
120                         17, STRING_ARRAY "\0\0\0\x2" "foo\0" "abcd\0"
121                 },
122                 {
123                         { SDB_TYPE_DATETIME | SDB_TYPE_ARRAY, { .array = {
124                                 2, datetime_values } } },
125                         24, DATETIME_ARRAY "\0\0\0\x2" "\0\0\0\0\0\0\x12\x67"
126                                 "\x11\x22\x10\xf4\x7d\xe9\x81\x15"
127                 },
128                 {
129                         { SDB_TYPE_BINARY | SDB_TYPE_ARRAY, { .array = {
130                                 2, binary_values } } },
131                         23, BINARY_ARRAY "\0\0\0\x2" "\0\0\0\x3" "\x1\x2\x3"
132                                 "\0\0\0\4" "\x42\x0\xa\x1b"
133                 },
134                 {
135                         { SDB_TYPE_REGEX | SDB_TYPE_ARRAY, { .array = {
136                                 1, regex_values } } },
137                         20, REGEX_ARRAY "\0\0\0\1" "dummy regex\0"
138                 },
139         };
141         size_t i;
143         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
144                 ssize_t len = sdb_proto_marshal_data(NULL, 0, &golden_data[i].datum);
145                 char buf[len > 0 ? len : 1];
146                 char v[sdb_data_strlen(&golden_data[i].datum)];
148                 if (sdb_data_format(&golden_data[i].datum, v, sizeof(v), 0) < 0)
149                         snprintf(v, sizeof(v), "<ERR>");
151                 fail_unless(len == golden_data[i].expected_len,
152                                 "sdb_proto_marshal_data(NULL, 0, %s) = %zi; expected: %zi",
153                                 v, len, golden_data[i].expected_len);
155                 if (len < 0)
156                         continue;
158                 len = sdb_proto_marshal_data(buf, sizeof(buf), &golden_data[i].datum);
159                 fail_unless(len == golden_data[i].expected_len,
160                                 "sdb_proto_marshal_data(<buf>, %zu, %s) = %zi; expected: %zi",
161                                 sizeof(buf), v, len, golden_data[i].expected_len);
162                 if (memcmp(buf, golden_data[i].expected, len) != 0) {
163                         size_t pos;
164                         for (pos = 0; pos < (size_t)len; ++pos)
165                                 if (buf[pos] != golden_data[i].expected[pos])
166                                         break;
167                         fail("sdb_proto_marshal_data(%s) -> \"%s\"; expected: \"%s\" "
168                                         "(bytes %zu differ: '%x' != '%x')",
169                                         v, buf, golden_data[i].expected,
170                                         pos, (int)buf[pos], (int)golden_data[i].expected[pos]);
171                 }
172         }
174 END_TEST
176 Suite *
177 util_proto_suite(void)
179         Suite *s = suite_create("utils::proto");
180         TCase *tc;
182         tc = tcase_create("core");
183         tcase_add_test(tc, test_marshal_data);
184         suite_add_tcase(s, tc);
186         return s;
187 } /* util_proto_suite */
189 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */