diff --git a/tests/cases/corner_cases.cddl b/tests/cases/corner_cases.cddl index c51e6111..e472f57a 100644 --- a/tests/cases/corner_cases.cddl +++ b/tests/cases/corner_cases.cddl @@ -26,6 +26,13 @@ Numbers2 = [ big_nint2: -0x80000000, ] +; Using .size and .cbor together with maps. +NumberMap = { + "byte" => uint .size 1, + ? "opt_short" => uint .size 2, + ? "opt_cbor" => bstr .size 5 .cbor (uint .size 4), +} + Strings = [ hello: "hello", threehundrebytebstr: bstr .size 300, diff --git a/tests/decode/test5_corner_cases/CMakeLists.txt b/tests/decode/test5_corner_cases/CMakeLists.txt index ea276910..f1679c37 100644 --- a/tests/decode/test5_corner_cases/CMakeLists.txt +++ b/tests/decode/test5_corner_cases/CMakeLists.txt @@ -21,6 +21,7 @@ set(py_command -t NestedListMap NestedMapListMap Numbers Numbers2 + NumberMap Strings Prim2 Optional diff --git a/tests/decode/test5_corner_cases/src/main.c b/tests/decode/test5_corner_cases/src/main.c index 91551531..17132f77 100644 --- a/tests/decode/test5_corner_cases/src/main.c +++ b/tests/decode/test5_corner_cases/src/main.c @@ -181,6 +181,100 @@ void test_numbers2(void) sizeof(payload_numbers2_inv6), &numbers2, &decode_len), NULL); } +void test_number_map(void) +{ + size_t decode_len = 0xFFFFFFFF; + const uint8_t payload_number_map1[] = { + MAP(3), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't', + 0x19, 0x12, 0x34, + 0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r', + 0x45, 0x1A, 0x12, 0x34, 0x56, 0x78, + END + }; + const uint8_t payload_number_map2[] = { + MAP(1), + 0x64, 'b', 'y', 't', 'e', + 0x04, + END + }; + const uint8_t payload_number_map3[] = { + MAP(2), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't', + 0x12, + END + }; + const uint8_t payload_number_map4_inv[] = { + MAP(2), + 0x64, 'b', 'y', 't', 'e', + 0x19, 42, 42, + END + }; + const uint8_t payload_number_map5_inv[] = { + MAP(2), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't', + 0x1A, 0x12, 0x34, 0x56, 0x78, + END + }; + const uint8_t payload_number_map6_inv[] = { + MAP(2), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r', + 0x43, 0x19, 0x12, 0x34, + END + }; + const uint8_t payload_number_map7_inv[] = { + MAP(1), + 0x64, 'B', 'y', 't', 'e', + 0x04, + END + }; + + struct NumberMap number_map; + + zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map1, + sizeof(payload_number_map1), &number_map, &decode_len), NULL); + zassert_equal(42, number_map._NumberMap_byte, NULL); + zassert_true(number_map._NumberMap_opt_short_present, NULL); + zassert_equal(0x1234, number_map._NumberMap_opt_short._NumberMap_opt_short, NULL); + zassert_true(number_map._NumberMap_opt_cbor_present, NULL); + zassert_equal(0x12345678, number_map._NumberMap_opt_cbor._NumberMap_opt_cbor_cbor, NULL); + + zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map2, + sizeof(payload_number_map2), &number_map, &decode_len), NULL); + zassert_equal(4, number_map._NumberMap_byte, NULL); + zassert_false(number_map._NumberMap_opt_short_present, NULL); + zassert_false(number_map._NumberMap_opt_cbor_present, NULL); + + zassert_equal(ZCBOR_SUCCESS, cbor_decode_NumberMap(payload_number_map3, + sizeof(payload_number_map3), &number_map, &decode_len), NULL); + zassert_equal(42, number_map._NumberMap_byte, NULL); + zassert_true(number_map._NumberMap_opt_short_present, NULL); + zassert_equal(0x12, number_map._NumberMap_opt_short._NumberMap_opt_short, NULL); + zassert_false(number_map._NumberMap_opt_cbor_present, NULL); + + zassert_equal(ZCBOR_ERR_WRONG_RANGE, cbor_decode_NumberMap(payload_number_map4_inv, + sizeof(payload_number_map4_inv), &number_map, &decode_len), NULL); + + int res = cbor_decode_NumberMap(payload_number_map5_inv, + sizeof(payload_number_map5_inv), &number_map, &decode_len); + zassert_equal(ARR_ERR1, res, "%d\r\n", res); + + zassert_equal(ARR_ERR1, cbor_decode_NumberMap(payload_number_map6_inv, + sizeof(payload_number_map6_inv), &number_map, &decode_len), NULL); + + res = cbor_decode_NumberMap(payload_number_map7_inv, + sizeof(payload_number_map7_inv), &number_map, &decode_len); + zassert_equal(ZCBOR_ERR_WRONG_VALUE, res, "%d\r\n", res); +} + void test_strings(void) { const uint8_t payload_strings1[] = { @@ -1458,6 +1552,7 @@ void test_main(void) ztest_test_suite(cbor_decode_test5, ztest_unit_test(test_numbers), ztest_unit_test(test_numbers2), + ztest_unit_test(test_number_map), ztest_unit_test(test_strings), ztest_unit_test(test_primitives), ztest_unit_test(test_string_overflow), diff --git a/tests/encode/test3_corner_cases/CMakeLists.txt b/tests/encode/test3_corner_cases/CMakeLists.txt index 557ef76c..e0ff2db0 100644 --- a/tests/encode/test3_corner_cases/CMakeLists.txt +++ b/tests/encode/test3_corner_cases/CMakeLists.txt @@ -20,6 +20,7 @@ set(py_command NestedListMap NestedMapListMap Numbers Numbers2 + NumberMap Strings Prim2 Optional diff --git a/tests/encode/test3_corner_cases/src/main.c b/tests/encode/test3_corner_cases/src/main.c index 20ef253e..89f93c9c 100644 --- a/tests/encode/test3_corner_cases/src/main.c +++ b/tests/encode/test3_corner_cases/src/main.c @@ -112,6 +112,85 @@ void test_numbers2(void) zassert_mem_equal(exp_payload_numbers2, output, sizeof(exp_payload_numbers2), NULL); } +void test_number_map(void) +{ + size_t encode_len = 0xFFFFFFFF; + const uint8_t exp_payload_number_map1[] = { + MAP(3), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x69, 'o', 'p', 't', '_', 's', 'h', 'o', 'r', 't', + 0x19, 0x12, 0x34, + 0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r', + 0x45, 0x1A, 0x12, 0x34, 0x56, 0x78, + END + }; + + const uint8_t exp_payload_number_map2[] = { + MAP(2), + 0x64, 'b', 'y', 't', 'e', + 0x18, 42, + 0x68, 'o', 'p', 't', '_', 'c', 'b', 'o', 'r', + 0x45, 0x1A, 0x12, 0x34, 0x56, 0x78, + END + }; + + uint8_t payload[80]; + + struct NumberMap number_map1 = { + ._NumberMap_byte = 42, + ._NumberMap_opt_short_present = true, + ._NumberMap_opt_short._NumberMap_opt_short = 0x1234, + ._NumberMap_opt_cbor_present = true, + ._NumberMap_opt_cbor._NumberMap_opt_cbor_cbor = 0x12345678, + }; + + int res = cbor_encode_NumberMap(payload, + sizeof(payload), &number_map1, &encode_len); + zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res); + zassert_equal(encode_len, sizeof(exp_payload_number_map1), NULL); + zassert_mem_equal(payload, exp_payload_number_map1, encode_len, NULL); + + struct NumberMap number_map2 = { + ._NumberMap_byte = 42, + ._NumberMap_opt_short_present = false, + ._NumberMap_opt_cbor_present = true, + ._NumberMap_opt_cbor._NumberMap_opt_cbor_cbor = 0x12345678, + }; + + res = cbor_encode_NumberMap(payload, + sizeof(payload), &number_map2, &encode_len); + zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res); + zassert_equal(encode_len, sizeof(exp_payload_number_map2), NULL); + zassert_mem_equal(payload, exp_payload_number_map2, encode_len, NULL); + + struct NumberMap number_map3 = { + ._NumberMap_byte = 42, + ._NumberMap_opt_short_present = false, + ._NumberMap_opt_cbor_present = true, + ._NumberMap_opt_cbor._NumberMap_opt_cbor.value = (uint8_t[]){0x1A, 0x12, 0x34, 0x56, 0x78}, + ._NumberMap_opt_cbor._NumberMap_opt_cbor.len = 5, + }; + + res = cbor_encode_NumberMap(payload, + sizeof(payload), &number_map3, &encode_len); + zassert_equal(ZCBOR_SUCCESS, res, "%d\r\n", res); + zassert_equal(encode_len, sizeof(exp_payload_number_map2), NULL); + zassert_mem_equal(payload, exp_payload_number_map2, encode_len, NULL); + + struct NumberMap number_map4_inv = { + ._NumberMap_byte = 42, + ._NumberMap_opt_short_present = false, + ._NumberMap_opt_cbor_present = true, + ._NumberMap_opt_cbor._NumberMap_opt_cbor.value = (uint8_t[]){0x19, 0x12, 0x34}, + ._NumberMap_opt_cbor._NumberMap_opt_cbor.len = 3, + }; + + res = cbor_encode_NumberMap(payload, + sizeof(payload), &number_map4_inv, &encode_len); + zassert_equal(ZCBOR_ERR_WRONG_RANGE, res, "%d\r\n", res); +} + void test_strings(void) { @@ -1163,6 +1242,7 @@ void test_main(void) ztest_test_suite(cbor_encode_test3, ztest_unit_test(test_numbers), ztest_unit_test(test_numbers2), + ztest_unit_test(test_number_map), ztest_unit_test(test_strings), ztest_unit_test(test_primitives), ztest_unit_test(test_optional), diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index 9d9e595c..5241072f 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -924,7 +924,9 @@ def skip_condition(self): return False def set_skipped(self, skipped): - if self.range_check_condition() and self.repeated_single_func_impl_condition(): + if self.range_check_condition() \ + and self.repeated_single_func_impl_condition() \ + and not self.key: self.skipped = True else: self.skipped = skipped