summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorWolfgang Hommel <wolfcw@users.noreply.github.com>2021-02-25 06:27:35 +0100
committerGitHub <noreply@github.com>2021-02-25 06:27:35 +0100
commit9337bccfcb7512ebf66bcbaf0f13f878cd53a6e8 (patch)
treea279b99fd6515b07e7af0083fff75d46a7078fa3 /test
parent3668fd9b0f7427de5c7b3c87c4c2b340aa18f67d (diff)
parent3a81c6becd99dba16813349e86fefa30d8cf05af (diff)
downloadlibfaketime-9337bccfcb7512ebf66bcbaf0f13f878cd53a6e8.tar.gz
Merge pull request #304 from dkg/cover-getentropy
better testing for interception of randomness from the kernel, including getentropy()
Diffstat (limited to 'test')
-rw-r--r--test/Makefile2
-rw-r--r--test/getentropy_test.c14
-rwxr-xr-xtest/randomtest.sh63
-rw-r--r--test/repeat_random.c40
4 files changed, 93 insertions, 26 deletions
diff --git a/test/Makefile b/test/Makefile
index fbbcde3..572e604 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 getentropy_test
./randomtest.sh
getpidtest: use_lib_getpid libgetpid.so
diff --git a/test/getentropy_test.c b/test/getentropy_test.c
new file mode 100644
index 0000000..a3d02e6
--- /dev/null
+++ b/test/getentropy_test.c
@@ -0,0 +1,14 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main() {
+ unsigned char buf[16];
+ if (getentropy(buf, sizeof(buf))) {
+ perror("failed to getentropy()");
+ return 1;
+ }
+ for (size_t i = 0; i < sizeof(buf); i++)
+ printf("%02x", buf[i]);
+ printf("\n");
+ return 0;
+}
diff --git a/test/randomtest.sh b/test/randomtest.sh
index 5330419..4055188 100755
--- a/test/randomtest.sh
+++ b/test/randomtest.sh
@@ -5,32 +5,34 @@ FTPL="${FAKETIME_TESTLIB:-../src/libfaketime.so.1}"
set -e
error=0
-./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 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=3
- printf >&2 'test runs with identical seeds differed!\n'
-fi
-if diff -u run2 run3 >/dev/null; then
- error=4
- printf >&2 'test runs with different seeds produced the same data!\n'
-fi
-
-rm -f run-base run0 run1 run2 run3
+for iface in getrandom getentropy; do
+ printf "Testing %s() interception...\n" "$iface"
+
+ "./${iface}_test" > "${iface}.alone"
+ LD_PRELOAD="$FTPL" "./${iface}_test" > "${iface}.preload"
+ FAKERANDOM_SEED=0x12345678DEADBEEF LD_PRELOAD="$FTPL" "./${iface}_test" > "${iface}.preload.seed0"
+ FAKERANDOM_SEED=0x12345678DEADBEEF LD_PRELOAD="$FTPL" "./${iface}_test" > "${iface}.preload.seed1"
+ FAKERANDOM_SEED=0x0000000000000000 LD_PRELOAD="$FTPL" "./${iface}_test" > "${iface}.preload.seed2"
+
+ if diff -u "${iface}.alone" "${iface}.preload" > /dev/null; then
+ error=1
+ printf >&2 '%s() without the LD_PRELOAD matches a run without LD_PRELOAD\n' "$iface"
+ fi
+ if diff -u "${iface}.preload" "${iface}.preload.seed0" > /dev/null; then
+ error=2
+ printf >&2 '%s() without a seed produced the same data as a run with a seed!\n' "$iface"
+ fi
+ if ! diff -u "${iface}.preload.seed0" "${iface}.preload.seed1"; then
+ error=3
+ printf >&2 '%s() with identical seeds differed!\n' "$iface"
+ fi
+ if diff -u "${iface}.preload.seed1" "${iface}.preload.seed2" >/dev/null; then
+ error=4
+ printf >&2 '%s() with different seeds produced the same data!\n' "$iface"
+ fi
+ rm -f "${iface}.alone" "${iface}.preload" "${iface}.preload.seed0" "${iface}.preload.seed1" "${iface}.preload.seed2"
+done
printf 'testing shared object with getrandom() in library constructor\n'
LD_LIBRARY_PATH=. ./use_lib_random
@@ -40,6 +42,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 > repeat3x5
+FAKERANDOM_SEED=0xDEADBEEFDEADBEEF LD_PRELOAD="$FTPL" ./repeat_random 5 3 > repeat5x3
+
+if ! diff -u repeat3x5 repeat5x3; 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 repeat3x5 repeat5x3
+
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;
+};