#!/usr/bin/perl -w

##########################################################################################
#
#  srmv2Suite[.pl]
#
#  A Perl script to test the srmV2.2 interface to the DPM
#
#  Author: Gilbert Grosdidier, LAL (Universite Paris-Sud, Orsay)
#  Creation Date:  03/11/2004
#  Last rev:       18/07/2007 (GG) + 16/11/2007
#
##########################################################################################

###
### Copyright (C) 2004-2007 by CNRS/IN2P3/LAL
### All rights reserved
###

# $Id: srmv2Suite,v 1.20 2007/11/17 02:20:58 grodid Exp $

## Usage : nohup time srmv2Suite lxb1727.cern.ch 8446 grodid fil103926c44 dteam optsubtree > & /tmp/suite-SLC3-lxb1727-sec.lis1244 &
## First : cd /afs/cern.ch/project/gd/grodid/SLC3-srmtester/srmv2.2-t002
## Usage : nohup time srmv2Suite lxb1727.cern.ch 8446 grodid fil103926s707 dteam subdir7s > & suite-SLC3-lxb1727-sec.lis1707 &


use strict;
use IO::Handle;

##09use File::CounterFile;

no strict 'refs';
use DBI;
use POSIX qw(strftime);
use Env;

STDOUT->autoflush(1);
STDERR->autoflush(1);

    my @glinfo = split /\s+/, `ls -l /proc/$$/fd | grep ' 1 -> ' 2>&1`;
    my $glogName = $glinfo[-1];
    my $gcmd = "nohup $0 @ARGV "."\>\&"." $glogName "."\&";
    print STDERR " GCMD: $gcmd \n\n";
    print STDOUT " GCMD: $gcmd \n\n";

system("voms-proxy-info -all");

my $target  = shift;
my $portnum = shift;
my $tstNAME = shift;
my $sfileH  = shift;
my $voname  = shift;
my $subTree = shift;

my $pxGRP;
my $pxDN;

my $Counter;
my $value;
my $valinc;
my $short;
my $sfile;
my $xCount = 0;
my $usage;
my $dateST;
my $dateSTZ;
chomp(my $dday = `date '+%j'`);
my $otStart = time();
if ( ! $sfileH ) {
    require File::CounterFile;
    $Counter = File::CounterFile->new("./CounterFile", 1000+$dday);
    $value = $Counter->value();
    $valinc = $Counter->inc();
    $sfile = "fil$valinc";
    $sfileH = $sfile;
    print " Counter Start Value: $sfile \n";
    $sfile = "${sfileH}A0";
#    exit 0;
} else {
    print " Counter Start Value: $sfileH \n";
    $sfile = "${sfileH}A0";
}
#my $stat26 = "26";
my $stat26 = "0";  ##  For the new status code values
my $rfioSW;

## Run this with something like:
## srmv2Suite full_nodename port_number username some_filename voname [dir_subtree]

##BAD$target =~ m/(\w*?)\.(.*)/;
$target =~ m/([-\w]*?)\.(.*)/;
my $mach = $1;
my $site_domain = $2;
chomp(my $fullname = `hostname -f`);
if ( ! $target || ! $tstNAME || ! $site_domain ) {
    print STDERR " Wrong ARGS ! \n";
    print STDERR " Usage: $0 Target_FullNodeName Port_Number UserName Unique_FileName VOname [Dir_Subtree]\n";
    exit 1;
} else {
    print STDOUT " The target Host is:  $target \n";
    print STDOUT " The target Domain is: $site_domain \n";
    print STDOUT " The current Host is: $fullname \n";
}
my $fmach = "$mach.$site_domain";

$ENV{DPNS_HOST} = $fmach;
$ENV{DPM_HOST}  = $fmach;
print STDOUT " Target Servers: DPNS: $ENV{DPNS_HOST} DPM, srmV2: $ENV{DPM_HOST} \n";

chomp(my $day = `date '+%b%d'`);
# my $u_token = "gilbert_${day}_v$valinc";
##my $u_token = "gilbert_${day}_A${sfileH}";
my $u_token = "gilbert_${day}_U${sfileH}";
my $s_token = "gilbert_${day}_S${sfileH}";
my $d_token = "gilbert_${day}_D${sfileH}";
my $mkdir_t = "dir22_${d_token}";
##19my $endPoint = "https://lxb1901.cern.ch:8444";
my $endPoint = "https://${target}:${portnum}";

my $db1;
##my $shandle1;
if ( $target eq 'lxb1727.cern.ch' ) {
    eval { require DBD::mysql; };
    if ( ! $@ ) {
	my ($userI, $userP) = split /\s+/, `head -1 $HOME/private/Grid/DPMCONFIG/lxb1727`;
	$db1 = DBI->connect('DBI:mysql:cns_db_alt5:lxb1727', "$userI", "$userP");
    } else {
	print STDOUT "\n WARNING: DBD::mysql NOT available: $@ \n\n";
    }
##  $shandle1 = $db1->prepare("select ltime,sfn from Cns_file_replica where sfn like ?");
} elsif ( $target eq 'lxb1902.cern.ch' ) {

## $ENV{ORACLE_HOME} = '/usr/lib/oracle/10.1.0.3/client';
$ENV{ORACLECLIENT} = 'libclntsh.so.10.1';
## $ENV{TWO_TASK}    = 'T:accdb';
## $ENV{TNS_ADMIN}   = '/opt/lcg/etc/.tnsadmin';
## $ENV{TNS_ADMIN}   = '/afs/cern.ch/project/gd/grodid/SLC3-CVS-dpm-142gk/LCG-DM/test/srmv2.2.8/.tnsadmin';

$ENV{ORACLE_CERN}='/afs/cern.ch/project/oracle';
$ENV{ORACLE_MOUNT}='/afs/cern.ch/project/oracle/@sys';
$ENV{TNS_ADMIN}='/afs/cern.ch/project/oracle/admin';
$ENV{ORACLE_HOME}='/afs/cern.ch/project/oracle/@sys/10103';
$ENV{TWO_TASK}='accdb';

    eval { require DBD::Oracle; };
    if ( ! $@ ) {
	my ($userI, $userP) = split /\s+/, `head -1 $HOME/private/Grid/DPMCONFIG/lxb1902`;
	$db1 = DBI->connect('DBI:Oracle:', "$userI", "$userP");
    } else {
	print STDOUT "\n WARNING: DBD::Oracle NOT available: $@ \n\n";
    }

##$shandle1 = $db1->prepare("select ltime,sfn from Cns_file_replica where sfn like ?");
}

##exit 0;

my $struct;

my $voteam;
if ( $ENV{VOTEAM} ) {
  $voteam = $ENV{VOTEAM};
} elsif ( $voname ) {
  $voteam = "$voname";
} else {
  $voteam = 'dteam';
}

my $baseDteam = "/dpm/$site_domain/home/$voteam";
if ( $ENV{BASENAME} ) {
  $baseDteam = $ENV{BASENAME};
}

my $baseDpm = $baseDteam;
if ( $subTree ) {
  $baseDpm = "$baseDteam/$subTree";
}

my $baseSrm = "srm://${target}:${portnum}/$baseDpm";

my $file = "${baseDpm}/$sfile";

## The 'S'calar or 'S'hort stuff
$short = "srmv22_Tfile.$sfile";
$file = "${baseSrm}/$short";
$struct->{$file}->{short} = "$short";

## The Abort'F'iles stuff
my $Fshort = "srmv22_Tfile.${sfile}F";
my $Ffile = "${baseSrm}/$Fshort";
$struct->{$Ffile}->{short} = "$Fshort";
$struct->{$Ffile}->{input} = "/etc/ldap.conf";
$struct->{$Ffile}->{fflag} = 1;
$struct->{$Ffile}->{pin} = 11100000;

## The 'A'rray stuff
# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv22_Tfile.${sfileH}A1";
my @Lfile = ();
##push @Lfile, "/dpm/dteam/grodid/fil$valinc";
#push @Lfile, "srm://$target.cern.ch:8444//dpm/dteam/grodid/srm_test_suite_file.fil$valinc";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[0]}->{short} = "$short";

my $tempDir;
if ( $ENV{TEMPDIR} ) { $tempDir = $ENV{TEMPDIR}; }
else { $tempDir = "/tmp"; }

# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv22_Tfile.${sfileH}A2";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[1]}->{short} = "$short";

# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv22_Tfile.${sfileH}A3";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[2]}->{short} = "$short";

$struct->{$file}->{input} = "/etc/ldap.conf";
$struct->{$Lfile[0]}->{input} = "/etc/termcap";

## Which kind of host are we running on ?

if ( -e "/boot/vmlinux-2.4.20-30.7.legacysmp" ) {
    ## This is RH73
    $struct->{$Lfile[1]}->{input} = "/boot/vmlinux-2.4.20-30.7.legacysmp";
    $struct->{$Lfile[2]}->{input} = "/boot/initrd-2.4.20-30.7.legacysmp.img";
    print STDOUT " The Current Host is a RH73 one: $fullname \n";
} elsif ( -e "/boot/vmlinux-2.4.21-20.EL.cernsmp" ) {
    ## This is SLC3
##19if ( $ENV{HOST} eq "lxb1902" ) {
##21chomp(my $fullname = `hostname -f`);
##21if ( "$fullname" eq "$SLhost" ) {
    $struct->{$Lfile[1]}->{input} = "/boot/vmlinux-2.4.21-20.EL.cernsmp";
    $struct->{$Lfile[2]}->{input} = "/boot/initrd-2.4.21-20.EL.cernsmp.img";
##19    $endPoint = "https://lxb1902.cern.ch:8444";
##21    $endPoint = "https://${target}:8444";
    print STDOUT " The Current Host is a SLC3 one: $fullname \n";
} else {
    print STDOUT " Host type unidentified: $fullname \n";
    ##exit 1;
    my @bList = `ls -dl /boot/* | sort -rn -k 5,5`;
    foreach ( @bList ) {
	my @m = split /\s+/;
	if ( ! $struct->{$Lfile[1]}->{input} ) {
	    $struct->{$Lfile[1]}->{input} = $m[8];
	} elsif ( $m[4]>100000 && $m[4]<900000 ) {
	    $struct->{$Lfile[2]}->{input} = $m[8];
	    last;
	}
    }
}
foreach my $kfile ( sort keys %{$struct} ) {
    my $rls = `ls -l $struct->{$kfile}->{input}`;
    print STDOUT " File: $kfile Orig: $struct->{$kfile}->{input} \n$rls";
}

## Path setup ... one must start it from test/srmv2 for the time being ...
    $ENV{PATH} = "../../rfio:$ENV{PATH}";
    $ENV{PATH} = "$ENV{PATH}:../../socket/dpmcli";
    $ENV{PATH} = "$ENV{PATH}:../../ns";
    $ENV{PATH} = "$ENV{PATH}:../../dpm";
    $ENV{PATH} = "$ENV{PATH}:../../test/srmv1";
    $ENV{PATH} = "$ENV{PATH}:../../test/srmv2";

$struct->{$file}->{fflag} = 1;
$struct->{$Lfile[0]}->{fflag} = 2;
$struct->{$Lfile[1]}->{fflag} = 2;
$struct->{$Lfile[2]}->{fflag} = 2;

$struct->{$file}->{pin} = 111000;
$struct->{$Lfile[0]}->{pin} = 222000;
$struct->{$Lfile[1]}->{pin} = 333000;
$struct->{$Lfile[2]}->{pin} = 444000;

