#!/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 function lfc_getcwd works properly i.e. if
# it is able to get the LFC current directory used by the name server.
#
##############################################################################
# meta: proxy=True
# meta: preconfig=../../LFC-config
use strict;
use lfc;
use TestCommon;

my ($name,$res,$err_num,$err_string);

test_title("lfc-getcwd");
# defining VO name
my $vo = $ENV{VO};

# initializing $error variable used for detecting test errors (it is set to 1 if any test failed)
my $error = 0;

# checking the possibility to get current working directory
action_title("Checking the possibility to get current working directory");
$name = "/grid/$vo";
lfc::lfc_chdir($name);
$res = lfc::lfc_getcwd($name,length($name)+1);
if (!defined($res)) {
  $err_num = $lfc::serrno;
  $err_string = lfc::sstrerror($err_num);
  action_failed();
  action_err_msg("There was an error while getting current working dir : Error $err_num ($err_string)");
  $error = 1;
} else {
  action_ok();
}


## checking the case when current working directory has not been set yet (ENOENT)
#action_title("Checking the case when current working directory has not been set yet (ENOENT)");
##$name = " ";
## uncomment the line below to check if test prints the proper message if condition is true
##$name = "/grid/$vo";
#print "\$name: $name\n";
#$res = lfc::lfc_getcwd($name,length($name)+1);
#if (defined($res)) {
#  action_failed();
#  action_err_msg("Test could get current working dir while it has not been set yet");
#  print "\$res: $res\n";
#  $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 current working directory does not exist anymore (ENOENT)
#action_title("Checking the case when current working directory does not exist anymore (ENOENT)");
#my $t = time();
#$name = "/grid/$vo/perl_getcwd_ENOENT_test_dir_$t";
#my $stat = lfcc::new_lfc_filestat();
#$res = lfc::lfc_stat($name,$stat);
#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;
#   $res = lfc::lfc_mkdir($name,0755);
#   if ($res != 0) {
#     $err_num = $lfc::serrno;
#     $err_string = lfc::sstrerror($err_num);
#     action_failed();
#     action_err_msg("Test could not create a test dir $name : Error $err_num ($err_string)\n");
#     $error = 1;
#   } else {
#     $res = lfc::lfc_chdir($name);
#     if ($res != 0) {
#       $err_num = $lfc::serrno;
#       $err_string = lfc::sstrerror($err_num);
#       action_failed();
#       action_err_msg("Test could not change current working dir to $name : Error $err_num ($err_string)\n");
#       $error = 1;
#     } else {
#       print "\$name value before dir removal is $name\n";
#       $res = lfc::lfc_rmdir($name);
#       if ($res != 0) {
#         $err_num = $lfc::serrno;
#         $err_string = lfc::sstrerror($err_num);
#         action_failed();
#         action_err_msg("Test could not remove test dir $name : Error $err_num ($err_string)\n");
#         $error = 1;
#       } else {
#         $res = lfc::lfc_getcwd($name,length($name)+1);
#         if (defined($res)) {
#           action_failed();
#           action_err_msg("Test could get current working dir $name while it was removed");
#           print "\$res: $res\n";
#           $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 size is greater than 0 and less than size of current directory name plus 1 (ERANGE)
action_title("Checking the case when size is greater than 0 and less than size of current directory name plus 1 (ERANGE)");
$name = "  ";
my $size = length($name);
# uncomment the line below to check if test prints the proper message if condition is true
#$size = 255;
$res = lfc::lfc_getcwd($name,$size);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could get current working dir $res while size was greater than 0 and less than size of current directory name plus 1");
  print "\$res: $res\n\$name: $name\n";
  $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 buf is a NULL pointer and memory could not be allocated (ENOMEM)
#action_title("Checking the case when buf is a NULL pointer and memory could not be allocated (ENOMEM)");
#$name = undef;
## uncomment the line below to check if test prints the proper message if condition is true
##$name = "/grid/$vo";
#$res = lfc::lfc_getcwd($name,255);
#if (defined($res)) {
#  action_failed();
#  action_err_msg("Test could get current working dir while buf was a NULL pointer and memory could not be allocated");
#  print "\$res: $res\n";
#  $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 size is less than or equal to zero (EINVAL)
action_title("Checking the case when size is less than or equal to zero (EINVAL)");
$name = "/grid/$vo";
$size = 0;
# uncomment the line below to check if test prints the proper message if condition is true
#$size = length($name)+1;
$res = lfc::lfc_getcwd($name,$size);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could get current working dir while size was less than or equal to zero");
  $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 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
#my $t = time();
#$name = "/grid/$vo";
#lfc::lfc_chdir($name);
#$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";
#$res = lfc::lfc_getcwd($name,length($name)+1);
## restoring STDERR
##open (STDERR,">&COPY_STDERR")|| die "Can't restore STDERR";
#if (defined($res)) {
#  action_failed();
#  action_err_msg("Test could get the current working dir $res on unknown LFC host ($ENV{'LFC_HOST'})");
#  $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();
#}
# restoring default LFC_HOST environment value
#$ENV{'LFC_HOST'} = $lfc_host;

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