#! /usr/bin/env perl
##**************************************************************
##
## 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 CondorTest;
use CondorUtils

$param_template = "x_param.protocol-allowdeny-ctb.template";
$testdesc =  'lib_auth_allowdeny-ctb - Security Protocol CLAIMTOBE Authentication Test';
$testname = "lib_auth_allowdeny-ctb";
$corename = "lib_auth_allowdeny-ctb";
$mytestnegotiation = "x_secneg_clR_dmnR_secprepostsrc";
$mytestnegotiation_result = "yes";
$mycmdfile = "lib_auth_allowdeny-ctb.cmd";


# where am I running
$currenthost = CondorTest::getFqdnHost();

print scalar localtime() . "\n";
$myvariation = 1;

$mypid = $$;
$mysaveme = $corename . ".saveme";

if($mypid == 0) {
	die "Saveme setup for test failed\n";
}

#print "$line\n";
$myparamname = $corename . $mypid . ".param";
$mysubmitnm = $corename . $mypid . ".cmd";
$mysubmitnmout = $corename . $mypid . ".cmd.out";
$mysubmitnmlog = $corename . $mypid . ".log";
$mysubmitnmout = $corename . $mypid . ".log";
$mycorenm = $corename . ".cmd";
$mycorenmout = $corename . ".cmd.out";
$mycorenmlog = $corename . ".log";

CondorTest::debug("Master PID is $mypid\n",1);
CondorTest::debug("Master PID submit file is $mysubmitnm\n",1);

# Edit submit file to submit to scheduler at securitypool (side b) schedd.
my $line;
open(CSUB,">$myparamname") || die "Can't open new submit file $myparamname: $!\n";
open(TPLATE,"<$param_template") || die "Can't open param template $param_template: $!\n";
while(<TPLATE>)
{
	CondorUtils::fullchomp($_);
	$line = $_;
	if( $line =~ /^\s*secprepostsrc\s*=\s*.*$/ )
	{
		CondorTest::debug("Setting up for $mytestnegotiation\n",1);
		print CSUB "secprepostsrc = $mytestnegotiation\n";
	}
	else
	{
		CondorTest::debug("template: $line\n",1);
		print CSUB "$line\n";
	}
}
close(TPLATE);
close(CSUB);
sleep 10;

my $configsec = CondorTest::StartPersonal( "lib_auth_allowdeny-ctb", $myparamname ,"allowdeny", $mypid);
my @securitypool = split /\+/, $configsec;
my $secconfig = shift @securitypool;
my $secport = shift @securitypool;

CondorTest::debug("---security config is $secconfig and securitypool port is $secport---\n",1);
	

$ENV{CONDOR_CONFIG} = $secconfig;
# submit into local schedd (side a)

my $result = system("./x_lib_auth_protocol.pl $mypid \"allowdeny\" $mytestnegotiation_result $mycmdfile \"ctb\"");
CondorTest::debug("Result of crafted lib_auth_allowdeny-ctb job is  $result\n",1);

system("cp *$corename* $mysaveme");
	
if( -e "$mysubmitnm" )
{
	system("cp $mysubmitnm $mycorenm");
}

if( -e "$mysubmitnmout" )
{
	system("cp $mysubmitnmout $mycorenmout");
}

if( -e "$mysubmitnmlog" )
{
	system("cp $mysubmitnmlog $mycorenmlog");
}

CondorTest::KillPersonal($secconfig);
print scalar localtime() . "\n";

if($result != 0)
{
    # actual job failed but we allowed cleanup
    exit(1);
}

CondorTest::debug("lib_auth_allowdeny-ctb SUCCESS\n",1);
exit(0);
