#!/usr/bin/perl -w

# myproxy test script
# written by Jim Basney <jbasney@ncsa.uiuc.edu>

# Assumes myproxy-server running as root on the local machine if
# MYPROXY_SERVER not set.
# Requires a valid proxy credential with lifetime of atleast 3 hours.
# Assumes myproxy-server.config has:
#   1. accepted_credentials, authorized_retrievers, and
#      authorized_renewers matching the proxy credential
#   2. default_renewers "none"
#
# Test cases are:
#   1. Store a credential (myproxy-init).
#   2. Get info on the stored credential (myproxy-info).
#   3. Retrieve stored credential (myproxy-logon).
#   4. Verify myproxy-logon fails on bad passphrase.
#   5. Change passphrase (myproxy-change-pass-phrase).
#   6. Retrieve with new passphrase.
#   7. Verify old passphrase no longer accepted.
#   8. Verify renewal fails by default.
#   9. Remove credential from repository (myproxy-destroy).
#  10. Verify credential is removed (myproxy_info).
#  11. Store credentials with retrieval policies.
#  12. Verify retrieval policies (one accept, one deny).
#  13. Store renewable credentials.
#  14. Get info on stored renewable credentials (myproxy-info -d).
#  15. Verify renewal policies (one accept, one deny).
#  16. Verify correct lifetime of retrieved credentials.
#
#  17. Store credential (myproxy-store -v -t 1)
#  18. Get info on the stored credential (myproxy-info)
#  19. Create proxy from stored credential (myproxy-logon).
#  20. Retrieve stored credential (myproxy-retrieve)
#  21. Verify passphrase checking (myproxy-logon).
#  22. Verify passphrase checking (myproxy-retrieve).
#  23. Verify renewal fails by default (myproxy-logon).
#  24. Verify renewal fails by default (myproxy-retrieve).
#  25. Remove credential from repository (myproxy-destroy).
#  26. Verify credential is removed (myproxy_info).
#  27. Store credentials with retrieval policies (myproxy-store).
#  28. Verify retrieval policies (one accept, one deny) (myproxy-logon).
#  29. Verify retrieval policies (one accept, one deny) (myproxy-retrieve).
#  30. Store renewable credentials.
#  31. Get info on stored renewable credentials (myproxy-info -d).
#  32. Verify renewal policies.
#  33. Verify correct lifetime of retrieved credentials.
#  34. Store credentials with retrieve key policies.
#  35. Get info for stored retrieve key credentials.
#  36. Verify myproxy-retrieve key retrieval policies.
#
# CoG test cases are:
#   1. CoG anonget of myproxy-init stored credential.
#   2. CoG get of myproxy-init stored credential.
#   3. (pending) Store a credential (CoG myproxy put).

use IPC::Open3;

#
# handle cmdline options
#
$usage = "usage: myproxy-test [-help] [-verbose] [-startserver]\n" .
         "                    [-performance iterations clients]\n" .
         "                    [-dbperformance] [-valgrind]\n";
$startserver = 0;
$performance = 0;
$dbperformance = 0;
$perfiters = 0;
$perfclients = 0;
$verbose = 0;
while (($arg = shift @ARGV)) {
    if ($arg eq "-h" || $arg eq "-help") {
	print STDERR $usage;
	exit 1;
    } elsif ($arg eq "-v" || $arg eq "-verbose") {
	$verbose = 1;
    } elsif ($arg eq "-startserver") {
	$startserver = 1;
    } elsif ($arg eq "-performance") {
	$performance = 1;
	$perfiters = shift @ARGV;
	$perfclients = shift @ARGV;
    } elsif ($arg eq "-dbperformance") {
	$dbperformance = 1;
    } elsif ($arg eq "-valgrind") {
	chomp($valgrind = `which valgrind 2>/dev/null`);
	die "valgrind not in PATH, stopped" if (!(-x $valgrind));
	$valgrind .= " --tool=addrcheck -q";
	$valgrind .= " --leak-check=full";
	$valgrind .= " --suppressions=$ENV{HOME}/.valgrind.supp";
	$valgrind .= " --log-file=/tmp/valgrind.log.$$";
	$valgrind .= " --num-callers=50 ";
    } else {
	print STDERR $usage;
	exit 1;
    }
}

#
# make sure I have a valid proxy
#
chomp($grid_proxy_init = `which grid-proxy-init 2>/dev/null`);
die "grid-proxy-init not found, stopped" if (!(-x $grid_proxy_init));
chomp($grid_proxy_info = `which grid-proxy-info 2>/dev/null`);
die "grid-proxy-info not found, stopped" if (!(-x $grid_proxy_info));
$timeleft = `$grid_proxy_info -timeleft 2>/dev/null`;
if (!defined($timeleft) || $timeleft eq "" || ($timeleft < 60*60*3)) {
    &debug("Problem with proxy.  Will try to create a new one.");
    `$grid_proxy_init -pwstdin </dev/null >/dev/null 2>&1`;
    $timeleft = `$grid_proxy_info -timeleft 2>/dev/null`;
}
die "grid-proxy-info failed, stopped"
    if (!defined($timeleft) || $timeleft eq "");
