From 1e4c0dd6805c75694e86342fe6a0c01bae5198fc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 27 Nov 2018 10:20:25 +0100 Subject: [PATCH] shared/tests: add helper functions to convert IP address to string We have nm_utils_inet*_ntop(), however: - that is partly private API libnm-core, and thus only available in components that have access to that. Partly it's public API of libnm, but still only available in components that use libnm. - relying on the static buffers is discouraged for nm_utils_inet*_ntop(). For testing, that is fine as we are in a more controlled envionment. So, add a test variant that explicitly relies on static buffers. That way, it's more convenient to use from tests. - these functions can assert more and are more convenient to use from tests. --- shared/nm-utils/nm-test-utils.h | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 18b42888e..64a024ca4 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -1166,6 +1166,48 @@ nmtst_inet6_from_string (const char *str) return &addr; } +static inline gconstpointer +nmtst_inet_from_string (int addr_family, const char *str) +{ + if (addr_family == AF_INET) { + static in_addr_t a; + + a = nmtst_inet4_from_string (str); + return &a; + } + if (addr_family == AF_INET6) + return nmtst_inet6_from_string (str); + + g_assert_not_reached (); + return NULL; +} + +static inline const char * +nmtst_inet_to_string (int addr_family, gconstpointer addr) +{ + static char buf[MAX (INET6_ADDRSTRLEN, INET_ADDRSTRLEN)]; + + g_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6)); + g_assert (addr); + + if (inet_ntop (addr_family, addr, buf, sizeof (buf)) != buf) + g_assert_not_reached (); + + return buf; +} + +static inline const char * +nmtst_inet4_to_string (in_addr_t addr) +{ + return nmtst_inet_to_string (AF_INET, &addr); +} + +static inline const char * +nmtst_inet6_to_string (const struct in6_addr *addr) +{ + return nmtst_inet_to_string (AF_INET6, addr); +} + static inline void _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const char *str_expected) {