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

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

test_title("lfc-opendirxg");

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

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

# defining LFC host
$lfc_host = $ENV{'LFC_HOST'};

# checking the ability to open an existing directory specified by pathname
action_title("Checking the ability to open an existing directory specified by pathname");
my $t = time();
$name = "/";
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;
} else {
  # uncomment the line below to check if test prints the proper message in case of failure
  #$name = "/grid/$vo/perl_opendirxg_test_$t";
  $res = lfc::lfc_opendirxg($lfc_host,$name,undef);
  if (!defined($res)) {
    $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 {
      action_ok();
      lfc::lfc_closedir($res);
    }
}

# checking the ability to open an existing directory specified by GUID
action_title("Checking the ability to open an existing directory specified by GUID");
$t = time();
$name = "/grid/$vo/perl_opendirxg_testdir_$t";
$guid = gen_guid();
$res = lfc::lfc_mkdirg($name,$guid,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 {
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = gen_guid();
  $res = lfc::lfc_opendirxg($lfc_host,undef,$guid);
  if (!defined($res)) {
    $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 {
      action_ok();
      lfc::lfc_closedir($res);
    }
  remove_dir($name);
}

# checking the ability to open an existing directory specified by both path and GUID
action_title("Checking the ability to open an existing directory specified by both path and GUID");
$t = time();
$name = "/grid/$vo/perl_opendirxg_testdir_$t";
$guid = gen_guid();
$res = lfc::lfc_mkdirg($name,$guid,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 {
  # uncomment the line below to check if test prints the proper message in case of failure
  #$guid = gen_guid();
  $res = lfc::lfc_opendirxg($lfc_host,$name,$guid);
  if (!defined($res)) {
    $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 {
      action_ok();
      lfc::lfc_closedir($res);
    }
  remove_dir($name);
}

# checking the case when a component of path prefix does not exist (ENOENT)
action_title("Checking the case when a component of path prefix does not exist (ENOENT)");
$t = time();
$name = "/grid/$vo/perl_opendirxg_ENOENT_test_$t";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$res = lfc::lfc_opendirxg($lfc_host,$name,undef);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open non-existing dir $name");
  $error = 1;
  lfc::lfc_closedir($res);
} 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 path is null pathname (ENOENT).
action_title("Checking the case when a path is null pathname (ENOENT)");
$name = "";
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$res = lfc::lfc_opendirxg($lfc_host,$name,undef);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir $name when a path was null pathname");
  lfc::lfc_closedir($res);
  $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 search permission is denied on a component of path prefix (EACCES)
action_title("Checking the case when a search permission is denied on a component of path prefix (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_opendirxg_EACCES_test_"."$t";
$res = lfc::lfc_mkdir($name,0644);
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 {
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($name,0755);
  $res = lfc::lfc_opendirxg($lfc_host,$name,undef);
  if (defined($res)) {
    action_failed();
    action_err_msg("Test could open a dir $name when a search permission was denied on a component of its path");
    $error = 1;
    lfc::lfc_closedir($res);
  } 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 test dir
  remove_dir($name);
}

# checking the case when read permission is denied on path (EACCES)
action_title("Checking the case when read permission is denied on a path (EACCES)");
# defining unique dir name with help of current number of seconds since epoch
$t = time();
$name = "/grid/$vo/perl_opendirxg_EACCES_test_"."$t";
$res = lfc::lfc_mkdir($name,0111);
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 {
  # uncomment the line below to check if test prints the proper message in case of failure
  #lfc::lfc_chmod($name,0755);
  $res = lfc::lfc_opendirxg($lfc_host,$name,undef);
  if (defined($res)) {
    action_failed();
    action_err_msg("Test could open a dir $name when read permission was denied on a path");
    $error = 1;
    lfc::lfc_closedir($res);
  } 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 test dir
  remove_dir($name);
}

# checking the case when a path is NULL pointer (EFAULT).
action_title("Checking the case when a path is NULL pointer (EFAULT)");
$name = undef;
# uncomment the line below to check if test prints the proper message in case of failure
#$name = "/";
$res = lfc::lfc_opendirxg($lfc_host,$name,undef);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir $name when a path was a NULL pointer");
  $error = 1;
  lfc::lfc_closedir($res);
} 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 the GUID exceeds CA_MAXGUIDLEN (EINVAL)
action_title("Checking the case when the length of the GUID exceeds CA_MAXGUIDLEN (EINVAL)");
$t = time();
$guid = gen_guid();
$guid = "$guid"."$t";
$res = lfc::lfc_opendirxg($lfc_host,undef,$guid);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir with guid length larger than CA_MAXGUIDLEN ($lfc::CA_MAXGUIDLEN)");
  $error = 1;
  lfc::lfc_closedir($res);
} 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_opendirxg_ENOTDIR_testfile_$t";
# uncomment the line below to check if test prints the proper message if condition below is true
#$name = "/perl_chmod_testfile_"."$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 {
  # create a file name with a path what includes the file
  my $new_name = $name."/perl_opendirxg_testfile";
  $res = lfc::lfc_opendirxg($lfc_host,$new_name,undef);
  if (defined($res)) {
    action_failed();
    action_err_msg("Test could open a dir $new_name a component of which path prefix is not a directory");
    $error = 1;
    lfc::lfc_closedir($res);
  } 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 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/perl_opendirxg_test_";
while (length($name)<$lfc::CA_MAXPATHLEN+1) { $name = $name.'a' }
# uncomment the line below to check if test prints the proper message if condition below is true
#$name = "/";
$res = lfc::lfc_opendirxg($lfc_host,$name,undef);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir with path length larger than CA_MAXPATHLEN ($lfc::CA_MAXPATHLEN)");
  $error = 1;
  lfc::lfc_closedir($res);
} 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 path component exceeds CA_MAXNAMELEN (ENAMETOOLONG)
action_title("Checking the case when the length of path component exceeds CA_MAXNAMELEN (ENAMETOOLONG)");
$name = "perl_opendirxg_ENAMETOOLONG_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 = "/";
$res = lfc::lfc_opendirxg($lfc_host,$name,undef);
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir with path component length larger than CA_MAXNAMELEN ($lfc::CA_MAXNAMELEN)");
  $error = 1;
  lfc::lfc_closedir($res);
} 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
#$lfc_host = $ENV{'LFC_HOST'};
#print "LFC_HOST is $lfc_host\n";
# setting wrong LFC hostname
$t = time();
$lfc_host = "random_lfc_host_".$t;
$name = "/grid/$vo/perl_opendirxg_SENOSHOST_test";
# 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_opendirxg($lfc_host,$name,undef);
# restoring STDERR
open (STDERR,">&COPY_STDERR")|| die "Can't restore STDERR";
if (defined($res)) {
  action_failed();
  action_err_msg("Test could open a dir on unknown LFC host ($ENV{'LFC_HOST'})");
  $error = 1;
  lfc::lfc_closedir($res);
} 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();
}

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

