#!/bin/sh
# usage: check1fuzz <fuzzer stimulus file>
# fuzz check script, for checking that one fuzz input works

set -e
: ${srcdir=.}
. "$srcdir"/shlib

fuzzfile="$1"

exec <"$fuzzfile"

case "$fuzzfile" in
fuzz-*)		kind=f;	case="${fuzzfile#fuzz-}"	;;
fuzzraw-*)	kind=r;	case="${fuzzfile#fuzzraw-}"	;;
*)		printf >&2 '\nWRONG-FUZZ-%s\n' $fuzzfile; exit 1 ;;
esac

desc="$case.$kind"

read nargs
if [ "$nargs" = 0 ]; then printf "SKIPPED-$desc "; exit 5; fi

read arg1len
read program

case "$program" in
*/*)	;;
*)	program="./$program" ;;
esac

exec 3>output-$desc.trouble

set +e
${program} <"$fuzzfile" >output-$desc.out.tmp 2>&3
rc=$?
set -e

if [ $rc = 0 ]; then
	set +e
	diff -u output-$desc.out.tmp case-$case.out >&3
	rc=$?
	set -e
fi

if [ $rc != 0 ]; then
	printf >&2 '\nFAIL-%s\n' $desc
	exit $rc
fi

printf '%s ' "$desc"
rm -f output-$desc.trouble