## Sizes in kbytes (1024)
my $dss;
($struct->{$file}->{size}, $dss) = split /\s+/, `ls -s $struct->{$file}->{input} | awk '{print \$1}'`;
($struct->{$Ffile}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Ffile}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[0]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[0]}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[1]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[1]}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[2]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[2]}->{input} | awk '{print \$1}'`;
#p    print " VERIF0: $file $struct->{$file}->{input} $struct->{$file}->{size} \n";

my $fflag = 1;
my $r_token;
my $f_token;
my $g_token;
my $TURL;
my $lTURL;
my $status;
my $tstatus;
my $sTURL;
my $findex;
my $command;
my @res;

my $tStart;
my $tEnd;
my $tElapse;
my $operation;

XcuteComm("srm2_testPing", 
	  "$endPoint",
	  "PNG", 0);
$status = ( XtractVal("versionInfo:") eq 'v2.2' ) ? '[OK]' : '[FAILED]';
$status = ( XtractVal("versionInfo:") =~ '2.2' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

XcuteComm("srm2_testGetTransferProtocols",
	  "$endPoint",
	  "GTP", 0);
$rfioSW = XtractVal('ExtraInfo: rfio');
$status = XtractVal('ExtraInfo: gsiftp') ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Temp GG 09/12/06
##undef $rfioSW;
chomp(my $lrfcp = `which rfcp`);
if ( "$lrfcp" ne '/opt/lcg/bin/rfcp' && "$lrfcp" !~ 'LCG-DM/rfio/rfcp' ) { 
##  undef $rfioSW; 
    print STDOUT "\n WARNING: rfio client with GSI authentication NOT available ? : >$lrfcp< \n\n";
}

my $protoc = 'gsiftp';
if ( $rfioSW ) { 
    $protoc = 'rfio'; 
    print STDOUT "\n Protocol rfio with GSI authentication available \n\n";
} else {
    print STDOUT "\n WARNING: Protocol rfio with GSI authentication NOT available \n\n";
}
## Temporaire tests SSL3
if ( $ENV{TSRMV22} ) { exit 0; }

if ( $subTree =~ /stress/ ) { 
    my @m = split /\//, "${baseSrm}";
    pop @m;
    my $baseSrs = join('/', @m);
    XcuteComm("srm2_testMkdir",
	      "${baseSrs}",
	      "MK00", 0);
$status = ( XtractVal("request state") eq '0' ||
	    XtractVal("request state") eq '11') ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
  }

if ( $subTree ) { 
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}",
	      "MK0", 0);
$status = ( XtractVal("request state") eq '0' ||
	    XtractVal("request state") eq '11') ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
  }

## Put operation for the Sfile (Scalar)
my $overWrite;
my $fsize;
if (0) {
$operation = "srm2_testPut";
$overWrite = 0;
$xCount++;
$fsize = $struct->{$file}->{size}*1024;
$command = "$operation $u_token 1 $overWrite $file 1000 2 $fsize";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "L: $_";
    my @m = split;
    if ( /r_token / ) { $r_token = $m[3]; }
#    if ( /TURL =/ ) { $lTURL = $_; $tstatus = $m[2]; $TURL = $m[5]; }
    if ( /state\[(\d+?)\]/ ) { 
	$findex = $1;
	$lTURL = $_; 
	chop($m[2]);
	$tstatus = $m[2]; 
	##NEW$tstatus = $RStatusCode{$m[2]}; 
##WAS	$TURL = $m[5]; 
	$TURL = $m[6]; 
	##PREV$TURL =~ m[(.*?)://(.*)];
	$TURL =~ m[(.*?)://.*?/(.*)];
	$sTURL = $2;
	my $lfile;
	if ( $fflag == 1 ) {
	    $lfile = $file;
	} else {
	    $lfile = $Lfile[$findex];
	}
	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{sTURL} = $sTURL;
##	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{r_token} = $r_token;
    }
    ##PREVif ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" || $tstatus ne "24" ) { 
    print " Put FAILED: $lTURL >$status< >$tstatus<\n";
    ##20 exit 1; 
}
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();
} else {
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
if (0) {
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite $file 1000 2 $fsize",
	  "LS", "$stat26");
} else {
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 1000 - - - 2 - - $file $fsize",
	  "LS", "$stat26", 1, 0);
}
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
##if ( $status ne '[OK]' ) { &printAndCExit(); }
$struct->{$file}->{TURL} = XtractVal('TURL =');
$struct->{$file}->{r_token} = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
}
### TEST exit 0;

##$TURL =~ m[(.*?)://(.*)];
##my $sTURL = $2;

## Put operation for the Lfile (Array)
if (0) {
    undef $status;
    ##PREV@res = `dpmcli/dpm-put --dpmutoken=$u_token --dpmproto rfio --dpmreqsize 100 @Lfile`;
    ##@res = `srm2_testput @Lfile`;
    $xCount++;
    my $lifet = 1000;
    my $nbfiles = scalar(@Lfile);
    $command = "$operation $u_token $nbfiles $overWrite";
    foreach my $kfile ( @Lfile ) {
	$lifet += 1000;
	my $fsize = $struct->{$kfile}->{size}*1024;
	if ( $lifet == 4000 ) {
	    $command .= " $kfile $lifet 2 $fsize";
	} else {
	    $command .= " $kfile $lifet 0 $fsize";
	}
	#p    print " VERIF: $kfile $struct->{$kfile}->{size} \n";
    }
    if ( $rfioSW ) {
	$command .= ' rfio';
    }
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
    chomp($dateST = `date '+%y%m%d-%H%M%S'`);
    $dateSTZ = `date --utc '+%FT%TZ'`;
    print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
    $tStart = time();
    @res = `$command 2>&1`;
    $tEnd = time();
    $tElapse = $tEnd - $tStart;
    $fflag = 2;
    foreach ( @res ) {
	print "L: $_";
	my @m = split;
	if ( /r_token / ) {
	    $r_token = $m[3];
	}
	#    if ( /TURL =/ ) { $lTURL = $_; $tstatus = $m[2]; $TURL = $m[5]; }
	if ( /state\[(\d+?)\]/ ) { 
	    $findex = $1;
	    $lTURL = $_; 
	    chop($m[2]);
	    $tstatus = $m[2]; 
	    ##NEW$tstatus = $RStatusCode{$m[2]}; 
	    ##WAS	$TURL = $m[5]; 
	    $TURL = $m[6]; 
	    ##PREV$TURL =~ m[(.*?)://(.*)];
	    $TURL =~ m[(.*?)://.*?/(.*)];
	    $sTURL = $2;
	    my $lfile;
	    if ( $fflag == 1 ) {
		$lfile = $file;
	    } else {
		$lfile = $Lfile[$findex];
	    }
	    $struct->{$lfile}->{TURL} = $TURL;
	    $struct->{$lfile}->{sTURL} = $sTURL;
	    ##	$struct->{$lfile}->{TURL} = $TURL;
	    $struct->{$lfile}->{r_token} = $r_token;
	}
	##PREVif ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
	if ( /request state/ ) {
	    $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ;
	}
    }
    ##PREVif ( $status ne "[OK]" || $tstatus ne "Ready," ) { 
    if ( $status ne "[OK]" || $tstatus ne "24" ) { 
	print " Put FAILED: $lTURL >$status< >$tstatus<\n";
	##20 exit 1; 
    }
    ##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
    &printAndCExit();
} else {
    $overWrite = 0;
    my $lifet = 1000;
    my $nbfiles = scalar(@Lfile);
    my $opsargs = "$u_token $nbfiles $overWrite";
    if (0) {
    foreach my $kfile ( @Lfile ) {
	$lifet += 1000;
	$fsize = $struct->{$kfile}->{size}*1024;
	if ( $lifet == 4000 ) {
	    $opsargs .= " $kfile $lifet 2 $fsize";
	} else {
	    $opsargs .= " $kfile $lifet 0 $fsize";
	}
    }
    if ( $rfioSW ) {
	$opsargs .= ' rfio';
    }
} else {
	$lifet += 1000;
	if ( $rfioSW ) {
	    $opsargs .= " $lifet - rfio - 2 - -";
	} else {
	    $opsargs .= " $lifet - - - 2 - -";
	}
	foreach my $kfile ( @Lfile ) {
	    $fsize = $struct->{$kfile}->{size}*1024;
	    $opsargs .= " $kfile $fsize";
	}
}
    XcuteComm("srm2_testPut",
	      "$opsargs",
	      "LA", "$stat26");
    $status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
    my $pr_token = XtractVal('r_token');
    foreach my $findex ( 0 .. $nbfiles-1 ) {
	$struct->{$Lfile[$findex]}->{TURL} = XtractVal("state\\[$findex\\] = 24, SRM_SPACE_AVAILABLE, TURL =");
	$struct->{$Lfile[$findex]}->{r_token} = $pr_token;
    }
    if ( $status ne '[OK]' ) { &printAndCExit(); }
}

## Put operation for the $Ffile
$fsize = $struct->{$Ffile}->{size}*1024;
if (0) {
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite $Ffile 1000 2 $fsize",
	  "FFI", "$stat26");
} else {
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 1000 - - - 2 - - $Ffile $fsize",
	  "FFI", "$stat26");
}
$struct->{$Ffile}->{TURL} = XtractVal("TURL =");
$struct->{$Ffile}->{r_token} = XtractVal("soap_call_ns1__srmPrepareToPut returned r_token");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Put operation for the pinTime expiration test (see end of script for relevant putDone)
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $pt_file = "${file}pt";
my $pt_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - - - 0 - - $pt_file $fsize",
	  "PTT1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pt_turl  = XtractVal('TURL =');
my $pt_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

&TransferTURL($pt_turl, $pt_input, 'PTT1b');

## Yet another put operation for the case where I will (inadvertantly) leave it pending with
## NEITHER a transfer NOR a putDone
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $pt2_file = "${file}pt2";
my $pt2_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 60 - - - 0 - - $pt2_file $fsize",
	  "PTT2a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pt2_turl  = XtractVal('TURL =');
my $pt2_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Yet another put operation for the case where I will (inadvertantly) leave it pending with
## NEITHER a transfer NOR a putDone >> same as above but with f_type 'P'
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $pt3_file = "${file}pt3";
my $pt3_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 80 - $protoc - 2 - - $pt3_file $fsize",
	  "PTT3a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pt3_turl  = XtractVal('TURL =');
my $pt3_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

##exit 0;  ##  TESTS V/P
## End of all put operations (the vector of files is in "keys %{$struct}")

##system("which rfcp");

## Three (transfer + putdone + get) operations for all files altogether
foreach my $kfile ( sort keys %{$struct} ) {

## Transfer step (uses rfcp when available) ...
    if (0) {
undef $status;
$xCount++;
@res = ();
my $lTURL = XtractGoodTurl($struct->{$kfile}->{TURL});
my $lLABL;
##if ( $rfioSW ) {
if ( $lTURL !~ /gsiftp/ ) {
    $operation = 'rfcp';
    $command = "$operation $struct->{$kfile}->{input} $lTURL";
    $lLABL = 'RFCP';
} else {
    $operation = 'globus-url-copy';
    $command = "$operation -dbg file:$struct->{$kfile}->{input} $lTURL";
    $lLABL = 'GSIFTP';
}
##PREV$command = "$operation $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL}";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `rfcp $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL} 2>&1`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
my $exitFlag = 0;
foreach ( @res ) {
    print " ${lLABL}_TRACE $_";
    if ( $ENV{RFIO_TRACE} ) {
#p	print " RFIO_TRACE $_";
    }
    if ( /226 Transfer complete./ ) { $status = "[OK]"; }
    if ( /226 Transfer Complete./ ) { $status = "[OK]"; }
    if ( /bytes in/ ) { $status = "[OK]"; }
    if ( /Command exited/ || /Timed out/ ) { 
	$status = "[FAILED]";
	$exitFlag = 1; 
    }
}
if ( $exitFlag && $ENV{RFIO_TRACE} ) {
    foreach ( @res ) {
	print " RFIO_FAIL $_";
    }
}

&printAndCExit();
###20 if ( $status ne "[OK]" ) { exit 1; }
###20 else { 
###20     if ( $ENV{RFIO_TRACE} ) {
###20 	
###20     }
###20     print " Operation: $operation = $status Duration: $tElapse \n"; 
###20 }
} else {
my $lTURL = XtractGoodTurl($struct->{$kfile}->{TURL});
my $lLABL;
my $opsargs;
if ( $lTURL !~ /gsiftp/ ) {
    $operation = 'rfcp';
    $opsargs = "$struct->{$kfile}->{input} $lTURL";
    $lLABL = 'RFCP_TRACE';
} else {
    $operation = 'globus-url-copy';
    $opsargs = "-dbg file:$struct->{$kfile}->{input} $lTURL";
    $lLABL = 'GSIFTP_TRACE';
}
    XcuteComm("$operation",
	      "$opsargs",
	      "$lLABL", 0, 1);
if ( $lTURL !~ /gsiftp/ ) {
    $status = ( XtractVal('seconds through') eq 'local' ) ? '[OK]' : '[FAILED]';
} else {
    $status = ( XtractVal('226 Transfer complete.') ) ? '[OK]' : '[FAILED]';
}
##$status = ( XtractVal('Command exited') ) ? '[FAILED]' : '[OK]';
##$status = ( XtractVal('Timed out') ) ? '[FAILED]' : '[OK]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
}

## Putdone step ...
    if (0) {
$operation = "srm2_testPutDone";
undef $status;
my $SURL;
@res = ();
$xCount++;
$command = "$operation $struct->{$kfile}->{r_token} $kfile";
##PREVprint " CMD: dpmcli/dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile`;
@res = `$command 2>&1`;
foreach ( @res ) {
    print "D: $_";
    my @m = split;
    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
$SURL = XtractVal('SURL =');
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if ( $SURL ne $kfile ) { 
    print " Mismatch0: $SURL $kfile \n";
    exit 1;
}
} else {
my $SURL;
    XcuteComm("srm2_testPutDone",
	      "$struct->{$kfile}->{r_token} $kfile",
	      "D", 0, 1);
$SURL = XtractVal('SURL =');
$status = ( "$SURL" eq "$kfile" ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { 
    print " Mismatch0: $SURL $kfile \n";
    &printAndCExit(); 
}
}

# Get operation (with rfio if available, gsiftp otherwise)
my $fTURL;
    if (0) {
$TURL = $struct->{$kfile}->{TURL};
undef $status;
$operation = "srm2_testGet";
@res = ();
$xCount++;
my $tMode;
if ( $rfioSW ) {
    $command = "$operation rfio $u_token $kfile";
    $tMode = 'rfio';
} else {
    $command = "$operation gsiftp $u_token $kfile";
    $tMode = 'gsiftp';
}
##PREVprint " CMD: dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "GF: $_";
    my @m = split;
    if ( /r_token / ) { $f_token = $m[3]; }
    if ( /TURL =/ ) { $fTURL = $m[5]; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
$fTURL = XtractVal('TURL =');
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if (0) {
if ( $TURL ne $fTURL ) { 
    print " Mismatch1: $TURL $fTURL \n";
    exit 1;
} else {
    print " TURL[".lc($tMode)."]: $fTURL \n";
}
} else {
#p    print " Mismatch1: $TURL $fTURL \n";
    print " TURL[".lc($tMode)."]: $fTURL \n";
}

} else {
my $tMode;
my $opsargs;
if ( $rfioSW ) {
    $opsargs = "rfio $u_token - $kfile";
    $tMode = 'rfio';
} else {
    $opsargs = "gsiftp $u_token - $kfile";
    $tMode = 'gsiftp';
}
    XcuteComm("srm2_testGet",
	      "$opsargs",
	      "GF", "$stat26", 1);
$fTURL = XtractVal('TURL =');
$f_token = XtractVal('r_token');
##&printAndCExit();
    print " TURL[".lc($tMode)."]: $fTURL \n";
}

# Get operation (always with gsiftp)
my $gTURL;
    if (0) {
undef $status;
@res = ();
$xCount++;
$command = "$operation gsiftp $u_token $kfile";
##PREVprint " CMD: dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "GG: $_";
    my @m = split;
    if ( /r_token / ) { $g_token = $m[3]; }
    if ( /TURL =/ ) { $gTURL = $m[5]; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
$gTURL = XtractVal('TURL =');
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

print " TURL[gsiftp]: $gTURL \n";

} else {
my $tMode;
my $opsargs;
    $opsargs = "gsiftp $u_token - $kfile";
    $tMode = 'gsiftp';
    XcuteComm("srm2_testGet",
	      "$opsargs",
	      "GG", "$stat26", 1);
$gTURL = XtractVal('TURL =');
$g_token = XtractVal('r_token');
##&printAndCExit();
    print " TURL[".lc($tMode)."]: $gTURL \n";
}

$struct->{$kfile}->{f_token} = $f_token;
$struct->{$kfile}->{g_token} = $g_token;
$struct->{$kfile}->{fTURL} = $fTURL;
$struct->{$kfile}->{gTURL} = $gTURL;
$TURL = $struct->{$kfile}->{TURL};
if ( $TURL =~ /rfio/ ) {
    if ( $TURL ne $fTURL ) { 
	print " Mismatch1: $TURL $fTURL \n";
	exit 1;
    }
} else {
    if ( $TURL ne $gTURL ) { 
	print " Mismatch1: $TURL $gTURL \n";
	exit 1;
    }
}

}
    print "<_________________________________________________________________________>\n\n";
## End Of Overall (transfer + putdone + get) operations for all files altogether

##PREV$operation = "dpm-getreqsummary";
$operation = "srm2_testGetRequestSummary";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestSummary",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "SU", 0, 1);
}

if (1) {
## Does not exist here ... (srmV2)
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestStatus",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST", "0", 1);
}
}

