From 170acbc4f6efbb5f1948767557152099728e81dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20G=C3=BCndo=C4=9Fan?= Date: Tue, 9 Feb 2016 13:21:30 +0100 Subject: [PATCH] rpl: srh: add unittests --- tests/unittests/tests-rpl_srh/Makefile | 1 + .../unittests/tests-rpl_srh/Makefile.include | 3 + tests/unittests/tests-rpl_srh/tests-rpl_srh.c | 124 ++++++++++++++++++ tests/unittests/tests-rpl_srh/tests-rpl_srh.h | 37 ++++++ 4 files changed, 165 insertions(+) create mode 100644 tests/unittests/tests-rpl_srh/Makefile create mode 100644 tests/unittests/tests-rpl_srh/Makefile.include create mode 100644 tests/unittests/tests-rpl_srh/tests-rpl_srh.c create mode 100644 tests/unittests/tests-rpl_srh/tests-rpl_srh.h diff --git a/tests/unittests/tests-rpl_srh/Makefile b/tests/unittests/tests-rpl_srh/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-rpl_srh/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-rpl_srh/Makefile.include b/tests/unittests/tests-rpl_srh/Makefile.include new file mode 100644 index 0000000000..4aa358aa3c --- /dev/null +++ b/tests/unittests/tests-rpl_srh/Makefile.include @@ -0,0 +1,3 @@ +USEMODULE += gnrc_ipv6 +USEMODULE += ipv6_addr +USEMODULE += gnrc_rpl_srh diff --git a/tests/unittests/tests-rpl_srh/tests-rpl_srh.c b/tests/unittests/tests-rpl_srh/tests-rpl_srh.c new file mode 100644 index 0000000000..5bc75a28c1 --- /dev/null +++ b/tests/unittests/tests-rpl_srh/tests-rpl_srh.c @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2016 Cenk Gündoğan + * + * 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/ipv6/addr.h" +#include "net/ipv6/ext.h" +#include "net/ipv6/hdr.h" +#include "net/gnrc/rpl/srh.h" + +#include "unittests-constants.h" +#include "tests-rpl_srh.h" + +#define IPV6_DST {{ 0x20, 0x01, 0xab, 0xcd, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x01 }} +#define IPV6_ADDR1 {{ 0x20, 0x01, 0xab, 0xcd, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x02 }} +#define IPV6_ADDR2 {{ 0x20, 0x01, 0xab, 0xcd, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x03 }} + +#define IPV6_ADDR1_ELIDED { 0x00, 0x00, 0x02 } +#define IPV6_ADDR2_ELIDED { 0x00, 0x00, 0x03 } +#define IPV6_ELIDED_PREFIX (13) + +#define SRH_SEG_LEFT (2) + +static void test_rpl_srh_nexthop_no_prefix_elided(void) +{ + ipv6_hdr_t hdr; + uint8_t buf[sizeof(gnrc_rpl_srh_t) + 2 * sizeof(ipv6_addr_t)] = { 0 }; + int res; + gnrc_rpl_srh_t *srh = (gnrc_rpl_srh_t *) buf; + uint8_t *vec = (uint8_t *) (srh + 1); + ipv6_addr_t a1 = IPV6_ADDR1, a2 = IPV6_ADDR2, dst = IPV6_DST, + expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; + + hdr.dst = dst; + + srh->len = (2 * sizeof(ipv6_addr_t)) / 8; + srh->seg_left = SRH_SEG_LEFT; + memcpy(vec, &a1, sizeof(a1)); + memcpy(vec + sizeof(a1), &a2, sizeof(a2)); + + /* first hop */ + res = gnrc_rpl_srh_process(&hdr, srh); + TEST_ASSERT_EQUAL_INT(res, EXT_RH_CODE_FORWARD); + TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 1, srh->seg_left); + TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected1)); + + /* second hop */ + res = gnrc_rpl_srh_process(&hdr, srh); + TEST_ASSERT_EQUAL_INT(res, EXT_RH_CODE_FORWARD); + TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 2, srh->seg_left); + TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected2)); +} + +static void test_rpl_srh_nexthop_prefix_elided(void) +{ + ipv6_hdr_t hdr; + uint8_t a1[3] = IPV6_ADDR1_ELIDED; + uint8_t a2[3] = IPV6_ADDR2_ELIDED; + uint8_t buf[sizeof(gnrc_rpl_srh_t) + sizeof(a1) + sizeof(a2)] = { 0 }; + int res; + gnrc_rpl_srh_t *srh = (gnrc_rpl_srh_t *) buf; + uint8_t *vec = (uint8_t *) (srh + 1); + ipv6_addr_t dst = IPV6_DST, expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; + + hdr.dst = dst; + + srh->len = (sizeof(a1) + sizeof(a2) + 2) / 8; + srh->seg_left = SRH_SEG_LEFT; + srh->compr = (IPV6_ELIDED_PREFIX << 4) | IPV6_ELIDED_PREFIX; + srh->pad_resv = 2 << 4; + memcpy(vec, &a1, sizeof(a1)); + memcpy(vec + sizeof(a1), &a2, sizeof(a2)); + + /* first hop */ + res = gnrc_rpl_srh_process(&hdr, srh); + TEST_ASSERT_EQUAL_INT(res, EXT_RH_CODE_FORWARD); + TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 1, srh->seg_left); + TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected1)); + + /* second hop */ + res = gnrc_rpl_srh_process(&hdr, srh); + TEST_ASSERT_EQUAL_INT(res, EXT_RH_CODE_FORWARD); + TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 2, srh->seg_left); + TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected2)); +} + +Test *tests_rpl_srh_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_rpl_srh_nexthop_no_prefix_elided), + new_TestFixture(test_rpl_srh_nexthop_prefix_elided), + }; + + EMB_UNIT_TESTCALLER(rpl_srh_tests, NULL, NULL, fixtures); + + return (Test *)&rpl_srh_tests; +} + +void tests_rpl_srh(void) +{ + TESTS_RUN(tests_rpl_srh_tests()); +} +/** @} */ diff --git a/tests/unittests/tests-rpl_srh/tests-rpl_srh.h b/tests/unittests/tests-rpl_srh/tests-rpl_srh.h new file mode 100644 index 0000000000..180ea81322 --- /dev/null +++ b/tests/unittests/tests-rpl_srh/tests-rpl_srh.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2016 Cenk Gündoğan + * + * 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 Unittests for the ``gnrc_rpl_srh`` module + * + * @author Cenk Gündoğan + */ +#ifndef TESTS_RPL_SRH_H_ +#define TESTS_RPL_SRH_H_ + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_rpl_srh(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_RPL_SRH_H_ */ +/** @} */