xref: /freebsd/contrib/libcbor/test/bad_inputs_test.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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 "cbor.h"
15 
16 /* These tests verify behavior on interesting randomly generated inputs from the
17  * fuzzer */
18 
19 cbor_item_t *item;
20 struct cbor_load_result res;
21 
22 /* Map start + array with embedded length */
23 unsigned char data1[] = {0xA9, 0x85};
24 static void test_1(void **state) {
25   item = cbor_load(data1, 2, &res);
26   assert_null(item);
27   assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
28   assert_int_equal(res.error.position, 2);
29 }
30 
31 unsigned char data2[] = {0x9D};
32 static void test_2(void **state) {
33   item = cbor_load(data2, 1, &res);
34   assert_null(item);
35   assert_true(res.error.code == CBOR_ERR_MALFORMATED);
36   assert_int_equal(res.error.position, 0);
37 }
38 
39 unsigned char data3[] = {0xD6};
40 static void test_3(void **state) {
41   item = cbor_load(data3, 1, &res);
42   assert_null(item);
43   assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
44   assert_int_equal(res.error.position, 1);
45 }
46 
47 #ifdef SANE_MALLOC
48 unsigned char data4[] = {0xBA, 0xC1, 0xE8, 0x3E, 0xE7, 0x20, 0xA8};
49 static void test_4(void **state) {
50   item = cbor_load(data4, 7, &res);
51   assert_null(item);
52   assert_true(res.error.code == CBOR_ERR_MEMERROR);
53   assert_int_equal(res.error.position, 5);
54 }
55 
56 unsigned char data5[] = {0x9A, 0xDA, 0x3A, 0xB2, 0x7F, 0x29};
57 static void test_5(void **state) {
58   assert_true(res.error.code == CBOR_ERR_MEMERROR);
59   item = cbor_load(data5, 6, &res);
60   assert_null(item);
61   assert_int_equal(res.error.position, 5);
62   /* Indef string expectation mismatch */
63 }
64 #endif
65 
66 unsigned char data6[] = {0x7F, 0x21, 0x4C, 0x02, 0x40};
67 static void test_6(void **state) {
68   item = cbor_load(data6, 5, &res);
69   assert_null(item);
70   assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
71   assert_int_equal(res.error.position, 2);
72 }
73 
74 #ifdef EIGHT_BYTE_SIZE_T
75 /* Extremely high size value (overflows size_t in representation size). Only
76  * works with 64b sizes */
77 unsigned char data7[] = {0xA2, 0x9B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
78                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
79 static void test_7(void **state) {
80   item = cbor_load(data7, 16, &res);
81   assert_null(item);
82   assert_true(res.error.code == CBOR_ERR_MEMERROR);
83   assert_int_equal(res.error.position, 10);
84 }
85 #endif
86 
87 unsigned char data8[] = {0xA3, 0x64, 0x68, 0x61, 0x6C, 0x66, 0xFF, 0x00,
88                          0x00, 0x66, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,
89                          0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x6D, 0x73, 0x69,
90                          0x6D, 0x70, 0x6C, 0x65, 0x20, 0x76, 0x61, 0x6C,
91                          0x75, 0x65, 0x73, 0x83, 0xF5, 0xF4, 0xF6};
92 static void test_8(void **state) {
93   item = cbor_load(data8, 39, &res);
94   assert_null(item);
95   assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
96   assert_int_equal(res.error.position, 7);
97 }
98 
99 unsigned char data9[] = {0xBF, 0x05, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x04};
100 static void test_9(void **state) {
101   item = cbor_load(data9, 8, &res);
102   assert_null(item);
103   assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
104   assert_int_equal(res.error.position, 3);
105 }
106 
107 int main(void) {
108   const struct CMUnitTest tests[] = {
109       cmocka_unit_test(test_1), cmocka_unit_test(test_2),
110       cmocka_unit_test(test_3),
111 #ifdef SANE_MALLOC
112       cmocka_unit_test(test_4), cmocka_unit_test(test_5),
113 #endif
114       cmocka_unit_test(test_6),
115 #ifdef EIGHT_BYTE_SIZE_T
116       cmocka_unit_test(test_7),
117 #endif
118       cmocka_unit_test(test_8), cmocka_unit_test(test_9),
119   };
120   return cmocka_run_group_tests(tests, NULL, NULL);
121 }
122