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 <joakim.andersson@nordicsemi.no>
This commit is contained in:
Joakim Andersson 2019-12-26 19:21:16 +01:00 committed by Alberto Escolar
commit 9483e432d8
2 changed files with 11 additions and 3 deletions

View file

@ -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

View file

@ -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(&eth->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);