die "proxy expired, stopped" if ($timeleft < 60);
die "proxy lifetime too short, stopped" if ($timeleft < 60*60*3);
chomp($cert_subject = `$grid_proxy_info -subject`);
die "grid-proxy-info -subject failed, stopped"
    if (!defined($cert_subject) || $cert_subject eq "");
$cert_subject = (split(/\/CN=proxy|\/CN=limited proxy|\/CN=\d+/,
		       $cert_subject))[0];

#
# check for the commands I want to run
#
chomp($myproxy_store = `which myproxy-store 2>/dev/null`);
die "myproxy-store not in PATH, stopped" if (!(-x $myproxy_store));
chomp($myproxy_init = `which myproxy-init 2>/dev/null`);
die "myproxy-init not in PATH, stopped" if (!(-x $myproxy_init));
chomp($myproxy_retrieve = `which myproxy-retrieve 2>/dev/null`);
die "myproxy-retrieve not in PATH, stopped" if (!(-x $myproxy_retrieve));
chomp($myproxy_info = `which myproxy-info 2>/dev/null`);
die "myproxy-info not in PATH, stopped" if (!(-x $myproxy_info));
chomp($myproxy_destroy = `which myproxy-destroy 2>/dev/null`);
die "myproxy-destroy not in PATH, stopped" if (!(-x $myproxy_destroy));
chomp($myproxy_get = `which myproxy-logon 2>/dev/null`);
die "myproxy-logon not in PATH, stopped" if (!(-x $myproxy_get));
chomp($myproxy_passwd = `which myproxy-change-pass-phrase 2>/dev/null`);
die "myproxy-change-pass-phrase not in PATH, stopped"
    if (!(-x $myproxy_passwd));
chomp($bintrue = `which true 2>/dev/null`);

#
# setup environment variables
#
if (!defined($ENV{'MYPROXY_SERVER'})) {
    chomp($hostname = `hostname 2>/dev/null`);
    $ENV{'MYPROXY_SERVER'} = $hostname;
}
if (!defined($ENV{'X509_USER_PROXY'})) {
    $ENV{'X509_USER_PROXY'} = "/tmp/x509up_u$<";
}
# make proxy from existing proxy, so we don't need to deal with long-term cred
$ENV{'X509_USER_CERT'} = $ENV{'X509_USER_PROXY'};
$ENV{'X509_USER_KEY'} = $ENV{'X509_USER_PROXY'};
$ENV{'LOGNAME'} = $ENV{'LOGNAME'} . ".myproxy-test";

srand(time||$$);
$passphrase = sprintf "%010.d", int(rand(0x7fffffff));

# all temporary files accessible only by running user
umask(0077);

# only test trusted certificates management if no certificates directory
# exists for us to mess up
if (!defined($ENV{X509_CERT_DIR}) &&
    defined($ENV{HOME}) && !(-e "$ENV{HOME}/.globus/certificates")) {
    $test_trustroots = 1;
    print STDERR "Testing with $ENV{HOME}/.globus/certificates.\n"
	if ($verbose);
} else {
    $test_trustroots = 0;
    print STDERR "Not testing CA certificate management because $ENV{HOME}/.globus/certificates exists.\n" if ($verbose);
}

#
# start server if requested
#
if ($startserver) {
    $ENV{'MYPROXY_SERVER'} = "localhost";
    $ENV{'MYPROXY_SERVER_PORT'} = 49152 + ($< % 16384);
    $ENV{'MYPROXY_SERVER_DN'} = $cert_subject;
    chomp($myproxy_server = `which myproxy-server 2>/dev/null`);
    die "myproxy-server not in PATH, stopped" if (!(-x $myproxy_server));
    $serverdir = "/tmp/myproxy-test.serverdir.$$";
    mkdir($serverdir, 0700) ||
	die "failed to create $serverdir, stopped";
    $serverconf = "/tmp/myproxy-test.serverconf.$$";
    open(CONF, ">$serverconf") ||
	 die "failed to open $serverconf, stopped";
    print CONF "accepted_credentials  \"*\"\n";
    print CONF "authorized_retrievers \"*\"\n";
    print CONF "default_retrievers    \"*\"\n";
    print CONF "authorized_renewers   \"*\"\n";
    print CONF "default_renewers      \"none\"\n";
    print CONF "authorized_key_retrievers \"*\"\n";
    print CONF "default_key_retrievers    \"none\"\n";
    print CONF "passphrase_policy_program $bintrue\n" if (-x $bintrue);
    close(CONF);
    $SERVERPIDFILE = "/tmp/myproxy-test.serverpid.$$";
    $servercmd = "$myproxy_server -s $serverdir -c $serverconf";
    $servercmd .= " -p $ENV{MYPROXY_SERVER_PORT} -P $SERVERPIDFILE";
    $servercmd = $valgrind . $servercmd if (defined($valgrind));
    &debug("running '$servercmd'");
    `$servercmd`;
    sleep(1);			# give server a chance to startup
    sleep(4) if (defined($valgrind)); # valgrind slows things down
    if (open SERVERPIDFILE) {
	$serverpid = <SERVERPIDFILE>;
	close SERVERPIDFILE;
    }
    if (!defined($serverpid) || $serverpid eq "") {
	print STDERR "failed to start myproxy-server:\n";
	`$servercmd -d`;	# show output on terminal
	&docleanup();
	exit 1;
    }
}