##exit 0;  ##  TESTS  pintime

$operation = "srm2_testExtendFileLifeTime";
foreach my $kfile ( sort keys %{$struct} ) {
	my $lt = $struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{f_token} $lt - $kfile",
		  "EF1", 0, 1);
	$lt = 2*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{g_token} $lt - $kfile",
		  "EG2", 0, 1);
}

##exit 0;  ##  TESTS queryDB

if (1) {
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestStatus",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST2", "0", 1);
}
}

$operation = "srm2_testExtendFileLifeTime";
foreach my $kfile ( sort keys %{$struct} ) {
	my $lt = 3*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{f_token} $lt - $kfile",
		  "EF3", 0, 1);
	$lt = 5*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{g_token} $lt - $kfile",
		  "EG5", 0, 1);
}

if (1) {
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestStatus",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST3", "0", 1);
}
}


##PREV$operation = "dpm-relfiles";
$operation = "srm2_testReleaseFiles";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testReleaseFiles",
		  "$endPoint $struct->{$kfile}->{f_token} $kfile",
		  "RL", 0, 1);
}

$operation = "srm2_testGetRequestStatus";
if (1) {
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestStatus",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST4", "0", 1);
}
}

    XcuteComm("srm2_testLs",
	      "-l $file @Lfile",
	      "LS", 0, 1);

## To get LsFullDetail

    XcuteComm("srm2_testLs",
	      "-dl $file @Lfile",
	      "LSFD", 0, 1);

if (1) {
## To crosscheck srm2_testReleaseFiles action specifically on this file
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $struct->{$Ffile}->{f_token}",
	      "STFI", '0', 1);
##$status = ( XtractVal('pin = Thu Jan  1 01:00:00 1970') eq 'pin = Thu Jan  1 01:00:00 1970' ) ? '[OK]' : '[FAILED]';
$status = ( XtractVal('pinDate = 1970-01-01T00:00:00Z') eq 'pinDate = 1970-01-01T00:00:00Z' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
  }



####exit 69;  ##  For special tests

## CopyCompare step ...

## for rfio and gsiftp 'get' operations
####foreach my $getType ( "fTURL", "gTURL" ) {
##foreach my $getType ( "gTURL" ) {
my @lTurlList = ( "gTURL" );
if ( $rfioSW ) {
    @lTurlList = ( "fTURL", "gTURL" );
}
foreach my $getType ( @lTurlList ) {
    ## for all 5 files at once ...
    foreach my $kfile ( sort keys %{$struct} ) {
	my $local;
	my $flocal;
	my $lTURL = XtractGoodTurl($struct->{$kfile}->{$getType});
	if ( $getType eq "fTURL" ) {  ##  rfio case
	    $operation = "rfcp";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."F";
##	    $struct->{$kfile}->{fLOCAL} = $local;
	    $flocal = $local;
	} else {  ##  gsiftp case
##	    if (0) {  ##  TEMP GG 13/04/05
	    if (1) {  ##  TEMP GG 13/04/05  ##  REVERTED on 10/05/05 for lxb1904
	    $operation = "globus-url-copy -vb";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."G";
##	    $struct->{$kfile}->{gLOCAL} = $local;
	    $flocal = "file://".$local;
	} else {
	    $operation = "rfcp";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."F";
##	    $struct->{$kfile}->{fLOCAL} = $local;
	    $flocal = $local;
	}
	}
	XcuteComm("$operation",
		  "$lTURL $flocal",
		  "DWNL_$getType", 0);  ##  download substep
	XcuteComm("diff -s",
		  "$struct->{$kfile}->{input} $local",
		  "DIFF_$getType", 0);  ##  diff substep, non-fatal
	unlink $local;
    }
}
## End of CopyCompare step ...

####exit 0;  ##  TEMPORARY ( to avoid killing any file ... )

foreach my $kfile ( sort keys %{$struct} ) {
##  $struct->{$kfile}->{copyX} = "${kfile}X";
    $struct->{$kfile}->{copyY} = "${baseSrm}/${mkdir_t}/$struct->{$kfile}->{short}"."Y";
    $struct->{$kfile}->{copyX} = "${kfile}X";
    $struct->{$kfile}->{copyZ} = "${kfile}Z";
}

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t",
	      "MK", 0, 1);

if ( $mach eq 'lxb1903' ) {
## 4 sequences about recursive Rmdir
## 1) empty dirs
## 2) non-empty dirs with V files
## 3) non-empty dirs with P files
###### 4) non-empty dirs with V+P files
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1",
	      "MKD1", 0, 1);
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2a",
	      "MKD2a", 0, 1);
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2b",
	      "MKD2b", 0, 1);
    XcuteComm("srm2_testRmdir",
	      "-R ${baseSrm}/$mkdir_t/D1",
	      "RMD1", 0, 1);

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1",
	      "MKD1V", 0, 1);
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2a",
	      "MKD2aV", 0, 1);

my $rmd_file = "${baseSrm}/$mkdir_t/D1/D2a/f2aV";
XcuteComm("srm2_testPut",
	  "$u_token 1 0 30 35 rfio - 0 0 0 $rmd_file $fsize",
	  "PTPD2aV", 0, 1);
my $rmd_token = XtractVal('r_token');
my $rmd_turl  = XtractVal('TURL =');
&TransferTURL($rmd_turl, '/etc/ldap.conf', 'D2aV');
&putDone($rmd_token, "$rmd_file", 'D2aV');

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2b",
	      "MKD2bV", 0, 1);

$rmd_file = "${baseSrm}/$mkdir_t/D1/D2b/f2bV";
XcuteComm("srm2_testPut",
	  "$u_token 1 0 35 30 rfio - 0 0 0 $rmd_file $fsize",
	  "PTPD2bV", 0, 1);
$rmd_token = XtractVal('r_token');
$rmd_turl  = XtractVal('TURL =');
&TransferTURL($rmd_turl, '/etc/ldap.conf', 'D2bV');
&putDone($rmd_token, "$rmd_file", 'D2bV');
sleep(35);

    XcuteComm("srm2_testRmdir",
	      "-R ${baseSrm}/$mkdir_t/D1",
	      "RMD1V", 0, 1);

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1",
	      "MKD1P", 0, 1);
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2a",
	      "MKD2aP", 0, 1);

$rmd_file = "${baseSrm}/$mkdir_t/D1/D2a/f2aP";
XcuteComm("srm2_testPut",
	  "$u_token 1 0 50 - rfio - 2 0 0 $rmd_file $fsize",
	  "PTPD2aP", 0, 1);
$rmd_token = XtractVal('r_token');
$rmd_turl  = XtractVal('TURL =');
&TransferTURL($rmd_turl, '/etc/ldap.conf', 'D2aP');
&putDone($rmd_token, "$rmd_file", 'D2aP');

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t/D1/D2b",
	      "MKD2bP", 0, 1);

$rmd_file = "${baseSrm}/$mkdir_t/D1/D2b/f2bP";
XcuteComm("srm2_testPut",
	  "$u_token 1 0 50 - rfio - 2 0 0 $rmd_file $fsize",
	  "PTPD2bP", 0, 1);
$rmd_token = XtractVal('r_token');
$rmd_turl  = XtractVal('TURL =');
&TransferTURL($rmd_turl, '/etc/ldap.conf', 'D2bP');
&putDone($rmd_token, "$rmd_file", 'D2bP');
##sleep(12);

    if (0) {
$rmd_file = "${baseSrm}/$mkdir_t/D1/D2b/f2cP";
XcuteComm("srm2_testPut",
	  "$u_token 1 0 50 - rfio - 2 0 0 $rmd_file $fsize",
	  "PTPD2cP", 0, 1);
$rmd_token = XtractVal('r_token');
$rmd_turl  = XtractVal('TURL =');
&TransferTURL($rmd_turl, '/etc/ldap.conf', 'D2cP');
&putDone($rmd_token, "$rmd_file", 'D2cP');
}

    XcuteComm("srm2_testRmdir",
	      "-R ${baseSrm}/$mkdir_t/D1",
	      "RMD1P", '12', 1);

    XcuteComm("srm2_testLs",
	      "-Rl ${baseSrm}/$mkdir_t/D1",
	      "RMD1PL", '0', 1);

    XcuteComm("srm2_testRm",
	      "${baseSrm}/$mkdir_t/D1/D2a/f2aP ${baseSrm}/$mkdir_t/D1/D2b/f2bP",
	      "RMD1PF", 0, 1);

    XcuteComm("srm2_testRmdir",
	      "-R ${baseSrm}/$mkdir_t/D1",
	      "RMD1PE", '0', 1);
}

## End of recursive Rmdir sequence ...

$operation = "srm2_testMv";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testMv",
		  "$kfile $struct->{$kfile}->{copyX}",
		  "MV", 0, 1);
}

my $args = "$file @Lfile";
foreach my $kfile ( sort keys %{$struct} ) {
    $args .= " $struct->{$kfile}->{copyX}";
}
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LS2", 27, 1);
##my $pda2 = XtractVal('request pathDetailArray');
$status = ( XtractVal("request pathDetailArray") eq '9' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testLs",
	      "-l ${baseSrm}/$mkdir_t",
	      "LSD", 0, 1);  ##  was 1

    XcuteComm("srm2_testLs",
	      "-2 ${baseSrm}/$mkdir_t",
	      "LSD2", 0, 1);  ##  was 1

    XcuteComm("srm2_testGetPermission",
	      "${baseSrm}/$mkdir_t $struct->{$Ffile}->{copyX}",
	      "GPD2", 0, 1);  ##  was 1

###BADexit 0;  ##  TEMP for checking gsiftp 24/05/05

$args = "";
    foreach my $kfile ( @Lfile[0,1] ) {
		$args .= " $struct->{$kfile}->{copyX}";
    }
## This one must partly fail ... with detailed explanation and filestatuses anyway
    XcuteComm("srm2_testRm",
	      "${args}_ff",
	      "RM", 27, 1);

## This particular one must partly fail as well ... with detailed explanation and filestatuses anyway
    XcuteComm("srm2_testRm",
	      "${args}",
	      "RMfp1", 27, 1);

## This particular one must fail in another way ... with detailed explanation and filestatuses anyway
    XcuteComm("srm2_testRm",
	      "${args}_ff",
	      "RMft2", 1, 1);

$args = "";
    foreach my $kfile ( sort keys %{$struct} ) {
	$args .= " $struct->{$kfile}->{copyX}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LS3", 27, 1);  ##  was 1
