From 9db7f8d0a0948d1f44e5d224ebfc002c82010c97 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Wed, 27 Nov 2024 09:21:02 +0100 Subject: [PATCH] tests: net: ip-addr: Add test case for an IPv4 LL address selection Add a test case verifying that a proper source IPv4 address and interface is selected for a given LL destination address. Signed-off-by: Robert Lubos --- tests/net/ip-addr/prj.conf | 2 ++ tests/net/ip-addr/src/main.c | 70 ++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/tests/net/ip-addr/prj.conf b/tests/net/ip-addr/prj.conf index 3f718037e91..7c105491f5e 100644 --- a/tests/net/ip-addr/prj.conf +++ b/tests/net/ip-addr/prj.conf @@ -13,6 +13,8 @@ CONFIG_NET_BUF_TX_COUNT=5 CONFIG_NET_LOG=y CONFIG_ENTROPY_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_NET_IF_MAX_IPV4_COUNT=2 +CONFIG_NET_IF_MAX_IPV6_COUNT=2 CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=2 CONFIG_NET_IF_MCAST_IPV4_ADDR_COUNT=3 diff --git a/tests/net/ip-addr/src/main.c b/tests/net/ip-addr/src/main.c index 9d98c540515..83e849c2f86 100644 --- a/tests/net/ip-addr/src/main.c +++ b/tests/net/ip-addr/src/main.c @@ -35,6 +35,7 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV6_LOG_LEVEL); #endif static struct net_if *default_iface; +static struct net_if *second_iface; #define TEST_BYTE_1(value, expected) \ do { \ @@ -370,6 +371,57 @@ ZTEST(ip_addr_fn, test_ipv6_addresses) "IPv6 removing address failed\n"); } +ZTEST(ip_addr_fn, test_ipv4_ll_address_select) +{ + struct net_if *iface; + const struct in_addr *out; + struct net_if_addr *ifaddr; + struct in_addr lladdr4_1 = { { { 169, 254, 250, 1 } } }; + struct in_addr lladdr4_2 = { { { 169, 254, 253, 1 } } }; + struct in_addr netmask_1 = { { { 255, 255, 255, 0 } } }; + struct in_addr netmask_2 = { { { 255, 255, 255, 252 } } }; + struct in_addr dst4_1 = { { { 169, 254, 250, 2 } } }; + struct in_addr dst4_2 = { { { 169, 254, 253, 2 } } }; + + ifaddr = net_if_ipv4_addr_add(default_iface, &lladdr4_1, NET_ADDR_MANUAL, 0); + zassert_not_null(ifaddr, "IPv4 interface address add failed"); + zassert_true(net_ipv4_is_my_addr(&lladdr4_1), + "My IPv4 address check failed"); + + net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4_1, &netmask_1); + + ifaddr = net_if_ipv4_addr_add(second_iface, &lladdr4_2, NET_ADDR_MANUAL, 0); + zassert_not_null(ifaddr, "IPv4 interface address add failed"); + zassert_true(net_ipv4_is_my_addr(&lladdr4_2), + "My IPv4 address check failed"); + + net_if_ipv4_set_netmask_by_addr(second_iface, &lladdr4_2, &netmask_2); + + out = net_if_ipv4_select_src_addr(NULL, &dst4_1); + iface = net_if_ipv4_select_src_iface(&dst4_1); + zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n", + iface); + + DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out), + iface); + + zassert_equal(out->s_addr, lladdr4_1.s_addr, + "IPv4 wrong src address selected, iface %p\n", iface); + zassert_equal_ptr(iface, default_iface, "Wrong iface selected"); + + out = net_if_ipv4_select_src_addr(NULL, &dst4_2); + iface = net_if_ipv4_select_src_iface(&dst4_2); + zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n", + iface); + + DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out), + iface); + + zassert_equal(out->s_addr, lladdr4_2.s_addr, + "IPv4 wrong src address selected, iface %p\n", iface); + zassert_equal_ptr(iface, second_iface, "Wrong iface selected"); +} + ZTEST(ip_addr_fn, test_ipv4_addresses) { const struct in_addr *out; @@ -703,11 +755,25 @@ ZTEST(ip_addr_fn, test_private_ipv4_addresses) } +void clear_addr4(struct net_if *iface, struct net_if_addr *addr, void *user_data) +{ + addr->is_used = false; +} + +static void test_before(void *f) +{ + ARG_UNUSED(f); + + net_if_ipv4_addr_foreach(default_iface, clear_addr4, NULL); + net_if_ipv4_addr_foreach(second_iface, clear_addr4, NULL); +} + void *test_setup(void) { - default_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)); + default_iface = net_if_get_by_index(1); + second_iface = net_if_get_by_index(2); return NULL; } -ZTEST_SUITE(ip_addr_fn, NULL, test_setup, NULL, NULL, NULL); +ZTEST_SUITE(ip_addr_fn, NULL, test_setup, test_before, NULL, NULL);