diff --git a/tests/gnrc_sock_ip/constants.h b/tests/gnrc_sock_ip/constants.h new file mode 100644 index 000000000000..88776e4ee242 --- /dev/null +++ b/tests/gnrc_sock_ip/constants.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 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. + */ + +/** + * @defgroup + * @ingroup + * @brief + * @{ + * + * @file + * @brief + * + * @author Martine Lenders + */ +#ifndef CONSTANTS_H_ +#define CONSTANTS_H_ + + +#ifdef __cplusplus +extern "C" { +#endif + +#define _TEST_PROTO (254) /* https://tools.ietf.org/html/rfc3692#section-2.1 */ +#define _TEST_NETIF (31) +#define _TEST_TIMEOUT (1000000U) +#define _TEST_ADDR_LOCAL { 0x7f, 0xc4, 0x11, 0x5a, 0xe6, 0x91, 0x8d, 0x5d, \ + 0x8c, 0xd1, 0x47, 0x07, 0xb7, 0x6f, 0x9b, 0x48 } +#define _TEST_ADDR_REMOTE { 0xe8, 0xb3, 0xb2, 0xe6, 0x70, 0xd4, 0x55, 0xba, \ + 0x93, 0xcf, 0x11, 0xe1, 0x72, 0x44, 0xc5, 0x9d } +#define _TEST_ADDR_WRONG { 0x2a, 0xce, 0x5d, 0x4e, 0xc8, 0xbf, 0x86, 0xf7, \ + 0x85, 0x49, 0xb4, 0x19, 0xf2, 0x28, 0xde, 0x9b } + +#ifdef __cplusplus +} +#endif + +#endif /* CONSTANTS_H_ */ +/** @} */ diff --git a/tests/gnrc_sock_ip/main.c b/tests/gnrc_sock_ip/main.c index 01e38ecd2a7c..2c26835eb3c6 100644 --- a/tests/gnrc_sock_ip/main.c +++ b/tests/gnrc_sock_ip/main.c @@ -24,47 +24,21 @@ #include "net/af.h" #include "net/sock/ip.h" -#include "net/gnrc/ipv6.h" -#include "net/gnrc/netif/hdr.h" -#include "net/gnrc/netreg.h" -#include "sched.h" +#include "xtimer.h" + +#include "constants.h" +#include "stack.h" -#define _MSG_QUEUE_SIZE (4) #define _TEST_BUFFER_SIZE (128) -#define _TEST_NETIF (31) -#define _TEST_TIMEOUT (1000000U) -#define _TEST_PROTO (254) /* https://tools.ietf.org/html/rfc3692#section-2.1 */ -#define _TEST_ADDR_LOCAL { 0x7f, 0xc4, 0x11, 0x5a, 0xe6, 0x91, 0x8d, 0x5d, \ - 0x8c, 0xd1, 0x47, 0x07, 0xb7, 0x6f, 0x9b, 0x48 } -#define _TEST_ADDR_REMOTE { 0xe8, 0xb3, 0xb2, 0xe6, 0x70, 0xd4, 0x55, 0xba, \ - 0x93, 0xcf, 0x11, 0xe1, 0x72, 0x44, 0xc5, 0x9d } -#define _TEST_ADDR_WRONG { 0x2a, 0xce, 0x5d, 0x4e, 0xc8, 0xbf, 0x86, 0xf7, \ - 0x85, 0x49, 0xb4, 0x19, 0xf2, 0x28, 0xde, 0x9b } + #define CALL(fn) puts("Calling " #fn); fn; tear_down() -static msg_t _msg_queue[_MSG_QUEUE_SIZE]; -static gnrc_netreg_entry_t _ip_handler; static sock_ip_t _sock, _sock2; static uint8_t _test_buffer[_TEST_BUFFER_SIZE]; -static bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint8_t proto, void *data, size_t data_len, - uint16_t netif); -static bool _check_stack(void); -static bool _check_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint8_t proto, void *data, size_t data_len, - uint16_t netif); - static void tear_down(void) { - const int pre_close_num = gnrc_netreg_num(GNRC_NETTYPE_IPV6, - _TEST_PROTO); - sock_ip_close(&_sock); - const int post_close_num = gnrc_netreg_num(GNRC_NETTYPE_IPV6, - _TEST_PROTO); - assert(pre_close_num >= post_close_num); - assert(0 == post_close_num); memset(&_sock, 0, sizeof(_sock)); } @@ -77,7 +51,6 @@ static void test_sock_ip_create__EAFNOSUPPORT(void) SOCK_FLAGS_REUSE_EP)); assert(-EAFNOSUPPORT == sock_ip_create(&_sock, NULL, &remote, _TEST_PROTO, SOCK_FLAGS_REUSE_EP)); - assert(0 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_create__EINVAL(void) @@ -99,7 +72,6 @@ static void test_sock_ip_create__no_endpoints(void) SOCK_FLAGS_REUSE_EP)); assert(-EADDRNOTAVAIL == sock_ip_get_local(&_sock, &ep)); assert(-ENOTCONN == sock_ip_get_remote(&_sock, &ep)); - assert(1 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_create__only_local(void) @@ -115,7 +87,6 @@ static void test_sock_ip_create__only_local(void) sizeof(ipv6_addr_t)) == 0); assert(SOCK_ADDR_ANY_NETIF == ep.netif); assert(-ENOTCONN == sock_ip_get_remote(&_sock, &ep)); - assert(1 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_create__only_local_reuse_ep(void) @@ -139,9 +110,7 @@ static void test_sock_ip_create__only_local_reuse_ep(void) sizeof(ipv6_addr_t)) == 0); assert(SOCK_ADDR_ANY_NETIF == ep2.netif); assert(-ENOTCONN == sock_ip_get_remote(&_sock, &ep2)); - assert(2 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); sock_ip_close(&_sock2); - assert(1 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_create__only_remote(void) @@ -157,7 +126,6 @@ static void test_sock_ip_create__only_remote(void) assert(memcmp(&ipv6_addr_unspecified, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0); assert(SOCK_ADDR_ANY_NETIF == ep.netif); - assert(1 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_create__full(void) @@ -178,7 +146,6 @@ static void test_sock_ip_create__full(void) assert(memcmp(&ipv6_addr_unspecified, &ep.addr.ipv6, sizeof(ipv6_addr_t)) == 0); assert(SOCK_ADDR_ANY_NETIF == ep.netif); - assert(1 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); } static void test_sock_ip_recv__EADDRNOTAVAIL(void) @@ -201,7 +168,7 @@ static void test_sock_ip_recv__ENOBUFS(void) assert(_inject_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); assert(-ENOBUFS == sock_ip_recv(&_sock, _test_buffer, 2, 0, NULL)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__EPROTO(void) @@ -218,7 +185,7 @@ static void test_sock_ip_recv__EPROTO(void) sizeof("ABCD"), _TEST_NETIF)); assert(-EPROTO == sock_ip_recv(&_sock, _test_buffer, sizeof(_test_buffer), 0, NULL)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__ETIMEDOUT(void) @@ -251,7 +218,7 @@ static void test_sock_ip_recv__socketed(void) sizeof("ABCD"), _TEST_NETIF)); assert(sizeof("ABCD") == sock_ip_recv(&_sock, _test_buffer, sizeof(_test_buffer), 0, NULL)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__socketed_with_remote(void) @@ -274,7 +241,7 @@ static void test_sock_ip_recv__socketed_with_remote(void) assert(AF_INET6 == result.family); assert(memcmp(&result.addr, &src_addr, sizeof(result.addr)) == 0); assert(_TEST_NETIF == result.netif); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__unsocketed(void) @@ -292,7 +259,7 @@ static void test_sock_ip_recv__unsocketed(void) sizeof("ABCD"), _TEST_NETIF)); assert(sizeof("ABCD") == sock_ip_recv(&_sock, _test_buffer, sizeof(_test_buffer), 0, NULL)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__unsocketed_with_remote(void) @@ -313,7 +280,7 @@ static void test_sock_ip_recv__unsocketed_with_remote(void) assert(AF_INET6 == result.family); assert(memcmp(&result.addr, &src_addr, sizeof(result.addr)) == 0); assert(_TEST_NETIF == result.netif); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_recv__with_timeout(void) @@ -335,7 +302,7 @@ static void test_sock_ip_recv__with_timeout(void) assert(AF_INET6 == result.family); assert(memcmp(&result.addr, &src_addr, sizeof(result.addr)) == 0); assert(_TEST_NETIF == result.netif); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__AFNOSUPPORT(void) @@ -345,7 +312,7 @@ static void test_sock_ip_send__AFNOSUPPORT(void) assert(-EAFNOSUPPORT == sock_ip_send(NULL, "ABCD", sizeof("ABCD"), _TEST_PROTO, &remote)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__EINVAL(void) @@ -361,23 +328,16 @@ static void test_sock_ip_send__EINVAL(void) SOCK_FLAGS_REUSE_EP)); assert(-EINVAL == sock_ip_send(&_sock, "ABCD", sizeof("ABCD"), _TEST_PROTO, &remote)); - assert(_check_stack()); -} - -static void test_sock_ip_send__ENOTCONN_no_sock(void) -{ - assert(-ENOTCONN == sock_ip_send(NULL, "ABCD", sizeof("ABCD"), _TEST_PROTO, - NULL)); - assert(_check_stack()); + assert(_check_net()); } -static void test_sock_ip_send__ENOTCONN_sock_not_socketed(void) +static void test_sock_ip_send__ENOTCONN(void) { assert(0 == sock_ip_create(&_sock, NULL, NULL, _TEST_PROTO, SOCK_FLAGS_REUSE_EP)); assert(-ENOTCONN == sock_ip_send(&_sock, "ABCD", sizeof("ABCD"), _TEST_PROTO, NULL)); - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__socketed_no_local_no_netif(void) @@ -393,7 +353,7 @@ static void test_sock_ip_send__socketed_no_local_no_netif(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), SOCK_ADDR_ANY_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__socketed_no_netif(void) @@ -412,7 +372,7 @@ static void test_sock_ip_send__socketed_no_netif(void) assert(_check_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), SOCK_ADDR_ANY_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__socketed_no_local(void) @@ -429,7 +389,7 @@ static void test_sock_ip_send__socketed_no_local(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__socketed(void) @@ -449,7 +409,7 @@ static void test_sock_ip_send__socketed(void) assert(_check_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__socketed_other_remote(void) @@ -471,7 +431,7 @@ static void test_sock_ip_send__socketed_other_remote(void) assert(_check_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__unsocketed_no_local_no_netif(void) @@ -487,7 +447,7 @@ static void test_sock_ip_send__unsocketed_no_local_no_netif(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), SOCK_ADDR_ANY_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__unsocketed_no_netif(void) @@ -506,7 +466,7 @@ static void test_sock_ip_send__unsocketed_no_netif(void) assert(_check_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), SOCK_ADDR_ANY_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__unsocketed_no_local(void) @@ -523,7 +483,7 @@ static void test_sock_ip_send__unsocketed_no_local(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__unsocketed(void) @@ -543,7 +503,7 @@ static void test_sock_ip_send__unsocketed(void) assert(_check_packet(&src_addr, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__no_sock_no_netif(void) @@ -557,7 +517,7 @@ static void test_sock_ip_send__no_sock_no_netif(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), SOCK_ADDR_ANY_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } static void test_sock_ip_send__no_sock(void) @@ -572,17 +532,13 @@ static void test_sock_ip_send__no_sock(void) assert(_check_packet(&ipv6_addr_unspecified, &dst_addr, _TEST_PROTO, "ABCD", sizeof("ABCD"), _TEST_NETIF)); xtimer_usleep(1000); /* let GNRC stack finish */ - assert(_check_stack()); + assert(_check_net()); } int main(void) { - assert(0 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); + _net_init(); tear_down(); - msg_init_queue(_msg_queue, _MSG_QUEUE_SIZE); - gnrc_netreg_entry_init_pid(&_ip_handler, GNRC_NETREG_DEMUX_CTX_ALL, - sched_active_pid); - assert(0 == gnrc_netreg_num(GNRC_NETTYPE_IPV6, _TEST_PROTO)); /* EADDRINUSE does not apply for GNRC */ CALL(test_sock_ip_create__EAFNOSUPPORT()); CALL(test_sock_ip_create__EINVAL()); @@ -604,11 +560,10 @@ int main(void) CALL(test_sock_ip_recv__unsocketed()); CALL(test_sock_ip_recv__unsocketed_with_remote()); CALL(test_sock_ip_recv__with_timeout()); - gnrc_netreg_register(GNRC_NETTYPE_IPV6, &_ip_handler); + _prepare_send_checks(); CALL(test_sock_ip_send__AFNOSUPPORT()); CALL(test_sock_ip_send__EINVAL()); - CALL(test_sock_ip_send__ENOTCONN_no_sock()); - CALL(test_sock_ip_send__ENOTCONN_sock_not_socketed()); + CALL(test_sock_ip_send__ENOTCONN()); CALL(test_sock_ip_send__socketed_no_local_no_netif()); CALL(test_sock_ip_send__socketed_no_netif()); CALL(test_sock_ip_send__socketed_no_local()); @@ -625,104 +580,3 @@ int main(void) return 0; } - -static gnrc_pktsnip_t *_build_ipv6_packet(const ipv6_addr_t *src, - const ipv6_addr_t *dst, uint8_t nh, - void *data, size_t data_len, - uint16_t netif) -{ - gnrc_pktsnip_t *netif_hdr, *ipv6, *payload; - ipv6_hdr_t *ipv6_hdr; - - if ((netif > INT16_MAX) || (data_len > UINT16_MAX)) { - return NULL; - } - - payload = gnrc_pktbuf_add(NULL, data, data_len, GNRC_NETTYPE_UNDEF); - if (payload == NULL) { - return NULL; - } - ipv6 = gnrc_ipv6_hdr_build(NULL, src, dst); - if (ipv6 == NULL) { - return NULL; - } - ipv6_hdr = ipv6->data; - ipv6_hdr->len = byteorder_htons((uint16_t)payload->size); - ipv6_hdr->nh = nh; - ipv6_hdr->hl = 64; - LL_APPEND(payload, ipv6); - netif_hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0); - if (netif_hdr == NULL) { - return NULL; - } - ((gnrc_netif_hdr_t *)netif_hdr->data)->if_pid = (kernel_pid_t)netif; - LL_APPEND(payload, netif_hdr); - return payload; -} - - -static bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint8_t proto, void *data, size_t data_len, - uint16_t netif) -{ - gnrc_pktsnip_t *pkt = _build_ipv6_packet(src, dst, proto, data, data_len, - netif); - - if (pkt == NULL) { - return false; - } - /* put directly in mbox, dispatching to IPv6 would result in the packet - * being dropped, since dst is not on any interface */ - return (gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, proto, pkt) > 0); -} - -static bool _check_stack(void) -{ - return (gnrc_pktbuf_is_sane() && gnrc_pktbuf_is_empty()); -} - -static inline bool _res(gnrc_pktsnip_t *pkt, bool res) -{ - gnrc_pktbuf_release(pkt); - return res; -} - -static bool _check_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, - uint8_t proto, void *data, size_t data_len, - uint16_t iface) -{ - gnrc_pktsnip_t *pkt, *ipv6; - ipv6_hdr_t *ipv6_hdr; - msg_t msg; - - msg_receive(&msg); - if (msg.type != GNRC_NETAPI_MSG_TYPE_SND) { - return false; - } - pkt = msg.content.ptr; - if (iface != SOCK_ADDR_ANY_NETIF) { - gnrc_netif_hdr_t *netif_hdr; - - if (pkt->type != GNRC_NETTYPE_NETIF) { - return _res(pkt, false); - } - netif_hdr = pkt->data; - if (netif_hdr->if_pid != iface) { - return _res(pkt, false); - } - ipv6 = pkt->next; - } - else { - ipv6 = pkt; - } - if (ipv6->type != GNRC_NETTYPE_IPV6) { - return _res(pkt, false); - } - ipv6_hdr = ipv6->data; - return _res(pkt, (memcmp(src, &ipv6_hdr->src, sizeof(ipv6_addr_t)) == 0) && - (memcmp(dst, &ipv6_hdr->dst, sizeof(ipv6_addr_t)) == 0) && - (ipv6_hdr->nh == proto) && - (ipv6->next != NULL) && - (data_len == ipv6->next->size) && - (memcmp(data, ipv6->next->data, data_len) == 0)); -} diff --git a/tests/gnrc_sock_ip/stack.c b/tests/gnrc_sock_ip/stack.c new file mode 100644 index 000000000000..448775e2ac10 --- /dev/null +++ b/tests/gnrc_sock_ip/stack.c @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2016 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 Martine Lenders + */ + +#include "stack.h" + +#include "net/gnrc/ipv6.h" +#include "net/gnrc/netif/hdr.h" +#include "net/gnrc/netreg.h" +#include "net/sock.h" +#include "sched.h" + +#define _MSG_QUEUE_SIZE (4) + +static msg_t _msg_queue[_MSG_QUEUE_SIZE]; +static gnrc_netreg_entry_t _ip_handler; + +void _net_init(void) +{ + msg_init_queue(_msg_queue, _MSG_QUEUE_SIZE); + gnrc_netreg_entry_init_pid(&_ip_handler, GNRC_NETREG_DEMUX_CTX_ALL, + sched_active_pid); +} + +void _prepare_send_checks(void) +{ + gnrc_netreg_register(GNRC_NETTYPE_IPV6, &_ip_handler); +} + +static gnrc_pktsnip_t *_build_ipv6_packet(const ipv6_addr_t *src, + const ipv6_addr_t *dst, uint8_t nh, + void *data, size_t data_len, + uint16_t netif) +{ + gnrc_pktsnip_t *netif_hdr, *ipv6, *payload; + ipv6_hdr_t *ipv6_hdr; + + if ((netif > INT16_MAX) || (data_len > UINT16_MAX)) { + return NULL; + } + + payload = gnrc_pktbuf_add(NULL, data, data_len, GNRC_NETTYPE_UNDEF); + if (payload == NULL) { + return NULL; + } + ipv6 = gnrc_ipv6_hdr_build(NULL, src, dst); + if (ipv6 == NULL) { + return NULL; + } + ipv6_hdr = ipv6->data; + ipv6_hdr->len = byteorder_htons((uint16_t)payload->size); + ipv6_hdr->nh = nh; + ipv6_hdr->hl = 64; + LL_APPEND(payload, ipv6); + netif_hdr = gnrc_netif_hdr_build(NULL, 0, NULL, 0); + if (netif_hdr == NULL) { + return NULL; + } + ((gnrc_netif_hdr_t *)netif_hdr->data)->if_pid = (kernel_pid_t)netif; + LL_APPEND(payload, netif_hdr); + return payload; +} + + +bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint8_t proto, void *data, size_t data_len, + uint16_t netif) +{ + gnrc_pktsnip_t *pkt = _build_ipv6_packet(src, dst, proto, data, data_len, + netif); + + if (pkt == NULL) { + return false; + } + /* put directly in mbox, dispatching to IPv6 would result in the packet + * being dropped, since dst is not on any interface */ + return (gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, proto, pkt) > 0); +} + +bool _check_net(void) +{ + return (gnrc_pktbuf_is_sane() && gnrc_pktbuf_is_empty()); +} + +static inline bool _res(gnrc_pktsnip_t *pkt, bool res) +{ + gnrc_pktbuf_release(pkt); + return res; +} + +bool _check_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint8_t proto, void *data, size_t data_len, + uint16_t netif) +{ + gnrc_pktsnip_t *pkt, *ipv6; + ipv6_hdr_t *ipv6_hdr; + msg_t msg; + + msg_receive(&msg); + if (msg.type != GNRC_NETAPI_MSG_TYPE_SND) { + return false; + } + pkt = msg.content.ptr; + if (netif != SOCK_ADDR_ANY_NETIF) { + gnrc_netif_hdr_t *netif_hdr; + + if (pkt->type != GNRC_NETTYPE_NETIF) { + return _res(pkt, false); + } + netif_hdr = pkt->data; + if (netif_hdr->if_pid != netif) { + return _res(pkt, false); + } + ipv6 = pkt->next; + } + else { + ipv6 = pkt; + } + if (ipv6->type != GNRC_NETTYPE_IPV6) { + return _res(pkt, false); + } + ipv6_hdr = ipv6->data; + return _res(pkt, (memcmp(src, &ipv6_hdr->src, sizeof(ipv6_addr_t)) == 0) && + (memcmp(dst, &ipv6_hdr->dst, sizeof(ipv6_addr_t)) == 0) && + (ipv6_hdr->nh == proto) && + (ipv6->next != NULL) && + (data_len == ipv6->next->size) && + (memcmp(data, ipv6->next->data, data_len) == 0)); +} + +/** @} */ diff --git a/tests/gnrc_sock_ip/stack.h b/tests/gnrc_sock_ip/stack.h new file mode 100644 index 000000000000..637ef466f5ec --- /dev/null +++ b/tests/gnrc_sock_ip/stack.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2016 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. + */ + +/** + * @defgroup + * @ingroup + * @brief + * @{ + * + * @file + * @brief + * + * @author Martine Lenders + */ +#ifndef STACK_H_ +#define STACK_H_ + +#include +#include + +#include "net/ipv6/addr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Initializes networking for tests + */ +void _net_init(void); + +/** + * @brief Does what ever preparations are needed to check the packets sent + */ +void _prepare_send_checks(void); + +/** + * @brief Injects a received IPv6 packet into the stack + * + * @param[in] src The source address of the IPv6 packet + * @param[in] dst The destination address of the IPv6 packet + * @param[in] proto The next header field of the IPv6 packet + * @param[in] data The payload of the IPv6 packet + * @param[in] data_len The payload length of the IPv6 packet + * @param[in] netif The interface the packet came over + * + * @return true, if packet was successfully injected + * @return false, if an error occured during injection + */ +bool _inject_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint8_t proto, void *data, size_t data_len, + uint16_t netif); + +/** + * @brief Checks networking state (e.g. packet buffer state) + * + * @return true, if networking component is still in valid state + * @return false, if networking component is in an invalid state + */ +bool _check_net(void); + +/** + * @brief Checks if a IPv6 packet was sent by the networking component + * + * @param[in] src Expected source address of the IPv6 packet + * @param[in] dst Expected destination address of the IPv6 packet + * @param[in] proto Expected next header field of the IPv6 packet + * @param[in] data Expected payload of the IPv6 packet + * @param[in] data_len Expected payload length of the IPv6 packet + * @param[in] netif Expected interface the packet is supposed to + * be send over + * + * @return true, if all parameters match as expected + * @return false, if not. + */ +bool _check_packet(const ipv6_addr_t *src, const ipv6_addr_t *dst, + uint8_t proto, void *data, size_t data_len, uint16_t netif); + + +#ifdef __cplusplus +} +#endif + +#endif /* STACK_H_ */ +/** @} */ diff --git a/tests/gnrc_sock_ip/tests/01-run.py b/tests/gnrc_sock_ip/tests/01-run.py index 092d909ba981..493f68ee7b0d 100755 --- a/tests/gnrc_sock_ip/tests/01-run.py +++ b/tests/gnrc_sock_ip/tests/01-run.py @@ -50,8 +50,7 @@ def testfunc(child): child.expect_exact(u"Calling test_sock_ip_recv__with_timeout()") child.expect_exact(u"Calling test_sock_ip_send__AFNOSUPPORT()") child.expect_exact(u"Calling test_sock_ip_send__EINVAL()") - child.expect_exact(u"Calling test_sock_ip_send__ENOTCONN_no_sock()") - child.expect_exact(u"Calling test_sock_ip_send__ENOTCONN_sock_not_socketed()") + child.expect_exact(u"Calling test_sock_ip_send__ENOTCONN()") child.expect_exact(u"Calling test_sock_ip_send__socketed_no_local_no_netif()") child.expect_exact(u"Calling test_sock_ip_send__socketed_no_netif()") child.expect_exact(u"Calling test_sock_ip_send__socketed_no_local()")