#!/usr/bin/perl -w
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2010.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Nikolay.Kutovskiy@jinr.ru
#
# DESCRIPTION:
# This test checks if the perl API lfc_rename function is able to rename a LFC
# file or directory in the name server.
#
##############################################################################
# meta: proxy=True
# meta: preconfig=../../LFC-config
use strict;
use lfc;
use TestCommon;

my ($name,$res,$error,$guid,$err_num,$err_string,$new_name);
test_title("lfc-rename");
# defining VO name
my $vo = $ENV{VO};

# initializing $error variable used for detecting test errors
$error = 0;

# checking the possibility to rename dir
action_title("Checking the possibility to rename dir");
# creating new dir
# defining unique dir name with help of current number of seconds since epoch
my $t = time();
$name = "/grid/$vo/perl_renamedir_test_"."$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/perl_renamedir_test_"."$t";
$res = lfc::lfc_mkdir($name,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $name creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  $new_name = $name."_renamed";
  # uncomment the line below to check if test prints the proper message in case of failure
  #$name = "/non-existing_dir_perl_testdir";
  $res = lfc::lfc_rename($name,$new_name);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("There was an error during dir $name renaming to $new_name: Error $err_num ($err_string)");
    $error = 1;
    # remove dir if it has been created but not renamed
    remove_dir($name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error $err_num ($err_string)\n";
    action_ok();
    # remove renamed dir
    remove_dir($new_name);
  }
}


# checking the possibility to rename file
action_title("Checking the possibility to rename file");
$guid = gen_guid();
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_filerename_test_"."$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/perl_filerename_test_"."$t";
$res = lfc::lfc_creatg($name,$guid,0664);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during file $name creation: Error $err_num ($err_string)");
  $error =1;
} else {
  # checking the status of created file
  my $statg = lfcc::new_lfc_filestatg();
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = $guid."$t";
  $res = lfc::lfc_statg(undef,$guid,$statg);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("There was an error during getting the status of  $name : Error $err_num ($err_string)");
    $error =1;
  } else {
    # create a new name what the existing file should be renamed to
    $new_name = $name."_renamed";
    #print "\$new_name: $new_name\n";
    # uncomment the line below to check if test prints the proper message in case of failure
    #$name = "/perl_filerename_test_"."$t";
    $res = lfc::lfc_rename($name,$new_name);
    if ($res != 0) {
      $err_num = $lfc::serrno;
      $err_string = lfc::sstrerror($err_num);
      action_failed();
      action_err_msg("There was an error during file $name renaming to $new_name: Error $err_num ($err_string)");
      $error = 1;
    } else {
      action_ok();
      remove_file($new_name);
    }
  }
}

