Path: blob/main/contrib/libcbor/test/bad_inputs_test.c
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include "assertions.h"8#include "cbor.h"910/* These tests verify behavior on interesting randomly generated inputs from the11* fuzzer */1213cbor_item_t *item;14struct cbor_load_result res;1516/* Map start + array with embedded length */17unsigned char data1[] = {0xA9, 0x85};18static void test_1(void **_CBOR_UNUSED(_state)) {19item = cbor_load(data1, 2, &res);20assert_null(item);21assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);22assert_size_equal(res.error.position, 2);23}2425unsigned char data2[] = {0x9D};26static void test_2(void **_CBOR_UNUSED(_state)) {27item = cbor_load(data2, 1, &res);28assert_null(item);29assert_true(res.error.code == CBOR_ERR_MALFORMATED);30assert_size_equal(res.error.position, 0);31}3233unsigned char data3[] = {0xD6};34static void test_3(void **_CBOR_UNUSED(_state)) {35item = cbor_load(data3, 1, &res);36assert_null(item);37assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);38assert_size_equal(res.error.position, 1);39}4041#ifdef SANE_MALLOC42unsigned char data4[] = {0xBA, 0xC1, 0xE8, 0x3E, 0xE7, 0x20, 0xA8};43static void test_4(void **_CBOR_UNUSED(_state)) {44item = cbor_load(data4, 7, &res);45assert_null(item);46assert_true(res.error.code == CBOR_ERR_MEMERROR);47assert_size_equal(res.error.position, 5);48}4950unsigned char data5[] = {0x9A, 0xDA, 0x3A, 0xB2, 0x7F, 0x29};51static void test_5(void **_CBOR_UNUSED(_state)) {52assert_true(res.error.code == CBOR_ERR_MEMERROR);53item = cbor_load(data5, 6, &res);54assert_null(item);55assert_size_equal(res.error.position, 5);56/* Indef string expectation mismatch */57}58#endif5960unsigned char data6[] = {0x7F, 0x21, 0x4C, 0x02, 0x40};61static void test_6(void **_CBOR_UNUSED(_state)) {62item = cbor_load(data6, 5, &res);63assert_null(item);64assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);65assert_size_equal(res.error.position, 2);66}6768#ifdef EIGHT_BYTE_SIZE_T69/* Extremely high size value (overflows size_t in representation size). Only70* works with 64b sizes */71unsigned char data7[] = {0xA2, 0x9B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,720x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};73static void test_7(void **_CBOR_UNUSED(_state)) {74item = cbor_load(data7, 16, &res);75assert_null(item);76assert_true(res.error.code == CBOR_ERR_MEMERROR);77assert_size_equal(res.error.position, 10);78}79#endif8081unsigned char data8[] = {0xA3, 0x64, 0x68, 0x61, 0x6C, 0x66, 0xFF, 0x00,820x00, 0x66, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,830xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x6D, 0x73, 0x69,840x6D, 0x70, 0x6C, 0x65, 0x20, 0x76, 0x61, 0x6C,850x75, 0x65, 0x73, 0x83, 0xF5, 0xF4, 0xF6};86static void test_8(void **_CBOR_UNUSED(_state)) {87item = cbor_load(data8, 39, &res);88assert_null(item);89assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);90assert_size_equal(res.error.position, 7);91}9293unsigned char data9[] = {0xBF, 0x05, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x04};94static void test_9(void **_CBOR_UNUSED(_state)) {95item = cbor_load(data9, 8, &res);96assert_null(item);97assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);98assert_size_equal(res.error.position, 3);99}100101int main(void) {102const struct CMUnitTest tests[] = {103cmocka_unit_test(test_1), cmocka_unit_test(test_2),104cmocka_unit_test(test_3),105#ifdef SANE_MALLOC106cmocka_unit_test(test_4), cmocka_unit_test(test_5),107#endif108cmocka_unit_test(test_6),109#ifdef EIGHT_BYTE_SIZE_T110cmocka_unit_test(test_7),111#endif112cmocka_unit_test(test_8), cmocka_unit_test(test_9),113};114return cmocka_run_group_tests(tests, NULL, NULL);115}116117118