Skip to content

Commit 2b3da39

Browse files
committed
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.
1 parent 469edf4 commit 2b3da39

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

tests/unittests/tests-nanocoap/tests-nanocoap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ static void test_nanocoap__get_multi_path(void)
182182
&token[0], 2, COAP_METHOD_GET, msgid);
183183

184184
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
185+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
185186

186187
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
187188
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
@@ -207,6 +208,7 @@ static void test_nanocoap__get_path_trailing_slash(void)
207208
&token[0], 2, COAP_METHOD_GET, msgid);
208209

209210
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
211+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
210212

211213
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
212214
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
@@ -231,6 +233,7 @@ static void test_nanocoap__get_root_path(void)
231233
&token[0], 2, COAP_METHOD_GET, msgid);
232234

233235
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
236+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
234237

235238
char uri[10] = {0};
236239
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
@@ -254,6 +257,7 @@ static void test_nanocoap__get_max_path(void)
254257
&token[0], 2, COAP_METHOD_GET, msgid);
255258

256259
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
260+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
257261

258262
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
259263
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
@@ -281,6 +285,7 @@ static void test_nanocoap__get_path_too_long(void)
281285
&token[0], 2, COAP_METHOD_GET, msgid);
282286

283287
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
288+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
284289

285290
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
286291
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
@@ -308,6 +313,7 @@ static void test_nanocoap__get_query(void)
308313
&token[0], 2, COAP_METHOD_GET, msgid);
309314

310315
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
316+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
311317

312318
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
313319
TEST_ASSERT_EQUAL_INT(path_opt_len, len);
@@ -350,6 +356,7 @@ static void test_nanocoap__get_multi_query(void)
350356
&token[0], 2, COAP_METHOD_GET, msgid);
351357

352358
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
359+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
353360

354361
uint8_t *query_pos = &pkt.payload[0];
355362
/* first opt header is 2 bytes long */
@@ -406,6 +413,7 @@ static void test_nanocoap__add_uri_query2(void)
406413
&token[0], 2, COAP_METHOD_GET, msgid);
407414

408415
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
416+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
409417

410418
/* includes key and value */
411419
char query[20] = {0};
@@ -463,6 +471,7 @@ static void test_nanocoap__option_add_buffer_max(void)
463471
&token[0], 2, COAP_METHOD_GET, msgid);
464472

465473
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
474+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
466475

467476
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
468477
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
@@ -487,6 +496,7 @@ static void __test_option_remove(uint16_t stride)
487496
&token[0], 2, COAP_METHOD_GET, msgid);
488497
/* shrink buffer to attempt overfill */
489498
coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len);
499+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
490500

491501
/* add seven options of options 1 to 7 */
492502
for (uint16_t count = 1; count < 8; count++) {
@@ -647,6 +657,7 @@ static void test_nanocoap__option_remove_no_payload(void)
647657
&token[0], 2, COAP_METHOD_GET, msgid);
648658
/* shrink buffer to attempt overfill */
649659
coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len);
660+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
650661

651662
len = coap_opt_add_uint(&pkt, 1U, 500U);
652663
TEST_ASSERT_EQUAL_INT(3U, len);
@@ -1004,6 +1015,7 @@ static void test_nanocoap__add_path_unterminated_string(void)
10041015
&token[0], 2, COAP_METHOD_GET, msgid);
10051016

10061017
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
1018+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
10071019
coap_opt_add_chars(&pkt, COAP_OPT_URI_PATH, &path[0], path_len, '/');
10081020

10091021
char uri[10] = {0};
@@ -1029,6 +1041,7 @@ static void test_nanocoap__add_get_proxy_uri(void)
10291041
&token[0], 2, COAP_METHOD_GET, msgid);
10301042

10311043
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
1044+
TEST_ASSERT_EQUAL_INT(len, coap_get_total_hdr_len(&pkt));
10321045

10331046
len = coap_opt_add_proxy_uri(&pkt, proxy_uri);
10341047

@@ -1093,6 +1106,7 @@ static void test_nanocoap__token_length_ext_16(void)
10931106
int res = coap_parse(&pkt, buf, 21);
10941107

10951108
TEST_ASSERT_EQUAL_INT(0, res);
1109+
TEST_ASSERT_EQUAL_INT(21, coap_get_total_hdr_len(&pkt));
10961110
TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt));
10971111
TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt));
10981112
TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt));
@@ -1121,6 +1135,7 @@ static void test_nanocoap__token_length_ext_269(void)
11211135
int res = coap_parse(&pkt, buf, 275);
11221136

11231137
TEST_ASSERT_EQUAL_INT(0, res);
1138+
TEST_ASSERT_EQUAL_INT(275, coap_get_total_hdr_len(&pkt));
11241139
TEST_ASSERT_EQUAL_INT(204, coap_get_code_decimal(&pkt));
11251140
TEST_ASSERT_EQUAL_INT(23, coap_get_id(&pkt));
11261141
TEST_ASSERT_EQUAL_INT(strlen(token), coap_get_token_len(&pkt));

0 commit comments

Comments
 (0)