#! /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-negot.template";
$testdesc =  'lib_auth_protocol-negot - Security Protocol Negotiation Test';
$testname = "lib_auth_protocol-negot";
$corename = "lib_auth_protocol-negot";
$mytestnegotiation = "x_secneg_clR_dmnR_secprepostsrc";
$mytestnegotiation_result = "yes";
$mytestnegotiation_pat = "secprepostsrc";
$mybaseparamname = "x_param.protocol-negot";
$datafile = "lib_auth_protocol-negot.data";


my $numberoftests = 4;
my $numberoferrors = 0;

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

print scalar localtime() . "\n";
$myvariation = 1;
$pid = $mypid = $$;
$mysaveme = $corename . ".saveme";

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

open(DF,"<$datafile") || die "Can not open negotiation data file!: $!\n";
my $line;
while(<DF>) {
	CondorUtils::fullchomp($_);
	#$mypid = $myvariation . "_" . $pid;
	$mypid = $pid;
	$label = "protocol" . "_" . $myvariation;
	#if($myvariation == 2) {
		#exit(0);
	#}
	$myvariation = $myvariation + 1;
	CondorTest::debug("Condor for this run in $mypid\n",1);
	print scalar localtime() . "\n";
	$line = $_;
	if($line =~ /^(x_secneg.*_secprepostsrc)\s(.*)$/ ) {
		CondorTest::debug("Negotiation is $1 and Expected result $2\n",1);
		$mytestnegotiation = $1;
		$mytestnegotiation_result = $2;
	}

	#print "$line\n";
	$myparamname = $mybaseparamname . $mypid;
	$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
		{
			#print "template: $line\n";
			print CSUB "$line\n";
		}
	}
	close(TPLATE);
	close(CSUB);
	my $configsec = CondorTest::StartPersonal( "lib_auth_protocol-negot", $myparamname ,$label, $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("./lib_auth_protocol-negot.pl $mypid $label $mytestnegotiation_result");
	CondorTest::debug("Result of crafted lib_auth_protocol-negot job is  $result\n",1);

	if($result != 0) {
		$mynumberoferrors = $mynumberoferrors + 1;
		CondorTest::debug("ERRORS detected: $mynumberoferrors of $mynumberoftests failed\n",1);
	}

    #fails due to directory matching pattern
	runcmd("cp *$corename* $mysaveme", {expect_result=>\&ANY});
	
	if( -e "$mysubmitnm" )
	{
		runcmd("cp $mysubmitnm $mycorenm");
	}

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

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

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

print scalar localtime() . "\n";

if( $mynumberoferrors != 0 )
{
	# actual test failed but we allowed cleanup
	CondorTest::debug("$mynumberoferrors of $numberoftests FAILED!\n",1);
	exit(1);
}

exit(0);
