aboutsummaryrefslogtreecommitdiff
path: root/contrib/libcbor/test/callbacks_test.c
blob: 65c5d37f4399c08d136dc60d3079ef74fdfca94f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */
#include "assertions.h"
#include "cbor.h"
#include "cbor/internal/builder_callbacks.h"
#include "cbor/internal/stack.h"
#include "test_allocator.h"

unsigned char data[] = {
    0x93, 0x01, 0x19, 0x01, 0x01, 0x1A, 0x00, 0x01, 0x05, 0xB8, 0x1B, 0x00,
    0x00, 0x00, 0x01, 0x8F, 0x5A, 0xE8, 0xB8, 0x20, 0x39, 0x01, 0x00, 0x3A,
    0x00, 0x01, 0x05, 0xB7, 0x3B, 0x00, 0x00, 0x00, 0x01, 0x8F, 0x5A, 0xE8,
    0xB7, 0x5F, 0x41, 0x01, 0x41, 0x02, 0xFF, 0x7F, 0x61, 0x61, 0x61, 0x62,
    0xFF, 0x9F, 0xFF, 0xA1, 0x61, 0x61, 0x61, 0x62, 0xC0, 0xBF, 0xFF, 0xF9,
    0x3C, 0x00, 0xFA, 0x47, 0xC3, 0x50, 0x00, 0xFB, 0x7E, 0x37, 0xE4, 0x3C,
    0x88, 0x00, 0x75, 0x9C, 0xF6, 0xF7, 0xF5};

/* Exercise the default callbacks */
static void test_default_callbacks(void** _CBOR_UNUSED(_state)) {
  size_t read = 0;
  while (read < 79) {
    struct cbor_decoder_result result =
        cbor_stream_decode(data + read, 79 - read, &cbor_empty_callbacks, NULL);
    read += result.read;
  }
}