#
# run performance tests if requested
#
if ($performance) {
    &doperftests();
    exit 0;
}

#
# BEGIN TESTS
#
$SUCCESSES = $FAILURES = 0;

# commands to test: myproxy-init, myproxy-info, myproxy-destroy,
#                   myproxy-logon, and myproxy-change-pass-phrase

($exitstatus, $output) =
    &runtest("myproxy-init -v -a -c 1 -t 1 -S",
	     $passphrase . "\n");
print "MyProxy Test 1 (store credential with default name): ";
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
    print "Skipping remaining tests.\n";
    goto end_of_all_tests;
}

($exitstatus, $output) = &runtest("myproxy-info -v", undef);
print "MyProxy Test 2 (get info for stored credential): ";
if ($exitstatus == 0 && $output =~ /username/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 3 (retrieve stored credential): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o - -v -S > /tmp/myproxy-test.$$",
	     $passphrase . "\n");
print "MyProxy Test 3- (retrieve stored credential to stdout): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -N -v -S",
	     $passphrase . "\n");
print "MyProxy Test 3N (authenticate w/o retrieving credential): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

if ($test_trustroots) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -T -t 1 -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    print "MyProxy Test 3T (retrieve stored credential w/ trustroots): ";
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifyproxy("/tmp/myproxy-test.$$");
    }
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifytrustroots();
    }
    if ($exitstatus == 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++; print STDERR $output;
    }
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     "badpassphrase\n");
print "MyProxy Test 4 (verify passphrase checking on retrieve): ";
if ($exitstatus != 0 && $output =~ /invalid pass phrase/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -N -v -S",
	     "badpassphrase\n");
print "MyProxy Test 4N (verify passphrase checking on authenticate): ";
if ($exitstatus != 0 && $output =~ /invalid pass phrase/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

$old_passphrase = $passphrase;
$passphrase = sprintf "%010.d", int(rand(0x7fffffff));
($exitstatus, $output) =
    &runtest("myproxy-change-pass-phrase -v -S",
	     "$old_passphrase\n$passphrase\n");
print "MyProxy Test 5 (change passphrase for credential): ";
if ($exitstatus == 0 && $output =~ /Pass phrase changed/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -N -v -S",
	     $passphrase . "\n");
print "MyProxy Test 6 (verify new passphrase): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     $old_passphrase . "\n");
print "MyProxy Test 7 (verify old passphrase fails): ";
if ($exitstatus != 0 && $output =~ /invalid pass phrase/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -a \$X509_USER_PROXY -t 1 -o /tmp/myproxy-test.$$ -v", undef);
print "MyProxy Test 8 (verify default renewal policy): ";
if ($exitstatus != 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-destroy -v", undef);
print "MyProxy Test 9 (remove credential from repository): ";
if ($exitstatus == 0 && $output =~ /was successfully removed/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-info -v", undef);
print "MyProxy Test 10 (verify credentials are removed): ";
if (!($output =~ /default credential/)) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-init -v -r 'nobody' -k 'nobody' -c 1 -t 1 -S",
	     $passphrase . "\n");
print "MyProxy Test 11 (store credentials with retrieval policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-init -v -x -r '$cert_subject' -k 'mine' -c 1 -t 1 -S",
		 $passphrase . "\n");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -k 'mine' -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 12 (verify retrieval policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -k 'nobody' -t 1 -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
&runtest("myproxy-destroy -v -k 'mine'", undef);
&runtest("myproxy-destroy -v -k 'nobody'", undef);

($exitstatus, $output) =
    &runtest("myproxy-init -v -R 'nobody' -k 'nobody' -c 1 -t 1 -d -S",
	     $passphrase . "\n");
print "MyProxy Test 13 (store credentials with renewal policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-init -v -x -R '$cert_subject' -k 'mine' -c 1 -t 1 -d -S",
		 $passphrase . "\n");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) = &runtest("myproxy-info -v -d", undef);
print "MyProxy Test 14 (get info for stored renewal credentials): ";
if ($exitstatus == 0 && $output =~ /username/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

($exitstatus, $output) =
    &runtest("myproxy-logon -k 'mine' -a $ENV{'X509_USER_PROXY'} -t 1 -o /tmp/myproxy-test.$$ -v -d",
	     undef);
print "MyProxy Test 15 (verify renewal policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -k 'nobody' -a $ENV{'X509_USER_PROXY'} -t 1 -o /tmp/myproxy-test.$$ -v -d",
		 undef);
    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
&runtest("myproxy-destroy -v -k 'mine' -d", undef);
&runtest("myproxy-destroy -v -k 'nobody' -d", undef);

($exitstatus, $output) =
    &runtest("myproxy-init -v -a -c 3 -t 2 -S",
	     $passphrase . "\n");
print "MyProxy Test 16 (verify lifetime of retrieved credentials): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    $timeleft = `$grid_proxy_info -file /tmp/myproxy-test.$$ -timeleft`;
    if (defined($timeleft) &&
	$timeleft > 60*60*2+300 || $timeleft < 60*60*2-300) {
	$output = "Credential lifetime ($timeleft) incorrect." .
	    "It should be " . (60*60*2) . ".\n";
	$exitstatus = 1;
    }
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    $timeleft = `$grid_proxy_info -file /tmp/myproxy-test.$$ -timeleft`;
    if ($timeleft > 60*61 || $timeleft < 60*59) {
	$output = "Credential lifetime ($timeleft) incorrect." .
	    "It should be " . (60*60) . ".\n";
	$exitstatus = 1;
    }
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
&runtest("myproxy-destroy -v", undef);

#
# Start of myproxy-store and myproxy-retrieve tests
#

# commands to test: myproxy-store, myproxy-info, myproxy-destroy,
#                   myproxy-logon, myproxy-retrieve, and 
#                   myproxy-change-pass-phrase

# For myproxy-store, we need an encrypted key to store.
# So, let's encrypt our proxy key.
$passphrase = sprintf "%010.d", int(rand(0x7fffffff));
$testkey = "/tmp/myproxy-test.$$.key";
&runtest("openssl rsa -des3 -passout stdin -in \$X509_USER_KEY -out $testkey",
	 $passphrase . "\n");
chmod(0600, $testkey);

#
# Test 17
#
($exitstatus, $output) =
    &runtest("myproxy-store -x -E '$cert_subject' -v -t 1 -y $testkey", undef);
print "MyProxy Test 17 (store credential with default name): ";
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} elsif (($output =~ /Error checking authorization/) ||
         ($output =~ /unknown command/)) {
    print "UNSUPPORTED\n"; $FAILURES++;
    print "Server does not support myproxy-store. Skipping futher myproxy-store tests.\n";
    goto end_of_store_tests;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
    print "Skipping futher myproxy-store tests.\n";
    goto end_of_store_tests;
}

