xref: /freebsd/contrib/libcbor/test/copy_test.c (revision b5b9517bfe394e55088f5a05882eabae7e9b7b29)
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 "assertions.h"
9 #include "cbor.h"
10 #include "test_allocator.h"
11 
12 cbor_item_t *item, *copy, *tmp;
13 
test_uints(void ** _state _CBOR_UNUSED)14 static void test_uints(void** _state _CBOR_UNUSED) {
15   item = cbor_build_uint8(10);
16   assert_uint8(copy = cbor_copy(item), 10);
17   cbor_decref(&item);
18   cbor_decref(&copy);
19 
20   item = cbor_build_uint16(10);
21   assert_uint16(copy = cbor_copy(item), 10);
22   cbor_decref(&item);
23   cbor_decref(&copy);
24 
25   item = cbor_build_uint32(10);
26   assert_uint32(copy = cbor_copy(item), 10);
27   cbor_decref(&item);
28   cbor_decref(&copy);
29 
30   item = cbor_build_uint64(10);
31   assert_uint64(copy = cbor_copy(item), 10);
32   cbor_decref(&item);
33   cbor_decref(&copy);
34 }
35 
test_negints(void ** _state _CBOR_UNUSED)36 static void test_negints(void** _state _CBOR_UNUSED) {
37   item = cbor_build_negint8(10);
38   assert_true(cbor_get_uint8(copy = cbor_copy(item)) == 10);
39   cbor_decref(&item);
40   cbor_decref(&copy);
41 
42   item = cbor_build_negint16(10);
43   assert_true(cbor_get_uint16(copy = cbor_copy(item)) == 10);
44   cbor_decref(&item);
45   cbor_decref(&copy);
46 
47   item = cbor_build_negint32(10);
48   assert_true(cbor_get_uint32(copy = cbor_copy(item)) == 10);
49   cbor_decref(&item);
50   cbor_decref(&copy);
51 
52   item = cbor_build_negint64(10);
53   assert_true(cbor_get_uint64(copy = cbor_copy(item)) == 10);
54   cbor_decref(&item);
55   cbor_decref(&copy);
56 }
57 
test_def_bytestring(void ** _state _CBOR_UNUSED)58 static void test_def_bytestring(void** _state _CBOR_UNUSED) {
59   item = cbor_build_bytestring((cbor_data) "abc", 3);
60   assert_memory_equal(cbor_bytestring_handle(copy = cbor_copy(item)),
61                       cbor_bytestring_handle(item), 3);
62   cbor_decref(&item);
63   cbor_decref(&copy);
64 }
65 
test_indef_bytestring(void ** _state _CBOR_UNUSED)66 static void test_indef_bytestring(void** _state _CBOR_UNUSED) {
67   item = cbor_new_indefinite_bytestring();
68   assert_true(cbor_bytestring_add_chunk(
69       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
70   copy = cbor_copy(item);
71 
72   assert_size_equal(cbor_bytestring_chunk_count(item),
73                     cbor_bytestring_chunk_count(copy));
74 
75   assert_memory_equal(
76       cbor_bytestring_handle(cbor_bytestring_chunks_handle(copy)[0]), "abc", 3);
77   cbor_decref(&item);
78   cbor_decref(&copy);
79 }
80 
test_def_string(void ** _state _CBOR_UNUSED)81 static void test_def_string(void** _state _CBOR_UNUSED) {
82   item = cbor_build_string("abc");
83   assert_memory_equal(cbor_string_handle(copy = cbor_copy(item)),
84                       cbor_string_handle(item), 3);
85   cbor_decref(&item);
86   cbor_decref(&copy);
87 }
88 
test_indef_string(void ** _state _CBOR_UNUSED)89 static void test_indef_string(void** _state _CBOR_UNUSED) {
90   item = cbor_new_indefinite_string();
91   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
92   copy = cbor_copy(item);
93 
94   assert_size_equal(cbor_string_chunk_count(item),
95                     cbor_string_chunk_count(copy));
96 
97   assert_memory_equal(cbor_string_handle(cbor_string_chunks_handle(copy)[0]),
98                       "abc", 3);
99   cbor_decref(&item);
100   cbor_decref(&copy);
101 }
102 
test_def_array(void ** _state _CBOR_UNUSED)103 static void test_def_array(void** _state _CBOR_UNUSED) {
104   item = cbor_new_definite_array(1);
105   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
106 
107   assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
108   cbor_decref(&item);
109   cbor_decref(&copy);
110   cbor_decref(&tmp);
111 }
112 
test_indef_array(void ** _state _CBOR_UNUSED)113 static void test_indef_array(void** _state _CBOR_UNUSED) {
114   item = cbor_new_indefinite_array();
115   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
116 
117   assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
118   cbor_decref(&item);
119   cbor_decref(&copy);
120   cbor_decref(&tmp);
121 }
122 
test_def_map(void ** _state _CBOR_UNUSED)123 static void test_def_map(void** _state _CBOR_UNUSED) {
124   item = cbor_new_definite_map(1);
125   assert_true(cbor_map_add(item, (struct cbor_pair){
126                                      .key = cbor_move(cbor_build_uint8(42)),
127                                      .value = cbor_move(cbor_build_uint8(43)),
128                                  }));
129 
130   assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
131 
132   cbor_decref(&item);
133   cbor_decref(&copy);
134 }
135 
test_indef_map(void ** _state _CBOR_UNUSED)136 static void test_indef_map(void** _state _CBOR_UNUSED) {
137   item = cbor_new_indefinite_map();
138   assert_true(cbor_map_add(item, (struct cbor_pair){
139                                      .key = cbor_move(cbor_build_uint8(42)),
140                                      .value = cbor_move(cbor_build_uint8(43)),
141                                  }));
142 
143   assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
144 
145   cbor_decref(&item);
146   cbor_decref(&copy);
147 }
148 
test_tag(void ** _state _CBOR_UNUSED)149 static void test_tag(void** _state _CBOR_UNUSED) {
150   item = cbor_build_tag(10, cbor_move(cbor_build_uint8(42)));
151 
152   assert_uint8(cbor_move(cbor_tag_item(copy = cbor_copy(item))), 42);
153 
154   cbor_decref(&item);
155   cbor_decref(&copy);
156 }
157 
test_ctrls(void ** _state _CBOR_UNUSED)158 static void test_ctrls(void** _state _CBOR_UNUSED) {
159   item = cbor_new_null();
160   assert_true(cbor_is_null(copy = cbor_copy(item)));
161   cbor_decref(&item);
162   cbor_decref(&copy);
163 }
164 
test_floats(void ** _state _CBOR_UNUSED)165 static void test_floats(void** _state _CBOR_UNUSED) {
166   item = cbor_build_float2(3.14f);
167   assert_true(cbor_float_get_float2(copy = cbor_copy(item)) ==
168               cbor_float_get_float2(item));
169   cbor_decref(&item);
170   cbor_decref(&copy);
171 
172   item = cbor_build_float4(3.14f);
173   assert_true(cbor_float_get_float4(copy = cbor_copy(item)) ==
174               cbor_float_get_float4(item));
175   cbor_decref(&item);
176   cbor_decref(&copy);
177 
178   item = cbor_build_float8(3.14);
179   assert_true(cbor_float_get_float8(copy = cbor_copy(item)) ==
180               cbor_float_get_float8(item));
181   cbor_decref(&item);
182   cbor_decref(&copy);
183 }
184 
test_definite_uints(void ** _state _CBOR_UNUSED)185 static void test_definite_uints(void** _state _CBOR_UNUSED) {
186   item = cbor_build_uint8(10);
187   assert_uint8(copy = cbor_copy_definite(item), 10);
188   cbor_decref(&item);
189   cbor_decref(&copy);
190 }
191 
test_definite_negints(void ** _state _CBOR_UNUSED)192 static void test_definite_negints(void** _state _CBOR_UNUSED) {
193   item = cbor_build_negint16(10);
194   assert_true(cbor_get_uint16(copy = cbor_copy_definite(item)) == 10);
195   cbor_decref(&item);
196   cbor_decref(&copy);
197 }
198 
test_definite_bytestring(void ** _state _CBOR_UNUSED)199 static void test_definite_bytestring(void** _state _CBOR_UNUSED) {
200   item = cbor_build_bytestring((cbor_data) "abc", 3);
201   assert_memory_equal(cbor_bytestring_handle(copy = cbor_copy_definite(item)),
202                       cbor_bytestring_handle(item), 3);
203   cbor_decref(&item);
204   cbor_decref(&copy);
205 }
206 
test_definite_indef_bytestring(void ** _state _CBOR_UNUSED)207 static void test_definite_indef_bytestring(void** _state _CBOR_UNUSED) {
208   item = cbor_new_indefinite_bytestring();
209   assert_true(cbor_bytestring_add_chunk(
210       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
211 
212   assert_memory_equal(cbor_bytestring_handle(copy = cbor_copy_definite(item)),
213                       "abc", 3);
214   assert_true(cbor_isa_bytestring(copy));
215   assert_true(cbor_bytestring_is_definite(copy));
216   assert_size_equal(cbor_bytestring_length(copy), 3);
217 
218   cbor_decref(&item);
219   cbor_decref(&copy);
220 }
221 
test_definite_bytestring_alloc_failure(void ** _state _CBOR_UNUSED)222 static void test_definite_bytestring_alloc_failure(void** _state _CBOR_UNUSED) {
223   item = cbor_new_indefinite_bytestring();
224   assert_true(cbor_bytestring_add_chunk(
225       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
226 
227   WITH_FAILING_MALLOC({ assert_null(cbor_copy_definite(item)); });
228   assert_size_equal(cbor_refcount(item), 1);
229 
230   cbor_decref(&item);
231 }
232 
test_definite_string(void ** _state _CBOR_UNUSED)233 static void test_definite_string(void** _state _CBOR_UNUSED) {
234   item = cbor_build_string("abc");
235   assert_memory_equal(cbor_string_handle(copy = cbor_copy_definite(item)),
236                       cbor_string_handle(item), 3);
237   cbor_decref(&item);
238   cbor_decref(&copy);
239 }
240 
test_definite_indef_string(void ** _state _CBOR_UNUSED)241 static void test_definite_indef_string(void** _state _CBOR_UNUSED) {
242   item = cbor_new_indefinite_string();
243   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
244 
245   assert_memory_equal(cbor_string_handle(copy = cbor_copy_definite(item)),
246                       "abc", 3);
247   assert_true(cbor_isa_string(copy));
248   assert_true(cbor_string_is_definite(copy));
249   assert_size_equal(cbor_string_length(copy), 3);
250 
251   cbor_decref(&item);
252   cbor_decref(&copy);
253 }
254 
test_definite_string_alloc_failure(void ** _state _CBOR_UNUSED)255 static void test_definite_string_alloc_failure(void** _state _CBOR_UNUSED) {
256   item = cbor_new_indefinite_string();
257   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
258 
259   WITH_FAILING_MALLOC({ assert_null(cbor_copy_definite(item)); });
260   assert_size_equal(cbor_refcount(item), 1);
261 
262   cbor_decref(&item);
263 }
264 
test_definite_array(void ** _state _CBOR_UNUSED)265 static void test_definite_array(void** _state _CBOR_UNUSED) {
266   item = cbor_new_definite_array(1);
267   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
268 
269   copy = cbor_copy_definite(item);
270   assert_true(cbor_isa_array(copy));
271   assert_true(cbor_array_is_definite(copy));
272   assert_size_equal(cbor_array_size(copy), 1);
273   assert_uint8(tmp = cbor_array_get(copy, 0), 42);
274 
275   cbor_decref(&item);
276   cbor_decref(&copy);
277   cbor_decref(&tmp);
278 }
279 
test_definite_indef_array(void ** _state _CBOR_UNUSED)280 static void test_definite_indef_array(void** _state _CBOR_UNUSED) {
281   item = cbor_new_indefinite_array();
282   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
283 
284   copy = cbor_copy_definite(item);
285   assert_true(cbor_isa_array(copy));
286   assert_true(cbor_array_is_definite(copy));
287   assert_uint8(tmp = cbor_array_get(copy, 0), 42);
288 
289   cbor_decref(&item);
290   cbor_decref(&copy);
291   cbor_decref(&tmp);
292 }
293 
test_definite_indef_array_nested(void ** _state _CBOR_UNUSED)294 static void test_definite_indef_array_nested(void** _state _CBOR_UNUSED) {
295   item = cbor_new_indefinite_array();
296   cbor_item_t* nested_array = cbor_new_indefinite_array();
297   assert_true(cbor_array_push(item, cbor_move(nested_array)));
298 
299   copy = cbor_copy_definite(item);
300   assert_true(cbor_isa_array(copy));
301   assert_true(cbor_array_is_definite(copy));
302   assert_size_equal(cbor_array_size(copy), 1);
303 
304   tmp = cbor_array_get(copy, 0);
305   assert_true(cbor_isa_array(tmp));
306   assert_true(cbor_array_is_definite(tmp));
307   assert_size_equal(cbor_array_size(tmp), 0);
308 
309   cbor_decref(&item);
310   cbor_decref(&copy);
311   cbor_decref(&tmp);
312 }
313 
test_definite_array_alloc_failure(void ** _state _CBOR_UNUSED)314 static void test_definite_array_alloc_failure(void** _state _CBOR_UNUSED) {
315   item = cbor_new_indefinite_array();
316   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
317 
318   WITH_FAILING_MALLOC({ assert_null(cbor_copy_definite(item)); });
319   assert_size_equal(cbor_refcount(item), 1);
320 
321   cbor_decref(&item);
322 }
323 
test_definite_array_item_alloc_failure(void ** _state _CBOR_UNUSED)324 static void test_definite_array_item_alloc_failure(void** _state _CBOR_UNUSED) {
325   item = cbor_new_indefinite_array();
326   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
327 
328   WITH_MOCK_MALLOC({ assert_null(cbor_copy_definite(item)); }, 3,
329                    // New array, new array data, item copy
330                    MALLOC, MALLOC, MALLOC_FAIL);
331 
332   assert_size_equal(cbor_refcount(item), 1);
333 
334   cbor_decref(&item);
335 }
336 
test_definite_map(void ** _state _CBOR_UNUSED)337 static void test_definite_map(void** _state _CBOR_UNUSED) {
338   item = cbor_new_definite_map(1);
339   assert_true(cbor_map_add(item, (struct cbor_pair){
340                                      .key = cbor_move(cbor_build_uint8(42)),
341                                      .value = cbor_move(cbor_build_uint8(43)),
342                                  }));
343 
344   copy = cbor_copy_definite(item);
345   assert_true(cbor_isa_map(copy));
346   assert_true(cbor_map_is_definite(copy));
347   assert_size_equal(cbor_map_size(copy), 1);
348   assert_uint8(cbor_map_handle(copy)[0].key, 42);
349 
350   cbor_decref(&item);
351   cbor_decref(&copy);
352 }
353 
test_definite_indef_map(void ** _state _CBOR_UNUSED)354 static void test_definite_indef_map(void** _state _CBOR_UNUSED) {
355   item = cbor_new_indefinite_map();
356   assert_true(cbor_map_add(item, (struct cbor_pair){
357                                      .key = cbor_move(cbor_build_uint8(42)),
358                                      .value = cbor_move(cbor_build_uint8(43)),
359                                  }));
360 
361   copy = cbor_copy_definite(item);
362   assert_true(cbor_isa_map(copy));
363   assert_true(cbor_map_is_definite(copy));
364   assert_size_equal(cbor_map_size(copy), 1);
365   assert_uint8(cbor_map_handle(copy)[0].key, 42);
366 
367   cbor_decref(&item);
368   cbor_decref(&copy);
369 }
370 
test_definite_indef_map_nested(void ** _state _CBOR_UNUSED)371 static void test_definite_indef_map_nested(void** _state _CBOR_UNUSED) {
372   item = cbor_new_indefinite_map();
373   cbor_item_t* key = cbor_new_indefinite_array();
374   cbor_item_t* value = cbor_new_indefinite_array();
375   assert_true(cbor_map_add(item, (struct cbor_pair){
376                                      .key = cbor_move(key),
377                                      .value = cbor_move(value),
378                                  }));
379 
380   copy = cbor_copy_definite(item);
381   assert_true(cbor_isa_map(copy));
382   assert_true(cbor_map_is_definite(copy));
383   assert_size_equal(cbor_map_size(copy), 1);
384 
385   assert_true(cbor_isa_array(cbor_map_handle(copy)[0].key));
386   assert_true(cbor_array_is_definite(cbor_map_handle(copy)[0].key));
387   assert_size_equal(cbor_array_size(cbor_map_handle(copy)[0].key), 0);
388 
389   assert_true(cbor_isa_array(cbor_map_handle(copy)[0].value));
390   assert_true(cbor_array_is_definite(cbor_map_handle(copy)[0].value));
391   assert_size_equal(cbor_array_size(cbor_map_handle(copy)[0].value), 0);
392 
393   cbor_decref(&item);
394   cbor_decref(&copy);
395 }
396 
test_definite_map_alloc_failure(void ** _state _CBOR_UNUSED)397 static void test_definite_map_alloc_failure(void** _state _CBOR_UNUSED) {
398   item = cbor_new_indefinite_map();
399   assert_true(cbor_map_add(item, (struct cbor_pair){
400                                      .key = cbor_move(cbor_build_uint8(42)),
401                                      .value = cbor_move(cbor_build_uint8(43)),
402                                  }));
403 
404   WITH_FAILING_MALLOC({ assert_null(cbor_copy_definite(item)); });
405   assert_size_equal(cbor_refcount(item), 1);
406 
407   cbor_decref(&item);
408 }
409 
test_definite_map_key_alloc_failure(void ** _state _CBOR_UNUSED)410 static void test_definite_map_key_alloc_failure(void** _state _CBOR_UNUSED) {
411   item = cbor_new_indefinite_map();
412   assert_true(cbor_map_add(item, (struct cbor_pair){
413                                      .key = cbor_move(cbor_build_uint8(42)),
414                                      .value = cbor_move(cbor_build_uint8(43)),
415                                  }));
416 
417   WITH_MOCK_MALLOC({ assert_null(cbor_copy_definite(item)); }, 3,
418                    // New map, map data, key copy
419                    MALLOC, MALLOC, MALLOC_FAIL);
420 
421   assert_size_equal(cbor_refcount(item), 1);
422 
423   cbor_decref(&item);
424 }
425 
test_definite_map_value_alloc_failure(void ** _state _CBOR_UNUSED)426 static void test_definite_map_value_alloc_failure(void** _state _CBOR_UNUSED) {
427   item = cbor_new_indefinite_map();
428   assert_true(cbor_map_add(item, (struct cbor_pair){
429                                      .key = cbor_move(cbor_build_uint8(42)),
430                                      .value = cbor_move(cbor_build_uint8(43)),
431                                  }));
432 
433   WITH_MOCK_MALLOC({ assert_null(cbor_copy_definite(item)); }, 4,
434                    // New map, map data, key copy, value copy
435                    MALLOC, MALLOC, MALLOC, MALLOC_FAIL);
436 
437   assert_size_equal(cbor_refcount(item), 1);
438 
439   cbor_decref(&item);
440 }
441 
test_definite_tag(void ** _state _CBOR_UNUSED)442 static void test_definite_tag(void** _state _CBOR_UNUSED) {
443   item = cbor_build_tag(10, cbor_move(cbor_build_uint8(42)));
444 
445   copy = cbor_copy_definite(item);
446   assert_uint8(tmp = cbor_tag_item(copy), 42);
447 
448   cbor_decref(&item);
449   cbor_decref(&copy);
450   cbor_decref(&tmp);
451 }
452 
test_definite_tag_nested(void ** _state _CBOR_UNUSED)453 static void test_definite_tag_nested(void** _state _CBOR_UNUSED) {
454   item = cbor_build_tag(10, cbor_move(cbor_new_indefinite_array()));
455 
456   copy = cbor_copy_definite(item);
457   assert_true(cbor_isa_tag(copy));
458 
459   tmp = cbor_tag_item(copy);
460   assert_true(cbor_isa_array(tmp));
461   assert_true(cbor_array_is_definite(tmp));
462   assert_size_equal(cbor_array_size(tmp), 0);
463 
464   cbor_decref(&item);
465   cbor_decref(&copy);
466   cbor_decref(&tmp);
467 }
468 
test_definite_tag_alloc_failure(void ** _state _CBOR_UNUSED)469 static void test_definite_tag_alloc_failure(void** _state _CBOR_UNUSED) {
470   item = cbor_build_tag(10, cbor_move(cbor_build_uint8(42)));
471 
472   WITH_FAILING_MALLOC({ assert_null(cbor_copy_definite(item)); });
473   assert_size_equal(cbor_refcount(item), 1);
474 
475   cbor_decref(&item);
476 }
477 
test_definite_ctrls(void ** _state _CBOR_UNUSED)478 static void test_definite_ctrls(void** _state _CBOR_UNUSED) {
479   item = cbor_new_null();
480   assert_true(cbor_is_null(copy = cbor_copy_definite(item)));
481   cbor_decref(&item);
482   cbor_decref(&copy);
483 }
484 
test_definite_floats(void ** _state _CBOR_UNUSED)485 static void test_definite_floats(void** _state _CBOR_UNUSED) {
486   item = cbor_build_float2(3.14f);
487   assert_true(cbor_float_get_float2(copy = cbor_copy_definite(item)) ==
488               cbor_float_get_float2(item));
489   cbor_decref(&item);
490   cbor_decref(&copy);
491 
492   item = cbor_build_float4(3.14f);
493   assert_true(cbor_float_get_float4(copy = cbor_copy_definite(item)) ==
494               cbor_float_get_float4(item));
495   cbor_decref(&item);
496   cbor_decref(&copy);
497 
498   item = cbor_build_float8(3.14);
499   assert_true(cbor_float_get_float8(copy = cbor_copy_definite(item)) ==
500               cbor_float_get_float8(item));
501   cbor_decref(&item);
502   cbor_decref(&copy);
503 }
504 
test_alloc_failure_simple(void ** _state _CBOR_UNUSED)505 static void test_alloc_failure_simple(void** _state _CBOR_UNUSED) {
506   item = cbor_build_uint8(10);
507 
508   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
509   assert_size_equal(cbor_refcount(item), 1);
510 
511   cbor_decref(&item);
512 }
513 
test_bytestring_alloc_failure(void ** _state _CBOR_UNUSED)514 static void test_bytestring_alloc_failure(void** _state _CBOR_UNUSED) {
515   item = cbor_new_indefinite_bytestring();
516   assert_true(cbor_bytestring_add_chunk(
517       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
518 
519   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
520   assert_size_equal(cbor_refcount(item), 1);
521 
522   cbor_decref(&item);
523 }
524 
test_bytestring_chunk_alloc_failure(void ** _state _CBOR_UNUSED)525 static void test_bytestring_chunk_alloc_failure(void** _state _CBOR_UNUSED) {
526   item = cbor_new_indefinite_bytestring();
527   assert_true(cbor_bytestring_add_chunk(
528       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
529 
530   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 2, MALLOC, MALLOC_FAIL);
531   assert_size_equal(cbor_refcount(item), 1);
532 
533   cbor_decref(&item);
534 }
535 
test_bytestring_chunk_append_failure(void ** _state _CBOR_UNUSED)536 static void test_bytestring_chunk_append_failure(void** _state _CBOR_UNUSED) {
537   item = cbor_new_indefinite_bytestring();
538   assert_true(cbor_bytestring_add_chunk(
539       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
540 
541   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 5,
542                    // New indef string, cbor_indefinite_string_data, chunk item,
543                    // chunk data, extend cbor_indefinite_string_data.chunks
544                    MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
545   assert_size_equal(cbor_refcount(item), 1);
546 
547   cbor_decref(&item);
548 }
549 
test_bytestring_second_chunk_alloc_failure(void ** _state _CBOR_UNUSED)550 static void test_bytestring_second_chunk_alloc_failure(
551     void** _state _CBOR_UNUSED) {
552   item = cbor_new_indefinite_bytestring();
553   assert_true(cbor_bytestring_add_chunk(
554       item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3))));
555   assert_true(cbor_bytestring_add_chunk(
556       item, cbor_move(cbor_build_bytestring((cbor_data) "def", 3))));
557 
558   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 6,
559                    // New indef string, cbor_indefinite_string_data, chunk item,
560                    // chunk data, extend cbor_indefinite_string_data.chunks,
561                    // second chunk item
562                    MALLOC, MALLOC, MALLOC, MALLOC, REALLOC, MALLOC_FAIL);
563   assert_size_equal(cbor_refcount(item), 1);
564 
565   cbor_decref(&item);
566 }
567 
test_string_alloc_failure(void ** _state _CBOR_UNUSED)568 static void test_string_alloc_failure(void** _state _CBOR_UNUSED) {
569   item = cbor_new_indefinite_string();
570   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
571 
572   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
573   assert_size_equal(cbor_refcount(item), 1);
574 
575   cbor_decref(&item);
576 }
577 
test_string_chunk_alloc_failure(void ** _state _CBOR_UNUSED)578 static void test_string_chunk_alloc_failure(void** _state _CBOR_UNUSED) {
579   item = cbor_new_indefinite_string();
580   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
581 
582   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 2, MALLOC, MALLOC_FAIL);
583   assert_size_equal(cbor_refcount(item), 1);
584 
585   cbor_decref(&item);
586 }
587 
test_string_chunk_append_failure(void ** _state _CBOR_UNUSED)588 static void test_string_chunk_append_failure(void** _state _CBOR_UNUSED) {
589   item = cbor_new_indefinite_string();
590   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
591 
592   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 5,
593                    // New indef string, cbor_indefinite_string_data, chunk item,
594                    // chunk data, extend cbor_indefinite_string_data.chunks
595                    MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
596   assert_size_equal(cbor_refcount(item), 1);
597 
598   cbor_decref(&item);
599 }
600 
test_string_second_chunk_alloc_failure(void ** _state _CBOR_UNUSED)601 static void test_string_second_chunk_alloc_failure(void** _state _CBOR_UNUSED) {
602   item = cbor_new_indefinite_string();
603   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc"))));
604   assert_true(cbor_string_add_chunk(item, cbor_move(cbor_build_string("def"))));
605 
606   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 6,
607                    // New indef string, cbor_indefinite_string_data, chunk item,
608                    // chunk data, extend cbor_indefinite_string_data.chunks,
609                    // second chunk item
610                    MALLOC, MALLOC, MALLOC, MALLOC, REALLOC, MALLOC_FAIL);
611   assert_size_equal(cbor_refcount(item), 1);
612 
613   cbor_decref(&item);
614 }
615 
test_array_alloc_failure(void ** _state _CBOR_UNUSED)616 static void test_array_alloc_failure(void** _state _CBOR_UNUSED) {
617   item = cbor_new_indefinite_array();
618   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
619 
620   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
621   assert_size_equal(cbor_refcount(item), 1);
622 
623   cbor_decref(&item);
624 }
625 
test_array_item_alloc_failure(void ** _state _CBOR_UNUSED)626 static void test_array_item_alloc_failure(void** _state _CBOR_UNUSED) {
627   item = cbor_new_indefinite_array();
628   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
629 
630   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 2,
631                    // New array, item copy
632                    MALLOC, MALLOC_FAIL);
633 
634   assert_size_equal(cbor_refcount(item), 1);
635 
636   cbor_decref(&item);
637 }
638 
test_array_push_failure(void ** _state _CBOR_UNUSED)639 static void test_array_push_failure(void** _state _CBOR_UNUSED) {
640   item = cbor_new_indefinite_array();
641   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
642 
643   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 3,
644                    // New array, item copy, array reallocation
645                    MALLOC, MALLOC, REALLOC_FAIL);
646 
647   assert_size_equal(cbor_refcount(item), 1);
648 
649   cbor_decref(&item);
650 }
651 
test_array_second_item_alloc_failure(void ** _state _CBOR_UNUSED)652 static void test_array_second_item_alloc_failure(void** _state _CBOR_UNUSED) {
653   item = cbor_new_indefinite_array();
654   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(42))));
655   assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(43))));
656 
657   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 4,
658                    // New array, item copy, array reallocation, second item copy
659                    MALLOC, MALLOC, REALLOC, MALLOC_FAIL);
660 
661   assert_size_equal(cbor_refcount(item), 1);
662 
663   cbor_decref(&item);
664 }
665 
test_map_alloc_failure(void ** _state _CBOR_UNUSED)666 static void test_map_alloc_failure(void** _state _CBOR_UNUSED) {
667   item = cbor_new_indefinite_map();
668   assert_true(
669       cbor_map_add(item, (struct cbor_pair){cbor_move(cbor_build_uint8(42)),
670                                             cbor_move(cbor_build_bool(true))}));
671 
672   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
673   assert_size_equal(cbor_refcount(item), 1);
674 
675   cbor_decref(&item);
676 }
677 
test_map_key_alloc_failure(void ** _state _CBOR_UNUSED)678 static void test_map_key_alloc_failure(void** _state _CBOR_UNUSED) {
679   item = cbor_new_indefinite_map();
680   assert_true(
681       cbor_map_add(item, (struct cbor_pair){cbor_move(cbor_build_uint8(42)),
682                                             cbor_move(cbor_build_bool(true))}));
683 
684   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 2,
685                    // New map, key copy
686                    MALLOC, MALLOC_FAIL);
687   assert_size_equal(cbor_refcount(item), 1);
688 
689   cbor_decref(&item);
690 }
691 
test_map_value_alloc_failure(void ** _state _CBOR_UNUSED)692 static void test_map_value_alloc_failure(void** _state _CBOR_UNUSED) {
693   item = cbor_new_indefinite_map();
694   assert_true(
695       cbor_map_add(item, (struct cbor_pair){cbor_move(cbor_build_uint8(42)),
696                                             cbor_move(cbor_build_bool(true))}));
697 
698   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 3,
699                    // New map, key copy, value copy
700                    MALLOC, MALLOC, MALLOC_FAIL);
701   assert_size_equal(cbor_refcount(item), 1);
702 
703   cbor_decref(&item);
704 }
705 
test_map_add_failure(void ** _state _CBOR_UNUSED)706 static void test_map_add_failure(void** _state _CBOR_UNUSED) {
707   item = cbor_new_indefinite_map();
708   assert_true(
709       cbor_map_add(item, (struct cbor_pair){cbor_move(cbor_build_uint8(42)),
710                                             cbor_move(cbor_build_bool(true))}));
711 
712   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 4,
713                    // New map, key copy, value copy, add
714                    MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
715   assert_size_equal(cbor_refcount(item), 1);
716 
717   cbor_decref(&item);
718 }
719 
test_map_second_key_failure(void ** _state _CBOR_UNUSED)720 static void test_map_second_key_failure(void** _state _CBOR_UNUSED) {
721   item = cbor_new_indefinite_map();
722   assert_true(
723       cbor_map_add(item, (struct cbor_pair){cbor_move(cbor_build_uint8(42)),
724                                             cbor_move(cbor_build_bool(true))}));
725   assert_true(cbor_map_add(
726       item, (struct cbor_pair){cbor_move(cbor_build_uint8(43)),
727                                cbor_move(cbor_build_bool(false))}));
728 
729   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 5,
730                    // New map, key copy, value copy, add, second key copy
731                    MALLOC, MALLOC, MALLOC, REALLOC, MALLOC_FAIL);
732   assert_size_equal(cbor_refcount(item), 1);
733 
734   cbor_decref(&item);
735 }
736 
test_tag_item_alloc_failure(void ** _state _CBOR_UNUSED)737 static void test_tag_item_alloc_failure(void** _state _CBOR_UNUSED) {
738   item = cbor_build_tag(1, cbor_move(cbor_build_uint8(42)));
739 
740   WITH_FAILING_MALLOC({ assert_null(cbor_copy(item)); });
741   assert_size_equal(cbor_refcount(item), 1);
742 
743   cbor_decref(&item);
744 }
745 
test_tag_alloc_failure(void ** _state _CBOR_UNUSED)746 static void test_tag_alloc_failure(void** _state _CBOR_UNUSED) {
747   item = cbor_build_tag(1, cbor_move(cbor_build_uint8(42)));
748 
749   WITH_MOCK_MALLOC({ assert_null(cbor_copy(item)); }, 2,
750                    // Item copy, tag
751                    MALLOC, MALLOC_FAIL);
752   assert_size_equal(cbor_refcount(item), 1);
753 
754   cbor_decref(&item);
755 }
756 
main(void)757 int main(void) {
758   const struct CMUnitTest tests[] = {
759       cmocka_unit_test(test_uints),
760       cmocka_unit_test(test_negints),
761       cmocka_unit_test(test_def_bytestring),
762       cmocka_unit_test(test_indef_bytestring),
763       cmocka_unit_test(test_def_string),
764       cmocka_unit_test(test_indef_string),
765       cmocka_unit_test(test_def_array),
766       cmocka_unit_test(test_indef_array),
767       cmocka_unit_test(test_def_map),
768       cmocka_unit_test(test_indef_map),
769       cmocka_unit_test(test_tag),
770       cmocka_unit_test(test_ctrls),
771       cmocka_unit_test(test_floats),
772       cmocka_unit_test(test_alloc_failure_simple),
773       cmocka_unit_test(test_bytestring_alloc_failure),
774       cmocka_unit_test(test_bytestring_chunk_alloc_failure),
775       cmocka_unit_test(test_bytestring_chunk_append_failure),
776       cmocka_unit_test(test_bytestring_second_chunk_alloc_failure),
777       cmocka_unit_test(test_string_alloc_failure),
778       cmocka_unit_test(test_string_chunk_alloc_failure),
779       cmocka_unit_test(test_string_chunk_append_failure),
780       cmocka_unit_test(test_string_second_chunk_alloc_failure),
781       cmocka_unit_test(test_array_alloc_failure),
782       cmocka_unit_test(test_array_item_alloc_failure),
783       cmocka_unit_test(test_array_push_failure),
784       cmocka_unit_test(test_array_second_item_alloc_failure),
785       cmocka_unit_test(test_map_alloc_failure),
786       cmocka_unit_test(test_map_key_alloc_failure),
787       cmocka_unit_test(test_map_value_alloc_failure),
788       cmocka_unit_test(test_map_add_failure),
789       cmocka_unit_test(test_map_second_key_failure),
790       cmocka_unit_test(test_tag_item_alloc_failure),
791       cmocka_unit_test(test_tag_alloc_failure),
792       cmocka_unit_test(test_definite_uints),
793       cmocka_unit_test(test_definite_negints),
794       cmocka_unit_test(test_definite_bytestring),
795       cmocka_unit_test(test_definite_bytestring_alloc_failure),
796       cmocka_unit_test(test_definite_indef_bytestring),
797       cmocka_unit_test(test_definite_string),
798       cmocka_unit_test(test_definite_indef_string),
799       cmocka_unit_test(test_definite_string_alloc_failure),
800       cmocka_unit_test(test_definite_array),
801       cmocka_unit_test(test_definite_indef_array),
802       cmocka_unit_test(test_definite_indef_array_nested),
803       cmocka_unit_test(test_definite_array_alloc_failure),
804       cmocka_unit_test(test_definite_array_item_alloc_failure),
805       cmocka_unit_test(test_definite_map),
806       cmocka_unit_test(test_definite_indef_map),
807       cmocka_unit_test(test_definite_indef_map_nested),
808       cmocka_unit_test(test_definite_map_alloc_failure),
809       cmocka_unit_test(test_definite_map_key_alloc_failure),
810       cmocka_unit_test(test_definite_map_value_alloc_failure),
811       cmocka_unit_test(test_definite_tag),
812       cmocka_unit_test(test_definite_tag_nested),
813       cmocka_unit_test(test_definite_tag_alloc_failure),
814       cmocka_unit_test(test_definite_ctrls),
815       cmocka_unit_test(test_definite_floats),
816   };
817   return cmocka_run_group_tests(tests, NULL, NULL);
818 }
819