summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-01-14 13:15:24 +0100
committerThomas Haller <thaller@redhat.com>2015-05-06 17:23:33 +0200
commit0405790e83fc15af78cfc9d5ae7ebb667331bc35 (patch)
treeaaa01d1848e11577a840aa9b4365c8a62a127c98
parent113a4bcb213279b4f3d9f1d3c5bb5e84325285b3 (diff)
downloadNetworkManager-0405790e83fc15af78cfc9d5ae7ebb667331bc35.tar.gz
core/tests: add nm_utils_get_testing() function
Code that is testable often needs special hooks to work both for unit-tests and production. Add a function nm_utils_get_testing() that returns whether the code is run as part of a unit-test. For non-testing mode, nm_utils_get_testing() will return zero (NM_UTILS_TEST_NONE). For unit tests, the test should call _nm_utils_set_testing() to configure tested functions. By specifing the @flags attribute, the test can enable/disable specific behaviors. https://bugzilla.gnome.org/show_bug.cgi?id=701112
-rw-r--r--include/nm-test-utils.h9
-rw-r--r--src/NetworkManagerUtils.c49
-rw-r--r--src/NetworkManagerUtils.h17
3 files changed, 75 insertions, 0 deletions
diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h
index 04c5d5f1c5..9a56a97c7b 100644
--- a/include/nm-test-utils.h
+++ b/include/nm-test-utils.h
@@ -96,6 +96,10 @@
#include "nm-glib-compat.h"
#include "gsystem-local-alloc.h"
+#ifdef __NETWORKMANAGER_LOGGING_H__
+/* We are running tests under src/ */
+#include "NetworkManagerUtils.h"
+#endif
/* Analog to EXIT_SUCCESS and EXIT_FAILURE. */
#define EXIT_SKIP (77)
@@ -258,6 +262,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);
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index 82c872175e..1657de3a88 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -59,6 +59,55 @@
#define CLOCK_BOOTTIME 7
#endif
+G_STATIC_ASSERT (sizeof (NMUtilsTestFlags) <= sizeof (int));
+int _nm_utils_testing = 0;
+
+gboolean
+nm_utils_get_testing_initialized ()
+{
+ return NM_FLAGS_HAS ((NMUtilsTestFlags) g_atomic_int_get (&_nm_utils_testing), _NM_UTILS_TEST_INITIALIZED);
+}
+
+NMUtilsTestFlags
+nm_utils_get_testing ()
+{
+ NMUtilsTestFlags flags;
+
+ flags = (NMUtilsTestFlags) _nm_utils_testing;
+ if (flags != NM_UTILS_TEST_NONE) {
+ /* Flags already initialized. Return them. */
+ return flags & NM_UTILS_TEST_ALL;
+ }
+
+ /* Accessing nm_utils_get_testing() causes us to set the flags to initialized.
+ * Detecting running tests also based on g_test_initialized(). */
+ flags = _NM_UTILS_TEST_INITIALIZED;
+ if (g_test_initialized ())
+ flags |= _NM_UTILS_TEST_GENERAL;
+
+ if (g_atomic_int_compare_and_exchange (&_nm_utils_testing, 0, (int) flags)) {
+ /* Done. We set it. */
+ return flags & NM_UTILS_TEST_ALL;
+ }
+ /* It changed in the meantime (??). Re-read the value. */
+ return ((NMUtilsTestFlags) _nm_utils_testing) & NM_UTILS_TEST_ALL;
+}
+
+void
+_nm_utils_set_testing (NMUtilsTestFlags flags)
+{
+ g_assert (!NM_FLAGS_ANY (flags, ~NM_UTILS_TEST_ALL));
+
+ /* mask out everything except ALL, and always set GENERAL. */
+ flags = (flags & NM_UTILS_TEST_ALL) | (_NM_UTILS_TEST_GENERAL | _NM_UTILS_TEST_INITIALIZED);
+
+ if (!g_atomic_int_compare_and_exchange (&_nm_utils_testing, 0, (int) flags)) {
+ /* We only allow setting _nm_utils_set_testing() once, before fetching the
+ * value with nm_utils_get_testing(). */
+ g_assert_not_reached ();
+ }
+}
+
/*
* nm_ethernet_address_is_valid:
* @addr: pointer to a binary or ASCII Ethernet address
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index 1e62ee7b62..50d1a0f310 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -188,4 +188,21 @@ void nm_utils_array_remove_at_indexes (GArray *array, const guint *indexes_to_de
void nm_utils_setpgid (gpointer unused);
+typedef enum {
+ NM_UTILS_TEST_NONE = 0,
+
+ /* Internal flag, marking that either nm_utils_get_testing() or _nm_utils_set_testing() was called. */
+ _NM_UTILS_TEST_INITIALIZED = (1LL << 0),
+
+ /* Indicate that test mode is enabled in general. Explicitly calling _nm_utils_set_testing() will always set this flag. */
+ _NM_UTILS_TEST_GENERAL = (1LL << 1),
+
+ _NM_UTILS_TEST_LAST,
+ NM_UTILS_TEST_ALL = (((_NM_UTILS_TEST_LAST - 1) << 1) - 1) & ~(_NM_UTILS_TEST_INITIALIZED),
+} NMUtilsTestFlags;
+
+gboolean nm_utils_get_testing_initialized (void);
+NMUtilsTestFlags nm_utils_get_testing (void);
+void _nm_utils_set_testing (NMUtilsTestFlags flags);
+
#endif /* __NETWORKMANAGER_UTILS_H__ */