#!/bin/csh

# -d testdir (sandbox will be made in this directory)
# -p principal (default :OWNER@)

set testdir = /tmp
set p = ":OWNER@"
set a = "A:$p"

set prog = $cwd
set file = "file"
set dir = "dir"
set r = $prog/run_one_test

while ($#argv > 0)
  switch ($1)
  case "-d":
    shift
    set testdir = $1
    breaksw
  case "-p":
    shift
    set p = $1
    breaksw
  endsw
  shift
end

set testdir = $testdir/sandbox

# Make a sandbox to play in
if (-d $testdir) then
  if (-d $testdir/$dir) then
    chmod -f 777 $testdir/$dir
  endif
  rm -rf $testdir
endif
mkdir $testdir
cd $testdir
echo "testdir = $testdir"

#
# File ace bit tests
#

# Create a test file, give owner all permissions
date > $file
if (! -f $file) then
  echo "can't create $dir/$file for testing"
  exit 1
endif

echo nfs4_setfacl -s "$a":rwaxdnNtTcCoy $file
nfs4_setfacl -s "$a":rwaxdnNtTcCoy $file
if ($status != 0) then
  exit 1
endif

# Remove permissions one at a time, check that they're really gone

$r $prog write "$a":raxdnNtTcCoy $file $file
$r $prog read "$a":waxdnNtTcCoy $file $file
$r $prog append "$a":rwxdnNtTcCoy $file $file
$r $prog chown "$a":rwaxdnNtTcCy $file $file
$r $prog setacl "$a":rwaxdnNtTcoy $file $file

#
# Directory ace bit tests
#

# Create a test directory, give owner all permissions
mkdir $dir
nfs4_setfacl -s "$a":xlfsDnNtTcCoy $dir

# Remove permissions one at a time, check that they're really gone

$r $prog creat "$a":xlsDnNtTcCoy $dir $dir/$file
$r $prog listdir "$a":xsfDnNtTcCoy $dir $dir
$r $prog mkdir "$a":xlfDnNtTcCoy $dir $dir/$dir

# Create a file for lookup and delete tests

nfs4_setfacl -s "$a":xlfsDnNtTcCoy $dir
touch $dir/$file
if (! -f $dir/$file) then
  echo "can't create $dir/$file for testing"
endif

$r $prog lookup "$a":lfsDnNtTcCoy $dir $dir/$file

# Delete test: first have to put back lookup ("x") rights,
# then remove "d", then "D"
nfs4_setfacl -s "$a":xlfsDnNtTcCoy $dir
nfs4_setfacl -s "$a":rwaxnNtTcCoy $dir/$file
if ($status != 0) then
  exit 1
endif

$r $prog rm "$a":xlfsnNtTcCoy $dir $dir/$file
