runtests.pl 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2008, Daniel Stenberg, <[email protected]>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at http://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # $Id$
  23. ###########################################################################
  24. # Experimental hooks are available to run tests remotely on machines that
  25. # are able to run curl but are unable to run the test harness.
  26. # The following sections need to be modified:
  27. #
  28. # $HOSTIP, $HOST6IP - Set to the address of the host running the test suite
  29. # $CLIENTIP, $CLIENT6IP - Set to the address of the host running curl
  30. # runclient, runclientoutput - Modify to copy all the files in the log/
  31. # directory to the system running curl, run the given command remotely
  32. # and save the return code or returned stdout (respectively), then
  33. # copy all the files from the remote system's log/ directory back to
  34. # the host running the test suite. This can be done a few ways, such
  35. # as using scp & ssh, rsync & telnet, or using a NFS shared directory
  36. # and ssh.
  37. #
  38. # 'make && make test' needs to be done on both machines before making the
  39. # above changes and running runtests.pl manually. In the shared NFS case,
  40. # the contents of the tests/server/ directory must be from the host
  41. # running the test suite, while the rest must be from the host running curl.
  42. #
  43. # Note that even with these changes a number of tests will still fail (mainly
  44. # to do with cookies, those that set environment variables, or those that
  45. # do more than touch the file system in a <precheck> or <postcheck>
  46. # section). These can be added to the $TESTCASES line below,
  47. # e.g. $TESTCASES="!8 !31 !63 !cookies..."
  48. #
  49. # Finally, to properly support -g and -n, checktestcmd needs to change
  50. # to check the remote system's PATH, and the places in the code where
  51. # the curl binary is read directly to determine its type also need to be
  52. # fixed. As long as the -g option is never given, and the -n is always
  53. # given, this won't be a problem.
  54. # These should be the only variables that might be needed to get edited:
  55. BEGIN {
  56. @INC=(@INC, $ENV{'srcdir'}, ".");
  57. }
  58. use strict;
  59. #use Time::HiRes qw( gettimeofday );
  60. #use warnings;
  61. use Cwd;
  62. # Variables and subs imported from sshhelp module
  63. use sshhelp qw(
  64. $sshdexe
  65. $sshexe
  66. $sftpexe
  67. $sshconfig
  68. $sftpconfig
  69. $sshlog
  70. $sftplog
  71. $sftpcmds
  72. display_sshdconfig
  73. display_sshconfig
  74. display_sftpconfig
  75. display_sshdlog
  76. display_sshlog
  77. display_sftplog
  78. find_sshd
  79. find_ssh
  80. find_sftp
  81. sshversioninfo
  82. );
  83. require "getpart.pm"; # array functions
  84. require "valgrind.pm"; # valgrind report parser
  85. require "ftp.pm";
  86. my $HOSTIP="127.0.0.1"; # address on which the test server listens
  87. my $HOST6IP="[::1]"; # address on which the test server listens
  88. my $CLIENTIP="127.0.0.1"; # address which curl uses for incoming connections
  89. my $CLIENT6IP="[::1]"; # address which curl uses for incoming connections
  90. my $base = 8990; # base port number
  91. my $HTTPPORT; # HTTP server port
  92. my $HTTP6PORT; # HTTP IPv6 server port
  93. my $HTTPSPORT; # HTTPS server port
  94. my $FTPPORT; # FTP server port
  95. my $FTP2PORT; # FTP server 2 port
  96. my $FTPSPORT; # FTPS server port
  97. my $FTP6PORT; # FTP IPv6 server port
  98. my $TFTPPORT; # TFTP
  99. my $TFTP6PORT; # TFTP
  100. my $SSHPORT; # SCP/SFTP
  101. my $SOCKSPORT; # SOCKS4/5 port
  102. my $srcdir = $ENV{'srcdir'} || '.';
  103. my $CURL="../src/curl"; # what curl executable to run on the tests
  104. my $DBGCURL=$CURL; #"../src/.libs/curl"; # alternative for debugging
  105. my $LOGDIR="log";
  106. my $TESTDIR="$srcdir/data";
  107. my $LIBDIR="./libtest";
  108. my $SERVERIN="$LOGDIR/server.input"; # what curl sent the server
  109. my $SERVER2IN="$LOGDIR/server2.input"; # what curl sent the second server
  110. my $CURLLOG="$LOGDIR/curl.log"; # all command lines run
  111. my $FTPDCMD="$LOGDIR/ftpserver.cmd"; # copy ftp server instructions here
  112. my $SERVERLOGS_LOCK="$LOGDIR/serverlogs.lock"; # server logs advisor read lock
  113. # Normally, all test cases should be run, but at times it is handy to
  114. # simply run a particular one:
  115. my $TESTCASES="all";
  116. # To run specific test cases, set them like:
  117. # $TESTCASES="1 2 3 7 8";
  118. #######################################################################
  119. # No variables below this point should need to be modified
  120. #
  121. my $HTTPPIDFILE=".http.pid";
  122. my $HTTP6PIDFILE=".http6.pid";
  123. my $HTTPSPIDFILE=".https.pid";
  124. my $FTPPIDFILE=".ftp.pid";
  125. my $FTP6PIDFILE=".ftp6.pid";
  126. my $FTP2PIDFILE=".ftp2.pid";
  127. my $FTPSPIDFILE=".ftps.pid";
  128. my $TFTPPIDFILE=".tftpd.pid";
  129. my $TFTP6PIDFILE=".tftp6.pid";
  130. my $SSHPIDFILE=".ssh.pid";
  131. my $SOCKSPIDFILE=".socks.pid";
  132. # invoke perl like this:
  133. my $perl="perl -I$srcdir";
  134. my $server_response_maxtime=13;
  135. # this gets set if curl is compiled with debugging:
  136. my $curl_debug=0;
  137. my $libtool;
  138. # name of the file that the memory debugging creates:
  139. my $memdump="$LOGDIR/memdump";
  140. # the path to the script that analyzes the memory debug output file:
  141. my $memanalyze="$perl $srcdir/memanalyze.pl";
  142. my $pwd = getcwd(); # current working directory
  143. my $start;
  144. my $forkserver=0;
  145. my $ftpchecktime; # time it took to verify our test FTP server
  146. my $stunnel = checkcmd("stunnel4") || checkcmd("stunnel");
  147. my $valgrind = checktestcmd("valgrind");
  148. my $valgrind_logfile="--logfile";
  149. my $valgrind_tool;
  150. my $gdb = checktestcmd("gdb");
  151. my $ssl_version; # set if libcurl is built with SSL support
  152. my $large_file; # set if libcurl is built with large file support
  153. my $has_idn; # set if libcurl is built with IDN support
  154. my $http_ipv6; # set if HTTP server has IPv6 support
  155. my $ftp_ipv6; # set if FTP server has IPv6 support
  156. my $tftp_ipv6; # set if TFTP server has IPv6 support
  157. my $has_ipv6; # set if libcurl is built with IPv6 support
  158. my $has_libz; # set if libcurl is built with libz support
  159. my $has_getrlimit; # set if system has getrlimit()
  160. my $has_ntlm; # set if libcurl is built with NTLM support
  161. my $has_openssl; # built with a lib using an OpenSSL-like API
  162. my $has_gnutls; # built with GnuTLS
  163. my $has_nss; # built with NSS
  164. my $has_yassl; # built with yassl
  165. my $ssllib; # name of the lib we use (for human presentation)
  166. my $has_crypto; # set if libcurl is built with cryptographic support
  167. my $has_textaware; # set if running on a system that has a text mode concept
  168. # on files. Windows for example
  169. my @protocols; # array of supported protocols
  170. my $skipped=0; # number of tests skipped; reported in main loop
  171. my %skipped; # skipped{reason}=counter, reasons for skip
  172. my @teststat; # teststat[testnum]=reason, reasons for skip
  173. my %disabled_keywords; # key words of tests to skip
  174. my %enabled_keywords; # key words of tests to run
  175. my $sshdid; # for socks server, ssh daemon version id
  176. my $sshdvernum; # for socks server, ssh daemon version number
  177. my $sshdverstr; # for socks server, ssh daemon version string
  178. my $sshderror; # for socks server, ssh daemon version error
  179. my $defserverlogslocktimeout = 20; # timeout to await server logs lock removal
  180. my $defpostcommanddelay = 0; # delay between command and postcheck sections
  181. #######################################################################
  182. # variables the command line options may set
  183. #
  184. my $short;
  185. my $verbose;
  186. my $debugprotocol;
  187. my $anyway;
  188. my $gdbthis; # run test case with gdb debugger
  189. my $keepoutfiles; # keep stdout and stderr files after tests
  190. my $listonly; # only list the tests
  191. my $postmortem; # display detailed info about failed tests
  192. my %run; # running server
  193. my %doesntrun; # servers that don't work, identified by pidfile
  194. # torture test variables
  195. my $torture;
  196. my $tortnum;
  197. my $tortalloc;
  198. # open and close each time to allow removal at any time
  199. sub logmsg {
  200. # uncomment the Time::HiRes usage for this
  201. # my ($seconds, $microseconds) = gettimeofday;
  202. # my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  203. # localtime($seconds);
  204. my $t;
  205. if(1) {
  206. # $t = sprintf ("%02d:%02d:%02d.%06d ", $hour, $min, $sec,
  207. # $microseconds);
  208. }
  209. for(@_) {
  210. print "${t}$_";
  211. }
  212. }
  213. # get the name of the current user
  214. my $USER = $ENV{USER}; # Linux
  215. if (!$USER) {
  216. $USER = $ENV{USERNAME}; # Windows
  217. if (!$USER) {
  218. $USER = $ENV{LOGNAME}; # Some UNIX (I think)
  219. }
  220. }
  221. # enable memory debugging if curl is compiled with it
  222. $ENV{'CURL_MEMDEBUG'} = $memdump;
  223. $ENV{'HOME'}=$pwd;
  224. sub catch_zap {
  225. my $signame = shift;
  226. logmsg "runtests.pl received SIG$signame, exiting\n";
  227. stopservers(1);
  228. die "Somebody sent me a SIG$signame";
  229. }
  230. $SIG{INT} = \&catch_zap;
  231. $SIG{KILL} = \&catch_zap;
  232. ##########################################################################
  233. # Clear all possible '*_proxy' environment variables for various protocols
  234. # to prevent them to interfere with our testing!
  235. my $protocol;
  236. foreach $protocol (('ftp', 'http', 'ftps', 'https', 'no')) {
  237. my $proxy = "${protocol}_proxy";
  238. # clear lowercase version
  239. $ENV{$proxy}=undef;
  240. # clear uppercase version
  241. $ENV{uc($proxy)}=undef;
  242. }
  243. # make sure we don't get affected by other variables that control our
  244. # behaviour
  245. $ENV{'SSL_CERT_DIR'}=undef;
  246. $ENV{'SSL_CERT_PATH'}=undef;
  247. $ENV{'CURL_CA_BUNDLE'}=undef;
  248. #######################################################################
  249. # Check if a given child process has just died. Reaps it if so.
  250. #
  251. sub checkdied {
  252. use POSIX ":sys_wait_h";
  253. my $pid = $_[0];
  254. if(not defined $pid || $pid <= 0) {
  255. return 0;
  256. }
  257. my $rc = waitpid($pid, &WNOHANG);
  258. return ($rc == $pid)?1:0;
  259. }
  260. #######################################################################
  261. # Start a new thread/process and run the given command line in there.
  262. # Return the pids (yes plural) of the new child process to the parent.
  263. #
  264. sub startnew {
  265. my ($cmd, $pidfile, $timeout, $fake)=@_;
  266. logmsg "startnew: $cmd\n" if ($verbose);
  267. my $child = fork();
  268. my $pid2 = 0;
  269. if(not defined $child) {
  270. logmsg "startnew: fork() failure detected\n";
  271. return (-1,-1);
  272. }
  273. if(0 == $child) {
  274. # Here we are the child. Run the given command.
  275. # Put an "exec" in front of the command so that the child process
  276. # keeps this child's process ID.
  277. exec("exec $cmd") || die "Can't exec() $cmd: $!";
  278. # exec() should never return back here to this process. We protect
  279. # ourselves by calling die() just in case something goes really bad.
  280. die "error: exec() has returned";
  281. }
  282. # Ugly hack but ssh client doesn't support pid files
  283. if ($fake) {
  284. if(open(OUT, ">$pidfile")) {
  285. print OUT $child . "\n";
  286. close(OUT);
  287. logmsg "startnew: $pidfile faked with pid=$child\n" if($verbose);
  288. }
  289. else {
  290. logmsg "startnew: failed to write fake $pidfile with pid=$child\n";
  291. }
  292. # could/should do a while connect fails sleep a bit and loop
  293. sleep $timeout;
  294. if (checkdied($child)) {
  295. logmsg "startnew: child process has failed to start\n" if($verbose);
  296. return (-1,-1);
  297. }
  298. }
  299. my $count = $timeout;
  300. while($count--) {
  301. if(-f $pidfile && -s $pidfile && open(PID, "<$pidfile")) {
  302. $pid2 = 0 + <PID>;
  303. close(PID);
  304. if(($pid2 > 0) && kill(0, $pid2)) {
  305. # if $pid2 is valid, then make sure this pid is alive, as
  306. # otherwise it is just likely to be the _previous_ pidfile or
  307. # similar!
  308. last;
  309. }
  310. # invalidate $pid2 if not actually alive
  311. $pid2 = 0;
  312. }
  313. if (checkdied($child)) {
  314. logmsg "startnew: child process has died, server might start up\n"
  315. if($verbose);
  316. # We can't just abort waiting for the server with a
  317. # return (-1,-1);
  318. # because the server might have forked and could still start
  319. # up normally. Instead, just reduce the amount of time we remain
  320. # waiting.
  321. $count >>= 2;
  322. }
  323. sleep(1);
  324. }
  325. # Return two PIDs, the one for the child process we spawned and the one
  326. # reported by the server itself (in case it forked again on its own).
  327. # Both (potentially) need to be killed at the end of the test.
  328. return ($child, $pid2);
  329. }
  330. #######################################################################
  331. # Check for a command in the PATH of the test server.
  332. #
  333. sub checkcmd {
  334. my ($cmd)=@_;
  335. my @paths=(split(":", $ENV{'PATH'}), "/usr/sbin", "/usr/local/sbin",
  336. "/sbin", "/usr/bin", "/usr/local/bin" );
  337. for(@paths) {
  338. if( -x "$_/$cmd" && ! -d "$_/$cmd") {
  339. # executable bit but not a directory!
  340. return "$_/$cmd";
  341. }
  342. }
  343. }
  344. #######################################################################
  345. # Check for a command in the PATH of the machine running curl.
  346. #
  347. sub checktestcmd {
  348. my ($cmd)=@_;
  349. return checkcmd($cmd);
  350. }
  351. #######################################################################
  352. # Run the application under test and return its return code
  353. #
  354. sub runclient {
  355. my ($cmd)=@_;
  356. return system($cmd);
  357. # This is one way to test curl on a remote machine
  358. # my $out = system("ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'");
  359. # sleep 2; # time to allow the NFS server to be updated
  360. # return $out;
  361. }
  362. #######################################################################
  363. # Run the application under test and return its stdout
  364. #
  365. sub runclientoutput {
  366. my ($cmd)=@_;
  367. return `$cmd`;
  368. # This is one way to test curl on a remote machine
  369. # my @out = `ssh $CLIENTIP cd \'$pwd\' \\; \'$cmd\'`;
  370. # sleep 2; # time to allow the NFS server to be updated
  371. # return @out;
  372. }
  373. #######################################################################
  374. # Memory allocation test and failure torture testing.
  375. #
  376. sub torture {
  377. my $testcmd = shift;
  378. my $gdbline = shift;
  379. # remove memdump first to be sure we get a new nice and clean one
  380. unlink($memdump);
  381. # First get URL from test server, ignore the output/result
  382. runclient($testcmd);
  383. logmsg " CMD: $testcmd\n" if($verbose);
  384. # memanalyze -v is our friend, get the number of allocations made
  385. my $count=0;
  386. my @out = `$memanalyze -v $memdump`;
  387. for(@out) {
  388. if(/^Allocations: (\d+)/) {
  389. $count = $1;
  390. last;
  391. }
  392. }
  393. if(!$count) {
  394. logmsg " found no allocs to make fail\n";
  395. return 0;
  396. }
  397. logmsg " $count allocations to make fail\n";
  398. for ( 1 .. $count ) {
  399. my $limit = $_;
  400. my $fail;
  401. my $dumped_core;
  402. if($tortalloc && ($tortalloc != $limit)) {
  403. next;
  404. }
  405. logmsg "Fail alloc no: $limit\r" if($verbose);
  406. # make the memory allocation function number $limit return failure
  407. $ENV{'CURL_MEMLIMIT'} = $limit;
  408. # remove memdump first to be sure we get a new nice and clean one
  409. unlink($memdump);
  410. logmsg "**> Alloc number $limit is now set to fail <**\n" if($gdbthis);
  411. my $ret;
  412. if($gdbthis) {
  413. runclient($gdbline)
  414. }
  415. else {
  416. $ret = runclient($testcmd);
  417. }
  418. # Now clear the variable again
  419. $ENV{'CURL_MEMLIMIT'} = undef;
  420. if(-r "core") {
  421. # there's core file present now!
  422. logmsg " core dumped\n";
  423. $dumped_core = 1;
  424. $fail = 2;
  425. }
  426. # verify that it returns a proper error code, doesn't leak memory
  427. # and doesn't core dump
  428. if($ret & 255) {
  429. logmsg " system() returned $ret\n";
  430. $fail=1;
  431. }
  432. else {
  433. my @memdata=`$memanalyze $memdump`;
  434. my $leak=0;
  435. for(@memdata) {
  436. if($_ ne "") {
  437. # well it could be other memory problems as well, but
  438. # we call it leak for short here
  439. $leak=1;
  440. }
  441. }
  442. if($leak) {
  443. logmsg "** MEMORY FAILURE\n";
  444. logmsg @memdata;
  445. logmsg `$memanalyze -l $memdump`;
  446. $fail = 1;
  447. }
  448. }
  449. if($fail) {
  450. logmsg " Failed on alloc number $limit in test.\n",
  451. " invoke with -t$limit to repeat this single case.\n";
  452. stopservers($verbose);
  453. return 1;
  454. }
  455. }
  456. logmsg "torture OK\n";
  457. return 0;
  458. }
  459. #######################################################################
  460. # stop the given test server (pid)
  461. #
  462. sub stopserver {
  463. my ($pid) = @_;
  464. if(not defined $pid || $pid <= 0) {
  465. return; # whad'da'ya wanna'da with no pid ?
  466. }
  467. # It might be more than one pid
  468. # Send each one a SIGTERM to gracefully kill it
  469. my @killed;
  470. my @pids = split(/\s+/, $pid);
  471. for (@pids) {
  472. chomp($_);
  473. if($_ =~ /^(\d+)$/) {
  474. if(($1 > 0) && kill(0, $1)) {
  475. if($verbose) {
  476. logmsg "RUN: Test server pid $1 signalled to die\n";
  477. }
  478. kill(15, $1); # die!
  479. push @killed, $1;
  480. }
  481. }
  482. }
  483. # Give each process killed up to a few seconds to die, then send
  484. # a SIGKILL to finish it off for good.
  485. for (@killed) {
  486. my $count = 5; # wait for this many seconds for server to die
  487. while($count--) {
  488. if (!kill(0, $_) || checkdied($_)) {
  489. last;
  490. }
  491. sleep(1);
  492. }
  493. if ($count < 0) {
  494. logmsg "RUN: forcing pid $_ to die with SIGKILL\n";
  495. kill(9, $_); # die!
  496. }
  497. }
  498. }
  499. #######################################################################
  500. # Verify that the server that runs on $ip, $port is our server. This also
  501. # implies that we can speak with it, as there might be occasions when the
  502. # server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
  503. # assign requested address" #
  504. sub verifyhttp {
  505. my ($proto, $ip, $port) = @_;
  506. my $cmd = "$CURL --max-time $server_response_maxtime --output $LOGDIR/verifiedserver --insecure --silent --verbose --globoff \"$proto://$ip:$port/verifiedserver\" 2>$LOGDIR/verifyhttp";
  507. my $pid;
  508. # verify if our/any server is running on this port
  509. logmsg "CMD; $cmd\n" if ($verbose);
  510. my $res = runclient($cmd);
  511. $res >>= 8; # rotate the result
  512. my $data;
  513. if($res && $verbose) {
  514. open(ERR, "<$LOGDIR/verifyhttp");
  515. my @e = <ERR>;
  516. close(ERR);
  517. logmsg "RUN: curl command returned $res\n";
  518. for(@e) {
  519. if($_ !~ /^([ \t]*)$/) {
  520. logmsg "RUN: $_";
  521. }
  522. }
  523. }
  524. open(FILE, "<$LOGDIR/verifiedserver");
  525. my @file=<FILE>;
  526. close(FILE);
  527. $data=$file[0]; # first line
  528. if ( $data =~ /WE ROOLZ: (\d+)/ ) {
  529. $pid = 0+$1;
  530. }
  531. elsif($res == 6) {
  532. # curl: (6) Couldn't resolve host '::1'
  533. logmsg "RUN: failed to resolve host ($proto://$ip:$port/verifiedserver)\n";
  534. return -1;
  535. }
  536. elsif($data || ($res != 7)) {
  537. logmsg "RUN: Unknown server is running on port $port\n";
  538. return -1;
  539. }
  540. return $pid;
  541. }
  542. #######################################################################
  543. # Verify that the server that runs on $ip, $port is our server. This also
  544. # implies that we can speak with it, as there might be occasions when the
  545. # server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
  546. # assign requested address" #
  547. sub verifyftp {
  548. my ($proto, $ip, $port) = @_;
  549. my $pid;
  550. my $time=time();
  551. my $extra;
  552. if($proto eq "ftps") {
  553. $extra = "--insecure --ftp-ssl-control ";
  554. }
  555. my $cmd="$CURL --max-time $server_response_maxtime --silent --verbose --globoff $extra\"$proto://$ip:$port/verifiedserver\" 2>$LOGDIR/verifyftp";
  556. # check if this is our server running on this port:
  557. my @data=runclientoutput($cmd);
  558. logmsg "RUN: $cmd\n" if($verbose);
  559. my $line;
  560. foreach $line (@data) {
  561. if ( $line =~ /WE ROOLZ: (\d+)/ ) {
  562. # this is our test server with a known pid!
  563. $pid = 0+$1;
  564. last;
  565. }
  566. }
  567. if($pid <= 0 && $data[0]) {
  568. # this is not a known server
  569. logmsg "RUN: Unknown server on our FTP port: $port\n";
  570. return 0;
  571. }
  572. # we can/should use the time it took to verify the FTP server as a measure
  573. # on how fast/slow this host/FTP is.
  574. my $took = time()-$time;
  575. if($verbose) {
  576. logmsg "RUN: Verifying our test FTP server took $took seconds\n";
  577. }
  578. $ftpchecktime = $took?$took:1; # make sure it never is zero
  579. return $pid;
  580. }
  581. #######################################################################
  582. # Verify that the ssh server has written out its pidfile, recovering
  583. # the pid from the file and returning it if a process with that pid is
  584. # actually alive.
  585. sub verifyssh {
  586. my ($proto, $ip, $port) = @_;
  587. my $pid = 0;
  588. if(open(FILE, "<$SSHPIDFILE")) {
  589. $pid=0+<FILE>;
  590. close(FILE);
  591. }
  592. if($pid > 0) {
  593. # if we have a pid it is actually our ssh server,
  594. # since runsshserver() unlinks previous pidfile
  595. if(!kill(0, $pid)) {
  596. logmsg "RUN: SSH server has died after starting up\n";
  597. checkdied($pid);
  598. unlink($SSHPIDFILE);
  599. $pid = -1;
  600. }
  601. }
  602. return $pid;
  603. }
  604. #######################################################################
  605. # Verify that we can connect to the sftp server, properly authenticate
  606. # with generated config and key files and run a simple remote pwd.
  607. sub verifysftp {
  608. my ($proto, $ip, $port) = @_;
  609. my $verified = 0;
  610. # Find out sftp client canonical file name
  611. my $sftp = find_sftp();
  612. if(!$sftp) {
  613. logmsg "RUN: SFTP server cannot find $sftpexe\n";
  614. return -1;
  615. }
  616. # Find out ssh client canonical file name
  617. my $ssh = find_ssh();
  618. if(!$ssh) {
  619. logmsg "RUN: SFTP server cannot find $sshexe\n";
  620. return -1;
  621. }
  622. # Connect to sftp server, authenticate and run a remote pwd
  623. # command using our generated configuration and key files
  624. my $cmd = "$sftp -b $sftpcmds -F $sftpconfig -S $ssh $ip > $sftplog 2>&1";
  625. my $res = runclient($cmd);
  626. # Search for pwd command response in log file
  627. if(open(SFTPLOGFILE, "<$sftplog")) {
  628. while(<SFTPLOGFILE>) {
  629. if(/^Remote working directory: /) {
  630. $verified = 1;
  631. last;
  632. }
  633. }
  634. close(SFTPLOGFILE);
  635. }
  636. return $verified;
  637. }
  638. #######################################################################
  639. # STUB for verifying socks
  640. sub verifysocks {
  641. my ($proto, $ip, $port) = @_;
  642. my $pid = 0;
  643. if(open(FILE, "<$SOCKSPIDFILE")) {
  644. $pid=0+<FILE>;
  645. close(FILE);
  646. }
  647. if($pid > 0) {
  648. # if we have a pid it is actually our socks server,
  649. # since runsocksserver() unlinks previous pidfile
  650. if(!kill(0, $pid)) {
  651. logmsg "RUN: SOCKS server has died after starting up\n";
  652. checkdied($pid);
  653. unlink($SOCKSPIDFILE);
  654. $pid = -1;
  655. }
  656. }
  657. return $pid;
  658. }
  659. #######################################################################
  660. # Verify that the server that runs on $ip, $port is our server.
  661. # Retry over several seconds before giving up. The ssh server in
  662. # particular can take a long time to start if it needs to generate
  663. # keys on a slow or loaded host.
  664. #
  665. my %protofunc = ('http' => \&verifyhttp,
  666. 'https' => \&verifyhttp,
  667. 'ftp' => \&verifyftp,
  668. 'ftps' => \&verifyftp,
  669. 'tftp' => \&verifyftp,
  670. 'ssh' => \&verifyssh,
  671. 'socks' => \&verifysocks);
  672. sub verifyserver {
  673. my ($proto, $ip, $port) = @_;
  674. my $count = 30; # try for this many seconds
  675. my $pid;
  676. while($count--) {
  677. my $fun = $protofunc{$proto};
  678. $pid = &$fun($proto, $ip, $port);
  679. if($pid > 0) {
  680. last;
  681. }
  682. elsif($pid < 0) {
  683. # a real failure, stop trying and bail out
  684. return 0;
  685. }
  686. sleep(1);
  687. }
  688. return $pid;
  689. }
  690. #######################################################################
  691. # start the http server
  692. #
  693. sub runhttpserver {
  694. my ($verbose, $ipv6) = @_;
  695. my $RUNNING;
  696. my $pidfile = $HTTPPIDFILE;
  697. my $port = $HTTPPORT;
  698. my $ip = $HOSTIP;
  699. my $nameext;
  700. my $fork = $forkserver?"--fork":"";
  701. if($ipv6) {
  702. # if IPv6, use a different setup
  703. $pidfile = $HTTP6PIDFILE;
  704. $port = $HTTP6PORT;
  705. $ip = $HOST6IP;
  706. $nameext="-ipv6";
  707. }
  708. # don't retry if the server doesn't work
  709. if ($doesntrun{$pidfile}) {
  710. return (0,0);
  711. }
  712. my $pid = checkserver($pidfile);
  713. if($pid > 0) {
  714. stopserver($pid);
  715. }
  716. unlink($pidfile);
  717. my $flag=$debugprotocol?"-v ":"";
  718. my $dir=$ENV{'srcdir'};
  719. if($dir) {
  720. $flag .= "-d \"$dir\" ";
  721. }
  722. my $cmd="$perl $srcdir/httpserver.pl -p $pidfile $fork$flag $port $ipv6";
  723. my ($httppid, $pid2) =
  724. startnew($cmd, $pidfile, 15, 0); # start the server in a new process
  725. if($httppid <= 0 || !kill(0, $httppid)) {
  726. # it is NOT alive
  727. logmsg "RUN: failed to start the HTTP$nameext server\n";
  728. stopserver("$pid2");
  729. $doesntrun{$pidfile} = 1;
  730. return (0,0);
  731. }
  732. # Server is up. Verify that we can speak to it.
  733. my $pid3 = verifyserver("http", $ip, $port);
  734. if(!$pid3) {
  735. logmsg "RUN: HTTP$nameext server failed verification\n";
  736. # failed to talk to it properly. Kill the server and return failure
  737. stopserver("$httppid $pid2");
  738. $doesntrun{$pidfile} = 1;
  739. return (0,0);
  740. }
  741. $pid2 = $pid3;
  742. if($verbose) {
  743. logmsg "RUN: HTTP$nameext server is now running PID $httppid\n";
  744. }
  745. sleep(1);
  746. return ($httppid, $pid2);
  747. }
  748. #######################################################################
  749. # start the https server (or rather, tunnel)
  750. #
  751. sub runhttpsserver {
  752. my ($verbose, $ipv6) = @_;
  753. my $STATUS;
  754. my $RUNNING;
  755. my $ip = $HOSTIP;
  756. my $pidfile = $HTTPSPIDFILE;
  757. if(!$stunnel) {
  758. return 0;
  759. }
  760. if($ipv6) {
  761. # not complete yet
  762. $ip = $HOST6IP;
  763. }
  764. # don't retry if the server doesn't work
  765. if ($doesntrun{$pidfile}) {
  766. return (0,0);
  767. }
  768. my $pid = checkserver($pidfile);
  769. if($pid > 0) {
  770. # kill previous stunnel!
  771. stopserver($pid);
  772. }
  773. unlink($pidfile);
  774. my $flag=$debugprotocol?"-v ":"";
  775. my $cmd="$perl $srcdir/httpsserver.pl $flag -p https -s \"$stunnel\" -d $srcdir -r $HTTPPORT $HTTPSPORT";
  776. my ($httpspid, $pid2) = startnew($cmd, $pidfile, 15, 0);
  777. if($httpspid <= 0 || !kill(0, $httpspid)) {
  778. # it is NOT alive
  779. logmsg "RUN: failed to start the HTTPS server\n";
  780. stopservers($verbose);
  781. $doesntrun{$pidfile} = 1;
  782. return(0,0);
  783. }
  784. # Server is up. Verify that we can speak to it.
  785. my $pid3 = verifyserver("https", $ip, $HTTPSPORT);
  786. if(!$pid3) {
  787. logmsg "RUN: HTTPS server failed verification\n";
  788. # failed to talk to it properly. Kill the server and return failure
  789. stopserver("$httpspid $pid2");
  790. $doesntrun{$pidfile} = 1;
  791. return (0,0);
  792. }
  793. # Here pid3 is actually the pid returned by the unsecure-http server.
  794. if($verbose) {
  795. logmsg "RUN: HTTPS server is now running PID $httpspid\n";
  796. }
  797. sleep(1);
  798. return ($httpspid, $pid2);
  799. }
  800. #######################################################################
  801. # start the ftp server
  802. #
  803. sub runftpserver {
  804. my ($id, $verbose, $ipv6) = @_;
  805. my $STATUS;
  806. my $RUNNING;
  807. my $port = $id?$FTP2PORT:$FTPPORT;
  808. # check for pidfile
  809. my $pidfile = $id?$FTP2PIDFILE:$FTPPIDFILE;
  810. my $ip=$HOSTIP;
  811. my $nameext;
  812. my $cmd;
  813. if($ipv6) {
  814. # if IPv6, use a different setup
  815. $pidfile = $FTP6PIDFILE;
  816. $port = $FTP6PORT;
  817. $ip = $HOST6IP;
  818. $nameext="-ipv6";
  819. }
  820. # don't retry if the server doesn't work
  821. if ($doesntrun{$pidfile}) {
  822. return (0,0);
  823. }
  824. my $pid = checkserver($pidfile);
  825. if($pid > 0) {
  826. stopserver($pid);
  827. }
  828. unlink($pidfile);
  829. # start our server:
  830. my $flag=$debugprotocol?"-v ":"";
  831. $flag .= "-s \"$srcdir\" ";
  832. my $addr;
  833. if($id) {
  834. $flag .="--id $id ";
  835. }
  836. if($ipv6) {
  837. $flag .="--ipv6 ";
  838. $addr = $HOST6IP;
  839. } else {
  840. $addr = $HOSTIP;
  841. }
  842. $cmd="$perl $srcdir/ftpserver.pl --pidfile $pidfile $flag --port $port --addr \"$addr\"";
  843. my ($ftppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
  844. if($ftppid <= 0 || !kill(0, $ftppid)) {
  845. # it is NOT alive
  846. logmsg "RUN: failed to start the FTP$id$nameext server\n";
  847. stopserver("$pid2");
  848. $doesntrun{$pidfile} = 1;
  849. return (0,0);
  850. }
  851. # Server is up. Verify that we can speak to it.
  852. my $pid3 = verifyserver("ftp", $ip, $port);
  853. if(!$pid3) {
  854. logmsg "RUN: FTP$id$nameext server failed verification\n";
  855. # failed to talk to it properly. Kill the server and return failure
  856. stopserver("$ftppid $pid2");
  857. $doesntrun{$pidfile} = 1;
  858. return (0,0);
  859. }
  860. $pid2 = $pid3;
  861. if($verbose) {
  862. logmsg "RUN: FTP$id$nameext server is now running PID $ftppid\n";
  863. }
  864. sleep(1);
  865. return ($pid2, $ftppid);
  866. }
  867. #######################################################################
  868. # start the ftps server (or rather, tunnel)
  869. #
  870. sub runftpsserver {
  871. my ($verbose, $ipv6) = @_;
  872. my $STATUS;
  873. my $RUNNING;
  874. my $ip = $HOSTIP;
  875. my $pidfile = $FTPSPIDFILE;
  876. if(!$stunnel) {
  877. return 0;
  878. }
  879. if($ipv6) {
  880. # not complete yet
  881. $ip = $HOST6IP;
  882. }
  883. # don't retry if the server doesn't work
  884. if ($doesntrun{$pidfile}) {
  885. return (0,0);
  886. }
  887. my $pid = checkserver($pidfile);
  888. if($pid > 0) {
  889. # kill previous stunnel!
  890. stopserver($pid);
  891. }
  892. unlink($pidfile);
  893. my $flag=$debugprotocol?"-v ":"";
  894. my $cmd="$perl $srcdir/httpsserver.pl $flag -p ftps -s \"$stunnel\" -d $srcdir -r $FTPPORT $FTPSPORT";
  895. my ($ftpspid, $pid2) = startnew($cmd, $pidfile, 15, 0);
  896. if($ftpspid <= 0 || !kill(0, $ftpspid)) {
  897. # it is NOT alive
  898. logmsg "RUN: failed to start the FTPS server\n";
  899. stopservers($verbose);
  900. $doesntrun{$pidfile} = 1;
  901. return(0,0);
  902. }
  903. # Server is up. Verify that we can speak to it.
  904. my $pid3 = verifyserver("ftps", $ip, $FTPSPORT);
  905. if(!$pid3) {
  906. logmsg "RUN: FTPS server failed verification\n";
  907. # failed to talk to it properly. Kill the server and return failure
  908. stopserver("$ftpspid $pid2");
  909. $doesntrun{$pidfile} = 1;
  910. return (0,0);
  911. }
  912. # Here pid3 is actually the pid returned by the unsecure-ftp server.
  913. if($verbose) {
  914. logmsg "RUN: FTPS server is now running PID $ftpspid\n";
  915. }
  916. sleep(1);
  917. return ($ftpspid, $pid2);
  918. }
  919. #######################################################################
  920. # start the tftp server
  921. #
  922. sub runtftpserver {
  923. my ($id, $verbose, $ipv6) = @_;
  924. my $STATUS;
  925. my $RUNNING;
  926. my $port = $TFTPPORT;
  927. # check for pidfile
  928. my $pidfile = $TFTPPIDFILE;
  929. my $ip=$HOSTIP;
  930. my $nameext;
  931. my $cmd;
  932. if($ipv6) {
  933. # if IPv6, use a different setup
  934. $pidfile = $TFTP6PIDFILE;
  935. $port = $TFTP6PORT;
  936. $ip = $HOST6IP;
  937. $nameext="-ipv6";
  938. }
  939. # don't retry if the server doesn't work
  940. if ($doesntrun{$pidfile}) {
  941. return (0,0);
  942. }
  943. my $pid = checkserver($pidfile);
  944. if($pid > 0) {
  945. stopserver($pid);
  946. }
  947. unlink($pidfile);
  948. # start our server:
  949. my $flag=$debugprotocol?"-v ":"";
  950. $flag .= "-s \"$srcdir\" ";
  951. if($id) {
  952. $flag .="--id $id ";
  953. }
  954. if($ipv6) {
  955. $flag .="--ipv6 ";
  956. }
  957. $cmd="./server/tftpd --pidfile $pidfile $flag $port";
  958. my ($tftppid, $pid2) = startnew($cmd, $pidfile, 15, 0);
  959. if($tftppid <= 0 || !kill(0, $tftppid)) {
  960. # it is NOT alive
  961. logmsg "RUN: failed to start the TFTP$id$nameext server\n";
  962. stopserver("$pid2");
  963. $doesntrun{$pidfile} = 1;
  964. return (0,0);
  965. }
  966. # Server is up. Verify that we can speak to it.
  967. my $pid3 = verifyserver("tftp", $ip, $port);
  968. if(!$pid3) {
  969. logmsg "RUN: TFTP$id$nameext server failed verification\n";
  970. # failed to talk to it properly. Kill the server and return failure
  971. stopserver("$tftppid $pid2");
  972. $doesntrun{$pidfile} = 1;
  973. return (0,0);
  974. }
  975. $pid2 = $pid3;
  976. if($verbose) {
  977. logmsg "RUN: TFTP$id$nameext server is now running PID $tftppid\n";
  978. }
  979. sleep(1);
  980. return ($pid2, $tftppid);
  981. }
  982. #######################################################################
  983. # Start the scp/sftp server
  984. #
  985. sub runsshserver {
  986. my ($id, $verbose, $ipv6) = @_;
  987. my $ip=$HOSTIP;
  988. my $port = $SSHPORT;
  989. my $socksport = $SOCKSPORT;
  990. my $pidfile = $SSHPIDFILE;
  991. # don't retry if the server doesn't work
  992. if ($doesntrun{$pidfile}) {
  993. return (0,0);
  994. }
  995. my $pid = checkserver($pidfile);
  996. if($pid > 0) {
  997. stopserver($pid);
  998. }
  999. unlink($pidfile);
  1000. my $flag=$verbose?'-v ':'';
  1001. $flag .= '-d ' if($debugprotocol);
  1002. my $cmd="$perl $srcdir/sshserver.pl ${flag}-u $USER -l $ip -p $port -s $socksport";
  1003. my ($sshpid, $pid2) = startnew($cmd, $pidfile, 60, 0);
  1004. # on loaded systems sshserver start up can take longer than the timeout
  1005. # passed to startnew, when this happens startnew completes without being
  1006. # able to read the pidfile and consequently returns a zero pid2 above.
  1007. if($sshpid <= 0 || !kill(0, $sshpid)) {
  1008. # it is NOT alive
  1009. logmsg "RUN: failed to start the SSH server\n";
  1010. stopserver("$pid2");
  1011. $doesntrun{$pidfile} = 1;
  1012. return (0,0);
  1013. }
  1014. # ssh server verification allows some extra time for the server to start up
  1015. # and gives us the opportunity of recovering the pid from the pidfile, when
  1016. # this verification succeeds the recovered pid is assigned to pid2.
  1017. my $pid3 = verifyserver("ssh",$ip,$port);
  1018. if(!$pid3) {
  1019. logmsg "RUN: SSH server failed verification\n";
  1020. # failed to fetch server pid. Kill the server and return failure
  1021. stopserver("$sshpid $pid2");
  1022. $doesntrun{$pidfile} = 1;
  1023. return (0,0);
  1024. }
  1025. $pid2 = $pid3;
  1026. # once it is known that the ssh server is alive, sftp server verification
  1027. # is performed actually connecting to it, authenticating and performing a
  1028. # very simple remote command. This verification is tried only one time.
  1029. if(verifysftp("sftp",$ip,$port) < 1) {
  1030. logmsg "RUN: SFTP server failed verification\n";
  1031. # failed to talk to it properly. Kill the server and return failure
  1032. display_sftplog();
  1033. display_sftpconfig();
  1034. display_sshdlog();
  1035. display_sshdconfig();
  1036. stopserver("$sshpid $pid2");
  1037. $doesntrun{$pidfile} = 1;
  1038. return (0,0);
  1039. }
  1040. if($verbose) {
  1041. logmsg "RUN: SSH server is now running PID $pid2\n";
  1042. }
  1043. return ($pid2, $sshpid);
  1044. }
  1045. #######################################################################
  1046. # Start the socks server
  1047. #
  1048. sub runsocksserver {
  1049. my ($id, $verbose, $ipv6) = @_;
  1050. my $ip=$HOSTIP;
  1051. my $port = $SOCKSPORT;
  1052. my $pidfile = $SOCKSPIDFILE;
  1053. # don't retry if the server doesn't work
  1054. if ($doesntrun{$pidfile}) {
  1055. return (0,0);
  1056. }
  1057. my $pid = checkserver($pidfile);
  1058. if($pid > 0) {
  1059. stopserver($pid);
  1060. }
  1061. unlink($pidfile);
  1062. # The ssh server must be already running
  1063. if(!$run{'ssh'}) {
  1064. logmsg "RUN: SOCKS server cannot find running SSH server\n";
  1065. $doesntrun{$pidfile} = 1;
  1066. return (0,0);
  1067. }
  1068. # Find out ssh daemon canonical file name
  1069. my $sshd = find_sshd();
  1070. if(!$sshd) {
  1071. logmsg "RUN: SOCKS server cannot find $sshdexe\n";
  1072. $doesntrun{$pidfile} = 1;
  1073. return (0,0);
  1074. }
  1075. # Find out ssh daemon version info
  1076. ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd);
  1077. if(!$sshdid) {
  1078. # Not an OpenSSH or SunSSH ssh daemon
  1079. logmsg "$sshderror\n" if($verbose);
  1080. logmsg "SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later\n";
  1081. $doesntrun{$pidfile} = 1;
  1082. return (0,0);
  1083. }
  1084. logmsg "ssh server found $sshd is $sshdverstr\n" if($verbose);
  1085. # Find out ssh client canonical file name
  1086. my $ssh = find_ssh();
  1087. if(!$ssh) {
  1088. logmsg "RUN: SOCKS server cannot find $sshexe\n";
  1089. $doesntrun{$pidfile} = 1;
  1090. return (0,0);
  1091. }
  1092. # Find out ssh client version info
  1093. my ($sshid, $sshvernum, $sshverstr, $ssherror) = sshversioninfo($ssh);
  1094. if(!$sshid) {
  1095. # Not an OpenSSH or SunSSH ssh client
  1096. logmsg "$ssherror\n" if($verbose);
  1097. logmsg "SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later\n";
  1098. $doesntrun{$pidfile} = 1;
  1099. return (0,0);
  1100. }
  1101. # Verify minimum ssh client version
  1102. if((($sshid =~ /OpenSSH/) && ($sshvernum < 299)) ||
  1103. (($sshid =~ /SunSSH/) && ($sshvernum < 100))) {
  1104. logmsg "ssh client found $ssh is $sshverstr\n";
  1105. logmsg "SCP, SFTP and SOCKS tests require OpenSSH 2.9.9 or later\n";
  1106. $doesntrun{$pidfile} = 1;
  1107. return (0,0);
  1108. }
  1109. logmsg "ssh client found $ssh is $sshverstr\n" if($verbose);
  1110. # Verify if ssh client and ssh daemon versions match
  1111. if(($sshdid ne $sshid) || ($sshdvernum != $sshvernum)) {
  1112. # Our test harness might work with slightly mismatched versions
  1113. logmsg "Warning: version mismatch: sshd $sshdverstr - ssh $sshverstr\n"
  1114. if($verbose);
  1115. }
  1116. # Config file options for ssh client are previously set from sshserver.pl
  1117. if(! -e $sshconfig) {
  1118. logmsg "RUN: SOCKS server cannot find $sshconfig\n";
  1119. $doesntrun{$pidfile} = 1;
  1120. return (0,0);
  1121. }
  1122. # start our socks server
  1123. my $cmd="$ssh -N -F $sshconfig $ip > $sshlog 2>&1";
  1124. my ($sshpid, $pid2) = startnew($cmd, $pidfile, 30, 1);
  1125. if($sshpid <= 0 || !kill(0, $sshpid)) {
  1126. # it is NOT alive
  1127. logmsg "RUN: failed to start the SOCKS server\n";
  1128. display_sshlog();
  1129. display_sshconfig();
  1130. display_sshdlog();
  1131. display_sshdconfig();
  1132. stopserver("$pid2");
  1133. $doesntrun{$pidfile} = 1;
  1134. return (0,0);
  1135. }
  1136. # Ugly hack but ssh doesn't support pid files
  1137. my $pid3 = verifyserver("socks",$ip,$port);
  1138. if(!$pid3) {
  1139. logmsg "RUN: SOCKS server failed verification\n";
  1140. # failed to talk to it properly. Kill the server and return failure
  1141. stopserver("$sshpid $pid2");
  1142. $doesntrun{$pidfile} = 1;
  1143. return (0,0);
  1144. }
  1145. $pid2 = $pid3;
  1146. if($verbose) {
  1147. logmsg "RUN: SOCKS server is now running PID $pid2\n";
  1148. }
  1149. return ($pid2, $sshpid);
  1150. }
  1151. #######################################################################
  1152. # Remove all files in the specified directory
  1153. #
  1154. sub cleardir {
  1155. my $dir = $_[0];
  1156. my $count;
  1157. my $file;
  1158. # Get all files
  1159. opendir(DIR, $dir) ||
  1160. return 0; # can't open dir
  1161. while($file = readdir(DIR)) {
  1162. if($file !~ /^\./) {
  1163. unlink("$dir/$file");
  1164. $count++;
  1165. }
  1166. }
  1167. closedir DIR;
  1168. return $count;
  1169. }
  1170. #######################################################################
  1171. # filter out the specified pattern from the given input file and store the
  1172. # results in the given output file
  1173. #
  1174. sub filteroff {
  1175. my $infile=$_[0];
  1176. my $filter=$_[1];
  1177. my $ofile=$_[2];
  1178. open(IN, "<$infile")
  1179. || return 1;
  1180. open(OUT, ">$ofile")
  1181. || return 1;
  1182. # logmsg "FILTER: off $filter from $infile to $ofile\n";
  1183. while(<IN>) {
  1184. $_ =~ s/$filter//;
  1185. print OUT $_;
  1186. }
  1187. close(IN);
  1188. close(OUT);
  1189. return 0;
  1190. }
  1191. #######################################################################
  1192. # compare test results with the expected output, we might filter off
  1193. # some pattern that is allowed to differ, output test results
  1194. #
  1195. sub compare {
  1196. # filter off patterns _before_ this comparison!
  1197. my ($subject, $firstref, $secondref)=@_;
  1198. my $result = compareparts($firstref, $secondref);
  1199. if($result) {
  1200. if(!$short) {
  1201. logmsg "\n $subject FAILED:\n";
  1202. logmsg showdiff($LOGDIR, $firstref, $secondref);
  1203. }
  1204. else {
  1205. logmsg "FAILED\n";
  1206. }
  1207. }
  1208. return $result;
  1209. }
  1210. #######################################################################
  1211. # display information about curl and the host the test suite runs on
  1212. #
  1213. sub checksystem {
  1214. unlink($memdump); # remove this if there was one left
  1215. my $feat;
  1216. my $curl;
  1217. my $libcurl;
  1218. my $versretval;
  1219. my $versnoexec;
  1220. my @version=();
  1221. my $curlverout="$LOGDIR/curlverout.log";
  1222. my $curlvererr="$LOGDIR/curlvererr.log";
  1223. my $versioncmd="$CURL --version 1>$curlverout 2>$curlvererr";
  1224. unlink($curlverout);
  1225. unlink($curlvererr);
  1226. $versretval = runclient($versioncmd);
  1227. $versnoexec = $!;
  1228. open(VERSOUT, "<$curlverout");
  1229. @version = <VERSOUT>;
  1230. close(VERSOUT);
  1231. for(@version) {
  1232. chomp;
  1233. if($_ =~ /^curl/) {
  1234. $curl = $_;
  1235. $curl =~ s/^(.*)(libcurl.*)/$1/g;
  1236. $libcurl = $2;
  1237. if($curl =~ /mingw32/) {
  1238. # This is a windows minw32 build, we need to translate the
  1239. # given path to the "actual" windows path.
  1240. my @m = `mount`;
  1241. my $matchlen;
  1242. my $bestmatch;
  1243. my $mount;
  1244. # example mount output:
  1245. # C:\DOCUME~1\Temp on /tmp type user (binmode,noumount)
  1246. # c:\ActiveState\perl on /perl type user (binmode)
  1247. # C:\msys\1.0\bin on /usr/bin type user (binmode,cygexec,noumount)
  1248. # C:\msys\1.0\bin on /bin type user (binmode,cygexec,noumount)
  1249. foreach $mount (@m) {
  1250. if( $mount =~ /(.*) on ([^ ]*) type /) {
  1251. my ($mingw, $real)=($2, $1);
  1252. if($pwd =~ /^$mingw/) {
  1253. # the path we got from pwd starts with the path
  1254. # we found on this line in the mount output
  1255. my $len = length($real);
  1256. if($len > $matchlen) {
  1257. # we remember the match that is the longest
  1258. $matchlen = $len;
  1259. $bestmatch = $real;
  1260. }
  1261. }
  1262. }
  1263. }
  1264. if(!$matchlen) {
  1265. logmsg "Serious error, can't find our \"real\" path\n";
  1266. }
  1267. else {
  1268. # now prepend the prefix from the mount command to build
  1269. # our "actual path"
  1270. $pwd = "$bestmatch$pwd";
  1271. }
  1272. $pwd =~ s#\\#/#g;
  1273. }
  1274. elsif ($curl =~ /win32/) {
  1275. # Native Windows builds don't understand the
  1276. # output of cygwin's pwd. It will be
  1277. # something like /cygdrive/c/<some path>.
  1278. #
  1279. # Use the cygpath utility to convert the
  1280. # working directory to a Windows friendly
  1281. # path. The -m option converts to use drive
  1282. # letter:, but it uses / instead \. Forward
  1283. # slashes (/) are easier for us. We don't
  1284. # have to escape them to get them to curl
  1285. # through a shell.
  1286. chomp($pwd = `cygpath -m $pwd`);
  1287. }
  1288. elsif ($libcurl =~ /openssl/i) {
  1289. $has_openssl=1;
  1290. $ssllib="OpenSSL";
  1291. }
  1292. elsif ($libcurl =~ /gnutls/i) {
  1293. $has_gnutls=1;
  1294. $ssllib="GnuTLS";
  1295. }
  1296. elsif ($libcurl =~ /nss/i) {
  1297. $has_nss=1;
  1298. $ssllib="NSS";
  1299. }
  1300. elsif ($libcurl =~ /yassl/i) {
  1301. $has_yassl=1;
  1302. $has_openssl=1;
  1303. $ssllib="yassl";
  1304. }
  1305. }
  1306. elsif($_ =~ /^Protocols: (.*)/i) {
  1307. # these are the protocols compiled in to this libcurl
  1308. @protocols = split(' ', $1);
  1309. # Generate a "proto-ipv6" version of each protocol to match the
  1310. # IPv6 <server> name. This works even if IPv6 support isn't
  1311. # compiled in because the <features> test will fail.
  1312. push @protocols, map($_ . "-ipv6", @protocols);
  1313. # 'none' is used in test cases to mean no server
  1314. push @protocols, ('none');
  1315. }
  1316. elsif($_ =~ /^Features: (.*)/i) {
  1317. $feat = $1;
  1318. if($feat =~ /debug/i) {
  1319. # debug is a listed "feature", use that knowledge
  1320. $curl_debug = 1;
  1321. # set the NETRC debug env
  1322. $ENV{'CURL_DEBUG_NETRC'} = "$LOGDIR/netrc";
  1323. }
  1324. if($feat =~ /SSL/i) {
  1325. # ssl enabled
  1326. $ssl_version=1;
  1327. }
  1328. if($feat =~ /Largefile/i) {
  1329. # large file support
  1330. $large_file=1;
  1331. }
  1332. if($feat =~ /IDN/i) {
  1333. # IDN support
  1334. $has_idn=1;
  1335. }
  1336. if($feat =~ /IPv6/i) {
  1337. $has_ipv6 = 1;
  1338. }
  1339. if($feat =~ /libz/i) {
  1340. $has_libz = 1;
  1341. }
  1342. if($feat =~ /NTLM/i) {
  1343. # NTLM enabled
  1344. $has_ntlm=1;
  1345. }
  1346. }
  1347. }
  1348. if(!$curl) {
  1349. logmsg "unable to get curl's version, further details are:\n";
  1350. logmsg "issued command: \n";
  1351. logmsg "$versioncmd \n";
  1352. if ($versretval == -1) {
  1353. logmsg "command failed with: \n";
  1354. logmsg "$versnoexec \n";
  1355. }
  1356. elsif ($versretval & 127) {
  1357. logmsg sprintf("command died with signal %d, and %s coredump.\n",
  1358. ($versretval & 127), ($versretval & 128)?"a":"no");
  1359. }
  1360. else {
  1361. logmsg sprintf("command exited with value %d \n", $versretval >> 8);
  1362. }
  1363. logmsg "contents of $curlverout: \n";
  1364. displaylogcontent("$curlverout");
  1365. logmsg "contents of $curlvererr: \n";
  1366. displaylogcontent("$curlvererr");
  1367. die "couldn't get curl's version";
  1368. }
  1369. if(-r "../lib/config.h") {
  1370. open(CONF, "<../lib/config.h");
  1371. while(<CONF>) {
  1372. if($_ =~ /^\#define HAVE_GETRLIMIT/) {
  1373. $has_getrlimit = 1;
  1374. }
  1375. }
  1376. close(CONF);
  1377. }
  1378. if($has_ipv6) {
  1379. # client has ipv6 support
  1380. # check if the HTTP server has it!
  1381. my @sws = `server/sws --version`;
  1382. if($sws[0] =~ /IPv6/) {
  1383. # HTTP server has ipv6 support!
  1384. $http_ipv6 = 1;
  1385. }
  1386. # check if the FTP server has it!
  1387. @sws = `server/sockfilt --version`;
  1388. if($sws[0] =~ /IPv6/) {
  1389. # FTP server has ipv6 support!
  1390. $ftp_ipv6 = 1;
  1391. }
  1392. }
  1393. if(!$curl_debug && $torture) {
  1394. die "can't run torture tests since curl was not build with debug";
  1395. }
  1396. # curl doesn't list cryptographic support separately, so assume it's
  1397. # always available
  1398. $has_crypto=1;
  1399. my $hostname=join(' ', runclientoutput("hostname"));
  1400. my $hosttype=join(' ', runclientoutput("uname -a"));
  1401. logmsg ("********* System characteristics ******** \n",
  1402. "* $curl\n",
  1403. "* $libcurl\n",
  1404. "* Features: $feat\n",
  1405. "* Host: $hostname",
  1406. "* System: $hosttype");
  1407. logmsg sprintf("* Server SSL: %s\n", $stunnel?"ON":"OFF");
  1408. logmsg sprintf("* libcurl SSL: %s\n", $ssl_version?"ON":"OFF");
  1409. logmsg sprintf("* libcurl debug: %s\n", $curl_debug?"ON":"OFF");
  1410. logmsg sprintf("* valgrind: %s\n", $valgrind?"ON":"OFF");
  1411. logmsg sprintf("* HTTP IPv6 %s\n", $http_ipv6?"ON":"OFF");
  1412. logmsg sprintf("* FTP IPv6 %s\n", $ftp_ipv6?"ON":"OFF");
  1413. logmsg sprintf("* HTTP port: %d\n", $HTTPPORT);
  1414. logmsg sprintf("* FTP port: %d\n", $FTPPORT);
  1415. logmsg sprintf("* FTP port 2: %d\n", $FTP2PORT);
  1416. if($stunnel) {
  1417. logmsg sprintf("* FTPS port: %d\n", $FTPSPORT);
  1418. logmsg sprintf("* HTTPS port: %d\n", $HTTPSPORT);
  1419. }
  1420. if($http_ipv6) {
  1421. logmsg sprintf("* HTTP IPv6 port: %d\n", $HTTP6PORT);
  1422. }
  1423. if($ftp_ipv6) {
  1424. logmsg sprintf("* FTP IPv6 port: %d\n", $FTP6PORT);
  1425. }
  1426. logmsg sprintf("* TFTP port: %d\n", $TFTPPORT);
  1427. if($tftp_ipv6) {
  1428. logmsg sprintf("* TFTP IPv6 port: %d\n", $TFTP6PORT);
  1429. }
  1430. logmsg sprintf("* SCP/SFTP port: %d\n", $SSHPORT);
  1431. logmsg sprintf("* SOCKS port: %d\n", $SOCKSPORT);
  1432. if($ssl_version) {
  1433. logmsg sprintf("* SSL library: %s\n", $ssllib);
  1434. }
  1435. $has_textaware = ($^O eq 'MSWin32') || ($^O eq 'msys');
  1436. logmsg sprintf("* Libtool lib: %s\n", $libtool?"ON":"OFF");
  1437. logmsg "***************************************** \n";
  1438. }
  1439. #######################################################################
  1440. # substitute the variable stuff into either a joined up file or
  1441. # a command, in either case passed by reference
  1442. #
  1443. sub subVariables {
  1444. my ($thing) = @_;
  1445. $$thing =~ s/%HOSTIP/$HOSTIP/g;
  1446. $$thing =~ s/%HTTPPORT/$HTTPPORT/g;
  1447. $$thing =~ s/%HOST6IP/$HOST6IP/g;
  1448. $$thing =~ s/%HTTP6PORT/$HTTP6PORT/g;
  1449. $$thing =~ s/%HTTPSPORT/$HTTPSPORT/g;
  1450. $$thing =~ s/%FTPPORT/$FTPPORT/g;
  1451. $$thing =~ s/%FTP6PORT/$FTP6PORT/g;
  1452. $$thing =~ s/%FTP2PORT/$FTP2PORT/g;
  1453. $$thing =~ s/%FTPSPORT/$FTPSPORT/g;
  1454. $$thing =~ s/%SRCDIR/$srcdir/g;
  1455. $$thing =~ s/%PWD/$pwd/g;
  1456. $$thing =~ s/%TFTPPORT/$TFTPPORT/g;
  1457. $$thing =~ s/%TFTP6PORT/$TFTP6PORT/g;
  1458. $$thing =~ s/%SSHPORT/$SSHPORT/g;
  1459. $$thing =~ s/%SOCKSPORT/$SOCKSPORT/g;
  1460. $$thing =~ s/%CURL/$CURL/g;
  1461. $$thing =~ s/%USER/$USER/g;
  1462. $$thing =~ s/%CLIENTIP/$CLIENTIP/g;
  1463. $$thing =~ s/%CLIENT6IP/$CLIENT6IP/g;
  1464. # The purpose of FTPTIME2 and FTPTIME3 is to provide times that can be
  1465. # used for time-out tests and that whould work on most hosts as these
  1466. # adjust for the startup/check time for this particular host. We needed
  1467. # to do this to make the test suite run better on very slow hosts.
  1468. my $ftp2 = $ftpchecktime * 2;
  1469. my $ftp3 = $ftpchecktime * 3;
  1470. $$thing =~ s/%FTPTIME2/$ftp2/g;
  1471. $$thing =~ s/%FTPTIME3/$ftp3/g;
  1472. }
  1473. sub fixarray {
  1474. my @in = @_;
  1475. for(@in) {
  1476. subVariables \$_;
  1477. }
  1478. return @in;
  1479. }
  1480. #######################################################################
  1481. # Run a single specified test case
  1482. #
  1483. sub singletest {
  1484. my ($testnum, $count, $total)=@_;
  1485. my @what;
  1486. my $why;
  1487. my %feature;
  1488. my $cmd;
  1489. # load the test case file definition
  1490. if(loadtest("${TESTDIR}/test${testnum}")) {
  1491. if($verbose) {
  1492. # this is not a test
  1493. logmsg "RUN: $testnum doesn't look like a test case\n";
  1494. }
  1495. $why = "no test";
  1496. }
  1497. else {
  1498. @what = getpart("client", "features");
  1499. }
  1500. for(@what) {
  1501. my $f = $_;
  1502. $f =~ s/\s//g;
  1503. $feature{$f}=$f; # we require this feature
  1504. if($f eq "SSL") {
  1505. if($ssl_version) {
  1506. next;
  1507. }
  1508. }
  1509. elsif($f eq "OpenSSL") {
  1510. if($has_openssl) {
  1511. next;
  1512. }
  1513. }
  1514. elsif($f eq "GnuTLS") {
  1515. if($has_gnutls) {
  1516. next;
  1517. }
  1518. }
  1519. elsif($f eq "NSS") {
  1520. if($has_nss) {
  1521. next;
  1522. }
  1523. }
  1524. elsif($f eq "netrc_debug") {
  1525. if($curl_debug) {
  1526. next;
  1527. }
  1528. }
  1529. elsif($f eq "large_file") {
  1530. if($large_file) {
  1531. next;
  1532. }
  1533. }
  1534. elsif($f eq "idn") {
  1535. if($has_idn) {
  1536. next;
  1537. }
  1538. }
  1539. elsif($f eq "ipv6") {
  1540. if($has_ipv6) {
  1541. next;
  1542. }
  1543. }
  1544. elsif($f eq "libz") {
  1545. if($has_libz) {
  1546. next;
  1547. }
  1548. }
  1549. elsif($f eq "NTLM") {
  1550. if($has_ntlm) {
  1551. next;
  1552. }
  1553. }
  1554. elsif($f eq "getrlimit") {
  1555. if($has_getrlimit) {
  1556. next;
  1557. }
  1558. }
  1559. elsif($f eq "crypto") {
  1560. if($has_crypto) {
  1561. next;
  1562. }
  1563. }
  1564. elsif($f eq "socks") {
  1565. next;
  1566. }
  1567. # See if this "feature" is in the list of supported protocols
  1568. elsif (grep /^$f$/, @protocols) {
  1569. next;
  1570. }
  1571. $why = "curl lacks $f support";
  1572. last;
  1573. }
  1574. if(!$why) {
  1575. my @keywords = getpart("info", "keywords");
  1576. my $match;
  1577. my $k;
  1578. for $k (@keywords) {
  1579. chomp $k;
  1580. if ($disabled_keywords{$k}) {
  1581. $why = "disabled by keyword";
  1582. } elsif ($enabled_keywords{$k}) {
  1583. $match = 1;
  1584. }
  1585. }
  1586. if(!$why && !$match && %enabled_keywords) {
  1587. $why = "disabled by missing keyword";
  1588. }
  1589. }
  1590. if(!$why) {
  1591. $why = serverfortest($testnum);
  1592. }
  1593. if(!$why) {
  1594. my @precheck = getpart("client", "precheck");
  1595. $cmd = $precheck[0];
  1596. chomp $cmd;
  1597. subVariables \$cmd;
  1598. if($cmd) {
  1599. my @o = `$cmd 2>/dev/null`;
  1600. if($o[0]) {
  1601. $why = $o[0];
  1602. chomp $why;
  1603. }
  1604. logmsg "prechecked $cmd\n" if($verbose);
  1605. }
  1606. }
  1607. if($why && !$listonly) {
  1608. # there's a problem, count it as "skipped"
  1609. $skipped++;
  1610. $skipped{$why}++;
  1611. $teststat[$testnum]=$why; # store reason for this test case
  1612. if(!$short) {
  1613. printf "test %03d SKIPPED: $why\n", $testnum;
  1614. }
  1615. return -1;
  1616. }
  1617. logmsg sprintf("test %03d...", $testnum);
  1618. # extract the reply data
  1619. my @reply = getpart("reply", "data");
  1620. my @replycheck = getpart("reply", "datacheck");
  1621. if (@replycheck) {
  1622. # we use this file instead to check the final output against
  1623. my %hash = getpartattr("reply", "datacheck");
  1624. if($hash{'nonewline'}) {
  1625. # Yes, we must cut off the final newline from the final line
  1626. # of the datacheck
  1627. chomp($replycheck[$#replycheck]);
  1628. }
  1629. @reply=@replycheck;
  1630. }
  1631. # curl command to run
  1632. my @curlcmd= fixarray ( getpart("client", "command") );
  1633. # this is the valid protocol blurb curl should generate
  1634. my @protocol= fixarray ( getpart("verify", "protocol") );
  1635. # redirected stdout/stderr to these files
  1636. $STDOUT="$LOGDIR/stdout$testnum";
  1637. $STDERR="$LOGDIR/stderr$testnum";
  1638. # if this section exists, we verify that the stdout contained this:
  1639. my @validstdout = fixarray ( getpart("verify", "stdout") );
  1640. # if this section exists, we verify upload
  1641. my @upload = getpart("verify", "upload");
  1642. # if this section exists, it might be FTP server instructions:
  1643. my @ftpservercmd = getpart("reply", "servercmd");
  1644. my $CURLOUT="$LOGDIR/curl$testnum.out"; # curl output if not stdout
  1645. # name of the test
  1646. my @testname= getpart("client", "name");
  1647. if(!$short) {
  1648. my $name = $testname[0];
  1649. $name =~ s/\n//g;
  1650. logmsg "[$name]\n";
  1651. }
  1652. if($listonly) {
  1653. return 0; # look successful
  1654. }
  1655. my @codepieces = getpart("client", "tool");
  1656. my $tool="";
  1657. if(@codepieces) {
  1658. $tool = $codepieces[0];
  1659. chomp $tool;
  1660. }
  1661. # remove server output logfiles
  1662. unlink($SERVERIN);
  1663. unlink($SERVER2IN);
  1664. if(@ftpservercmd) {
  1665. # write the instructions to file
  1666. writearray($FTPDCMD, \@ftpservercmd);
  1667. }
  1668. my (@setenv)= getpart("client", "setenv");
  1669. my @envs;
  1670. my $s;
  1671. for $s (@setenv) {
  1672. chomp $s; # cut off the newline
  1673. subVariables \$s;
  1674. if($s =~ /([^=]*)=(.*)/) {
  1675. my ($var, $content)=($1, $2);
  1676. $ENV{$var}=$content;
  1677. # remember which, so that we can clear them afterwards!
  1678. push @envs, $var;
  1679. }
  1680. }
  1681. # get the command line options to use
  1682. my @blaha;
  1683. ($cmd, @blaha)= getpart("client", "command");
  1684. # make some nice replace operations
  1685. $cmd =~ s/\n//g; # no newlines please
  1686. # substitute variables in the command line
  1687. subVariables \$cmd;
  1688. if($curl_debug) {
  1689. unlink($memdump);
  1690. }
  1691. # create a (possibly-empty) file before starting the test
  1692. my @inputfile=getpart("client", "file");
  1693. my %fileattr = getpartattr("client", "file");
  1694. my $filename=$fileattr{'name'};
  1695. if(@inputfile || $filename) {
  1696. if(!$filename) {
  1697. logmsg "ERROR: section client=>file has no name attribute\n";
  1698. return -1;
  1699. }
  1700. my $fileContent = join('', @inputfile);
  1701. subVariables \$fileContent;
  1702. # logmsg "DEBUG: writing file " . $filename . "\n";
  1703. open(OUTFILE, ">$filename");
  1704. binmode OUTFILE; # for crapage systems, use binary
  1705. print OUTFILE $fileContent;
  1706. close(OUTFILE);
  1707. }
  1708. my %cmdhash = getpartattr("client", "command");
  1709. my $out="";
  1710. if($cmdhash{'option'} !~ /no-output/) {
  1711. #We may slap on --output!
  1712. if (!@validstdout) {
  1713. $out=" --output $CURLOUT ";
  1714. }
  1715. }
  1716. my $serverlogslocktimeout = $defserverlogslocktimeout;
  1717. if($cmdhash{'timeout'}) {
  1718. # test is allowed to override default server logs lock timeout
  1719. if($cmdhash{'timeout'} =~ /(\d+)/) {
  1720. $serverlogslocktimeout = $1 if($1 >= 0);
  1721. }
  1722. }
  1723. my $postcommanddelay = $defpostcommanddelay;
  1724. if($cmdhash{'delay'}) {
  1725. # test is allowed to specify a delay after command is executed
  1726. if($cmdhash{'delay'} =~ /(\d+)/) {
  1727. $postcommanddelay = $1 if($1 > 0);
  1728. }
  1729. }
  1730. my $cmdargs;
  1731. if(!$tool) {
  1732. # run curl, add --verbose for debug information output
  1733. $cmdargs ="$out --include --verbose --trace-time $cmd";
  1734. }
  1735. else {
  1736. $cmdargs = " $cmd"; # $cmd is the command line for the test file
  1737. $CURLOUT = $STDOUT; # sends received data to stdout
  1738. }
  1739. my @stdintest = getpart("client", "stdin");
  1740. if(@stdintest) {
  1741. my $stdinfile="$LOGDIR/stdin-for-$testnum";
  1742. writearray($stdinfile, \@stdintest);
  1743. $cmdargs .= " <$stdinfile";
  1744. }
  1745. my $CMDLINE;
  1746. if(!$tool) {
  1747. $CMDLINE="$CURL";
  1748. }
  1749. else {
  1750. $CMDLINE="$LIBDIR/$tool";
  1751. if(! -f $CMDLINE) {
  1752. print "The tool set in the test case for this: '$tool' does not exist\n";
  1753. return -1;
  1754. }
  1755. $DBGCURL=$CMDLINE;
  1756. }
  1757. my $usevalgrind = $valgrind && ((getpart("verify", "valgrind"))[0] !~ /disable/);
  1758. if($usevalgrind) {
  1759. $CMDLINE = "$valgrind ".$valgrind_tool."--leak-check=yes --num-callers=16 ${valgrind_logfile}=$LOGDIR/valgrind$testnum $CMDLINE";
  1760. }
  1761. $CMDLINE .= "$cmdargs >>$STDOUT 2>>$STDERR";
  1762. if($verbose) {
  1763. logmsg "$CMDLINE\n";
  1764. }
  1765. print CMDLOG "$CMDLINE\n";
  1766. unlink("core");
  1767. my $dumped_core;
  1768. my $cmdres;
  1769. # Apr 2007: precommand isn't being used and could be removed
  1770. my @precommand= getpart("client", "precommand");
  1771. if($precommand[0]) {
  1772. # this is pure perl to eval!
  1773. my $code = join("", @precommand);
  1774. eval $code;
  1775. if($@) {
  1776. logmsg "perl: $code\n";
  1777. logmsg "precommand: $@";
  1778. stopservers($verbose);
  1779. return -1;
  1780. }
  1781. }
  1782. if($gdbthis) {
  1783. open(GDBCMD, ">$LOGDIR/gdbcmd");
  1784. print GDBCMD "set args $cmdargs\n";
  1785. print GDBCMD "show args\n";
  1786. close(GDBCMD);
  1787. }
  1788. # run the command line we built
  1789. if ($torture) {
  1790. $cmdres = torture($CMDLINE,
  1791. "$gdb --directory libtest $DBGCURL -x $LOGDIR/gdbcmd");
  1792. }
  1793. elsif($gdbthis) {
  1794. runclient("$gdb --directory libtest $DBGCURL -x $LOGDIR/gdbcmd");
  1795. $cmdres=0; # makes it always continue after a debugged run
  1796. }
  1797. else {
  1798. $cmdres = runclient("$CMDLINE");
  1799. my $signal_num = $cmdres & 127;
  1800. $dumped_core = $cmdres & 128;
  1801. if(!$anyway && ($signal_num || $dumped_core)) {
  1802. $cmdres = 1000;
  1803. }
  1804. else {
  1805. $cmdres /= 256;
  1806. }
  1807. }
  1808. if(!$dumped_core) {
  1809. if(-r "core") {
  1810. # there's core file present now!
  1811. $dumped_core = 1;
  1812. }
  1813. }
  1814. if($dumped_core) {
  1815. logmsg "core dumped\n";
  1816. if(0 && $gdb) {
  1817. logmsg "running gdb for post-mortem analysis:\n";
  1818. open(GDBCMD, ">$LOGDIR/gdbcmd2");
  1819. print GDBCMD "bt\n";
  1820. close(GDBCMD);
  1821. runclient("$gdb --directory libtest -x $LOGDIR/gdbcmd2 -batch $DBGCURL core ");
  1822. # unlink("$LOGDIR/gdbcmd2");
  1823. }
  1824. }
  1825. # If a server logs advisor read lock file exists, it is an indication
  1826. # that the server has not yet finished writing out all its log files,
  1827. # including server request log files used for protocol verification.
  1828. # So, if the lock file exists the script waits here a certain amount
  1829. # of time until the server removes it, or the given time expires.
  1830. if($serverlogslocktimeout) {
  1831. my $lockretry = $serverlogslocktimeout * 4;
  1832. while((-f $SERVERLOGS_LOCK) && $lockretry--) {
  1833. select(undef, undef, undef, 0.25);
  1834. }
  1835. if(($lockretry < 0) &&
  1836. ($serverlogslocktimeout >= $defserverlogslocktimeout)) {
  1837. logmsg "Warning: server logs lock timeout ",
  1838. "($serverlogslocktimeout seconds) expired\n";
  1839. }
  1840. }
  1841. # Test harness ssh server does not have this synchronization mechanism,
  1842. # this implies that some ssh server based tests might need a small delay
  1843. # once that the client command has run to avoid false test failures.
  1844. sleep($postcommanddelay) if($postcommanddelay);
  1845. # run the postcheck command
  1846. my @postcheck= getpart("client", "postcheck");
  1847. $cmd = $postcheck[0];
  1848. chomp $cmd;
  1849. subVariables \$cmd;
  1850. if($cmd) {
  1851. logmsg "postcheck $cmd\n" if($verbose);
  1852. my $rc = runclient("$cmd");
  1853. # Must run the postcheck command in torture mode in order
  1854. # to clean up, but the result can't be relied upon.
  1855. if($rc != 0 && !$torture) {
  1856. logmsg " postcheck FAILED\n";
  1857. return 1;
  1858. }
  1859. }
  1860. # remove the special FTP command file after each test!
  1861. unlink($FTPDCMD);
  1862. my $e;
  1863. for $e (@envs) {
  1864. $ENV{$e}=""; # clean up
  1865. }
  1866. # Skip all the verification on torture tests
  1867. if ($torture) {
  1868. if(!$cmdres && !$keepoutfiles) {
  1869. cleardir($LOGDIR);
  1870. }
  1871. return $cmdres;
  1872. }
  1873. my @err = getpart("verify", "errorcode");
  1874. my $errorcode = $err[0] || "0";
  1875. my $ok="";
  1876. my $res;
  1877. if (@validstdout) {
  1878. # verify redirected stdout
  1879. my @actual = loadarray($STDOUT);
  1880. # variable-replace in the stdout we have from the test case file
  1881. @validstdout = fixarray(@validstdout);
  1882. # get all attributes
  1883. my %hash = getpartattr("verify", "stdout");
  1884. # get the mode attribute
  1885. my $filemode=$hash{'mode'};
  1886. if(($filemode eq "text") && $has_textaware) {
  1887. # text mode when running on windows: fix line endings
  1888. map s/\r\n/\n/g, @actual;
  1889. }
  1890. if($hash{'nonewline'}) {
  1891. # Yes, we must cut off the final newline from the final line
  1892. # of the protocol data
  1893. chomp($validstdout[$#validstdout]);
  1894. }
  1895. $res = compare("stdout", \@actual, \@validstdout);
  1896. if($res) {
  1897. return 1;
  1898. }
  1899. $ok .= "s";
  1900. }
  1901. else {
  1902. $ok .= "-"; # stdout not checked
  1903. }
  1904. my %replyattr = getpartattr("reply", "data");
  1905. if(!$replyattr{'nocheck'} && (@reply || $replyattr{'sendzero'})) {
  1906. # verify the received data
  1907. my @out = loadarray($CURLOUT);
  1908. my %hash = getpartattr("reply", "data");
  1909. # get the mode attribute
  1910. my $filemode=$hash{'mode'};
  1911. if(($filemode eq "text") && $has_textaware) {
  1912. # text mode when running on windows: fix line endings
  1913. map s/\r\n/\n/g, @out;
  1914. }
  1915. $res = compare("data", \@out, \@reply);
  1916. if ($res) {
  1917. return 1;
  1918. }
  1919. $ok .= "d";
  1920. }
  1921. else {
  1922. $ok .= "-"; # data not checked
  1923. }
  1924. if(@upload) {
  1925. # verify uploaded data
  1926. my @out = loadarray("$LOGDIR/upload.$testnum");
  1927. $res = compare("upload", \@out, \@upload);
  1928. if ($res) {
  1929. return 1;
  1930. }
  1931. $ok .= "u";
  1932. }
  1933. else {
  1934. $ok .= "-"; # upload not checked
  1935. }
  1936. if(@protocol) {
  1937. # Verify the sent request
  1938. my @out = loadarray($SERVERIN);
  1939. # what to cut off from the live protocol sent by curl
  1940. my @strip = getpart("verify", "strip");
  1941. my @protstrip=@protocol;
  1942. # check if there's any attributes on the verify/protocol section
  1943. my %hash = getpartattr("verify", "protocol");
  1944. if($hash{'nonewline'}) {
  1945. # Yes, we must cut off the final newline from the final line
  1946. # of the protocol data
  1947. chomp($protstrip[$#protstrip]);
  1948. }
  1949. for(@strip) {
  1950. # strip off all lines that match the patterns from both arrays
  1951. chomp $_;
  1952. @out = striparray( $_, \@out);
  1953. @protstrip= striparray( $_, \@protstrip);
  1954. }
  1955. # what parts to cut off from the protocol
  1956. my @strippart = getpart("verify", "strippart");
  1957. my $strip;
  1958. for $strip (@strippart) {
  1959. chomp $strip;
  1960. for(@out) {
  1961. eval $strip;
  1962. }
  1963. }
  1964. $res = compare("protocol", \@out, \@protstrip);
  1965. if($res) {
  1966. return 1;
  1967. }
  1968. $ok .= "p";
  1969. }
  1970. else {
  1971. $ok .= "-"; # protocol not checked
  1972. }
  1973. my @outfile=getpart("verify", "file");
  1974. if(@outfile) {
  1975. # we're supposed to verify a dynamically generated file!
  1976. my %hash = getpartattr("verify", "file");
  1977. my $filename=$hash{'name'};
  1978. if(!$filename) {
  1979. logmsg "ERROR: section verify=>file has no name attribute\n";
  1980. stopservers($verbose);
  1981. return -1;
  1982. }
  1983. my @generated=loadarray($filename);
  1984. # what parts to cut off from the file
  1985. my @stripfile = getpart("verify", "stripfile");
  1986. my $filemode=$hash{'mode'};
  1987. if(($filemode eq "text") && $has_textaware) {
  1988. # text mode when running on windows means adding an extra
  1989. # strip expression
  1990. push @stripfile, "s/\r\n/\n/";
  1991. }
  1992. my $strip;
  1993. for $strip (@stripfile) {
  1994. chomp $strip;
  1995. for(@generated) {
  1996. eval $strip;
  1997. }
  1998. }
  1999. @outfile = fixarray(@outfile);
  2000. $res = compare("output", \@generated, \@outfile);
  2001. if($res) {
  2002. return 1;
  2003. }
  2004. $ok .= "o";
  2005. }
  2006. else {
  2007. $ok .= "-"; # output not checked
  2008. }
  2009. # accept multiple comma-separated error codes
  2010. my @splerr = split(/ *, */, $errorcode);
  2011. my $errok;
  2012. foreach $e (@splerr) {
  2013. if($e == $cmdres) {
  2014. # a fine error code
  2015. $errok = 1;
  2016. last;
  2017. }
  2018. }
  2019. if($errok) {
  2020. $ok .= "e";
  2021. }
  2022. else {
  2023. if(!$short) {
  2024. printf "\ncurl returned $cmdres, %d was expected\n", $errorcode;
  2025. }
  2026. logmsg " exit FAILED\n";
  2027. return 1;
  2028. }
  2029. @what = getpart("client", "killserver");
  2030. for(@what) {
  2031. my $serv = $_;
  2032. chomp $serv;
  2033. if($serv =~ /^ftp(\d*)(-ipv6|)/) {
  2034. my ($id, $ext) = ($1, $2);
  2035. #print STDERR "SERV $serv $id $ext\n";
  2036. ftpkillslave($id, $ext, $verbose);
  2037. }
  2038. if($run{$serv}) {
  2039. stopserver($run{$serv}); # the pid file is in the hash table
  2040. $run{$serv}=0; # clear pid
  2041. }
  2042. else {
  2043. logmsg "RUN: The $serv server is not running\n";
  2044. }
  2045. }
  2046. if($curl_debug) {
  2047. if(! -f $memdump) {
  2048. logmsg "\n** ALERT! memory debugging with no output file?\n";
  2049. }
  2050. else {
  2051. my @memdata=`$memanalyze $memdump`;
  2052. my $leak=0;
  2053. for(@memdata) {
  2054. if($_ ne "") {
  2055. # well it could be other memory problems as well, but
  2056. # we call it leak for short here
  2057. $leak=1;
  2058. }
  2059. }
  2060. if($leak) {
  2061. logmsg "\n** MEMORY FAILURE\n";
  2062. logmsg @memdata;
  2063. return 1;
  2064. }
  2065. else {
  2066. $ok .= "m";
  2067. }
  2068. }
  2069. }
  2070. else {
  2071. $ok .= "-"; # memory not checked
  2072. }
  2073. if($valgrind) {
  2074. # this is the valid protocol blurb curl should generate
  2075. if($usevalgrind) {
  2076. opendir(DIR, "log") ||
  2077. return 0; # can't open log dir
  2078. my @files = readdir(DIR);
  2079. closedir(DIR);
  2080. my $f;
  2081. my $l;
  2082. foreach $f (@files) {
  2083. if($f =~ /^valgrind$testnum\.pid/) {
  2084. $l = $f;
  2085. last;
  2086. }
  2087. }
  2088. my $src=$ENV{'srcdir'};
  2089. if(!$src) {
  2090. $src=".";
  2091. }
  2092. my @e = valgrindparse($src, $feature{'SSL'}, "$LOGDIR/$l");
  2093. if($e[0]) {
  2094. logmsg " valgrind ERROR ";
  2095. logmsg @e;
  2096. return 1;
  2097. }
  2098. $ok .= "v";
  2099. }
  2100. else {
  2101. if(!$short) {
  2102. logmsg " valgrind SKIPPED\n";
  2103. }
  2104. $ok .= "-"; # skipped
  2105. }
  2106. }
  2107. else {
  2108. $ok .= "-"; # valgrind not checked
  2109. }
  2110. logmsg "$ok " if(!$short);
  2111. my $sofar= time()-$start;
  2112. my $esttotal = $sofar/$count * $total;
  2113. my $estleft = $esttotal - $sofar;
  2114. my $left=sprintf("remaining: %02d:%02d",
  2115. $estleft/60,
  2116. $estleft%60);
  2117. printf "OK (%-3d out of %-3d, %s)\n", $count, $total, $left;
  2118. # the test succeeded, remove all log files
  2119. if(!$keepoutfiles) {
  2120. cleardir($LOGDIR);
  2121. }
  2122. unlink($FTPDCMD); # remove the instructions for this test
  2123. return 0;
  2124. }
  2125. #######################################################################
  2126. # Stop all running test servers
  2127. sub stopservers {
  2128. my ($verbose)=@_;
  2129. for(keys %run) {
  2130. my $server = $_;
  2131. my $pids=$run{$server};
  2132. my $pid;
  2133. my $prev;
  2134. foreach $pid (split(" ", $pids)) {
  2135. if($pid != $prev) {
  2136. # no need to kill same pid twice!
  2137. logmsg sprintf("* kill pid for %s => %d\n",
  2138. $server, $pid) if($verbose);
  2139. stopserver($pid);
  2140. }
  2141. $prev = $pid;
  2142. }
  2143. delete $run{$server};
  2144. }
  2145. ftpkillslaves($verbose);
  2146. }
  2147. #######################################################################
  2148. # startservers() starts all the named servers
  2149. #
  2150. # Returns: string with error reason or blank for success
  2151. sub startservers {
  2152. my @what = @_;
  2153. my ($pid, $pid2);
  2154. for(@what) {
  2155. my $what = lc($_);
  2156. $what =~ s/[^a-z0-9-]//g;
  2157. if($what eq "ftp") {
  2158. if(!$run{'ftp'}) {
  2159. ($pid, $pid2) = runftpserver("", $verbose);
  2160. if($pid <= 0) {
  2161. return "failed starting FTP server";
  2162. }
  2163. printf ("* pid ftp => %d %d\n", $pid, $pid2) if($verbose);
  2164. $run{'ftp'}="$pid $pid2";
  2165. }
  2166. }
  2167. elsif($what eq "ftp2") {
  2168. if(!$run{'ftp2'}) {
  2169. ($pid, $pid2) = runftpserver("2", $verbose);
  2170. if($pid <= 0) {
  2171. return "failed starting FTP2 server";
  2172. }
  2173. printf ("* pid ftp2 => %d %d\n", $pid, $pid2) if($verbose);
  2174. $run{'ftp2'}="$pid $pid2";
  2175. }
  2176. }
  2177. elsif($what eq "ftp-ipv6") {
  2178. if(!$run{'ftp-ipv6'}) {
  2179. ($pid, $pid2) = runftpserver("", $verbose, "ipv6");
  2180. if($pid <= 0) {
  2181. return "failed starting FTP-IPv6 server";
  2182. }
  2183. logmsg sprintf("* pid ftp-ipv6 => %d %d\n", $pid,
  2184. $pid2) if($verbose);
  2185. $run{'ftp-ipv6'}="$pid $pid2";
  2186. }
  2187. }
  2188. elsif($what eq "http") {
  2189. if(!$run{'http'}) {
  2190. ($pid, $pid2) = runhttpserver($verbose);
  2191. if($pid <= 0) {
  2192. return "failed starting HTTP server";
  2193. }
  2194. printf ("* pid http => %d %d\n", $pid, $pid2) if($verbose);
  2195. $run{'http'}="$pid $pid2";
  2196. }
  2197. }
  2198. elsif($what eq "http-ipv6") {
  2199. if(!$run{'http-ipv6'}) {
  2200. ($pid, $pid2) = runhttpserver($verbose, "IPv6");
  2201. if($pid <= 0) {
  2202. return "failed starting HTTP-IPv6 server";
  2203. }
  2204. logmsg sprintf("* pid http-ipv6 => %d %d\n", $pid, $pid2)
  2205. if($verbose);
  2206. $run{'http-ipv6'}="$pid $pid2";
  2207. }
  2208. }
  2209. elsif($what eq "ftps") {
  2210. if(!$stunnel) {
  2211. # we can't run ftps tests without stunnel
  2212. return "no stunnel";
  2213. }
  2214. if(!$ssl_version) {
  2215. # we can't run ftps tests if libcurl is SSL-less
  2216. return "curl lacks SSL support";
  2217. }
  2218. if(!$run{'ftp'}) {
  2219. ($pid, $pid2) = runftpserver("", $verbose);
  2220. if($pid <= 0) {
  2221. return "failed starting FTP server";
  2222. }
  2223. printf ("* pid ftp => %d %d\n", $pid, $pid2) if($verbose);
  2224. $run{'ftp'}="$pid $pid2";
  2225. }
  2226. if(!$run{'ftps'}) {
  2227. ($pid, $pid2) = runftpsserver($verbose);
  2228. if($pid <= 0) {
  2229. return "failed starting FTPS server (stunnel)";
  2230. }
  2231. logmsg sprintf("* pid ftps => %d %d\n", $pid, $pid2)
  2232. if($verbose);
  2233. $run{'ftps'}="$pid $pid2";
  2234. }
  2235. }
  2236. elsif($what eq "file") {
  2237. # we support it but have no server!
  2238. }
  2239. elsif($what eq "https") {
  2240. if(!$stunnel) {
  2241. # we can't run ftps tests without stunnel
  2242. return "no stunnel";
  2243. }
  2244. if(!$ssl_version) {
  2245. # we can't run ftps tests if libcurl is SSL-less
  2246. return "curl lacks SSL support";
  2247. }
  2248. if(!$run{'http'}) {
  2249. ($pid, $pid2) = runhttpserver($verbose);
  2250. if($pid <= 0) {
  2251. return "failed starting HTTP server";
  2252. }
  2253. printf ("* pid http => %d %d\n", $pid, $pid2) if($verbose);
  2254. $run{'http'}="$pid $pid2";
  2255. }
  2256. if(!$run{'https'}) {
  2257. ($pid, $pid2) = runhttpsserver($verbose);
  2258. if($pid <= 0) {
  2259. return "failed starting HTTPS server (stunnel)";
  2260. }
  2261. logmsg sprintf("* pid https => %d %d\n", $pid, $pid2)
  2262. if($verbose);
  2263. $run{'https'}="$pid $pid2";
  2264. }
  2265. }
  2266. elsif($what eq "tftp") {
  2267. if(!$run{'tftp'}) {
  2268. ($pid, $pid2) = runtftpserver("", $verbose);
  2269. if($pid <= 0) {
  2270. return "failed starting TFTP server";
  2271. }
  2272. printf ("* pid tftp => %d %d\n", $pid, $pid2) if($verbose);
  2273. $run{'tftp'}="$pid $pid2";
  2274. }
  2275. }
  2276. elsif($what eq "tftp-ipv6") {
  2277. if(!$run{'tftp-ipv6'}) {
  2278. ($pid, $pid2) = runtftpserver("", $verbose, "IPv6");
  2279. if($pid <= 0) {
  2280. return "failed starting TFTP-IPv6 server";
  2281. }
  2282. printf("* pid tftp-ipv6 => %d %d\n", $pid, $pid2) if($verbose);
  2283. $run{'tftp-ipv6'}="$pid $pid2";
  2284. }
  2285. }
  2286. elsif($what eq "sftp" || $what eq "scp" || $what eq "socks4" || $what eq "socks5" ) {
  2287. if(!$run{'ssh'}) {
  2288. ($pid, $pid2) = runsshserver("", $verbose);
  2289. if($pid <= 0) {
  2290. return "failed starting SSH server";
  2291. }
  2292. printf ("* pid ssh => %d %d\n", $pid, $pid2) if($verbose);
  2293. $run{'ssh'}="$pid $pid2";
  2294. }
  2295. if($what eq "socks4" || $what eq "socks5") {
  2296. if(!$run{'socks'}) {
  2297. ($pid, $pid2) = runsocksserver("", $verbose);
  2298. if($pid <= 0) {
  2299. return "failed starting socks server";
  2300. }
  2301. printf ("* pid socks => %d %d\n", $pid, $pid2) if($verbose);
  2302. $run{'socks'}="$pid $pid2";
  2303. }
  2304. }
  2305. if($what eq "socks5") {
  2306. if(!$sshdid) {
  2307. # Not an OpenSSH or SunSSH ssh daemon
  2308. logmsg "Not OpenSSH or SunSSH; socks5 tests need at least OpenSSH 3.7\n";
  2309. return "failed starting socks5 server";
  2310. }
  2311. elsif(($sshdid =~ /OpenSSH/) && ($sshdvernum < 370)) {
  2312. # Need OpenSSH 3.7 for socks5 - http://www.openssh.com/txt/release-3.7
  2313. logmsg "$sshdverstr insufficient; socks5 tests need at least OpenSSH 3.7\n";
  2314. return "failed starting socks5 server";
  2315. }
  2316. elsif(($sshdid =~ /SunSSH/) && ($sshdvernum < 100)) {
  2317. # Need SunSSH 1.0 for socks5
  2318. logmsg "$sshdverstr insufficient; socks5 tests need at least SunSSH 1.0\n";
  2319. return "failed starting socks5 server";
  2320. }
  2321. }
  2322. }
  2323. elsif($what eq "none") {
  2324. logmsg "* starts no server\n" if ($verbose);
  2325. }
  2326. else {
  2327. warn "we don't support a server for $what";
  2328. return "no server for $what";
  2329. }
  2330. }
  2331. return 0;
  2332. }
  2333. ##############################################################################
  2334. # This function makes sure the right set of server is running for the
  2335. # specified test case. This is a useful design when we run single tests as not
  2336. # all servers need to run then!
  2337. #
  2338. # Returns: a string, blank if everything is fine or a reason why it failed
  2339. #
  2340. sub serverfortest {
  2341. my ($testnum)=@_;
  2342. my @what = getpart("client", "server");
  2343. if(!$what[0]) {
  2344. warn "Test case $testnum has no server(s) specified";
  2345. return "no server specified";
  2346. }
  2347. for (@what) {
  2348. my $proto = lc($_);
  2349. chomp $proto;
  2350. if (! grep /^$proto$/, @protocols) {
  2351. if (substr($proto,0,5) ne "socks") {
  2352. return "curl lacks $proto support";
  2353. }
  2354. }
  2355. }
  2356. return &startservers(@what);
  2357. }
  2358. #######################################################################
  2359. # Check options to this test program
  2360. #
  2361. my $number=0;
  2362. my $fromnum=-1;
  2363. my @testthis;
  2364. my %disabled;
  2365. while(@ARGV) {
  2366. if ($ARGV[0] eq "-v") {
  2367. # verbose output
  2368. $verbose=1;
  2369. }
  2370. elsif($ARGV[0] =~ /^-b(.*)/) {
  2371. my $portno=$1;
  2372. if($portno =~ s/(\d+)$//) {
  2373. $base = int $1;
  2374. }
  2375. }
  2376. elsif ($ARGV[0] eq "-c") {
  2377. # use this path to curl instead of default
  2378. $DBGCURL=$CURL=$ARGV[1];
  2379. shift @ARGV;
  2380. }
  2381. elsif ($ARGV[0] eq "-d") {
  2382. # have the servers display protocol output
  2383. $debugprotocol=1;
  2384. }
  2385. elsif ($ARGV[0] eq "-f") {
  2386. # run fork-servers, which makes the server fork for all new
  2387. # connections This is NOT what you wanna do without knowing exactly
  2388. # why and for what
  2389. $forkserver=1;
  2390. }
  2391. elsif ($ARGV[0] eq "-g") {
  2392. # run this test with gdb
  2393. $gdbthis=1;
  2394. }
  2395. elsif($ARGV[0] eq "-s") {
  2396. # short output
  2397. $short=1;
  2398. }
  2399. elsif($ARGV[0] eq "-n") {
  2400. # no valgrind
  2401. undef $valgrind;
  2402. }
  2403. elsif($ARGV[0] =~ /^-t(.*)/) {
  2404. # torture
  2405. $torture=1;
  2406. my $xtra = $1;
  2407. if($xtra =~ s/(\d+)$//) {
  2408. $tortalloc = $1;
  2409. }
  2410. # we undef valgrind to make this fly in comparison
  2411. undef $valgrind;
  2412. }
  2413. elsif($ARGV[0] eq "-a") {
  2414. # continue anyway, even if a test fail
  2415. $anyway=1;
  2416. }
  2417. elsif($ARGV[0] eq "-p") {
  2418. $postmortem=1;
  2419. }
  2420. elsif($ARGV[0] eq "-l") {
  2421. # lists the test case names only
  2422. $listonly=1;
  2423. }
  2424. elsif($ARGV[0] eq "-k") {
  2425. # keep stdout and stderr files after tests
  2426. $keepoutfiles=1;
  2427. }
  2428. elsif($ARGV[0] eq "-h") {
  2429. # show help text
  2430. print <<EOHELP
  2431. Usage: runtests.pl [options] [test selection(s)]
  2432. -a continue even if a test fails
  2433. -bN use base port number N for test servers (default $base)
  2434. -c path use this curl executable
  2435. -d display server debug info
  2436. -g run the test case with gdb
  2437. -h this help text
  2438. -k keep stdout and stderr files present after tests
  2439. -l list all test case names/descriptions
  2440. -n no valgrind
  2441. -p print log file contents when a test fails
  2442. -s short output
  2443. -t[N] torture (simulate memory alloc failures); N means fail Nth alloc
  2444. -v verbose output
  2445. [num] like "5 6 9" or " 5 to 22 " to run those tests only
  2446. [!num] like "!5 !6 !9" to disable those tests
  2447. [keyword] like "IPv6" to select only tests containing the key word
  2448. [!keyword] like "!cookies" to disable any tests containing the key word
  2449. EOHELP
  2450. ;
  2451. exit;
  2452. }
  2453. elsif($ARGV[0] =~ /^(\d+)/) {
  2454. $number = $1;
  2455. if($fromnum >= 0) {
  2456. for($fromnum .. $number) {
  2457. push @testthis, $_;
  2458. }
  2459. $fromnum = -1;
  2460. }
  2461. else {
  2462. push @testthis, $1;
  2463. }
  2464. }
  2465. elsif($ARGV[0] =~ /^to$/i) {
  2466. $fromnum = $number+1;
  2467. }
  2468. elsif($ARGV[0] =~ /^!(\d+)/) {
  2469. $fromnum = -1;
  2470. $disabled{$1}=$1;
  2471. }
  2472. elsif($ARGV[0] =~ /^!(.+)/) {
  2473. $disabled_keywords{$1}=$1;
  2474. }
  2475. elsif($ARGV[0] =~ /^([-[{a-zA-Z].*)/) {
  2476. $enabled_keywords{$1}=$1;
  2477. }
  2478. else {
  2479. print "Unknown option: $ARGV[0]\n";
  2480. exit;
  2481. }
  2482. shift @ARGV;
  2483. }
  2484. if($testthis[0] ne "") {
  2485. $TESTCASES=join(" ", @testthis);
  2486. }
  2487. if($valgrind) {
  2488. # we have found valgrind on the host, use it
  2489. # verify that we can invoke it fine
  2490. my $code = runclient("valgrind >/dev/null 2>&1");
  2491. if(($code>>8) != 1) {
  2492. #logmsg "Valgrind failure, disable it\n";
  2493. undef $valgrind;
  2494. } else {
  2495. # since valgrind 2.1.x, '--tool' option is mandatory
  2496. # use it, if it is supported by the version installed on the system
  2497. runclient("valgrind --help 2>&1 | grep -- --tool > /dev/null 2>&1");
  2498. if (($? >> 8)==0) {
  2499. $valgrind_tool="--tool=memcheck ";
  2500. }
  2501. open(C, "<$CURL");
  2502. my $l = <C>;
  2503. if($l =~ /^\#\!/) {
  2504. # A shell script. This is typically when built with libtool,
  2505. $valgrind="../libtool --mode=execute $valgrind";
  2506. }
  2507. close(C);
  2508. # valgrind 3 renamed the --logfile option to --log-file!!!
  2509. my $ver=join(' ', runclientoutput("valgrind --version"));
  2510. # cut off all but digits and dots
  2511. $ver =~ s/[^0-9.]//g;
  2512. if($ver >= 3) {
  2513. $valgrind_logfile="--log-file";
  2514. }
  2515. }
  2516. }
  2517. if ($gdbthis) {
  2518. # open the executable curl and read the first 4 bytes of it
  2519. open(CHECK, "<$CURL");
  2520. my $c;
  2521. sysread CHECK, $c, 4;
  2522. close(CHECK);
  2523. if($c eq "#! /") {
  2524. # A shell script. This is typically when built with libtool,
  2525. $libtool = 1;
  2526. $gdb = "libtool --mode=execute gdb";
  2527. }
  2528. }
  2529. $HTTPPORT = $base + 0; # HTTP server port
  2530. $HTTPSPORT = $base + 1; # HTTPS server port
  2531. $FTPPORT = $base + 2; # FTP server port
  2532. $FTPSPORT = $base + 3; # FTPS server port
  2533. $HTTP6PORT = $base + 4; # HTTP IPv6 server port (different IP protocol
  2534. # but we follow the same port scheme anyway)
  2535. $FTP2PORT = $base + 5; # FTP server 2 port
  2536. $FTP6PORT = $base + 6; # FTP IPv6 port
  2537. $TFTPPORT = $base + 7; # TFTP (UDP) port
  2538. $TFTP6PORT = $base + 8; # TFTP IPv6 (UDP) port
  2539. $SSHPORT = $base + 9; # SSH (SCP/SFTP) port
  2540. $SOCKSPORT = $base + 10; # SOCKS port
  2541. #######################################################################
  2542. # clear and create logging directory:
  2543. #
  2544. cleardir($LOGDIR);
  2545. mkdir($LOGDIR, 0777);
  2546. #######################################################################
  2547. # Output curl version and host info being tested
  2548. #
  2549. if(!$listonly) {
  2550. checksystem();
  2551. }
  2552. #######################################################################
  2553. # If 'all' tests are requested, find out all test numbers
  2554. #
  2555. if ( $TESTCASES eq "all") {
  2556. # Get all commands and find out their test numbers
  2557. opendir(DIR, $TESTDIR) || die "can't opendir $TESTDIR: $!";
  2558. my @cmds = grep { /^test([0-9]+)$/ && -f "$TESTDIR/$_" } readdir(DIR);
  2559. closedir(DIR);
  2560. open(D, "<$TESTDIR/DISABLED");
  2561. while(<D>) {
  2562. if(/^ *\#/) {
  2563. # allow comments
  2564. next;
  2565. }
  2566. if($_ =~ /(\d+)/) {
  2567. $disabled{$1}=$1; # disable this test number
  2568. }
  2569. }
  2570. close(D);
  2571. $TESTCASES=""; # start with no test cases
  2572. # cut off everything but the digits
  2573. for(@cmds) {
  2574. $_ =~ s/[a-z\/\.]*//g;
  2575. }
  2576. # sort the numbers from low to high
  2577. foreach my $n (sort { $a <=> $b } @cmds) {
  2578. if($disabled{$n}) {
  2579. # skip disabled test cases
  2580. my $why = "configured as DISABLED";
  2581. $skipped++;
  2582. $skipped{$why}++;
  2583. $teststat[$n]=$why; # store reason for this test case
  2584. next;
  2585. }
  2586. $TESTCASES .= " $n";
  2587. }
  2588. }
  2589. #######################################################################
  2590. # Start the command line log
  2591. #
  2592. open(CMDLOG, ">$CURLLOG") ||
  2593. logmsg "can't log command lines to $CURLLOG\n";
  2594. #######################################################################
  2595. # Display the contents of the given file. Line endings are canonicalized
  2596. # and excessively long files are elided
  2597. sub displaylogcontent {
  2598. my ($file)=@_;
  2599. if(open(SINGLE, "<$file")) {
  2600. my $linecount = 0;
  2601. my $truncate;
  2602. my @tail;
  2603. while(my $string = <SINGLE>) {
  2604. $string =~ s/\r\n/\n/g;
  2605. $string =~ s/[\r\f\032]/\n/g;
  2606. $string .= "\n" unless ($string =~ /\n$/);
  2607. $string =~ tr/\n//;
  2608. for my $line (split("\n", $string)) {
  2609. $line =~ s/\s*\!$//;
  2610. if ($truncate) {
  2611. push @tail, " $line\n";
  2612. } else {
  2613. logmsg " $line\n";
  2614. }
  2615. $linecount++;
  2616. $truncate = $linecount > 1000;
  2617. }
  2618. }
  2619. if (@tail) {
  2620. logmsg "=== File too long: lines here were removed\n";
  2621. # This won't work properly if time stamps are enabled in logmsg
  2622. logmsg join('',@tail[$#tail-200..$#tail]);
  2623. }
  2624. close(SINGLE);
  2625. }
  2626. }
  2627. sub displaylogs {
  2628. my ($testnum)=@_;
  2629. opendir(DIR, "$LOGDIR") ||
  2630. die "can't open dir: $!";
  2631. my @logs = readdir(DIR);
  2632. closedir(DIR);
  2633. logmsg "== Contents of files in the $LOGDIR/ dir after test $testnum\n";
  2634. foreach my $log (sort @logs) {
  2635. if($log =~ /\.(\.|)$/) {
  2636. next; # skip "." and ".."
  2637. }
  2638. if($log =~ /^\.nfs/) {
  2639. next; # skip ".nfs"
  2640. }
  2641. if(($log eq "memdump") || ($log eq "core")) {
  2642. next; # skip "memdump" and "core"
  2643. }
  2644. if((-d "$LOGDIR/$log") || (! -s "$LOGDIR/$log")) {
  2645. next; # skip directory and empty files
  2646. }
  2647. if(($log =~ /^stdout\d+/) && ($log !~ /^stdout$testnum/)) {
  2648. next; # skip stdoutNnn of other tests
  2649. }
  2650. if(($log =~ /^stderr\d+/) && ($log !~ /^stderr$testnum/)) {
  2651. next; # skip stderrNnn of other tests
  2652. }
  2653. if(($log =~ /^upload\d+/) && ($log !~ /^upload$testnum/)) {
  2654. next; # skip uploadNnn of other tests
  2655. }
  2656. if(($log =~ /^curl\d+\.out/) && ($log !~ /^curl$testnum\.out/)) {
  2657. next; # skip curlNnn.out of other tests
  2658. }
  2659. if(($log =~ /^test\d+\.txt/) && ($log !~ /^test$testnum\.txt/)) {
  2660. next; # skip testNnn.txt of other tests
  2661. }
  2662. if(($log =~ /^file\d+\.txt/) && ($log !~ /^file$testnum\.txt/)) {
  2663. next; # skip fileNnn.txt of other tests
  2664. }
  2665. if(($log =~ /^valgrind\d+/) && ($log !~ /^valgrind$testnum/)) {
  2666. next; # skip valgrindNnn of other tests
  2667. }
  2668. logmsg "=== Start of file $log\n";
  2669. displaylogcontent("$LOGDIR/$log");
  2670. logmsg "=== End of file $log\n";
  2671. }
  2672. }
  2673. #######################################################################
  2674. # The main test-loop
  2675. #
  2676. my $failed;
  2677. my $testnum;
  2678. my $ok=0;
  2679. my $total=0;
  2680. my $lasttest;
  2681. my @at = split(" ", $TESTCASES);
  2682. my $count=0;
  2683. $start = time();
  2684. foreach $testnum (@at) {
  2685. $lasttest = $testnum if($testnum > $lasttest);
  2686. $count++;
  2687. my $error = singletest($testnum, $count, scalar(@at));
  2688. if($error < 0) {
  2689. # not a test we can run
  2690. next;
  2691. }
  2692. $total++; # number of tests we've run
  2693. if($error>0) {
  2694. $failed.= "$testnum ";
  2695. if($postmortem) {
  2696. # display all files in log/ in a nice way
  2697. displaylogs($testnum);
  2698. }
  2699. if(!$anyway) {
  2700. # a test failed, abort
  2701. logmsg "\n - abort tests\n";
  2702. last;
  2703. }
  2704. }
  2705. elsif(!$error) {
  2706. $ok++; # successful test counter
  2707. }
  2708. # loop for next test
  2709. }
  2710. #######################################################################
  2711. # Close command log
  2712. #
  2713. close(CMDLOG);
  2714. # Tests done, stop the servers
  2715. stopservers($verbose);
  2716. unlink($SOCKSPIDFILE);
  2717. my $all = $total + $skipped;
  2718. if($total) {
  2719. logmsg sprintf("TESTDONE: $ok tests out of $total reported OK: %d%%\n",
  2720. $ok/$total*100);
  2721. if($ok != $total) {
  2722. logmsg "TESTFAIL: These test cases failed: $failed\n";
  2723. }
  2724. }
  2725. else {
  2726. logmsg "TESTFAIL: No tests were performed\n";
  2727. }
  2728. if($all) {
  2729. my $sofar = time()-$start;
  2730. logmsg "TESTDONE: $all tests were considered during $sofar seconds.\n";
  2731. }
  2732. if($skipped) {
  2733. my $s=0;
  2734. logmsg "TESTINFO: $skipped tests were skipped due to these restraints:\n";
  2735. for(keys %skipped) {
  2736. my $r = $_;
  2737. printf "TESTINFO: \"%s\" %d times (", $r, $skipped{$_};
  2738. # now show all test case numbers that had this reason for being
  2739. # skipped
  2740. my $c=0;
  2741. for(0 .. scalar @teststat) {
  2742. my $t = $_;
  2743. if($teststat[$_] eq $r) {
  2744. logmsg ", " if($c);
  2745. logmsg $_;
  2746. $c++;
  2747. }
  2748. }
  2749. logmsg ")\n";
  2750. }
  2751. }
  2752. if($total && ($ok != $total)) {
  2753. exit 1;
  2754. }