From 9483e432d8e2ce424d4e29cf9d4753798cf95868 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Thu, 26 Dec 2019 19:21:16 +0100 Subject: [PATCH] tests: net: arp: Fix net_buf assert in ARP unit test Fix ASSERTs that appears when enabling asserts in net buf. Asserts are: - Pulling from net buf before any data has been added. - Pulling more data than has been allocated for the buf. Fix warning: W: You have 1 IPv4 net_if addresses but 2 network interfaces W: Consider increasing CONFIG_NET_IF_MAX_IPV4_COUNT value. Signed-off-by: Joakim Andersson --- tests/net/arp/prj.conf | 1 + tests/net/arp/src/main.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/net/arp/prj.conf b/tests/net/arp/prj.conf index a45a27fe3a6..d3884b6bb2f 100644 --- a/tests/net/arp/prj.conf +++ b/tests/net/arp/prj.conf @@ -15,3 +15,4 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=3 CONFIG_NET_IPV6=n CONFIG_ZTEST=y +CONFIG_NET_IF_MAX_IPV4_COUNT=2 diff --git a/tests/net/arp/src/main.c b/tests/net/arp/src/main.c index d59229c0d89..7e24d7de4cf 100644 --- a/tests/net/arp/src/main.c +++ b/tests/net/arp/src/main.c @@ -176,12 +176,14 @@ static inline struct net_pkt *prepare_arp_reply(struct net_if *iface, struct net_eth_hdr *eth; pkt = net_pkt_alloc_with_buffer(iface, sizeof(struct net_eth_hdr) + + sizeof(struct net_eth_hdr) + sizeof(struct net_arp_hdr), AF_UNSPEC, 0, K_SECONDS(1)); zassert_not_null(pkt, "out of mem reply"); eth = NET_ETH_HDR(pkt); + net_buf_add(pkt->buffer, sizeof(struct net_eth_hdr)); net_buf_pull(pkt->buffer, sizeof(struct net_eth_hdr)); (void)memset(ð->dst.addr, 0xff, sizeof(struct net_eth_addr)); @@ -191,6 +193,7 @@ static inline struct net_pkt *prepare_arp_reply(struct net_if *iface, *eth_rep = eth; + net_buf_add(pkt->buffer, sizeof(struct net_eth_hdr)); net_buf_pull(pkt->buffer, sizeof(struct net_eth_hdr)); hdr = NET_ARP_HDR(pkt); @@ -231,6 +234,7 @@ static inline struct net_pkt *prepare_arp_request(struct net_if *iface, eth_req = NET_ETH_HDR(req); eth = NET_ETH_HDR(pkt); + net_buf_add(req->buffer, sizeof(struct net_eth_hdr)); net_buf_pull(req->buffer, sizeof(struct net_eth_hdr)); req_hdr = NET_ARP_HDR(req); @@ -241,6 +245,7 @@ static inline struct net_pkt *prepare_arp_request(struct net_if *iface, eth->type = htons(NET_ETH_PTYPE_ARP); *eth_hdr = eth; + net_buf_add(pkt->buffer, sizeof(struct net_eth_hdr)); net_buf_pull(pkt->buffer, sizeof(struct net_eth_hdr)); hdr = NET_ARP_HDR(pkt); @@ -345,15 +350,16 @@ void test_arp(void) zassert_not_null(ifaddr, "Cannot add address"); ifaddr->addr_state = NET_ADDR_PREFERRED; + len = strlen(app_data); + /* Application data for testing */ - pkt = net_pkt_alloc_with_buffer(iface, 0, AF_INET, 0, K_SECONDS(1)); + pkt = net_pkt_alloc_with_buffer(iface, sizeof(struct net_ipv4_hdr) + + len, AF_INET, 0, K_SECONDS(1)); zassert_not_null(pkt, "out of mem"); net_pkt_lladdr_src(pkt)->addr = (u8_t *)net_if_get_link_addr(iface); net_pkt_lladdr_src(pkt)->len = sizeof(struct net_eth_addr); - len = strlen(app_data); - ipv4 = (struct net_ipv4_hdr *)net_buf_add(pkt->buffer, sizeof(struct net_ipv4_hdr)); net_ipaddr_copy(&ipv4->src, &src); @@ -603,6 +609,7 @@ void test_arp(void) NET_ETH_PTYPE_ARP); eth_hdr = (struct net_eth_hdr *)net_pkt_data(pkt); + net_buf_add(pkt->buffer, sizeof(struct net_eth_hdr)); net_buf_pull(pkt->buffer, sizeof(struct net_eth_hdr)); arp_hdr = NET_ARP_HDR(pkt);