# checking the case when a component of oldpath prefix does not exist (ENOENT)
action_title("Checking the case when a component of oldpath prefix does not exist (ENOENT)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_renamedir_test_"."$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/grid/$vo";
# checking if dir does not exist
my $stat = lfcc::new_lfc_filestat();
$res = lfc::lfc_stat($name,$stat);
if ($res == 0) {
  action_failed();
  action_err_msg("Test dir $name exists and hence subtest can not check if lfc_rename function behaves properly in case of (ENOENT) error\n");
  $error = 1;
} else {
  $new_name = $name."_renamed";
  $res = lfc::lfc_rename($name,$new_name);
  if ($res == 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("Test could rename non-existing dir $name to $new_name");
    $error = 1;
    # remove renamed dir
    remove_dir($new_name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error $err_num ($err_string)\n";
    action_ok();
  }
  # no action here since there is very little probability that $name exists
}

# checking the case when a component of oldpath prefix is null pathname (ENOENT)
action_title("Checking the case when a component of oldpath prefix is null pathname (ENOENT)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$new_name = "/grid/$vo/perl_renamedir_test_$t"."_renamed";
$name = "";
$res = lfc::lfc_rename($name,$new_name);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename dir with null pathname to $new_name");
  $error = 1;
  # remove renamed dir
  remove_dir($new_name);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error $err_num ($err_string)\n";
  action_ok();
}

# checking the case when a component of newpath prefix is null pathname (ENOENT)
action_title("Checking the case when a component of newpath prefix is null pathname (ENOENT)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_renamedir_test_$t";
$new_name = "";
$res = lfc::lfc_rename($name,$new_name);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename dir $name to one with null pathname");
  $error = 1;
  # remove renamed dir
  remove_dir($new_name);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error $err_num ($err_string)\n";
  action_ok();
}

# checking the case when a search permission is denied on any component of oldpath (EACCES)
action_title("Checking the case when a search permission is denied on any component of oldpath (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
my $dir_1 = "/grid/$vo/perl_rename_test_$t";
my $dir_2 = $dir_1."/test_dir";
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  lfc::lfc_mkdir($dir_2,0755);
  lfc::lfc_chmod($dir_1,0000);
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($dir_1,0755);
  $new_name = $dir_2."_renamed";
  $res = lfc::lfc_rename($dir_2,$new_name);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename the dir $dir_2 to $new_name although search permission on a component of oldpath was denied");
    $error = 1;
    remove_dir($new_name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    # removing $dir_2
    # changing $dir permissions first
    $res = lfc::lfc_chmod($dir_1,0755);
    if ($res != 0) {
      $err_num = $lfc::serrno;
      $err_string = lfc::sstrerror($err_num);
      action_failed();
      action_err_msg("Failed to change mode for dir $dir_1 from 0000 to 0755: Error $err_num ($err_string)");
    $error = 1;
    } else {
      remove_dir($dir_2);
    }
  }
  # removing $dir_1
  remove_dir($dir_1);
}

# checking the case when a write permission is denied on any component of oldpath (EACCES)
action_title("Checking the case when a write permission is denied on any component of oldpath (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$dir_1 = "/grid/$vo/perl_rename_EACCES_test_$t";
$dir_2 = $dir_1."/test_dir";
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  lfc::lfc_mkdir($dir_2,0755);
  lfc::lfc_chmod($dir_1,0444);
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($dir_1,0755);
  $new_name = $dir_2."_renamed";
  $res = lfc::lfc_rename($dir_2,$new_name);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename the dir $dir_2 to $new_name although write permission on a component of oldpath was denied");
    $error = 1;
    remove_dir($new_name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    # removing $dir_2
    # changing $dir permissions first
    $res = lfc::lfc_chmod($dir_1,0755);
    if ($res != 0) {
      $err_num = $lfc::serrno;
      $err_string = lfc::sstrerror($err_num);
      action_failed();
      action_err_msg("Failed to change mode for dir $dir_1 from 0444 to 0755: Error $err_num ($err_string)");
    $error = 1;
    } else {
      remove_dir($dir_2);
    }
  }
  # removing $dir_1
  remove_dir($dir_1);
}

# checking the case when a search permission is denied on any component of newpath (EACCES)
action_title("Checking the case when a search permission is denied on any component of newpath (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$dir_1 = "/grid/$vo/perl_rename_EACCES_test_dir1_$t";
$dir_2 = "/grid/$vo/perl_rename_EACCES_test_dir2_$t";
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  lfc::lfc_mkdir($dir_2,0444);
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($dir_2,0755);
  $new_name = $dir_2."/perl_testdir1_renamed";
  $res = lfc::lfc_rename($dir_1,$new_name);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename the dir $dir_1 to $new_name although search permission on a component of newpath was denied");
    $error = 1;
    remove_dir($new_name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    remove_dir($dir_1);
  }
  $res = lfc::lfc_chmod($dir_2,0755);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("Failed to change mode for dir $dir_2 from 0444 to 0755: Error $err_num ($err_string)");
    $error = 1;
  } else {
  # removing $dir_2
  remove_dir($dir_2);
  }
}

# checking the case when a write permission on the parent directory is denied on newpath (EACCES)
action_title("Checking the case when a write permission on the parent directory is denied on newpath (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_rename_test_$t";
$res = lfc::lfc_mkdir($name,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $name creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  $new_name = "/perl_rename_test";
  # uncomment the line below to check if test prints the proper message in case of failure
  #$new_name = "/grid/$vo/perl_renamed_test_dir";
  $res = lfc::lfc_rename($name,$new_name);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename the dir $name to $new_name when write permission on the parent dir was denied on newpath");
    $error = 1;
    remove_dir($new_name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    remove_dir($name);
  }
}

# checking the case when oldpath is null pointer (EFAULT)
action_title("Checking the case when oldpath is null pointer (EFAULT)");
$name = undef;
$new_name = "/grid/$vo/perl_rename_test_EFAULT";
$res = lfc::lfc_rename($name,$new_name);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename the dir $name to $new_name when oldpath was a null pointer");
  $error = 1;
  remove_dir($new_name);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
}

# checking the case when newpath is null pointer (EFAULT)
action_title("Checking the case when newpath is null pointer (EFAULT)");
$new_name = undef;
$name = "/grid/$vo/perl_rename_test_EFAULT";
$res = lfc::lfc_rename($name,$new_name);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename the dir $name to $new_name when newpath was a null pointer");
  $error = 1;
  remove_dir($new_name);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
}