$status = ( XtractVal("request pathDetailArray") eq '5' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

$operation = "srm2_testMv";
foreach my $kfile ( $Lfile[2] ) {
	XcuteComm("srm2_testMv",
		  "$struct->{$kfile}->{copyX} $struct->{$kfile}->{copyY}",
		  "MVk", 0, 1);
}

## To check that Mv towards a Dir target (instead of a file target) is fine ...
foreach my $kfile ( $Lfile[2] ) {
	XcuteComm("srm2_testMv",
		  "$struct->{$kfile}->{copyY} ${baseSrm}",
		  "MVd1", 0, 1);
}

## Reverse the above Mv ...
foreach my $kfile ( $Lfile[2] ) {
	XcuteComm("srm2_testMv",
		  "${baseSrm}/$struct->{$kfile}->{short}"."Y ${baseSrm}/$mkdir_t",
		  "MVd2", 0, 1);
}

## Extracting the user/group codes from the remote DPM node PWD/GRP files
my %vogid = ();
my %vogauid = ();
my %vonid = ();
my %voaid = ();
my %votid = ();
my %duser = ();
## Useless but ...
my $sPWD;
my $sGRP;

## To extract $pxGRP and $pxDN values from the proxy (to be used in ACL comparison later) ...
&getPxAttributes();
##exit 0;

##my $tempDir;
##if ( $ENV{TEMPDIR} ) { $tempDir = $ENV{TEMPDIR}; }
##else { $tempDir = "/tmp"; }

if (0) {
$sPWD = "$tempDir/${mach}_PWD_$sfileH";
$sGRP = "$tempDir/${mach}_GRP_$sfileH";

system("globus-url-copy gsiftp://${mach}.cern.ch/${mach}:/etc/passwd file://$sPWD");
system("globus-url-copy gsiftp://${mach}.cern.ch/${mach}:/etc/group  file://$sGRP");
if ( ! ( -s "$sGRP" && -s "$sPWD" ) ) {
    system("rfcp ${mach}:/etc/passwd $sPWD");
    system("rfcp ${mach}:/etc/group  $sGRP");
}
}

####if ( -s "$sGRP" && -s "$sPWD" ) {

if (0) {
foreach my $grp ( "atlas", "cms", "dteam" ) {
    my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep ${grp}006 $sPWD`;
    if ( ! $gid ) { next; }
    $vogid{$grp} = $gid;
    my ($ruid, $rpl, $rgid, $spus) = split /:/, `grep ${gid} $sGRP`;
    $vogauid{$grp} = $ruid;
    my ($mode, $ix, $irt, $aid, $nest) = split /\s+/, `dpns-ls -ld /dpm/${site_domain}/home/${grp}`;
    $voaid{$aid} = $gid;
    $vonid{$gid} = $aid;
    $votid{$grp} = $aid;
    $votid{$aid} = $grp;
    print " GRP: $grp, GID: $gid, AID: $aid RUID: $ruid\n";
}
}

if (0) {
foreach my $grp ( "atlas", "cms", "dteam", "$pxGRP" ) {
    my @m = split /\s+/, `dpm-getgrpbynam $grp`;
    my $ruid = $m[-1];
    $vogauid{$grp} = $ruid;
    $vogauid{$grp} = $grp;
    my ($mode, $ix, $irt, $aid, $nest) = split /\s+/, `dpns-ls -ld /dpm/${site_domain}/home/${grp}`;
    $votid{$grp} = $aid;
    $votid{$aid} = $grp;
    print " GRP: $grp, AID: $aid RUID: $ruid\n";
}
} else {
foreach my $grp ( "atlas", "cms", "dteam", "$pxGRP" ) {
    $vogauid{$grp} = $grp;
}
}

if (0) {
foreach my $nid ( 1, 4, 5, 6 ) {
    my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep dteam00$nid $sPWD`;
    $duser{$auid} = $uid;
    $duser{$uid} = $auid;
    print " DTEAM: AUID: $auid, UID: $uid\n";
}
}

if (0) {
foreach my $nid ( 1, 2, 3 ) {
    ##my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep dteam00$nid $sPWD`;
    my $auid = "dteamtest0$nid";
    ##my $aout = `dpm-getusrbynam $auid`;
    my @m = split /\s+/, `dpm-getusrbynam $auid`;
    my $uid = $m[-1];
    $duser{$auid} = $uid;
    $duser{$uid} = $auid;
    print " DTEAM: AUID: $auid, UID: $uid\n";
}
}

####}

##BELOWmy ($cmod, $ccix, $cuid, $cgid, $cest) = split /\s+/, `dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X`;
##unlink "$sPWD";
##unlink "$sGRP";
if (0) {
my $atlas = $vogid{"atlas"};
my $cms   = $vogid{"cms"};
my $dteam = $vogid{"dteam"};
}

##Temp: back to old situation (until virtual ID fixes it - 10/11/05)
if (0) {
my $atlas = "atlas";
my $cms   = "cms";
my $dteam = "dteam";
}
if (0) {
my $atlas = "zp";
my $cms   = "zh";
my $dteam = "cg";
}
## Seems to be the good one for now 10/11/05, at least for lxgate22 & lxb1727
## Solves only the issue of making srm2_testSetPermission to work smoothly, not the comparison headache
my $atlas = $vogauid{"atlas"};
my $cms   = $vogauid{"cms"};
##my $dteam = $vogauid{"dteam"};
my $dteam = $vogauid{"$pxGRP"};

my $cfile = $file;
my $fileM = "$struct->{$Lfile[2]}->{copyY}";
## Collision test .........................................................................
## First setPermission to 444
$operation = "srm2_testSetPermission";
#OO$command = "$operation 2 4 4 $struct->{$cfile}->{copyX} 1 - dteam 4";
$command = "$operation 2 4 4 $struct->{$cfile}->{copyX} 1 - $dteam 4";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSP444: $_"; }

## Then issue a testMv, which MUST fail by now ... (14/07/05) with "Internal error"
## Seems to fail indeed, by now ... (12/09/05) with "File exists", which is far better ! TBC
	XcuteComm("srm2_testMv",
		  "$fileM $struct->{$cfile}->{copyX}",
		  "MWg", 11, 0);
## Then move back to setPermission to 664
$operation = "srm2_testSetPermission";
#OO$command = "$operation 2 6 4 $struct->{$cfile}->{copyX} 1 - dteam 6";
$command = "$operation 2 6 4 $struct->{$cfile}->{copyX} 1 - $dteam 6";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSP664: $_"; }

$args = "$file";
    foreach my $kfile ( $file, @Lfile ) {
	$args .= " $struct->{$kfile}->{copyX}";
	$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSk", 27, 1);  ##  was 1
$status = ( XtractVal("request pathDetailArray") eq '9' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testLs",
	      "-l2 ${baseSrm}/$mkdir_t",
	      "LSD3", 0, 1);  ##  was 1

    XcuteComm("srm2_testGetPermission",
	      "${baseSrm}/$mkdir_t $struct->{$Ffile}->{copyX}",
	      "GPD3", 0, 1);  ##  was 1

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMd", 12, 1);

$args = "$file";
    foreach my $kfile ( $file, @Lfile ) {
		$args .= " $struct->{$kfile}->{copyX}";
		$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSd", 27, 1);  ##  was 1
$status = ( XtractVal("request pathDetailArray") eq '9' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

$args = "";
    foreach my $kfile ( $Lfile[2] ) {
		$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testRm",
	      "$args",
	      "RMy", 0, 1);

$args = "$file";
foreach my $kfile ( $file, @Lfile ) {
	$args .= " $struct->{$kfile}->{copyX}";
	$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSr", 27, 1);  ##  was 1
$status = ( XtractVal("request pathDetailArray") eq '9' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMD", 0, 1);

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMA", 5, 1);

## Here begins the ACL testing section ...
#my $rfile = $Lfile[1];
my $rfile = $file;
my ($cmod, $ccix, $cuid, $cgid, $cest) = split /\s+/, `dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X`;

my @aclRes = ();
my @naclRes = ();
if (0) {
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	print "ACL0: # owner: $rest\n";
    } else {
	print "ACL0: $_";
    }
    #push @aclRes, "ACL1: $_";
    #my @m = split;
}
}

    XcuteComm("srm2_testGetPermission",
	      "$struct->{$rfile}->{copyX}",
	      "NACL0", 0, 1);  ##  was 1

## To get more info ...
    print "<_________________________________________________________________________>\n\n";
system("dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X");

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL0", 0, 1);  ##  was 1

# For owner and other permissions, first
$operation = "srm2_testSetPermission";
undef $status;
@res = ();
$command = "$operation 2 7 4 $struct->{$rfile}->{copyX} - -";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPOP: $_"; }

# For group permissions, now
#ADD, REMOVE, CHANGE= 0, 1, 2
$command = "$operation 0 - - $struct->{$rfile}->{copyX} 2 - $atlas 3 $cms 6";
##p print " CMD: $command\n";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGP36: $_"; }

$command = "$operation 2 - - $struct->{$rfile}->{copyX} 1 - $dteam 5";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGP5: $_"; }

if (0) {
#my @aclRes = ();
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL1: $_";
    push @aclRes, "ACL1: $_";
    #my @m = split;
}
}

    XcuteComm("srm2_testGetPermission",
	      "$struct->{$rfile}->{copyX}",
	      "NACL1", 0, 1);  ##  was 1
foreach ( @res ) {
  if ( /request/ ) { next; }
  #chomp;
  push @naclRes, "NACL1: $_";
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL1", 0, 1);  ##  was 1

$operation = "srm2_testSetPermission";
#also test a REMOVE op, and a GROUP NONE
$command = "$operation 1 - - $struct->{$rfile}->{copyX} 1 - $atlas 3"; 
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGPA: $_"; }
$command = "$operation 2 - - $struct->{$rfile}->{copyX} 1 - $cms 0";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGPC: $_"; }

if (0) {
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL2: $_";
    push @aclRes, "ACL2: $_";
    #my @m = split;
}
}

    XcuteComm("srm2_testGetPermission",
	      "$struct->{$rfile}->{copyX}",
	      "NACL2", 0, 1);  ##  was 1
foreach ( @res ) {
  if ( /request/ ) { next; }
  #chomp;
  push @naclRes, "NACL2: $_";
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL2", 0, 1);  ##  was 1

#dteamsgm#'/C=CH/O=CERN/OU=GRID/CN=Maarten Litmaath 1689'
#dteamsgm#'/C=CH/O=CERN/OU=GRID/CN=Jean-Philippe Baud 7183'
#dteamsgm#'/C=CH/O=CERN/OU=GRID/CN=Sophie Lemaitre 5847'
#.dteam#'/C=CH/O=CERN/OU=GRID/CN=Sophie Lemaitre 2268'
#.dteam#'/DC=ch/DC=cern/OU=Organic Units/OU=Users/CN=grodid/CN=397549/CN=Gilbert Grosdidier'
#.dteam#'/O=GRID-FR/C=FR/O=CNRS/OU=LAL/CN=Gilbert Grosdidier'
#.dteam#'/DC=org/DC=doegrids/OU=People/CN=Gilbert Grosdidier 378567'
#.lhcb#'/DC=org/DC=doegrids/OU=People/CN=Gilbert Grosdidier 378567'
my $dteamtest03 = '\'/C=CH/O=CERN/OU=GRID/CN=Sophie Lemaitre 5847\'';
$dteamtest03 = 'dteamtest03';

#For user permissions, later
$operation = "srm2_testSetPermission";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 dteam006 6";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 dteamtest03 6";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 $dteamtest03 6";
##$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 '/O=GRID-FR/C=FR/O=CNRS/OU=LAL/CN=Gilbert Grosdidier' 6";

    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP6: $_"; }
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 2 dteam004 4 dteam005 5";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 2 dteamtest01 4 dteamtest02 5";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP45: $_"; }

if (0) {
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL3: $_";
    push @aclRes, "ACL3: $_";
    #my @m = split;
}
}

    XcuteComm("srm2_testGetPermission",
	      "$struct->{$rfile}->{copyX}",
	      "NACL3", 0, 1);  ##  was 1
foreach ( @res ) {
  if ( /request/ ) { next; }
  #chomp;
  push @naclRes, "NACL3: $_";
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL3", 0, 1);  ##  was 1

$operation = "srm2_testSetPermission";
$command = "$operation 1 - - $struct->{$rfile}->{copyX} - 1 dteam004 4";
$command = "$operation 1 - - $struct->{$rfile}->{copyX} - 1 dteamtest01 4";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP4: $_"; }
$command = "$operation 2 - - $struct->{$rfile}->{copyX} - 1 dteam006 0";
$command = "$operation 2 - - $struct->{$rfile}->{copyX} - 1 $dteamtest03 0";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP0: $_"; }

if (0) {
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL4: $_";
    push @aclRes, "ACL4: $_";
    #my @m = split;
}
}

    XcuteComm("srm2_testGetPermission",
	      "$struct->{$rfile}->{copyX}",
	      "NACL4", 0, 1);  ##  was 1
foreach ( @res ) {
  if ( /request/ ) { next; }
  #chomp;
  push @naclRes, "NACL4: $_";
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL4", 0, 1);  ##  was 1

## ../../ns/dpns-getacl -a /dpm/dteam/grodid/srm_test_suite_file.fil606X

if (0) {
$operation = "compareACL";
    $xCount++;
    print "<_________________________________________________________________________>\n\n";
$status = compareACL("$baseDpm/$struct->{$rfile}->{short}X");
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();
}

$operation = "compareNACL";
    $xCount++;
    print "<_________________________________________________________________________>\n\n";
$status = compareNACL("$struct->{$rfile}->{copyX}");
#############################291206&printAndCExit();
#############################060207&printAndCExit();

    print "<_________________________________________________________________________>\n\n";

my $fileX = "$struct->{$rfile}->{copyX}";

if (0) {  ##  Obsoleted
XcuteComm("srm2_testReassignToUser",
	  "adummyname 1000 $fileX",
	  "RTU", 16);
}

XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 4000 2000 20000 0",
	  "RSP1", 0);
my $sp_token = XtractVal("soap_call_ns1__srmReserveSpace returned s_token:");
####exit 0;  ##  Test

if (0) {  ##  Moved
XcuteComm("srm2_testReleaseSpace",
	  "$endPoint $sp_token StSysInfo1 1",
	  "RLS", 16);
}

XcuteComm("srm2_testUpdateSpace", 
	  "$endPoint $sp_token StSysInfo1 2000 1000 10000",
	  "USP", 0);

if (0) {  ##  Obsoleted
XcuteComm("srm2_testCompactSpace", 
	  "$endPoint $sp_token StSysInfo1",
	  "CSP", 16);
}

##	  "$endPoint $s_token StSysInfo1 3000 1500 15000 0",
XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 30000000 15000000 15000 0",
	  "RSP2", 0);
my $sp_token1 = XtractVal("soap_call_ns1__srmReserveSpace returned s_token:");

XcuteComm("srm2_testGetSpaceMetaData", 
	  "$endPoint $sp_token $sp_token1",
	  "SMD", 0);

## Special JPB 18/10/06
if (1) {

    XcuteComm("srm2_testUpdateSpace", 
	      "$endPoint $sp_token1 StSysInfo1 2000 1000 15",
	      "USPex", 0);
    sleep 16;
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token1",
	      "SMDex", 0);
## Put operation for the $Ffile again with an expired space token
    $fsize = $struct->{$Ffile}->{size}*1024;
if (0) {
    XcuteComm("srm2_testPut",
	      "$u_token 1 $overWrite ${Ffile}ex 1000 2 $fsize - $sp_token1",
	      "FFIex", "$stat26");
} else {
    XcuteComm("srm2_testPut",
	      "$u_token 1 $overWrite 1000 - - $sp_token1 2 - - ${Ffile}ex $fsize",
	      "FFIex", "$stat26");
}
##    $struct->{$Ffile}->{TURL} = XtractVal("TURL =");
##    $struct->{$Ffile}->{r_token} = XtractVal("soap_call_ns1__srmPrepareToPut returned r_token");
    $status = ( XtractVal('request state') eq "7" || XtractVal('request state') eq "1" ) ? '[OK]' : '[FAILED]';
    if ( $status ne '[OK]' ) { &printAndCExit(); }
}

if (0) {  ##  Obsoleted
XcuteComm("srm2_testChangeFileStorageType",
	  "0 $fileX",
	  "FST", 16);
}

XcuteComm("srm2_testGetSpaceTokens", 
	  "$endPoint $s_token",
	  "GST", 0, 1);

XcuteComm("srm2_testReleaseSpace",
	  "$endPoint $sp_token StSysInfo1 1",
	  "RLS", 0, 1);

if (0) {  ##  Obsoleted
XcuteComm("srm2_testRemoveFiles",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RFL", 16);
}

XcuteComm("srm2_testAbortFiles",
	  "$struct->{$Ffile}->{r_token} $Ffile",
	  "AFL", 0, 1);

## Improve the AbortFiles test
    XcuteComm("srm2_testPut",
	      "$u_token 2 $overWrite 1000 - - - 2 - - ${Ffile}af1 $fsize ${Ffile}af2 $fsize",
	      "FAF1", "$stat26");
my $afr_token = XtractVal("soap_call_ns1__srmPrepareToPut returned r_token");
    XcuteComm("srm2_testPut",
	      "$u_token 1 0 1000 - - - 2 - - ${Ffile}af1 $fsize",
	      "FAFo0", "$stat26");
    XcuteComm("srm2_testPut",
	      "$u_token 1 2 1000 - - - 2 - - ${Ffile}af1 $fsize",
	      "FAFo2", "$stat26");
XcuteComm("srm2_testAbortFiles",
	  "$afr_token ${Ffile}af1 ${Ffile}af2",
	  "FAF2", 0, 1);
XcuteComm("srm2_testAbortFiles",
	  "$afr_token ${Ffile}af1",
	  "FAF3", 0, 1);
##    $status = ( XtractVal('request state') eq '4' ) ? '[OK]' : '[FAILED]';
##    if ( $status ne '[OK]' ) { &printAndCExit(); }

XcuteComm("srm2_testSuspendRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "SRQ", 16);

XcuteComm("srm2_testResumeRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RRQ", 16);

XcuteComm("srm2_testCheckPermission",
	  "$fileX",
	  "CHP", 0, 1);

XcuteComm("srm2_testGetRequestTokens",
		"$endPoint $u_token",
		"GID", 0, 1);

XcuteComm("srm2_testStatusOfLsRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "SLR", 16);

XcuteComm("srm2_testStatusOfReserveSpaceRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RSR", 16);

XcuteComm("srm2_testStatusOfUpdateSpaceRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RSR", 16);

XcuteComm("srm2_testChangeSpaceForFiles",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "CSF", 16);

XcuteComm("srm2_testStatusOfChangeSpaceForFilesRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "CSFR", 16);

if (0) {
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "EFS", 16);
}

XcuteComm("srm2_testPurgeFromSpace",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "PFS", 16);


if (0) {
$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz,dpmmode,dpmstatus,dpmfstat $u_token";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
##    if ( /Stat problem/||/send2nsd: NS009/||/Array Control/ ) { next; }
    print "GID2: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();
}

if (0) {
$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmturl:90,dpmrtok,dpmcred $u_token";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
##    if ( /Stat problem/||/send2nsd: NS009/||/Array Control/ ) { next; }
    if ( /r_token/ ) { next; }
    print "GID3: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();
}

$operation = "srm2_testMv";
foreach my $kfile ( $file ) {
    XcuteComm("srm2_testMv",
	      "$struct->{$kfile}->{copyX} $kfile",
	      "MVB", 0, 1);
}

if (0) {
$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz,dpmmode,dpmstatus,dpmfstat $u_token";
$xCount++;
$usage = `$operation 2>&1`;
print "<_________________________________________________________________________>\n\n";
$dateST = `date '+%y%m%d-%H%M%S'`;
print "++++ $$ $dateST\n";
print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "GID4: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();
}

TestPutOW(0);
##TestPutOW($overWrite++);
TestPutOW(1);

## The overwrite flag can take values (0|1|2) or (Never|Always|WhenFileIsDifferent)
## Last value is useless - implemented as being equal to Always
TestPutOW(2);

## So that this last test (with 2) is discarded.
if (0) {
my $fileB = "file.Bigger";
system (" cat $struct->{$file}->{input} $struct->{$file}->{input} > $fileB");
$struct->{$file}->{input} = "$fileB";
($struct->{$file}->{size}, $dss) = split /\s+/, `ls -s $struct->{$file}->{input} | awk '{print \$1}'`;
TestPutOW(2);
}

## Actually Transfer the file
$lTURL = XtractGoodTurl($struct->{$file}->{TURL});
undef $status;
$xCount++;
$operation = "rfcp";
$operation = "globus-url-copy";
@res = ();
$command = "$operation $struct->{$file}->{input} $lTURL";
$command = "$operation -dbg file:$struct->{$file}->{input} $lTURL";
@res = `$command 2>&1`;
foreach ( @res ) {
	    ## To fit new gridftp reply ...
	    if ( $_ =~ /^226 Transfer Complete\./ ) { $_ = "226 Transfer complete.\n"; }
    print " GSIFTP_TROW $_";
}
$status = ( XtractVal('226 Transfer complete.') eq '226 Transfer complete.' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

$operation = "srm2_testPutDone";
undef $status;
my $SURL;
@res = ();
$xCount++;
$command = "$operation $struct->{$file}->{r_token} $file";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "PDOW: $_";
    my @m = split;
    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
$SURL = XtractVal('SURL =');
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if ( $SURL ne $file ) { 
    print " Mismatch2: $SURL $file \n";
    exit 1;
}

## Tail of test about expired pinTime for PrepareToPut (cf top of script)
##&TransferTURL($pt_turl, $pt_input, 'PTT1b');

if (0) {
$operation = "srm2_testPutDone";
undef $status;
my $pt_surl;
@res = ();
$xCount++;
$command = "$operation $pt_token $pt_file";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "PTT1c: $_";
    my @m = split;
##    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
$pt_surl = XtractVal('SURL =');
&printAndCExit();
if ( $pt_surl ne $pt_file ) { 
    print " Mismatch_pt: $pt_surl $pt_file \n";
    exit 1;
}
} else {
    &putDone($pt_token, $pt_file, 'PTT1c', '1');
##    &putDone($pt_token, $pt_file, 'PTT1c');
}
## End of test about expired pinTime for PrepareToPut (cf top of script)

## This operation must fail, but the next one must succeed without spitting an SRM_DUPLICATION_ERROR
## To make it fail with SRM_INVALID_REQUEST, a wrong space_token is supplied to the srm2_testPut command
## ?? ee5fa4b0-b04c-405d-be9f-d5b43513228c
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $ilf_file = "${file}_ilf";
##my $wg_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 1000 - $protoc WRONG 2 - - $ilf_file $fsize",
	  "WRG1", "$stat26");
##$status = ( XtractVal('request state') eq '4' ) ? '[OK]' : '[FAILED]';
$status = ( XtractVal('request state') eq '1' ) ? '[OK]' : '[FAILED]';
##my $pt_turl  = XtractVal('TURL =');
##my $pt_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Test to check that infinite filelifetime is supplied (after putDone) to a SURL where P f_type is provided, with NO space_token
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $ilf_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 1000 - $protoc - 2 - - $ilf_file $fsize",
	  "ILF1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $ilf_turl  = XtractVal('TURL =');
my $ilf_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($ilf_turl, $ilf_input, 'ILF1b');
&putDone($ilf_token, $ilf_file, 'ILF1c');
## End of check for infinite filelifetime

## Test to check that day_length filelifetime is supplied (after putDone) to a SURL where V f_type is provided, with NO space_token
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $dlf_file = "${file}_dlf";
my $dlf_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - - - 0 - - $dlf_file $fsize",
	  "DLF1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $dlf_turl  = XtractVal('TURL =');
my $dlf_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($dlf_turl, $dlf_input, 'DLF1b');
&putDone($dlf_token, $dlf_file, 'DLF1c');
## End of check for day_length filelifetime

## Test to check that correct non_default filelifetime is supplied (after putDone) to a SURL where V f_type is provided, with NO space_token
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $clf_file = "${file}_clf";
my $clf_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 200 56789 $protoc - 0 - - $clf_file $fsize",
	  "CLF1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $clf_turl  = XtractVal('TURL =');
my $clf_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($clf_turl, $clf_input, 'CLF1b');
&putDone($clf_token, $clf_file, 'CLF1c');
## End of check for correct non_default filelifetime

## Test to check that space_token filelifetime is supplied (after putDone) to a SURL where V f_type is provided, with NO specified filelifetime
XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 300000 150000 456789 0",
	  "SLF0", 0);
##	  "$endPoint $s_token StSysInfo1 300000 150000 3456789 0",
my $sp_token_slf = XtractVal("s_token:");

$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $slf_file = "${file}_slf";
my $slf_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 200 - $protoc $sp_token_slf 0 - - $slf_file $fsize",
	  "SLF1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $slf_turl  = XtractVal('TURL =');
my $slf_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($slf_turl, $slf_input, 'SLF1b');
&putDone($slf_token, $slf_file, 'SLF1c');
## End of check for space_token filelifetime

## Test to check that space_token filelifetime is supplied (after putDone) to a SURL where V f_type is provided, with NORMAL specified filelifetime
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $sln_file = "${file}_sln";
my $sln_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 200 56789 $protoc $sp_token_slf 0 - - $sln_file $fsize",
	  "SLN1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $sln_turl  = XtractVal('TURL =');
my $sln_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($sln_turl, $sln_input, 'SLN1b');
&putDone($sln_token, $sln_file, 'SLN1c');
## End of check for space_token filelifetime with NORMAL specified filelifetime

## Test to check that space_token filelifetime is supplied (after putDone) to a SURL where V f_type is provided, with TOO_BIG specified filelifetime
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $slt_file = "${file}_slt";
my $slt_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 200 567890 $protoc $sp_token_slf 0 - - $slt_file $fsize",
	  "SLT1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $slt_turl  = XtractVal('TURL =');
my $slt_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($slt_turl, $slt_input, 'SLT1b');
&putDone($slt_token, $slt_file, 'SLT1c');
## End of check for space_token filelifetime with TOO_BIG specified filelifetime

## Test sequence about ExtendFileLifeTimeInSpace
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token_slf",
	      "XIS1", 0);
my $slf_rlft = XtractVal('r_lifetime:');
## Too big
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf 9999000 $slf_file",
	  "XIS2", 0);
#my $slf_lft = XtractVal('fileLifetime:');
$status = ( ($slf_rlft - XtractVal('fileLifetime:')) < 40 ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
## Smaller
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf 99000 $slf_file",
	  "XIS3", 0);
$status = ( (99000 - XtractVal('fileLifetime:')) == 0 ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
## Default (should return spaceLifetime)
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf - $slf_file",
	  "XIS4", 0);
## 60 was 40 was 20 was 10 before (changed for stress test)
$status = ( ($slf_rlft - XtractVal('fileLifetime:')) < 60 ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
## Bad SURL
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf 88000 ${slf_file}_wng",
	  "XIS5", 1);
## Bad SToken
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "${sp_token_slf}_wng 88000 $slf_file",
	  "XIS6", 4);
