From 8f74148bdd2f4e0d0eec6176bc678ef33da9eff3 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Mon, 25 Sep 2023 18:18:11 +0300 Subject: [PATCH] tests: net: icmpv6: Convert to use new ICMP API Converting the ICMPv6 tests to use the new ICMP API so that the tests pass. Signed-off-by: Jukka Rissanen --- tests/net/icmpv6/prj.conf | 7 +- tests/net/icmpv6/src/main.c | 195 +++++++++++++++++++++++++----------- 2 files changed, 144 insertions(+), 58 deletions(-) diff --git a/tests/net/icmpv6/prj.conf b/tests/net/icmpv6/prj.conf index a803a29637d..dbd3d93f626 100644 --- a/tests/net/icmpv6/prj.conf +++ b/tests/net/icmpv6/prj.conf @@ -1,7 +1,7 @@ CONFIG_NETWORKING=y CONFIG_NET_TEST=y CONFIG_NET_IPV6=y -CONFIG_NET_IPV4=y +CONFIG_NET_IPV4=n CONFIG_NET_BUF=y CONFIG_ZTEST_STACK_SIZE=1024 CONFIG_NET_PKT_RX_COUNT=2 @@ -15,3 +15,8 @@ CONFIG_ZTEST=y CONFIG_ZTEST_NEW_API=y CONFIG_MAIN_STACK_SIZE=1280 CONFIG_NET_L2_ETHERNET=n +CONFIG_NET_L2_DUMMY=y +CONFIG_NET_IPV6_NBR_CACHE=n +CONFIG_NET_IPV6_ND=n +CONFIG_NET_IPV6_DAD=n +CONFIG_NET_IPV6_MLD=n diff --git a/tests/net/icmpv6/src/main.c b/tests/net/icmpv6/src/main.c index 5546e0ff724..8594f628fc2 100644 --- a/tests/net/icmpv6/src/main.c +++ b/tests/net/icmpv6/src/main.c @@ -2,6 +2,7 @@ /* * Copyright (c) 2016 Intel Corporation + * Copyright (c) 2023 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,10 +20,16 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_ICMPV6_LOG_LEVEL); #include #include +#include +#include +#include +#include "ipv6.h" +#include "net_private.h" #include "icmpv6.h" #include +static struct net_if *test_iface; static int handler_called; static int handler_status; @@ -30,7 +37,7 @@ static int handler_status; #define ICMPV6_MSG_SIZE 104 -static char icmpv6_echo_req[] = +static uint8_t icmpv6_echo_req[] = "\x60\x02\xea\x12\x00\x40\x3a\x40\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xda\xcb\x8a\xff\xfe\x34\xc8\xf3\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xec\x88\x2d\x63\xfd\x67\x31\x66\x80\x00\xa4\x24\x0b\x95\x00\x01" \ @@ -39,7 +46,7 @@ static char icmpv6_echo_req[] = "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \ "\x30\x31\x32\x33\x34\x35\x36\x37"; -static char icmpv6_echo_rep[] = +static uint8_t icmpv6_echo_rep[] = "\x60\x09\x23\xa0\x00\x40\x3a\x40\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xec\x88\x2d\x63\xfd\x67\x31\x66\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xda\xcb\x8a\xff\xfe\x34\xc8\xf3\x81\x00\xa3\x24\x0b\x95\x00\x01" \ @@ -48,7 +55,7 @@ static char icmpv6_echo_rep[] = "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \ "\x30\x31\x32\x33\x34\x35\x36\x37"; -static char icmpv6_inval_chksum[] = +static uint8_t icmpv6_inval_chksum[] = "\x60\x09\x23\xa0\x00\x40\x3a\x40\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xec\x88\x2d\x63\xfd\x67\x31\x66\xfe\x80\x00\x00\x00\x00\x00\x00" \ "\xda\xcb\x8a\xff\xfe\x34\xc8\xf3\x00\x00\xa3\x24\x0b\x95\x00\x01" \ @@ -57,19 +64,84 @@ static char icmpv6_inval_chksum[] = "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \ "\x30\x31\x32\x33\x34\x35\x36\x37"; -static enum net_verdict handle_test_msg(struct net_pkt *pkt, - struct net_ipv6_hdr *ip_hdr, - struct net_icmp_hdr *icmp_hdr) +struct net_icmpv6_context { + uint8_t mac_addr[sizeof(struct net_eth_addr)]; + struct net_linkaddr ll_addr; +}; + +static struct net_icmpv6_context net_icmpv6_context_data; + +static int net_icmpv6_dev_init(const struct device *dev) { + struct net_icmpv6_context *net_icmpv6_context = dev->data; + + net_icmpv6_context = net_icmpv6_context; + + return 0; +} + +static uint8_t *net_icmpv6_get_mac(const struct device *dev) +{ + struct net_icmpv6_context *context = dev->data; + + if (context->mac_addr[2] == 0x00) { + /* 00-00-5E-00-53-xx Documentation RFC 7042 */ + context->mac_addr[0] = 0x00; + context->mac_addr[1] = 0x00; + context->mac_addr[2] = 0x5E; + context->mac_addr[3] = 0x00; + context->mac_addr[4] = 0x53; + context->mac_addr[5] = 0x01; + } + + return context->mac_addr; +} + +static void net_icmpv6_iface_init(struct net_if *iface) +{ + uint8_t *mac = net_icmpv6_get_mac(net_if_get_device(iface)); + + net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET); +} + +static int tester_send(const struct device *dev, struct net_pkt *pkt) +{ + net_pkt_unref(pkt); + return 0; +} + +static struct dummy_api net_icmpv6_if_api = { + .iface_api.init = net_icmpv6_iface_init, + .send = tester_send, +}; + +NET_DEVICE_INIT(net_icmpv6_test, "net_icmpv6_test", + net_icmpv6_dev_init, NULL, + &net_icmpv6_context_data, NULL, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &net_icmpv6_if_api, DUMMY_L2, + NET_L2_GET_CTX_TYPE(DUMMY_L2), 127); + +static int handle_test_msg(struct net_icmp_ctx *ctx, + struct net_pkt *pkt, + struct net_icmp_ip_hdr *hdr, + struct net_icmp_hdr *icmp_hdr, + void *user_data) +{ + ARG_UNUSED(ctx); + ARG_UNUSED(hdr); + ARG_UNUSED(icmp_hdr); + ARG_UNUSED(user_data); + struct net_buf *last = net_buf_frag_last(pkt->buffer); - enum net_verdict ret; + int ret; if (last->len != ICMPV6_MSG_SIZE) { handler_status = -EINVAL; - ret = NET_DROP; + ret = -EINVAL; } else { handler_status = 0; - ret = NET_OK; + ret = 0; } handler_called++; @@ -77,47 +149,52 @@ static enum net_verdict handle_test_msg(struct net_pkt *pkt, return ret; } -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, -}; - -ZTEST(icmpv6_fn, test_icmpv6) +static struct net_pkt *create_pkt(uint8_t *data, int len, + struct net_ipv6_hdr **hdr) { - if (IS_ENABLED(CONFIG_NET_TC_THREAD_COOPERATIVE)) { - k_thread_priority_set(k_current_get(), - K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1)); - } else { - k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(9)); - } - - struct net_ipv6_hdr *hdr; struct net_pkt *pkt; - int ret; - - net_icmpv6_register_handler(&test_handler1); - net_icmpv6_register_handler(&test_handler2); pkt = net_pkt_alloc_with_buffer(NULL, ICMPV6_MSG_SIZE, AF_UNSPEC, 0, K_SECONDS(1)); zassert_not_null(pkt, "Allocation failed"); + net_pkt_set_iface(pkt, test_iface); + net_pkt_set_family(pkt, AF_INET6); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_write(pkt, icmpv6_inval_chksum, ICMPV6_MSG_SIZE); + net_pkt_write(pkt, data, len); - hdr = (struct net_ipv6_hdr *)pkt->buffer->data; net_pkt_cursor_init(pkt); + *hdr = net_pkt_cursor_get_pos(pkt); net_pkt_set_overwrite(pkt, true); net_pkt_skip(pkt, sizeof(struct net_ipv6_hdr)); + /* The cursor should be at the start of the ICMPv6 header */ + + return pkt; +} + +ZTEST(icmpv6_fn, test_icmpv6) +{ + struct net_icmp_ctx ctx1; + struct net_icmp_ctx ctx2; + struct net_ipv6_hdr *hdr; + struct net_pkt *pkt; + int ret; + + ret = net_icmp_init_ctx(&ctx1, NET_ICMPV6_ECHO_REPLY, + 0, handle_test_msg); + zassert_equal(ret, 0, "Cannot register %s handler (%d)", + STRINGIFY(NET_ICMPV6_ECHO_REPLY), ret); + + ret = net_icmp_init_ctx(&ctx2, NET_ICMPV6_ECHO_REQUEST, + 0, handle_test_msg); + zassert_equal(ret, 0, "Cannot register %s handler (%d)", + STRINGIFY(NET_ICMPV6_ECHO_REQUEST), ret); + + pkt = create_pkt(icmpv6_inval_chksum, ICMPV6_MSG_SIZE, &hdr); + zassert_not_null(pkt, "Cannot create pkt"); + ret = net_icmpv6_input(pkt, hdr); /**TESTPOINT: Check input*/ @@ -125,16 +202,8 @@ ZTEST(icmpv6_fn, test_icmpv6) handler_status = -1; - net_pkt_cursor_init(pkt); - net_pkt_set_overwrite(pkt, false); - pkt->buffer->len = 0; - - net_pkt_write(pkt, icmpv6_echo_rep, ICMPV6_MSG_SIZE); - - hdr = (struct net_ipv6_hdr *)pkt->buffer->data; - net_pkt_cursor_init(pkt); - net_pkt_set_overwrite(pkt, true); - net_pkt_skip(pkt, sizeof(struct net_ipv6_hdr)); + pkt = create_pkt(icmpv6_echo_rep, ICMPV6_MSG_SIZE, &hdr); + zassert_not_null(pkt, "Cannot create pkt"); ret = net_icmpv6_input(pkt, hdr); @@ -144,16 +213,8 @@ ZTEST(icmpv6_fn, test_icmpv6) handler_status = -1; - net_pkt_cursor_init(pkt); - net_pkt_set_overwrite(pkt, false); - pkt->buffer->len = 0; - - net_pkt_write(pkt, icmpv6_echo_req, ICMPV6_MSG_SIZE); - - hdr = (struct net_ipv6_hdr *)pkt->buffer->data; - net_pkt_cursor_init(pkt); - net_pkt_set_overwrite(pkt, true); - net_pkt_skip(pkt, sizeof(struct net_ipv6_hdr)); + pkt = create_pkt(icmpv6_echo_req, ICMPV6_MSG_SIZE, &hdr); + zassert_not_null(pkt, "Cannot create pkt"); ret = net_icmpv6_input(pkt, hdr); @@ -163,7 +224,27 @@ ZTEST(icmpv6_fn, test_icmpv6) /**TESTPOINT: Check input*/ zassert_true(!(handler_called != 2), "Callbacks not called properly"); + + ret = net_icmp_cleanup_ctx(&ctx1); + zassert_equal(ret, 0, "Cannot unregister handler (%d)", ret); + + ret = net_icmp_cleanup_ctx(&ctx2); + zassert_equal(ret, 0, "Cannot unregister handler (%d)", ret); +} + +static void *setup(void) +{ + if (IS_ENABLED(CONFIG_NET_TC_THREAD_COOPERATIVE)) { + k_thread_priority_set(k_current_get(), + K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1)); + } else { + k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(9)); + } + + test_iface = net_if_lookup_by_dev(DEVICE_GET(net_icmpv6_test)); + + return NULL; } /**test case main entry*/ -ZTEST_SUITE(icmpv6_fn, NULL, NULL, NULL, NULL, NULL); +ZTEST_SUITE(icmpv6_fn, NULL, setup, NULL, NULL, NULL);