summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2018-01-12 11:59:22 +0100
committerKarel Zak2018-01-12 11:59:22 +0100
commitbc9ca775a2027a4b587a0fddffb18068e2803199 (patch)
treead67c5aad997c62b17eeeb51ba77e938caa39464
parentinclude/debug: don't print pointer address for SUID programs (diff)
parenttests: mark mbsencode as KNOWN_FAIL (diff)
downloadkernel-qcow2-util-linux-bc9ca775a2027a4b587a0fddffb18068e2803199.tar.gz
kernel-qcow2-util-linux-bc9ca775a2027a4b587a0fddffb18068e2803199.tar.xz
kernel-qcow2-util-linux-bc9ca775a2027a4b587a0fddffb18068e2803199.zip
Merge branch 'mbsencode' of https://github.com/yontalcar/util-linux
* 'mbsencode' of https://github.com/yontalcar/util-linux: tests: mark mbsencode as KNOWN_FAIL tests: mbsencode - test for HAVE_WIDECHAR lib/mbsalign: Fix escaping nonprintable multibyte characters tests: mbsencode - removed emoji, added control unicode character tests: add tests for encode functions from lib/mbsalign.c lib/mbsalign: escape "\x" when HAVE_WIDECHAR not defined
-rw-r--r--lib/mbsalign.c9
-rw-r--r--tests/commands.sh1
-rw-r--r--tests/expected/misc/mbsencode-invalid-ascii10
-rw-r--r--tests/expected/misc/mbsencode-invalid-utf810
-rw-r--r--tests/expected/misc/mbsencode-safe-ascii10
-rw-r--r--tests/expected/misc/mbsencode-safe-utf810
-rw-r--r--tests/helpers/Makemodule.am3
-rw-r--r--tests/helpers/test_mbsencode.c52
-rwxr-xr-xtests/ts/misc/mbsencode83
9 files changed, 183 insertions, 5 deletions
diff --git a/lib/mbsalign.c b/lib/mbsalign.c
index 07fc313b8..8fdab9ee9 100644
--- a/lib/mbsalign.c
+++ b/lib/mbsalign.c
@@ -168,7 +168,7 @@ char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf, const c
} else if (!iswprint(wc)) {
size_t i;
for (i = 0; i < len; i++) {
- sprintf(r, "\\x%02x", (unsigned char) *p);
+ sprintf(r, "\\x%02x", (unsigned char) p[i]);
r += 4;
*width += 4;
}
@@ -223,6 +223,9 @@ char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf)
#ifdef HAVE_WIDECHAR
wchar_t wc;
size_t len = mbrtowc(&wc, p, MB_CUR_MAX, &st);
+#else
+ size_t len = 1;
+#endif
if (len == 0)
break; /* end of string */
@@ -251,10 +254,6 @@ char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf)
*width += wcwidth(wc);
}
p += len;
-#else
- *r++ = *p++;
- (*width)++;
-#endif
}
*r = '\0';
diff --git a/tests/commands.sh b/tests/commands.sh
index 502935ede..985064446 100644
--- a/tests/commands.sh
+++ b/tests/commands.sh
@@ -38,6 +38,7 @@ TS_HELPER_SYSINFO="$top_builddir/test_sysinfo"
TS_HELPER_TIOCSTI="$top_builddir/test_tiocsti"
TS_HELPER_UUID_PARSER="$top_builddir/test_uuid_parser"
TS_HELPER_UUID_NAMESPACE="$top_builddir/test_uuid_namespace"
+TS_HELPER_MBSENCODE="$top_builddir/test_mbsencode"
# paths to commands
TS_CMD_ADDPART=${TS_CMD_ADDPART:-"$top_builddir/addpart"}
diff --git a/tests/expected/misc/mbsencode-invalid-ascii b/tests/expected/misc/mbsencode-invalid-ascii
new file mode 100644
index 000000000..4e759e1e0
--- /dev/null
+++ b/tests/expected/misc/mbsencode-invalid-ascii
@@ -0,0 +1,10 @@
+9 foo bar baz
+15 \\foo.local\bar
+19 \\foo.local\x5cxbar
+11 \xc3\xbcber
+21 c\xcc\x8ca\xcc\x81rka
+56 \xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0\xcc\x81
+24 \xe5\x8c\x97\xe4\xba\xac
+8 \xc2\x83
+4 \xff
+16 \xe8\xe1\xf9\xa7
diff --git a/tests/expected/misc/mbsencode-invalid-utf8 b/tests/expected/misc/mbsencode-invalid-utf8
new file mode 100644
index 000000000..b6b51e651
--- /dev/null
+++ b/tests/expected/misc/mbsencode-invalid-utf8
@@ -0,0 +1,10 @@
+9 foo bar baz
+15 \\foo.local\bar
+19 \\foo.local\x5cxbar
+4 über
+5 čárka
+6 Москва́
+4 北京
+-1 ƒ
+4 \xff
+16 \xe8\xe1\xf9\xa7
diff --git a/tests/expected/misc/mbsencode-safe-ascii b/tests/expected/misc/mbsencode-safe-ascii
new file mode 100644
index 000000000..a7473d703
--- /dev/null
+++ b/tests/expected/misc/mbsencode-safe-ascii
@@ -0,0 +1,10 @@
+14 foo\x09bar baz
+15 \\foo.local\bar
+19 \\foo.local\x5cxbar
+11 \xc3\xbcber
+21 c\xcc\x8ca\xcc\x81rka
+56 \xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0\xcc\x81
+24 \xe5\x8c\x97\xe4\xba\xac
+8 \xc2\x83
+4 \xff
+16 \xe8\xe1\xf9\xa7
diff --git a/tests/expected/misc/mbsencode-safe-utf8 b/tests/expected/misc/mbsencode-safe-utf8
new file mode 100644
index 000000000..63a417c17
--- /dev/null
+++ b/tests/expected/misc/mbsencode-safe-utf8
@@ -0,0 +1,10 @@
+14 foo\x09bar baz
+15 \\foo.local\bar
+19 \\foo.local\x5cxbar
+4 über
+5 čárka
+6 Москва́
+4 北京
+8 \xc2\x83
+4 \xff
+16 \xe8\xe1\xf9\xa7
diff --git a/tests/helpers/Makemodule.am b/tests/helpers/Makemodule.am
index 2f2611b67..ab0b3cea9 100644
--- a/tests/helpers/Makemodule.am
+++ b/tests/helpers/Makemodule.am
@@ -1,3 +1,6 @@
+check_PROGRAMS += test_mbsencode
+test_mbsencode_SOURCES = tests/helpers/test_mbsencode.c
+test_mbsencode_LDADD = $(LDADD) libcommon.la
check_PROGRAMS += test_byteswap
test_byteswap_SOURCES = tests/helpers/test_byteswap.c
diff --git a/tests/helpers/test_mbsencode.c b/tests/helpers/test_mbsencode.c
new file mode 100644
index 000000000..a8f93836c
--- /dev/null
+++ b/tests/helpers/test_mbsencode.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018 Vaclav Dolezal <vdolezal@redhat.com>
+ *
+ * This file is part of util-linux.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <locale.h>
+
+#include "mbsalign.h"
+
+int main(int argc, char **argv)
+{
+ int i = 1;
+ char *(*encode_fn)(const char *, size_t *) = mbs_safe_encode;
+
+ setlocale(LC_ALL, "");
+
+ if (i < argc) {
+ if (!strcmp(argv[i], "--safe")) {
+ i++;
+ encode_fn = mbs_safe_encode;
+ } else if (!strcmp(argv[i], "--invalid")) {
+ i++;
+ encode_fn = mbs_invalid_encode;
+ } else if (!strcmp(argv[i], "--")) {
+ i++;
+ }
+ }
+
+ for (; i < argc; i++) {
+ size_t width;
+ char *res;
+ res = encode_fn(argv[i], &width);
+ printf("%zi %s\n", width, res);
+ free(res);
+ }
+
+ return 0;
+}
diff --git a/tests/ts/misc/mbsencode b/tests/ts/misc/mbsencode
new file mode 100755
index 000000000..405d34c56
--- /dev/null
+++ b/tests/ts/misc/mbsencode
@@ -0,0 +1,83 @@
+#!/bin/bash
+
+#
+# Copyright (C) 2018 Vaclav Dolezal <vdolezal@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="mbsencode"
+
+. $TS_TOPDIR/functions.sh
+ts_init "$*"
+
+# These test may fail on some machines (locales, other libc...)
+TS_KNOWN_FAIL="yes"
+
+STRINGS=(
+# ASCII
+ $'foo\tbar baz'
+ '\\foo.local\bar'
+ '\\foo.local\xbar'
+
+# UNICODE
+ 'über'
+ $'c\xcc\x8ca\xcc\x81rka' # 'c\u030Ca\u0301rka'
+ 'Москва́'
+ '北京'
+ $'\xc2\x83' # U+0083
+
+# INVALID UNICODE
+ $'\xff'
+ $'\xe8\xe1\xf9\xa7'
+)
+
+if grep -q '^#define HAVE_WIDECHAR' ${top_builddir}/config.h ;then
+ HAVE_WIDECHAR=true
+else
+ HAVE_WIDECHAR=false
+fi
+
+ts_init_subtest "safe-ascii"
+$TS_HELPER_MBSENCODE --safe "${STRINGS[@]}" >> $TS_OUTPUT 2>&1
+ts_finalize_subtest
+
+ts_init_subtest "invalid-ascii"
+if [ "$HAVE_WIDECHAR" = true ]; then
+ $TS_HELPER_MBSENCODE --invalid "${STRINGS[@]}" >> $TS_OUTPUT 2>&1
+ ts_finalize_subtest
+else
+ ts_skip_subtest 'No widechar support'
+fi
+
+ts_init_subtest "safe-utf8"
+if [ "$HAVE_WIDECHAR" = true ]; then
+ LC_ALL=C.UTF-8 \
+ $TS_HELPER_MBSENCODE --safe "${STRINGS[@]}" >> $TS_OUTPUT 2>&1
+ ts_finalize_subtest
+else
+ ts_skip_subtest 'No widechar support'
+fi
+
+ts_init_subtest "invalid-utf8"
+if [ "$HAVE_WIDECHAR" = true ]; then
+ LC_ALL=C.UTF-8 \
+ $TS_HELPER_MBSENCODE --invalid "${STRINGS[@]}" >> $TS_OUTPUT 2>&1
+ ts_finalize_subtest
+else
+ ts_skip_subtest 'No widechar support'
+fi
+
+ts_finalize
+