#
# Test 18
#
($exitstatus, $output) = &runtest("myproxy-info -v", undef);
print "MyProxy Test 18 (get info for stored credential): ";
if ($exitstatus == 0 && $output =~ /username/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 19
#
($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 19 (create proxy from stored credential): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 20
#

($exitstatus, $output) =
    &runtest("myproxy-retrieve -c /tmp/myproxy-test.cert.$$ -y /tmp/myproxy-test.key.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 20 (retrieve stored credential): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifycert("/tmp/myproxy-test.cert.$$", "/tmp/myproxy-test.key.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
unlink( "/tmp/myproxy-test.cert.$$" );
unlink( "/tmp/myproxy-test.key.$$" );

if ($test_trustroots) {
    ($exitstatus, $output) =
	&runtest("myproxy-retrieve -T -c /tmp/myproxy-test.cert.$$ -y /tmp/myproxy-test.key.$$ -v -S",
		 $passphrase . "\n");
    print "MyProxy Test 20T (retrieve stored credential w/ trustroots): ";
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifycert("/tmp/myproxy-test.cert.$$", "/tmp/myproxy-test.key.$$");
    }
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifytrustroots();
    }
    if ($exitstatus == 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++; print STDERR $output;
    }
    unlink( "/tmp/myproxy-test.cert.$$" );
    unlink( "/tmp/myproxy-test.key.$$" );
}

#
# Test 21
#
($exitstatus, $output) =
    &runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     "badpassphrase\n");
