diff --git a/tests/net/icmpv6/Makefile b/tests/net/icmpv6/Makefile new file mode 100644 index 00000000000..fa7f724b6bb --- /dev/null +++ b/tests/net/icmpv6/Makefile @@ -0,0 +1,6 @@ +BOARD ?= qemu_x86 +MDEF_FILE = prj.mdef +KERNEL_TYPE ?= nano +CONF_FILE = prj_$(ARCH).conf + +include $(ZEPHYR_BASE)/Makefile.inc diff --git a/tests/net/icmpv6/prj.mdef b/tests/net/icmpv6/prj.mdef new file mode 100644 index 00000000000..967f3e15bf6 --- /dev/null +++ b/tests/net/icmpv6/prj.mdef @@ -0,0 +1,5 @@ +% Application : ICMPv6 test + +% TASK NAME PRIO ENTRY STACK GROUPS +% =================================================== + TASK MAIN 7 main 2048 [EXE] diff --git a/tests/net/icmpv6/prj_x86.conf b/tests/net/icmpv6/prj_x86.conf new file mode 100644 index 00000000000..68e2af2052c --- /dev/null +++ b/tests/net/icmpv6/prj_x86.conf @@ -0,0 +1,11 @@ +CONFIG_NETWORKING=y +CONFIG_NET_IPV6=y +CONFIG_NET_IPV4=y +CONFIG_NET_YAIP=y +CONFIG_NET_BUF=y +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_NET_NBUF_RX_COUNT=2 +CONFIG_NET_NBUF_TX_COUNT=2 +CONFIG_NET_NBUF_DATA_COUNT=5 +CONFIG_NET_LOG=y +CONFIG_SYS_LOG_SHOW_COLOR=y diff --git a/tests/net/icmpv6/src/Makefile b/tests/net/icmpv6/src/Makefile new file mode 100644 index 00000000000..5a8e37cd6fc --- /dev/null +++ b/tests/net/icmpv6/src/Makefile @@ -0,0 +1,3 @@ +obj-y = main.o + +ccflags-y += -I${srctree}/net/yaip diff --git a/tests/net/icmpv6/src/main.c b/tests/net/icmpv6/src/main.c new file mode 100644 index 00000000000..4640d85c0ee --- /dev/null +++ b/tests/net/icmpv6/src/main.c @@ -0,0 +1,123 @@ +/* main.c - Application main entry point */ + +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include + +#include "icmpv6.h" + +static int handler_called; + +struct header { + int status; +}; + +#define buf_status(buf) (((struct header *)net_buf_user_data((buf)))->status) + +#define TEST_MSG "foobar devnull" + +static struct nano_fifo bufs_fifo; +static struct nano_fifo data_fifo; + +static NET_BUF_POOL(bufs_pool, 2, 0, &bufs_fifo, NULL, sizeof(struct header)); +static NET_BUF_POOL(data_pool, 2, 128, &data_fifo, NULL, 0); + +static enum net_verdict handle_test_msg(struct net_buf *buf) +{ + struct net_buf *last = net_buf_frag_last(buf); + + if (last->len != (strlen(TEST_MSG) + 1)) { + buf_status(buf) = -EINVAL; + } else { + buf_status(buf) = 0; + } + + handler_called++; + + return 0; +} + +static struct net_icmpv6_handler test_handler1 = { + .type = NET_ICMPV6_ECHO_REPLY, + .code = 0, + .handler = handle_test_msg, +}; + +static struct net_icmpv6_handler test_handler2 = { + .type = NET_ICMPV6_ECHO_REQUEST, + .code = 0, + .handler = handle_test_msg, +}; + +void main(void) +{ + struct net_buf *buf, *frag; + int ret; + + net_buf_pool_init(bufs_pool); + net_buf_pool_init(data_pool); + + net_icmpv6_register_handler(&test_handler1); + net_icmpv6_register_handler(&test_handler2); + + buf = net_buf_get(&bufs_fifo, 0); + frag = net_buf_get(&data_fifo, 0); + + net_buf_frag_add(buf, frag); + + memcpy(net_buf_add(frag, sizeof(TEST_MSG)), + TEST_MSG, sizeof(TEST_MSG)); + + ret = net_icmpv6_input(buf, net_buf_frags_len(buf->frags), 0, 0); + if (!ret) { + printk("%d: Callback not called properly\n", __LINE__); + return; + } + + ret = net_icmpv6_input(buf, net_buf_frags_len(buf->frags), + NET_ICMPV6_ECHO_REPLY, 0); + if (ret < 0 || buf_status(buf) != 0) { + printk("%d: Callback not called properly\n", __LINE__); + return; + } + + ret = net_icmpv6_input(buf, net_buf_frags_len(buf->frags), 1, 0); + if (!ret) { + printk("%d: Callback not called properly\n", __LINE__); + return; + } + + ret = net_icmpv6_input(buf, net_buf_frags_len(buf->frags), + NET_ICMPV6_ECHO_REQUEST, 0); + if (ret < 0 || buf_status(buf) != 0) { + printk("%d: Callback not called properly\n", __LINE__); + return; + } + + if (handler_called != 2) { + printk("%d: Callbacks not called properly\n", __LINE__); + return; + } + + printk("ICMPv6 tests passed\n"); +}