Skip to content

Commit 2e2915a

Browse files
hubertmisjukkar
authored andcommitted
tests: net: coap: Acknowledgement initialization
New test that verifies CoAP Acknowledgement initialization function that create a response packet for given request. Both request and expected response packets are given in the test as PDU. Signed-off-by: Hubert Miś <[email protected]>
1 parent 5e43418 commit 2e2915a

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

tests/net/lib/coap/src/main.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,124 @@ static int test_parse_malformed_marker(void)
523523
return result;
524524
}
525525

526+
static int test_parse_req_build_ack(void)
527+
{
528+
uint8_t pdu[] = { 0x45, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n',
529+
0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o',
530+
'a', 'd', 0x00 };
531+
uint8_t ack_pdu[] = { 0x65, 0x80, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' };
532+
struct coap_packet cpkt;
533+
struct coap_packet ack_cpkt;
534+
uint8_t *data = NULL;
535+
uint8_t *ack_data = NULL;
536+
int result = TC_FAIL;
537+
int r;
538+
539+
data = (uint8_t *)k_malloc(COAP_BUF_SIZE);
540+
if (!data) {
541+
goto done;
542+
}
543+
544+
memcpy(data, pdu, sizeof(pdu));
545+
546+
r = coap_packet_parse(&cpkt, data, sizeof(pdu), NULL, 0);
547+
if (r) {
548+
TC_PRINT("Could not parse packet\n");
549+
goto done;
550+
}
551+
552+
ack_data = (uint8_t *)k_malloc(COAP_BUF_SIZE);
553+
if (!ack_data) {
554+
goto done;
555+
}
556+
557+
r = coap_ack_init(&ack_cpkt, &cpkt, ack_data, COAP_BUF_SIZE,
558+
COAP_RESPONSE_CODE_BAD_REQUEST);
559+
if (r < 0) {
560+
TC_PRINT("Could not initialize ACK packet\n");
561+
goto done;
562+
}
563+
564+
if (ack_cpkt.offset != sizeof(ack_pdu)) {
565+
TC_PRINT("Different size from the reference packet\n");
566+
goto done;
567+
}
568+
569+
if (memcmp(ack_pdu, ack_cpkt.data, ack_cpkt.offset)) {
570+
TC_PRINT("Built packet doesn't match reference packet\n");
571+
goto done;
572+
}
573+
574+
result = TC_PASS;
575+
576+
done:
577+
k_free(data);
578+
k_free(ack_data);
579+
580+
TC_END_RESULT(result);
581+
582+
return result;
583+
}
584+
585+
static int test_parse_req_build_empty_ack(void)
586+
{
587+
uint8_t pdu[] = { 0x45, 0xA5, 0xDE, 0xAD, 't', 'o', 'k', 'e', 'n',
588+
0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o',
589+
'a', 'd', 0x00 };
590+
uint8_t ack_pdu[] = { 0x60, 0x00, 0xDE, 0xAD };
591+
struct coap_packet cpkt;
592+
struct coap_packet ack_cpkt;
593+
uint8_t *data = NULL;
594+
uint8_t *ack_data = NULL;
595+
int result = TC_FAIL;
596+
int r;
597+
598+
data = (uint8_t *)k_malloc(COAP_BUF_SIZE);
599+
if (!data) {
600+
goto done;
601+
}
602+
603+
memcpy(data, pdu, sizeof(pdu));
604+
605+
r = coap_packet_parse(&cpkt, data, sizeof(pdu), NULL, 0);
606+
if (r) {
607+
TC_PRINT("Could not parse packet\n");
608+
goto done;
609+
}
610+
611+
ack_data = (uint8_t *)k_malloc(COAP_BUF_SIZE);
612+
if (!ack_data) {
613+
goto done;
614+
}
615+
616+
r = coap_ack_init(&ack_cpkt, &cpkt, ack_data, COAP_BUF_SIZE,
617+
COAP_CODE_EMPTY);
618+
if (r < 0) {
619+
TC_PRINT("Could not initialize ACK packet\n");
620+
goto done;
621+
}
622+
623+
if (ack_cpkt.offset != sizeof(ack_pdu)) {
624+
TC_PRINT("Different size from the reference packet\n");
625+
goto done;
626+
}
627+
628+
if (memcmp(ack_pdu, ack_cpkt.data, ack_cpkt.offset)) {
629+
TC_PRINT("Built packet doesn't match reference packet\n");
630+
goto done;
631+
}
632+
633+
result = TC_PASS;
634+
635+
done:
636+
k_free(data);
637+
k_free(ack_data);
638+
639+
TC_END_RESULT(result);
640+
641+
return result;
642+
}
643+
526644
static int test_match_path_uri(void)
527645
{
528646
int result = TC_FAIL;
@@ -1471,6 +1589,9 @@ static const struct {
14711589
test_parse_malformed_opt_len_ext },
14721590
{ "Parse malformed empty payload with marker",
14731591
test_parse_malformed_marker, },
1592+
{ "Parse request and build ack ", test_parse_req_build_ack, },
1593+
{ "Parse request and build empty ack ",
1594+
test_parse_req_build_empty_ack, },
14741595
{ "Test match path uri", test_match_path_uri, },
14751596
{ "Test block sized 1 transfer", test_block1_size, },
14761597
{ "Test block sized 2 transfer", test_block2_size, },

0 commit comments

Comments
 (0)