#!/bin/sh
# (c) 2006, Steve Grubb <sgrubb@redhat.com>
# (c) 2006, LMH <lmh@info-pull.com>
#
# This software may be freely redistributed under the terms of the GNU
# public license version 2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

if [ $# -ne 2 ] ; then
	echo "Usage: ./run_test <file system type> <pass number>"
	exit 1
fi
fs="$1"
i="$2"
if [ `id -u` != 0 ] ; then
	echo "You need to be root to run this test suite"
	exit 1
fi
trap cleanup 1 2 3 5 15
# Clear ring buff
dmesg -c >/dev/null 2>&1
if [ x"$DIR" = "x" ] ; then
	DIR="/media/test"
fi

cleanup () {
	umount $DIR 2>/dev/null
	rmdir $DIR
}

check_results () {
	echo "++ Checking results"
	if [ "$fs" = "xfs" -o "$fs" = "gfs2" ] ; then
		FAULT=`dmesg | grep -Ei 'oops|unable to handle|assert|panic' | grep -v 'swap header version'`
	else
		FAULT=`dmesg | grep -Ei 'oops|bug|unable to handle|assert|panic|\[<' | grep -v 'swap header version' | grep -v 'ilesystem panic'`
	fi
	if [ x"$FAULT" != "x" ] ; then
		echo "++ Something found (`pwd`/fs/$fs.$i.img)..."
		exit 1
	fi
}

###
# Start of real test
###
echo "++ Testing $PWD/fs/$fs.$i.img..."
if [ $fs = "swap" ] ; then
	swapon ./cfs/$fs.$i.img
	swapoff ./cfs/$fs.$i.img
	check_results
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	rm -f cfs/$fs.*
	sync
	exit 0
fi
mount ./cfs/$fs.$i.img $DIR -t $fs -o loop >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	# perform basic operations...
	echo "+++ Checking dir..."
	ls -Z $DIR >/dev/null 2>&1
	if [ $? -ne 0 ] ; then
		ls $DIR >/dev/null 2>&1
	fi
	echo "+++ Making files..."
	touch $DIR/file >/dev/null 2>&1
	ln -s $DIR/file $DIR/fileb >/dev/null 2>&1
	mkdir $DIR/dir1 >/dev/null 2>&1
	echo "+++ Checking stat..."
	stat $DIR/file >/dev/null 2>&1
	stat $DIR/fileb >/dev/null 2>&1
	stat $DIR/dir1 >/dev/null 2>&1
	echo "+++ Writing to files..."
	echo "test" > $DIR/file
	cat $DIR/file >/dev/null 2>&1
	chcon -u user_u $DIR/file >/dev/null 2>&1
	chown nobody,nobody $DIR/COPYING >/dev/null 2>&1
	chmod 0600 $DIR/COPYING >/dev/null 2>&1
	echo "+++ Reading from files..."
	# This tends to hang machines...so commented out for now
#	dd if=$DIR/mangle of=/dev/null >/dev/null 2>&1
	cat $DIR/* >/dev/null 2>&1
	echo "+++ device files..."
	rm $DIR/null >/dev/null 2>&1
	mknod $DIR/null c 1 3 >/dev/null 2>&1
	echo "+++ Writing to dirs..."
	cat $DIR/file > $DIR/dir1 2>/dev/null
	cp $DIR/file $DIR/dir1 >/dev/null 2>&1
	chcon -u user_u $DIR/dir1 >/dev/null 2>&1
	echo "+++ Checking unlink..."
	rm -rf $DIR/file >/dev/null 2>&1
	rm -rf $DIR/fileb >/dev/null 2>&1
	rm -rf $DIR/dir1/* >/dev/null 2>&1
	rmdir  $DIR/dir1 >/dev/null 2>&1
	echo "++ unmounting ./cfs/$fs.$i.img"
	umount $DIR >/dev/null 2>&1
	if [ $? -ne 0 ] ; then
		echo "Failed to unmount test file system"
		exit 1
	fi
	sync
# NOTE: we do not have an else clause here for mount failing. This sometimes
# happens. We count on check_results to catch any true mount problems. This
# does mean that we could miss some test problems like where a filesystem
# really didn't unmount but umount thinks it did.
fi
check_results
if [ $? -ne 0 ] ; then
	exit 1
fi

exit 0 
