diff options
-rw-r--r-- | tests/commands.sh | 1 | ||||
-rw-r--r-- | tests/expected/misc/mbsencode-invalid-ascii | 10 | ||||
-rw-r--r-- | tests/expected/misc/mbsencode-invalid-utf8 | 10 | ||||
-rw-r--r-- | tests/expected/misc/mbsencode-safe-ascii | 10 | ||||
-rw-r--r-- | tests/expected/misc/mbsencode-safe-utf8 | 10 | ||||
-rw-r--r-- | tests/helpers/Makemodule.am | 3 | ||||
-rw-r--r-- | tests/helpers/test_mbsencode.c | 52 | ||||
-rwxr-xr-x | tests/ts/misc/mbsencode | 62 |
8 files changed, 158 insertions, 0 deletions
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..412790f7f --- /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 +16 \xf0\x9f\x90\xb1 +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..979f49c24 --- /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 北京 +2 🐱 +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..8ee2403bf --- /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 +16 \xf0\x9f\x90\xb1 +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..1154a2b02 --- /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 北京 +2 🐱 +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..7790ef08d --- /dev/null +++ b/tests/ts/misc/mbsencode @@ -0,0 +1,62 @@ +#!/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 "$*" + +STRINGS=( +# ASCII + $'foo\tbar baz' + '\\foo.local\bar' + '\\foo.local\xbar' + +# UNICODE + 'über' + $'c\xcc\x8ca\xcc\x81rka' # 'c\u030Ca\u0301rka' + 'Москва́' + '北京' + $'\xf0\x9f\x90\xb1' # \U0001F431 + +# INVALID UNICODE + $'\xff' + $'\xe8\xe1\xf9\xa7' +) + +ts_init_subtest "safe-ascii" +$TS_HELPER_MBSENCODE --safe "${STRINGS[@]}" >> $TS_OUTPUT 2>&1 +ts_finalize_subtest + +ts_init_subtest "invalid-ascii" +$TS_HELPER_MBSENCODE --invalid "${STRINGS[@]}" >> $TS_OUTPUT 2>&1 +ts_finalize_subtest + +ts_init_subtest "safe-utf8" +LC_ALL=C.UTF-8 \ +$TS_HELPER_MBSENCODE --safe "${STRINGS[@]}" >> $TS_OUTPUT 2>&1 +ts_finalize_subtest + +ts_init_subtest "invalid-utf8" +LC_ALL=C.UTF-8 \ +$TS_HELPER_MBSENCODE --invalid "${STRINGS[@]}" >> $TS_OUTPUT 2>&1 +ts_finalize_subtest + +ts_finalize + |