1 /*
2 * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com>
3 *
4 * libcbor is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8 #include <setjmp.h>
9 #include <stdarg.h>
10 #include <stddef.h>
11
12 #include <cmocka.h>
13
14 #include <string.h>
15 #include "assertions.h"
16 #include "cbor.h"
17
18 cbor_item_t *map;
19 struct cbor_load_result res;
20
21 unsigned char empty_map[] = {0xA0};
22
test_empty_map(void ** state)23 static void test_empty_map(void **state) {
24 map = cbor_load(empty_map, 1, &res);
25 assert_non_null(map);
26 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
27 assert_true(cbor_isa_map(map));
28 assert_true(cbor_map_size(map) == 0);
29 assert_true(res.read == 1);
30 assert_int_equal(cbor_map_allocated(map), 0);
31 cbor_decref(&map);
32 assert_null(map);
33 }
34
35 unsigned char simple_map[] = {0xA2, 0x01, 0x02, 0x03, 0x04};
36
37 /* {1: 2, 3: 4} */
test_simple_map(void ** state)38 static void test_simple_map(void **state) {
39 map = cbor_load(simple_map, 5, &res);
40 assert_non_null(map);
41 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
42 assert_true(cbor_isa_map(map));
43 assert_true(cbor_map_is_definite(map));
44 assert_true(cbor_map_size(map) == 2);
45 assert_true(res.read == 5);
46 struct cbor_pair *handle = cbor_map_handle(map);
47 assert_uint8(handle[0].key, 1);
48 assert_uint8(handle[0].value, 2);
49 assert_uint8(handle[1].key, 3);
50 assert_uint8(handle[1].value, 4);
51 cbor_decref(&map);
52 assert_null(map);
53 }
54
55 unsigned char simple_indef_map[] = {0xBF, 0x01, 0x02, 0x03, 0x04, 0xFF};
56
57 /* {_ 1: 2, 3: 4} */
test_indef_simple_map(void ** state)58 static void test_indef_simple_map(void **state) {
59 map = cbor_load(simple_indef_map, 6, &res);
60 assert_non_null(map);
61 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
62 assert_true(cbor_isa_map(map));
63 assert_true(cbor_map_is_indefinite(map));
64 assert_true(cbor_map_size(map) == 2);
65 assert_true(res.read == 6);
66 struct cbor_pair *handle = cbor_map_handle(map);
67 assert_uint8(handle[0].key, 1);
68 assert_uint8(handle[0].value, 2);
69 assert_uint8(handle[1].key, 3);
70 assert_uint8(handle[1].value, 4);
71 cbor_decref(&map);
72 assert_null(map);
73 }
74
75 //{
76 // "glossary": {
77 // "title": "example glossary"
78 // }
79 //}
80 unsigned char def_nested_map[] = {
81 0xA1, 0x68, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79, 0xA1, 0x65,
82 0x74, 0x69, 0x74, 0x6C, 0x65, 0x70, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C,
83 0x65, 0x20, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79};
84
test_def_nested_map(void ** state)85 static void test_def_nested_map(void **state) {
86 map = cbor_load(def_nested_map, 34, &res);
87 assert_non_null(map);
88 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
89 assert_true(cbor_isa_map(map));
90 assert_true(cbor_map_is_definite(map));
91 assert_true(cbor_map_size(map) == 1);
92 assert_true(res.read == 34);
93 struct cbor_pair *handle = cbor_map_handle(map);
94 assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
95 assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_MAP);
96 struct cbor_pair *inner_handle = cbor_map_handle(handle[0].value);
97 assert_true(cbor_typeof(inner_handle[0].key) == CBOR_TYPE_STRING);
98 assert_true(cbor_typeof(inner_handle[0].value) == CBOR_TYPE_STRING);
99 assert_memory_equal(cbor_string_handle(inner_handle[0].value),
100 "example glossary", strlen("example glossary"));
101 cbor_decref(&map);
102 assert_null(map);
103 }
104
105 unsigned char streamed_key_map[] = {0xA1, 0x7F, 0x61, 0x61,
106 0x61, 0x62, 0xFF, 0xA0};
107
108 /* '{ (_"a" "b"): {}}' */
test_streamed_key_map(void ** state)109 static void test_streamed_key_map(void **state) {
110 map = cbor_load(streamed_key_map, 8, &res);
111 assert_non_null(map);
112 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
113 assert_true(cbor_isa_map(map));
114 assert_true(cbor_map_is_definite(map));
115 assert_true(cbor_map_size(map) == 1);
116 assert_true(res.read == 8);
117 struct cbor_pair *handle = cbor_map_handle(map);
118 assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
119 assert_true(cbor_string_is_indefinite(handle[0].key));
120 assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
121 assert_true(cbor_isa_map(handle[0].value));
122 assert_int_equal(cbor_map_size(handle[0].value), 0);
123 cbor_decref(&map);
124 assert_null(map);
125 }
126
127 unsigned char streamed_kv_map[] = {0xA1, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF,
128 0x7F, 0x61, 0x63, 0x61, 0x64, 0xFF};
129
130 /* '{ (_"a" "b"): (_"c", "d")}' */
test_streamed_kv_map(void ** state)131 static void test_streamed_kv_map(void **state) {
132 map = cbor_load(streamed_kv_map, 13, &res);
133 assert_non_null(map);
134 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
135 assert_true(cbor_isa_map(map));
136 assert_true(cbor_map_is_definite(map));
137 assert_int_equal(cbor_map_size(map), 1);
138 assert_int_equal(res.read, 13);
139 struct cbor_pair *handle = cbor_map_handle(map);
140 assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
141 assert_true(cbor_string_is_indefinite(handle[0].key));
142 assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
143 assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
144 assert_true(cbor_string_is_indefinite(handle[0].value));
145 assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
146 assert_memory_equal(
147 cbor_string_handle(cbor_string_chunks_handle(handle[0].value)[1]), "d",
148 1);
149 cbor_decref(&map);
150 assert_null(map);
151 }
152
153 unsigned char streamed_streamed_kv_map[] = {0xBF, 0x7F, 0x61, 0x61, 0x61,
154 0x62, 0xFF, 0x7F, 0x61, 0x63,
155 0x61, 0x64, 0xFF, 0xFF};
156
157 /* '{_ (_"a" "b"): (_"c", "d")}' */
test_streamed_streamed_kv_map(void ** state)158 static void test_streamed_streamed_kv_map(void **state) {
159 map = cbor_load(streamed_streamed_kv_map, 14, &res);
160 assert_non_null(map);
161 assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
162 assert_true(cbor_isa_map(map));
163 assert_true(cbor_map_is_indefinite(map));
164 assert_int_equal(cbor_map_size(map), 1);
165 assert_int_equal(res.read, 14);
166 struct cbor_pair *handle = cbor_map_handle(map);
167 assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
168 assert_true(cbor_string_is_indefinite(handle[0].key));
169 assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
170 assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
171 assert_true(cbor_string_is_indefinite(handle[0].value));
172 assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
173 assert_memory_equal(
174 cbor_string_handle(cbor_string_chunks_handle(handle[0].value)[1]), "d",
175 1);
176 cbor_decref(&map);
177 assert_null(map);
178 }
179
main(void)180 int main(void) {
181 const struct CMUnitTest tests[] = {
182 cmocka_unit_test(test_empty_map),
183 cmocka_unit_test(test_simple_map),
184 cmocka_unit_test(test_indef_simple_map),
185 cmocka_unit_test(test_def_nested_map),
186 cmocka_unit_test(test_streamed_key_map),
187 cmocka_unit_test(test_streamed_kv_map),
188 cmocka_unit_test(test_streamed_streamed_kv_map)};
189 return cmocka_run_group_tests(tests, NULL, NULL);
190 }
191