Skip to content

Commit

Permalink
zcbor.py: Fixes for deeply nested types
Browse files Browse the repository at this point in the history
+ tests

Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad committed Aug 21, 2024
1 parent 7b4a0d7 commit bb962b9
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 11 deletions.
6 changes: 6 additions & 0 deletions tests/cases/corner_cases.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,9 @@ SingleElemList = [
[true],
{1: bstr},
]

Choice1 = nil / [ * [tstr] ]
Choice2 = nil / ([ * [tstr] ])
Choice3 = nil / ([ * ([tstr])/nil ])
Choice4 = nil / ([ * ([[Choice3]])/nil ])
Choice5 = nil / ([ * {*int=>[tstr]}/nil ])
5 changes: 5 additions & 0 deletions tests/decode/test5_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ set(py_command
Keywords
EmptyContainer
SingleElemList
Choice1
Choice2
Choice3
Choice4
Choice5
--decode
--git-sha-header
--short-names
Expand Down
131 changes: 131 additions & 0 deletions tests/decode/test5_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include <common_test.h>
#include <zcbor_print.h>

static void zassert_zcbor_string(struct zcbor_string *str, uint8_t *expected, size_t len)
{
zassert_equal(len, str->len, "exp: %s\n", expected);
zassert_mem_equal(expected, str->value, len, "exp: %s\n", expected);
}


ZTEST(cbor_decode_test5, test_numbers)
{
Expand Down Expand Up @@ -2256,4 +2262,129 @@ ZTEST(cbor_decode_test5, test_single_elem_list)
}


ZTEST(cbor_decode_test5, test_nested_choices)
{
uint8_t nested_choices_payload1[] = {0xf6};
uint8_t nested_choices_payload2[] = {LIST(2),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
};
uint8_t nested_choices_payload3[] = {LIST(3),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
0xf6,
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
};
uint8_t nested_choices_payload4[] = {LIST(2),
LIST(1), LIST(1),
LIST(3),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
0xf6,
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
END END
0xf6,
END
};
uint8_t nested_choices_payload5[] = {LIST(2),
MAP(2),
0, LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1, LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
0xf6,
END
};
struct Choice1_r result1;
struct Choice2_r result2;
struct Choice3_r result3;
struct Choice4_r result4;
struct Choice5_r result5;

size_t num_decode;

zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice1(nested_choices_payload1,
sizeof(nested_choices_payload1), &result1, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice2(nested_choices_payload1,
sizeof(nested_choices_payload1), &result2, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice3(nested_choices_payload1,
sizeof(nested_choices_payload1), &result3, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice4(nested_choices_payload1,
sizeof(nested_choices_payload1), &result4, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice5(nested_choices_payload1,
sizeof(nested_choices_payload1), &result5, &num_decode), NULL);
zassert_equal(result1.Choice1_choice, Choice1_nil_c);
zassert_equal(result2.Choice2_choice, Choice2_nil_c);
zassert_equal(result3.Choice3_choice, Choice3_nil_c);
zassert_equal(result4.Choice4_choice, Choice4_nil_c);
zassert_equal(result5.Choice5_choice, Choice5_nil_c);

zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice1(nested_choices_payload2,
sizeof(nested_choices_payload2), &result1, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice2(nested_choices_payload2,
sizeof(nested_choices_payload2), &result2, &num_decode), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice3(nested_choices_payload2,
sizeof(nested_choices_payload2), &result3, &num_decode), NULL);

zassert_equal(result1.Choice1_choice, Choice1_tstr_l_l_c);
zassert_equal(result2.Choice2_choice, Choice2_tstr_l_l_c);
zassert_equal(result3.Choice3_choice, Choice3_union_l_c);

zassert_equal(2, result1.tstr_count);
zassert_equal(2, result2.tstr_count);
zassert_equal(2, result3.Union_count);
zassert_equal(result3.Union[0].Union_choice, union_tstr_l_tstr_c);
zassert_equal(result3.Union[1].Union_choice, union_tstr_l_tstr_c);
zassert_zcbor_string(&result1.tstr[0], "hello", 5);
zassert_zcbor_string(&result2.tstr[0], "hello", 5);
zassert_zcbor_string(&result3.Union[0].tstr, "hello", 5);
zassert_zcbor_string(&result1.tstr[1], "world", 5);
zassert_zcbor_string(&result2.tstr[1], "world", 5);
zassert_zcbor_string(&result3.Union[1].tstr, "world", 5);

zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice3(nested_choices_payload3,
sizeof(nested_choices_payload3), &result3, &num_decode), NULL);

zassert_equal(result3.Choice3_choice, Choice3_union_l_c);

zassert_equal(3, result3.Union_count);
zassert_equal(result3.Union[0].Union_choice, union_tstr_l_tstr_c);
zassert_equal(result3.Union[1].Union_choice, Choice3_union_l_union_nil_c);
zassert_equal(result3.Union[2].Union_choice, union_tstr_l_tstr_c);
zassert_zcbor_string(&result3.Union[0].tstr, "hello", 5);
zassert_zcbor_string(&result3.Union[2].tstr, "world", 5);

zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice4(nested_choices_payload4,
sizeof(nested_choices_payload4), &result4, &num_decode), NULL);

zassert_equal(result4.Choice4_choice, Choice4_union_l_c);
zassert_equal(2, result4.Union_count);
zassert_equal(result4.Union_count, 2);
zassert_equal(result4.Union[0].Union_choice, Choice3_m_l_l_Choice3_m_l_Choice3_m_c);

zassert_equal(3, result4.Union[0].Choice3_m.Union_count);
zassert_equal(result4.Union[0].Choice3_m.Choice3_choice, Choice3_union_l_c);
zassert_equal(result4.Union[0].Choice3_m.Union[0].Union_choice, union_tstr_l_tstr_c);
zassert_equal(result4.Union[0].Choice3_m.Union[1].Union_choice, Choice3_union_l_union_nil_c);
zassert_equal(result4.Union[0].Choice3_m.Union[2].Union_choice, union_tstr_l_tstr_c);
zassert_zcbor_string(&result4.Union[0].Choice3_m.Union[0].tstr, "hello", 5);
zassert_zcbor_string(&result4.Union[0].Choice3_m.Union[2].tstr, "world", 5);

zassert_equal(result4.Union[1].Union_choice, Choice4_union_l_union_nil_c);

zassert_equal(ZCBOR_SUCCESS, cbor_decode_Choice5(nested_choices_payload5,
sizeof(nested_choices_payload5), &result5, &num_decode), NULL);

zassert_equal(result5.Choice5_choice, Choice5_union_l_c);
zassert_equal(2, result5.Union_count);
zassert_equal(result5.Union[0].Union_choice, union_map_c);
zassert_equal(result5.Union[0].tstr_l_count, 2);
zassert_equal(result5.Union[0].tstr_l[0].tstr_l_key, 0);
zassert_zcbor_string(&result5.Union[0].tstr_l[0].tstr, "hello", 5);
zassert_equal(result5.Union[0].tstr_l[1].tstr_l_key, 1);
zassert_zcbor_string(&result5.Union[0].tstr_l[1].tstr, "world", 5);
zassert_equal(result5.Union[1].Union_choice, Choice5_union_l_union_nil_c);
}


ZTEST_SUITE(cbor_decode_test5, NULL, NULL, NULL, NULL, NULL);
5 changes: 5 additions & 0 deletions tests/encode/test3_corner_cases/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ set(py_command
MapUnionPrimAlias
EmptyContainer
SingleElemList
Choice1
Choice2
Choice3
Choice4
Choice5
-e
${bit_arg}
--short-names
Expand Down
145 changes: 145 additions & 0 deletions tests/encode/test3_corner_cases/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,4 +1649,149 @@ ZTEST(cbor_encode_test3, test_single_elem_list)
}


ZTEST(cbor_encode_test3, test_nested_choices)
{
uint8_t nested_choices_exp_payload1[] = {0xf6};
uint8_t nested_choices_exp_payload2[] = {LIST(2),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
};
uint8_t nested_choices_exp_payload3[] = {LIST(3),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
0xf6,
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
};
uint8_t nested_choices_exp_payload4[] = {LIST(2),
LIST(1), LIST(1),
LIST(3),
LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
0xf6,
LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
END END
0xf6,
END
};
uint8_t nested_choices_exp_payload5[] = {LIST(2),
MAP(2),
0, LIST(1), 0x65, 'h', 'e', 'l', 'l', 'o', END
1, LIST(1), 0x65, 'w', 'o', 'r', 'l', 'd', END
END
0xf6,
END
};
struct Choice1_r input1 = {.Choice1_choice = Choice1_nil_c};
struct Choice2_r input2 = {.Choice2_choice = Choice2_nil_c};
struct Choice3_r input3 = {.Choice3_choice = Choice3_nil_c};
struct Choice4_r input4 = {.Choice4_choice = Choice4_nil_c};
struct Choice5_r input5 = {.Choice5_choice = Choice5_nil_c};

uint8_t payload[50];

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice1(payload,
sizeof(payload), &input1, NULL), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice2(payload,
sizeof(payload), &input2, NULL), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
sizeof(payload), &input3, NULL), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice4(payload,
sizeof(payload), &input4, NULL), NULL);
zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice5(payload,
sizeof(payload), &input5, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));
zassert_mem_equal(payload, nested_choices_exp_payload1, sizeof(nested_choices_exp_payload1));

