Test repeated invocations of getrandom()

A single program that invokes getrandom() repeatedly should end up
with the same stream of bytes, regardless of how it chunks up the
reading from the entropy source.

This test already passses.  I'm including it because it seems
like a useful confirmation.
This commit is contained in:
Daniel Kahn Gillmor
2021-02-24 15:24:45 -05:00
parent a8283c646d
commit 00d6edf90c
4 changed files with 54 additions and 4 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@ test/lib*.o
test/lib*.so
test/use_lib_random
test/use_lib_getpid
test/repeat_random
src/libfaketime.dylib.1
src/libfaketime.1.dylib

View File

@@ -25,7 +25,7 @@ functest:
%_test: %_test.c
${CC} -o $@ ${CFLAGS} $<
randomtest: getrandom_test use_lib_random librandom.so
randomtest: getrandom_test use_lib_random librandom.so repeat_random
./randomtest.sh
getpidtest: use_lib_getpid libgetpid.so

View File

@@ -14,7 +14,7 @@ FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" ./getrandom_test > run3
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'
printf >&2 'test run without the LD_PRELOAD matches a run without LD_PRELOAD\n'
fi
if diff -u run0 run1 > /dev/null; then
@@ -30,8 +30,6 @@ if diff -u run2 run3 >/dev/null; then
printf >&2 'test runs with different seeds produced the same data!\n'
fi
rm -f run-base run0 run1 run2 run3
printf 'testing shared object with getrandom() in library constructor\n'
LD_LIBRARY_PATH=. ./use_lib_random
printf 'now with LD_PRELOAD and FAKERANDOM_SEED\n'
@@ -40,6 +38,17 @@ FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_li
printf 'now with LD_PRELOAD without FAKERANDOM_SEED\n'
LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_random
FAKERANDOM_SEED=0xDEADBEEFDEADBEEF LD_PRELOAD="$FTPL" ./repeat_random 3 5 > run4
FAKERANDOM_SEED=0xDEADBEEFDEADBEEF LD_PRELOAD="$FTPL" ./repeat_random 5 3 > run5
if ! diff -u run4 run5; then
error=5
printf >&2 '5 calls of getrandom(3) did not produce the same stream as 3 calls of getrandom(5)\n'
fi
rm -f run-base run0 run1 run2 run3 run4 run5
if [ 0 = $error ]; then
printf 'getrandom interception test successful.\n'
fi

40
test/repeat_random.c Normal file
View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/random.h>
void usage(const char* name) {
fprintf(stderr,
"Usage: %s REPS SIZE\n\n"
"Gather and print REPS blocks of SIZE bytes from getrandom()\n",
name);
}
int main(int argc, const char **argv) {
int reps, size;
unsigned char *buf;
if (argc != 3) {
usage(argv[0]);
return 1;
}
reps = atoi(argv[1]);
size = atoi(argv[2]);
buf = malloc(size);
if (!buf) {
fprintf(stderr, "failure to allocate buffer of size %d\n", size);
return 1;
}
for (int i = 0; i < reps; i++) {
ssize_t resp = getrandom(buf, size, 0);
if (resp != size) {
fprintf(stderr, "tried to get %d bytes, got %zd\n", size, resp);
free(buf);
return 2;
}
for (int j = 0; j < size; j++) {
printf("%02x", buf[j]);
}
}
free(buf);
printf("\n");
return 0;
};