#!/bin/sh
expect() {
	addr=$1
	opts=$(echo $2 |tr , '\n' |sort |tr '\n' , |sed 's/,$//')
	mtpt=$(eval 'echo $'$#)
	expected="mount -t 9p -o $opts $addr $mtpt"
	shift; shift
	actual=$(9mount -n "$@" 2>&1)
	aopts=$(echo $actual |sed 's/.*-o \([^ ]*\) .*/\1/' |tr , '\n' |sort |tr '\n' , |sed 's/,$//')
	actual=$(echo $actual |sed 's/-o [^ ]*/-o '"$aopts"'/')
	if [ "$expected" != "$actual" ]; then
		echo '	'9mount "$@"
		echo $expected' #expected'
		echo $actual' #actual'
		exit 1
	fi
}

mtpt=/tmp/9mount
mkdir -p $mtpt
trap 'rmdir $mtpt' EXIT
dfltopts="name=$USER,uname=$USER,noextend,nodev,nosuid"

expect 127.0.0.1 $dfltopts,tcp,trans=tcp,port=888 tcp!localhost!888 $mtpt
mkdir -p /tmp/ns.$USER.:0
nc -lkU /tmp/ns.$USER.:0/foo &
expect /tmp/ns.$USER.:0/foo $dfltopts,unix,trans=unix unix!/tmp/ns.$USER.:0/foo $mtpt
kill $!
expect /dev/bar $dfltopts,virtio,trans=virtio virtio!/dev/bar $mtpt
expect nodev $dfltopts,fd,trans=fd,rfdno=0,wfdno=1 - $mtpt

dfltopts="tcp,trans=tcp,$dfltopts"

testflag() {
	opts=$1
	shift
	expect 127.0.0.1 $dfltopts,$opts "$@" tcp!localhost $mtpt
}

# some behaviour differs when running as root
if [ $(id -u) -eq 0 ]; then
	dashv_expect=$(echo $dfltopts | sed 's/,nodev//')
	msize_expect="msize=16384"
else
	dashv_expect=$dfltopts
	msize_expect="msize=8192"
fi

testflag "uid=$(id -u),gid=$(id -g),dfltuid=$(id -u),dfltgid=$(id -g)" -i
testflag "access=any" -s
expect 127.0.0.1 $(echo $dfltopts |sed 's/,noextend//') -u tcp!localhost $mtpt
expect 127.0.0.1 $dashv_expect -v tcp!localhost $mtpt
testflag "access=$(id -u)" -x
testflag "aname=abcdef" -a abcdef
testflag "cache=loose" -c loose
testflag "debug=0x0130" -d fcall,conv,mux
testflag $msize_expect -m 16384

shouldfail() {
	output=$(9mount -n "$@" 2>&1) && {
		echo '	'9mount "$@"
		echo $output' #should have failed!'
	}
}

shouldfail -a tcp!localhost $mtpt
shouldfail -a main/active,bwahaha tcp!localhost $mtpt
shouldfail -d lol tcp!localhost $mtpt
shouldfail -m z tcp!localhost $mtpt
shouldfail udp!localhost $mtpt
shouldfail unix!/tmp/9mount!qux $mtpt
shouldfail virtio!/dev/chan!bar $mtpt
shouldfail tcp!localhost!564!foo $mtpt
