From 0e78ce5ed6ef20fc54623d601cc3d0c3fea67d05 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 Apr 2016 16:53:02 +0200 Subject: nmtst: add nmtst_rand_buf() util --- shared/nm-test-utils.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'shared/nm-test-utils.h') diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h index 2503245761..8e21186dda 100644 --- a/shared/nm-test-utils.h +++ b/shared/nm-test-utils.h @@ -782,6 +782,34 @@ nmtst_get_rand_int (void) return g_rand_int (nmtst_get_rand ()); } +inline static gpointer +nmtst_rand_buf (GRand *rand, gpointer buffer, gsize buffer_length) +{ + guint32 v; + guint8 *b = buffer; + + if (!buffer_length) + return buffer; + + g_assert (buffer); + + if (!rand) + rand = nmtst_get_rand (); + + for (; buffer_length >= sizeof (guint32); buffer_length -= sizeof (guint32), b += sizeof (guint32)) { + v = g_rand_int (rand); + memcpy (b, &v, sizeof (guint32)); + } + if (buffer_length > 0) { + v = g_rand_int (rand); + do { + *(b++) = v & 0xFF; + v >>= 8; + } while (--buffer_length > 0); + } + return buffer; +} + inline static void * nmtst_rand_perm (GRand *rand, void *dst, const void *src, gsize elmt_size, gsize n_elmt) { -- cgit v1.2.1 From 2e6ec6d8ac8a4ec0ee51a03b8bd1118a72f0f59b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 6 Apr 2016 15:33:53 +0200 Subject: nmtst: add nmtst_platform_ip4_address() util --- shared/nm-test-utils.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'shared/nm-test-utils.h') diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h index 8e21186dda..b792a69c85 100644 --- a/shared/nm-test-utils.h +++ b/shared/nm-test-utils.h @@ -1179,11 +1179,54 @@ _nmtst_assert_resolve_relative_path_equals (const char *f1, const char *f2, cons #ifdef __NETWORKMANAGER_PLATFORM_H__ +inline static NMPlatformIP4Address * +nmtst_platform_ip4_address (const char *address, const char *peer_address, guint plen) +{ + static NMPlatformIP4Address addr; + + g_assert (plen <= 32); + + memset (&addr, 0, sizeof (addr)); + addr.address = nmtst_inet4_from_string (address); + if (peer_address) + addr.peer_address = nmtst_inet4_from_string (peer_address); + else + addr.peer_address = addr.address; + addr.plen = plen; + + return &addr; +} + +inline static NMPlatformIP4Address * +nmtst_platform_ip4_address_full (const char *address, const char *peer_address, guint plen, + int ifindex, NMIPConfigSource source, guint32 timestamp, + guint32 lifetime, guint32 preferred, guint32 flags, + const char *label) +{ + NMPlatformIP4Address *addr = nmtst_platform_ip4_address (address, peer_address, plen); + + G_STATIC_ASSERT (IFNAMSIZ == sizeof (addr->label)); + g_assert (!label || strlen (label) < IFNAMSIZ); + + addr->ifindex = ifindex; + addr->source = source; + addr->timestamp = timestamp; + addr->lifetime = lifetime; + addr->preferred = preferred; + addr->n_ifa_flags = flags; + if (label) + g_strlcpy (addr->label, label, sizeof (addr->label)); + + return addr; +} + inline static NMPlatformIP6Address * nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen) { static NMPlatformIP6Address addr; + g_assert (plen <= 128); + memset (&addr, 0, sizeof (addr)); addr.address = *nmtst_inet6_from_string (address); addr.peer_address = *nmtst_inet6_from_string (peer_address); @@ -1214,6 +1257,8 @@ nmtst_platform_ip4_route (const char *network, guint plen, const char *gateway) { static NMPlatformIP4Route route; + g_assert (plen <= 32); + memset (&route, 0, sizeof (route)); route.network = nmtst_inet4_from_string (network); route.plen = plen; @@ -1246,6 +1291,8 @@ nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway) { static NMPlatformIP6Route route; + nm_assert (plen <= 128); + memset (&route, 0, sizeof (route)); route.network = *nmtst_inet6_from_string (network); route.plen = plen; -- cgit v1.2.1 From 6bf022359f18319fabde5eb20bd8983844a113d8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 11 Apr 2016 13:09:52 +0200 Subject: core/trivial: rename "source" field of addresses and routes The "source" field of NMPlatformIPRoute (now "rt_source") maps to the protocol field of the route. The source of NMPlatformIPAddress (now "addr_source") has no direct equivalent in the kernel. As their use is different, they should have different names. Also, the name "source" is used all over the place. Hence give the fields a more distinct name. --- shared/nm-test-utils.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'shared/nm-test-utils.h') diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h index b792a69c85..0759b3c4fd 100644 --- a/shared/nm-test-utils.h +++ b/shared/nm-test-utils.h @@ -1209,7 +1209,7 @@ nmtst_platform_ip4_address_full (const char *address, const char *peer_address, g_assert (!label || strlen (label) < IFNAMSIZ); addr->ifindex = ifindex; - addr->source = source; + addr->addr_source = source; addr->timestamp = timestamp; addr->lifetime = lifetime; addr->preferred = preferred; @@ -1243,7 +1243,7 @@ nmtst_platform_ip6_address_full (const char *address, const char *peer_address, NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen); addr->ifindex = ifindex; - addr->source = source; + addr->addr_source = source; addr->timestamp = timestamp; addr->lifetime = lifetime; addr->preferred = preferred; @@ -1277,7 +1277,7 @@ nmtst_platform_ip4_route_full (const char *network, guint plen, const char *gate NMPlatformIP4Route *route = nmtst_platform_ip4_route (network, plen, gateway); route->ifindex = ifindex; - route->source = source; + route->rt_source = source; route->metric = metric; route->mss = mss; route->scope_inv = nm_platform_route_scope_inv (scope); @@ -1309,7 +1309,7 @@ nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gate NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway); route->ifindex = ifindex; - route->source = source; + route->rt_source = source; route->metric = metric; route->mss = mss; -- cgit v1.2.1 From fbf4904ae004159959b4b3e203810badc079021e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 9 May 2016 11:04:51 +0200 Subject: nmtst: allow non-boolean argument to nmtst_assert_success() Reimplement nmtst_assert_success() as a macro which allows non-boolean @success arguments. --- shared/nm-test-utils.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'shared/nm-test-utils.h') diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h index 0759b3c4fd..0875518cdd 100644 --- a/shared/nm-test-utils.h +++ b/shared/nm-test-utils.h @@ -153,13 +153,11 @@ g_assert_not_reached (); \ } G_STMT_END -inline static void -_nmtst_assert_success (gboolean success, GError *error, const char *file, int line) -{ - if (!success || error) - g_error ("(%s:%d) FAILURE success=%d, error=%s", file, line, success, error ? error->message : "(no error)"); -} -#define nmtst_assert_success(success, error) _nmtst_assert_success ((success), (error), __FILE__, __LINE__) +#define nmtst_assert_success(success, error) \ + G_STMT_START { \ + g_assert_no_error (error); \ + g_assert ((success)); \ + } G_STMT_END #define nmtst_assert_no_success(success, error) \ G_STMT_START { \ -- cgit v1.2.1 From a2c843499ca15ca507f6cab2e071a1068d1d709f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 17 May 2016 14:04:28 +0200 Subject: all/tests: split core part out of "nm-test-utils.h" A large part of "nm-test-utils.h" is only relevant for tests inside "src/" directory, as they are helpers related to NetworkManager core part. Split this part out of "nm-test-utils.h" header. --- shared/nm-test-utils.h | 274 +------------------------------------------------ 1 file changed, 3 insertions(+), 271 deletions(-) (limited to 'shared/nm-test-utils.h') diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h index 0875518cdd..ad3cad650f 100644 --- a/shared/nm-test-utils.h +++ b/shared/nm-test-utils.h @@ -101,13 +101,6 @@ #include "nm-utils.h" -#ifdef __NETWORKMANAGER_LOGGING_H__ -/* We are running tests under src/. Let's include some files by default. - * They are useful, and affect how nm-test-utils.h itself behaves. */ -#include "NetworkManagerUtils.h" -#include "nm-keyfile-internal.h" -#endif - /*******************************************************************************/ /* general purpose functions that have no dependency on other nmtst functions */ @@ -510,7 +503,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ if (!__nmtst_internal.assert_logging) { gboolean success = TRUE; -#ifdef __NETWORKMANAGER_LOGGING_H__ +#ifdef _NMTST_INSIDE_CORE success = nm_logging_setup (log_level, log_domains, NULL, NULL); *out_set_logging = TRUE; #endif @@ -527,7 +520,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ * This transforms g_test_expect_message() into a NOP, but we also have to relax * g_log_set_always_fatal(), which was set by g_test_init(). */ g_log_set_always_fatal (G_LOG_FATAL_MASK); -#ifdef __NETWORKMANAGER_LOGGING_H__ +#ifdef _NMTST_INSIDE_CORE if (c_log_domains || c_log_level) { /* Normally, tests with assert_logging do not overwrite the logging level/domains because * the logging statements are part of the assertions. But if the test is run with @@ -588,27 +581,7 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ #endif } -#ifdef __NETWORKMANAGER_LOGGING_H__ -inline static void -nmtst_init_with_logging (int *argc, char ***argv, const char *log_level, const char *log_domains) -{ - __nmtst_init (argc, argv, FALSE, log_level, log_domains, NULL); -} -inline static void -nmtst_init_assert_logging (int *argc, char ***argv, const char *log_level, const char *log_domains) -{ - gboolean set_logging; - - __nmtst_init (argc, argv, TRUE, NULL, NULL, &set_logging); - - if (!set_logging) { - gboolean success; - - success = nm_logging_setup (log_level, log_domains, NULL, NULL); - g_assert (success); - } -} -#else +#ifndef _NMTST_INSIDE_CORE inline static void nmtst_init (int *argc, char ***argv, gboolean assert_logging) { @@ -1175,247 +1148,6 @@ _nmtst_assert_resolve_relative_path_equals (const char *f1, const char *f2, cons /*******************************************************************************/ -#ifdef __NETWORKMANAGER_PLATFORM_H__ - -inline static NMPlatformIP4Address * -nmtst_platform_ip4_address (const char *address, const char *peer_address, guint plen) -{ - static NMPlatformIP4Address addr; - - g_assert (plen <= 32); - - memset (&addr, 0, sizeof (addr)); - addr.address = nmtst_inet4_from_string (address); - if (peer_address) - addr.peer_address = nmtst_inet4_from_string (peer_address); - else - addr.peer_address = addr.address; - addr.plen = plen; - - return &addr; -} - -inline static NMPlatformIP4Address * -nmtst_platform_ip4_address_full (const char *address, const char *peer_address, guint plen, - int ifindex, NMIPConfigSource source, guint32 timestamp, - guint32 lifetime, guint32 preferred, guint32 flags, - const char *label) -{ - NMPlatformIP4Address *addr = nmtst_platform_ip4_address (address, peer_address, plen); - - G_STATIC_ASSERT (IFNAMSIZ == sizeof (addr->label)); - g_assert (!label || strlen (label) < IFNAMSIZ); - - addr->ifindex = ifindex; - addr->addr_source = source; - addr->timestamp = timestamp; - addr->lifetime = lifetime; - addr->preferred = preferred; - addr->n_ifa_flags = flags; - if (label) - g_strlcpy (addr->label, label, sizeof (addr->label)); - - return addr; -} - -inline static NMPlatformIP6Address * -nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen) -{ - static NMPlatformIP6Address addr; - - g_assert (plen <= 128); - - memset (&addr, 0, sizeof (addr)); - addr.address = *nmtst_inet6_from_string (address); - addr.peer_address = *nmtst_inet6_from_string (peer_address); - addr.plen = plen; - - return &addr; -} - -inline static NMPlatformIP6Address * -nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen, - int ifindex, NMIPConfigSource source, guint32 timestamp, - guint32 lifetime, guint32 preferred, guint32 flags) -{ - NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen); - - addr->ifindex = ifindex; - addr->addr_source = source; - addr->timestamp = timestamp; - addr->lifetime = lifetime; - addr->preferred = preferred; - addr->n_ifa_flags = flags; - - return addr; -} - -inline static NMPlatformIP4Route * -nmtst_platform_ip4_route (const char *network, guint plen, const char *gateway) -{ - static NMPlatformIP4Route route; - - g_assert (plen <= 32); - - memset (&route, 0, sizeof (route)); - route.network = nmtst_inet4_from_string (network); - route.plen = plen; - route.gateway = nmtst_inet4_from_string (gateway); - - return &route; -} - -inline static NMPlatformIP4Route * -nmtst_platform_ip4_route_full (const char *network, guint plen, const char *gateway, - int ifindex, NMIPConfigSource source, - guint metric, guint mss, - guint8 scope, - const char *pref_src) -{ - NMPlatformIP4Route *route = nmtst_platform_ip4_route (network, plen, gateway); - - route->ifindex = ifindex; - route->rt_source = source; - route->metric = metric; - route->mss = mss; - route->scope_inv = nm_platform_route_scope_inv (scope); - route->pref_src = nmtst_inet4_from_string (pref_src); - - return route; -} - -inline static NMPlatformIP6Route * -nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway) -{ - static NMPlatformIP6Route route; - - nm_assert (plen <= 128); - - memset (&route, 0, sizeof (route)); - route.network = *nmtst_inet6_from_string (network); - route.plen = plen; - route.gateway = *nmtst_inet6_from_string (gateway); - - return &route; -} - -inline static NMPlatformIP6Route * -nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway, - int ifindex, NMIPConfigSource source, - guint metric, guint mss) -{ - NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway); - - route->ifindex = ifindex; - route->rt_source = source; - route->metric = metric; - route->mss = mss; - - return route; -} - -inline static int -_nmtst_platform_ip4_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data) -{ - return nm_platform_ip4_route_cmp ((const NMPlatformIP4Route *) a, (const NMPlatformIP4Route *) b); -} - -inline static void -nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b, gsize len, gboolean ignore_order) -{ - gsize i; - gs_free const NMPlatformIP4Route *c_a = NULL, *c_b = NULL; - - g_assert (a); - g_assert (b); - - if (ignore_order) { - a = c_a = g_memdup (a, sizeof (NMPlatformIP4Route) * len); - b = c_b = g_memdup (b, sizeof (NMPlatformIP4Route) * len); - g_qsort_with_data (c_a, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL); - g_qsort_with_data (c_b, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL); - } - - for (i = 0; i < len; i++) { - if (nm_platform_ip4_route_cmp (&a[i], &b[i]) != 0) { - char buf[sizeof (_nm_utils_to_string_buffer)]; - - g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (long unsigned) i, - nm_platform_ip4_route_to_string (&a[i], NULL, 0), - nm_platform_ip4_route_to_string (&b[i], buf, sizeof (buf))); - g_assert_not_reached (); - } - } -} - -inline static int -_nmtst_platform_ip6_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data) -{ - return nm_platform_ip6_route_cmp ((const NMPlatformIP6Route *) a, (const NMPlatformIP6Route *) b); -} - -inline static void -nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b, gsize len, gboolean ignore_order) -{ - gsize i; - gs_free const NMPlatformIP6Route *c_a = NULL, *c_b = NULL; - - g_assert (a); - g_assert (b); - - if (ignore_order) { - a = c_a = g_memdup (a, sizeof (NMPlatformIP6Route) * len); - b = c_b = g_memdup (b, sizeof (NMPlatformIP6Route) * len); - g_qsort_with_data (c_a, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL); - g_qsort_with_data (c_b, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL); - } - - for (i = 0; i < len; i++) { - if (nm_platform_ip6_route_cmp (&a[i], &b[i]) != 0) { - char buf[sizeof (_nm_utils_to_string_buffer)]; - - g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (long unsigned) i, - nm_platform_ip6_route_to_string (&a[i], NULL, 0), - nm_platform_ip6_route_to_string (&b[i], buf, sizeof (buf))); - g_assert_not_reached (); - } - } -} - -#endif - - -#ifdef __NETWORKMANAGER_IP4_CONFIG_H__ - -inline static NMIP4Config * -nmtst_ip4_config_clone (NMIP4Config *config) -{ - NMIP4Config *copy = nm_ip4_config_new (-1); - - g_assert (copy); - g_assert (config); - nm_ip4_config_replace (copy, config, NULL); - return copy; -} - -#endif - - -#ifdef __NETWORKMANAGER_IP6_CONFIG_H__ - -inline static NMIP6Config * -nmtst_ip6_config_clone (NMIP6Config *config) -{ - NMIP6Config *copy = nm_ip6_config_new (-1); - - g_assert (copy); - g_assert (config); - nm_ip6_config_replace (copy, config, NULL); - return copy; -} - -#endif - #ifdef NM_SETTING_IP_CONFIG_H inline static void nmtst_setting_ip_config_add_address (NMSettingIPConfig *s_ip, -- cgit v1.2.1