# checking the case when newpath is an existing dir and is not empty (EEXIST)
action_title("Checking the case when newpath is an existing directory and is not empty (EEXIST)");
$t = time();
$dir_1 = "/grid/$vo/perl_rename_testdir1_EFAULT_$t";
$dir_2 = "/grid/$vo/perl_rename_testdir2_EFAULT_$t";
$name = $dir_2."/perl_rename_testdir3_EFAULT";
lfc::lfc_mkdir($dir_1,0755);
lfc::lfc_mkdir($dir_2,0755);
lfc::lfc_mkdir($name,0755);
# uncomment the line below to check if test prints the proper message in case of failure
#remove_dir($name);
$res = lfc::lfc_rename($dir_1,$dir_2);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename the dir $dir_1 to $dir_2 when newpath was an existing directory and was not empty");
  $error = 1;
  remove_dir($name);
  remove_dir($dir_2);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
  remove_dir($name);
  remove_dir($dir_2);
  remove_dir($dir_1);
}

# checking the case when a component of newpath prefix is not a directory (ENOTDIR)
action_title("Checking the case when a component of newpath prefix is not a directory (ENOTDIR)");
# creating a test file which will be a part of the new dir path
$t = time();
$name = "/grid/$vo/perl_rename_testfile_ENOTDIR_$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/perl_filerename_test_"."$t";
$guid = gen_guid();
$res = lfc::lfc_creatg($name,$guid,0664);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during file $name creation: Error $err_num ($err_string)");
  $error =1;
} else {
  # checking the status of created file
  my $statg = lfcc::new_lfc_filestatg();
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = gen_guid();
  $res = lfc::lfc_statg(undef,$guid,$statg);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("There was an error during getting the status of  $name : Error $err_num ($err_string)");
    $error =1;
  } else {
    # renaming $dir_1 to $dir_2 what includes file in its pathname
    $dir_1 = "/grid/$vo/perl_rename_testdir1_ENOTDIR_$t";
    lfc::lfc_mkdir($dir_1,0755);
    $dir_2 = $name."/renamed_dir";
    # uncomment the line below to check if test prints the proper message in case of failure
    #$dir_2 = "/grid/$vo/perl_rename_testdir2_ENOTDIR_$t";
    $res = lfc::lfc_rename($dir_1,$dir_2);
    if ($res == 0) {
      action_failed();
      action_err_msg("Test could rename dir $dir_1 to $dir_2 a component of which path prefix was not a directory");
      remove_dir($dir_2);
      $error = 1;
    } else {
      # uncomment three lines below to see if the error is correct
      #$err_num = $lfc::serrno;
      #$err_string = lfc::sstrerror($err_num);
      #print "Error $err_num ($err_string)\n";
      action_ok();
      remove_dir($dir_1);
    }
  }
  remove_file($name);
}

# checking the case when oldpath is a directory and newpath is an existing regular file (ENOTDIR)
action_title("Checking the case when oldpath is a directory and newpath is an existing regular file (ENOTDIR)");
# creating a test file which test dir will be renamed to
$t = time();
$name = "/grid/$vo/perl_rename_testfile_ENOTDIR_$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/perl_filerename_test_"."$t";
$guid = gen_guid();
$res = lfc::lfc_creatg($name,$guid,0664);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during file $name creation: Error $err_num ($err_string)");
  $error =1;
} else {
  # checking the status of created file
  my $statg = lfcc::new_lfc_filestatg();
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = gen_guid();
  $res = lfc::lfc_statg(undef,$guid,$statg);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("There was an error during getting the status of $name : Error $err_num ($err_string)");
    $error =1;
  } else {
    # renaming dir $dir_1 to file $name
    $dir_1 = "/grid/$vo/perl_rename_testdir1_ENOTDIR_$t";
    lfc::lfc_mkdir($dir_1,0755);
    $res = lfc::lfc_rename($dir_1,$name);
    if ($res == 0) {
      action_failed();
      action_err_msg("Test could rename dir $dir_1 to existing file $name");
      $error = 1;
    } else {
      # uncomment three lines below to see if the error is correct
      #$err_num = $lfc::serrno;
      #$err_string = lfc::sstrerror($err_num);
      #print "Error $err_num ($err_string)\n";
      action_ok();
      remove_dir($dir_1);
    }
  }
  remove_file($name);
}