## Multiple SURLs (partial failure)
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf 77000 $slf_file ${slf_file}_wng $slt_file",
	  "XIS7", 27);
$status = ( (77000 - XtractVal('fileLifetime:')) == 0 ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
## Increase spaceLifetime beyond pool DEF_LIFETIME value (7.0d(ays))
XcuteComm("srm2_testUpdateSpace", 
	  "$endPoint $sp_token_slf StSysInfo1 300000 150000 3000000",
	  "XIS8", 0);
## Default (should return 604800 = 7.0d)
XcuteComm("srm2_testExtendFileLifeTimeInSpace",
	  "$sp_token_slf - $slf_file",
	  "XIS9", 0);
$status = ( (604800 - XtractVal('fileLifetime:')) == 0 ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## End of sequence about ExtendFileLifeTimeInSpace

## Test about infinite pinTime
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token - $file",
	      "INFa", "$stat26", 1);
my $ir_token = XtractVal('r_token');

##    XcuteComm("srm2_testGetRequestStatus",
##	      "$endPoint $ir_token",
##	      "INFd", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$ir_token -1 - $file",
		  "INFb", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $ir_token",
	      "INFc", 0, 1);
########TEMP GG $status = ( XtractVal('pinDate =') eq 'INFINITE' ) ? '[OK]' : '[FAILED]';
$status = ( XtractVal('pinDate =') =~ /1970-01-01T11:59:1|1970-01-01T11:59:2|1970-01-01T11:59:3|1970-01-01T11:59:4|1970-01-01T11:59:5|1970-01-01T12:00:00Z/ ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

if (1) {                                       ##################################### MUST BE ONE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## Test POSIX length sequence for file name
## Must pass
my $psl1_file = "${short}psl1";
my $flen = length("$psl1_file");
my $goodPad = "GoodPadding" x 25;
my $finePad = "FinePadding" x 25;
$psl1_file = substr("$goodPad", 0, 255 - $flen) . "$psl1_file";
##my $psl1_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl1_file $fsize",
	  "PSL1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
##my $psl1_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $psl1_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $psl1_token",
	      "PSL1b", 0, 1);
## Must pass
my $goodDir = substr("$finePad", 0, 255);
    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$goodDir",
	      "PSL4m", 0);
$status = ( XtractVal("request state") eq '0' ||
	    XtractVal("request state") eq '11') ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

my $psl4_file = "${short}psl4";
   $flen = length("$psl4_file");
##my $goodPad = "GoodPadding" x 25;
$psl4_file = substr("$goodPad", 0, 255 - $flen) . "$psl4_file";
##my $psl4_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/${goodDir}/$psl4_file $fsize",
	  "PSL4a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
##my $psl4_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $psl4_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $psl4_token",
	      "PSL4b", 0, 1);

## Must fail
my $psl2_file = "${short}psl2";
$flen = length("$psl2_file");
my $badPad = "BadPadding" x 27;
$psl2_file = substr("$badPad", 0, 256 - $flen) . "$psl2_file";
##my $psl2_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl2_file $fsize",
	  "PSL2a", "$stat26");
$status = ( XtractVal('request state') eq '1' ) ? '[OK]' : '[FAILED]';
##my $psl2_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
##my $psl2_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $psl2_token",
#	      "PSL2b", 0, 1);
}

## Test strange strings in file name
## Must pass
my $psl3_file = "${short}psl3";
##my $flen = length("$psl3_file");
##GOODmy $weirdPad = "Weird\\\'Padding\\\"Weird";
## -_.!~*'
my $weirdPad = "Weird\\\'Padding\\\"Weird-A_B\\\!C\\\~D\\\*E\\\#F";
$psl3_file = "$weirdPad" . "$psl3_file";
my $psl3_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl3_file $fsize",
	  "PSL3a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $psl3_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $psl3_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $psl3_token",
	      "PSL3b", 0, 1);

