Skip to content

Commit

Permalink
Merge pull request wolfcw#294 from dkg/improve-FAKE_RANDOM-tests
Browse files Browse the repository at this point in the history
Improve tests for FAKE_RANDOM
  • Loading branch information
wolfcw committed Feb 23, 2021
2 parents 3c0b101 + 54994ce commit b4a822c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion test/getrandom_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <sys/random.h>
#include <stdlib.h>

int main() {
int base() {
char *buf = calloc(100, 1);
size_t buflen = 100;
unsigned flags = GRND_NONBLOCK;
Expand All @@ -23,3 +23,7 @@ int main() {
free(buf);
return 0;
}

int main() {
return base() + base();
}
18 changes: 13 additions & 5 deletions test/randomtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,35 @@

FTPL="${FAKETIME_TESTLIB:-../src/libfaketime.so.1}"

set -e

error=0
./getrandom_test > run0
./getrandom_test > run-base
LD_PRELOAD="$FTPL" ./getrandom_test > run0
FAKERANDOM_SEED=0x12345678DEADBEEF LD_PRELOAD="$FTPL" ./getrandom_test > run1
FAKERANDOM_SEED=0x12345678DEADBEEF LD_PRELOAD="$FTPL" ./getrandom_test > run2
FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" ./getrandom_test > run3


if diff -u run0 run1 > /dev/null; then
if diff -u run-base run0 > /dev/null; then
error=1
printf >&2 'test run without the LD_PRELOAD matches a run without LD_PRELOAD'
fi

if diff -u run0 run1 > /dev/null; then
error=2
printf >&2 'test run without a seed produced the same data as a run with a seed!\n'
fi
if ! diff -u run1 run2; then
error=2
error=3
printf >&2 'test runs with identical seeds differed!\n'
fi
if diff -u run2 run3 >/dev/null; then
error=3
error=4
printf >&2 'test runs with different seeds produced the same data!\n'
fi

rm -f run0 run1 run2 run3
rm -f run-base run0 run1 run2 run3

if [ 0 = $error ]; then
printf 'getrandom interception test successful.\n'
Expand Down

0 comments on commit b4a822c

Please sign in to comment.