From eb9605eaf65c0c4858511a10e6ae6398cc2dc1c8 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Fri, 13 Nov 2020 18:19:31 -0500 Subject: [PATCH] Tests: fix randstring() with BSD `tr` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit On FreeBSD, `tr` will error out with an "Illegal byte sequence" error when it is provided with an invalid multi-byte character. This happens regularly when its input is random. Forcing `tr` to use the 'C' locale works around this problem as that locale only allows single-byte characters. Change-Id: I8d8e84fb7f356205dd45479538e5bc0bff3c1668 Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- tests/utils/utils.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index 196f636e6..1428afd62 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -204,8 +204,13 @@ function validate_kernel_version () # $2 = include special characters; 1 = yes, 0 = no; defaults to yes function randstring() { + local len="${1:-16}" + [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]" - cat /dev/urandom 2>/dev/null | tr -cd "$CHAR" 2>/dev/null | head -c ${1:-16} 2>/dev/null + # /dev/urandom isn't guaranteed to generate valid multi-byte characters. + # Specifying the C locale eliminates the "Illegal byte sequence" error + # that 'tr' outputs in such cases. + LC_CTYPE=C tr -cd "$CHAR" < /dev/urandom 2>/dev/null | head -c "$len" 2>/dev/null echo } -- 2.34.1