diff options
| author | Iain Lane <iain@orangesquash.org.uk> | 2018-09-24 09:29:55 +0100 |
|---|---|---|
| committer | Iain Lane <iain@orangesquash.org.uk> | 2018-09-24 09:29:55 +0100 |
| commit | e152ec7bf4ba252ff9d3eb13eabd417b931dac9a (patch) | |
| tree | c323cf856ee0bb8e44590670dd54c19653a55748 /shared/nm-utils/nm-test-utils.h | |
| parent | ee9c73a923909e23a649407be77e25235d769e25 (diff) | |
Import Upstream version 1.12.2
Diffstat (limited to 'shared/nm-utils/nm-test-utils.h')
| -rw-r--r-- | shared/nm-utils/nm-test-utils.h | 127 |
1 files changed, 76 insertions, 51 deletions
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 126546ec..743893f0 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -21,6 +21,10 @@ #ifndef __NM_TEST_UTILS_H__ #define __NM_TEST_UTILS_H__ +#if defined(NETWORKMANAGER_COMPILATION) && !defined(NETWORKMANAGER_COMPILATION_TEST) +#error Need to mark the compilation with NETWORKMANAGER_COMPILATION_TEST. +#endif + /******************************************************************************* * HOWTO run tests. * @@ -39,9 +43,10 @@ * and g_test_assert_expected_messages() will not fail. * * NMTST_SEED_RAND environment variable: - * Tests that use random numbers from nmtst_get_rand() get seeded randomly at each start. - * You can specify the seed by setting NMTST_SEED_RAND. Also, tests will print the seed - * to stdout, so that you know the chosen seed. + * Tests that use random numbers from nmtst_get_rand() get seeded at each start. + * You can specify the seed by setting NMTST_SEED_RAND to a particular number or empty ("") + * for a random one. If NMTST_SEED_RAND is not set (default) a stable seed gets chosen. + * Tests will print the seed to stdout, so that you know which one was chosen or generated. * * * NMTST_DEBUG environment variable: @@ -141,10 +146,16 @@ const gint64 nmtst_wait_start_us = g_get_monotonic_time (); \ const gint64 nmtst_wait_duration_us = (max_wait_ms) * 1000L; \ const gint64 nmtst_wait_end_us = nmtst_wait_start_us + nmtst_wait_duration_us; \ + gint64 _nmtst_wait_remaining_us = nmtst_wait_duration_us; \ + int _nmtst_wait_iteration = 0; \ \ while (TRUE) { \ + _nm_unused const gint64 nmtst_wait_remaining_us = _nmtst_wait_remaining_us; \ + _nm_unused int nmtst_wait_iteration = _nmtst_wait_iteration++; \ + \ { wait }; \ - if (g_get_monotonic_time () > nmtst_wait_end_us) { \ + _nmtst_wait_remaining_us = (nmtst_wait_end_us - g_get_monotonic_time ()); \ + if (_nmtst_wait_remaining_us <= 0) { \ _not_expired = FALSE; \ break; \ } \ @@ -158,6 +169,14 @@ g_assert_not_reached (); \ } G_STMT_END +#define nmtst_assert_nonnull(command) \ + ({ \ + typeof (*(command)) *_ptr = (command); \ + \ + g_assert (_ptr && (TRUE || (command))); \ + _ptr; \ + }) + #define nmtst_assert_success(success, error) \ G_STMT_START { \ g_assert_no_error (error); \ @@ -199,7 +218,6 @@ _nmtst_exit (void) \ nmtst_free (); \ } - static inline gboolean nmtst_initialized (void) { @@ -264,7 +282,6 @@ BREAK_INNER_LOOPS: return (char **) g_array_free (result, FALSE); } - /* free instances allocated by nmtst (especially nmtst_init()) on shutdown * to release memory. After nmtst_free(), the test is uninitialized again. */ static inline void @@ -328,8 +345,6 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ __nmtst_internal.assert_logging = !!assert_logging; - nm_g_type_init (); - is_debug = g_test_verbose (); nmtst_debug = g_getenv ("NMTST_DEBUG"); @@ -424,6 +439,11 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ g_array_append_val (debug_messages, msg); } } else { + /* We're intentionally assigning a value to static variables + * s_tests_x and p_tests_x without using it afterwards, just + * so that valgrind doesn't complain about the leak. */ + NM_PRAGMA_WARNING_DISABLE("-Wunused-but-set-variable") + /* g_test_init() is a variadic function, so we cannot pass it * (variadic) arguments. If you need to pass additional parameters, * call nmtst_init() with argc==NULL and call g_test_init() yourself. */ @@ -497,6 +517,8 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ s_tests = NULL; } } + + NM_PRAGMA_WARNING_REENABLE } if (test_quick_set) @@ -529,13 +551,8 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ *out_set_logging = TRUE; #endif g_assert (success); -#if GLIB_CHECK_VERSION(2,34,0) if (__nmtst_internal.no_expect_message) g_log_set_always_fatal (G_LOG_FATAL_MASK); -#else - /* g_test_expect_message() is a NOP, so allow any messages */ - g_log_set_always_fatal (G_LOG_FATAL_MASK); -#endif } else if (__nmtst_internal.no_expect_message) { /* We have a test that would be assert_logging, but the user specified no_expect_message. * This transforms g_test_expect_message() into a NOP, but we also have to relax @@ -555,14 +572,9 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ } #endif } else { -#if GLIB_CHECK_VERSION(2,34,0) /* We were called not to set logging levels. This means, that the user * expects to assert against (all) messages. Any uncought message is fatal. */ g_log_set_always_fatal (G_LOG_LEVEL_MASK); -#else - /* g_test_expect_message() is a NOP, so allow any messages */ - g_log_set_always_fatal (G_LOG_FATAL_MASK); -#endif } if ((!__nmtst_internal.assert_logging || (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message)) && @@ -629,7 +641,6 @@ nmtst_test_quick (void) return __nmtst_internal.test_quick; } -#if GLIB_CHECK_VERSION(2,34,0) #undef g_test_expect_message #define g_test_expect_message(...) \ G_STMT_START { \ @@ -637,9 +648,7 @@ nmtst_test_quick (void) if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) { \ g_debug ("nmtst: assert-logging: g_test_expect_message %s", G_STRINGIFY ((__VA_ARGS__))); \ } else { \ - G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ g_test_expect_message (__VA_ARGS__); \ - G_GNUC_END_IGNORE_DEPRECATIONS \ } \ } G_STMT_END #undef g_test_assert_expected_messages_internal @@ -653,10 +662,21 @@ nmtst_test_quick (void) if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) \ g_debug ("nmtst: assert-logging: g_test_assert_expected_messages(%s, %s:%d, %s)", _domain?:"", _file?:"", _line, _func?:""); \ \ - G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ g_test_assert_expected_messages_internal (_domain, _file, _line, _func); \ - G_GNUC_END_IGNORE_DEPRECATIONS \ } G_STMT_END + +#define NMTST_EXPECT(domain, level, msg) g_test_expect_message (domain, level, msg) + +#if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_LIBNM_UTIL +#define NMTST_EXPECT_LIBNM_U(level, msg) NMTST_EXPECT ("libnm-util", level, msg) +#define NMTST_EXPECT_LIBNM_G(level, msg) NMTST_EXPECT ("libnm-glib", level, msg) + +#define NMTST_EXPECT_LIBNM_U_CRITICAL(msg) NMTST_EXPECT_LIBNM_U (G_LOG_LEVEL_CRITICAL, msg) +#define NMTST_EXPECT_LIBNM_G_CRITICAL(msg) NMTST_EXPECT_LIBNM_G (G_LOG_LEVEL_CRITICAL, msg) +#else +#define NMTST_EXPECT_LIBNM(level, msg) NMTST_EXPECT ("libnm", level, msg) + +#define NMTST_EXPECT_LIBNM_CRITICAL(msg) NMTST_EXPECT_LIBNM (G_LOG_LEVEL_CRITICAL, msg) #endif /*****************************************************************************/ @@ -771,9 +791,20 @@ nmtst_get_rand (void) if (G_UNLIKELY (!__nmtst_internal.rand)) { guint32 seed; - const char *str; + const char *str = g_getenv ("NMTST_SEED_RAND"); - if ((str = g_getenv ("NMTST_SEED_RAND"))) { + if (!str) { + /* No NMTST_SEED_RAND. Pick a stable one. */ + seed = 0; + __nmtst_internal.rand = g_rand_new_with_seed (seed); + } else if (str[0] == '\0') { + /* NMTST_SEED_RAND is set but empty. Pick a random one. */ + __nmtst_internal.rand = g_rand_new (); + + seed = g_rand_int (__nmtst_internal.rand); + g_rand_set_seed (__nmtst_internal.rand, seed); + } else { + /* NMTST_SEED_RAND is set. Use it as a seed. */ gchar *s; gint64 i; @@ -782,11 +813,6 @@ nmtst_get_rand (void) seed = i; __nmtst_internal.rand = g_rand_new_with_seed (seed); - } else { - __nmtst_internal.rand = g_rand_new (); - - seed = g_rand_int (__nmtst_internal.rand); - g_rand_set_seed (__nmtst_internal.rand, seed); } __nmtst_internal.rand_seed = seed; @@ -801,6 +827,12 @@ nmtst_get_rand_int (void) return g_rand_int (nmtst_get_rand ()); } +static inline gboolean +nmtst_get_rand_bool (void) +{ + return nmtst_get_rand_int () % 2; +} + static inline gpointer nmtst_rand_buf (GRand *rand, gpointer buffer, gsize buffer_length) { @@ -902,32 +934,28 @@ _nmtst_main_loop_run_timeout (gpointer user_data) { GMainLoop **p_loop = user_data; - g_assert (p_loop); - g_assert (*p_loop); - - g_main_loop_quit (*p_loop); - *p_loop = NULL; - + g_assert (p_loop && *p_loop); + g_main_loop_quit (g_steal_pointer (p_loop)); return G_SOURCE_REMOVE; } static inline gboolean -nmtst_main_loop_run (GMainLoop *loop, int timeout_ms) +nmtst_main_loop_run (GMainLoop *loop, guint timeout_ms) { - GSource *source = NULL; - guint id = 0; + nm_auto_unref_gsource GSource *source = NULL; GMainLoop *loopx = loop; if (timeout_ms > 0) { source = g_timeout_source_new (timeout_ms); g_source_set_callback (source, _nmtst_main_loop_run_timeout, &loopx, NULL); - id = g_source_attach (source, g_main_loop_get_context (loop)); - g_assert (id); - g_source_unref (source); + g_source_attach (source, g_main_loop_get_context (loop)); } g_main_loop_run (loop); + if (source) + g_source_destroy (source); + /* if the timeout was reached, return FALSE. */ return loopx != NULL; } @@ -1116,7 +1144,7 @@ _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const cha char buf[100]; g_error ("%s:%d: Unexpected IPv4 address: expected %s, got %s", - file, line, str_expected ? str_expected : "0.0.0.0", + file, line, str_expected ?: "0.0.0.0", inet_ntop (AF_INET, &addr, buf, sizeof (buf))); } } @@ -1134,7 +1162,7 @@ _nmtst_assert_ip6_address (const char *file, int line, const struct in6_addr *ad char buf[100]; g_error ("%s:%d: Unexpected IPv6 address: expected %s, got %s", - file, line, str_expected ? str_expected : "::", + file, line, str_expected ?: "::", inet_ntop (AF_INET6, addr, buf, sizeof (buf))); } } @@ -1499,13 +1527,12 @@ _nmtst_connection_normalize (NMConnection *connection, ...) static inline NMConnection * _nmtst_connection_duplicate_and_normalize (NMConnection *connection, ...) { - gboolean was_modified; va_list args; connection = nmtst_clone_connection (connection); va_start (args, connection); - was_modified = _nmtst_connection_normalize_v (connection, args); + _nmtst_connection_normalize_v (connection, args); va_end (args); return connection; @@ -1697,7 +1724,7 @@ nmtst_assert_setting_verifies (NMSetting *setting) g_assert (success); } -#if defined(__NM_SIMPLE_CONNECTION_H__) +#if defined(__NM_SIMPLE_CONNECTION_H__) && NM_CHECK_VERSION (1, 10, 0) && (!defined (NM_VERSION_MAX_ALLOWED) || NM_VERSION_MAX_ALLOWED >= NM_VERSION_1_10) static inline void _nmtst_assert_connection_has_settings (NMConnection *connection, gboolean has_at_least, gboolean has_at_most, ...) { @@ -1715,7 +1742,7 @@ _nmtst_assert_connection_has_settings (NMConnection *connection, gboolean has_at va_start (ap, has_at_most); while ((name = va_arg (ap, const char *))) { - if (!nm_g_hash_table_add (names, (gpointer) name)) + if (!g_hash_table_add (names, (gpointer) name)) g_assert_not_reached (); g_ptr_array_add (names_arr, (gpointer) name); } @@ -1751,8 +1778,7 @@ _nmtst_assert_connection_has_settings (NMConnection *connection, gboolean has_at #define nmtst_assert_connection_has_settings(connection, ...) _nmtst_assert_connection_has_settings ((connection), TRUE, TRUE, __VA_ARGS__, NULL) #define nmtst_assert_connection_has_settings_at_least(connection, ...) _nmtst_assert_connection_has_settings ((connection), TRUE, FALSE, __VA_ARGS__, NULL) #define nmtst_assert_connection_has_settings_at_most(connection, ...) _nmtst_assert_connection_has_settings ((connection), FALSE, TRUE, __VA_ARGS__, NULL) - -#endif /* __NM_SIMPLE_CONNECTION_H__ */ +#endif static inline void nmtst_assert_setting_verify_fails (NMSetting *setting, @@ -1809,7 +1835,6 @@ nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const cha nmtst_assert_hwaddr_equals (hwaddr1, hwaddr1_len, expected, __FILE__, __LINE__) #endif - #if defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__) && defined(__NM_KEYFILE_INTERNAL_H__) static inline NMConnection * |