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
This commit is contained in:
Thomas Haller
2015-01-14 13:15:24 +01:00
parent 9b11276d75
commit b9d8dc050a
3 changed files with 80 additions and 0 deletions

View File

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

View File

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

View File

@@ -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__ */