## Test for 2 successive PTP w/o PutDone in between. Must fail ...
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl3_file $fsize",
	  "PSL3c", "$stat26");
$status = ( XtractVal('request state') eq '11' ) ? '[OK]' : '[FAILED]';
## $psl3_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
## $psl3_token = XtractVal('r_token');
##if ( $status ne '[OK]' ) { &printAndCExit(); }

#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $psl3_token",
#	      "PSL3d", 0, 1);

##&TransferTURL("\"$psl3_turl\"", $psl3_input, 'PSL3e');
&TransferTURL(quotemeta($psl3_turl), $psl3_input, 'PSL3e');

## To check that weird strings are actually correctly handled. Must fail ...
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl3_file $fsize",
	  "PSL3f", "$stat26");
$status = ( XtractVal('request state') eq '11' ) ? '[OK]' : '[FAILED]';

## To check how fails the sequence PTP, Mv, PDone.
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $ptm_file = "${file}_ptm";
my $ptm_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 0 - - $ptm_file $fsize",
	  "PTM1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $ptm_turl  = XtractVal('TURL =');
my $ptm_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($ptm_turl, $ptm_input, 'PTM1b');
    XcuteComm("srm2_testMv",
	      "$ptm_file ${ptm_file}bizz",
	      "PTM1m", 0, 1);
## Must probably not fail any more ...
&putDone($ptm_token, $ptm_file, 'PTM1c');
##TEMP&putDone($ptm_token, $ptm_file, 'PTM1c', 1);

## Must probably fail ...
&putDone($ptm_token, "${ptm_file}bizz", 'PTM1d', 1);

#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $psl3_token",
#	      "PSL3g", 0, 1);

## Test srmLs avec offset/count

    XcuteComm("srm2_testLs",
	      "${baseSrm}",
	      "LSFL", 0, 1);  ##  was 1

    XcuteComm("srm2_testLs",
	      "-lO 5 5 ${baseSrm}",
	      "LSOC", 0, 1);  ##  was 1

## Tests about reserveSpace lifetime, and relevant file removal

my $sp_size1 = 15000000;  ##  in bytes
my $sp_size0 = 2*$sp_size1;
my $rq_size2 = 18000000;  
my $rq_size3 = 10000000;  
my $rq_size4 =  6000000;  
my $rq_size5 =  2000000;  
my $sp_ltime1 = 15000;  ##  in sec.
my $rq_ltime2 = 20000;
my $rq_ltime3 = 10000;
my $rq_ltime4 = 10000;
XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 $sp_size0 $sp_size1 $sp_ltime1 0",
	  "RSP3", 0);
my $sp_token3 = XtractVal("s_token:");
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3", 0);

## Must be rejected ...
$overWrite = 0;
my $rp_file1 = "${file}_R3a";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 $rq_ltime3 - $sp_token3 0 - - $rp_file1 $rq_size2",
	  "RSP3a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
##my $rp_turl2 = XtractVal('TURL =');
##my $rp_token2 = XtractVal('r_token');
####if ( $status ne '[OK]' ) { &printAndCExit(); }
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3a", 0);

## Must be accepted ... but the effective filelifetime will be shrinked to that of the space_token after putDone
my $rp_file2 = "${file}_R3b";
my $rp_input2 = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 $rq_ltime2 - $sp_token3 0 - - $rp_file2 $rq_size3",
	  "RSP3b", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $rp_turl2 = XtractVal('TURL =');
my $rp_token2 = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3b", 0);
&TransferTURL($rp_turl2, $rp_input2, 'TRT3b');
&putDone($rp_token2, $rp_file2, 'PTD3b');

## This one must fail ... to check that filestatuses are correctly filled
##&TransferTURL($rp_turl2, "$rp_input2 ${rp_input2}_bb", 'TRT3bb');
&putDone($rp_token2, "$rp_file2 ${rp_file2}_bb", 'PTD3bb', '1');

## Must be accepted ... 
##$fsize = $struct->{$file}->{size}*1024;
my $rp_file3 = "${file}_R3c";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 $rq_ltime3 - $sp_token3 0 - - $rp_file3 $rq_size3",
	  "RSP3c", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $rp_turl3 = XtractVal('TURL =');
my $rp_token3 = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3c", 0);

## Must be rejected ... total space size exceeds space_token one ??
##$fsize = $struct->{$file}->{size}*1024;
my $rp_file4 = "${file}_R3d";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 $rq_ltime3 - $sp_token3 0 - - $rp_file4 $rq_size4",
	  "RSP3d", "$stat26");
$status = ( XtractVal('request state') eq '8' ) ? '[OK]' : '[FAILED]';
##my $rp_turl4 = XtractVal('TURL =');
##my $rp_token4 = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3d", 0);

## Must be partly rejected ... total space size exceeds space_token one ??
##$fsize = $struct->{$file}->{size}*1024;
my $rp_file5 = "${file}_R3e";
my $rp_input5 = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 3 $overWrite 100 $rq_ltime3 $protoc $sp_token3 0 - - ${rp_file5}_0 $rq_size5 ${rp_file5}_1 $rq_size5 ${rp_file5}_2 $rq_size5",
	  "RSP3e", "$stat26");
$status = ( XtractVal('request state') eq '8' ) ? '[OK]' : '[FAILED]';
	my $rp_turl5_0 = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
	my $rp_turl5_1 = XtractVal('state\[1\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $rp_token5 = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($rp_turl5_0, $rp_input5, 'TRT3e_0');
&TransferTURL($rp_turl5_1, $rp_input5, 'TRT3e_1');
## Must be partly rejected ... 2 files out of 4 are not correct ...
&putDone($rp_token5, "${rp_file5}_0 ${rp_file5}_1 ${rp_file5}_2 ${rp_file5}_ff", 'PTD3e', '27');
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "SMD3e", 0);

## Partial success ...
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $rp_token5 ${rp_token5}_ff",
	      "GRT3e1", 27, 1);
## $status = ( XtractVal('PutR: state\[0\] =') eq '0,' ) ? '[OK]' : '[FAILED]';
$status = ( XtractVal('PutR: state\[1\] =') eq '0,' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Partial success ... as well ??
    XcuteComm("srm2_testGet",
	      "rfio $u_token - ${rp_file5}_0 ${rp_file5}_1 ${rp_file5}_2 ${rp_file5}_ff",
	      "GET3e", '27', 0);
##	      "GET3e", "$stat26", 0);
my $grp_token5 = XtractVal('r_token');

## Partial success ... again !
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $rp_token5 ${rp_token5}_ff $grp_token5",
	      "GRT3e2", 27, 1);
$status = ( XtractVal('GetR: state\[1\] =') eq '22,' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Check rp_file2 is still existing ...
    XcuteComm("srm2_testGet",
	      "rfio $u_token - $rp_file2",
	      "GET3b", "$stat26", 1);
my $grp_token2 = XtractVal('r_token');

    XcuteComm("srm2_testGetRequestSummary",
	      "$endPoint $grp_token2",
	      "GRS3b", 0, 1);
$status = ( XtractVal('state\[0\]:') eq "$stat26" ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Put operation for the reservespace oversize test 
# 1st file : simple overflow of the preparetoput reserved size
# 2nd file : same as before + overflow of the space reserved size
$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $po1_file = "${file}po1";
my $po2_file = "${file}po2";
my $po1_input = "$struct->{$Lfile[1]}->{input}";  ## actual size about 3.7MB
my $po2_input = "$struct->{$Lfile[1]}->{input}";  ## actual size about 3.7MB
XcuteComm("srm2_testPut",
	  "$u_token 2 $overWrite 200 - $protoc $sp_token3 0 - - $po1_file $fsize $po2_file $fsize",
	  "POT1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
	my $po1_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
	my $po2_turl = XtractVal('state\[1\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $po_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }
&TransferTURL($po1_turl, $po1_input, 'POT1b1');
&TransferTURL($po2_turl, $po2_input, 'POT1b2');
&putDone($po_token, "${po1_file} ${po2_file}", 'POT1c');
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "POT1d", 0);

## and finally release the space to check if the file disappears with it ...
XcuteComm("srm2_testReleaseSpace",
	  "$endPoint $sp_token3 StSysInfo1 1",
	  "RLS3b", 0);

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $rp_token2 $grp_token2",
	      "GRT3b", 0, 1);
$status = ( XtractVal('PutR: state\[0\] =') eq '0,' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## Put operation for the ExtendFileLifeTime test

XcuteComm("srm2_testUpdateSpace", 
	  "$endPoint $sp_token3 StSysInfo1 $sp_size0 $sp_size0 $sp_ltime1",
	  "PET1u", 0);
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $sp_token3",
	      "PET1m", 0);

$overWrite = 0;
$fsize = $struct->{$file}->{size}*1024;
my $pe1_file = "${file}pe1";
my $pe1_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 140 - $protoc $sp_token3 0 - - $pe1_file $fsize",
	  "PET1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pe1_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $pe1_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1r0", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe1_token -1 - $pe1_file",
		  "PET1b", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1ri", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe1_token 999 - $pe1_file",
		  "PET1c", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1r", 0, 1);
my $pe1_gr_token;
my $pet1get = 1;
if ( $pet1get ) {
    &TransferTURL($pe1_turl, $pe1_input, 'PET1tr');
    &putDone($pe1_token, "${pe1_file}", 'PET1pt');
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token - $pe1_file",
	      "PET1gr0", "$stat26", 1);
    $pe1_gr_token = XtractVal('r_token');
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_gr_token",
	      "PET1gr1", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 999999 $pe1_file",
		  "PET1d", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1sd", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - -1 $pe1_file",
		  "PET1e", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1se", 0, 1);
if ( $pet1get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe1_gr_token",
#	      "PET1gr2", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 9999 $pe1_file",
		  "PET1f", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe1_token",
	      "PET1sf", 0, 1);
if ( $pet1get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe1_gr_token",
#	      "PET1gr3", 0, 1);
}

## Same ExtendFileLifeTime test as above, but without spaceToken ... 'V'olatile file

my $pe2_file = "${file}pe2";
my $pe2_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 120 - $protoc - 0 - - $pe2_file $fsize",
	  "PET2a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pe2_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $pe2_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2r0", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe2_token -1 - $pe2_file",
		  "PET2b", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2ri", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe2_token 999 - $pe2_file",
		  "PET2c", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2r", 0, 1);
my $pe2_gr_token;
my $pet2get = 1;
if ( $pet2get ) {
    &TransferTURL($pe2_turl, $pe2_input, 'PET2tr');
    &putDone($pe2_token, "${pe2_file}", 'PET2pt');
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token 1200 $pe2_file",
	      "PET2gr0", "$stat26", 1);
    $pe2_gr_token = XtractVal('r_token');
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_gr_token",
	      "PET2gr1", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 999999 $pe2_file",
		  "PET2d", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2sd", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - -1 $pe2_file",
		  "PET2e", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2se", 0, 1);
if ( $pet2get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe2_gr_token",
#	      "PET2gr2", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 9999 $pe2_file",
		  "PET2f", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe2_token",
	      "PET2sf", 0, 1);
if ( $pet2get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe2_gr_token",
#	      "PET2gr3", 0, 1);
}

## Same ExtendFileLifeTime test as above, but with a 'P'ermanent file

my $pe3_file = "${file}pe3";
my $pe3_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - $pe3_file $fsize",
	  "PET3a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
my $pe3_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $pe3_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3r0", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe3_token -1 - $pe3_file",
		  "PET3b", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3ri", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pe3_token 999 - $pe3_file",
		  "PET3c", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3r", 0, 1);
my $pe3_gr_token;
my $pet3get = 1;
if ( $pet3get ) {
    &TransferTURL($pe3_turl, $pe3_input, 'PET3tr');
    &putDone($pe3_token, "${pe3_file}", 'PET3pt');
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token - $pe3_file",
	      "PET3gr0", "$stat26", 1);
    $pe3_gr_token = XtractVal('r_token');
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_gr_token",
	      "PET3gr1", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 999999 $pe3_file",
		  "PET3d", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3sd", 0, 1);
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - -1 $pe3_file",
		  "PET3e", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3se", 0, 1);
if ( $pet3get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe3_gr_token",
#	      "PET3gr2", 0, 1);
}
	XcuteComm("srm2_testExtendFileLifeTime",
		  "- - 9999 $pe3_file",
		  "PET3f", 0, 1);
    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pe3_token",
	      "PET3sf", 0, 1);
if ( $pet3get ) {
#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $pe3_gr_token",
#	      "PET3gr3", 0, 1);
}

## Test putDone on an already putDone file : must fail
     &putDone($pe1_token, "${pe1_file}", 'AGN1pt', 1);
#    XcuteComm("srm2_testPutDone",
#	      "$pe1_token $pe1_file",
#	      "AGN1pt", 1, 1);
####&putDone($pe1_token, "${pe1_file}", 'AGN1pt_bis', 1);

## Test ExtendFileLifeTime on an already putDone file : must fail
    XcuteComm("srm2_testExtendFileLifeTime",
	      "$pe1_token 9999 -  $pe1_file",
	      "AGN2pt", 1, 1);

## Test partial Get failure + partial then global ReleaseFiles
    XcuteComm("srm2_testGet",
	      "rfio $u_token - ${pe3_file}_bid $pe3_file $pe2_file $pe1_file",
	      "GRETa", '27', 1);
my $gret_token = XtractVal('r_token');

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $gret_token",
	      "GRETb", 0, 1);

if (1) {
    XcuteComm("srm2_testReleaseFiles",
	      "$endPoint $gret_token $pe2_file $pe3_file",
	      "GRETc", 0, 1);
} else {
##    XcuteComm("srm2_testReleaseFiles",
##	      "$endPoint $gret_token $pe2_file $pe1_file",
##	      "GRETc", 0, 1);
    XcuteComm("srm2_testReleaseFiles",
	      "$endPoint $gret_token $pe2_file",
	      "GRETc", 0, 1);
}

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $gret_token",
	      "GRETd", 0, 1);

    XcuteComm("srm2_testReleaseFiles",
	      "$endPoint $gret_token",
	      "GRETe", '27', 1);

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $gret_token",
	      "GRETf", 0, 1);

