From a13c0ec1d33c32b5132a2390d303cff409ef6f2c Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Fri, 4 May 2018 13:02:02 -0400 Subject: [PATCH] net/nanocoap: unit tests for pkt-based request --- .../unittests/tests-nanocoap/tests-nanocoap.c | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/unittests/tests-nanocoap/tests-nanocoap.c b/tests/unittests/tests-nanocoap/tests-nanocoap.c index d569434435925..b62e4ed85e5f4 100644 --- a/tests/unittests/tests-nanocoap/tests-nanocoap.c +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.c @@ -13,6 +13,7 @@ */ #include #include +#include #include "embUnit.h" @@ -45,10 +46,111 @@ static void test_nanocoap__hdr(void) TEST_ASSERT_EQUAL_STRING((char *)path, (char *)path_tmp); } +/* + * Client GET request with simple path. Test request generation. + * Request /time resource from libcoap example + */ +static void test_nanocoap__get_req(void) +{ + uint8_t buf[128]; + coap_pkt_t pkt; + uint16_t msgid = 0xABCD; + uint8_t token[2] = {0xDA, 0xEC}; + char path[] = "/time"; + size_t total_hdr_len = 6; + size_t total_opt_len = 5; + + size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); + TEST_ASSERT_EQUAL_INT(total_hdr_len, len); + + coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + + TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt)); + TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); + TEST_ASSERT_EQUAL_INT(total_hdr_len, coap_get_total_hdr_len(&pkt)); + TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt)); + TEST_ASSERT_EQUAL_INT(122, pkt.payload_len); + + len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); + TEST_ASSERT_EQUAL_INT(total_opt_len, len); + + char uri[10] = {0}; + coap_get_uri(&pkt, (uint8_t *)&uri[0]); + TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); + + len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE); + TEST_ASSERT_EQUAL_INT(total_hdr_len + total_opt_len, len); +} + +/* + * Builds on get_req test, to test payload and Content-Format option. + */ +static void test_nanocoap__put_req(void) +{ + uint8_t buf[128]; + coap_pkt_t pkt; + uint16_t msgid = 0xABCD; + uint8_t token[2] = {0xDA, 0xEC}; + char path[] = "/value"; + size_t total_hdr_len = 6; + size_t uri_opt_len = 6; + size_t fmt_opt_len = 1; + + size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_PUT, msgid); + TEST_ASSERT_EQUAL_INT(total_hdr_len, len); + + coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + + TEST_ASSERT_EQUAL_INT(COAP_METHOD_PUT, coap_get_code(&pkt)); + TEST_ASSERT_EQUAL_INT(122, pkt.payload_len); + + len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); + TEST_ASSERT_EQUAL_INT(uri_opt_len, len); + + len = coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_TEXT); + TEST_ASSERT_EQUAL_INT(fmt_opt_len, len); + TEST_ASSERT_EQUAL_INT(COAP_FORMAT_TEXT, coap_get_content_type(&pkt)); + + len = coap_opt_finish(&pkt, COAP_OPT_FINISH_PAYLOAD); + TEST_ASSERT_EQUAL_INT(total_hdr_len + uri_opt_len + fmt_opt_len + 1, len); + TEST_ASSERT_EQUAL_INT(0xFF, *(pkt.payload - 1)); + TEST_ASSERT_EQUAL_INT(&buf[0] + 128 - pkt.payload, pkt.payload_len); +} + +/* + * Builds on get_req test, to test path with multiple segments. + */ +static void test_nanocoap__get_multi_path(void) +{ + uint8_t buf[128]; + coap_pkt_t pkt; + uint16_t msgid = 0xABCD; + uint8_t token[2] = {0xDA, 0xEC}; + char path[] = "/ab/cde"; + size_t uri_opt_len = 7; + + size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, + &token[0], 2, COAP_METHOD_GET, msgid); + + coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); + + len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); + TEST_ASSERT_EQUAL_INT(uri_opt_len, len); + + char uri[10] = {0}; + coap_get_uri(&pkt, (uint8_t *)&uri[0]); + TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); +} + Test *tests_nanocoap_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_nanocoap__hdr), + new_TestFixture(test_nanocoap__get_req), + new_TestFixture(test_nanocoap__put_req), + new_TestFixture(test_nanocoap__get_multi_path), }; EMB_UNIT_TESTCALLER(nanocoap_tests, NULL, NULL, fixtures);