diff options
Diffstat (limited to 'shared/nm-utils')
| -rw-r--r-- | shared/nm-utils/nm-compat.c | 1 | ||||
| -rw-r--r-- | shared/nm-utils/nm-compat.h | 1 | ||||
| -rw-r--r-- | shared/nm-utils/nm-test-utils.h | 104 | ||||
| -rw-r--r-- | shared/nm-utils/nm-vpn-editor-plugin-call.h | 1 | ||||
| -rw-r--r-- | shared/nm-utils/nm-vpn-plugin-macros.h | 2 | ||||
| -rw-r--r-- | shared/nm-utils/nm-vpn-plugin-utils.c | 2 | ||||
| -rw-r--r-- | shared/nm-utils/nm-vpn-plugin-utils.h | 2 | ||||
| -rw-r--r-- | shared/nm-utils/tests/test-shared-general.c | 105 |
8 files changed, 178 insertions, 40 deletions
diff --git a/shared/nm-utils/nm-compat.c b/shared/nm-utils/nm-compat.c index aa7c42f1..ea3e5392 100644 --- a/shared/nm-utils/nm-compat.c +++ b/shared/nm-utils/nm-compat.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This library is free software; you can redistribute it and/or diff --git a/shared/nm-utils/nm-compat.h b/shared/nm-utils/nm-compat.h index 52341690..a8e3ee97 100644 --- a/shared/nm-utils/nm-compat.h +++ b/shared/nm-utils/nm-compat.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This library is free software; you can redistribute it and/or diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index d0ec7d9f..b2d103f1 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -710,18 +709,10 @@ nmtst_test_quick (void) #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_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 /*****************************************************************************/ @@ -866,15 +857,22 @@ nmtst_get_rand (void) } static inline guint32 -nmtst_get_rand_int (void) +nmtst_get_rand_uint32 (void) { return g_rand_int (nmtst_get_rand ()); } +static inline guint +nmtst_get_rand_uint (void) +{ + G_STATIC_ASSERT_EXPR (sizeof (guint32) == sizeof (guint)); + return nmtst_get_rand_uint32 (); +} + static inline gboolean nmtst_get_rand_bool (void) { - return nmtst_get_rand_int () % 2; + return nmtst_get_rand_uint32 () % 2; } static inline gpointer @@ -909,7 +907,7 @@ nmtst_rand_buf (GRand *rand, gpointer buffer, gsize buffer_length) ({ \ 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))]; \ + NM_UNIQ_T (UNIQ, uniq)[nmtst_get_rand_uint32 () % G_N_ELEMENTS (NM_UNIQ_T (UNIQ, uniq))]; \ }) #define nmtst_rand_select(...) \ @@ -1935,6 +1933,69 @@ nmtst_assert_setting_verify_fails (NMSetting *setting, g_clear_error (&error); } +static inline void +nmtst_assert_setting_is_equal (gconstpointer /* const NMSetting * */ a, + gconstpointer /* const NMSetting * */ b, + NMSettingCompareFlags flags) +{ + gs_unref_hashtable GHashTable *hash = NULL; + guint32 r = nmtst_get_rand_uint32 (); + + g_assert (NM_IS_SETTING (a)); + g_assert (NM_IS_SETTING (b)); + + if (NM_FLAGS_HAS (r, 0x4)) + NMTST_SWAP (a, b); + + g_assert (nm_setting_compare ((NMSetting *) a, + (NMSetting *) b, + flags)); + + if (NM_FLAGS_HAS (r, 0x8)) + NMTST_SWAP (a, b); + + g_assert (nm_setting_diff ((NMSetting *) a, + (NMSetting *) b, + flags, + NM_FLAGS_HAS (r, 0x1), + &hash)); + g_assert (!hash); +} +#endif + +#ifdef __NM_SETTING_PRIVATE_H__ +static inline NMSetting * +nmtst_assert_setting_dbus_new (GType gtype, GVariant *variant) +{ + NMSetting *setting; + gs_free_error GError *error = NULL; + + g_assert (g_type_is_a (gtype, NM_TYPE_SETTING)); + g_assert (gtype != NM_TYPE_SETTING); + g_assert (variant); + g_assert (g_variant_is_of_type (variant, NM_VARIANT_TYPE_SETTING)); + + setting = _nm_setting_new_from_dbus (gtype, + variant, + NULL, + NM_SETTING_PARSE_FLAGS_STRICT, + &error); + nmtst_assert_success (setting, error); + return setting; +} + +static inline void +nmtst_assert_setting_dbus_roundtrip (gconstpointer /* const NMSetting * */ setting) +{ + gs_unref_object NMSetting *setting2 = NULL; + gs_unref_variant GVariant *variant = NULL; + + g_assert (NM_IS_SETTING (setting)); + + variant = _nm_setting_to_dbus ((NMSetting *) setting, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); + setting2 = nmtst_assert_setting_dbus_new (G_OBJECT_TYPE (setting), variant); + nmtst_assert_setting_is_equal (setting, setting2, NM_SETTING_COMPARE_FLAG_EXACT); +} #endif #ifdef __NM_UTILS_H__ @@ -2148,6 +2209,25 @@ typedef enum { #endif /* __NM_CONNECTION_H__ */ +static inline GVariant * +nmtst_variant_from_string (const GVariantType *variant_type, + const char *variant_str) +{ + GVariant *variant; + GError *error = NULL; + + g_assert (variant_type); + g_assert (variant_str); + + variant = g_variant_parse (variant_type, + variant_str, + NULL, + NULL, + &error); + nmtst_assert_success (variant, error); + return variant; +} + /*****************************************************************************/ static inline void diff --git a/shared/nm-utils/nm-vpn-editor-plugin-call.h b/shared/nm-utils/nm-vpn-editor-plugin-call.h index fd982acf..ea5e27b0 100644 --- a/shared/nm-utils/nm-vpn-editor-plugin-call.h +++ b/shared/nm-utils/nm-vpn-editor-plugin-call.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* NetworkManager -- Network link manager * * This program is free software; you can redistribute it and/or modify diff --git a/shared/nm-utils/nm-vpn-plugin-macros.h b/shared/nm-utils/nm-vpn-plugin-macros.h index acc549f2..97260f5f 100644 --- a/shared/nm-utils/nm-vpn-plugin-macros.h +++ b/shared/nm-utils/nm-vpn-plugin-macros.h @@ -1,5 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/shared/nm-utils/nm-vpn-plugin-utils.c b/shared/nm-utils/nm-vpn-plugin-utils.c index 353a2817..afc05bdf 100644 --- a/shared/nm-utils/nm-vpn-plugin-utils.c +++ b/shared/nm-utils/nm-vpn-plugin-utils.c @@ -1,5 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/shared/nm-utils/nm-vpn-plugin-utils.h b/shared/nm-utils/nm-vpn-plugin-utils.h index f3928d1e..961e0187 100644 --- a/shared/nm-utils/nm-vpn-plugin-utils.h +++ b/shared/nm-utils/nm-vpn-plugin-utils.h @@ -1,5 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/shared/nm-utils/tests/test-shared-general.c b/shared/nm-utils/tests/test-shared-general.c index 83cffd7f..24affba7 100644 --- a/shared/nm-utils/tests/test-shared-general.c +++ b/shared/nm-utils/tests/test-shared-general.c @@ -29,24 +29,10 @@ /*****************************************************************************/ -static int _monotonic_timestamp_initialized; - -void -_nm_utils_monotonic_timestamp_initialized (const struct timespec *tp, - gint64 offset_sec, - gboolean is_boottime) -{ - g_assert (!_monotonic_timestamp_initialized); - _monotonic_timestamp_initialized = 1; -} - -/*****************************************************************************/ - static void test_monotonic_timestamp (void) { g_assert (nm_utils_get_monotonic_timestamp_s () > 0); - g_assert (_monotonic_timestamp_initialized); } /*****************************************************************************/ @@ -163,10 +149,10 @@ test_nm_strndup_a (void) char ch; gsize i, l; - input = g_strnfill (nmtst_get_rand_int () % 20, 'x'); + input = g_strnfill (nmtst_get_rand_uint32 () % 20, 'x'); for (i = 0; input[i]; i++) { - while ((ch = ((char) nmtst_get_rand_int ())) == '\0') { + while ((ch = ((char) nmtst_get_rand_uint32 ())) == '\0') { /* repeat. */ } input[i] = ch; @@ -189,7 +175,7 @@ test_nm_strndup_a (void) gs_free char *dup_free = NULL; const char *dup; - l = nmtst_get_rand_int () % 23; + l = nmtst_get_rand_uint32 () % 23; dup = nm_strndup_a (10, input, l, &dup_free); g_assert (strncmp (dup, input, l) == 0); g_assert (strlen (dup) <= l); @@ -228,7 +214,7 @@ test_unaligned (void) guint8 val = 0; while (val == 0) - val = nmtst_get_rand_int () % 256; + val = nmtst_get_rand_uint32 () % 256; buf[shift] = val; @@ -441,6 +427,87 @@ test_strstrip_avoid_copy (void) _do_strstrip_avoid_copy (" 01234567890 "); _do_strstrip_avoid_copy (" 012345678901 "); } + +/*****************************************************************************/ + +static void +test_nm_utils_bin2hexstr (void) +{ + int n_run; + + for (n_run = 0; n_run < 100; n_run++) { + guint8 buf[100]; + guint8 buf2[G_N_ELEMENTS (buf) + 1]; + gsize len = nmtst_get_rand_uint32 () % (G_N_ELEMENTS (buf) + 1); + char strbuf1[G_N_ELEMENTS (buf) * 3]; + gboolean allocate = nmtst_get_rand_bool (); + char delimiter = nmtst_get_rand_bool () ? ':' : '\0'; + gboolean upper_case = nmtst_get_rand_bool (); + gsize expected_strlen; + char *str_hex; + gsize required_len; + gboolean outlen_set; + gsize outlen; + guint8 *bin2; + + nmtst_rand_buf (NULL, buf, len); + + if (len == 0) + expected_strlen = 0; + else if (delimiter != '\0') + expected_strlen = (len * 3u) - 1; + else + expected_strlen = len * 2u; + + g_assert_cmpint (expected_strlen, <, G_N_ELEMENTS (strbuf1)); + + str_hex = nm_utils_bin2hexstr_full (buf, len, delimiter, upper_case, !allocate ? strbuf1 : NULL); + + g_assert (str_hex); + if (!allocate) + g_assert (str_hex == strbuf1); + g_assert_cmpint (strlen (str_hex), ==, expected_strlen); + + g_assert (NM_STRCHAR_ALL (str_hex, ch, (ch >= '0' && ch <= '9') + || ch == delimiter + || ( upper_case + ? (ch >= 'A' && ch <= 'F') + : (ch >= 'a' && ch <= 'f')))); + + required_len = nmtst_get_rand_bool () ? len : 0u; + + outlen_set = required_len == 0 || nmtst_get_rand_bool (); + + memset (buf2, 0, sizeof (buf2)); + + bin2 = nm_utils_hexstr2bin_full (str_hex, + nmtst_get_rand_bool (), + delimiter != '\0' && nmtst_get_rand_bool (), + delimiter != '\0' + ? nmtst_rand_select ((const char *) ":", ":-") + : nmtst_rand_select ((const char *) ":", ":-", "", NULL), + required_len, + buf2, + len, + outlen_set ? &outlen : NULL); + if (len > 0) { + g_assert (bin2); + g_assert (bin2 == buf2); + } else + g_assert (!bin2); + + if (outlen_set) + g_assert_cmpint (outlen, ==, len); + + g_assert_cmpmem (buf, len, buf2, len); + + g_assert (buf2[len] == '\0'); + + if (allocate) + g_free (str_hex); + } +} + /*****************************************************************************/ NMTST_DEFINE (); @@ -458,7 +525,7 @@ int main (int argc, char **argv) g_test_add_func ("/general/test_unaligned", test_unaligned); g_test_add_func ("/general/test_strv_cmp", test_strv_cmp); g_test_add_func ("/general/test_strstrip_avoid_copy", test_strstrip_avoid_copy); + g_test_add_func ("/general/test_nm_utils_bin2hexstr", test_nm_utils_bin2hexstr); return g_test_run (); } - |