110ff414cSEd Maste /*
210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste *
410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste */
710ff414cSEd Maste
8*5d3e7166SEd Maste #include "assertions.h"
910ff414cSEd Maste #include "cbor.h"
1010ff414cSEd Maste
1110ff414cSEd Maste /* These tests verify behavior on interesting randomly generated inputs from the
1210ff414cSEd Maste * fuzzer */
1310ff414cSEd Maste
1410ff414cSEd Maste cbor_item_t *item;
1510ff414cSEd Maste struct cbor_load_result res;
1610ff414cSEd Maste
1710ff414cSEd Maste /* Map start + array with embedded length */
1810ff414cSEd Maste unsigned char data1[] = {0xA9, 0x85};
test_1(void ** _CBOR_UNUSED (_state))19*5d3e7166SEd Maste static void test_1(void **_CBOR_UNUSED(_state)) {
2010ff414cSEd Maste item = cbor_load(data1, 2, &res);
2110ff414cSEd Maste assert_null(item);
2210ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
23*5d3e7166SEd Maste assert_size_equal(res.error.position, 2);
2410ff414cSEd Maste }
2510ff414cSEd Maste
2610ff414cSEd Maste unsigned char data2[] = {0x9D};
test_2(void ** _CBOR_UNUSED (_state))27*5d3e7166SEd Maste static void test_2(void **_CBOR_UNUSED(_state)) {
2810ff414cSEd Maste item = cbor_load(data2, 1, &res);
2910ff414cSEd Maste assert_null(item);
3010ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_MALFORMATED);
31*5d3e7166SEd Maste assert_size_equal(res.error.position, 0);
3210ff414cSEd Maste }
3310ff414cSEd Maste
3410ff414cSEd Maste unsigned char data3[] = {0xD6};
test_3(void ** _CBOR_UNUSED (_state))35*5d3e7166SEd Maste static void test_3(void **_CBOR_UNUSED(_state)) {
3610ff414cSEd Maste item = cbor_load(data3, 1, &res);
3710ff414cSEd Maste assert_null(item);
3810ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
39*5d3e7166SEd Maste assert_size_equal(res.error.position, 1);
4010ff414cSEd Maste }
4110ff414cSEd Maste
4210ff414cSEd Maste #ifdef SANE_MALLOC
4310ff414cSEd Maste unsigned char data4[] = {0xBA, 0xC1, 0xE8, 0x3E, 0xE7, 0x20, 0xA8};
test_4(void ** _CBOR_UNUSED (_state))44*5d3e7166SEd Maste static void test_4(void **_CBOR_UNUSED(_state)) {
4510ff414cSEd Maste item = cbor_load(data4, 7, &res);
4610ff414cSEd Maste assert_null(item);
4710ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_MEMERROR);
48*5d3e7166SEd Maste assert_size_equal(res.error.position, 5);
4910ff414cSEd Maste }
5010ff414cSEd Maste
5110ff414cSEd Maste unsigned char data5[] = {0x9A, 0xDA, 0x3A, 0xB2, 0x7F, 0x29};
test_5(void ** _CBOR_UNUSED (_state))52*5d3e7166SEd Maste static void test_5(void **_CBOR_UNUSED(_state)) {
5310ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_MEMERROR);
5410ff414cSEd Maste item = cbor_load(data5, 6, &res);
5510ff414cSEd Maste assert_null(item);
56*5d3e7166SEd Maste assert_size_equal(res.error.position, 5);
5710ff414cSEd Maste /* Indef string expectation mismatch */
5810ff414cSEd Maste }
5910ff414cSEd Maste #endif
6010ff414cSEd Maste
6110ff414cSEd Maste unsigned char data6[] = {0x7F, 0x21, 0x4C, 0x02, 0x40};
test_6(void ** _CBOR_UNUSED (_state))62*5d3e7166SEd Maste static void test_6(void **_CBOR_UNUSED(_state)) {
6310ff414cSEd Maste item = cbor_load(data6, 5, &res);
6410ff414cSEd Maste assert_null(item);
6510ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
66*5d3e7166SEd Maste assert_size_equal(res.error.position, 2);
6710ff414cSEd Maste }
6810ff414cSEd Maste
6910ff414cSEd Maste #ifdef EIGHT_BYTE_SIZE_T
7010ff414cSEd Maste /* Extremely high size value (overflows size_t in representation size). Only
7110ff414cSEd Maste * works with 64b sizes */
7210ff414cSEd Maste unsigned char data7[] = {0xA2, 0x9B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
7310ff414cSEd Maste 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
test_7(void ** _CBOR_UNUSED (_state))74*5d3e7166SEd Maste static void test_7(void **_CBOR_UNUSED(_state)) {
7510ff414cSEd Maste item = cbor_load(data7, 16, &res);
7610ff414cSEd Maste assert_null(item);
7710ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_MEMERROR);
78*5d3e7166SEd Maste assert_size_equal(res.error.position, 10);
7910ff414cSEd Maste }
8010ff414cSEd Maste #endif
8110ff414cSEd Maste
8210ff414cSEd Maste unsigned char data8[] = {0xA3, 0x64, 0x68, 0x61, 0x6C, 0x66, 0xFF, 0x00,
8310ff414cSEd Maste 0x00, 0x66, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,
8410ff414cSEd Maste 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x6D, 0x73, 0x69,
8510ff414cSEd Maste 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x76, 0x61, 0x6C,
8610ff414cSEd Maste 0x75, 0x65, 0x73, 0x83, 0xF5, 0xF4, 0xF6};
test_8(void ** _CBOR_UNUSED (_state))87*5d3e7166SEd Maste static void test_8(void **_CBOR_UNUSED(_state)) {
8810ff414cSEd Maste item = cbor_load(data8, 39, &res);
8910ff414cSEd Maste assert_null(item);
9010ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
91*5d3e7166SEd Maste assert_size_equal(res.error.position, 7);
9210ff414cSEd Maste }
9310ff414cSEd Maste
9410ff414cSEd Maste unsigned char data9[] = {0xBF, 0x05, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x04};
test_9(void ** _CBOR_UNUSED (_state))95*5d3e7166SEd Maste static void test_9(void **_CBOR_UNUSED(_state)) {
9610ff414cSEd Maste item = cbor_load(data9, 8, &res);
9710ff414cSEd Maste assert_null(item);
9810ff414cSEd Maste assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
99*5d3e7166SEd Maste assert_size_equal(res.error.position, 3);
10010ff414cSEd Maste }
10110ff414cSEd Maste
main(void)10210ff414cSEd Maste int main(void) {
10310ff414cSEd Maste const struct CMUnitTest tests[] = {
10410ff414cSEd Maste cmocka_unit_test(test_1), cmocka_unit_test(test_2),
10510ff414cSEd Maste cmocka_unit_test(test_3),
10610ff414cSEd Maste #ifdef SANE_MALLOC
10710ff414cSEd Maste cmocka_unit_test(test_4), cmocka_unit_test(test_5),
10810ff414cSEd Maste #endif
10910ff414cSEd Maste cmocka_unit_test(test_6),
11010ff414cSEd Maste #ifdef EIGHT_BYTE_SIZE_T
11110ff414cSEd Maste cmocka_unit_test(test_7),
11210ff414cSEd Maste #endif
11310ff414cSEd Maste cmocka_unit_test(test_8), cmocka_unit_test(test_9),
11410ff414cSEd Maste };
11510ff414cSEd Maste return cmocka_run_group_tests(tests, NULL, NULL);
11610ff414cSEd Maste }
117