From d627ec7075eb97ebf8994b621558fb4f0260a4c2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 21 Sep 2020 17:16:09 +0200 Subject: [PATCH] platform/tests: add nmtstp_platform_ip_addresses_assert() test helper --- src/platform/tests/test-common.c | 127 +++++++++++++++++++++++++++++++ src/platform/tests/test-common.h | 26 +++++++ 2 files changed, 153 insertions(+) diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c index 21da665ce0..1caffae18d 100644 --- a/src/platform/tests/test-common.c +++ b/src/platform/tests/test-common.c @@ -126,6 +126,133 @@ nmtstp_platform_ip_address_find (NMPlatform *self, /*****************************************************************************/ +typedef struct { + NMIPAddr addr; + int addr_family; + bool found:1; +} IPAddressesAssertData; + +void +_nmtstp_platform_ip_addresses_assert (const char *filename, + int lineno, + NMPlatform *self, + int ifindex, + gboolean force_exact_4, + gboolean force_exact_6, + gboolean ignore_ll6, + guint addrs_len, + const char *const*addrs) +{ + gs_free IPAddressesAssertData *addrs_bin = NULL; + int IS_IPv4; + guint i; + + g_assert (filename); + g_assert (lineno >= 0); + g_assert (NM_IS_PLATFORM (self)); + g_assert (ifindex >= 0); + + addrs_bin = g_new (IPAddressesAssertData, addrs_len); + + for (i = 0; i < addrs_len; i++) { + const char *addrstr = addrs[i]; + int addr_family; + NMIPAddr a; + + if (!addrstr) { + addr_family = AF_UNSPEC; + a = nm_ip_addr_zero; + } else if (inet_pton (AF_INET, addrstr, &a) == 1) + addr_family = AF_INET; + else if (inet_pton (AF_INET6, addrstr, &a) == 1) + addr_family = AF_INET6; + else + g_error ("%s:%d: invalid IP address in argument: %s", filename, lineno, addrstr); + + addrs_bin[i] = (IPAddressesAssertData) { + .addr_family = addr_family, + .addr = a, + .found = FALSE, + }; + } + + for (IS_IPv4 = 1; IS_IPv4 >= 0; IS_IPv4--) { + const int addr_family = IS_IPv4 ? AF_INET : AF_INET6; + gs_unref_ptrarray GPtrArray *plat_addrs = NULL; + NMPLookup lookup; + guint j; + + plat_addrs = nm_platform_lookup_clone (self, + nmp_lookup_init_object (&lookup, + NMP_OBJECT_TYPE_IP_ADDRESS (IS_IPv4), + ifindex), + NULL, + NULL); + + for (i = 0; i < addrs_len; i++) { + IPAddressesAssertData *addr_bin = &addrs_bin[i]; + + if (addr_bin->addr_family != addr_family) + continue; + + g_assert (!addr_bin->found); + for (j = 0; j < nm_g_ptr_array_len (plat_addrs); ) { + const NMPlatformIPAddress *a = NMP_OBJECT_CAST_IP_ADDRESS (plat_addrs->pdata[j]); + + if (memcmp (&addr_bin->addr, a->address_ptr, nm_utils_addr_family_to_size (addr_family)) != 0) { + j++; + continue; + } + + g_assert (!addr_bin->found); + addr_bin->found = TRUE; + g_ptr_array_remove_index_fast (plat_addrs, j); + } + + if (!addr_bin->found) { + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + + g_error ("%s:%d: IPv%c address %s was not found on ifindex %d", + filename, + lineno, + nm_utils_addr_family_to_char (addr_bin->addr_family), + nm_utils_inet_ntop (addr_bin->addr_family, &addr_bin->addr, sbuf), + ifindex); + } + } + if ( !IS_IPv4 + && ignore_ll6 + && nm_g_ptr_array_len (plat_addrs) > 0) { + /* we prune all remaining, non-matching IPv6 link local addresses. */ + for (j = 0; j < nm_g_ptr_array_len (plat_addrs); ) { + const NMPlatformIPAddress *a = NMP_OBJECT_CAST_IP_ADDRESS (plat_addrs->pdata[j]); + + if (!IN6_IS_ADDR_LINKLOCAL (a->address_ptr)) { + j++; + continue; + } + + g_ptr_array_remove_index_fast (plat_addrs, j); + } + } + if ( (IS_IPv4 ? force_exact_4 : force_exact_6) + && nm_g_ptr_array_len (plat_addrs) > 0) { + char sbuf[sizeof (_nm_utils_to_string_buffer)]; + + g_error ("%s:%d: %u IPv%c addresses found on ifindex %d that should not be there (one is %s)", + filename, + lineno, + plat_addrs->len, + nm_utils_addr_family_to_char (addr_family), + ifindex, + nmp_object_to_string (plat_addrs->pdata[0], NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof (sbuf))); + } + } + +} + +/*****************************************************************************/ + gboolean nmtstp_platform_ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, guint8 plen, guint32 metric) { diff --git a/src/platform/tests/test-common.h b/src/platform/tests/test-common.h index 095daa044f..3599f50850 100644 --- a/src/platform/tests/test-common.h +++ b/src/platform/tests/test-common.h @@ -283,6 +283,32 @@ nmtstp_platform_ip6_address_find (NMPlatform *self, return (const NMPlatformIP6Address *) nmtstp_platform_ip_address_find (self, ifindex, AF_INET6, addr); } +void _nmtstp_platform_ip_addresses_assert (const char *filename, + int lineno, + NMPlatform *self, + int ifindex, + gboolean force_exact_4, + gboolean force_exact_6, + gboolean ignore_ll6, + guint addrs_len, + const char *const*addrs); + +#define nmtstp_platform_ip_addresses_assert(self, \ + ifindex, \ + force_exact_4, \ + force_exact_6, \ + ignore_ll6, \ + ...) \ + _nmtstp_platform_ip_addresses_assert (__FILE__, \ + __LINE__, \ + (self), \ + (ifindex), \ + (force_exact_4), \ + (force_exact_6), \ + (ignore_ll6), \ + NM_NARG (__VA_ARGS__), \ + ((const char *const[]) { "dummy", ##__VA_ARGS__, NULL }) + 1) + /*****************************************************************************/ static inline gboolean