Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
net/nanocoap: add unit test for iterate options
  • Loading branch information
kb2ma committed Aug 24, 2019
1 parent 4e89741 commit 47ef5c1
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/unittests/tests-nanocoap/tests-nanocoap.c
Expand Up @@ -547,6 +547,79 @@ static void test_nanocoap__server_option_count_overflow(void)
TEST_ASSERT(res < 0);
}

/*
* Helper for options tests below.
* POST request to a CoRE RD server to update the entries for a node
* from RIOT cord_ep example. Generated by RIOT.
* Includes 4 options:
* Uri-Path: resourcedirectory
* Content-Format: 40 (0x28)
* Uri-Query: ep-RIOT-0C49232323232323
* Uri-Query: lt=60
* Payload: </node/info> (absent if omit_payload)
*/
static int _read_rd_post_req(coap_pkt_t *pkt, bool omit_payload)
{
static uint8_t pkt_data[] = {
0x42, 0x02, 0x20, 0x92, 0xb9, 0x27, 0xbd, 0x04,
0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6f, 0x72,
0x79, 0x11, 0x28, 0x3d, 0x0b, 0x65, 0x70, 0x3d,
0x52, 0x49, 0x4f, 0x54, 0x2d, 0x30, 0x43, 0x34,
0x39, 0x32, 0x33, 0x32, 0x33, 0x32, 0x33, 0x32,
0x33, 0x32, 0x33, 0x32, 0x33, 0x05, 0x6c, 0x74,
0x3d, 0x36, 0x30, 0xff, 0x3c, 0x2f, 0x6e, 0x6f,
0x64, 0x65, 0x2f, 0x69, 0x6e, 0x66, 0x6f, 0x3e
};

size_t len = omit_payload ? 59 : sizeof(pkt_data);
return coap_parse(pkt, pkt_data, len);
}

/*
* Tests use of coap_opt_get_next() to iterate over options.
*/
static void test_nanocoap__options_iterate(void)
{
coap_pkt_t pkt;
int res = _read_rd_post_req(&pkt, true);
TEST_ASSERT_EQUAL_INT(0, res);

/* read all options */
coap_optpos_t opt = {0, 0};
uint8_t *value;
ssize_t exp_len[] = {17, 1, 24, 5, -ENOENT};
ssize_t exp_optnum[] = {COAP_OPT_URI_PATH, COAP_OPT_CONTENT_FORMAT,
COAP_OPT_URI_QUERY, COAP_OPT_URI_QUERY};

for (int i = 0; i < 5; i++) {
ssize_t optlen = coap_opt_get_next(&pkt, &opt, &value, !i);
TEST_ASSERT_EQUAL_INT(exp_len[i], optlen);
if (optlen >= 0) {
TEST_ASSERT_EQUAL_INT(exp_optnum[i], opt.opt_num);
}
else {
TEST_ASSERT_EQUAL_INT(4, i);
}
}

/* test with no payload to verify end of options handling */
memset(&pkt, 0, sizeof(pkt));
res = _read_rd_post_req(&pkt, false);
TEST_ASSERT_EQUAL_INT(0, res);

for (int i = 0; i < 5; i++) {
ssize_t optlen = coap_opt_get_next(&pkt, &opt, &value, !i);
TEST_ASSERT_EQUAL_INT(exp_len[i], optlen);
if (optlen >= 0) {
TEST_ASSERT_EQUAL_INT(exp_optnum[i], opt.opt_num);
}
else {
TEST_ASSERT_EQUAL_INT(4, i);
}
}
}

Test *tests_nanocoap_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand All @@ -561,6 +634,7 @@ Test *tests_nanocoap_tests(void)
new_TestFixture(test_nanocoap__get_query),
new_TestFixture(test_nanocoap__get_multi_query),
new_TestFixture(test_nanocoap__option_add_buffer_max),
new_TestFixture(test_nanocoap__options_iterate),
new_TestFixture(test_nanocoap__server_get_req),
new_TestFixture(test_nanocoap__server_reply_simple),
new_TestFixture(test_nanocoap__server_get_req_con),
Expand Down

0 comments on commit 47ef5c1

Please sign in to comment.