unsigned char bytestring_data[] = {0x01, 0x02, 0x03};
static void test_builder_byte_string_callback_append(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(
      _cbor_stack_push(&stack, cbor_new_indefinite_bytestring(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  cbor_builder_byte_string_callback(&context, bytestring_data, 3);

  assert_false(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  cbor_item_t* bytestring = stack.top->item;
  assert_size_equal(cbor_refcount(bytestring), 1);
  assert_true(cbor_typeof(bytestring) == CBOR_TYPE_BYTESTRING);
  assert_true(cbor_isa_bytestring(bytestring));
  assert_size_equal(cbor_bytestring_length(bytestring), 0);
  assert_true(cbor_bytestring_is_indefinite(bytestring));
  assert_size_equal(cbor_bytestring_chunk_count(bytestring), 1);

  cbor_item_t* chunk = cbor_bytestring_chunks_handle(bytestring)[0];
  assert_size_equal(cbor_refcount(chunk), 1);
  assert_true(cbor_typeof(bytestring) == CBOR_TYPE_BYTESTRING);
  assert_true(cbor_isa_bytestring(chunk));
  assert_true(cbor_bytestring_is_definite(chunk));
  assert_size_equal(cbor_bytestring_length(chunk), 3);
  assert_memory_equal(cbor_bytestring_handle(chunk), bytestring_data, 3);
  // Data is copied
  assert_ptr_not_equal(cbor_bytestring_handle(chunk), bytestring_data);

  cbor_decref(&bytestring);
  _cbor_stack_pop(&stack);
}

static void test_builder_byte_string_callback_append_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(
      _cbor_stack_push(&stack, cbor_new_indefinite_bytestring(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  WITH_FAILING_MALLOC(
      { cbor_builder_byte_string_callback(&context, bytestring_data, 3); });

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* bytestring = stack.top->item;
  assert_size_equal(cbor_refcount(bytestring), 1);
  assert_true(cbor_typeof(bytestring) == CBOR_TYPE_BYTESTRING);
  assert_true(cbor_isa_bytestring(bytestring));
  assert_size_equal(cbor_bytestring_length(bytestring), 0);
  assert_true(cbor_bytestring_is_indefinite(bytestring));
  assert_size_equal(cbor_bytestring_chunk_count(bytestring), 0);

  cbor_decref(&bytestring);
  _cbor_stack_pop(&stack);
}

static void test_builder_byte_string_callback_append_item_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(
      _cbor_stack_push(&stack, cbor_new_indefinite_bytestring(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  // Allocate new data block, but fail to allocate a new item with it
  WITH_MOCK_MALLOC(
      { cbor_builder_byte_string_callback(&context, bytestring_data, 3); }, 2,
      MALLOC, MALLOC_FAIL);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* bytestring = stack.top->item;
  assert_size_equal(cbor_refcount(bytestring), 1);
  assert_true(cbor_typeof(bytestring) == CBOR_TYPE_BYTESTRING);
  assert_true(cbor_isa_bytestring(bytestring));
  assert_size_equal(cbor_bytestring_length(bytestring), 0);
  assert_true(cbor_bytestring_is_indefinite(bytestring));
  assert_size_equal(cbor_bytestring_chunk_count(bytestring), 0);

  cbor_decref(&bytestring);
  _cbor_stack_pop(&stack);
}

static void test_builder_byte_string_callback_append_parent_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(
      _cbor_stack_push(&stack, cbor_new_indefinite_bytestring(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  // Allocate new item, but fail to push it into the parent on the stack
  WITH_MOCK_MALLOC(
      { cbor_builder_byte_string_callback(&context, bytestring_data, 3); }, 3,
      MALLOC, MALLOC, REALLOC_FAIL);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* bytestring = stack.top->item;
  assert_size_equal(cbor_refcount(bytestring), 1);
  assert_true(cbor_typeof(bytestring) == CBOR_TYPE_BYTESTRING);
  assert_true(cbor_isa_bytestring(bytestring));
  assert_size_equal(cbor_bytestring_length(bytestring), 0);
  assert_true(cbor_bytestring_is_indefinite(bytestring));
  assert_size_equal(cbor_bytestring_chunk_count(bytestring), 0);

  cbor_decref(&bytestring);
  _cbor_stack_pop(&stack);
}

unsigned char string_data[] = {0x61, 0x62, 0x63};
static void test_builder_string_callback_append(void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_indefinite_string(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  cbor_builder_string_callback(&context, string_data, 3);

  assert_false(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  cbor_item_t* string = stack.top->item;
  assert_size_equal(cbor_refcount(string), 1);
  assert_true(cbor_isa_string(string));
  assert_size_equal(cbor_string_length(string), 0);
  assert_true(cbor_string_is_indefinite(string));
  assert_size_equal(cbor_string_chunk_count(string), 1);

  cbor_item_t* chunk = cbor_string_chunks_handle(string)[0];
  assert_size_equal(cbor_refcount(chunk), 1);
  assert_true(cbor_isa_string(chunk));
  assert_true(cbor_string_is_definite(chunk));
  assert_size_equal(cbor_string_length(chunk), 3);
  assert_memory_equal(cbor_string_handle(chunk), "abc", 3);
  // Data is copied
  assert_ptr_not_equal(cbor_string_handle(chunk), string_data);

  cbor_decref(&string);
  _cbor_stack_pop(&stack);
}

static void test_builder_string_callback_append_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_indefinite_string(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  WITH_FAILING_MALLOC(
      { cbor_builder_string_callback(&context, string_data, 3); });

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* string = stack.top->item;
  assert_size_equal(cbor_refcount(string), 1);
  assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
  assert_true(cbor_isa_string(string));
  assert_size_equal(cbor_string_length(string), 0);
  assert_true(cbor_string_is_indefinite(string));
  assert_size_equal(cbor_string_chunk_count(string), 0);

  cbor_decref(&string);
  _cbor_stack_pop(&stack);
}

static void test_builder_string_callback_append_item_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_indefinite_string(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  // Allocate new data block, but fail to allocate a new item with it
  WITH_MOCK_MALLOC({ cbor_builder_string_callback(&context, string_data, 3); },
                   2, MALLOC, MALLOC_FAIL);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* string = stack.top->item;
  assert_size_equal(cbor_refcount(string), 1);
  assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
  assert_true(cbor_isa_string(string));
  assert_size_equal(cbor_string_length(string), 0);
  assert_true(cbor_string_is_indefinite(string));
  assert_size_equal(cbor_string_chunk_count(string), 0);

  cbor_decref(&string);
  _cbor_stack_pop(&stack);
}

static void test_builder_string_callback_append_parent_alloc_failure(
    void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_indefinite_string(), 0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  // Allocate new item, but fail to push it into the parent on the stack
  WITH_MOCK_MALLOC({ cbor_builder_string_callback(&context, string_data, 3); },
                   3, MALLOC, MALLOC, REALLOC_FAIL);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* string = stack.top->item;
  assert_size_equal(cbor_refcount(string), 1);
  assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
  assert_true(cbor_isa_string(string));
  assert_size_equal(cbor_string_length(string), 0);
  assert_true(cbor_string_is_indefinite(string));
  assert_size_equal(cbor_string_chunk_count(string), 0);

  cbor_decref(&string);
  _cbor_stack_pop(&stack);
}

static void test_append_array_failure(void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_definite_array(0), 0));
  stack.top->subitems = 1;
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };
  cbor_item_t* item = cbor_build_uint8(42);

  _cbor_builder_append(item, &context);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* array = stack.top->item;
  assert_size_equal(cbor_refcount(array), 1);
  assert_true(cbor_isa_array(array));
  assert_size_equal(cbor_array_size(array), 0);

  // item free'd by _cbor_builder_append
  cbor_decref(&array);
  _cbor_stack_pop(&stack);
}

static void test_append_map_failure(void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(
      _cbor_stack_push(&stack, cbor_new_indefinite_map(), /*subitems=*/0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };
  cbor_item_t* item = cbor_build_uint8(42);

  WITH_MOCK_MALLOC({ _cbor_builder_append(item, &context); }, 1, REALLOC_FAIL);

  assert_true(context.creation_failed);
  assert_false(context.syntax_error);
  assert_size_equal(context.stack->size, 1);

  // The stack remains unchanged
  cbor_item_t* map = stack.top->item;
  assert_size_equal(cbor_refcount(map), 1);
  assert_true(cbor_isa_map(map));
  assert_size_equal(cbor_map_size(map), 0);

  // item free'd by _cbor_builder_append
  cbor_decref(&map);
  _cbor_stack_pop(&stack);
}

// Size 1 array start, but we get an indef break
unsigned char invalid_indef_break_data[] = {0x81, 0xFF};
static void test_invalid_indef_break(void** _CBOR_UNUSED(_state)) {
  struct cbor_load_result res;
  cbor_item_t* item = cbor_load(invalid_indef_break_data, 2, &res);

  assert_null(item);
  assert_size_equal(res.read, 2);
  assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
}

static void test_invalid_state_indef_break(void** _CBOR_UNUSED(_state)) {
  struct _cbor_stack stack = _cbor_stack_init();
  assert_non_null(_cbor_stack_push(&stack, cbor_new_int8(), /*subitems=*/0));
  struct _cbor_decoder_context context = {
      .creation_failed = false,
      .syntax_error = false,
      .root = NULL,
      .stack = &stack,
  };

  cbor_builder_indef_break_callback(&context);

  assert_false(context.creation_failed);
  assert_true(context.syntax_error);
  assert_size_equal(context.stack->size, 1);
  // The stack remains unchanged
  cbor_item_t* small_int = stack.top->item;
  assert_size_equal(cbor_refcount(small_int), 1);
  assert_true(cbor_isa_uint(small_int));

  cbor_decref(&small_int);
  _cbor_stack_pop(&stack);
}

int main(void) {
  const struct CMUnitTest tests[] = {
      cmocka_unit_test(test_default_callbacks),
      cmocka_unit_test(test_builder_byte_string_callback_append),
      cmocka_unit_test(test_builder_byte_string_callback_append_alloc_failure),
      cmocka_unit_test(
          test_builder_byte_string_callback_append_item_alloc_failure),
      cmocka_unit_test(
          test_builder_byte_string_callback_append_parent_alloc_failure),
      cmocka_unit_test(test_builder_string_callback_append),
      cmocka_unit_test(test_builder_string_callback_append_alloc_failure),
      cmocka_unit_test(test_builder_string_callback_append_item_alloc_failure),
      cmocka_unit_test(
          test_builder_string_callback_append_parent_alloc_failure),
      cmocka_unit_test(test_append_array_failure),
      cmocka_unit_test(test_append_map_failure),
      cmocka_unit_test(test_invalid_indef_break),
      cmocka_unit_test(test_invalid_state_indef_break),
  };

  cmocka_run_group_tests(tests, NULL, NULL);
}