From 6b50db1713e0e31c311661f7dec9680130fc2374 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Fri, 8 Nov 2019 20:18:52 +0100 Subject: [PATCH] tests: add a simple proof of concept test for gnrc_sock + gnrc_neterr --- tests/gnrc_sock_neterr/Makefile | 12 ++++ tests/gnrc_sock_neterr/Makefile.ci | 10 ++++ tests/gnrc_sock_neterr/README.md | 11 ++++ tests/gnrc_sock_neterr/main.c | 79 ++++++++++++++++++++++++++ tests/gnrc_sock_neterr/tests/01-run.py | 21 +++++++ 5 files changed, 133 insertions(+) create mode 100644 tests/gnrc_sock_neterr/Makefile create mode 100644 tests/gnrc_sock_neterr/Makefile.ci create mode 100644 tests/gnrc_sock_neterr/README.md create mode 100644 tests/gnrc_sock_neterr/main.c create mode 100755 tests/gnrc_sock_neterr/tests/01-run.py diff --git a/tests/gnrc_sock_neterr/Makefile b/tests/gnrc_sock_neterr/Makefile new file mode 100644 index 0000000000..6dcf5c07a0 --- /dev/null +++ b/tests/gnrc_sock_neterr/Makefile @@ -0,0 +1,12 @@ +include ../Makefile.tests_common + +USEMODULE += auto_init_gnrc_netif +USEMODULE += gnrc_ipv6_default +USEMODULE += gnrc_neterr +USEMODULE += gnrc_sock_udp +USEMODULE += od +USEMODULE += xtimer + +CFLAGS += -DGNRC_PKTBUF_SIZE=200 + +include $(RIOTBASE)/Makefile.include diff --git a/tests/gnrc_sock_neterr/Makefile.ci b/tests/gnrc_sock_neterr/Makefile.ci new file mode 100644 index 0000000000..73c8498472 --- /dev/null +++ b/tests/gnrc_sock_neterr/Makefile.ci @@ -0,0 +1,10 @@ +BOARD_INSUFFICIENT_MEMORY := \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-nano \ + arduino-uno \ + atmega328p \ + nucleo-f031k6 \ + nucleo-f042k6 \ + stm32f030f4-demo \ + # diff --git a/tests/gnrc_sock_neterr/README.md b/tests/gnrc_sock_neterr/README.md new file mode 100644 index 0000000000..350578f201 --- /dev/null +++ b/tests/gnrc_sock_neterr/README.md @@ -0,0 +1,11 @@ +This is a simple proof of concept test, that `gnrc_sock` with `gnrc_neterr` +works as expected to report errors within the network stack up to the `sock` +object. + +It just tests if the sock reports `-EHOSTUNREACH` when sending to an unreachable +address. To confirm that this is indeed coming from the stack and not the `sock` +wrapper you can do + +``` +git grep EHOSTUNREACH -- sys/net/gnrc/sock +``` diff --git a/tests/gnrc_sock_neterr/main.c b/tests/gnrc_sock_neterr/main.c new file mode 100644 index 0000000000..61ee768024 --- /dev/null +++ b/tests/gnrc_sock_neterr/main.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2019 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. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * + * @author Martine Lenders + * + * @} + */ + +#include +#include +#include "msg.h" +#include "net/sock/udp.h" + +#define TEST_PORT (38664U) +#define TEST_LINK_LOCAL_REMOTE { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } +#define TEST_GLOBAL_REMOTE { 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } +#define TEST_PAYLOAD { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef } + +static const uint8_t _test_link_local_remote[] = TEST_LINK_LOCAL_REMOTE; +static const uint8_t _test_global_remote[] = TEST_GLOBAL_REMOTE; +static const uint8_t _test_payload[] = TEST_PAYLOAD; + +static msg_t _msg_queue[8U]; +static sock_udp_t _udp_sock; + +int _test_udp_send(sock_udp_ep_t *remote, char *errno_str, int exp_res) { + + int res = sock_udp_send(&_udp_sock, _test_payload, sizeof(_test_payload), + remote); + /* remote is not reachable or route does not exist, so it should return an + * error */ + if (-res == exp_res) { + printf("SUCCESS: error code %s (%li == %i)\n", + errno_str, (long)(-res), exp_res); + return 0; + } + else { + printf("FAILURE: gnrc_udp_send() had an unexpected error code: %li\n", + (long int)res); + return 1; + } +} + +int main(void) +{ + sock_udp_ep_t local = SOCK_IPV6_EP_ANY; + sock_udp_ep_t remote = SOCK_IPV6_EP_ANY; + + local.port = TEST_PORT; + remote.port = TEST_PORT - 1; + + msg_init_queue(_msg_queue, ARRAY_SIZE(_msg_queue)); + sock_udp_create(&_udp_sock, &local, NULL, 0); + + memcpy(remote.addr.ipv6, _test_link_local_remote, + sizeof(_test_link_local_remote)); + /* With a 6LN this send may succeed, as according to RFC 6775 link-local + * addresses are assumed to be generated from the EUI-64, reversing the + * procedure of generating the address from EUI-64 for address resolution */ + _test_udp_send(&remote, "EHOSTUNREACH", EHOSTUNREACH); + memcpy(remote.addr.ipv6, _test_global_remote, + sizeof(_test_global_remote)); + _test_udp_send(&remote, "ENETUNREACH", ENETUNREACH); +} + +/** @} */ diff --git a/tests/gnrc_sock_neterr/tests/01-run.py b/tests/gnrc_sock_neterr/tests/01-run.py new file mode 100755 index 0000000000..1129059b3e --- /dev/null +++ b/tests/gnrc_sock_neterr/tests/01-run.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 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. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect(r"SUCCESS: error code EHOSTUNREACH \((\d+) == (\d+)\)") + assert child.match.group(1) == child.match.group(2) + child.expect(r"SUCCESS: error code ENETUNREACH \((\d+) == (\d+)\)") + assert child.match.group(1) == child.match.group(2) + + +if __name__ == "__main__": + sys.exit(run(testfunc))