# checking the case when newpath is a directory while oldpath is a regular file (EISDIR)
action_title ("Checking the case when newpath is a directory while oldpath is a regular file (EISDIR)");
# creating a test file which test dir will be renamed to
$t = time();
$name = "/grid/$vo/perl_rename_testfile_ENOTDIR_$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/perl_filerename_test_"."$t";
$guid = gen_guid();
$res = lfc::lfc_creatg($name,$guid,0664);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during file $name creation: Error $err_num ($err_string)");
  $error =1;
} else {
  # checking the status of created file
  my $statg = lfcc::new_lfc_filestatg();
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = gen_guid();
  $res = lfc::lfc_statg(undef,$guid,$statg);
  if ($res != 0) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("There was an error during getting the status of $name : Error $err_num ($err_string)");
    $error =1;
  } else {
    # renaming file $name to dir $dir_1
    $dir_1 = "/grid/$vo/perl_rename_testdir1_ENOTDIR_$t";
    lfc::lfc_mkdir($dir_1,0755);
    $res = lfc::lfc_rename($name,$dir_1);
    if ($res == 0) {
      action_failed();
      action_err_msg("Test could rename file $name to dir $dir_1");
      $error = 1;
    } else {
      # uncomment three lines below to see if the error is correct
      #$err_num = $lfc::serrno;
      #$err_string = lfc::sstrerror($err_num);
      #print "Error $err_num ($err_string)\n";
      action_ok();
      remove_dir($dir_1);
    }
  }
  remove_file($name);
}


# checking the case when newpath is a descendant of oldpath (EINVAL)
action_title("Checking the case when newpath is a descendant of oldpath (EINVAL)");
$dir_1 = "/grid/$vo/perl_rename_testdir_EINVAL_$t";
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during directory $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  $name = $dir_1."/renamed_test_dir";
  # uncomment the line below to check if test prints the proper message in case of failure
  #$name = "/grid/$vo/perl_renamed_test_dir";
  $res = lfc::lfc_rename($dir_1,$name);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename the dir $dir_1 to $name while the newpath is a descendant of the oldpath");
    $error = 1;
    remove_dir($name);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    remove_dir($dir_1);
  }
}

# checking the case when the length of oldpath component exceeds CA_MAXNAMELEN (ENAMETOOLONG)
action_title("Checking the case when the length of oldpath component exceeds CA_MAXNAMELEN (ENAMETOOLONG)");
$t = time();
$dir_1 = "perl_rename_testdir1_$t";
while (length($dir_1)<$lfc::CA_MAXNAMELEN+1) { $dir_1 = $dir_1.'a' }
$dir_1 = "/grid/$vo/".$dir_1;
#print "dir_1 is $dir_1 and its length is ",length($dir_1)," CA_MAXNAMELEN is $lfc::CA_MAXNAMELEN\n";
# uncomment two lines below to check if test prints the proper message in case of failure
#$dir_1 = "/grid/$vo/perl_rename_testdir1_$t";
#lfc::lfc_mkdir($dir_1,0755);
$dir_2 = "/grid/$vo/perl_rename_testdir2_$t";
$res = lfc::lfc_rename($dir_1,$dir_2);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename dir $dir_1 to $dir_2 while the length of a path exceeds CA_MAXNAMELEN ($lfc::CA_MAXNAMELEN)");
  $error = 1;
  remove_dir($dir_2);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
}

# checking the case when the length of newpath component exceeds CA_MAXNAMELEN (ENAMETOOLONG)
action_title("Checking the case when the length of newpath component exceeds CA_MAXNAMELEN (ENAMETOOLONG)");
$t = time();
$dir_1 = "/grid/$vo/perl_rename_testdir1_$t";
# creating test dir to rename
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during dir $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  $dir_2 = "perl_rename_testdir2_$t";
  while (length($dir_2)<$lfc::CA_MAXNAMELEN+1) { $dir_2 = $dir_2.'a' }
  $dir_2 = "/grid/$vo/".$dir_2;
  #print "dir_2 is $dir_2 and its length is ",length($dir_2)," CA_MAXNAMELEN is $lfc::CA_MAXNAMELEN\n";
  # uncomment the line below to check if test prints the proper message in case of failure
  #$dir_2 = "/grid/$vo/perl_rename_testdir2_$t";
  $res = lfc::lfc_rename($dir_1,$dir_2);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename dir $dir_1 to $dir_2 while the length of a newpath exceeds CA_MAXNAMELEN ($lfc::CA_MAXNAMELEN)");
    $error = 1;
    remove_dir($dir_2);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    remove_dir($dir_1);
  }
}

# checking the case when the length of oldpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)
#action_title("Checking the case when the length of oldpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)");
#$t = time();
#$dir_1 = "perl_rename_testdir1_$t";
#while (length($dir_1)<$lfc::CA_MAXPATHLEN+1) { $dir_1 = $dir_1.'/perl_test_dir' }
#print "dir_1 is $dir_1 and its length is ",length($dir_1),"\n";
#$dir_2 = "/grid/$vo/perl_rename_testdir2_$t";
#$res = lfc::lfc_rename($dir_1,$dir_2);
#if ($res == 0) {
#  action_failed();
#  action_err_msg("Test could rename dir $dir_1 to $dir_2 while the oldpath was larger than CA_MAXPATHLEN ($lfc::CA_MAXPATHLEN)");
#  $error = 1;
#  remove_dir($dir_2);
#} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
#  action_ok();
#}

# checking the case when the length of oldpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)
action_title("Checking the case when the length of oldpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)");
$t = time();
$dir_1 = "/grid/$vo/perl_rename_testdir1_$t";
while (length($dir_1)<$lfc::CA_MAXPATHLEN+1) { $dir_1 = $dir_1.'a' }
#print "dir_1 is $dir_1 and its length is ",length($dir_1),"\n";
$dir_2 = "/grid/$vo/perl_rename_testdir2_$t";
$res = lfc::lfc_rename($dir_1,$dir_2);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename dir $dir_1 to $dir_2 while the oldpath was larger than CA_MAXPATHLEN ($lfc::CA_MAXPATHLEN)");
  $error = 1;
  remove_dir($dir_2);
} else {
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
}


# checking the case when the length of newpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)
action_title("Checking the case when the length of newpath exceeds CA_MAXPATHLEN (ENAMETOOLONG)");
$t = time();
$dir_1 = "/grid/$vo/perl_rename_testdir1_$t";
# creating test dir to rename
$res = lfc::lfc_mkdir($dir_1,0755);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error during dir $dir_1 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  $dir_2 = "/grid/$vo/perl_rename_testdir2_$t";
  while (length($dir_2)<$lfc::CA_MAXPATHLEN+1) { $dir_2 = $dir_2.'a' }
  #print "dir_2 is $dir_2 and its length is",length($dir_2),"\n";
  # uncomment the line below to check if test prints the proper message in case of failure
  #$dir_2 = "/grid/$vo/perl_renamed_test_dir_$t";
  $res = lfc::lfc_rename($dir_1,$dir_2);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could rename dir $dir_1 to $dir_2 while the newpath was larger than CA_MAXPATHLEN ($lfc::CA_MAXPATHLEN)");
    $error = 1;
    remove_dir($dir_2);
  } else {
    # uncomment three lines below to see if the error is correct
    #$err_num = $lfc::serrno;
    #$err_string = lfc::sstrerror($err_num);
    #print "Error: $err_num ($err_string)\n";
    action_ok();
    remove_dir($dir_1);
  }
}

# checking the case when LFC host is unknown
action_title("Checking the case when LFC host is unknown (SENOSHOST)");
# retrieving default value for LFC_HOST env variable
my $lfc_host = $ENV{'LFC_HOST'};
#print "LFC_HOST is $lfc_host\n";
#changing the value for LFC_HOST environment variable
$t = time();
$ENV{'LFC_HOST'} = "random_lfc_host_".$t;
# uncomment the line below to check if test prints the proper message in case of failure
#$ENV{'LFC_HOST'} = $lfc_host;
# temporary closing STDERR to prevent error message to be displayed during that subtest execution
open (COPY_STDERR,">&STDERR");
close(STDERR) or die "Can't close STDERR: $!\n";
$name = "/grid/$vo/perl_rename_SENOSHOST_test";
$new_name = "/grid/$vo/perl_rename_SENOSHOST_test_renamed";
$res = lfc::lfc_rename($name,$new_name);
# restoring STDERR
open (STDERR,">&COPY_STDERR")|| die "Can't restore STDERR";
if ($res == 0) {
  action_failed();
  action_err_msg("Test could rename $name to $new_name on unknown LFC host ($ENV{'LFC_HOST'})");
  $error = 1;
  remove_dir($name);
} else {
  action_ok();
  # uncomment three lines below to see if the error is correct
  #$err_num = $lfc::serrno;
  #$err_string = lfc::sstrerror($err_num);
  #print "Error $err_num ($err_string)\n";
}
# restoring default LFC_HOST environment value
$ENV{'LFC_HOST'} = $lfc_host;

# printing the test final status
if ($error) {
  test_failed();
} else {
  test_passed();
}