print "MyProxy Test 21 (verify passphrase checking on myproxy-logon): ";
if ($exitstatus != 0 && $output =~ /invalid pass phrase/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 22
#
($exitstatus, $output) =
    &runtest("myproxy-retrieve -c /tmp/myproxy-test-cert.$$ -y /tmp/myproxy-test-key.$$ -v -S",
	     "badpassphrase\n");
print "MyProxy Test 22 (verify passphrase checking on myproxy-retrieve): ";
if ($exitstatus != 0 && $output =~ /invalid pass phrase/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
unlink( "/tmp/myproxy-test.cert.$$" );
unlink( "/tmp/myproxy-test.key.$$" );

#
# Test 23
#
($exitstatus, $output) =
    &runtest("myproxy-logon -a \$X509_USER_PROXY -t 1 -o /tmp/myproxy-test.$$ -v", undef);
print "MyProxy Test 23 (verify myproxy-logon default renewal policy): ";
if ($exitstatus != 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 24
#
($exitstatus, $output) =
    &runtest("myproxy-retrieve -a \$X509_USER_PROXY  -c /tmp/myproxy-test-cert.$$ -y /tmp/myproxy-test-key.$$ -v", undef);
print "MyProxy Test 24 (verify myproxy-retrieve default renewal policy): ";
if ($exitstatus != 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
unlink( "/tmp/myproxy-test-cert.$$" );
unlink( "/tmp/myproxy-test-key.$$" );

#
# Test 25
#
($exitstatus, $output) =
    &runtest("myproxy-destroy -v", undef);
print "MyProxy Test 25 (remove credential from repository): ";
if ($exitstatus == 0 && $output =~ /was successfully removed/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 26
#
($exitstatus, $output) =
    &runtest("myproxy-info -v", undef);
print "MyProxy Test 26 (verify credentials are removed): ";
if (!($output =~ /default credential/)) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 27
#
($exitstatus, $output) =
    &runtest("myproxy-store -v -r 'nobody' -k 'nobody' -t 1 -y $testkey",
	     undef);
print "MyProxy Test 27 (store credentials with retrieval policies): ";

if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-store -x -E '$cert_subject' -v -r '$cert_subject' -k 'mine' -t 1 -y $testkey", undef);
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 28
#
($exitstatus, $output) =
    &runtest("myproxy-logon -k 'mine' -t 1 -o /tmp/myproxy-test.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 28 (verify myproxy-logon retrieval policies): ";

if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -k 'nobody' -t 1 -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");

    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 29
#
($exitstatus, $output) =
    &runtest("myproxy-retrieve -k 'mine' -c /tmp/myproxy-test.cert.12.1.$$ -y /tmp/myproxy-test.key.12.1.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 29 (verify myproxy-retrieve retrieval policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifycert("/tmp/myproxy-test.cert.12.1.$$", "/tmp/myproxy-test.key.12.1.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-retrieve -k 'nobody' -c /tmp/myproxy-test.cert.12.2.$$ -y /tmp/myproxy-test.key.12.2.$$ -v -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
&runtest("myproxy-destroy -v -k 'mine'", undef);
&runtest("myproxy-destroy -v -k 'nobody'", undef);
`rm -f /tmp/myproxy-test.*.12.*.$$`;

#
# Test 30
#
($exitstatus, $output) =
    &runtest("myproxy-store -v -R 'nobody' -k 'nobody' -t 1 -d -y \$X509_USER_PROXY -c \$X509_USER_PROXY",
	     undef);
print "MyProxy Test 30 (store credentials with renewal policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-store -v -x -R '$cert_subject' -k 'mine' -t 1 -d -y \$X509_USER_PROXY -c \$X509_USER_PROXY", undef);
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 31
#
($exitstatus, $output) = &runtest("myproxy-info -v -d", undef);
print "MyProxy Test 31 (get info for stored renewal credentials): ";
if ($exitstatus == 0 && $output =~ /username/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 32
#
($exitstatus, $output) =
    &runtest("myproxy-logon -k 'mine' -a $ENV{'X509_USER_PROXY'} -t 1 -o /tmp/myproxy-test.$$ -v -d -S",
	     $passphrase . "\n");
print "MyProxy Test 32 (verify renewal policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -k 'nobody' -a $ENV{'X509_USER_PROXY'} -t 1 -o /tmp/myproxy-test.$$ -v -d -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

&runtest("myproxy-destroy -v -k 'mine' -d", undef);
&runtest("myproxy-destroy -v -k 'nobody' -d", undef);

#
# Test 33
#
($exitstatus, $output) =
    &runtest("myproxy-store -x -E '$cert_subject' -v -a -t 2 -y $testkey", undef);
print "MyProxy Test 33 (verify lifetime of retrieved credentials): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    $timeleft = `$grid_proxy_info -file /tmp/myproxy-test.$$ -timeleft`;
    if (defined($timeleft) &&
	$timeleft > 60*60*2+300 || $timeleft < 60*60*2-300) {
	$output = "Credential lifetime ($timeleft) incorrect." .
	    "It should be " . (60*60*2) . ".\n";
	$exitstatus = 1;
    }
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
		 $passphrase . "\n");
    $timeleft = `$grid_proxy_info -file /tmp/myproxy-test.$$ -timeleft`;
    if ($timeleft > 60*61 || $timeleft < 60*59) {
	$output = "Credential lifetime ($timeleft) incorrect." .
	    "It should be " . (60*60) . ".\n";
	$exitstatus = 1;
    }
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifyproxy("/tmp/myproxy-test.$$");
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
&runtest("myproxy-destroy -v", undef);

#
# Test 34
#
($exitstatus, $output) =
    &runtest("myproxy-store -v -E 'nobody' -k 'nobody' -t 1 -y $testkey",
	     undef);
print "MyProxy Test 34 (store credentials with retrieve key policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-store -v -x -E '$cert_subject' -k 'mine' -t 1 -y $testkey", undef);
}
if ($exitstatus == 0) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 35
#
($exitstatus, $output) = &runtest("myproxy-info -v", undef);
print "MyProxy Test 35 (get info for stored retrieve key credentials): ";
if ($exitstatus == 0 && $output =~ /username/) {
    print "SUCCEEDED\n"; $SUCCESSES++;
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}

#
# Test 36
#
($exitstatus, $output) =
    &runtest("myproxy-retrieve -k 'mine' -c /tmp/myproxy-test.cert.12.1.$$ -y /tmp/myproxy-test.key.12.1.$$ -v -S",
	     $passphrase . "\n");
print "MyProxy Test 36 (verify myproxy-retrieve key retrieval policies): ";
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&verifycert("/tmp/myproxy-test.cert.12.1.$$", "/tmp/myproxy-test.key.12.1.$$");
}
if ($exitstatus == 0) {
    ($exitstatus, $output) =
	&runtest("myproxy-retrieve -k 'nobody' -c /tmp/myproxy-test.cert.12.2.$$ -y /tmp/myproxy-test.key.12.2.$$ -v -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++;
	print STDERR "Shouldn't have allowed retrieval.\n";
	print STDERR $output;
    }
} else {
    print "FAILED\n"; $FAILURES++; print STDERR $output;
}
end_of_store_tests:
&runtest("myproxy-destroy -v -k 'mine'", undef);
&runtest("myproxy-destroy -v -k 'nobody'", undef);
`rm -f /tmp/myproxy-test.*.12.*.$$`;
unlink("/tmp/myproxy-test.$$.key");

#
# COG tests
#
if (defined($ENV{"COG_INSTALL_PATH"})) {
    &docogtests();
} else {
    print "\$COG_INSTALL_PATH undefined. Skipping Java CoG tests.\n";
}

#
# END TESTS
#
end_of_all_tests:
&docleanup();

print "MyProxy Tests Complete: ", $SUCCESSES, " tests passed, ";
print $FAILURES, " tests failed\n";

if (defined($valgrind)) {
    `cat /tmp/valgrind.log.$$* > valgrind.log.$$`;
    `rm -f /tmp/valgrind.log.$$*`;
    if (-s "valgrind.log.$$") {
	print "Valgrind errors in valgrind.log.$$.\n";
	$FAILURES++;
    } else {
	print "Valgrind found no errors.\n";
	unlink("valgrind.log.$$");
    }
}

exit $FAILURES;

#
# SUBROUTINES
#

sub runtest {
    local($command, $input) = @_;

    $command = $valgrind . $command if (defined($valgrind));
    print STDERR "running: ", $command, "\n" if ($verbose);
    $pid = open3(*Writer, *Reader, '', "exec $command") ||
	die "failed to run $command";
    print Writer $input if (defined($input));
    close(Writer);
    @output = <Reader>;
    close(Reader);
    waitpid($pid, 0);
    $exitstatus = $?;
    $output = join('', @output);

    return ($exitstatus, $output);
}

sub docogtests {

    $cogmyproxy = "$ENV{COG_INSTALL_PATH}/bin/myproxy -h $ENV{MYPROXY_SERVER}";

    if (defined($ENV{"MYPROXY_SERVER_DN"})) {
	$cogmyproxy .= " -s \"$ENV{MYPROXY_SERVER_DN}\"";
    }

    if (defined($ENV{"MYPROXY_SERVER_PORT"})) {
	$cogmyproxy .= " -p $ENV{MYPROXY_SERVER_PORT}";
    }

    $cogmyproxy .= " -l $ENV{'LOGNAME'}";

    $ENV{"COG_OPTS"} = " -DX509_USER_PROXY=$ENV{X509_USER_PROXY}"
                     . " -DX509_USER_CERT=$ENV{X509_USER_CERT}"
                     . " -DX509_USER_KEY=$ENV{X509_USER_KEY}";

    ($exitstatus, $output) =
	&runtest("myproxy-init -v -a -c 1 -t 1 -S",
		 $passphrase . "\n");
    print "MyProxy CoG Test 1 (anonget of myproxy-init credential): ";
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &runtest("$cogmyproxy anonget -o /tmp/myproxy-test.$$",
		     $passphrase . "\n");
    }
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifyproxy("/tmp/myproxy-test.$$");
    }
    if ($exitstatus == 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++; print STDERR $output;
    }

    ($exitstatus, $output) =
	&runtest("myproxy-init -v -a -c 1 -t 1 -S",
		 $passphrase . "\n");
    print "MyProxy CoG Test 2 (get of myproxy-init credential): ";
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &runtest("$cogmyproxy get -o /tmp/myproxy-test.$$",
		     $passphrase . "\n");
    }
    if ($exitstatus == 0) {
	($exitstatus, $output) =
	    &verifyproxy("/tmp/myproxy-test.$$");
    }
    if ($exitstatus == 0) {
	print "SUCCEEDED\n"; $SUCCESSES++;
    } else {
	print "FAILED\n"; $FAILURES++; print STDERR $output;
    }

#     This test is disabled for now, pending new Java CoG release
#     with certificate chain support:
#       (http://bugzilla.globus.org/globus/show_bug.cgi?id=1914)
#
#     ($exitstatus, $output) =
# 	&runtest("$cogmyproxy put -a -c 1 -t 1",
# 		 $passphrase . "\n");
#     print "MyProxy CoG Test 3 (store credential with default name): ";
#     if ($exitstatus == 0) {
# 	print "SUCCEEDED\n"; $SUCCESSES++;
#     } else {
# 	print "FAILED\n"; $FAILURES++; print STDERR $output;
#     }
}

