From 63b3a8e8a0352b85833695ba913fb0d81196d67f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 16 Jul 2008 17:23:37 +0000 Subject: [PATCH] * Add a bunch of unit tests to check for zzuf basic capabilities: SIGSEGV and SIGFPE detection, memory exhaustion detection. --- .gitignore | 3 ++ test/Makefile.am | 4 +-- test/bug-div0.c | 31 ++++++++++++++++++++ test/bug-memory.c | 33 +++++++++++++++++++++ test/bug-overflow.c | 31 ++++++++++++++++++++ test/check-div0 | 58 +++++++++++++++++++++++++++++++++++++ test/check-memory | 58 +++++++++++++++++++++++++++++++++++++ test/check-overflow | 58 +++++++++++++++++++++++++++++++++++++ test/{rng => check-rng} | 0 test/{utils => check-utils} | 0 10 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 test/bug-div0.c create mode 100644 test/bug-memory.c create mode 100644 test/bug-overflow.c create mode 100755 test/check-div0 create mode 100755 test/check-memory create mode 100755 test/check-overflow rename test/{rng => check-rng} (100%) rename test/{utils => check-utils} (100%) diff --git a/.gitignore b/.gitignore index 2bfbab1..313794c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ stamp-* src/zzuf test/zzcat test/zzero +test/bug-div0 +test/bug-memory +test/bug-overflow diff --git a/test/Makefile.am b/test/Makefile.am index 2d873cb..3a28300 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,9 +1,9 @@ EXTRA_DIST = rng utils file-00 file-ff file-random file-text -noinst_PROGRAMS = zzcat zzero +noinst_PROGRAMS = zzcat zzero bug-overflow bug-memory bug-div0 -TESTS = rng utils +TESTS = check-rng check-overflow check-memory check-div0 check-utils zzcat_SOURCES = zzcat.c diff --git a/test/bug-div0.c b/test/bug-div0.c new file mode 100644 index 0000000..c1d1c55 --- /dev/null +++ b/test/bug-div0.c @@ -0,0 +1,31 @@ +/* + * bug-div0 - program dividing by zero when fuzzed + * Copyright (c) 2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include + +int buf[1]; + +int main(void) +{ + int ch; + + while((ch = getc(stdin)) != EOF) + buf[0] = 1 / !ch; + + return EXIT_SUCCESS; +} + diff --git a/test/bug-memory.c b/test/bug-memory.c new file mode 100644 index 0000000..19a30df --- /dev/null +++ b/test/bug-memory.c @@ -0,0 +1,33 @@ +/* + * bug-memory - program exhausting the memory when fuzzed + * Copyright (c) 2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include + +int main(void) +{ + int i, ch; + + while((ch = getc(stdin)) != EOF) + { + char *tmp = malloc(ch * 1024 * 1024); + for(i = 0; i < 1024; i++) + tmp[ch * 1024 * i] = i; + } + + return EXIT_SUCCESS; +} + diff --git a/test/bug-overflow.c b/test/bug-overflow.c new file mode 100644 index 0000000..9fe0758 --- /dev/null +++ b/test/bug-overflow.c @@ -0,0 +1,31 @@ +/* + * bug-overflow - program causing a buffer overflow when fuzzed + * Copyright (c) 2008 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include + +char buf[1]; + +int main(void) +{ + int ch; + + while((ch = getc(stdin)) != EOF) + buf[ch * 1024 * 1024] = ch; + + return EXIT_SUCCESS; +} + diff --git a/test/check-div0 b/test/check-div0 new file mode 100755 index 0000000..899f2d5 --- /dev/null +++ b/test/check-div0 @@ -0,0 +1,58 @@ +#!/bin/sh + +set -e + +DIR="$(dirname "$0")" +ZZUF="$DIR/../src/zzuf" +PROGRAM="$DIR/bug-div0" +if [ ! -f "$PROGRAM" ]; then + echo "error: test/bug-div0 is missing" + exit 1 +fi +FAILED=0 +TESTED=0 + +if [ -z "$1" ]; then + seed=$(date | $ZZUF -m 2>/dev/null | cut -f2 -d' ' | tr -d abcdef | cut -b1-8) +else + seed="$1" +fi + +echo "*** running zzuf division-by-zero test with seed $seed ***" + +echo "*** bug-div0 < /file-00" +if ! $PROGRAM < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi -r0 bug-div0 < /file-00" +if ! "$ZZUF" -r0 -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi bug-div0 < file-00" +if "$ZZUF" -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "" +if [ "$FAILED" != 0 ]; then + echo "*** $FAILED tests failed out of $TESTED ***" + exit 1 +fi +echo "*** all $TESTED tests OK ***" + +exit 0 + diff --git a/test/check-memory b/test/check-memory new file mode 100755 index 0000000..78f7fe3 --- /dev/null +++ b/test/check-memory @@ -0,0 +1,58 @@ +#!/bin/sh + +set -e + +DIR="$(dirname "$0")" +ZZUF="$DIR/../src/zzuf" +PROGRAM="$DIR/bug-memory" +if [ ! -f "$PROGRAM" ]; then + echo "error: test/bug-memory is missing" + exit 1 +fi +FAILED=0 +TESTED=0 + +if [ -z "$1" ]; then + seed=$(date | $ZZUF -m 2>/dev/null | cut -f2 -d' ' | tr -d abcdef | cut -b1-8) +else + seed="$1" +fi + +echo "*** running zzuf buffer memory test with seed $seed ***" + +echo "*** bug-memory < /file-00" +if ! $PROGRAM < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi -r0 bug-memory < /file-00" +if ! "$ZZUF" -r0 -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi bug-memory < file-00" +if "$ZZUF" -M 256 -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "" +if [ "$FAILED" != 0 ]; then + echo "*** $FAILED tests failed out of $TESTED ***" + exit 1 +fi +echo "*** all $TESTED tests OK ***" + +exit 0 + diff --git a/test/check-overflow b/test/check-overflow new file mode 100755 index 0000000..9bcd171 --- /dev/null +++ b/test/check-overflow @@ -0,0 +1,58 @@ +#!/bin/sh + +set -e + +DIR="$(dirname "$0")" +ZZUF="$DIR/../src/zzuf" +PROGRAM="$DIR/bug-overflow" +if [ ! -f "$PROGRAM" ]; then + echo "error: test/bug-overflow is missing" + exit 1 +fi +FAILED=0 +TESTED=0 + +if [ -z "$1" ]; then + seed=$(date | $ZZUF -m 2>/dev/null | cut -f2 -d' ' | tr -d abcdef | cut -b1-8) +else + seed="$1" +fi + +echo "*** running zzuf buffer overflow test with seed $seed ***" + +echo "*** bug-overflow < /file-00" +if ! $PROGRAM < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi -r0 bug-overflow < /file-00" +if ! "$ZZUF" -r0 -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "*** zzuf -qi bug-overflow < file-00" +if "$ZZUF" -qi "$PROGRAM" < "$DIR/file-00"; then + echo "unexpected exit status $?" + FAILED="$(($FAILED + 1))" +else + echo "*** test OK ***" +fi +TESTED="$(($TESTED + 1))" + +echo "" +if [ "$FAILED" != 0 ]; then + echo "*** $FAILED tests failed out of $TESTED ***" + exit 1 +fi +echo "*** all $TESTED tests OK ***" + +exit 0 + diff --git a/test/rng b/test/check-rng similarity index 100% rename from test/rng rename to test/check-rng diff --git a/test/utils b/test/check-utils similarity index 100% rename from test/utils rename to test/check-utils