Tests: fix randstring() with BSD `tr`
[lttng-tools.git] / tests / utils / utils.sh
index 196f636e68b3625731b9a16483be696345042fc0..1428afd62841ad99a930fc6fee7be617a2ddf1e0 100644 (file)
@@ -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
 }
 
This page took 0.022968 seconds and 4 git commands to generate.