From 7e5d14fbf01f823aabbfbb525165b5800c25d13d Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Fri, 12 Jan 2018 05:41:37 -0500 Subject: [PATCH] net/nanocoap: add test for message ID byte order --- tests/unittests/tests-nanocoap/Makefile | 1 + .../unittests/tests-nanocoap/Makefile.include | 1 + .../unittests/tests-nanocoap/tests-nanocoap.c | 58 +++++++++++++++++++ .../unittests/tests-nanocoap/tests-nanocoap.h | 37 ++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 tests/unittests/tests-nanocoap/Makefile create mode 100644 tests/unittests/tests-nanocoap/Makefile.include create mode 100644 tests/unittests/tests-nanocoap/tests-nanocoap.c create mode 100644 tests/unittests/tests-nanocoap/tests-nanocoap.h diff --git a/tests/unittests/tests-nanocoap/Makefile b/tests/unittests/tests-nanocoap/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-nanocoap/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-nanocoap/Makefile.include b/tests/unittests/tests-nanocoap/Makefile.include new file mode 100644 index 0000000000..cd6b8172a7 --- /dev/null +++ b/tests/unittests/tests-nanocoap/Makefile.include @@ -0,0 +1 @@ +USEMODULE += nanocoap diff --git a/tests/unittests/tests-nanocoap/tests-nanocoap.c b/tests/unittests/tests-nanocoap/tests-nanocoap.c new file mode 100644 index 0000000000..8bad21e6a2 --- /dev/null +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 Ken Bannister. All rights reserved. + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + */ +#include +#include + +#include "embUnit.h" + +#include "net/nanocoap.h" + +#include "unittests-constants.h" +#include "tests-nanocoap.h" + +/* + * Validates encoded message ID byte order. + */ +static void test_nanocoap__req_msgid(void) +{ + uint8_t buf[128]; + uint16_t msgid = 0xABCD; + char path[] = "/test"; + + uint8_t *pktpos = &buf[0]; + pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_REQ, NULL, 0, COAP_METHOD_GET, msgid); + pktpos += coap_put_option_uri(pktpos, 0, path, COAP_OPT_URI_PATH); + + coap_pkt_t pkt; + coap_parse(&pkt, &buf[0], pktpos - &buf[0]); + + TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt)); +} + +Test *tests_nanocoap_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_nanocoap__req_msgid), + }; + + EMB_UNIT_TESTCALLER(nanocoap_tests, NULL, NULL, fixtures); + + return (Test *)&nanocoap_tests; +} + +void tests_nanocoap(void) +{ + TESTS_RUN(tests_nanocoap_tests()); +} +/** @} */ diff --git a/tests/unittests/tests-nanocoap/tests-nanocoap.h b/tests/unittests/tests-nanocoap/tests-nanocoap.h new file mode 100644 index 0000000000..61b7af6844 --- /dev/null +++ b/tests/unittests/tests-nanocoap/tests-nanocoap.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018 Ken Bannister. All rights reserved. + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unit tests for the nanocoap module + * + * @author Ken Bannister + */ +#ifndef TESTS_NANOCOAP_H +#define TESTS_NANOCOAP_H + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_nanocoap(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_NANOCOAP_H */ +/** @} */