mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-29 02:10:09 +01:00
libnm/tests: add test for broken behavior of nm_utils_bin_utf8safe_escape()
(cherry picked from commit 5f54270d93)
This commit is contained in:
parent
5ef24e8d0b
commit
77c2ff8922
1 changed files with 35 additions and 5 deletions
|
|
@ -9137,6 +9137,12 @@ _do_test_utils_str_utf8safe(const char * str,
|
|||
((nmtst_get_rand_bool()) ? NM_UTILS_STR_UTF8_SAFE_FLAG_NONE \
|
||||
: NM_UTILS_STR_UTF8_SAFE_FLAG_SECRET)
|
||||
|
||||
if (expected && strlen(expected) == str_len && memcmp(str, expected, str_len) == 0) {
|
||||
g_error("Test error: pass expected as NULL (instead of \"%s\", if the escaping will "
|
||||
"produce no difference.",
|
||||
expected);
|
||||
}
|
||||
|
||||
buf_safe = nm_utils_buf_utf8safe_escape(str, str_len, flags | RND_FLAG, &str_free_1);
|
||||
|
||||
str_safe = nm_utils_str_utf8safe_escape(str, flags | RND_FLAG, &str_free_2);
|
||||
|
|
@ -9195,11 +9201,16 @@ _do_test_utils_str_utf8safe(const char * str,
|
|||
g_assert(str);
|
||||
g_assert(str_safe != str);
|
||||
g_assert(str_safe == str_free_2);
|
||||
g_assert(strchr(str, '\\') || !g_utf8_validate(str, -1, NULL)
|
||||
|| (NM_FLAGS_HAS(flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_NON_ASCII)
|
||||
&& NM_STRCHAR_ANY(str, ch, (guchar) ch >= 127))
|
||||
|| (NM_FLAGS_HAS(flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)
|
||||
&& NM_STRCHAR_ANY(str, ch, (guchar) ch < ' ')));
|
||||
if (nm_streq(str, "ab∞c")) {
|
||||
/* Hack to pass test for broken behavior. */
|
||||
g_assert(((char) -1) < 0);
|
||||
} else {
|
||||
g_assert(strchr(str, '\\') || !g_utf8_validate(str, -1, NULL)
|
||||
|| (NM_FLAGS_HAS(flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_NON_ASCII)
|
||||
&& NM_STRCHAR_ANY(str, ch, (guchar) ch >= 127))
|
||||
|| (NM_FLAGS_HAS(flags, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)
|
||||
&& NM_STRCHAR_ANY(str, ch, (guchar) ch < ' ')));
|
||||
}
|
||||
g_assert(g_utf8_validate(str_safe, -1, NULL));
|
||||
|
||||
str_free_6 = g_strcompress(str_safe);
|
||||
|
|
@ -9273,6 +9284,25 @@ test_utils_str_utf8safe(void)
|
|||
do_test_utils_str_utf8safe_unescape("\n\\012", "\n\012");
|
||||
do_test_utils_str_utf8safe_unescape("\n\\.", "\n.");
|
||||
do_test_utils_str_utf8safe_unescape("\\n\\.3\\r", "\n.3\r");
|
||||
|
||||
if (((char) -1) < 0) {
|
||||
/* Test buggy behavior on systems with signed "char". Will be fixed next. */
|
||||
do_test_utils_str_utf8safe("ab∞c",
|
||||
"ab\\342\\210\\236c",
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL);
|
||||
do_test_utils_str_utf8safe("ab\ab∞c",
|
||||
"ab\\007b\\342\\210\\236c",
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL);
|
||||
} else {
|
||||
do_test_utils_str_utf8safe("ab∞c", NULL, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL);
|
||||
do_test_utils_str_utf8safe("ab\ab∞c",
|
||||
"ab\\007b∞c",
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL);
|
||||
}
|
||||
do_test_utils_str_utf8safe("ab\ab∞c",
|
||||
"ab\\007b\\342\\210\\236c",
|
||||
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL
|
||||
| NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_NON_ASCII);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue