Start using the centralized pidfile and logfile name generation

subroutines for ssh and socks test suite servers.
This commit is contained in:
Yang Tse 2010-01-15 18:55:01 +00:00
parent a644af129e
commit ada2774ab2
3 changed files with 157 additions and 82 deletions

View File

@ -154,20 +154,7 @@ my $TESTCASES="all";
# No variables below this point should need to be modified
#
my $HTTPPIDFILE=".http.pid";
my $HTTP6PIDFILE=".http6.pid";
my $HTTPSPIDFILE=".https.pid";
my $FTPPIDFILE=".ftp.pid";
my $FTP6PIDFILE=".ftp6.pid";
my $FTP2PIDFILE=".ftp2.pid";
my $FTPSPIDFILE=".ftps.pid";
my $TFTPPIDFILE=".tftpd.pid";
my $TFTP6PIDFILE=".tftp6.pid";
my $SSHPIDFILE=".ssh.pid";
my $SOCKSPIDFILE=".socks.pid";
my $POP3PIDFILE=".pop3.pid";
my $IMAPPIDFILE=".imap.pid";
my $SMTPPIDFILE=".smtp.pid";
my $SOCKSPIDFILE;
# invoke perl like this:
my $perl="perl -I$srcdir";
@ -616,7 +603,7 @@ sub stopserver {
# assign requested address" #
sub verifyhttp {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $cmd = "$VCURL --max-time $server_response_maxtime --output $LOGDIR/verifiedserver --insecure --silent --verbose --globoff \"$proto://$ip:$port/verifiedserver\" 2>$LOGDIR/verifyhttp";
my $pid;
@ -665,7 +652,7 @@ sub verifyhttp {
# assign requested address" #
sub verifyftp {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $pid;
my $time=time();
my $extra;
@ -717,9 +704,10 @@ sub verifyftp {
# actually alive.
sub verifyssh {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $pidfile = server_pidfilename($proto, $ipvnum, $idnum);
my $pid = 0;
if(open(FILE, "<$SSHPIDFILE")) {
if(open(FILE, "<$pidfile")) {
$pid=0+<FILE>;
close(FILE);
}
@ -729,7 +717,7 @@ sub verifyssh {
if(!kill(0, $pid)) {
logmsg "RUN: SSH server has died after starting up\n";
checkdied($pid);
unlink($SSHPIDFILE);
unlink($pidfile);
$pid = -1;
}
}
@ -741,7 +729,7 @@ sub verifyssh {
# with generated config and key files and run a simple remote pwd.
sub verifysftp {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $verified = 0;
# Find out sftp client canonical file name
my $sftp = find_sftp();
@ -777,9 +765,10 @@ sub verifysftp {
# STUB for verifying socks
sub verifysocks {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $pidfile = server_pidfilename($proto, $ipvnum, $idnum);
my $pid = 0;
if(open(FILE, "<$SOCKSPIDFILE")) {
if(open(FILE, "<$pidfile")) {
$pid=0+<FILE>;
close(FILE);
}
@ -789,7 +778,7 @@ sub verifysocks {
if(!kill(0, $pid)) {
logmsg "RUN: SOCKS server has died after starting up\n";
checkdied($pid);
unlink($SOCKSPIDFILE);
unlink($pidfile);
$pid = -1;
}
}
@ -815,7 +804,7 @@ my %protofunc = ('http' => \&verifyhttp,
'socks' => \&verifysocks);
sub verifyserver {
my ($proto, $ip, $port) = @_;
my ($proto, $ip, $port, $ipvnum, $idnum) = @_;
my $count = 30; # try for this many seconds
my $pid;
@ -823,7 +812,7 @@ sub verifyserver {
while($count--) {
my $fun = $protofunc{$proto};
$pid = &$fun($proto, $ip, $port);
$pid = &$fun($proto, $ip, $port, $ipvnum, $idnum);
if($pid > 0) {
last;
@ -897,7 +886,7 @@ sub runhttpserver {
}
# Server is up. Verify that we can speak to it.
my $pid3 = verifyserver($proto, $ip, $port);
my $pid3 = verifyserver($proto, $ip, $port, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -974,7 +963,7 @@ sub runhttpsserver {
}
# Server is up. Verify that we can speak to it.
my $pid3 = verifyserver("https", $ip, $HTTPSPORT);
my $pid3 = verifyserver($proto, $ip, $HTTPSPORT, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -1066,7 +1055,7 @@ sub runpingpongserver {
}
# Server is up. Verify that we can speak to it.
my $pid3 = verifyserver($proto, $ip, $port);
my $pid3 = verifyserver($proto, $ip, $port, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -1143,7 +1132,7 @@ sub runftpsserver {
}
# Server is up. Verify that we can speak to it.
my $pid3 = verifyserver("ftps", $ip, $FTPSPORT);
my $pid3 = verifyserver($proto, $ip, $FTPSPORT, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -1220,7 +1209,7 @@ sub runtftpserver {
}
# Server is up. Verify that we can speak to it.
my $pid3 = verifyserver($proto, $ip, $port);
my $pid3 = verifyserver($proto, $ip, $port, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -1242,36 +1231,47 @@ sub runtftpserver {
#######################################################################
# Start the scp/sftp server
# Start the ssh (scp/sftp) server
#
sub runsshserver {
my ($id, $verbose, $ipv6) = @_;
my $ip=$HOSTIP;
my $port = $SSHPORT;
my $socksport = $SOCKSPORT;
my $pidfile = $SSHPIDFILE;
my $proto = 'ssh';
my $ipvnum = 4;
my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
my $srvrname;
my $pidfile;
my $logfile;
my $flags = "";
$pidfile = server_pidfilename($proto, $ipvnum, $idnum);
# don't retry if the server doesn't work
if ($doesntrun{$pidfile}) {
return (0,0);
}
$srvrname = servername_str($proto, $ipvnum, $idnum);
my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
unlink($pidfile);
my $flag=$verbose?'-v ':'';
$flag .= '-d ' if($debugprotocol);
$srvrname = servername_str($proto, $ipvnum, $idnum);
my $cmd="$perl $srcdir/sshserver.pl ${flag}-u $USER -l $ip -p $port -s $socksport";
$logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
$flags .= "--verbose " if($verbose);
$flags .= "--debugprotocol " if($debugprotocol);
$flags .= "--pidfile \"$pidfile\" ";
$flags .= "--id $idnum " if($idnum > 1);
$flags .= "--ipv$ipvnum --addr \"$ip\" ";
$flags .= "--sshport $port --socksport $socksport ";
$flags .= "--user \"$USER\"";
my $cmd = "$perl $srcdir/sshserver.pl $flags";
my ($sshpid, $pid2) = startnew($cmd, $pidfile, 60, 0);
# on loaded systems sshserver start up can take longer than the timeout
@ -1290,7 +1290,7 @@ sub runsshserver {
# and gives us the opportunity of recovering the pid from the pidfile, when
# this verification succeeds the recovered pid is assigned to pid2.
my $pid3 = verifyserver("ssh",$ip,$port);
my $pid3 = verifyserver($proto, $ip, $port, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to fetch server pid. Kill the server and return failure
@ -1304,7 +1304,10 @@ sub runsshserver {
# is performed actually connecting to it, authenticating and performing a
# very simple remote command. This verification is tried only one time.
if(verifysftp("sftp",$ip,$port) < 1) {
$sshdlog = server_logfilename($LOGDIR, 'ssh', $ipvnum, $idnum);
$sftplog = server_logfilename($LOGDIR, 'sftp', $ipvnum, $idnum);
if(verifysftp("sftp", $ip, $port, $ipvnum, $idnum) < 1) {
logmsg "RUN: SFTP server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
display_sftplog();
@ -1330,25 +1333,32 @@ sub runsocksserver {
my ($id, $verbose, $ipv6) = @_;
my $ip=$HOSTIP;
my $port = $SOCKSPORT;
my $pidfile = $SOCKSPIDFILE;
my $proto = 'socks';
my $ipvnum = 4;
my $idnum = ($id && ($id =~ /^(\d+)$/) && ($id > 1)) ? $id : 1;
my $srvrname;
my $pidfile;
my $logfile;
my $flags = "";
$pidfile = server_pidfilename($proto, $ipvnum, $idnum);
$SOCKSPIDFILE = $pidfile;
# don't retry if the server doesn't work
if ($doesntrun{$pidfile}) {
return (0,0);
}
$srvrname = servername_str($proto, $ipvnum, $idnum);
my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
unlink($pidfile);
$srvrname = servername_str($proto, $ipvnum, $idnum);
$logfile = server_logfilename($LOGDIR, $proto, $ipvnum, $idnum);
# The ssh server must be already running
if(!$run{'ssh'}) {
logmsg "RUN: SOCKS server cannot find running SSH server\n";
@ -1417,6 +1427,8 @@ sub runsocksserver {
return (0,0);
}
$sshlog = server_logfilename($LOGDIR, 'socks', $ipvnum, $idnum);
# start our socks server
my $cmd="$ssh -N -F $sshconfig $ip > $sshlog 2>&1";
my ($sshpid, $pid2) = startnew($cmd, $pidfile, 30, 1);
@ -1434,7 +1446,7 @@ sub runsocksserver {
}
# Ugly hack but ssh doesn't support pid files
my $pid3 = verifyserver("socks",$ip,$port);
my $pid3 = verifyserver($proto, $ip, $port, $ipvnum, $idnum);
if(!$pid3) {
logmsg "RUN: $srvrname server failed verification\n";
# failed to talk to it properly. Kill the server and return failure
@ -1922,7 +1934,6 @@ sub singletest {
logmsg "Warning: test$testnum not present in tests/data/Makefile.am\n";
}
# load the test case file definition
if(loadtest("${TESTDIR}/test${testnum}")) {
if($verbose) {
@ -2392,13 +2403,13 @@ sub singletest {
foreach my $server (@killservers) {
chomp $server;
if($run{$server}) {
$pidlist .= " $run{$server}";
$pidlist .= "$run{$server} ";
$run{$server} = 0;
}
if($server =~ /^(ftp|http|imap|pop3|smtp)s(.*)$/) {
$server = "$1$2";
if($run{$server}) {
$pidlist .= " $run{$server}";
$pidlist .= "$run{$server} ";
$run{$server} = 0;
}
}

View File

@ -5,7 +5,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
# Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
@ -24,7 +24,7 @@
package sshhelp;
use strict;
#use warnings;
use warnings;
use Exporter;
use File::Spec;
@ -111,9 +111,9 @@ $sshkeygenexe = 'ssh-keygen' .exe_ext(); # base name and ext of ssh-keygen
$sshdconfig = 'curl_sshd_config'; # ssh daemon config file
$sshconfig = 'curl_ssh_config'; # ssh client config file
$sftpconfig = 'curl_sftp_config'; # sftp client config file
$sshdlog = 'log/sshd.log'; # ssh daemon log file
$sshlog = 'log/ssh.log'; # ssh client log file
$sftplog = 'log/sftp.log'; # sftp client log file
$sshdlog = undef; # ssh daemon log file
$sshlog = undef; # ssh client log file
$sftplog = undef; # sftp client log file
$sftpcmds = 'curl_sftp_cmds'; # sftp client commands batch file
$knownhosts = 'curl_client_knownhosts'; # ssh knownhosts file
$hstprvkeyf = 'curl_host_dsa_key'; # host private key file
@ -240,6 +240,7 @@ sub display_sftpconfig {
# Display contents of the ssh daemon log file
#
sub display_sshdlog {
die "error: \$sshdlog uninitialized" if(not defined $sshdlog);
display_file($sshdlog);
}
@ -248,6 +249,7 @@ sub display_sshdlog {
# Display contents of the ssh client log file
#
sub display_sshlog {
die "error: \$sshlog uninitialized" if(not defined $sshlog);
display_file($sshlog);
}
@ -256,6 +258,7 @@ sub display_sshlog {
# Display contents of the sftp client log file
#
sub display_sftplog {
die "error: \$sftplog uninitialized" if(not defined $sftplog);
display_file($sftplog);
}

View File

@ -6,7 +6,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
# Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
@ -25,17 +25,8 @@
# Starts sshd for use in the SCP, SFTP and SOCKS curl test harness tests.
# Also creates the ssh configuration files needed for these tests.
# Options:
#
# -v
# -d
# -u user
# -l listen address
# -p SCP/SFTP server port
# -s SOCKS4/5 server port
use strict;
#use warnings;
use warnings;
use Cwd;
#***************************************************************************
@ -75,6 +66,14 @@ use sshhelp qw(
sshversioninfo
);
#***************************************************************************
# Subs imported from serverhelp module
#
use serverhelp qw(
server_pidfilename
server_logfilename
);
#***************************************************************************
@ -83,8 +82,13 @@ my $debugprotocol = 0; # set to 1 for protocol debugging
my $port = 8999; # our default SCP/SFTP server port
my $socksport = $port + 1; # our default SOCKS4/5 server port
my $listenaddr = '127.0.0.1'; # default address on which to listen
my $ipvnum = 4; # default IP version of listener address
my $idnum = 1; # dafault ssh daemon instance number
my $proto = 'ssh'; # protocol the ssh daemon speaks
my $path = getcwd(); # current working directory
my $logdir = $path .'/log'; # directory for log files
my $username = $ENV{USER}; # default user
my $pidfile; # ssh daemon pid file
my $error;
my @cfgarr;
@ -94,35 +98,92 @@ my @cfgarr;
# Parse command line options
#
while(@ARGV) {
if($ARGV[0] eq '-v') {
if($ARGV[0] eq '--verbose') {
$verbose = 1;
}
elsif($ARGV[0] eq '-d') {
elsif($ARGV[0] eq '--debugprotocol') {
$verbose = 1;
$debugprotocol = 1;
}
elsif($ARGV[0] eq '-u') {
$username = $ARGV[1];
shift @ARGV;
}
elsif($ARGV[0] eq '-l') {
$listenaddr = $ARGV[1];
shift @ARGV;
}
elsif($ARGV[0] eq '-p') {
if($ARGV[1] =~ /^(\d+)$/) {
$port = $1;
elsif($ARGV[0] eq '--user') {
if($ARGV[1]) {
$username = $ARGV[1];
shift @ARGV;
}
shift @ARGV;
}
elsif($ARGV[0] eq '-s') {
if($ARGV[1] =~ /^(\d+)$/) {
$socksport = $1;
elsif($ARGV[0] eq '--id') {
if($ARGV[1]) {
if($ARGV[1] =~ /^(\d+)$/) {
$idnum = $1 if($1 > 0);
shift @ARGV;
}
}
shift @ARGV;
}
elsif($ARGV[0] eq '--ipv4') {
$ipvnum = 4;
$listenaddr = '127.0.0.1' if($listenaddr eq '::1');
}
elsif($ARGV[0] eq '--ipv6') {
$ipvnum = 6;
$listenaddr = '::1' if($listenaddr eq '127.0.0.1');
}
elsif($ARGV[0] eq '--addr') {
if($ARGV[1]) {
my $tmpstr = $ARGV[1];
if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
$listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
shift @ARGV;
}
elsif($ipvnum == 6) {
$listenaddr = $tmpstr;
$listenaddr =~ s/^\[(.*)\]$/$1/;
shift @ARGV;
}
}
}
elsif($ARGV[0] eq '--pidfile') {
if($ARGV[1]) {
$pidfile = "$path/". $ARGV[1];
shift @ARGV;
}
}
elsif($ARGV[0] eq '--sshport') {
if($ARGV[1]) {
if($ARGV[1] =~ /^(\d+)$/) {
$port = $1;
shift @ARGV;
}
}
}
elsif($ARGV[0] eq '--socksport') {
if($ARGV[1]) {
if($ARGV[1] =~ /^(\d+)$/) {
$socksport = $1;
shift @ARGV;
}
}
}
else {
print STDERR "\nWarning: sshserver.pl unknown parameter: $ARGV[0]\n";
}
shift @ARGV;
};
}
#***************************************************************************
# Default ssh daemon pid file name
#
if(!$pidfile) {
$pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
}
#***************************************************************************
# ssh, socks and sftp server log file names
#
$sshdlog = server_logfilename($logdir, 'ssh', $ipvnum, $idnum);
$sftplog = server_logfilename($logdir, 'sftp', $ipvnum, $idnum);
$sshlog = server_logfilename($logdir, 'socks', $ipvnum, $idnum);
#***************************************************************************
@ -420,7 +481,7 @@ push @cfgarr, '#';
push @cfgarr, "AuthorizedKeysFile $path/$clipubkeyf";
push @cfgarr, "AuthorizedKeysFile2 $path/$clipubkeyf";
push @cfgarr, "HostKey $path/$hstprvkeyf";
push @cfgarr, "PidFile $path/.ssh.pid";
push @cfgarr, "PidFile $pidfile";
push @cfgarr, '#';
push @cfgarr, "Port $port";
push @cfgarr, "ListenAddress $listenaddr";