#!/bin/sh
exec perl -x -S $0 ${1+"$@"} # -*-perl-*-
#!perl


if ($] !~ /^5\..*/) {
  # uh-oh. this isn't perl 5.
  foreach (split(/:/, $ENV{PATH})) { # try to find "perl5".
    exec("$_/perl5", "-x", "-S", $0, @ARGV) if (-x "$_/perl5");
  }
  # we failed. bail.
  die "Your perl is too old; I need perl 5. See the README for what to do.\n";
}

# load the real script. this is isolated in an 'eval' so perl4 won't
# choke on the perl5-isms.
eval join("\n", <DATA>);
if ($@) { die "$@"; }
__END__
#
# regression
#
# Authors: Mathew Monroe, Nat Lanza
#
#
# Copyright (c) of Carnegie Mellon University, 1997,1998,1999.
#
# Permission to reproduce, use, and prepare derivative works of
# this software for internal use is granted provided the copyright
# and "No Warranty" statements are included with all reproductions
# and derivative works. This software may also be redistributed
# without charge provided that the copyright and "No Warranty"
# statements are included in all redistributions.
#
# NO WARRANTY. THIS SOFTWARE IS FURNISHED ON AN "AS IS" BASIS.
# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED AS TO THE MATTER INCLUDING, BUT NOT LIMITED
# TO: WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY
# OF RESULTS OR RESULTS OBTAINED FROM USE OF THIS SOFTWARE. CARNEGIE
# MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT
# TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
#

$| = 1;

use strict;
use vars qw($idscan $partscan $fail $server $partname $partnum $rootid $path $utilpath $prog $dumpattr $opt_s $opt_v $opt_R $opt_T $opt_i $opt_D);
use Getopt::Std;

main();

sub main {
  getopts('svR:T:i:D');
  $fail = 0;
  $server = shift(@ARGV) or do_usage();
  $partname = shift(@ARGV) or do_usage();
  $idscan = "id (0x[0-9a-f]+,0x[0-9a-f]+,0x[0-9a-f]+)";
  $partscan = "partition ([0-9]+)";

  my $iters;
  my $partition_sec;
  my $systype;
  my $nasd_options_return;
  my @args;

  $systype = `uname -s`;
  chomp $systype;

  if ($opt_R) { $path = $opt_R; }
  else { $path = `pwd`; chomp($path); }
  print "Test Path = $path\n";

  if ($opt_T) { $utilpath = $opt_T; }
  else { $utilpath = "../utils"; }
  print "Util Path = $utilpath\n";

  $prog = $path . "/nasd_edrfs_test";
  $dumpattr = $utilpath . "/nasd_edrfs_dumpattr";
  if ($opt_i) {
    $iters = $opt_i;
  }
  else {
    $iters = 1;
  }

  printf("Starting test sequence with security %s\n",
	 $opt_s ? "enabled" : "disabled");
  printf("Using server \"%s\"\n", $server);

  if ($opt_v) { print "Operating in verbose mode.\n\n"; }
  else { print "\n"; }

  $fail = 0;

  test_noops($iters);
  if ($fail) { do_fail(); }

  test_mount();
  if ($fail) { do_fail(); }

  test_fsstat();
  # Do not test fsinfo, it is not supported.
  # Do not test access, it is also not supported.

  test_lookup();
  test_readdir();
  test_setattr();
  test_create();
  test_symlink();
  test_remove();
  test_mkdir();
  test_rmdir();

  test_rename();
  if ($fail) { do_fail(); }

  if (!$fail) {
    print "\nAll tests completed successfully.\n";
    exit 0;
  } else {
    print "\n$fail tests failed.\n";
    exit 1;
  }
}


#
# Helper functions
#

sub do_warning {
  print "A critical section of the test sequence has failed.\n";
  print "This is probably because the server you're running it on\n";
  print "is not setup correctly with an empty partition for testing.\n";
  exit(1);
}

sub do_usage {
  print "usage: regression [options] server partname\n";
  print "   options include\n";
  print "     -s      [ enable security testing ]\n";
  print "     -i      [ Specify the number of iterations for tests. ]\n";
  print "     -R      [ specify the path to the testing program.\n";
  print "               If not specified, the path is assumed to be \".\". ]\n";
  print "     -T      [ specify the path to the dumpattr program.\n";
  print "               If not specified, the path is assumed to be\n";
  print "               \"../utils\". ]\n";
  print "     -D      [ Dump to test_data directory instead of reading. ]\n";
  exit(1);
}

