diff options
| author | Michael Biebl <biebl@debian.org> | 2019-07-31 10:51:42 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2019-07-31 10:51:42 +0200 |
| commit | 2e5fa45ddfbb5cffa1e78221f1cea706e2f298af (patch) | |
| tree | 86f69d36c56de3074280456eddc854a780b8e04b /shared/nm-utils/tests/test-shared-general.c | |
| parent | 85563b7fc7ec2cd21e38debb9b28db342e2e8e7c (diff) | |
New upstream version 1.19.90 upstream/1.19.90
Diffstat (limited to 'shared/nm-utils/tests/test-shared-general.c')
| -rw-r--r-- | shared/nm-utils/tests/test-shared-general.c | 105 |
1 files changed, 86 insertions, 19 deletions
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 (); } - |