diff --git a/tests/gnrc_rpl_srh/Makefile b/tests/gnrc_rpl_srh/Makefile index ee151e7f8513918757ff46a19644f64a03fdcbb4..93532e0b88e7525c25fbd81169233dbf087de093 100644 --- a/tests/gnrc_rpl_srh/Makefile +++ b/tests/gnrc_rpl_srh/Makefile @@ -37,6 +37,8 @@ USEMODULE += gnrc_pktbuf_cmd # IPv6 extension headers USEMODULE += gnrc_rpl_srh USEMODULE += od +# Add unittest framework +USEMODULE += embunit # Add also the shell, some shell commands USEMODULE += shell USEMODULE += shell_commands diff --git a/tests/gnrc_rpl_srh/main.c b/tests/gnrc_rpl_srh/main.c index 5997e5098c905721b095cb6f0640ca4339748c48..26bb43adba089a108715b34858df5e1bd2594fa5 100644 --- a/tests/gnrc_rpl_srh/main.c +++ b/tests/gnrc_rpl_srh/main.c @@ -19,18 +19,187 @@ * @} */ +#include <stdlib.h> #include <stdio.h> +#include <string.h> +#include "embUnit.h" #include "shell.h" +#include "net/ipv6/addr.h" +#include "net/ipv6/ext.h" +#include "net/ipv6/hdr.h" #include "net/gnrc/pktbuf.h" #include "net/gnrc/pktdump.h" #include "net/gnrc/netreg.h" +#include "net/gnrc/rpl/srh.h" +#include "net/gnrc/ipv6/ext/rh.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_MCAST_ADDR {{ 0xff, 0x05, 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) +#define MAX_BUF_SIZE ((sizeof(gnrc_rpl_srh_t) + 2) + sizeof(ipv6_addr_t)) + +static ipv6_hdr_t hdr; +static uint8_t buf[MAX_BUF_SIZE]; static char line_buf[SHELL_DEFAULT_BUFSIZE]; static gnrc_netreg_entry_t ip_entry = GNRC_NETREG_ENTRY_INIT_PID( 0, KERNEL_PID_UNDEF ); +static void set_up_tests(void) +{ + memset(&hdr, 0, sizeof(hdr)); + memset(buf, 0, sizeof(buf)); +} + +static inline void _init_hdrs(gnrc_rpl_srh_t **srh, uint8_t **vec, + const ipv6_addr_t *dst) +{ + *srh = (gnrc_rpl_srh_t *)buf; + *vec = (uint8_t *)(*srh + 1); + memcpy(&hdr.dst, dst, sizeof(hdr.dst)); +} + +static void test_rpl_srh_dst_multicast(void) +{ + static const ipv6_addr_t a1 = IPV6_ADDR1, a2 = IPV6_ADDR2; + static const ipv6_addr_t mcast = IPV6_MCAST_ADDR; + gnrc_rpl_srh_t *srh; + uint8_t *vec; + void *err_ptr; + int res; + + _init_hdrs(&srh, &vec, &mcast); + 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)); + + res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); + TEST_ASSERT((&hdr.dst) == err_ptr); +} + +static void test_rpl_srh_route_multicast(void) +{ + static const ipv6_addr_t a1 = IPV6_ADDR1; + static const ipv6_addr_t mcast = IPV6_MCAST_ADDR; + static const ipv6_addr_t dst = IPV6_DST; + gnrc_rpl_srh_t *srh; + uint8_t *vec; + void *err_ptr; + int res; + + _init_hdrs(&srh, &vec, &dst); + srh->len = (2 * sizeof(ipv6_addr_t)) / 8; + srh->seg_left = SRH_SEG_LEFT; + memcpy(vec, &mcast, sizeof(mcast)); + memcpy(vec + sizeof(mcast), &a1, sizeof(a1)); + + res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); + TEST_ASSERT(vec == err_ptr); +} + +static void test_rpl_srh_too_many_seg_left(void) +{ + static const ipv6_addr_t a1 = IPV6_ADDR1; + static const ipv6_addr_t dst = IPV6_DST; + gnrc_rpl_srh_t *srh; + uint8_t *vec; + void *err_ptr; + int res; + + _init_hdrs(&srh, &vec, &dst); + srh->len = sizeof(ipv6_addr_t) / 8; + srh->seg_left = SRH_SEG_LEFT; + memcpy(vec, &a1, sizeof(a1)); + + res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); + TEST_ASSERT((&srh->seg_left) == err_ptr); +} + +static void test_rpl_srh_nexthop_no_prefix_elided(void) +{ + static const ipv6_addr_t a1 = IPV6_ADDR1, a2 = IPV6_ADDR2, dst = IPV6_DST; + static const ipv6_addr_t expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; + gnrc_rpl_srh_t *srh; + uint8_t *vec; + void *err_ptr; + int res; + + _init_hdrs(&srh, &vec, &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, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); + 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, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); + 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) +{ + static const ipv6_addr_t dst = IPV6_DST; + static const ipv6_addr_t expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; + gnrc_rpl_srh_t *srh; + uint8_t *vec; + void *err_ptr; + int res; + static const uint8_t a1[3] = IPV6_ADDR1_ELIDED; + static const uint8_t a2[3] = IPV6_ADDR2_ELIDED; + + _init_hdrs(&srh, &vec, &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, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); + 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, &err_ptr); + TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); + TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 2, srh->seg_left); + TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected2)); +} + +/* tools for external interaction */ static inline void _ipreg_usage(char *cmd) { printf("Usage: %s {reg|unreg}", cmd); @@ -70,8 +239,25 @@ static const shell_command_t shell_commands[] = { { NULL, NULL, NULL } }; +static void run_unittests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_rpl_srh_dst_multicast), + new_TestFixture(test_rpl_srh_route_multicast), + new_TestFixture(test_rpl_srh_too_many_seg_left), + new_TestFixture(test_rpl_srh_nexthop_no_prefix_elided), + new_TestFixture(test_rpl_srh_nexthop_prefix_elided), + }; + + EMB_UNIT_TESTCALLER(rpl_srh_tests, set_up_tests, NULL, fixtures); + TESTS_START(); + TESTS_RUN((Test *)&rpl_srh_tests); + TESTS_END(); +} + int main(void) { + run_unittests(); shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); return 0; } diff --git a/tests/gnrc_rpl_srh/tests/01-run.py b/tests/gnrc_rpl_srh/tests/01-run.py index f659bc488b51a297fb9930ae4b432ca3a609be9d..41797d723baee3190fa41af316c80cd7591e90fa 100755 --- a/tests/gnrc_rpl_srh/tests/01-run.py +++ b/tests/gnrc_rpl_srh/tests/01-run.py @@ -330,6 +330,8 @@ def testfunc(child): global sniffer tap = get_bridge(os.environ["TAP"]) + child.expect(r"OK \((\d+) tests\)") # wait for and check result of unittests + print(".", end="", flush=True) lladdr_src = get_host_lladdr(tap) child.sendline("ifconfig") child.expect("HWaddr: (?P<hwaddr>[A-Fa-f:0-9]+)") diff --git a/tests/unittests/tests-gnrc_rpl_srh/Makefile b/tests/unittests/tests-gnrc_rpl_srh/Makefile deleted file mode 100644 index 48422e909a47d7cd428d10fa73825060ccc8d8c2..0000000000000000000000000000000000000000 --- a/tests/unittests/tests-gnrc_rpl_srh/Makefile +++ /dev/null @@ -1 +0,0 @@ -include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-gnrc_rpl_srh/Makefile.include b/tests/unittests/tests-gnrc_rpl_srh/Makefile.include deleted file mode 100644 index 4aa358aa3cfb60facec2b0432d3f4bc64bdbef88..0000000000000000000000000000000000000000 --- a/tests/unittests/tests-gnrc_rpl_srh/Makefile.include +++ /dev/null @@ -1,3 +0,0 @@ -USEMODULE += gnrc_ipv6 -USEMODULE += ipv6_addr -USEMODULE += gnrc_rpl_srh diff --git a/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.c b/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.c deleted file mode 100644 index 6122c43937057de3d4c7ef291e0da3c49d3b5e94..0000000000000000000000000000000000000000 --- a/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.c +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (C) 2016 Cenk Gündoğan <mail@cgundogan.de> - * Copyright (C) 2018 Freie Universität Berlin - * - * 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 - * @author Cenk Gündoğan <mail@cgundogan.de> - * @author Martine Lenders <m.lenders@fu-berlin.de> - */ -#include <stdlib.h> -#include <string.h> -#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 "net/gnrc/ipv6/ext/rh.h" - -#include "unittests-constants.h" -#include "tests-gnrc_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_MCAST_ADDR {{ 0xff, 0x05, 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) -#define MAX_BUF_SIZE ((sizeof(gnrc_rpl_srh_t) + 2) + sizeof(ipv6_addr_t)) - -static ipv6_hdr_t hdr; -static uint8_t buf[MAX_BUF_SIZE]; - -static void set_up(void) -{ - memset(&hdr, 0, sizeof(hdr)); - memset(buf, 0, sizeof(buf)); -} - -static inline void _init_hdrs(gnrc_rpl_srh_t **srh, uint8_t **vec, - const ipv6_addr_t *dst) -{ - *srh = (gnrc_rpl_srh_t *)buf; - *vec = (uint8_t *)(*srh + 1); - memcpy(&hdr.dst, dst, sizeof(hdr.dst)); -} - -static void test_rpl_srh_dst_multicast(void) -{ - static const ipv6_addr_t a1 = IPV6_ADDR1, a2 = IPV6_ADDR2; - static const ipv6_addr_t mcast = IPV6_MCAST_ADDR; - gnrc_rpl_srh_t *srh; - uint8_t *vec; - void *err_ptr; - int res; - - _init_hdrs(&srh, &vec, &mcast); - 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)); - - res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); - TEST_ASSERT((&hdr.dst) == err_ptr); -} - -static void test_rpl_srh_route_multicast(void) -{ - static const ipv6_addr_t a1 = IPV6_ADDR1; - static const ipv6_addr_t mcast = IPV6_MCAST_ADDR; - static const ipv6_addr_t dst = IPV6_DST; - gnrc_rpl_srh_t *srh; - uint8_t *vec; - void *err_ptr; - int res; - - _init_hdrs(&srh, &vec, &dst); - srh->len = (2 * sizeof(ipv6_addr_t)) / 8; - srh->seg_left = SRH_SEG_LEFT; - memcpy(vec, &mcast, sizeof(mcast)); - memcpy(vec + sizeof(mcast), &a1, sizeof(a1)); - - res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); - TEST_ASSERT(vec == err_ptr); -} - -static void test_rpl_srh_too_many_seg_left(void) -{ - static const ipv6_addr_t a1 = IPV6_ADDR1; - static const ipv6_addr_t dst = IPV6_DST; - gnrc_rpl_srh_t *srh; - uint8_t *vec; - void *err_ptr; - int res; - - _init_hdrs(&srh, &vec, &dst); - srh->len = sizeof(ipv6_addr_t) / 8; - srh->seg_left = SRH_SEG_LEFT; - memcpy(vec, &a1, sizeof(a1)); - - res = gnrc_rpl_srh_process(&hdr, srh, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_ERROR); - TEST_ASSERT((&srh->seg_left) == err_ptr); -} - -static void test_rpl_srh_nexthop_no_prefix_elided(void) -{ - static const ipv6_addr_t a1 = IPV6_ADDR1, a2 = IPV6_ADDR2, dst = IPV6_DST; - static const ipv6_addr_t expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; - gnrc_rpl_srh_t *srh; - uint8_t *vec; - void *err_ptr; - int res; - - _init_hdrs(&srh, &vec, &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, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); - 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, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); - 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) -{ - static const ipv6_addr_t dst = IPV6_DST; - static const ipv6_addr_t expected1 = IPV6_ADDR1, expected2 = IPV6_ADDR2; - gnrc_rpl_srh_t *srh; - uint8_t *vec; - void *err_ptr; - int res; - static const uint8_t a1[3] = IPV6_ADDR1_ELIDED; - static const uint8_t a2[3] = IPV6_ADDR2_ELIDED; - - _init_hdrs(&srh, &vec, &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, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); - 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, &err_ptr); - TEST_ASSERT_EQUAL_INT(res, GNRC_IPV6_EXT_RH_FORWARDED); - TEST_ASSERT_EQUAL_INT(SRH_SEG_LEFT - 2, srh->seg_left); - TEST_ASSERT(ipv6_addr_equal(&hdr.dst, &expected2)); -} - -static Test *tests_rpl_srh_tests(void) -{ - EMB_UNIT_TESTFIXTURES(fixtures) { - new_TestFixture(test_rpl_srh_dst_multicast), - new_TestFixture(test_rpl_srh_route_multicast), - new_TestFixture(test_rpl_srh_too_many_seg_left), - new_TestFixture(test_rpl_srh_nexthop_no_prefix_elided), - new_TestFixture(test_rpl_srh_nexthop_prefix_elided), - }; - - EMB_UNIT_TESTCALLER(rpl_srh_tests, set_up, NULL, fixtures); - - return (Test *)&rpl_srh_tests; -} - -void tests_gnrc_rpl_srh(void) -{ - TESTS_RUN(tests_rpl_srh_tests()); -} -/** @} */ diff --git a/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.h b/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.h deleted file mode 100644 index 50e3725e9123a6b539f28eadfd11741de02fa207..0000000000000000000000000000000000000000 --- a/tests/unittests/tests-gnrc_rpl_srh/tests-gnrc_rpl_srh.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2016 Cenk Gündoğan <mail@cgundogan.de> - * - * 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 <mail@cgundogan.de> - */ -#ifndef TESTS_GNRC_RPL_SRH_H -#define TESTS_GNRC_RPL_SRH_H - -#include "embUnit.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief The entry point of this test suite. - */ -void tests_gnrc_rpl_srh(void); - -#ifdef __cplusplus -} -#endif - -#endif /* TESTS_GNRC_RPL_SRH_H */ -/** @} */