about summary refs log tree commit diff
path: root/include/nm-test-utils.h
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2015-07-14 19:38:58 +0200
committerMichael Biebl <biebl@debian.org>2015-07-14 19:38:58 +0200
commit50a58f0fabd8a34c1b6108a107e08abe3c1ccd24 (patch)
tree6790165f39daee79e2b6c6617483320613493367 /include/nm-test-utils.h
parentf408e27bccfacf347605a8d98649975a68f38a17 (diff)
Imported Upstream version 1.0.4 upstream/1.0.4
Diffstat (limited to 'include/nm-test-utils.h')
-rw-r--r--include/nm-test-utils.h123
1 files changed, 113 insertions, 10 deletions
diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h
index 5f83d09d..258d3937 100644
--- a/include/nm-test-utils.h
+++ b/include/nm-test-utils.h
@@ -92,13 +92,14 @@
 #include <errno.h>
 
 #include "nm-utils.h"
-#include "nm-utils-internal.h"
+#include "nm-macros-internal.h"
 #include "nm-glib-compat.h"
 #include "gsystem-local-alloc.h"
 
-
-/* Analog to EXIT_SUCCESS and EXIT_FAILURE. */
-#define EXIT_SKIP (77)
+#ifdef __NETWORKMANAGER_LOGGING_H__
+/* We are running tests under src/ */
+#include "NetworkManagerUtils.h"
+#endif
 
 /*******************************************************************************/
 
@@ -122,6 +123,14 @@ nmtst_assert_error (GError *error,
 	}
 }
 
+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 ? error->message : "(no error)");
+}
+#define nmtst_assert_success(success, error) _nmtst_assert_success ((success), (error), __FILE__, __LINE__)
+
 /*******************************************************************************/
 
 struct __nmtst_internal
@@ -258,6 +267,11 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_
 	g_assert (!argc || (g_strv_length (*argv) == *argc));
 	g_assert (!assert_logging || (!log_level && !log_domains));
 
+#ifdef __NETWORKMANAGER_UTILS_H__
+	if (!nm_utils_get_testing_initialized ())
+		_nm_utils_set_testing (_NM_UTILS_TEST_GENERAL);
+#endif
+
 	if (argc)
 		__nmtst_internal.orig_argv = g_strdupv (*argv);
 
@@ -710,6 +724,39 @@ __nmtst_spawn_sync (const char *working_directory, char **standard_out, char **s
 
 /*******************************************************************************/
 
+inline static char *
+nmtst_file_resolve_relative_path (const char *rel, const char *cwd)
+{
+	gs_free char *cwd_free = NULL;
+
+	g_assert (rel && *rel);
+
+	if (g_path_is_absolute (rel))
+		return g_strdup (rel);
+
+	if (!cwd)
+		cwd = cwd_free = g_get_current_dir ();
+	return g_build_filename (cwd, rel, NULL);
+}
+
+inline static void
+_nmtst_assert_resolve_relative_path_equals (const char *f1, const char *f2, const char *file, int line)
+{
+	gs_free char *p1 = NULL, *p2 = NULL;
+
+	p1 = nmtst_file_resolve_relative_path (f1, NULL);
+	p2 = nmtst_file_resolve_relative_path (f2, NULL);
+	g_assert (p1 && *p1);
+
+	/* Fixme: later we might need to coalesce repeated '/', "./", and "../".
+	 * For now, it's good enough. */
+	if (g_strcmp0 (p1, p2) != 0)
+		g_error ("%s:%d : filenames don't match \"%s\" vs. \"%s\" // \"%s\" - \"%s\"", file, line, f1, f2, p1, p2);
+}
+#define nmtst_assert_resolve_relative_path_equals(f1, f2) _nmtst_assert_resolve_relative_path_equals (f1, f2, __FILE__, __LINE__);
+
+/*******************************************************************************/
+
 #ifdef __NETWORKMANAGER_PLATFORM_H__
 
 inline static NMPlatformIP6Address *
@@ -742,6 +789,38 @@ nmtst_platform_ip6_address_full (const char *address, const char *peer_address,
 	return addr;
 }
 
+inline static NMPlatformIP4Route *
+nmtst_platform_ip4_route (const char *network, guint plen, const char *gateway)
+{
+	static NMPlatformIP4Route route;
+
+	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->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)
 {
@@ -799,9 +878,6 @@ nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP
 			         nmtst_static_1024_02 (nm_platform_ip4_route_to_string (&b[i])));
 			g_assert_not_reached ();
 		}
-
-		/* also check with memcmp, though this might fail for valid programs (due to field alignment) */
-		g_assert_cmpint (memcmp (&a[i], &b[i], sizeof (a[i])), ==, 0);
 	}
 }
 
@@ -834,9 +910,6 @@ nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP
 			         nmtst_static_1024_02 (nm_platform_ip6_route_to_string (&b[i])));
 			g_assert_not_reached ();
 		}
-
-		/* also check with memcmp, though this might fail for valid programs (due to field alignment) */
-		g_assert_cmpint (memcmp (&a[i], &b[i], sizeof (a[i])), ==, 0);
 	}
 }
 
@@ -1193,6 +1266,36 @@ nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const cha
     nmtst_assert_hwaddr_equals (hwaddr1, hwaddr1_len, expected, G_STRLOC)
 #endif
 
+#if defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__) && defined(__NM_KEYFILE_INTERNAL_H__)
+
+inline static NMConnection *
+nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *keyfile_name, const char *base_dir)
+{
+	GKeyFile *keyfile;
+	GError *error = NULL;
+	gboolean success;
+	NMConnection *con;
+
+	g_assert (keyfile_str);
+
+	keyfile =  g_key_file_new ();
+	success = g_key_file_load_from_data (keyfile, keyfile_str, strlen (keyfile_str), G_KEY_FILE_NONE, &error);
+	g_assert_no_error (error);
+	g_assert (success);
+
+	con = nm_keyfile_read (keyfile, keyfile_name, base_dir, NULL, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (NM_IS_CONNECTION (con));
+
+	g_key_file_unref (keyfile);
+
+	nmtst_connection_normalize (con);
+
+	return con;
+}
+
+#endif
+
 #ifdef __NM_CONNECTION_H__
 
 typedef enum {