xref: /freebsd/contrib/libcbor/test/copy_test.c (revision 1b10e191f341111fad7be32ead11484dfd09b800)
1 /*
2  * Copyright (c) 2014-2020 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 "assertions.h"
15 #include "cbor.h"
16 
17 cbor_item_t *item, *copy, *tmp;
18 
19 static void test_uints(void **state) {
20   item = cbor_build_uint8(10);
21   assert_uint8(copy = cbor_copy(item), 10);
22   cbor_decref(&item);
23   cbor_decref(&copy);
24 
25   item = cbor_build_uint16(10);
26   assert_uint16(copy = cbor_copy(item), 10);
27   cbor_decref(&item);
28   cbor_decref(&copy);
29 
30   item = cbor_build_uint32(10);
31   assert_uint32(copy = cbor_copy(item), 10);
32   cbor_decref(&item);
33   cbor_decref(&copy);
34 
35   item = cbor_build_uint64(10);
36   assert_uint64(copy = cbor_copy(item), 10);
37   cbor_decref(&item);
38   cbor_decref(&copy);
39 }
40 
41 static void test_negints(void **state) {
42   item = cbor_build_negint8(10);
43   assert_true(cbor_get_uint8(copy = cbor_copy(item)) == 10);
44   cbor_decref(&item);
45   cbor_decref(&copy);
46 
47   item = cbor_build_negint16(10);
48   assert_true(cbor_get_uint16(copy = cbor_copy(item)) == 10);
49   cbor_decref(&item);
50   cbor_decref(&copy);
51 
52   item = cbor_build_negint32(10);
53   assert_true(cbor_get_uint32(copy = cbor_copy(item)) == 10);
54   cbor_decref(&item);
55   cbor_decref(&copy);
56 
57   item = cbor_build_negint64(10);
58   assert_true(cbor_get_uint64(copy = cbor_copy(item)) == 10);
59   cbor_decref(&item);
60   cbor_decref(&copy);
61 }
62 
63 static void test_def_bytestring(void **state) {
64   item = cbor_build_bytestring((cbor_data) "abc", 3);
65   assert_memory_equal(cbor_bytestring_handle(copy = cbor_copy(item)),
66                       cbor_bytestring_handle(item), 3);
67   cbor_decref(&item);
68   cbor_decref(&copy);
69 }
70 
71 static void test_indef_bytestring(void **state) {
72   item = cbor_new_indefinite_bytestring();
73   cbor_bytestring_add_chunk(
74       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3)));
75   copy = cbor_copy(item);
76 
77   assert_int_equal(cbor_bytestring_chunk_count(item),
78                    cbor_bytestring_chunk_count(copy));
79 
80   assert_memory_equal(
81       cbor_bytestring_handle(cbor_bytestring_chunks_handle(copy)[0]), "abc", 3);
82   cbor_decref(&item);
83   cbor_decref(&copy);
84 }
85 
86 static void test_def_string(void **state) {
87   item = cbor_build_string("abc");
88   assert_memory_equal(cbor_string_handle(copy = cbor_copy(item)),
89                       cbor_string_handle(item), 3);
90   cbor_decref(&item);
91   cbor_decref(&copy);
92 }
93 
94 static void test_indef_string(void **state) {
95   item = cbor_new_indefinite_string();
96   cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc")));
97   copy = cbor_copy(item);
98 
99   assert_int_equal(cbor_string_chunk_count(item),
100                    cbor_string_chunk_count(copy));
101 
102   assert_memory_equal(cbor_string_handle(cbor_string_chunks_handle(copy)[0]),
103                       "abc", 3);
104   cbor_decref(&item);
105   cbor_decref(&copy);
106 }
107 
108 static void test_def_array(void **state) {
109   item = cbor_new_definite_array(1);
110   cbor_array_push(item, cbor_move(cbor_build_uint8(42)));
111 
112   assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
113   cbor_decref(&item);
114   cbor_decref(&copy);
115   cbor_decref(&tmp);
116 }
117 
118 static void test_indef_array(void **state) {
119   item = cbor_new_indefinite_array();
120   cbor_array_push(item, cbor_move(cbor_build_uint8(42)));
121 
122   assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
123   cbor_decref(&item);
124   cbor_decref(&copy);
125   cbor_decref(&tmp);
126 }
127 
128 static void test_def_map(void **state) {
129   item = cbor_new_definite_map(1);
130   cbor_map_add(item, (struct cbor_pair){
131                          .key = cbor_move(cbor_build_uint8(42)),
132                          .value = cbor_move(cbor_build_uint8(43)),
133                      });
134 
135   assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
136 
137   cbor_decref(&item);
138   cbor_decref(&copy);
139 }
140 
141 static void test_indef_map(void **state) {
142   item = cbor_new_indefinite_map(1);
143   cbor_map_add(item, (struct cbor_pair){
144                          .key = cbor_move(cbor_build_uint8(42)),
145                          .value = cbor_move(cbor_build_uint8(43)),
146                      });
147 
148   assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
149 
150   cbor_decref(&item);
151   cbor_decref(&copy);
152 }
153 
154 static void test_tag(void **state) {
155   item = cbor_build_tag(10, cbor_move(cbor_build_uint8(42)));
156 
157   assert_uint8(cbor_move(cbor_tag_item(copy = cbor_copy(item))), 42);
158 
159   cbor_decref(&item);
160   cbor_decref(&copy);
161 }
162 
163 static void test_ctrls(void **state) {
164   item = cbor_new_null();
165   assert_true(cbor_is_null(copy = cbor_copy(item)));
166   cbor_decref(&item);
167   cbor_decref(&copy);
168 }
169 
170 static void test_floats(void **state) {
171   item = cbor_build_float2(3.14f);
172   assert_true(cbor_float_get_float2(copy = cbor_copy(item)) ==
173               cbor_float_get_float2(item));
174   cbor_decref(&item);
175   cbor_decref(&copy);
176 
177   item = cbor_build_float4(3.14f);
178   assert_true(cbor_float_get_float4(copy = cbor_copy(item)) ==
179               cbor_float_get_float4(item));
180   cbor_decref(&item);
181   cbor_decref(&copy);
182 
183   item = cbor_build_float8(3.14);
184   assert_true(cbor_float_get_float8(copy = cbor_copy(item)) ==
185               cbor_float_get_float8(item));
186   cbor_decref(&item);
187   cbor_decref(&copy);
188 }
189 
190 int main(void) {
191   const struct CMUnitTest tests[] = {
192 
193       cmocka_unit_test(test_uints),
194       cmocka_unit_test(test_negints),
195       cmocka_unit_test(test_def_bytestring),
196       cmocka_unit_test(test_indef_bytestring),
197       cmocka_unit_test(test_def_string),
198       cmocka_unit_test(test_indef_string),
199       cmocka_unit_test(test_def_array),
200       cmocka_unit_test(test_indef_array),
201       cmocka_unit_test(test_def_map),
202       cmocka_unit_test(test_indef_map),
203       cmocka_unit_test(test_tag),
204       cmocka_unit_test(test_ctrls),
205       cmocka_unit_test(test_floats)};
206   return cmocka_run_group_tests(tests, NULL, NULL);
207 }
208