sub do_fail {
  print "\nA critical test failed, aborting.\n";
  exit(1);
}

sub create_file($$) {
  my $id = $_[0];
  my $name = $_[1];

  my $result = `$prog create $server NASD_SUCCESS $id $name 0 2>&1`;
  if ($?) {
    $result = lookup_file($id, $name);
    if (!$result) {
      do_warning();
      return;
    }
    return $result;
  }
  if ($result =~ /$idscan/) {
    return $1;
  }
}

sub remove_file($$) {
  my $id = $_[0];
  my $name = $_[1];

  my $result = `$prog remove $server NASD_SUCCESS $id $name 2>&1`;
  if ($?) {
    print " remove_file() failed:\n\n$result\n\n";
  }
}

sub lookup_file($$) {
  my $id = $_[0];
  my $name = $_[1];

  my $result = `$prog lookup $server NASD_SUCCESS $id $name 2>&1`;
  if ($?) {
    print " lookup_file() failed:\n\n$result\n\n";
    return;
  }
  if ($result =~ /$idscan/) {
    return $1;
  }

  print "Couldn't get id from:\n$result\n";
  return;
}
  
sub mk_dir($$) {
  my $id = $_[0];
  my $name = $_[1];

  my $result = `$prog mkdir $server NASD_SUCCESS $id $name 0 0 0 0 2>&1`;
  if ($?) {
    $result = lookup_file($id, $name);
    if (!$result) {
      do_warning();
      return;
    }
    return $result;
  }
  if ($result =~ /$idscan/) {
    return $1;
  }
}

sub rm_dir($$) {
  my $id = $_[0];
  my $name = $_[1];

  my $result = `$prog rmdir $server NASD_SUCCESS $id $name 2>&1`;
  if ($?) {
    print " rm_dir() failed:\n\n$result\n\n";
  }
}

# convert a edrfs id to a nasd id
sub nasdid($) {
  my $edrfs_id = $_[0];
  $edrfs_id =~ /(0x[0-9a-f]+),0x[0-9a-f]+,0x[0-9a-f]+/;
  return $1;
}

# dump a directory to a file
sub dump_attr($$) {
  my $outfile = $_[0];
  my $id = $_[1];
  my $ofile;
  my $result;
  my @dump_args = ($dumpattr, "-t", $server, $partnum, $id, "passwd");

  $result = `@dump_args 2>&1`;
  open (ofile, ">$outfile") or die "Can't open output file $outfile\n";
  print ofile "$result\n";
  close (ofile);
}

#compair a directory to one on disk
sub comp_attr($$) {
  my $infile = $_[0];
  my $id = $_[1];
  my $ifile;
  my $result;
  my $idata;
  my @dump_args = ($dumpattr, "-t", $server, $partnum, $id, "passwd");

  $result = `@dump_args 2>&1`;

  open (ifile, "<$infile") or die "Can't open input file $infile\n";
  {
    local $/;
    $idata = <ifile>;
  }
  close (ifile);
  chomp($idata);
  chomp($idata);
  chomp($result);

  if( $idata ne $result ) {
    print "\nThe directory information is not correct!\n";
    print "*****\n$result\n*****\n!=\n*****\n$idata\n*****";
    $fail++;
    return 0;
  } else {
    if($opt_v) {
      print "Attributes match correctly.\n";
    } else {
      print "+";
    }
    return 1;
  }
}

# wrapper function arounf the dump and comp attr functions
sub check_attr($$) {
  my $a = $_[0];
  my $b = $_[1];

  if ($opt_D) {
    return dump_attr($a,$b);
  } else {
    return comp_attr($a,$b);
  }
}

#
# Test functions
#

sub test_noops {
  my $niters = $_[0];
  my $ofail = $fail;
  my $result;
  my @null_args = ("$prog", "null", $server, "NASD_SUCCESS");
  
  if ($opt_v) { print "Null:\n"; }
  else { print "Null"; }

  for (my $i = 0; $i < $niters; $i++) {
    my $j = $i + 1;
    $result = `@null_args 2>&1`;    
    if ($?) {
      print "failed:\n\n$result\n\n";
      $fail++;
      return;
    } elsif ($opt_v) { print "Iteration $j succeeded:\n$result\n"; }
    else { print "."; }
  }

  if($ofail == $fail){
    if ($opt_v) { print "Test passed.\n\n"; }
    else { print " passed\n"; }
  }
  else {
    print "Failed.\n";
  }
}

