#!/bin/sh

# Test if randombytes() works in the chrooted env.
# E.g. randombytes devurandom variant must have
# /dev/urandom open before the chroot() call.

set -e

dir=`dirname "$0"`

# change directory to $AUTOPKGTEST_TMP
cd "${AUTOPKGTEST_TMP}"

cleanup() {
  ex=$?
  rm -f randombytes-info
  exit "${ex}"
}
trap "cleanup" EXIT TERM INT

CC="cc -O"

echo 'librandombytes - chrooted:'
mkdir -p 'jail'
(
  cat "${dir}/../../command/randombytes-info.c"
  echo
  echo '__attribute__((constructor))'
  echo 'static void jail(void) {'
  echo '  if (chroot("jail") == -1) {'
  echo '    fprintf(stderr, "unable to chroot\\n");'
  echo '    fflush(stderr);'
  echo '    _exit(1);'
  echo '  }'
  echo '}'
) | ${CC} -xc -o randombytes-info - -lrandombytes -lm -lrt

exec ./randombytes-info
