diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 94040c12c..c61e8f85f 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 afcb88b84..904b4cdc7 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -59,6 +59,60 @@ #define CLOCK_BOOTTIME 7 #endif +G_STATIC_ASSERT (sizeof (NMUtilsTestFlags) <= sizeof (int)); +int _nm_utils_testing = 0; + +gboolean +nm_utils_get_testing_initialized () +{ + NMUtilsTestFlags flags; + + flags = (NMUtilsTestFlags) _nm_utils_testing; + if (flags == NM_UTILS_TEST_NONE) + flags = (NMUtilsTestFlags) g_atomic_int_get (&_nm_utils_testing); + return flags != NM_UTILS_TEST_NONE; +} + +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_return_if_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 fe88a7201..593d2be71 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -189,4 +189,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__ */