#!/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_closedir function is able to close open
# directory in the name server.
#
##############################################################################
# meta: proxy=True
# meta: preconfig=../../LFC-config
use strict;
use lfc;
use TestCommon;

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

test_title("lfc-closedir");

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

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

# checking the ability to close open directory
action_title("Checking the ability to close open directory");
$name = "/";
$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 dir $name : Error $err_num ($err_string)\n");
  $error = 1;
} else {
  # uncomment the line below to check if test prints the proper message in case of failure
  #$name = "/grid/$vo/perl_closedir_test";
  $dir = lfc::lfc_opendir($name);
  if (!defined($dir)) {
    $err_num = $lfc::serrno;
    $err_string = lfc::sstrerror($err_num);
    action_failed();
    action_err_msg("Test failed to open dir $name : Error $err_num ($err_string)\n");
    $error = 1;
    } else {
      $res = lfc::lfc_closedir($dir);
      if ($res != 0) {
        $err_num = $lfc::serrno;
        $err_string = lfc::sstrerror($err_num);
        action_failed();
        action_err_msg("Test failed to close dir $name : Error $err_num ($err_string)\n");
        $error = 1;
      } else {
        action_ok();
      }
    }
}

# checking the case when dirp is a NULL pointer (EFAULT)
action_title("Checking the case when dirp is a NULL pointer (EFAULT)");
$res = lfc::lfc_closedir(undef);
if ($res == 0) {
  action_failed();
  action_err_msg("Test could close the dir while dirp was a NULL pointer");
  $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)");
#$name = "/";
#$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 dir $name : Error $err_num ($err_string)\n");
#  $error = 1;
#} else {
#  # uncomment the line below to check if test prints the proper message in case of failure
#  #$name = "/grid/$vo/perl_closedir_test";
#  $dir = lfc::lfc_opendir($name);
#  if (!defined($dir)) {
#    $err_num = $lfc::serrno;
#    $err_string = lfc::sstrerror($err_num);
#    action_failed();
#    action_err_msg("Test failed to open dir $name : Error $err_num ($err_string)\n");
#    $error = 1;
#    } else {
#      my $lfc_host = $ENV{'LFC_HOST'};
#      $ENV{'LFC_HOST'} = "random_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_closedir($dir);
#      # restoring STDERR
#      open (STDERR,">&COPY_STDERR")|| die "Can't restore STDERR";
#      if ($res == 0) {
#        action_failed();
#        action_err_msg("Test could close a dir 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;
#    }
#}

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