From 92041e74d2cbef19c1a92c8e3e987432fbb2d23a Mon Sep 17 00:00:00 2001 From: mpf Date: Wed, 20 Aug 2008 09:29:51 +0000 Subject: [PATCH] Regression tests for gzip(1): - Test if we detect truncated or corrupted files. - Test basic functionality OK millert@, markus@ --- regress/usr.bin/Makefile | 6 +-- regress/usr.bin/gzip/Makefile | 17 +++++++++ regress/usr.bin/gzip/t1.sh | 70 +++++++++++++++++++++++++++++++++++ regress/usr.bin/gzip/t2.sh | 39 +++++++++++++++++++ 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 regress/usr.bin/gzip/Makefile create mode 100644 regress/usr.bin/gzip/t1.sh create mode 100644 regress/usr.bin/gzip/t2.sh diff --git a/regress/usr.bin/Makefile b/regress/usr.bin/Makefile index e3811f50c83..9896cf325f6 100644 --- a/regress/usr.bin/Makefile +++ b/regress/usr.bin/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.18 2006/03/14 03:57:42 ray Exp $ +# $OpenBSD: Makefile,v 1.19 2008/08/20 09:29:51 mpf Exp $ # $NetBSD: Makefile,v 1.1 1997/12/30 23:27:11 cgd Exp $ -SUBDIR+= basename bc cap_mkdb dc diff diff3 dirname grep gzsig m4 make patch -SUBDIR+= rcs sdiff sort ssh tsort +SUBDIR+= basename bc cap_mkdb dc diff diff3 dirname grep gzip gzsig +SUBDIR+= m4 make patch rcs sdiff sort ssh tsort .include diff --git a/regress/usr.bin/gzip/Makefile b/regress/usr.bin/gzip/Makefile new file mode 100644 index 00000000000..6a17ae8851a --- /dev/null +++ b/regress/usr.bin/gzip/Makefile @@ -0,0 +1,17 @@ +# $OpenBSD: Makefile,v 1.1 2008/08/20 09:29:51 mpf Exp $ + +TESTSCRIPTS=t1 t2 + +.for t in ${TESTSCRIPTS} +REGRESS_TARGETS+=t-${t} +CLEANFILES+=${t}.gz + +t-${t}: + sh ${.CURDIR}/${t}.sh ${.CURDIR} ${.OBJDIR} +.endfor + +CLEANFILES+=*.test + +.PHONY: ${REGRESS_TARGETS} + +.include diff --git a/regress/usr.bin/gzip/t1.sh b/regress/usr.bin/gzip/t1.sh new file mode 100644 index 00000000000..8eb29931990 --- /dev/null +++ b/regress/usr.bin/gzip/t1.sh @@ -0,0 +1,70 @@ +#!/bin/sh +# $OpenBSD: t1.sh,v 1.1 2008/08/20 09:29:51 mpf Exp $ + +# Test if gzip(1) detects truncated or corrupted files + +# Copyright (c) 2008 Marco Pfatschbacher +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +# truncate at 2k +gzip < /etc/rc | dd bs=1k count=2 of=t1.gz 2>/dev/null +if gzip -vt t1.gz; then + echo "=> ERROR: truncation not detected!" + exit 1 +else + echo "=> OK" +fi + +# truncate at 1k +gzip < /etc/rc | dd bs=1k count=1 of=t1.gz 2>/dev/null +if gzip -vt t1.gz; then + echo "=> ERROR: truncation not detected!" + exit 1 +else + echo "=> OK" +fi + +# skip some data in the middle +gzip < /etc/rc | dd bs=1k seek=1 >> t1.gz 2>/dev/null +if gzip -vt t1.gz; then + echo "=> ERROR: corruption not detected!" + exit 1 +else + echo "=> OK" +fi + +# simple fuzzer that modifies one random byte at a random offset + +gzip < /etc/rc > t1.gz +for i in `jot 100`; do + where=$((RANDOM % 2048 + 256)) # random offset (but skip the header) + orig=`dd if=t1.gz skip=$where bs=1 count=1 2>/dev/null |\ + hexdump -e '"%d"'` + fuzz=$((((orig + RANDOM) % 256) + 1)) + if [ $fuzz = $orig ]; then + fuzz=$(((fuzz + 1) % 256)) + fi + echo "$i/100: fuzzing byte @$where: $orig -> $fuzz" + + if (dd if=t1.gz bs=1 count=$where 2>/dev/null; \ + echo -n \\0`printf "%o" $fuzz`; \ + dd if=t1.gz bs=1 skip=$((where+1)) 2>/dev/null) | gzip -tv; then + echo "=> ERROR: corruption not detected!" + exit 1 + else + echo "=> OK" + fi +done + +exit 0 diff --git a/regress/usr.bin/gzip/t2.sh b/regress/usr.bin/gzip/t2.sh new file mode 100644 index 00000000000..9d2d39f4c67 --- /dev/null +++ b/regress/usr.bin/gzip/t2.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# $OpenBSD: t2.sh,v 1.1 2008/08/20 09:29:51 mpf Exp $ + +# test basic gzip functionality + +gzip -c /etc/rc > t1.gz +if ! gzip -vt t1.gz; then + echo "=> ERROR: could not gzip" + exit 1 +else + echo "=> OK" +fi + +if ! gunzip -c t1.gz | cmp -s - /etc/rc; then + echo "=> ERROR: uncompressed file does not match" + gunzip -c t1.gz | diff - /etc/rc + exit 1 +else + echo "=> OK" +fi + +gzip -c /etc/rc /etc/motd > t1.gz +if ! gzip -vt t1.gz; then + echo "=> ERROR: could not gzip multiple files" + exit 1 +else + echo "=> OK" +fi + +cat /etc/rc /etc/motd > rcmotd.test +if ! gunzip -c t1.gz | cmp -s - rcmotd.test; then + echo "=> ERROR: gunzipped files do not match" + gunzip -c t1.gz | diff - rcmotd.test + exit 1 +else + echo "=> OK" +fi + +exit 0 -- 2.20.1