diff options
Diffstat (limited to 'shared/nm-utils/nm-test-utils.h')
| -rw-r--r-- | shared/nm-utils/nm-test-utils.h | 81 |
1 files changed, 67 insertions, 14 deletions
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 62d608c6..6b41c11e 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -1,4 +1,4 @@ -/* SPDX-License-Identifier: LGPL-2.1+ */ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2014 Red Hat, Inc. */ @@ -6,10 +6,6 @@ #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. * @@ -84,8 +80,6 @@ * *******************************************************************************/ -#include "nm-default.h" - #if defined(NM_ASSERT_NO_MSG) && NM_ASSERT_NO_MSG #undef g_return_if_fail_warning #undef g_assertion_message_expr @@ -98,10 +92,6 @@ #include <string.h> #include <errno.h> -#ifndef NM_TEST_UTILS_NO_LIBNM - #include "nm-utils.h" -#endif - /*****************************************************************************/ #define NMTST_G_RETURN_MSG_S(expr) "*: assertion '" NM_ASSERT_G_RETURN_EXPR(expr) "' failed" @@ -900,14 +890,16 @@ nmtst_get_rand_uint64(void) static inline guint nmtst_get_rand_uint(void) { - G_STATIC_ASSERT_EXPR(sizeof(guint32) == sizeof(guint)); - return nmtst_get_rand_uint32(); + G_STATIC_ASSERT_EXPR((sizeof(guint) == sizeof(guint32) || (sizeof(guint) == sizeof(guint64)))); + if (sizeof(guint32) == sizeof(guint)) + return nmtst_get_rand_uint32(); + return nmtst_get_rand_uint64(); } static inline gsize nmtst_get_rand_size(void) { - G_STATIC_ASSERT_EXPR(sizeof(gsize) == sizeof(guint32) || sizeof(gsize) == sizeof(guint64)); + G_STATIC_ASSERT_EXPR((sizeof(gsize) == sizeof(guint32) || (sizeof(gsize) == sizeof(guint64)))); if (sizeof(gsize) == sizeof(guint32)) return nmtst_get_rand_uint32(); return nmtst_get_rand_uint64(); @@ -919,6 +911,17 @@ nmtst_get_rand_bool(void) return nmtst_get_rand_uint32() % 2; } +static inline gboolean +nmtst_get_rand_one_case_in(guint32 num) +{ + /* num=1 doesn't make much sense, because it will always return %TRUE. + * Still accept it, it might be that @num is calculated, so 1 might be + * a valid edge case. */ + g_assert(num > 0); + + return (nmtst_get_rand_uint32() % num) == 0; +} + static inline gpointer nmtst_rand_buf(GRand *rand, gpointer buffer, gsize buffer_length) { @@ -998,6 +1001,23 @@ nmtst_rand_perm(GRand *rand, void *dst, const void *src, gsize elmt_size, gsize return dst; } +static inline const char ** +nmtst_rand_perm_strv(const char *const *strv) +{ + const char **res; + gsize n; + + if (!strv) + return NULL; + + /* this returns a (scrambled) SHALLOW copy of the strv array! */ + + n = NM_PTRARRAY_LEN(strv); + res = (const char **) (nm_utils_strv_dup(strv, n, FALSE) ?: g_new0(char *, 1)); + nmtst_rand_perm(NULL, res, res, sizeof(char *), n); + return res; +} + static inline GSList * nmtst_rand_perm_gslist(GRand *rand, GSList *list) { @@ -1181,6 +1201,14 @@ nmtst_main_loop_run(GMainLoop *loop, guint timeout_msec) return loopx != NULL; } +#define nmtst_main_loop_run_assert(loop, timeout_msec) \ + G_STMT_START \ + { \ + if (!nmtst_main_loop_run((loop), (timeout_msec))) \ + g_assert_not_reached(); \ + } \ + G_STMT_END + static inline void _nmtst_main_loop_quit_on_notify(GObject *object, GParamSpec *pspec, gpointer user_data) { @@ -2751,4 +2779,29 @@ nmtst_keyfile_get_num_keys(GKeyFile *keyfile, const char *group_name) /*****************************************************************************/ +#if defined(NM_SETTING_IP_CONFIG_H) && defined(__NM_SHARED_UTILS_H__) + +static inline NMIPAddress * +nmtst_ip_address_new(int addr_family, const char *str) +{ + NMIPAddr addr; + int plen; + GError * error = NULL; + NMIPAddress *a; + + if (!nm_utils_parse_inaddr_prefix_bin(addr_family, str, &addr_family, &addr, &plen)) + g_assert_not_reached(); + + if (plen == -1) + plen = addr_family == AF_INET ? 32 : 128; + + a = nm_ip_address_new_binary(addr_family, &addr, plen, &error); + nmtst_assert_success(a, error); + return a; +} + +#endif + +/*****************************************************************************/ + #endif /* __NM_TEST_UTILS_H__ */ |