sub test_mount {
  my $result;
  my $ofail = $fail;
  my @mount_args = ("$prog", "mount", $server, "", "");
  
  if ($opt_v) { print "Mount:\n"; }
  else { print "Mount"; }

  # check if an invalid partition will mount
  if ($opt_v) { print "Tesing NULL partition name..."; }
  @mount_args[3] = "NASD_EDRFS_BAD_NAME";
  @mount_args[4] = "\"\"";
  $result = `@mount_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed NULL partition name.\n"; }
    else { print "."; }
  }

  #check for a non-existant partition name
  if ($opt_v) { print "Tesing non-existant partition name..."; }
  if ($partname eq "/mengsiqi") {
    @mount_args[4] = "/mengzhanglao";
  } else {
    @mount_args[4] = "/mengsiqi";
  }
  $result = `@mount_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed non-existant partition name.\n"; }
    else { print "."; }
  }
  
  # mount the real root partition
  if ($opt_v) { print "Tesing root partition name..."; }
  @mount_args[3] = "NASD_SUCCESS";
  @mount_args[4] = $partname;
  $result = `@mount_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
    return;
  }
  if ($result =~ /$idscan/) {
    $rootid = $1;
  } else {
    print "Couldn't get id from:\n$result\n";
    return;
  }
  if ($result =~ /$partscan/) {
    $partnum = $1;
  } else {
    print "Couldn't get partition number from:\n$result\n";
    return;
  }

  if($ofail == $fail){
    if ($opt_v) { print "Passed.  Working on ID $rootid\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}

sub test_fsstat {
  my $result;
  my $ofail = $fail;
  my @fsstat_args = ("$prog", "fsstat", $server, "", "");

  if ($opt_v) { print "Fsstat:\n"; }
  else { print "Fsstat"; }
  
  # check if an invalid partition will fsstat
  if ($opt_v) { print "Tesing invalid directory..."; }
  @fsstat_args[3] = "NASD_BAD_IDENTIFIER";
  @fsstat_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  $result = `@fsstat_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an valid partition will fsstat
  if ($opt_v) { print "Tesing valid directory..."; }
  @fsstat_args[3] = "NASD_SUCCESS";
  @fsstat_args[4] = $rootid;
  $result = `@fsstat_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed valid directory test.\n"; }
    else { print "."; }
  }

  if($ofail == $fail){
    if ($opt_v) { print "All fsstat tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_lookup {
  my $fileid1;
  my $ofail = $fail;
  my $result;
  my @lookup_args = ("$prog", "lookup", $server, "", "", "");

  if ($opt_v) { print "Lookup:\n"; }
  else { print "Lookup"; }
  
  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch files..."; }  
  $fileid1 = create_file($rootid, "lookup1");
  if (!$fileid1){
    if ($opt_v) { print "failed!\n"; }
    else { print "Scratch file creation failed!\n"; }
    do_warning();
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # check if an invalid directory lookup
  if ($opt_v) { print "Tesing invalid directory lookup..."; }
  @lookup_args[3] = "NASD_BAD_IDENTIFIER";
  @lookup_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  @lookup_args[5] = "lookup1";
  $result = `@lookup_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an invalid file lookup
  if ($opt_v) { print "Tesing invalid file lookup..."; }
  @lookup_args[3] = "NASD_EDRFS_BAD_NAME";
  @lookup_args[4] = $rootid;
  @lookup_args[5] = "mengsiqi"; # A fun name
  $result = `@lookup_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid file test.\n"; }
    else { print "."; }
  }

  # check if an valid file will lookup
  if ($opt_v) { print "Tesing valid file lookup..."; }
  @lookup_args[3] = "NASD_SUCCESS";
  @lookup_args[4] = $rootid;
  @lookup_args[5] = "lookup1";
  $result = `@lookup_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed valid file lookup test.\n"; }
    else { print "."; }
  }

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($rootid, "lookup1");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All lookup tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


