Code

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