tests: add tests for encode functions from lib/mbsalign.c

Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
This commit is contained in:
Vaclav Dolezal 2017-12-21 15:10:15 +01:00
parent 2ba65f379d
commit cad13ba344
8 changed files with 158 additions and 0 deletions

View 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"}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View 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

View File

@ -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;
}

62
tests/ts/misc/mbsencode Executable file
View File

@ -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