input1.Choice1_choice = Choice1_tstr_l_l_c;
input1.tstr_count = 2;
input1.tstr[0].value = "hello";
input1.tstr[0].len = 5;
input1.tstr[1].value = "world";
input1.tstr[1].len = 5;

input2.Choice2_choice = Choice2_tstr_l_l_c;
input2.tstr_count = 2;
input2.tstr[0].value = "hello";
input2.tstr[0].len = 5;
input2.tstr[1].value = "world";
input2.tstr[1].len = 5;

input3.Choice3_choice = Choice3_union_l_c;
input3.Union_count = 2;
input3.Union[0].Union_choice = union_tstr_l_tstr_c;
input3.Union[1].Union_choice = union_tstr_l_tstr_c;
input3.Union[0].tstr.value = "hello";
input3.Union[0].tstr.len = 5;
input3.Union[1].tstr.value = "world";
input3.Union[1].tstr.len = 5;

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice1(payload,
sizeof(payload), &input1, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice2(payload,
sizeof(payload), &input2, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
sizeof(payload), &input3, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload2, sizeof(nested_choices_exp_payload2));

input3.Choice3_choice = Choice3_union_l_c;
input3.Union_count = 3;
input3.Union[0].Union_choice = union_tstr_l_tstr_c;
input3.Union[1].Union_choice = Choice3_union_l_union_nil_c;
input3.Union[2].Union_choice = union_tstr_l_tstr_c;
input3.Union[0].tstr.value = "hello";
input3.Union[0].tstr.len = 5;
input3.Union[2].tstr.value = "world";
input3.Union[2].tstr.len = 5;

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice3(payload,
sizeof(payload), &input3, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload3, sizeof(nested_choices_exp_payload3));

input4.Choice4_choice = Choice4_union_l_c;
input4.Union_count = 2;
input4.Union[0].Union_choice = Choice3_m_l_l_Choice3_m_l_Choice3_m_c;

input4.Union[0].Choice3_m.Choice3_choice = Choice3_union_l_c;
input4.Union[0].Choice3_m.Union_count = 3;
input4.Union[0].Choice3_m.Union[0].Union_choice = union_tstr_l_tstr_c;
input4.Union[0].Choice3_m.Union[1].Union_choice = Choice3_union_l_union_nil_c;
input4.Union[0].Choice3_m.Union[2].Union_choice = union_tstr_l_tstr_c;
input4.Union[0].Choice3_m.Union[0].tstr.value = "hello";
input4.Union[0].Choice3_m.Union[0].tstr.len = 5;
input4.Union[0].Choice3_m.Union[2].tstr.value = "world";
input4.Union[0].Choice3_m.Union[2].tstr.len = 5;

input4.Union[1].Union_choice = Choice4_union_l_union_nil_c;

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice4(payload,
sizeof(payload), &input4, NULL), NULL);

zassert_mem_equal(payload, nested_choices_exp_payload4, sizeof(nested_choices_exp_payload4));

input5.Choice5_choice = Choice5_union_l_c;
input5.Union_count = 2;
input5.Union[0].Union_choice = union_map_c;
input5.Union[0].tstr_l_count = 2;
input5.Union[0].tstr_l[0].tstr_l_key = 0;
input5.Union[0].tstr_l[0].tstr.value = "hello";
input5.Union[0].tstr_l[0].tstr.len = 5;
input5.Union[0].tstr_l[1].tstr_l_key = 1;
input5.Union[0].tstr_l[1].tstr.value = "world";
input5.Union[0].tstr_l[1].tstr.len = 5;
input5.Union[1].Union_choice = Choice5_union_l_union_nil_c;

zassert_equal(ZCBOR_SUCCESS, cbor_encode_Choice5(payload,
sizeof(payload), &input5, NULL), NULL);
zassert_mem_equal(payload, nested_choices_exp_payload5, sizeof(nested_choices_exp_payload5));
}


ZTEST_SUITE(cbor_encode_test3, NULL, NULL, NULL, NULL, NULL);
Loading

0 comments on commit bb962b9

Please sign in to comment.