#
# verify_proxy
#
# Check to see if user has a valid proxy, and verify proxy is usable
#
# Dependencies: (-x grid-proxy-info)
#
# grid-proxy-info -timeleft
#     die if no output, or output is less than 60 seconds
# $proxy = grid-proxy-info -path
# grid-proxy-init -debug -verify -cert $proxy -key $proxy
#     if $? == 0 then proxy is valid
#     if $? != 0, die, b/c proxy is invalid and won't work
#
sub verifyproxy {
    local($proxyfile) = @_;

    chomp (my $timeleft = `$grid_proxy_info -file $proxyfile -timeleft`);
    if (!defined($timeleft) || $timeleft eq "") {
	$output = "failed to verify proxy\n";
	$output .= "'grid-proxy-info -timeleft' failed\n";
	return (1, $output);
    }
    if ($timeleft < 1) {
	$output = "proxy is expired\n";
	return (1, $output);
    }
    chomp (my $proxytype = `$grid_proxy_info -file $proxyfile -type`);
    local($oldproxy) = "";
    if ($proxytype =~ /legacy/) {
	$oldproxy = "-old";
    }
    local($output) = '$grid_proxy_init $oldproxy -debug -verify -cert $proxyfile -key $proxyfile -valid 0:1 -out /tmp/tmpproxy.$$';
    if ($? != 0) {
	$output = "failed to verify proxy\n" . $output;
	unlink("/tmp/tmpproxy.$$");
	return (1, $output);
    }

    # remove the new proxy we created for validation
    # NOTE: this does not affect the user's original proxy in any way
    unlink("/tmp/tmpproxy.$$");

    return (0, "");
}

