diff options
| author | Michael Biebl <biebl@debian.org> | 2019-03-26 23:25:23 +0100 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2019-03-26 23:25:23 +0100 |
| commit | 9a6dcbf895f9da01768e64b73cec88c16157d91e (patch) | |
| tree | a359958930d731e9f1b59344642e10754419fe84 /shared/nm-utils/nm-test-utils.h | |
| parent | 964ae8cc391520440cf5aa13e2b9cc34850ea6c2 (diff) | |
New upstream version 1.16.0 upstream/1.16.0
Diffstat (limited to 'shared/nm-utils/nm-test-utils.h')
| -rw-r--r-- | shared/nm-utils/nm-test-utils.h | 145 |
1 files changed, 134 insertions, 11 deletions
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 7decd363..c5ea5e3f 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -30,6 +30,9 @@ * * Our tests (make check) include this header-only file nm-test-utils.h. * + * You should always include this header *as last*. Reason is, that depending on + * previous includes, functionality will be enabled. + * * Logging: * In tests, nm-logging redirects to glib logging. By default, glib suppresses all debug * messages unless you set G_MESSAGES_DEBUG. To enable debug logging, you can explicitly set @@ -110,7 +113,9 @@ #include <string.h> #include <errno.h> +#ifndef NM_TEST_UTILS_NO_LIBNM #include "nm-utils.h" +#endif /*****************************************************************************/ @@ -191,6 +196,25 @@ /*****************************************************************************/ +/* Our nm-error error numbers use negative values to signal failure. + * A non-negative value signals success. Hence, the correct way for checking + * is always (r < 0) vs. (r >= 0). Never (r == 0). + * + * For assertions in tests, we also want to assert that no positive values + * are returned. For a lot of functions, positive return values are unexpected + * and a bug. This macro evaluates @r to success or failure, while asserting + * that @r is not positive. */ +#define NMTST_NM_ERR_SUCCESS(r) \ + ({ \ + const int _r = (r); \ + \ + if (_r >= 0) \ + g_assert_cmpint (_r, ==, 0); \ + (_r >= 0); \ + }) + +/*****************************************************************************/ + struct __nmtst_internal { GRand *rand0; @@ -695,6 +719,7 @@ nmtst_test_quick (void) #else #define NMTST_EXPECT_LIBNM(level, msg) NMTST_EXPECT ("libnm", level, msg) +#define NMTST_EXPECT_LIBNM_WARNING(msg) NMTST_EXPECT_LIBNM (G_LOG_LEVEL_WARNING, msg) #define NMTST_EXPECT_LIBNM_CRITICAL(msg) NMTST_EXPECT_LIBNM (G_LOG_LEVEL_CRITICAL, msg) #endif @@ -880,6 +905,16 @@ nmtst_rand_buf (GRand *rand, gpointer buffer, gsize buffer_length) return buffer; } +#define _nmtst_rand_select(uniq, v0, ...) \ + ({ \ + typeof (v0) NM_UNIQ_T (UNIQ, uniq)[1 + NM_NARG (__VA_ARGS__)] = { (v0), __VA_ARGS__ }; \ + \ + NM_UNIQ_T (UNIQ, uniq)[nmtst_get_rand_int () % G_N_ELEMENTS (NM_UNIQ_T (UNIQ, uniq))]; \ + }) + +#define nmtst_rand_select(...) \ + _nmtst_rand_select (NM_UNIQ, __VA_ARGS__) + static inline void * nmtst_rand_perm (GRand *rand, void *dst, const void *src, gsize elmt_size, gsize n_elmt) { @@ -1025,7 +1060,7 @@ nmtst_reexec_sudo (void) execvp (__nmtst_internal.sudo_cmd, argv); errsv = errno; - g_error (">> exec %s failed: %d - %s", __nmtst_internal.sudo_cmd, errsv, strerror (errsv)); + g_error (">> exec %s failed: %d - %s", __nmtst_internal.sudo_cmd, errsv, nm_strerror_native (errsv)); } /*****************************************************************************/ @@ -1089,6 +1124,8 @@ __define_nmtst_static(02, 1024) __define_nmtst_static(03, 1024) #undef __define_nmtst_static +#if defined (__NM_UTILS_H__) || defined (NM_UTILS_H) + #define NMTST_UUID_INIT(uuid) \ gs_free char *_nmtst_hidden_##uuid = nm_utils_uuid_generate (); \ const char *const uuid = _nmtst_hidden_##uuid @@ -1105,6 +1142,8 @@ nmtst_uuid_generate (void) return u; } +#endif + #define NMTST_SWAP(x,y) \ G_STMT_START { \ char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \ @@ -1156,6 +1195,48 @@ nmtst_inet6_from_string (const char *str) return &addr; } +static inline gconstpointer +nmtst_inet_from_string (int addr_family, const char *str) +{ + if (addr_family == AF_INET) { + static in_addr_t a; + + a = nmtst_inet4_from_string (str); + return &a; + } + if (addr_family == AF_INET6) + return nmtst_inet6_from_string (str); + + g_assert_not_reached (); + return NULL; +} + +static inline const char * +nmtst_inet_to_string (int addr_family, gconstpointer addr) +{ + static char buf[NM_CONST_MAX (INET6_ADDRSTRLEN, INET_ADDRSTRLEN)]; + + g_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6)); + g_assert (addr); + + if (inet_ntop (addr_family, addr, buf, sizeof (buf)) != buf) + g_assert_not_reached (); + + return buf; +} + +static inline const char * +nmtst_inet4_to_string (in_addr_t addr) +{ + return nmtst_inet_to_string (AF_INET, &addr); +} + +static inline const char * +nmtst_inet6_to_string (const struct in6_addr *addr) +{ + return nmtst_inet_to_string (AF_INET6, addr); +} + static inline void _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const char *str_expected) { @@ -1289,7 +1370,7 @@ nmtst_file_unlink_if_exists (const char *name) if (unlink (name) != 0) { errsv = errno; if (errsv != ENOENT) - g_error ("nmtst_file_unlink_if_exists(%s): failed with %s", name, strerror (errsv)); + g_error ("nmtst_file_unlink_if_exists(%s): failed with %s", name, nm_strerror_native (errsv)); } } @@ -1302,7 +1383,7 @@ nmtst_file_unlink (const char *name) if (unlink (name) != 0) { errsv = errno; - g_error ("nmtst_file_unlink(%s): failed with %s", name, strerror (errsv)); + g_error ("nmtst_file_unlink(%s): failed with %s", name, nm_strerror_native (errsv)); } } @@ -1895,8 +1976,8 @@ nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const cha static inline NMConnection * nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *full_filename) { - GKeyFile *keyfile; - GError *error = NULL; + gs_unref_keyfile GKeyFile *keyfile = NULL; + gs_free_error GError *error = NULL; gboolean success; NMConnection *con; gs_free char *filename = g_path_get_basename (full_filename); @@ -1907,14 +1988,10 @@ nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *full_ 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); + nmtst_assert_success (success, error); con = nm_keyfile_read (keyfile, base_dir, NULL, NULL, &error); - g_assert_no_error (error); - g_assert (NM_IS_CONNECTION (con)); - - g_key_file_unref (keyfile); + nmtst_assert_success (NM_IS_CONNECTION (con), error); nm_keyfile_read_ensure_id (con, filename); nm_keyfile_read_ensure_uuid (con, full_filename); @@ -2070,4 +2147,50 @@ typedef enum { #endif /* __NM_CONNECTION_H__ */ +/*****************************************************************************/ + +static inline void +nmtst_keyfile_assert_data (GKeyFile *kf, const char *data, gssize data_len) +{ + gs_unref_keyfile GKeyFile *kf2 = NULL; + gs_free_error GError *error = NULL; + gs_free char *d1 = NULL; + gs_free char *d2 = NULL; + gboolean success; + gsize d1_len; + gsize d2_len; + + g_assert (kf); + g_assert (data || data_len == 0); + g_assert (data_len >= -1); + + d1 = g_key_file_to_data (kf, &d1_len, &error); + nmtst_assert_success (d1, error); + + if (data_len == -1) { + g_assert_cmpint (strlen (d1), ==, d1_len); + data_len = strlen (data); + g_assert_cmpstr (d1, ==, data); + } + + g_assert_cmpmem (d1, d1_len, data, (gsize) data_len); + + /* also check that we can re-generate the same keyfile from the data. */ + + kf2 = g_key_file_new (); + success = g_key_file_load_from_data (kf2, + d1, + d1_len, + G_KEY_FILE_NONE, + &error); + nmtst_assert_success (success, error); + + d2 = g_key_file_to_data (kf2, &d2_len, &error); + nmtst_assert_success (d2, error); + + g_assert_cmpmem (d2, d2_len, d1, d1_len); +} + +/*****************************************************************************/ + #endif /* __NM_TEST_UTILS_H__ */ |