#!/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 <mpf@openbsd.org> # # 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