#
# verifycert
#
# Check to see if user has a valid usable certificate
#
# Dependencies: (-x grid-proxy-int)
#
# grid-proxy-init -debug -verify -cert $certfile -key $keyfile
#     if $? == 0 then proxy is valid
#     if $? != 0, die, b/c proxy is invalid and won't work
#
sub verifycert {
    local($certfile, $keyfile) = @_;

    local($output) = '$grid_proxy_init $oldproxy -debug -verify -cert $certfile -key $keyfile -valid 0:1 -out /tmp/tmpproxy.$$';
    if ($? != 0) {
	$output = "failed to verify certificate from: $certfile and $keyfile\n" . $output;
	unlink("/tmp/tmpproxy.$$");
	return (1, $output);
    }

    # remove the new proxy we created for validation
    # NOTE: this does not affect the user's original proxy in any way
    unlink("/tmp/tmpproxy.$$");

    return (0, "");
}

#
# verifytrustroots
#
# Check to see that trustroots were properly installed in
# ~/.globus/certificates, then remove that directory.
#
sub verifytrustroots {
    return (1, "internal error: not supposed to test trustroots!")
	if (!$test_trustroots);
    $trustrootdir = "$ENV{HOME}/.globus/certificates";
    if (!(-d $trustrootdir)) {
	return (1, "trust root directory $trustrootdir doesn't exist!");
    }
    $diritems = 0;
    opendir(DIRHANDLE, $trustrootdir) or
	die "couldn't open $trustrootdir : $!";
    while (defined($filename = readdir(DIRHANDLE))) {
	next if $filename =~ /^\.\.?$/;     # skip . and ..
	$diritems++;
    }
    closedir(DIRHANDLE);
    if ($diritems == 0) {
	rmdir($trustrootdir);
	return (1, "trust root directory $trustrootdir is empty!");
    }
    `rm -rf $trustrootdir`;
    return (0, "");
}


sub debug {
    print STDERR join('', @_), "\n" if ($verbose);
}

sub docleanup {
    unlink("/tmp/myproxy-test.$$");
    kill('TERM', $serverpid) if (defined($serverpid));
    unlink($SERVERPIDFILE) if (defined($SERVERPIDFILE));
    unlink($serverconf) if (defined($serverconf));
    `rm -rf $serverdir` if (defined($serverdir));
}

sub doperftests {
    &dogpiperftest();
    &doinitperftest();
    &dogetdelegperftest();
    &doinfoperftest();
    &docpperftest();
    &dodbperftest() if ($dbperformance);
    &docleanup();
}

