From 2b3da3911ca049c3680ff0964f5d5bfa88a023e8 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Fri, 1 Nov 2024 13:37:20 +0100 Subject: [PATCH] tests/unittests: increase coverage for coap_get_total_hdr_len() Previously the corner case when RFC 8974 extended TKL fields are used the result of coap_get_totel_hdr_len() was not tested, resulting in a bug slipping through the test. This increase the test coverage. --- tests/unittests/tests-nanocoap/tests-nanocoap.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/unittests/tests-nanocoap/tests-nanocoap.c b/tests/unittests/tests-nanocoap/tests-nanocoap.c index 99e0fe5a94..b76ebeb035 100644 --- a/tests/unittests/tests-nanocoap/tests-nanocoap.c +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.c @@ -182,6 +182,7 @@ static void test_nanocoap__get_multi_path(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(uri_opt_len, len); @@ -207,6 +208,7 @@ static void test_nanocoap__get_path_trailing_slash(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(uri_opt_len, len); @@ -231,6 +233,7 @@ static void test_nanocoap__get_root_path(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); char uri[10] = {0}; coap_get_uri_path(&pkt, (uint8_t *)&uri[0]); @@ -254,6 +257,7 @@ static void test_nanocoap__get_max_path(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(uri_opt_len, len); @@ -281,6 +285,7 @@ static void test_nanocoap__get_path_too_long(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(uri_opt_len, len); @@ -308,6 +313,7 @@ static void test_nanocoap__get_query(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(path_opt_len, len); @@ -350,6 +356,7 @@ static void test_nanocoap__get_multi_query(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); uint8_t *query_pos = &pkt.payload[0]; /* first opt header is 2 bytes long */ @@ -406,6 +413,7 @@ static void test_nanocoap__add_uri_query2(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); /* includes key and value */ char query[20] = {0}; @@ -463,6 +471,7 @@ static void test_nanocoap__option_add_buffer_max(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(uri_opt_len, len); @@ -487,6 +496,7 @@ static void __test_option_remove(uint16_t stride) &token[0], 2, COAP_METHOD_GET, msgid); /* shrink buffer to attempt overfill */ coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); /* add seven options of options 1 to 7 */ for (uint16_t count = 1; count < 8; count++) { @@ -647,6 +657,7 @@ static void test_nanocoap__option_remove_no_payload(void) &token[0], 2, COAP_METHOD_GET, msgid); /* shrink buffer to attempt overfill */ coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_uint(&pkt, 1U, 500U); TEST_ASSERT_EQUAL_INT(3U, len); @@ -1004,6 +1015,7 @@ static void test_nanocoap__add_path_unterminated_string(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); coap_opt_add_chars(&pkt, COAP_OPT_URI_PATH, &path[0], path_len, '/'); char uri[10] = {0}; @@ -1029,6 +1041,7 @@ static void test_nanocoap__add_get_proxy_uri(void) &token[0], 2, COAP_METHOD_GET, msgid); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt)); len = coap_opt_add_proxy_uri(&pkt, proxy_uri); @@ -1093,6 +1106,7 @@ static void test_nanocoap__token_length_ext_16(void) int res = coap_parse(&pkt, buf, 21); TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt)); @@ -1121,6 +1135,7 @@ static void test_nanocoap__token_length_ext_269(void) int res = coap_parse(&pkt, buf, 275); TEST_ASSERT_EQUAL_INT(0, res); + TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt)); TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt)); TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt));