#!/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_statg function gets the proper
# information about a LFC file or directory in the name server having the given
# GUID.
#
##############################################################################
# meta: proxy=True
# meta: preconfig=../../LFC-config
use strict;
use lfc;
use TestCommon;

my ($name,$res,$statg,$err_num,$err_string,$guid);
test_title("lfc-statg");

# defining VO name
my $vo = $ENV{VO};

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

# checking the status of existing directory specified by path
action_title("Checking the status of existing directory specified by path");
$name = "/";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/non-existing_dir";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res != 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("Test failed to get the status of $name dir: Error $err_num ($err_string)\n");
  $error = 1;
} else {
  action_ok();
}

# checking the status of existing file specified by GUID
action_title("Checking the status of existing file specified by GUID");
# creating a test file
my $t = time();
$name = "/grid/$vo/perl_testfile_"."$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 {
  $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 file $name creation: Error $err_num ($err_string)");
    $error =1;
  } else {
    action_ok();
  }
  remove_file($name);
}

# checking the case when the named file/directory with provided path does not exist
action_title("Checking the case when the named file/directory with provided path does not exist (ENOENT)");
$name = "/nonexisting_dir";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("Test could get the status of non-existing directory $name");
  $error = 1;
} 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";
}

# checking the case when the named file/directory with provided GUID does not exist
action_title("Checking the case when the named file/directory with provided GUID does not exist (ENOENT)");
#$name = "/nonexisting_dir";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$guid = gen_guid();
$statg = lfcc::new_lfc_filestatg();
$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("Test could get the status of non-existing directory $name");
  $error = 1;
} 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";
}

# checking the case when the named file/directory is a null pathname
action_title("Checking the case when the named file/directory is a null pathname (ENOENT)");
$name = "";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("Test could get the status of directory $name with a null pathname");
  $error = 1;
} 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";
}

# checking the case when a specified path is a NULL pointer (EFAULT).
action_title("Checking the case when a specified path is a NULL pointer (EFAULT)");
$name = undef;
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/grid/$vo";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could get the status of the entry which path was a NULL pointer");
  $error = 1;
} 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";
}

# checking the case when a statbuf is a NULL pointer (EFAULT).
action_title("Checking the case when a statbuf is a NULL pointer (EFAULT)");
$name = "/grid/$vo";
$statg = undef;
# uncomment the line below to check if test prints the proper message in case of failure
#$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could get the status of the entry with NULL pointer for statbuf");
  $error = 1;
} 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";
}

# checking the case when the length of the GUID exceeds CA_MAXGUIDLEN (EINVAL)
action_title("Checking the case when the length of the GUID exceeds CA_MAXGUIDLEN (EINVAL)");
$guid = gen_guid();
$guid = "$guid"."1";
#print "guid length is ",length($guid),"\n";
#print "\$lfc::CA_MAXGUIDLEN is $lfc::CA_MAXGUIDLEN\n";
# uncomment the line below to check if test prints the proper message in case of failure
#$guid = gen_guid();
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg(undef,$guid,$statg);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could create a dir with guid length larger than CA_MAXGUIDLEN ($lfc::CA_MAXGUIDLEN)");
  $error = 1;
} else {
  # uncomment three lines below to see if the error is correct
  #my $err_num = $lfc::serrno;
  #my $err_string = lfc::sstrerror($err_num);
  #print "Error: $err_num ($err_string)\n";
  action_ok();
}

# checking the case when both path and GUID are specified and they point to a different entries (EINVAL)
action_title("Checking the case when both path and GUID are specified and they point to a different entries (EINVAL)");
# creating a test file
$t = time();
$name = "/grid/$vo/perl_testfile_"."$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 {
  $statg = lfcc::new_lfc_filestatg();
  # storing old guid to check the true condition for 'if' operator and setting new guid what doesn't correspond to created file
  my $old_guid = $guid;
  $guid = gen_guid();
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = $old_guid;
  $res = lfc::lfc_statg($name,$guid,$statg);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could get the status of the file although path and GUID pointed to different files");
    $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_file($name);
}

# 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";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
# restoring STDERR
open (STDERR,">&COPY_STDERR")|| die "Can't restore STDERR";
if ($res == 0) {
  action_failed();
  action_err_msg("Test could get the status of dir $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;

# checking the case when a search permission is denied on any component of path (EACCES)
action_title("Checking the case when a search permission is denied on any component of path (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
my $dir = "/grid/$vo/perl_statg_test_"."$t";
my $dir_2 = $dir."/test_dir";
$res = lfc::lfc_mkdir($dir,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 creation: Error $err_num ($err_string)");
  $error = 1;
} else {
  lfc::lfc_mkdir($dir_2,0755);
  lfc::lfc_chmod($dir,0000);
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($dir,0755);
  $statg = lfcc::new_lfc_filestatg();
  $res = lfc::lfc_statg($dir_2,undef,$statg);
  if ($res == 0) {
    action_failed();
    action_err_msg("Test could get the status of dir $dir_2 having no permissions to retrieve such info");
    $error = 1;
  } 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";
  }
  # removing test dirs
  # changing its permissions first
  $res = lfc::lfc_chmod($dir,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 from 0000 to 0755: Error $err_num ($err_string)");
    $error = 1;
  } else {
    remove_dir($dir_2);
    remove_dir($dir);
  }
}

# checking the case when the length of path component exceeds CA_MAXNAMELEN (ENAMETOOLONG)
action_title("Checking the case when the length of path component exceeds CA_MAXNAMELEN (ENAMETOOLONG)");
$name = "perl_statg_test_";
while (length($name)<$lfc::CA_MAXNAMELEN+1) { $name = $name.'a' }
$name = "/grid/$vo/".$name;
#print "\$name is $name,length is ",length($name),"\nCA_MAXNAMELEN is $lfc::CA_MAXNAMELEN\n";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/grid/$vo/";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could get the status of the dir $name with length of path component larger than CA_MAXNAMELEN ($lfc::CA_MAXNAMELEN)");
  $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();
}

# checking the case when the length of specified path exceeds CA_MAXPATHLEN (ENAMETOOLONG)
action_title("Checking the case when the length of specified path exceeds CA_MAXPATHLEN (ENAMETOOLONG)");
$name = "/grid/$vo/";
while (length($name)<$lfc::CA_MAXPATHLEN+1) { $name = $name.'a' }
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/grid/$vo";
$statg = lfcc::new_lfc_filestatg();
$res = lfc::lfc_statg($name,undef,$statg);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could get the status of the dir $name with path name larger than CA_MAXPATHLEN ($lfc::CA_MAXPATHLEN)");
  $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();
}

# checking the case when a component of path prefix is not a directory (ENOTDIR)
action_title("Checking the case when a component of path 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_testfile_"."$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
  $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 {
    # checking the status of the dir path to which includes a file
    my $dir_name = $name."/perl_stat_test_dir";
    # uncomment the line below to check if test prints the proper message in case of failure
    #$dir_name = "/grid/$vo/perl_mkdirg_test";
    $statg = lfcc::new_lfc_filestatg();
    $res = lfc::lfc_statg($dir_name,undef,$statg);
    if ($res == 0) {
      action_failed();
      action_err_msg("Test could get the status of the dir $dir_name a component of which path prefix is not a directory");
      remove_dir($dir_name);
      $error = 1;
    } 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";
    }
  }
  remove_file($name);
}

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