sub dogpiperftest {
    $starttime = time();
    $i = $perfclients;
    while ($i--) {
	$pid = fork();
	last if ($pid == 0);
    }
    if ($pid == 0) {
	$i = $perfiters;
	while ($i--) {
	    ($exitstatus, $output) =
		&runtest("grid-proxy-init -hours 1 -out /tmp/myproxy-test.$$", undef);
	    if ($exitstatus != 0) {
		print "$$: grid-proxy-init FAILED with $i iters to go.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	unlink("/tmp/myproxy-test.$$");
	&debug("$$ is done.");
	exit 0;
    }
    $i = $perfclients;
    while ($i--) {
	wait();
	if ($?) {
	    printf "child failed. aborting.\n";
	    &docleanup(); exit 1;
	}
    }
    $endtime = time();
    print $perfiters*$perfclients, " grid-proxy-inits in ";
    print $endtime-$starttime, " seconds.\n";
}

sub doinitperftest {
    $starttime = time();
    $i = $perfclients;
    while ($i--) {
	$pid = fork();
	last if ($pid == 0);
    }
    if ($pid == 0) {
	$i = $perfiters;
	while ($i--) {
	    ($exitstatus, $output) =
		&runtest("myproxy-init -v -a -c 1 -t 1 -S -k $$",
			 $passphrase . "\n");
	    if ($exitstatus != 0) {
		print "$$: myproxy-init FAILED with $i iters to go.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	&runtest("myproxy-destroy -v -k $$", undef);
	&debug("$$ is done.");
	exit 0;
    }
    $i = $perfclients;
    while ($i--) {
	wait();
	if ($?) {
	    printf "child failed. aborting.\n";
	    &docleanup(); exit 1;
	}
    }
    $endtime = time();
    print $perfiters*$perfclients, " myproxy-inits in ";
    print $endtime-$starttime, " seconds.\n";
}

sub dogetdelegperftest {
    ($exitstatus, $output) =
	&runtest("myproxy-init -v -a -c 1 -t 1 -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "myproxy-init failed\n"; print STDERR $output;
	exit($exitstatus>>8);
    }
    $starttime = time();
    $i = $perfclients;
    while ($i--) {
	$pid = fork();
	last if ($pid == 0);
    }
    if ($pid == 0) {
	$i = $perfiters;
	while ($i--) {
	    ($exitstatus, $output) =
		&runtest("myproxy-logon -t 1 -o /tmp/myproxy-test.$$ -v -S",
			 $passphrase . "\n");
	    if ($exitstatus != 0) {
		print "$$: myproxy-logon FAILED with $i iters to go.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	unlink("/tmp/myproxy-test.$$");
	&debug("$$ is done.");
	exit 0;
    }
    $i = $perfclients;
    while ($i--) {
	wait();
	if ($?) {
	    printf "child failed. aborting.\n";
	    &docleanup(); exit 1;
	}
    }
    $endtime = time();
    print $perfiters*$perfclients, " myproxy-logons in ";
    print $endtime-$starttime, " seconds.\n";
    &runtest("myproxy-destroy -v", undef);
}

sub doinfoperftest {
    ($exitstatus, $output) =
	&runtest("myproxy-init -v -a -c 1 -t 1 -S",
		 $passphrase . "\n");
    if ($exitstatus != 0) {
	print "myproxy-init failed\n"; print STDERR $output;
	exit($exitstatus>>8);
    }
    $starttime = time();
    $i = $perfclients;
    while ($i--) {
	$pid = fork();
	last if ($pid == 0);
    }
    if ($pid == 0) {
	$i = $perfiters;
	while ($i--) {
	    ($exitstatus, $output) = &runtest("myproxy-info -v", undef);
	    if ($exitstatus != 0) {
		print "$$: myproxy-info FAILED with $i iters to go.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	&debug("$$ is done.");
	exit 0;
    }
    $i = $perfclients;
    while ($i--) {
	wait();
	if ($?) {
	    printf "child failed. aborting.\n";
	    &docleanup(); exit 1;
	}
    }
    $endtime = time();
    print $perfiters*$perfclients, " myproxy-infos in ";
    print $endtime-$starttime, " seconds.\n";
    &runtest("myproxy-destroy -v", undef);
}

sub docpperftest {
    $starttime = time();
    $i = $perfclients;
    while ($i--) {
	$pid = fork();
	last if ($pid == 0);
    }
    if ($pid == 0) {
	($exitstatus, $output) =
	    &runtest("myproxy-init -v -a -c 1 -t 1 -S -k $$",
		     $passphrase . "\n");
	if ($exitstatus != 0) {
	    print "$$: myproxy-init FAILED with $i iters to go.\n";
	    print STDERR $output;
	    exit($exitstatus>>8);
	}
	$i = $perfiters;
	while ($i--) {
	    $old_passphrase = $passphrase;
	    $passphrase = sprintf "%010.d", int(rand(0x7fffffff));
	    ($exitstatus, $output) =
		&runtest("myproxy-change-pass-phrase -v -S -k $$",
			 "$old_passphrase\n$passphrase\n");
	    if ($exitstatus != 0) {
		print "$$: myproxy-change-pass-phrase FAILED with $i iters to go.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	&runtest("myproxy-destroy -v -k $$", undef);
	&debug("$$ is done.");
	exit 0;
    }
    $i = $perfclients;
    while ($i--) {
	wait();
	if ($?) {
	    printf "child failed. aborting.\n";
	    &docleanup(); exit 1;
	}
    }
    $endtime = time();
    print $perfiters*$perfclients, " myproxy-change-pass-phrases in ";
    print $endtime-$starttime, " seconds.\n";
}

sub dodbperftest {
    foreach $dbsize (10,100,1000,10000) {
	$i = $dbsize;
	while ($i--) {
	    ($exitstatus, $output) =
		&runtest("myproxy-admin-load-credential -v -d -l $i" .
			 " -c $ENV{X509_USER_CERT} -y $ENV{X509_USER_KEY}" .
			 (defined($serverdir) ? " -s $serverdir" : ""), undef);
	    if ($exitstatus != 0) {
		print "myproxy-admin-load-credential FAILED.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	$starttime = time();
	$i = $dbsize;
	while ($i--) {
	    ($exitstatus, $output) = &runtest("myproxy-info -v -l $i", undef);
	    if ($exitstatus != 0) {
		print "myproxy-info FAILED.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	$endtime = time();
	print $dbsize, " myproxy-infos in ", $endtime-$starttime, " seconds.\n";
	$starttime = $endtime;
	$i = $dbsize;
	while ($i--) {
	    ($exitstatus, $output) = &runtest("myproxy-destroy -v -l $i",
					      undef);
	    if ($exitstatus != 0) {
		print "myproxy-destroy FAILED.\n";
		print STDERR $output;
		exit($exitstatus>>8);
	    }
	}
	$endtime = time();
	print $dbsize, " myproxy-destroys in ";
	print $endtime-$starttime, " seconds.\n";
    }
}
