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.
This commit is contained in:
Thomas Haller
2018-11-27 10:20:25 +01:00
parent e442e3881e
commit 1e4c0dd680

View File

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