Skip to content

Commit

Permalink
zcbor.py: Fix bug where indirection for ints with range checks was wrong
Browse files Browse the repository at this point in the history
Access indirection did not match typedef.
Add regression tests.

See #193

Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad committed Mar 30, 2022
1 parent 91220b3 commit bdd2cd4
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests/cases/corner_cases.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/decode/test5_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(py_command
-t NestedListMap NestedMapListMap
Numbers
Numbers2
NumberMap
Strings
Prim2
Optional
Expand Down
95 changes: 95 additions & 0 deletions tests/decode/test5_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions tests/encode/test3_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(py_command
NestedListMap NestedMapListMap
Numbers
Numbers2
NumberMap
Strings
Prim2
Optional
Expand Down
80 changes: 80 additions & 0 deletions tests/encode/test3_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion zcbor/zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bdd2cd4

Please sign in to comment.