##################################################################
#
# Generic NFS tests
#
# Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
#
##################################################################

##################################################################
#
# Initialization
#
##################################################################
INIT {
	global	ARGS host path

	if {[llength $ARGS] < 1} {
		set host "localhost"
	} else {
		set host [lindex $ARGS 0]
	}
	if {[llength $ARGS] < 2} {
		set path "/tmp/nfsbench"
	} else {
		set path [lindex $ARGS 1]
	}

	set mnt [rpc create $host mount 1]
	set nfs [rpc create $host nfs 2]
	set root [$mnt mount $path]
}

##################################################################
# Helper function for various create() tests
##################################################################
proc create_test {sattr} {

	global nfs root

	catch {$nfs remove $root test}
	return [$nfs create $root test $sattr]
}

proc create_test_root {sattr} {

	set uid [rpc setuid 0]
	set gid [rpc setgid 0]
	set failed [catch {set result [create_test $sattr]} error]
	rpc setuid $uid
	rpc setgid $gid
	if {$failed} {
		return -code error $error
	}
	return $result
}

##################################################################
#
# Test all kinds of simple stuff
#
##################################################################
TEST	getattr		Check the getattr call
FAIL	ABORT
BODY {
	set result [$nfs getattr $root]
	set ftype [lindex $result 0]
	if {$ftype != "DIR"} {
		WARN "mounted object $host:$path is not a directory ($ftype)!"
		ABORT
	}
}

##################################################################
TEST	create		Check the create call
FAIL	ABORT
BODY {
	catch {$nfs remove $root test}
	set result [$nfs create $root test {-mode 0777}]
	set fmode [expr [lindex $result 2] & 07777]
	if {$fmode != 0777} {
		WARN "File created with mode [format \"%0o\" $fmode] != 0777"
	}
}

##################################################################
TEST	lookup		Check the lookup call
FAIL	ABORT
BODY {
	set result [$nfs lookup $root test]
}

##################################################################
TEST	remove		Check the remove call
FAIL	ABORT
BODY {
	set result [$nfs remove $root test]
}

##################################################################
TEST	mkdir		Check the mkdir call
FAIL	ABORT
BODY {
	catch {$nfs rmdir $root "testdir"}
	$nfs mkdir $root "testdir" {-mode 0777}
}

##################################################################
TEST	rmdir		Check the rmdir call
FAIL	ABORT
BODY {
	$nfs rmdir $root "testdir"
}

##################################################################
TEST	symlink		Check the symlink call
FAIL	ABORT
BODY {
	$nfs symlink $root "test" "/tmp/xyzzy" {-mode 0777}
}

##################################################################
TEST	readlink	Check the readlink call
FAIL	ABORT
BODY {
	set fh [lindex [$nfs lookup $root test] 0]
	$nfs readlink $fh
}

##################################################################
TEST	rename		Check the rename call
FAIL	ABORT
BODY {
	catch {$nfs remove $root test}
	catch {$nfs remove $root funghi}
	$nfs create $root test {-mode 0666}
	$nfs rename $root test $root funghi
}

##################################################################
TEST	ren0001		Check fh invariance against rename
FAIL	ABORT
BODY {
	set fh1 [lindex [$nfs lookup $root funghi] 0]
	$nfs rename $root funghi $root test
	set fh2 [lindex [$nfs lookup $root test] 0]
	if {$fh1 != $fh2} {
		WARN "file handles not invariant under rename"
		$nfs remove $root test
	} else {
		$nfs mkdir $root testdir {-mode 0777}
		set dir [lindex [$nfs lookup $root testdir] 0]
		$nfs rename $root test $dir kaboom
		set fh2 [lindex [$nfs lookup $dir kaboom] 0]
		if {$fh1 != $fh2} {
			WARN "file handles not invariant under rename to\
				different directory"
		}
		$nfs remove $dir kaboom
	}
	$nfs rmdir $root testdir
}

##################################################################
TEST	link		Check the link call
FAIL	ABORT
BODY {
	catch {$nfs remove $root test}
	catch {$nfs remove $root funghi}
	set fh [lindex [$nfs create $root test {-mode 0666}] 0]
	$nfs link $fh $root funghi
}

##################################################################
TEST	lnk0001		Check whether server allows link to itself
FAIL	ABORT
BODY {
	catch {$nfs remove $root test}
	catch {$nfs remove $root funghi}
	set result [$nfs create $root test {-mode 0666}]
	set fh     [lindex $result 0]
	set nlinks [lindex $result 3]
	if {![catch {$nfs link $fh $root test} error]} {
		ERROR "server allows linking file to itself"
	} else {
		if {[lindex [$nfs getattr $fh] 2] != $nlinks} {
			ERROR "server returns error $error but increments\
					link count"
		}
	}
}

