net: Add unit tests for ICMPv6 handler
Change-Id: Ibdbe82ed56f110e2e0c6443700863632ee1ef56b Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
7c78925050
commit
5580cdcc54
5 changed files with 148 additions and 0 deletions
6
tests/net/icmpv6/Makefile
Normal file
6
tests/net/icmpv6/Makefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
BOARD ?= qemu_x86
|
||||
MDEF_FILE = prj.mdef
|
||||
KERNEL_TYPE ?= nano
|
||||
CONF_FILE = prj_$(ARCH).conf
|
||||
|
||||
include $(ZEPHYR_BASE)/Makefile.inc
|
5
tests/net/icmpv6/prj.mdef
Normal file
5
tests/net/icmpv6/prj.mdef
Normal file
|
@ -0,0 +1,5 @@
|
|||
% Application : ICMPv6 test
|
||||
|
||||
% TASK NAME PRIO ENTRY STACK GROUPS
|
||||
% ===================================================
|
||||
TASK MAIN 7 main 2048 [EXE]
|
11
tests/net/icmpv6/prj_x86.conf
Normal file
11
tests/net/icmpv6/prj_x86.conf
Normal file
|
@ -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
|
3
tests/net/icmpv6/src/Makefile
Normal file
3
tests/net/icmpv6/src/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
obj-y = main.o
|
||||
|
||||
ccflags-y += -I${srctree}/net/yaip
|
123
tests/net/icmpv6/src/main.c
Normal file
123
tests/net/icmpv6/src/main.c
Normal file
|
@ -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 <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <misc/printk.h>
|
||||
|
||||
#include <net/buf.h>
|
||||
|
||||
#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");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue