#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## 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.
##
##**************************************************************

use CondorUtils;
use Cwd;

my @got;
$testdesc =  'lib_auth_config - runs tests of IpVerify';
$testname = "lib_auth_config";
if(is_windows()) {
	$test = "condor_test_auth.exe";
} else {
	$test = "condor_test_auth";
}
$inputdir = "lib_auth_config_tests";

@inputfiles = ();
$topdir = getcwd();

print scalar localtime() . "\n";

if(!(-f $test)) {
	print "Failure from lack of testing program <$test>\n";
	exit(1);
}

# get input list by reading directory in perl
my $fullpath = "";

chdir("$inputdir");
opendir(GD,".") or die "failed to open .:$!\n";
foreach my $subfile (readdir GD) {
	next if $subfile =~ /^\.\.?$/;
	if($subfile =~ /_input/) {
		#print "saving:$subfile:\n";
		$fullpath = "";
		if(is_windows()) {
			$fullpath = "$inputdir" . "\\" . "$subfile";
		} else {
			$fullpath = "$inputdir" . "/$subfile";
		}
		push @inputfiles,$fullpath;
	}
}
#foreach my $input ( @inputfiles ) {
	#print "input:$input\n"
#}

my $total_failures = 0;

chdir("$topdir");
#foreach $input ( glob($inputdir . "/test_auth_*_input") ) {
foreach my $input ( @inputfiles ) {
  my $config = $input;
  $config =~ s/_input/_config/;

  $ENV{CONDOR_CONFIG} = "$config";

  print "\n";
  print "Running authorization test $input with the following configuration:\n";
  print "########## BEGIN CONFIGURATION #############\n";

  open( config_fp, "< $config" ) or die "Can't open $config: $!";
  while( <config_fp> ) {
    print;
  }
  close config_fp;

  print "########### END CONFIGURATION ##############\n";
  print "\n";
  print "########## BEGIN TEST #############\n";

  open( input_pipe, "|$test -debug 2>&1");
  open( input_fp, "<$input");
  while ( <input_fp> ) {
      print input_pipe $_;
  }
  close input_fp;
  close input_pipe;
  my $result=$?;
  print "########### END TEST ##############\n";

  if ( $result != 0 ) {
    print "Authorization test Failure: condor_test_auth < $input\n";
  }
  $total_failures += $result;
}

  if ($total_failures == 0) {
    print "\n";
    print "Authorization test Success\n";
  }

exit($total_failures);