## Test ReleaseFiles on Put request : must fail
    XcuteComm("srm2_testReleaseFiles",
	      "$endPoint $pe3_token",
	      "GRETp", 4, 1);

## AbortRequest sequence ...
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token - $file",
	      "GRQa", "$stat26", 1);
my $gr_token = XtractVal('r_token');

    XcuteComm("srm2_testGetRequestSummary",
	      "$endPoint $gr_token",
	      "GRQb", 0, 1);
$status = ( XtractVal('state\[0\]:') eq "$stat26" ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testAbortRequest",
	      "$endPoint $gr_token",
	      "GRQc", 0, 1);

    XcuteComm("srm2_testGetRequestSummary",
	      "$endPoint $gr_token",
	      "GRQd", 0, 1);
$status = ( XtractVal('state\[0\]:') eq '20' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

## testBring sequence ...
if (0) {
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token $file",
	      "BRGa", 26, 1);
  }
    XcuteComm("srm2_testBring",
	      "$u_token $file",
	      "BRGa", "$stat26", 1);
my $brg_token = XtractVal('r_token');
##$status = ( XtractVal("request state0") eq '18' ) ? '[OK]' : '[FAILED]';
##if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestSummary",
	      "$endPoint $brg_token",
	      "BRGsm", 0, 1);
$status = ( XtractVal('state\[0\]:') eq "$stat26" ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $brg_token",
	      "BRGst", '0', 1);
$status = ( XtractVal('SURL =') eq "${file}," ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testReleaseFiles",
	      "$endPoint $brg_token $file",
	      "BRGrf", '0', 1);

## testCopy sequence ...
    XcuteComm("srm2_testCopy",
	      "$u_token $file srm://dummy.cern.ch:${portnum}//dpm/cern.ch/home/dteam/srmv22_Tfile.fil103926c28A0cp01",
	      "CPYa", '16', 0);
##TEMPREM	      "CPYa", '00', 1);
my $cpy_token = XtractVal('r_token');
##TEMPREM $status = ( XtractVal("request state0") eq '18' ) ? '[OK]' : '[FAILED]';
####$status = ( XtractVal("request state") eq '16' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

if (0) { ##TEMPREM
    XcuteComm("srm2_testGetRequestSummary",
	      "$endPoint $cpy_token",
	      "CPYs", 0, 1);
$status = ( XtractVal('state\[0\]:') eq '18' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $cpy_token",
	      "CPYr", '0', 1);
$status = ( XtractVal('sSURL =') eq "${file}," ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }
}

if (0) {
## test POSIX length sequence

## Must pass
my $psl1_file = "${short}psl1";
my $flen = length("$psl1_file");
my $goodPad = "GoodPadding" x 25;
$psl1_file = substr("$goodPad", 0, 255 - $flen) . "$psl1_file";
##my $psl1_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl1_file $fsize",
	  "PSL1a", "$stat26");
$status = ( XtractVal('request state') eq "$stat26" ) ? '[OK]' : '[FAILED]';
##my $psl1_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
my $psl1_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $psl1_token",
	      "PSL1b", 0, 1);

## Must fail
my $psl2_file = "${short}psl2";
$flen = length("$psl2_file");
my $badPad = "BadPadding" x 27;
$psl2_file = substr("$badPad", 0, 256 - $flen) . "$psl2_file";
##my $psl2_input = "$struct->{$file}->{input}";
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 100 - $protoc - 2 - - ${baseSrm}/$psl2_file $fsize",
	  "PSL2a", "$stat26");
$status = ( XtractVal('request state') eq '1' ) ? '[OK]' : '[FAILED]';
##my $psl2_turl = XtractVal('state\[0\] = 24, SRM_SPACE_AVAILABLE, TURL =');
##my $psl2_token = XtractVal('r_token');
if ( $status ne '[OK]' ) { &printAndCExit(); }

#    XcuteComm("srm2_testGetRequestStatus",
#	      "$endPoint $psl2_token",
#	      "PSL2b", 0, 1);

## Test srmLs avec offset/count

    XcuteComm("srm2_testLs",
	      "${baseSrm}",
	      "LSFL", 0, 1);  ##  was 1

    XcuteComm("srm2_testLs",
	      "-lO 5 5 ${baseSrm}",
	      "LSOC", 0, 1);  ##  was 1
}

## ExtendFileLifetime on EXPIRED (GET then PUT) TURL sequence ...
    XcuteComm("srm2_testGet",
	      "gsiftp $u_token 10 $file",
	      "GEXTa", "$stat26", 1);
my $gxr_token = XtractVal('r_token');

XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite 10 - $protoc - 2 - - ${file}pxr $fsize",
	  "PEXTa", "$stat26", 1);
my $pxr_token = XtractVal('r_token');

sleep(12);

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $gxr_token",
	      "GEXTst", '0', 1);
