Skip to content

Commit

Permalink
tests: net: iface: Test foreach multicast helper functions
Browse files Browse the repository at this point in the history
Add test cases for IPv4/IPv6 foreach functions that iterate all
multicast addresses assigned to an interface.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
  • Loading branch information
pdgendt authored and dleach02 committed Feb 28, 2024
1 parent d9d710e commit 408babb
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/net/iface/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
0x04 } } };

static struct in_addr inaddr_mcast = { { { 224, 0, 0, 1 } } };
static struct in6_addr in6addr_mcast;

static struct net_if *iface1;
Expand Down Expand Up @@ -439,6 +440,13 @@ static void *iface_setup(void)

ifaddr->addr_state = NET_ADDR_PREFERRED;

maddr = net_if_ipv4_maddr_add(iface1, &inaddr_mcast);
if (!maddr) {
DBG("Cannot add multicast IPv4 address %s\n",
net_sprint_ipv4_addr(&inaddr_mcast));
zassert_not_null(maddr, "mcast");
}

net_ipv6_addr_create(&in6addr_mcast, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);

maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast);
Expand Down Expand Up @@ -1174,6 +1182,34 @@ ZTEST(net_iface, test_ipv4_addr_foreach)
zassert_equal(count, 0, "Incorrect number of callback calls");
}

static void foreach_ipv4_maddr_check(struct net_if *iface,
struct net_if_mcast_addr *if_addr,
void *user_data)
{
int *count = (int *)user_data;

(*count)++;

zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
zassert_mem_equal(&if_addr->address.in_addr, &inaddr_mcast,
sizeof(struct in_addr), "Wrong IPv4 multicast address");
}

ZTEST(net_iface, test_ipv4_maddr_foreach)
{
int count = 0;

/* iface1 has one IPv4 multicast address configured */
net_if_ipv4_maddr_foreach(iface1, foreach_ipv4_maddr_check, &count);
zassert_equal(count, 1, "Incorrect number of callback calls");

count = 0;

/* iface4 has no IPv4 multicast address configured */
net_if_ipv4_maddr_foreach(iface4, foreach_ipv4_maddr_check, &count);
zassert_equal(count, 0, "Incorrect number of callback calls");
}

static void foreach_ipv6_addr_check(struct net_if *iface,
struct net_if_addr *if_addr,
void *user_data)
Expand Down Expand Up @@ -1208,6 +1244,34 @@ ZTEST(net_iface, test_ipv6_addr_foreach)
zassert_equal(count, 0, "Incorrect number of callback calls");
}

static void foreach_ipv6_maddr_check(struct net_if *iface,
struct net_if_mcast_addr *if_addr,
void *user_data)
{
int *count = (int *)user_data;

(*count)++;

zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
zassert_mem_equal(&if_addr->address.in6_addr, &in6addr_mcast, sizeof(struct in6_addr),
"Wrong IPv6 multicast address");
}

ZTEST(net_iface, test_ipv6_maddr_foreach)
{
int count = 0;

/* iface1 has one IPv6 multicast address configured */
net_if_ipv6_maddr_foreach(iface1, foreach_ipv6_maddr_check, &count);
zassert_equal(count, 1, "Incorrect number of callback calls");

count = 0;

/* iface4 has no IPv6 multicast address configured */
net_if_ipv6_maddr_foreach(iface4, foreach_ipv6_maddr_check, &count);
zassert_equal(count, 0, "Incorrect number of callback calls");
}

ZTEST(net_iface, test_interface_name)
{
int ret;
Expand Down

0 comments on commit 408babb

Please sign in to comment.