summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-24 15:24:45 -0500
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>2021-02-24 15:24:45 -0500
commit00d6edf90cfb397d37b0107113fefa444a2a0a76 (patch)
treecb0abe5d07747cab2f5bec8656284a3c80fd80ca /test
parenta8283c646d43518b7c8d9cd90ce8f627edd76a9e (diff)
downloadlibfaketime-00d6edf90cfb397d37b0107113fefa444a2a0a76.tar.gz
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.
Diffstat (limited to 'test')
-rw-r--r--test/Makefile2
-rwxr-xr-xtest/randomtest.sh15
-rw-r--r--test/repeat_random.c40
3 files changed, 53 insertions, 4 deletions
diff --git a/test/Makefile b/test/Makefile
index 8f56489..8959b71 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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
diff --git a/test/randomtest.sh b/test/randomtest.sh
index 5330419..4290ec6 100755
--- a/test/randomtest.sh
+++ b/test/randomtest.sh
@@ -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
diff --git a/test/repeat_random.c b/test/repeat_random.c
new file mode 100644
index 0000000..5a93433
--- /dev/null
+++ b/test/repeat_random.c
@@ -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;
+};