$status = ( XtractVal('state\[0\] =') eq '6,' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

    XcuteComm("srm2_testGetRequestStatus",
	      "$endPoint $pxr_token",
	      "PEXTst", '0', 1);
$status = ( XtractVal('state\[0\] =') eq '24,' ) ? '[OK]' : '[FAILED]';
if ( $status ne '[OK]' ) { &printAndCExit(); }

	XcuteComm("srm2_testExtendFileLifeTime",
		  "$gxr_token 100 - $file",
		  "GEXTb", 1, 1);

	XcuteComm("srm2_testExtendFileLifeTime",
		  "$pxr_token 100 - ${file}pxr",
		  "PEXTb", 1, 1);

XcuteComm("srm2_testGetSpaceTokens", 
	  "$endPoint $s_token",
	  "GST2", 0, 1);
my @s_tokens = ();
foreach ( @res ) {
    if ( ! /s_token/ ) { next; }
    my @ms = split /\s+/, $_;
    push @s_tokens, $ms[-1];
}
foreach my $s_token ( @s_tokens ) {
XcuteComm("srm2_testGetSpaceMetaData", 
	  "$endPoint $s_token",
	  "SMD1a", 0);
#my $t_space = XtractVal('t_space:');
my $g_space = XtractVal('g_space:');
my $u_space = XtractVal('u_space:');
#my $r_lifetime = XtractVal('r_lifetime:');
##if ( $g_space == 30000000 ) {
##$u_space = int((($g_space-$u_space+1000000)/1000000))*1000000;

if ( $g_space >= 14000000 ) {
$u_space = int((($g_space-$u_space+1000)/1000))*1000;

XcuteComm("srm2_testUpdateSpace", 
	  "$endPoint $s_token - - $u_space -",
	  "SMD1b", 0);
    XcuteComm("srm2_testGetSpaceMetaData", 
	      "$endPoint $s_token",
	      "SMD1c", 0);
}
}

## End of event loop -----------------------------------------------------------------------

    print "<_________________________________________________________________________>\n\n";
my $tAlmost = time() - $otStart;
$operation = "OVERALL SUITE";
print " Operation: $operation = $status Duration: $tAlmost sec.\n";
    print "<_________________________________________________________________________>\n\n";

exit 0;

####################################################################################################
sub printAndCExit {
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
    if ( $status ne "[OK]" ) {
	foreach ( @res ) {
	    print " FAILURE: $_";
	}
	print " Operation: $operation = $status Duration: $tElapse \n";
#p	print "<_________________________________________________________________________>\n\n";
	exit 1;
    } else {
	print " Operation: $operation = $status Duration: $tElapse \n";
#p	print "<_________________________________________________________________________>\n\n";
	return;
    }
}

####################################################################################################
sub XtractVal {
    my $string = shift;
##    $flat =~ m/$string\s+(.+?)\s+/;
    my $val = "";
    foreach (@res) {
	if ( /$string\s+(\S+?)\s+/ ) {
	    $val = $1;
	} elsif ( /($string\S*?)\s+/ ) {
	    $val = $1;
	}
    }
    print " XtractVal: >$string< >$val< \n";
    return $val;
}

####################################################################################################
sub getPxAttributes {

    ## extract the 'role' or 'VO' (DPM view)
    chomp(my $fqan0 = `voms-proxy-info -fqan | head -1`);
##    if ( $fqan0 =~ 'WARNING' ) {
##	chomp($fqan0 = `voms-proxy-info -vo | head -1`);
    if ( $fqan0 eq '' ) {
	$pxGRP = $voteam;
	print " Got pxGRP: $pxGRP\n";
    } else {
	my @m = split /\//, $fqan0;
	$pxGRP = $m[1];
	foreach my $in ( 2 .. scalar(@m) ) {
	    if ( $m[$in] =~ 'NULL' ) { last; }
	    $pxGRP .= "/" . "$m[$in]";
	}
	print " Got pxGRP: $pxGRP\n";
    }

    ## extract the 'DN'
    chomp(my $ident = `voms-proxy-info -identity | tail -1`);
    $pxDN = "$ident";
##NotAnyMore    $pxDN =~ s/ /_/g;
    print " Got pxDN: $pxDN\n";

    return 0;
}

####################################################################################################
sub XcuteComm {

    $operation = shift;
    my $cargs = shift;
    my $label = shift;
    my $xpectStat = shift;
    my $fatalXit  = shift;
    my $runGdb = shift;
    $xCount++;

##old    undef $status;
    $status = "[UNDEF]";
    @res = ();
    $usage = `$operation 2>&1`;
    $command = "$operation $cargs";
  chomp($dateST  = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##O    my $tStart = time();
    $tStart = time();

    if (0) {
	if ( $operation ne 'srm2_testPut' ) {
	@res = `$command 2>&1`;
    } else {
	system("$command");
}
#OLD    my $exitFlag = $?;
#OLD    print " STEP: $xCount OPERATION: $operation RET: $exitFlag\n";
    } else {
	##print " fatalXit runGdb: >$fatalXit< >$runGdb< \n";
	if ( $runGdb ) {
	    $command = "gdb $operation <<EOGDB \
set args $cargs \
run \
where \
quit \
EOGDB";
        open RES, "$command 2>&1 |";
	while ( <RES> ) {
	    push @res, $_;
	}
	close RES;
	} else {
        open RES, "$command 2>&1 |";
##      open RES, "$command |";
	while ( <RES> ) {
	    ## To fit new gridftp reply ...
	    if ( $_ =~ /^226 Transfer Complete\./ ) { $_ = "226 Transfer complete.\n"; }
# 	    if ( ! /^\s+$|^\.\.\.$/ ) {
# 		print "$label: $_";
# 	    }
	    push @res, $_;
	}
	close RES;
    }
    }
    my $exitFlag = $?;
#    print " STEP: $xCount OPERATION: $operation RET: $exitFlag\n";

# # 	foreach ( @res ) {
# # 	    print " RES: $_";
# # 	}

##O    my $tEnd = time();
##O    my $tElapse = $tEnd - $tStart;
    $tEnd = time();
    $tElapse = $tEnd - $tStart;
##    my $exitFlag = 0;
    foreach ( @res ) {
	$_ =~ s/req.*TURL/TURL/;
	if ( ! /^\s+$|^\.\.\.$/ ) { print "$label: $_"; }
	my @m = split;
	if ( /request state/ ) {
	    $status = ( "$xpectStat" eq "$m[2]" ) ? "[OK]" : "[FAILED]" ;
	} elsif ( /Command exited/ || /Timed out/ ) {
	    $status = "[FAILED]" ;
	}
    }

    if ( $status eq "[UNDEF]" ) {
	##if ( $exitFlag ) { $status = "[FAILED]" ; }
	$status = ( $exitFlag ) ? "[FAILED]" : "[OK]" ;
##	$status = ( $exitFlag && $fatalXit ) ? "[FAILED]" : "[OK]" ;
    }

    if ( $status ne "[OK]" && $fatalXit) {
#    if ( $status ne "[OK]" && ( $fatalXit || $exitFlag ) ) {
	foreach ( @res ) {
	    print " FAILURE: $_";
	}
	print " Operation: $operation = $status Duration: $tElapse \n";
	print "<_________________________________________________________________________>\n\n";
	exit 1;
    } else {
	if ( $exitFlag ) {
	    print " RET: $exitFlag\n";
 	foreach ( @res ) {
 	    print " WARNING: $_";
 	}
        }
	print " Operation: $operation = $status Duration: $tElapse \n";
	print "<_________________________________________________________________________>\n\n";
    }

##  if ( $db1 && $operation eq 'srm2_testExtendFileLifeTime' ) {
    if ( $db1 && $operation =~ 'srm2_testExtendFileLifeTime' ) {
	##my @m = split /\s+/, $cargs;
	&queryDB($label, $operation, $cargs);
    }

##system('ls -l core*');

    return 0;

}

####################################################################################################
sub compareNACL {
    my $file = shift;
    my @comp;

    ## Should use here local definition for grp names
    ## That is to say %votid
my $atlas = "atlas";
my $cms   = "cms";
my $dteam = "$pxGRP";
my %duserl = ();
$duserl{dteamtest01} = "dteamtest01";
$duserl{dteamtest02} = "dteamtest02";
$duserl{dteamtest03} = "$dteamtest03";

    my $tstNAME = "$pxDN";
    my $tstGRP  = "$pxGRP";

    @comp = <<"EOACL";
NACL1: Stat: 0, SRM_SUCCESS
NACL1: Path: $file
NACL1: OwnR: $tstNAME
NACL1: OwnP: RWX
NACL1: OthP: R
NACL1: GrpP: 3 $dteam:RX $atlas:WX $cms:RW
NACL1: =================
NACL2: Stat: 0, SRM_SUCCESS
NACL2: Path: $file
NACL2: OwnR: $tstNAME
NACL2: OwnP: RWX
NACL2: OthP: R
NACL2: GrpP: 2 $dteam:RX $cms:NONE
NACL2: =================
NACL3: Stat: 0, SRM_SUCCESS
NACL3: Path: $file
NACL3: OwnR: $tstNAME
NACL3: OwnP: RWX
NACL3: OthP: R
NACL3: UsrP: 3 $duserl{dteamtest01}:R $duserl{dteamtest02}:RX $duserl{dteamtest03}:RW
NACL3: GrpP: 2 $dteam:RX $cms:NONE
NACL3: =================
NACL4: Stat: 0, SRM_SUCCESS
NACL4: Path: $file
NACL4: OwnR: $tstNAME
NACL4: OwnP: RWX
NACL4: OthP: R
NACL4: UsrP: 2 $duserl{dteamtest02}:RX $duserl{dteamtest03}:NONE
NACL4: GrpP: 2 $dteam:RX $cms:NONE
NACL4: =================
EOACL

my $naclRes = join "", @naclRes;

    foreach ( @naclRes ) {
      ##print;
    }

    foreach ( @comp ) {
      ##print;
    }

if ( "$naclRes" ne "@comp" ) { 
    print "\n NACLRES:> \n@naclRes \n";
    print "    COMP:> \n@comp \n";
    print " compareNACL [FAILED]\n"; 
    return "[FAILED]"; 
}

    return "[OK]";
}

####################################################################################################
sub compareACL {
    my $file = shift;
    my @comp;

    if ( 0 ) {
    @comp = <<"EOACL";
ACL1: # file: $file
ACL1: # owner: $tstNAME
ACL1: # group: cg
ACL1: user::rwx
ACL1: group::r-x                #effective:r-x
ACL1: group:zp:-wx              #effective:-wx
ACL1: group:zh:rw-              #effective:rw-
ACL1: mask::rwx
ACL1: other::r--
ACL2: # file: $file
ACL2: # owner: $tstNAME
ACL2: # group: cg
ACL2: user::rwx
ACL2: group::r-x                #effective:r-x
ACL2: group:zh:---              #effective:---
ACL2: mask::rwx
ACL2: other::r--
ACL3: # file: $file
ACL3: # owner: $tstNAME
ACL3: # group: cg
ACL3: user::rwx
ACL3: user:dteam004:r--         #effective:r--
ACL3: user:dteam005:r-x         #effective:r-x
ACL3: user:dteam006:rw-         #effective:rw-
ACL3: group::r-x                #effective:r-x
ACL3: group:zh:---              #effective:---
ACL3: mask::rwx
ACL3: other::r--
ACL4: # file: $file
ACL4: # owner: $tstNAME
ACL4: # group: cg
ACL4: user::rwx
ACL4: user:dteam005:r-x         #effective:r-x
ACL4: user:dteam006:---         #effective:---
ACL4: group::r-x                #effective:r-x
ACL4: group:zh:---              #effective:---
ACL4: mask::rwx
ACL4: other::r--
EOACL
} else {

    ## Should use here local definition for grp names
    ## That is to say %votid
#my $atlas = $votid{"atlas"};
#my $cms   = $votid{"cms"};
#my $dteam = $votid{"dteam"};
my $atlas = "atlas";
my $cms   = "cms";
#my $dteam = "dteam";
my $dteam = "$pxGRP";
my %duserl = ();
$duserl{dteamtest01} = "dteamtest01";
$duserl{dteamtest02} = "dteamtest02";
$duserl{dteamtest03} = "dteamtest03";

##    my $tstNAME = "$cuid";
##    my $tstGRP  = "$cgid";
    my $tstNAME = "$pxDN";
    my $tstGRP  = "$pxGRP";
    @comp = <<"EOACL";
ACL1: # file: $file
ACL1: # owner: $tstNAME
ACL1: # group: $tstGRP
ACL1: user::rwx
ACL1: group::r-x                #effective:r-x
ACL1: group:$atlas:-wx            #effective:-wx
ACL1: group:$cms:rw-              #effective:rw-
ACL1: mask::rwx
ACL1: other::r--
ACL2: # file: $file
ACL2: # owner: $tstNAME
ACL2: # group: $tstGRP
ACL2: user::rwx
ACL2: group::r-x                #effective:r-x
ACL2: group:$cms:---              #effective:---
ACL2: mask::rwx
ACL2: other::r--
ACL3: # file: $file
ACL3: # owner: $tstNAME
ACL3: # group: $tstGRP
ACL3: user::rwx
ACL3: user:$duserl{dteamtest01}:r--         #effective:r--
ACL3: user:$duserl{dteamtest02}:r-x         #effective:r-x
ACL3: user:$duserl{dteamtest03}:rw-         #effective:rw-
ACL3: group::r-x                #effective:r-x
ACL3: group:$cms:---              #effective:---
ACL3: mask::rwx
ACL3: other::r--
ACL4: # file: $file
ACL4: # owner: $tstNAME
ACL4: # group: $tstGRP
ACL4: user::rwx
ACL4: user:$duserl{dteamtest02}:r-x         #effective:r-x
ACL4: user:$duserl{dteamtest03}:---         #effective:---
ACL4: group::r-x                #effective:r-x
ACL4: group:$cms:---              #effective:---
ACL4: mask::rwx
ACL4: other::r--
EOACL
}

# # ACL1: # file: $file
# # ACL1: # owner: $tstNAME
# # ACL1: # group: $tstGRP
# # ACL1: user::rwx
# # ACL1: group::r--                #effective:r--
# # ACL1: group:atlas:-wx            #effective:-wx
# # ACL1: group:cms:rw-              #effective:rw-
# # ACL1: mask::rwx
# # ACL1: other::r--
# # ACL2: # file: $file
# # ACL2: # owner: $tstNAME
# # ACL2: # group: $tstGRP
# # ACL2: user::rwx
# # ACL2: group::r--                #effective:r--
# # ACL2: group:cms:---              #effective:---
# # ACL2: mask::rwx
# # ACL2: other::r--
# # ACL3: # file: $file
# # ACL3: # owner: $tstNAME
# # ACL3: # group: $tstGRP
# # ACL3: user::rwx
# # ACL3: user:dteam004:r--         #effective:r--
# # ACL3: user:dteam005:r-x         #effective:r-x
# # ACL3: user:dteam006:rw-         #effective:rw-
# # ACL3: group::r--                #effective:r--
# # ACL3: group:cms:---              #effective:---
# # ACL3: mask::rwx
# # ACL3: other::r--
# # ACL4: # file: $file
# # ACL4: # owner: $tstNAME
# # ACL4: # group: $tstGRP
# # ACL4: user::rwx
# # ACL4: user:dteam005:r-x         #effective:r-x
# # ACL4: user:dteam006:---         #effective:---
# # ACL4: group::r--                #effective:r--
# # ACL4: group:cms:---              #effective:---
# # ACL4: mask::rwx
# # ACL4: other::r--

my @aclOrig = (join "",@aclRes);
my ($a, $b);
    if ( 0 ) {
foreach my $c ( 1..10 ) {
    $a = shift @aclOrig;
    $b = shift @comp;
    print "A: >$a<\n";
    print "B: >$b<\n";
}
}

    $aclOrig[0] =~ s;\s+; ;g;
    $comp[0] =~ s;\s+; ;g;
#    print " lengths: ", scalar(@aclRes), " ", scalar(@comp), " \n";

if ( "@aclOrig" ne "@comp" ) { 
    my @aL = split / /, $aclOrig[0];
    my @cL = split / /, $comp[0];
    my $n = 0;
    foreach my $a ( @aL ) {
	my $c = shift @cL;
	$n++;
	if ( $a ne $c ) {
	    print " DIFF: $a $c $n\n";
	}
    }
}

if ( "@aclOrig" ne "@comp" ) { 
    print " ACLRES: @aclOrig \n";
    print "   COMP: @comp \n";
    print " compareACL [FAILED]\n"; 
##############################    return "[FAILED]"; 
}

    return "[OK]";
}

####################################################################################################
sub XtractGoodTurl {
    my $TURL = shift;
    if (0) {
    my $sTURL;
    	if ( $TURL =~ /rfio/ ) {  
	    $TURL =~ m[(.*?)://.*?/(.*)];
	    $TURL =~ m[(.*?)://(.*)];
	    $sTURL = $2;
	    $sTURL =~ s;//;:/; ;

	    if ( $ENV{NEW_RFIO_SYNTAX} ) {
		$sTURL = $TURL;  ###  For the NEW RFIOD 30/11/05
	    }

	} else {
	    $TURL =~ m[(.*?://.*?)(/.*?:)(/.*)];
	    $sTURL = $1 . $3;

	    $sTURL = $TURL;  ###  For the NEW FTPD 23/05/05

	}
    print "\n XtractGoodTurl\n INPUT: $TURL\n OUTPUT: $sTURL\n";
    return $sTURL;
} else {  ##  GG  06/02/07
    return $TURL;
}
}

####################################################################################################
sub TestPutOW {
my $overWrite = shift;

my $status;
my $lTURL;
$operation = "srm2_testPut";
$xCount++;
my $lifet = 123456;
my $nbfiles = 1;
$command = "$operation $u_token $nbfiles $overWrite";
#foreach my $kfile ( $Lfile[0] ) {
foreach my $kfile ( $file ) {
    my $fsize = $struct->{$kfile}->{size}*1024;
    if ( $overWrite == 2 ) { $fsize *= 100; }
##	$command .= " $kfile $lifet 0 $fsize";
	$command .= " $lifet - - - 0 - - $kfile $fsize";
}
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "LOW$overWrite: $_";
    my @m = split;
    if ( /r_token / ) { $r_token = $m[3]; }
    if ( /state\[(\d+?)\]/ ) { 
	$lTURL = $_; 
	chop($m[2]);
	$tstatus = $m[2]; 
##WAS	$TURL = $m[5];
	$TURL = $m[6]; 
	$struct->{$file}->{TURL} = $TURL;
	$struct->{$file}->{r_token} = $r_token;
    }
    if ( $overWrite ) {
	if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    } else {
	## This operation must fail when overWrite = 0 ...
	if ( /request state/ ) { 
	    $tstatus = "24";  ##  Cheating ...
	    $status = ( "11" eq $m[2] ) ? "[OK]" : "[FAILED]" ;
	}
    }
}
if ( $status ne "[OK]" || $tstatus ne "24" ) { 
    print " Put FAILED: $lTURL >$status< >$tstatus<\n";
    ##exit 1; 
}
else { print " Operation: $operation = $status Duration: $tElapse \n"; }

    return;
}

####################################################################################################
sub TransferTURL {

my $fileTURL  = shift;
my $fileInput = shift;
my $lLABL     = shift;

my $lTURL = XtractGoodTurl($fileTURL);
my $opsargs;
if ( $lTURL !~ /gsiftp/ ) {
    $operation = 'rfcp';
    $opsargs = "$fileInput $lTURL";
    $lLABL = 'RFCP_TRACE_' . "$lLABL";
} else {
    $operation = 'globus-url-copy';
    $opsargs = "-dbg file:$fileInput $lTURL";
    $lLABL = 'GSIFTP_TRACE_' . "$lLABL";
}

    XcuteComm("$operation",
	      "$opsargs",
	      "$lLABL", 0, 1);

if ( $lTURL !~ /gsiftp/ ) {
    $status = ( XtractVal('seconds through') eq 'local' ) ? '[OK]' : '[FAILED]';
} else {
    $status = ( XtractVal('226 Transfer complete.') ) ? '[OK]' : '[FAILED]';
}

if ( $status ne '[OK]' ) { &printAndCExit(); }

    return;
}

####################################################################################################
sub putDone {

my $pt_token = shift;
my $pt_file = shift;
my $lLABL     = shift;
my $xpectStat = shift || '0';

$operation = "srm2_testPutDone";
undef $status;
my $pt_surl;
@res = ();
$xCount++;
$command = "$operation $pt_token $pt_file";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  chomp($dateST  = `date '+%y%m%d-%H%M%S'`);
  $dateSTZ = `date --utc '+%FT%TZ'`;
  print "++++ $$ $dateST $dateSTZ\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "PD_${lLABL}: $_";
    my @m = split;
##    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "$xpectStat" eq "$m[2]" ) ? "[OK]" : "[FAILED]" ; }
}
&printAndCExit();

if ( "$xpectStat" ne '0' ) { return; }
$pt_surl = XtractVal('SURL =');
my @m = split /\s+/, $pt_file;
my $pt_filel = $m[-1];

if ( $pt_surl ne $pt_filel ) { 
    print " Mismatch3: $pt_surl $pt_filel \n";
    exit 1;
}

	XcuteComm("srm2_testGetRequestStatus",
		  "$endPoint $pt_token",
		  "GRS_${lLABL}", "0", 1);

    return;

}

####################################################################################################
sub queryDB {

    my $label = shift;
    my $opera = shift;
    my $cargs = shift;
    my ($tok, $pT, $lT, $fn);
    if ( $opera =~ 'Space' ) {
	($tok, $pT, $fn) = split /\s+/, $cargs;
	$lT = 'NA';
    } else {
	($tok, $pT, $lT, $fn) = split /\s+/, $cargs;
    }

    my ($ltime, $sfn);
    my @m = split '/', $fn;
    $fn = $m[-1];

    my $shandle1 = $db1->prepare("select ltime,sfn from Cns_file_replica where sfn like ?");

    $shandle1->execute("%${fn}%");
    ($ltime, $sfn) = $shandle1->fetchrow_array;
    $shandle1->finish;

    #my $Atime = scalar localtime $ltime;
    my $Dtime = $ltime - time();
    my $Atime = strftime '%FT%TZ', localtime($ltime);
    @m = split /-/, $Atime;
    if ( $m[0] eq '2038' ) { $Dtime = 'INFINITE'; }
    if ( $Atime eq '1970-01-01T01:00:00Z' )  { $Dtime = '0'; }
    ##GOODprint " Values: $fn: datestz: $Atime deltime: $Dtime sfn: $sfn \n";
    print " String: $fn: datestz: $Atime deltime: $Dtime \n";

    my $rpT = XtractVal('pinLifetime:');
    $rpT = ( $rpT eq "" ) ? '-' : $rpT;
    my $rlT = XtractVal('fileLifetime:');
    $rlT = ( $rlT eq "" ) ? '-' : $rlT;

#   print " LIFET: $label fn: $fn requested: $pT $lT returned: $rpT $rlT filelifetimeDB: $Dtime \n";
#   printf (" LIFET: %6s fn: %32s requested: %6s %6s returned: %6s %7s filelifetimeDB: %8s \n", $label, $fn, $pT, $lT, $rpT, $rlT, $Dtime);
    printf (" LIFET: %5s reqd: %6s %6s retd: %6s %7s lfTDB: %8s \n", $label, $pT, $lT, $rpT, $rlT, $Dtime);

    return;
}
####################################################################################################