#
# We don't do a good set of tests on readdir because it will be going
# away and replaced by client side directory parseing.
#
sub test_readdir {
  my $ofail = $fail;
  my $result;
  my @readdir_args = ("$prog", "readdir", $server, "", "", "0", "0", "1");

  if ($opt_v) { print "Readdir:\n"; }
  else { print "Readdir"; }

  # check if an invalid directory will readdir
  if ($opt_v) { print "Tesing invalid directory readdir..."; }
  @readdir_args[3] = "NASD_BAD_IDENTIFIER";
  @readdir_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  $result = `@readdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }
  # check if reading negative directorty entries work
  if ($opt_v) { print "Tesing negative number of directory entries..."; }
  @readdir_args[7] = "-1";
  $result = `@readdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    $result =~ /Count ([0-9]+)/;
    if($1 ne "0"){
      print " failed: invalid number of directories ($1).\n\n$result\n\n";
      $fail++;
    }
    else {
      if ($opt_v) { print "Passed negative number of directories test.\n"; }
      else { print "."; }
    }
  }

  # check if an valid file directory readdir
  if ($opt_v) { print "Tesing valid single readdir..."; }
  @readdir_args[3] = "NASD_SUCCESS";
  @readdir_args[4] = $rootid;
  @readdir_args[7] = "1";
  $result = `@readdir_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    $result =~ /Count ([0-9]+)/;
    if($1 ne "1"){
      print " failed: invalid number of directories ($1).\n\n$result\n\n";
      $fail++;
    }
    else{
      if ($opt_v) { print "Passed valid single readdir test.\n"; }
      else { print "."; }
    }
  }

  # check if an multi readdir
  if ($opt_v) { print "Tesing valid multi readdir..."; }
  @readdir_args[7] = "1024";
  $result = `@readdir_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    $result =~ /Count ([0-9]+)/;
    if($1 ne "2"){
      print " Failed: invalid number of directories ($1).\n\n$result\n\n";
      $fail++;
    }
    else{
      if ($opt_v) { print "Passed valid multi readdir test.\n"; }
      else { print "."; }
    }
  }

  if($ofail == $fail){
    if ($opt_v) { print "All readdir tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_setattr {
  my $fileid1;
  my $ofail = $fail;
  my $result;
  my @setattr_args = ("$prog", "setattr", $server, "", "", 0, 1, 2, 3, 4);

  if ($opt_v) { print "Setattr:\n"; }
  else { print "Setattr"; }

  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch files..."; }  
  $fileid1 = create_file($rootid, "setattr1");
  if (!$fileid1){
    if ($opt_v) { print "failed!\n"; }
    else { print "Scratch file creation failed!\n"; }
    do_warning();
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # check if an invalid directory setattr
  if ($opt_v) { print "Tesing invalid directory setattr..."; }
  @setattr_args[3] = "NASD_BAD_IDENTIFIER";
  @setattr_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  $result = `@setattr_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask 1
  if ($opt_v) { print "Tesing invalid fieldmask 1..."; }
  @setattr_args[3] = "NASD_OP_NOT_SUPPORTED";
  @setattr_args[4] = $fileid1;
  @setattr_args[5] = 0x09000000; # FS_SPECIFC | EDRFS_NLINK, could change
  $result = `@setattr_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask 1.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask 2
  if ($opt_v) { print "Tesing invalid fieldmask 2..."; }
  @setattr_args[3] = "NASD_OP_NOT_SUPPORTED";
  @setattr_args[5] = 0x03000000; # FS_SPECIFC | EDRFS_TYPE, could change
  $result = `@setattr_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask 2.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask 3
  if ($opt_v) { print "Tesing invalid fieldmask 3..."; }
  @setattr_args[3] = "NASD_BAD_FIELDMASK";
  @setattr_args[5] = 0x48000000; # FS_SPECIFC | ???, could change
  $result = `@setattr_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask 4.\n"; }
    else { print "."; }
  }

  check_attr("test_data/setattr1", nasdid($fileid1));

  # check if an file setattr works
  if ($opt_v) { print "Tesing file setattr..."; }
  @setattr_args[3] = "NASD_SUCCESS";
  @setattr_args[5] = 0x31000000; # FS_SPECIFIC | EDRFS_GID | EDFRS_UID
  $result = `@setattr_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed file test.\n"; }
    else { print "."; }
  }

  check_attr("test_data/setattr2", nasdid($fileid1));

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($rootid, "setattr1");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All setattr tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_create {
  my $ofail = $fail;
  my $result;
  my @create_args = ("$prog", "create", $server, "", $rootid, "", "0");

  if ($opt_v) { print "Create:\n"; }
  else { print "Create"; }

  # check if an invalid name
  if ($opt_v) { print "Tesing invalid name..."; }
  @create_args[3] = "NASD_EDRFS_BAD_NAME";
  @create_args[5] = "\"\"";
  $result = `@create_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid name.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask
  if ($opt_v) { print "Tesing invalid fieldmask..."; }
  @create_args[3] = "NASD_OP_NOT_SUPPORTED";
  @create_args[5] = "create1";
  @create_args[6] = 0x09000000; # FS_SPECIFC | EDRFS_NLINK, could change
  $result = `@create_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask 2
  if ($opt_v) { print "Tesing invalid fieldmask 2..."; }
  @create_args[3] = "NASD_BAD_FIELDMASK";
  @create_args[6] = 0x48000000; # FS_SPECIFC | ???, could change
  $result = `@create_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask 2.\n"; }
    else { print "."; }
  }

  # check if an invalid directory create
  if ($opt_v) { print "Tesing invalid directory create..."; }
  @create_args[3] = "NASD_BAD_IDENTIFIER";
  @create_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  @create_args[5] = "create1";
  @create_args[6] = 0;
  $result = `@create_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an file create works
  if ($opt_v) { print "Tesing file create..."; }
  @create_args[3] = "NASD_SUCCESS";
  @create_args[4] = $rootid;
  @create_args[5] = "create1";
  $result = `@create_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed file test.\n"; }
    else { print "."; }
  }

  check_attr("test_data/create1",nasdid(lookup_file($rootid, "create1")));

  # check if an duplicate file will create
  if ($opt_v) { print "Tesing duplicate file create..."; }
  @create_args[3] = "NASD_EDRFS_ALREADY_EXISTS";
  $result = `@create_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed dubplicate file create test.\n"; }
    else { print "."; }
  }

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($rootid, "create1");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All create tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_symlink {
  my $fileid1;
  my $ofail = $fail;
  my $result;
  my @symlink_args = ("$prog", "symlink", $server, "", $rootid, "", "symink1",
		     "0", "0", "0");

  if ($opt_v) { print "Symlink:\n"; }
  else { print "Symlink"; }
  
  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch files..."; }  
  $fileid1 = create_file($rootid, "symlink1");
  if (!$fileid1){
    if ($opt_v) { print "failed!\n"; }
    else { print "Scratch file creation failed!\n"; }
    do_warning();
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # check if an invalid dirpath name
  if ($opt_v) { print "Tesing invalid dirpath name..."; }
  @symlink_args[3] = "NASD_EDRFS_BAD_NAME";
  @symlink_args[5] = "\"\"";
  $result = `@symlink_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid dirpath name.\n"; }
    else { print "."; }
  }

  # check if an invalid directory symlink
  if ($opt_v) { print "Tesing invalid directory symlink..."; }
  @symlink_args[3] = "NASD_BAD_IDENTIFIER";
  @symlink_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  @symlink_args[5] = "symlink2";
  $result = `@symlink_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an file symlink works
  if ($opt_v) { print "Tesing file symlink..."; }
  @symlink_args[3] = "NASD_SUCCESS";
  @symlink_args[4] = $rootid;
  $result = `@symlink_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed file test.\n"; }
    else { print "."; }
  }

  check_attr("test_data/symlink1", nasdid(lookup_file($rootid, "symlink2")));

  # check if an file symlink works
  if ($opt_v) { print "Tesing file symlink to non existant..."; }
  @symlink_args[5] = "symlink3";
  @symlink_args[6] = "\"\"";
  $result = `@symlink_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed file test.\n"; }
    else { print "."; }
  }

  check_attr("test_data/symlink2", nasdid(lookup_file($rootid, "symlink3")));

  # check if an duplicate file will symlink
  if ($opt_v) { print "Tesing duplicate file symlink to non existant..."; }
  @symlink_args[3] = "NASD_EDRFS_ALREADY_EXISTS";
  $result = `@symlink_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed duplicate file symlink test.\n"; }
    else { print "."; }
  }

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($rootid, "symlink1");
  remove_file($rootid, "symlink2");
  remove_file($rootid, "symlink3");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All symlink tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_remove {
  my $fileid1;
  my $ofail = $fail;
  my $result;
  my @remove_args = ("$prog", "remove", $server, "", $rootid, "remove1");

  if ($opt_v) { print "Remove:\n"; }
  else { print "Remove"; }
  
  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch files..."; }  
  $fileid1 = create_file($rootid, "remove1");
  if (!$fileid1){
    if ($opt_v) { print " failed!\n"; }
    else { print "Scratch file creation failed!\n"; }
    do_warning();
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # check if an invalid directory remove
  if ($opt_v) { print "Tesing invalid directory remove..."; }
  @remove_args[3] = "NASD_BAD_IDENTIFIER";
  @remove_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  $result = `@remove_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an invalid dirpath name
  if ($opt_v) { print "Tesing invalid dirpath name..."; }
  @remove_args[3] = "NASD_EDRFS_BAD_NAME";
  @remove_args[4] = $rootid;
  @remove_args[5] = "\"\"";
  $result = `@remove_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid dirpath name.\n"; }
    else { print "."; }
  }

  # check if an can remove a directory
  if ($opt_v) { print "Tesing remove a directory..."; }
  @remove_args[3] = "NASD_EDRFS_IS_DIR";
  @remove_args[5] = ".";
  $result = `@remove_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed remove a directory.\n"; }
    else { print "."; }
  }

  # check if an file remove works
  if ($opt_v) { print "Tesing file remove..."; }
  @remove_args[3] = "NASD_SUCCESS";
  @remove_args[4] = $rootid;
  @remove_args[5] = "remove1";
  $result = `@remove_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed file test.\n"; }
    else { print "."; }
  }

  if($ofail == $fail){
    if ($opt_v) { print "All remove tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_mkdir {
  my $ofail = $fail;
  my $result;
  my @mkdir_args = ("$prog", "mkdir", $server, "", $rootid, "", "0", "0",
		    "0", "0");

  if ($opt_v) { print "Mkdir:\n"; }
  else { print "Mkdir"; }

  # check if an invalid name
  if ($opt_v) { print "Tesing invalid name..."; }
  @mkdir_args[3] = "NASD_EDRFS_BAD_NAME";
  @mkdir_args[5] = "\"\"";
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid name.\n"; }
    else { print "."; }
  }

  # check if an invalid directory mkdir
  if ($opt_v) { print "Tesing invalid directory mkdir..."; }
  @mkdir_args[3] = "NASD_BAD_IDENTIFIER";
  @mkdir_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  @mkdir_args[5] = "mkdir1";
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask
  if ($opt_v) { print "Tesing invalid fieldmask..."; }
  @mkdir_args[3] = "NASD_OP_NOT_SUPPORTED";
  @mkdir_args[4] = $rootid;
  @mkdir_args[6] = 0x09000000; # FS_SPECIFC | EDRFS_NLINK, could change
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask.\n"; }
    else { print "."; }
  }

  # check if an invalid fieldmask 2
  if ($opt_v) { print "Tesing invalid fieldmask 2..."; }
  @mkdir_args[3] = "NASD_BAD_FIELDMASK";
  @mkdir_args[6] = 0x48000000; # FS_SPECIFC | ??, could change
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid fieldmask 2.\n"; }
    else { print "."; }
  }

  # check if an file mkdir works
  if ($opt_v) { print "Tesing mkdir..."; }
  @mkdir_args[3] = "NASD_SUCCESS";
  @mkdir_args[4] = $rootid;
  @mkdir_args[6] = 0;
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed mkdir test.\n"; }
    else { print "."; }
  }

  check_attr("test_data/mkdir1", nasdid(lookup_file($rootid, "mkdir1")));

  # check if an duplicate file will mkdir
  if ($opt_v) { print "Tesing duplicate file mkdir..."; }
  @mkdir_args[3] = "NASD_EDRFS_ALREADY_EXISTS";
  $result = `@mkdir_args 2>&1`;
  if ($?) {
    print "failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed dubplicate file mkdir test.\n"; }
    else { print "."; }
  }

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  rm_dir($rootid, "mkdir1");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All mkdir tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_rmdir {
  my $dirid1;
  my $fileid1;
  my $ofail = $fail;
  my $result;
  my @rmdir_args = ("$prog", "rmdir", $server, "", "", "rmdir1");

  if ($opt_v) { print "Rmdir:\n"; }
  else { print "Rmdir"; }
  
  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch directories..."; }  
  $dirid1 = mk_dir($rootid, "rmdir1");
  $fileid1 = create_file($rootid, "rmdir2");
  create_file($dirid1, "rmdir1.1");
  if (!$dirid1 || !$fileid1){
    if ($opt_v) { print " failed!\n"; }
    else { print "Scratch file creation failed!\n"; }
    do_warning();
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # check if an invalid directory rmdir
  if ($opt_v) { print "Tesing invalid directory rmdir..."; }
  @rmdir_args[3] = "NASD_BAD_IDENTIFIER";
  @rmdir_args[4] = "0x0,0x1,0x1"; # could change if nasd_types does
  $result = `@rmdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid directory test.\n"; }
    else { print "."; }
  }

  # check if an invalid dirpath name
  if ($opt_v) { print "Tesing invalid dirpath name..."; }
  @rmdir_args[3] = "NASD_EDRFS_BAD_NAME";
  @rmdir_args[4] = $rootid;
  @rmdir_args[5] = "\"\"";
  $result = `@rmdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed invalid dirpath name.\n"; }
    else { print "."; }
  }

  # check if an can rmdir a file
  if ($opt_v) { print "Tesing rmdir a file..."; }
  @rmdir_args[3] = "NASD_EDRFS_NOT_DIR";
  @rmdir_args[5] = "rmdir2";
  $result = `@rmdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed rmdir a file.\n"; }
    else { print "."; }
  }

  # check if an can rmdir a non empty directory
  if ($opt_v) { print "Tesing rmdir a non empty directory..."; }
  @rmdir_args[3] = "NASD_EDRFS_DIR_NOT_EMPTY";
  @rmdir_args[5] = "rmdir1";
  $result = `@rmdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed rmdir a non empty directory.\n"; }
    else { print "."; }
  }

  remove_file($dirid1, "rmdir1.1");

  # check if an file rmdir works
  if ($opt_v) { print "Testing good rmdir..."; }
  @rmdir_args[3] = "NASD_SUCCESS";
  $result = `@rmdir_args 2>&1`;
  if ($?) {
    print " failed:\n\n$result\n\n";
    $fail++;
  }
  else {
    if ($opt_v) { print "Passed good rmdir test.\n"; }
    else { print "."; }
  }

  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($rootid, "rmdir2");
  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All rmdir tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}


