diff options
-rw-r--r-- | functions.sh | 27 | ||||
-rwxr-xr-x | test-functions | 22 |
2 files changed, 49 insertions, 0 deletions
diff --git a/functions.sh b/functions.sh index a97dde7..0c35b2d 100644 --- a/functions.sh +++ b/functions.sh @@ -12,6 +12,7 @@ # The following variables affect initialisation and/or function behaviour. # BASH : whether bash-specific features may be employed +# BASH_VERSINFO : whether bash-specific features may be employed # BASHPID : potentially used by _update_columns() to detect subshells # COLUMNS : potentially used by _update_columns() to get the column count # EERROR_QUIET : whether error printing functions should be silenced @@ -474,6 +475,32 @@ vewend() } # +# Generates a random uint32 with the assistance of the kernel CSPRNG. +# +srandom() +{ + # shellcheck disable=3028 + if [ "${BASH_VERSINFO:-0}" -ge 5 ]; then + srandom() + { + printf '%d\n' "${SRANDOM}" + } + elif [ -c /dev/urandom ]; then + srandom() + { + printf '%d\n' "0x$( + LC_ALL=C od -vAn -N4 -tx1 /dev/urandom | tr -d '[:space:]' + )" + } + else + warn "srandom: /dev/urandom doesn't exist as a character device" + return 1 + fi + + srandom +} + +# # Prints a diagnostic message prefixed with the basename of the running script. # warn() diff --git a/test-functions b/test-functions index 8ff6380..0c4a222 100755 --- a/test-functions +++ b/test-functions @@ -397,6 +397,27 @@ test_yesno() { iterate_tests 3 "$@" } +test_srandom() { + set -- \ + eq 0 \ + eq 0 \ + eq 0 \ + eq 0 \ + eq 0 + + row=0 + + callback() { + number=$(srandom) + test_description="srandom ($(( row += 1 ))/5: ${number:-blank})" + is_int "${number}" \ + && test "${number}" -ge 0 \ + && test "${number}" -le 4294967295 + } + + iterate_tests 2 "$@" +} + iterate_tests() { slice_width=$1 shift @@ -457,6 +478,7 @@ test_is_visible || rc=1 test_yesno || rc=1 test_die || rc=1 test_edo || rc=1 +test_srandom || rc=1 cleanup_tmpdir |