]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: add tests for encode functions from lib/mbsalign.c
authorVaclav Dolezal <vdolezal@redhat.com>
Thu, 21 Dec 2017 14:10:15 +0000 (15:10 +0100)
committerVaclav Dolezal <vdolezal@redhat.com>
Wed, 10 Jan 2018 16:40:53 +0000 (17:40 +0100)
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
tests/commands.sh
tests/expected/misc/mbsencode-invalid-ascii [new file with mode: 0644]
tests/expected/misc/mbsencode-invalid-utf8 [new file with mode: 0644]
tests/expected/misc/mbsencode-safe-ascii [new file with mode: 0644]
tests/expected/misc/mbsencode-safe-utf8 [new file with mode: 0644]
tests/helpers/Makemodule.am
tests/helpers/test_mbsencode.c [new file with mode: 0644]
tests/ts/misc/mbsencode [new file with mode: 0755]

index 502935edeab9a44aca05e3b7ce5a90eff893a9cf..985064446d08bb44ae96f2f09d4418326ed22c7d 100644 (file)
@@ -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 (file)
index 0000000..412790f
--- /dev/null
@@ -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 (file)
index 0000000..979f49c
--- /dev/null
@@ -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 (file)
index 0000000..8ee2403
--- /dev/null
@@ -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 (file)
index 0000000..1154a2b
--- /dev/null
@@ -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
index 2f2611b67a43b44975b641b17e43f70c7ec420a4..ab0b3cea97692842b723a8f8df02babcf6b3130c 100644 (file)
@@ -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 (file)
index 0000000..a8f9383
--- /dev/null
@@ -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 (executable)
index 0000000..7790ef0
--- /dev/null
@@ -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
+