sub test_rename {
  my $ofail = $fail;
  my $result;
  my $i;
  my $fileid1;
  my $fileid2;
  my $dirid1;
  my $dirid2;
  my $dirid3;
  my $dirid4;
  my @rename_args = ("$prog", "rename", $server, "",
		     $rootid, $rootid, "", "");

  if ($opt_v) { print "Rename:\n"; }
  else { print "Rename"; }

  # setup a couple of files for later tests
  if ($opt_v) { print "Setting up some scratch files..."; }  
  $fileid1 = create_file($rootid, "rename1");
  $fileid2 = create_file($rootid, "rename2");
  $dirid1 = mk_dir($rootid, "renamedir1");
  $dirid2 = mk_dir($rootid, "renamedir2");
  $dirid3 = mk_dir($rootid, "renamedir3");
  $dirid4 = mk_dir($dirid3, "renamedir3.1");
  create_file($dirid3, "rename3.1");
  if (!$fileid1 || !$fileid2 || !$dirid1){
    if ($opt_v) { print "failed!\n"; }
    else { print "Scratch file creation failed!\n"; do_warning();}
  }
  if ($opt_v) { print "done\n"; }
  else { print "." }

  # to_path is "."
  if ($opt_v) { print "to_path is \".\"..."; }
  @rename_args[3] = "NASD_EDRFS_BAD_NAME";
  @rename_args[6] = "rename1";
  @rename_args[7] = ".";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path is \".\" failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }


  # to_path too short (fails)
  if ($opt_v) { print "in_to_path too short..."; }
  @rename_args[7] = "\"\"";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path too short failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # from_path is "."
  if ($opt_v) { print "from_path is \".\"..."; }
  @rename_args[6] = ".";
  @rename_args[7] = "rename2";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "from_path is \".\" failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }


  # from_path too short (fails)
  if ($opt_v) { print "in_from_path too short..."; }
  @rename_args[6] = "\"\"";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "from_path too short failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # from_directory doesn't exist (fails)
  if ($opt_v) { print "from_directory doesn't exist..."; }
  @rename_args[3] = "NASD_BAD_IDENTIFIER";
  @rename_args[4] = "0x0,0x1,0x1"; # This could change if nasd_types does.
  @rename_args[6] = "rename1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "from_directory doesn't exist failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # from_directory exists, is file (fails)
  if ($opt_v) { print "from_directory is file..."; }
  @rename_args[3] = "NASD_EDRFS_NOT_DIR";
  @rename_args[4] = $fileid1;
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "from_directory is file failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }


  # to_directory doesn't exist (fails)
  if ($opt_v) { print "to_directory doesn't exist..."; }
  @rename_args[3] = "NASD_BAD_IDENTIFIER";
  @rename_args[4] = $rootid;
  @rename_args[5] = "0x0,0x1,0x1"; # This could change
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_directory doesn't exist failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # to_directory exists, is file (fails)
  if ($opt_v) { print "to_directory is file..."; }
  @rename_args[3] = "NASD_EDRFS_NOT_DIR";
  @rename_args[5] = $fileid1;
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_directory is file failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # from_path doesn't exist (fails)
  if ($opt_v) { print "from_path doesn't exist..."; }
  @rename_args[3] = "NASD_EDRFS_BAD_NAME";
  @rename_args[5] = $rootid;
  @rename_args[6] = "dumb";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "from_path doesn't exist failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # to_path exists, is directory, from_path is file (fails)
  if ($opt_v) { print "to_path is directory, from_path is file..."; }
  @rename_args[3] = "NASD_EDRFS_ALREADY_EXISTS";
  @rename_args[6] = "rename1";
  @rename_args[7] = "renamedir1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path is directory, from_path is file failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # to_path exists, is file, from_path is directory (fails)
  if ($opt_v) { print "to_path is file, from_path is directory..."; }
  @rename_args[3] = "NASD_EDRFS_ALREADY_EXISTS";
  @rename_args[6] = "renamedir1";
  @rename_args[7] = "rename1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path is file, from_path is directory failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # to_path is nonempty directory, from_path is directory (fails)
  if ($opt_v) {
    print "to_path is nonempty directory, from_path is directory...";
  }
  @rename_args[3] = "NASD_EDRFS_DIR_NOT_EMPTY";
  @rename_args[6] = "renamedir1";
  @rename_args[7] = "renamedir3";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path is nonempty directory, from_path is directory failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }


  # to_path is empty directory, from_path is directory
  if ($opt_v) {
    print "to_path is empty directory, directorys...";
  }
  @rename_args[3] = "NASD_SUCCESS";
  @rename_args[6] = "renamedir1";
  @rename_args[7] = "renamedir2";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "to_path is empty directory, directorys failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename1", nasdid(lookup_file($rootid, "renamedir2")));

  # same directory, to_path exists, files.
  if ($opt_v) {
    print "same directory, to_path exists, files...";
  }
  @rename_args[6] = "rename1";
  @rename_args[7] = "rename2";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "same directory, to_path exists, files failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename2", nasdid(lookup_file($rootid, "rename2")));


  # same directory, to_path does not exist, files.
  if ($opt_v) {
    print "same directory, to_path does not exist, files...";
  }
  @rename_args[6] = "rename2";
  @rename_args[7] = "rename1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "same directory, to_path does not exist, files failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename3", nasdid(lookup_file($rootid, "rename1")));


  # diff directory, to_path is empty directory.
  if ($opt_v) {
    print "different directory, to_path is empty directory...";
  }
  @rename_args[5] = $dirid3;
  @rename_args[6] = "renamedir2";
  @rename_args[7] = "renamedir3.1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "different directory, to_path is empty directory failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename4", nasdid(lookup_file($dirid3, "renamedir3.1")));


  # diff directory, to_path is empty file.
  if ($opt_v) {
    print "different directory, to_path is empty file...";
  }
  @rename_args[6] = "rename1";
  @rename_args[7] = "rename3.1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "different directory, to_path is empty file failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename5", nasdid(lookup_file($dirid3, "rename3.1")));


  # same file
  if ($opt_v) {
    print "same file...";
  }
  @rename_args[4] = $dirid3;
  @rename_args[6] = "rename3.1";
  @rename_args[7] = "rename3.1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "same file failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename6", nasdid(lookup_file($dirid3, "rename3.1")));


  # same empty directory
  if ($opt_v) {
    print "same empty directory...";
  }
  @rename_args[6] = "renamedir3.1";
  @rename_args[7] = "renamedir3.1";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "same empty directory failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename7", nasdid(lookup_file($dirid3, "renamedir3.1")));


  # same nonempty directory
  if ($opt_v) {
    print "same nonempty directory...";
  }
  @rename_args[4] = $rootid;
  @rename_args[5] = $rootid;
  @rename_args[6] = "renamedir3";
  @rename_args[7] = "renamedir3";
  $result = `@rename_args 2>&1`;
  if ($?) {
    print "same nonempty directory failed:\n\n$result\n\n";
    $fail++;
    goto rename_cleanup;
  }
  if ($opt_v) { print "passed\n"; }
  else { print "." }

  check_attr("test_data/rename9", nasdid(lookup_file($rootid, "renamedir3")));

 rename_cleanup:
  #Need to cleanup the files we created and used
  if ($opt_v) { print "Cleaning up scratch files..."; }  
  remove_file($dirid3, "rename3.1");
  rm_dir($dirid3, "renamedir3.1");
  rm_dir($rootid, "renamedir3");

  if ($opt_v) { print "done\n"; }

  if($ofail == $fail){
    if ($opt_v) { print "All rename tests passed.\n"; }
    else { print " passed\n"; }
  }
  else {
    printf "Failed.\n";
  }
}
