Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/net/nanocoap: fix coap_get_total_hdr_len() #20946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,18 +582,6 @@
return coap_hdr_data_ptr(pkt->hdr);
}

/**
* @brief Get the total header length (4-byte header + token length)
*
* @param[in] pkt CoAP packet
*
* @returns total header length
*/
static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
{
return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
}

/**
* @brief Get the total length of a CoAP packet in the packet buffer
*
Expand Down Expand Up @@ -676,6 +664,19 @@
return ((uint8_t *)hdr) + sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(hdr);
}

/**
* @brief Get the total header length (4-byte header + token length)
*
* @param[in] pkt CoAP packet
*
* @returns total header length
*/
static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
{
return sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
coap_get_token_len(pkt);
}

/**
* @brief Write the given raw message code to given CoAP header
*
Expand Down Expand Up @@ -1880,7 +1881,7 @@
*
* @returns amount of bytes written to @p buf
*/
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);

Check warning on line 1884 in sys/include/net/nanocoap.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

/**
* @brief Insert block1 option into buffer
Expand Down Expand Up @@ -2319,4 +2320,4 @@
}
#endif
#endif /* NET_NANOCOAP_H */
/** @} */

Check warning on line 2323 in sys/include/net/nanocoap.h

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
15 changes: 15 additions & 0 deletions tests/unittests/tests-nanocoap/tests-nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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]);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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};
Expand All @@ -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);

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
Loading