run-tests.php 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | PHP Version 7 |
  6. +----------------------------------------------------------------------+
  7. | Copyright (c) 1997-2018 The PHP Group |
  8. +----------------------------------------------------------------------+
  9. | This source file is subject to version 3.01 of the PHP license, |
  10. | that is bundled with this package in the file LICENSE, and is |
  11. | available through the world-wide-web at the following url: |
  12. | https://php.net/license/3_01.txt |
  13. | If you did not receive a copy of the PHP license and are unable to |
  14. | obtain it through the world-wide-web, please send a note to |
  15. | license@php.net so we can mail you a copy immediately. |
  16. +----------------------------------------------------------------------+
  17. | Authors: Ilia Alshanetsky <iliaa@php.net> |
  18. | Preston L. Bannister <pbannister@php.net> |
  19. | Marcus Boerger <helly@php.net> |
  20. | Derick Rethans <derick@php.net> |
  21. | Sander Roobol <sander@php.net> |
  22. | (based on version by: Stig Bakken <ssb@php.net>) |
  23. | (based on the PHP 3 test framework by Rasmus Lerdorf) |
  24. +----------------------------------------------------------------------+
  25. */
  26. /* $Id$ */
  27. define('INIT_DIR', getcwd());
  28. // change into the PHP source directory.
  29. if (getenv('TEST_PHP_SRCDIR')) {
  30. @chdir(getenv('TEST_PHP_SRCDIR'));
  31. }
  32. define('TEST_PHP_SRCDIR', getcwd());
  33. /* Sanity check to ensure that pcre extension needed by this script is available.
  34. * In the event it is not, print a nice error message indicating that this script will
  35. * not run without it.
  36. */
  37. if (!extension_loaded('pcre')) {
  38. echo <<<NO_PCRE_ERROR
  39. +-----------------------------------------------------------+
  40. | ! ERROR ! |
  41. | The test-suite requires that you have pcre extension |
  42. | enabled. To enable this extension either compile your PHP |
  43. | with --with-pcre-regex or if you've compiled pcre as a |
  44. | shared module load it via php.ini. |
  45. +-----------------------------------------------------------+
  46. NO_PCRE_ERROR;
  47. exit(1);
  48. }
  49. if (!function_exists('proc_open')) {
  50. echo <<<NO_PROC_OPEN_ERROR
  51. +-----------------------------------------------------------+
  52. | ! ERROR ! |
  53. | The test-suite requires that proc_open() is available. |
  54. | Please check if you disabled it in php.ini. |
  55. +-----------------------------------------------------------+
  56. NO_PROC_OPEN_ERROR;
  57. exit(1);
  58. }
  59. // If timezone is not set, use UTC.
  60. if (ini_get('date.timezone') == '') {
  61. date_default_timezone_set('UTC');
  62. }
  63. // Delete some security related environment variables
  64. putenv('SSH_CLIENT=deleted');
  65. putenv('SSH_AUTH_SOCK=deleted');
  66. putenv('SSH_TTY=deleted');
  67. putenv('SSH_CONNECTION=deleted');
  68. set_time_limit(0);
  69. ini_set('pcre.backtrack_limit', PHP_INT_MAX);
  70. // delete as much output buffers as possible
  71. while(@ob_end_clean());
  72. if (ob_get_level()) echo "Not all buffers were deleted.\n";
  73. error_reporting(E_ALL);
  74. $environment = $_ENV ?? array();
  75. // Note: php.ini-development sets variables_order="GPCS" not "EGPCS", in which case $_ENV is NOT populated.
  76. // detect and handle this case, or die or warn
  77. if (empty($environment)) {
  78. // not documented, but returns array of all environment variables
  79. $environment = getenv();
  80. }
  81. if (empty($environment['TEMP'])) {
  82. $environment['TEMP'] = sys_get_temp_dir();
  83. if (empty($environment['TEMP'])) {
  84. // for example, OpCache on Windows will fail in this case because child processes (for tests) will not get
  85. // a TEMP variable, so GetTempPath() will fallback to c:\windows, while GetTempPath() will return %TEMP% for parent
  86. // (likely a different path). The parent will initialize the OpCache in that path, and child will fail to reattach to
  87. // the OpCache because it will be using the wrong path.
  88. die("TEMP environment is NOT set");
  89. } else if (count($environment)==1) {
  90. // not having other environment variables, only having TEMP, is probably ok, but strange and may make a
  91. // difference in the test pass rate, so warn the user.
  92. echo "WARNING: Only 1 environment variable will be available to tests(TEMP environment variable)".PHP_EOL;
  93. }
  94. }
  95. //
  96. if ((substr(PHP_OS, 0, 3) == "WIN") && empty($environment["SystemRoot"])) {
  97. $environment["SystemRoot"] = getenv("SystemRoot");
  98. }
  99. // Don't ever guess at the PHP executable location.
  100. // Require the explicit specification.
  101. // Otherwise we could end up testing the wrong file!
  102. $php = null;
  103. $php_cgi = null;
  104. $phpdbg = null;
  105. if (getenv('TEST_PHP_EXECUTABLE')) {
  106. $php = getenv('TEST_PHP_EXECUTABLE');
  107. if ($php=='auto') {
  108. $php = TEST_PHP_SRCDIR . '/sapi/cli/php';
  109. putenv("TEST_PHP_EXECUTABLE=$php");
  110. if (!getenv('TEST_PHP_CGI_EXECUTABLE')) {
  111. $php_cgi = TEST_PHP_SRCDIR . '/sapi/cgi/php-cgi';
  112. if (file_exists($php_cgi)) {
  113. putenv("TEST_PHP_CGI_EXECUTABLE=$php_cgi");
  114. } else {
  115. $php_cgi = null;
  116. }
  117. }
  118. }
  119. $environment['TEST_PHP_EXECUTABLE'] = $php;
  120. }
  121. if (getenv('TEST_PHP_CGI_EXECUTABLE')) {
  122. $php_cgi = getenv('TEST_PHP_CGI_EXECUTABLE');
  123. if ($php_cgi=='auto') {
  124. $php_cgi = TEST_PHP_SRCDIR . '/sapi/cgi/php-cgi';
  125. putenv("TEST_PHP_CGI_EXECUTABLE=$php_cgi");
  126. }
  127. $environment['TEST_PHP_CGI_EXECUTABLE'] = $php_cgi;
  128. }
  129. if (!getenv('TEST_PHPDBG_EXECUTABLE')) {
  130. if (!strncasecmp(PHP_OS, "win", 3) && file_exists(dirname($php) . "/phpdbg.exe")) {
  131. $phpdbg = realpath(dirname($php) . "/phpdbg.exe");
  132. } elseif (file_exists(dirname($php) . "/../../sapi/phpdbg/phpdbg")) {
  133. $phpdbg = realpath(dirname($php) . "/../../sapi/phpdbg/phpdbg");
  134. } elseif (file_exists("./sapi/phpdbg/phpdbg")) {
  135. $phpdbg = realpath("./sapi/phpdbg/phpdbg");
  136. } elseif (file_exists(dirname($php) . "/phpdbg")) {
  137. $phpdbg = realpath(dirname($php) . "/phpdbg");
  138. } else {
  139. $phpdbg = null;
  140. }
  141. if ($phpdbg) {
  142. putenv("TEST_PHPDBG_EXECUTABLE=$phpdbg");
  143. }
  144. }
  145. if (getenv('TEST_PHPDBG_EXECUTABLE')) {
  146. $phpdbg = getenv('TEST_PHPDBG_EXECUTABLE');
  147. if ($phpdbg=='auto') {
  148. $phpdbg = TEST_PHP_SRCDIR . '/sapi/phpdbg/phpdbg';
  149. putenv("TEST_PHPDBG_EXECUTABLE=$phpdbg");
  150. }
  151. $environment['TEST_PHPDBG_EXECUTABLE'] = $phpdbg;
  152. }
  153. if (!function_exists("hrtime")) {
  154. function hrtime(bool $as_num = false)
  155. {
  156. $t = microtime(true);
  157. if ($as_num) {
  158. return $t*1000000000;
  159. }
  160. $s = floor($t);
  161. return array(0 => $s, 1 => ($t - $s)*1000000000);
  162. }
  163. }
  164. function verify_config()
  165. {
  166. global $php;
  167. if (empty($php) || !file_exists($php)) {
  168. error('environment variable TEST_PHP_EXECUTABLE must be set to specify PHP executable!');
  169. }
  170. if (function_exists('is_executable') && !is_executable($php)) {
  171. error("invalid PHP executable specified by TEST_PHP_EXECUTABLE = $php");
  172. }
  173. }
  174. if (getenv('TEST_PHP_LOG_FORMAT')) {
  175. $log_format = strtoupper(getenv('TEST_PHP_LOG_FORMAT'));
  176. } else {
  177. $log_format = 'LEODS';
  178. }
  179. // Check whether a detailed log is wanted.
  180. if (getenv('TEST_PHP_DETAILED')) {
  181. $DETAILED = getenv('TEST_PHP_DETAILED');
  182. } else {
  183. $DETAILED = 0;
  184. }
  185. junit_init();
  186. if (getenv('SHOW_ONLY_GROUPS')) {
  187. $SHOW_ONLY_GROUPS = explode(",", getenv('SHOW_ONLY_GROUPS'));
  188. } else {
  189. $SHOW_ONLY_GROUPS = array();
  190. }
  191. // Check whether user test dirs are requested.
  192. if (getenv('TEST_PHP_USER')) {
  193. $user_tests = explode (',', getenv('TEST_PHP_USER'));
  194. } else {
  195. $user_tests = array();
  196. }
  197. $exts_to_test = array();
  198. $ini_overwrites = array(
  199. 'output_handler=',
  200. 'open_basedir=',
  201. 'disable_functions=',
  202. 'output_buffering=Off',
  203. 'error_reporting=' . (E_ALL | E_STRICT),
  204. 'display_errors=1',
  205. 'display_startup_errors=1',
  206. 'log_errors=0',
  207. 'html_errors=0',
  208. 'track_errors=0',
  209. 'report_memleaks=1',
  210. 'report_zend_debug=0',
  211. 'docref_root=',
  212. 'docref_ext=.html',
  213. 'error_prepend_string=',
  214. 'error_append_string=',
  215. 'auto_prepend_file=',
  216. 'auto_append_file=',
  217. 'ignore_repeated_errors=0',
  218. 'precision=14',
  219. 'memory_limit=128M',
  220. 'log_errors_max_len=0',
  221. 'opcache.fast_shutdown=0',
  222. 'opcache.file_update_protection=0',
  223. 'zend.assertions=1',
  224. );
  225. $no_file_cache = '-d opcache.file_cache= -d opcache.file_cache_only=0';
  226. function write_information()
  227. {
  228. global $php, $php_cgi, $phpdbg, $php_info, $user_tests, $ini_overwrites, $pass_options, $exts_to_test, $valgrind, $no_file_cache;
  229. // Get info from php
  230. $info_file = __DIR__ . '/run-test-info.php';
  231. @unlink($info_file);
  232. $php_info = '<?php echo "
  233. PHP_SAPI : " , PHP_SAPI , "
  234. PHP_VERSION : " , phpversion() , "
  235. ZEND_VERSION: " , zend_version() , "
  236. PHP_OS : " , PHP_OS , " - " , php_uname() , "
  237. INI actual : " , realpath(get_cfg_var("cfg_file_path")) , "
  238. More .INIs : " , (function_exists(\'php_ini_scanned_files\') ? str_replace("\n","", php_ini_scanned_files()) : "** not determined **"); ?>';
  239. save_text($info_file, $php_info);
  240. $info_params = array();
  241. settings2array($ini_overwrites, $info_params);
  242. settings2params($info_params);
  243. $php_info = `$php $pass_options $info_params $no_file_cache "$info_file"`;
  244. define('TESTED_PHP_VERSION', `$php -n -r "echo PHP_VERSION;"`);
  245. if ($php_cgi && $php != $php_cgi) {
  246. $php_info_cgi = `$php_cgi $pass_options $info_params $no_file_cache -q "$info_file"`;
  247. $php_info_sep = "\n---------------------------------------------------------------------";
  248. $php_cgi_info = "$php_info_sep\nPHP : $php_cgi $php_info_cgi$php_info_sep";
  249. } else {
  250. $php_cgi_info = '';
  251. }
  252. if ($phpdbg) {
  253. $phpdbg_info = `$phpdbg $pass_options $info_params $no_file_cache -qrr "$info_file"`;
  254. $php_info_sep = "\n---------------------------------------------------------------------";
  255. $phpdbg_info = "$php_info_sep\nPHP : $phpdbg $phpdbg_info$php_info_sep";
  256. } else {
  257. $phpdbg_info = '';
  258. }
  259. if (function_exists('opcache_invalidate')) {
  260. opcache_invalidate($info_file, true);
  261. }
  262. @unlink($info_file);
  263. // load list of enabled extensions
  264. save_text($info_file, '<?php echo str_replace("Zend OPcache", "opcache", implode(",", get_loaded_extensions())); ?>');
  265. $exts_to_test = explode(',',`$php $pass_options $info_params $no_file_cache "$info_file"`);
  266. // check for extensions that need special handling and regenerate
  267. $info_params_ex = array(
  268. 'session' => array('session.auto_start=0'),
  269. 'tidy' => array('tidy.clean_output=0'),
  270. 'zlib' => array('zlib.output_compression=Off'),
  271. 'xdebug' => array('xdebug.default_enable=0','xdebug.mode=off'),
  272. 'mbstring' => array('mbstring.func_overload=0'),
  273. );
  274. foreach($info_params_ex as $ext => $ini_overwrites_ex) {
  275. if (in_array($ext, $exts_to_test)) {
  276. $ini_overwrites = array_merge($ini_overwrites, $ini_overwrites_ex);
  277. }
  278. }
  279. if (function_exists('opcache_invalidate')) {
  280. opcache_invalidate($info_file, true);
  281. }
  282. @unlink($info_file);
  283. // Write test context information.
  284. echo "
  285. =====================================================================
  286. PHP : $php $php_info $php_cgi_info $phpdbg_info
  287. CWD : ".TEST_PHP_SRCDIR."
  288. Extra dirs : ";
  289. foreach ($user_tests as $test_dir) {
  290. echo "{$test_dir}\n ";
  291. }
  292. echo "
  293. VALGRIND : " . ($valgrind ? $valgrind->getHeader() : 'Not used') . "
  294. =====================================================================
  295. ";
  296. }
  297. define('PHP_QA_EMAIL', 'qa-reports@lists.php.net');
  298. define('QA_SUBMISSION_PAGE', 'http://qa.php.net/buildtest-process.php');
  299. define('QA_REPORTS_PAGE', 'http://qa.php.net/reports');
  300. define('TRAVIS_CI' , (bool) getenv('TRAVIS'));
  301. function save_or_mail_results()
  302. {
  303. global $sum_results, $just_save_results, $failed_test_summary,
  304. $PHP_FAILED_TESTS, $php, $output_file;
  305. /* We got failed Tests, offer the user to send an e-mail to QA team, unless NO_INTERACTION is set */
  306. if (!getenv('NO_INTERACTION') && !TRAVIS_CI) {
  307. $fp = fopen("php://stdin", "r+");
  308. if ($sum_results['FAILED'] || $sum_results['BORKED'] || $sum_results['WARNED'] || $sum_results['LEAKED']) {
  309. echo "\nYou may have found a problem in PHP.";
  310. }
  311. echo "\nThis report can be automatically sent to the PHP QA team at\n";
  312. echo QA_REPORTS_PAGE . " and http://news.php.net/php.qa.reports\n";
  313. echo "This gives us a better understanding of PHP's behavior.\n";
  314. echo "If you don't want to send the report immediately you can choose\n";
  315. echo "option \"s\" to save it. You can then email it to ". PHP_QA_EMAIL . " later.\n";
  316. echo "Do you want to send this report now? [Yns]: ";
  317. flush();
  318. $user_input = fgets($fp, 10);
  319. $just_save_results = (strtolower($user_input[0]) == 's');
  320. }
  321. if ($just_save_results || !getenv('NO_INTERACTION') || TRAVIS_CI) {
  322. if ($just_save_results || TRAVIS_CI || strlen(trim($user_input)) == 0 || strtolower($user_input[0]) == 'y') {
  323. /*
  324. * Collect information about the host system for our report
  325. * Fetch phpinfo() output so that we can see the PHP environment
  326. * Make an archive of all the failed tests
  327. * Send an email
  328. */
  329. if ($just_save_results) {
  330. $user_input = 's';
  331. }
  332. /* Ask the user to provide an email address, so that QA team can contact the user */
  333. if (TRAVIS_CI) {
  334. $user_email = 'travis at php dot net';
  335. } elseif (!strncasecmp($user_input, 'y', 1) || strlen(trim($user_input)) == 0) {
  336. echo "\nPlease enter your email address.\n(Your address will be mangled so that it will not go out on any\nmailinglist in plain text): ";
  337. flush();
  338. $user_email = trim(fgets($fp, 1024));
  339. $user_email = str_replace("@", " at ", str_replace(".", " dot ", $user_email));
  340. }
  341. $failed_tests_data = '';
  342. $sep = "\n" . str_repeat('=', 80) . "\n";
  343. $failed_tests_data .= $failed_test_summary . "\n";
  344. $failed_tests_data .= get_summary(true, false) . "\n";
  345. if ($sum_results['FAILED']) {
  346. foreach ($PHP_FAILED_TESTS['FAILED'] as $test_info) {
  347. $failed_tests_data .= $sep . $test_info['name'] . $test_info['info'];
  348. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['output']), FILE_BINARY);
  349. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['diff']), FILE_BINARY);
  350. $failed_tests_data .= $sep . "\n\n";
  351. }
  352. $status = "failed";
  353. } else {
  354. $status = "success";
  355. }
  356. $failed_tests_data .= "\n" . $sep . 'BUILD ENVIRONMENT' . $sep;
  357. $failed_tests_data .= "OS:\n" . PHP_OS . " - " . php_uname() . "\n\n";
  358. $ldd = $autoconf = $sys_libtool = $libtool = $compiler = 'N/A';
  359. if (substr(PHP_OS, 0, 3) != "WIN") {
  360. /* If PHP_AUTOCONF is set, use it; otherwise, use 'autoconf'. */
  361. if (getenv('PHP_AUTOCONF')) {
  362. $autoconf = shell_exec(getenv('PHP_AUTOCONF') . ' --version');
  363. } else {
  364. $autoconf = shell_exec('autoconf --version');
  365. }
  366. /* Always use the generated libtool - Mac OSX uses 'glibtool' */
  367. $libtool = shell_exec(INIT_DIR . '/libtool --version');
  368. /* Use shtool to find out if there is glibtool present (MacOSX) */
  369. $sys_libtool_path = shell_exec(__DIR__ . '/build/shtool path glibtool libtool');
  370. if ($sys_libtool_path) {
  371. $sys_libtool = shell_exec(str_replace("\n", "", $sys_libtool_path) . ' --version');
  372. }
  373. /* Try the most common flags for 'version' */
  374. $flags = array('-v', '-V', '--version');
  375. $cc_status = 0;
  376. foreach($flags AS $flag) {
  377. system(getenv('CC') . " $flag >/dev/null 2>&1", $cc_status);
  378. if ($cc_status == 0) {
  379. $compiler = shell_exec(getenv('CC') . " $flag 2>&1");
  380. break;
  381. }
  382. }
  383. $ldd = shell_exec("ldd $php 2>/dev/null");
  384. }
  385. $failed_tests_data .= "Autoconf:\n$autoconf\n";
  386. $failed_tests_data .= "Bundled Libtool:\n$libtool\n";
  387. $failed_tests_data .= "System Libtool:\n$sys_libtool\n";
  388. $failed_tests_data .= "Compiler:\n$compiler\n";
  389. $failed_tests_data .= "Bison:\n". shell_exec('bison --version 2>/dev/null') . "\n";
  390. $failed_tests_data .= "Libraries:\n$ldd\n";
  391. $failed_tests_data .= "\n";
  392. if (isset($user_email)) {
  393. $failed_tests_data .= "User's E-mail: " . $user_email . "\n\n";
  394. }
  395. $failed_tests_data .= $sep . "PHPINFO" . $sep;
  396. $failed_tests_data .= shell_exec($php . ' -ddisplay_errors=stderr -dhtml_errors=0 -i 2> /dev/null');
  397. if (($just_save_results || !mail_qa_team($failed_tests_data, $status)) && !TRAVIS_CI) {
  398. file_put_contents($output_file, $failed_tests_data);
  399. if (!$just_save_results) {
  400. echo "\nThe test script was unable to automatically send the report to PHP's QA Team\n";
  401. }
  402. echo "Please send " . $output_file . " to " . PHP_QA_EMAIL . " manually, thank you.\n";
  403. } elseif (!getenv('NO_INTERACTION') && !TRAVIS_CI) {
  404. fwrite($fp, "\nThank you for helping to make PHP better.\n");
  405. fclose($fp);
  406. }
  407. }
  408. }
  409. }
  410. // Determine the tests to be run.
  411. $test_files = array();
  412. $redir_tests = array();
  413. $test_results = array();
  414. $PHP_FAILED_TESTS = array('BORKED' => array(), 'FAILED' => array(), 'WARNED' => array(), 'LEAKED' => array(), 'XFAILED' => array(), 'SLOW' => array());
  415. // If parameters given assume they represent selected tests to run.
  416. $result_tests_file= false;
  417. $failed_tests_file= false;
  418. $pass_option_n = false;
  419. $pass_options = '';
  420. $output_file = INIT_DIR . '/php_test_results_' . date('Ymd_Hi') . '.txt';
  421. $just_save_results = false;
  422. $valgrind = null;
  423. $html_output = false;
  424. $html_file = null;
  425. $temp_source = null;
  426. $temp_target = null;
  427. $temp_urlbase = null;
  428. $conf_passed = null;
  429. $no_clean = false;
  430. $slow_min_ms = INF;
  431. $cfgtypes = array('show', 'keep');
  432. $cfgfiles = array('skip', 'php', 'clean', 'out', 'diff', 'exp', 'mem');
  433. $cfg = array();
  434. foreach($cfgtypes as $type) {
  435. $cfg[$type] = array();
  436. foreach($cfgfiles as $file) {
  437. $cfg[$type][$file] = false;
  438. }
  439. }
  440. if (getenv('TEST_PHP_ARGS')) {
  441. if (!isset($argc, $argv) || !$argc) {
  442. $argv = array(__FILE__);
  443. }
  444. $argv = array_merge($argv, explode(' ', getenv('TEST_PHP_ARGS')));
  445. $argc = count($argv);
  446. }
  447. if (isset($argc) && $argc > 1) {
  448. for ($i=1; $i<$argc; $i++) {
  449. $is_switch = false;
  450. $switch = substr($argv[$i],1,1);
  451. $repeat = substr($argv[$i],0,1) == '-';
  452. while ($repeat) {
  453. if (!$is_switch) {
  454. $switch = substr($argv[$i],1,1);
  455. }
  456. $is_switch = true;
  457. if ($repeat) {
  458. foreach($cfgtypes as $type) {
  459. if (strpos($switch, '--' . $type) === 0) {
  460. foreach($cfgfiles as $file) {
  461. if ($switch == '--' . $type . '-' . $file) {
  462. $cfg[$type][$file] = true;
  463. $is_switch = false;
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. if (!$is_switch) {
  471. $is_switch = true;
  472. break;
  473. }
  474. $repeat = false;
  475. switch($switch) {
  476. case 'r':
  477. case 'l':
  478. $test_list = file($argv[++$i]);
  479. if ($test_list) {
  480. foreach($test_list as $test) {
  481. $matches = array();
  482. if (preg_match('/^#.*\[(.*)\]\:\s+(.*)$/', $test, $matches)) {
  483. $redir_tests[] = array($matches[1], $matches[2]);
  484. } else if (strlen($test)) {
  485. $test_files[] = trim($test);
  486. }
  487. }
  488. }
  489. if ($switch != 'l') {
  490. break;
  491. }
  492. $i--;
  493. // break left intentionally
  494. case 'w':
  495. $failed_tests_file = fopen($argv[++$i], 'w+t');
  496. break;
  497. case 'a':
  498. $failed_tests_file = fopen($argv[++$i], 'a+t');
  499. break;
  500. case 'W':
  501. $result_tests_file = fopen($argv[++$i], 'w+t');
  502. break;
  503. case 'c':
  504. $conf_passed = $argv[++$i];
  505. break;
  506. case 'd':
  507. $ini_overwrites[] = $argv[++$i];
  508. break;
  509. case 'g':
  510. $SHOW_ONLY_GROUPS = explode(",", $argv[++$i]);
  511. break;
  512. //case 'h'
  513. case '--keep-all':
  514. foreach($cfgfiles as $file) {
  515. $cfg['keep'][$file] = true;
  516. }
  517. break;
  518. //case 'l'
  519. case 'm':
  520. $valgrind = new RuntestsValgrind($environment);
  521. break;
  522. case 'n':
  523. if (!$pass_option_n) {
  524. $pass_options .= ' -n';
  525. }
  526. $pass_option_n = true;
  527. break;
  528. case 'e':
  529. $pass_options .= ' -e';
  530. break;
  531. case '--no-clean':
  532. $no_clean = true;
  533. break;
  534. case 'p':
  535. $php = $argv[++$i];
  536. putenv("TEST_PHP_EXECUTABLE=$php");
  537. $environment['TEST_PHP_EXECUTABLE'] = $php;
  538. break;
  539. case 'P':
  540. if(constant('PHP_BINARY')) {
  541. $php = PHP_BINARY;
  542. } else {
  543. break;
  544. }
  545. putenv("TEST_PHP_EXECUTABLE=$php");
  546. $environment['TEST_PHP_EXECUTABLE'] = $php;
  547. break;
  548. case 'q':
  549. putenv('NO_INTERACTION=1');
  550. break;
  551. //case 'r'
  552. case 's':
  553. $output_file = $argv[++$i];
  554. $just_save_results = true;
  555. break;
  556. case '--set-timeout':
  557. $environment['TEST_TIMEOUT'] = $argv[++$i];
  558. break;
  559. case '--show-all':
  560. foreach($cfgfiles as $file) {
  561. $cfg['show'][$file] = true;
  562. }
  563. break;
  564. case '--show-slow':
  565. $slow_min_ms = $argv[++$i];
  566. break;
  567. case '--temp-source':
  568. $temp_source = $argv[++$i];
  569. break;
  570. case '--temp-target':
  571. $temp_target = $argv[++$i];
  572. if ($temp_urlbase) {
  573. $temp_urlbase = $temp_target;
  574. }
  575. break;
  576. case '--temp-urlbase':
  577. $temp_urlbase = $argv[++$i];
  578. break;
  579. case 'v':
  580. case '--verbose':
  581. $DETAILED = true;
  582. break;
  583. case 'x':
  584. $environment['SKIP_SLOW_TESTS'] = 1;
  585. break;
  586. case '--offline':
  587. $environment['SKIP_ONLINE_TESTS'] = 1;
  588. break;
  589. //case 'w'
  590. case '-':
  591. // repeat check with full switch
  592. $switch = $argv[$i];
  593. if ($switch != '-') {
  594. $repeat = true;
  595. }
  596. break;
  597. case '--html':
  598. $html_file = fopen($argv[++$i], 'wt');
  599. $html_output = is_resource($html_file);
  600. break;
  601. case '--version':
  602. echo '$Id$' . "\n";
  603. exit(1);
  604. default:
  605. echo "Illegal switch '$switch' specified!\n";
  606. case 'h':
  607. case '-help':
  608. case '--help':
  609. echo <<<HELP
  610. Synopsis:
  611. php run-tests.php [options] [files] [directories]
  612. Options:
  613. -l <file> Read the testfiles to be executed from <file>. After the test
  614. has finished all failed tests are written to the same <file>.
  615. If the list is empty and no further test is specified then
  616. all tests are executed (same as: -r <file> -w <file>).
  617. -r <file> Read the testfiles to be executed from <file>.
  618. -w <file> Write a list of all failed tests to <file>.
  619. -a <file> Same as -w but append rather then truncating <file>.
  620. -W <file> Write a list of all tests and their result status to <file>.
  621. -c <file> Look for php.ini in directory <file> or use <file> as ini.
  622. -n Pass -n option to the php binary (Do not use a php.ini).
  623. -d foo=bar Pass -d option to the php binary (Define INI entry foo
  624. with value 'bar').
  625. -g Comma separated list of groups to show during test run
  626. (possible values: PASS, FAIL, XFAIL, SKIP, BORK, WARN, LEAK, REDIRECT).
  627. -m Test for memory leaks with Valgrind.
  628. -p <php> Specify PHP executable to run.
  629. -P Use PHP_BINARY as PHP executable to run.
  630. -q Quiet, no user interaction (same as environment NO_INTERACTION).
  631. -s <file> Write output to <file>.
  632. -x Sets 'SKIP_SLOW_TESTS' environmental variable.
  633. --offline Sets 'SKIP_ONLINE_TESTS' environmental variable.
  634. --verbose
  635. -v Verbose mode.
  636. --help
  637. -h This Help.
  638. --html <file> Generate HTML output.
  639. --temp-source <sdir> --temp-target <tdir> [--temp-urlbase <url>]
  640. Write temporary files to <tdir> by replacing <sdir> from the
  641. filenames to generate with <tdir>. If --html is being used and
  642. <url> given then the generated links are relative and prefixed
  643. with the given url. In general you want to make <sdir> the path
  644. to your source files and <tdir> some pach in your web page
  645. hierarchy with <url> pointing to <tdir>.
  646. --keep-[all|php|skip|clean]
  647. Do not delete 'all' files, 'php' test file, 'skip' or 'clean'
  648. file.
  649. --set-timeout [n]
  650. Set timeout for individual tests, where [n] is the number of
  651. seconds. The default value is 60 seconds, or 300 seconds when
  652. testing for memory leaks.
  653. --show-[all|php|skip|clean|exp|diff|out]
  654. Show 'all' files, 'php' test file, 'skip' or 'clean' file. You
  655. can also use this to show the output 'out', the expected result
  656. 'exp' or the difference between them 'diff'. The result types
  657. get written independent of the log format, however 'diff' only
  658. exists when a test fails.
  659. --show-slow [n]
  660. Show all tests that took longer than [n] milliseconds to run.
  661. --no-clean Do not execute clean section if any.
  662. HELP;
  663. exit(1);
  664. }
  665. }
  666. if (!$is_switch) {
  667. $testfile = realpath($argv[$i]);
  668. if (!$testfile && strpos($argv[$i], '*') !== false && function_exists('glob')) {
  669. if (substr($argv[$i], -5) == '.phpt') {
  670. $pattern_match = glob($argv[$i]);
  671. } else if (preg_match("/\*$/", $argv[$i])) {
  672. $pattern_match = glob($argv[$i] . '.phpt');
  673. } else {
  674. die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
  675. }
  676. if (is_array($pattern_match)) {
  677. $test_files = array_merge($test_files, $pattern_match);
  678. }
  679. } else if (is_dir($testfile)) {
  680. find_files($testfile);
  681. } else if (substr($testfile, -5) == '.phpt') {
  682. $test_files[] = $testfile;
  683. } else {
  684. die('Cannot find test file "' . $argv[$i] . '".' . PHP_EOL);
  685. }
  686. }
  687. }
  688. if (strlen($conf_passed)) {
  689. if (substr(PHP_OS, 0, 3) == "WIN") {
  690. $pass_options .= " -c " . escapeshellarg($conf_passed);
  691. } else {
  692. $pass_options .= " -c '" . realpath($conf_passed) . "'";
  693. }
  694. }
  695. $test_files = array_unique($test_files);
  696. $test_files = array_merge($test_files, $redir_tests);
  697. // Run selected tests.
  698. $test_cnt = count($test_files);
  699. if ($test_cnt) {
  700. putenv('NO_INTERACTION=1');
  701. verify_config();
  702. write_information();
  703. usort($test_files, "test_sort");
  704. $start_time = time();
  705. if (!$html_output) {
  706. echo "Running selected tests.\n";
  707. } else {
  708. show_start($start_time);
  709. }
  710. $test_idx = 0;
  711. run_all_tests($test_files, $environment);
  712. $end_time = time();
  713. if ($html_output) {
  714. show_end($end_time);
  715. }
  716. if ($failed_tests_file) {
  717. fclose($failed_tests_file);
  718. }
  719. if ($result_tests_file) {
  720. fclose($result_tests_file);
  721. }
  722. compute_summary();
  723. if ($html_output) {
  724. fwrite($html_file, "<hr/>\n" . get_summary(false, true));
  725. }
  726. echo "=====================================================================";
  727. echo get_summary(false, false);
  728. if ($html_output) {
  729. fclose($html_file);
  730. }
  731. if ($output_file != '' && $just_save_results) {
  732. save_or_mail_results();
  733. }
  734. junit_save_xml();
  735. if (getenv('REPORT_EXIT_STATUS') !== '0' &&
  736. getenv('REPORT_EXIT_STATUS') !== 'no' &&
  737. ($sum_results['FAILED'] || $sum_results['BORKED'] || $sum_results['LEAKED'])) {
  738. exit(1);
  739. }
  740. exit(0);
  741. }
  742. }
  743. verify_config();
  744. write_information();
  745. // Compile a list of all test files (*.phpt).
  746. $test_files = array();
  747. $exts_tested = count($exts_to_test);
  748. $exts_skipped = 0;
  749. $ignored_by_ext = 0;
  750. sort($exts_to_test);
  751. $test_dirs = array();
  752. $optionals = array('tests', 'ext', 'Zend', 'sapi');
  753. foreach($optionals as $dir) {
  754. if (is_dir($dir)) {
  755. $test_dirs[] = $dir;
  756. }
  757. }
  758. // Convert extension names to lowercase
  759. foreach ($exts_to_test as $key => $val) {
  760. $exts_to_test[$key] = strtolower($val);
  761. }
  762. foreach ($test_dirs as $dir) {
  763. find_files(TEST_PHP_SRCDIR."/{$dir}", $dir == 'ext');
  764. }
  765. foreach ($user_tests as $dir) {
  766. find_files($dir, $dir == 'ext');
  767. }
  768. function find_files($dir, $is_ext_dir = false, $ignore = false)
  769. {
  770. global $test_files, $exts_to_test, $ignored_by_ext, $exts_skipped;
  771. $o = opendir($dir) or error("cannot open directory: $dir");
  772. while (($name = readdir($o)) !== false) {
  773. if (is_dir("{$dir}/{$name}") && !in_array($name, array('.', '..', '.svn'))) {
  774. $skip_ext = ($is_ext_dir && !in_array(strtolower($name), $exts_to_test));
  775. if ($skip_ext) {
  776. $exts_skipped++;
  777. }
  778. find_files("{$dir}/{$name}", false, $ignore || $skip_ext);
  779. }
  780. // Cleanup any left-over tmp files from last run.
  781. if (substr($name, -4) == '.tmp') {
  782. @unlink("$dir/$name");
  783. continue;
  784. }
  785. // Otherwise we're only interested in *.phpt files.
  786. if (substr($name, -5) == '.phpt') {
  787. if ($ignore) {
  788. $ignored_by_ext++;
  789. } else {
  790. $testfile = realpath("{$dir}/{$name}");
  791. $test_files[] = $testfile;
  792. }
  793. }
  794. }
  795. closedir($o);
  796. }
  797. function test_name($name)
  798. {
  799. if (is_array($name)) {
  800. return $name[0] . ':' . $name[1];
  801. } else {
  802. return $name;
  803. }
  804. }
  805. function test_sort($a, $b)
  806. {
  807. $a = test_name($a);
  808. $b = test_name($b);
  809. $ta = strpos($a, TEST_PHP_SRCDIR."/tests") === 0 ? 1 + (strpos($a, TEST_PHP_SRCDIR."/tests/run-test") === 0 ? 1 : 0) : 0;
  810. $tb = strpos($b, TEST_PHP_SRCDIR."/tests") === 0 ? 1 + (strpos($b, TEST_PHP_SRCDIR."/tests/run-test") === 0 ? 1 : 0) : 0;
  811. if ($ta == $tb) {
  812. return strcmp($a, $b);
  813. } else {
  814. return $tb - $ta;
  815. }
  816. }
  817. $test_files = array_unique($test_files);
  818. usort($test_files, "test_sort");
  819. $start_time = time();
  820. show_start($start_time);
  821. $test_cnt = count($test_files);
  822. $test_idx = 0;
  823. run_all_tests($test_files, $environment);
  824. $end_time = time();
  825. if ($failed_tests_file) {
  826. fclose($failed_tests_file);
  827. }
  828. if ($result_tests_file) {
  829. fclose($result_tests_file);
  830. }
  831. // Summarize results
  832. if (0 == count($test_results)) {
  833. echo "No tests were run.\n";
  834. return;
  835. }
  836. compute_summary();
  837. show_end($end_time);
  838. show_summary();
  839. if ($html_output) {
  840. fclose($html_file);
  841. }
  842. save_or_mail_results();
  843. junit_save_xml();
  844. if (getenv('REPORT_EXIT_STATUS') !== '0' &&
  845. getenv('REPORT_EXIT_STATUS') !== 'no' &&
  846. ($sum_results['FAILED'] || $sum_results['BORKED'] || $sum_results['LEAKED'])) {
  847. exit(1);
  848. }
  849. exit(0);
  850. //
  851. // Send Email to QA Team
  852. //
  853. function mail_qa_team($data, $status = false)
  854. {
  855. $url_bits = parse_url(QA_SUBMISSION_PAGE);
  856. if ($proxy = getenv('http_proxy')) {
  857. $proxy = parse_url($proxy);
  858. $path = $url_bits['host'].$url_bits['path'];
  859. $host = $proxy['host'];
  860. if (empty($proxy['port'])) {
  861. $proxy['port'] = 80;
  862. }
  863. $port = $proxy['port'];
  864. } else {
  865. $path = $url_bits['path'];
  866. $host = $url_bits['host'];
  867. $port = empty($url_bits['port']) ? 80 : $port = $url_bits['port'];
  868. }
  869. $data = "php_test_data=" . urlencode(base64_encode(str_replace("\00", '[0x0]', $data)));
  870. $data_length = strlen($data);
  871. $fs = fsockopen($host, $port, $errno, $errstr, 10);
  872. if (!$fs) {
  873. return false;
  874. }
  875. $php_version = urlencode(TESTED_PHP_VERSION);
  876. echo "\nPosting to ". QA_SUBMISSION_PAGE . "\n";
  877. fwrite($fs, "POST " . $path . "?status=$status&version=$php_version HTTP/1.1\r\n");
  878. fwrite($fs, "Host: " . $host . "\r\n");
  879. fwrite($fs, "User-Agent: QA Browser 0.1\r\n");
  880. fwrite($fs, "Content-Type: application/x-www-form-urlencoded\r\n");
  881. fwrite($fs, "Content-Length: " . $data_length . "\r\n\r\n");
  882. fwrite($fs, $data);
  883. fwrite($fs, "\r\n\r\n");
  884. fclose($fs);
  885. return 1;
  886. }
  887. //
  888. // Write the given text to a temporary file, and return the filename.
  889. //
  890. function save_text($filename, $text, $filename_copy = null)
  891. {
  892. global $DETAILED;
  893. if ($filename_copy && $filename_copy != $filename) {
  894. if (file_put_contents($filename_copy, $text, FILE_BINARY) === false) {
  895. error("Cannot open file '" . $filename_copy . "' (save_text)");
  896. }
  897. }
  898. if (file_put_contents($filename, $text, FILE_BINARY) === false) {
  899. error("Cannot open file '" . $filename . "' (save_text)");
  900. }
  901. if (1 < $DETAILED) echo "
  902. FILE $filename {{{
  903. $text
  904. }}}
  905. ";
  906. }
  907. //
  908. // Write an error in a format recognizable to Emacs or MSVC.
  909. //
  910. function error_report($testname, $logname, $tested)
  911. {
  912. $testname = realpath($testname);
  913. $logname = realpath($logname);
  914. switch (strtoupper(getenv('TEST_PHP_ERROR_STYLE'))) {
  915. case 'MSVC':
  916. echo $testname . "(1) : $tested\n";
  917. echo $logname . "(1) : $tested\n";
  918. break;
  919. case 'EMACS':
  920. echo $testname . ":1: $tested\n";
  921. echo $logname . ":1: $tested\n";
  922. break;
  923. }
  924. }
  925. function system_with_timeout($commandline, $env = null, $stdin = null, $captureStdIn = true, $captureStdOut = true, $captureStdErr = true)
  926. {
  927. global $valgrind;
  928. $data = '';
  929. $bin_env = array();
  930. foreach((array)$env as $key => $value) {
  931. $bin_env[$key] = $value;
  932. }
  933. $descriptorspec = array();
  934. if ($captureStdIn) {
  935. $descriptorspec[0] = array('pipe', 'r');
  936. }
  937. if ($captureStdOut) {
  938. $descriptorspec[1] = array('pipe', 'w');
  939. }
  940. if ($captureStdErr) {
  941. $descriptorspec[2] = array('pipe', 'w');
  942. }
  943. $proc = proc_open($commandline, $descriptorspec, $pipes, TEST_PHP_SRCDIR, $bin_env, array('suppress_errors' => true));
  944. if (!$proc) {
  945. return false;
  946. }
  947. if ($captureStdIn) {
  948. if (!is_null($stdin)) {
  949. fwrite($pipes[0], $stdin);
  950. }
  951. fclose($pipes[0]);
  952. unset($pipes[0]);
  953. }
  954. $timeout = $valgrind ? 300 : ($env['TEST_TIMEOUT'] ?? 60);
  955. while (true) {
  956. /* hide errors from interrupted syscalls */
  957. $r = $pipes;
  958. $w = null;
  959. $e = null;
  960. $n = @stream_select($r, $w, $e, $timeout);
  961. if ($n === false) {
  962. break;
  963. } else if ($n === 0) {
  964. /* timed out */
  965. $data .= "\n ** ERROR: process timed out **\n";
  966. proc_terminate($proc, 9);
  967. return $data;
  968. } else if ($n > 0) {
  969. if ($captureStdOut) {
  970. $line = fread($pipes[1], 8192);
  971. } elseif ($captureStdErr) {
  972. $line = fread($pipes[2], 8192);
  973. } else {
  974. $line = '';
  975. }
  976. if (strlen($line) == 0) {
  977. /* EOF */
  978. break;
  979. }
  980. $data .= $line;
  981. }
  982. }
  983. $stat = proc_get_status($proc);
  984. if ($stat['signaled']) {
  985. $data .= "\nTermsig=" . $stat['stopsig'] . "\n";
  986. }
  987. if ($stat["exitcode"] > 128 && $stat["exitcode"] < 160) {
  988. $data .= "\nTermsig=" . ($stat["exitcode"] - 128) . "\n";
  989. }
  990. proc_close($proc);
  991. return $data;
  992. }
  993. function run_all_tests($test_files, $env, $redir_tested = null)
  994. {
  995. global $test_results, $failed_tests_file, $result_tests_file, $php, $test_idx;
  996. foreach($test_files as $name) {
  997. if (is_array($name)) {
  998. $index = "# $name[1]: $name[0]";
  999. if ($redir_tested) {
  1000. $name = $name[0];
  1001. }
  1002. } else if ($redir_tested) {
  1003. $index = "# $redir_tested: $name";
  1004. } else {
  1005. $index = $name;
  1006. }
  1007. $test_idx++;
  1008. $result = run_test($php, $name, $env);
  1009. if (!is_array($name) && $result != 'REDIR') {
  1010. $test_results[$index] = $result;
  1011. if ($failed_tests_file && ($result == 'XFAILED' || $result == 'FAILED' || $result == 'WARNED' || $result == 'LEAKED')) {
  1012. fwrite($failed_tests_file, "$index\n");
  1013. }
  1014. if ($result_tests_file) {
  1015. fwrite($result_tests_file, "$result\t$index\n");
  1016. }
  1017. }
  1018. }
  1019. }
  1020. //
  1021. // Show file or result block
  1022. //
  1023. function show_file_block($file, $block, $section = null)
  1024. {
  1025. global $cfg;
  1026. if ($cfg['show'][$file]) {
  1027. if (is_null($section)) {
  1028. $section = strtoupper($file);
  1029. }
  1030. echo "\n========" . $section . "========\n";
  1031. echo rtrim($block);
  1032. echo "\n========DONE========\n";
  1033. }
  1034. }
  1035. //
  1036. // Run an individual test case.
  1037. //
  1038. function run_test($php, $file, $env)
  1039. {
  1040. global $log_format, $ini_overwrites, $PHP_FAILED_TESTS;
  1041. global $pass_options, $DETAILED, $IN_REDIRECT, $test_cnt, $test_idx;
  1042. global $valgrind, $temp_source, $temp_target, $cfg, $environment;
  1043. global $no_clean;
  1044. global $SHOW_ONLY_GROUPS;
  1045. global $no_file_cache;
  1046. global $slow_min_ms;
  1047. $temp_filenames = null;
  1048. $org_file = $file;
  1049. if (isset($env['TEST_PHP_CGI_EXECUTABLE'])) {
  1050. $php_cgi = $env['TEST_PHP_CGI_EXECUTABLE'];
  1051. }
  1052. if (isset($env['TEST_PHPDBG_EXECUTABLE'])) {
  1053. $phpdbg = $env['TEST_PHPDBG_EXECUTABLE'];
  1054. }
  1055. if (is_array($file)) {
  1056. $file = $file[0];
  1057. }
  1058. if ($DETAILED) echo "
  1059. =================
  1060. TEST $file
  1061. ";
  1062. // Load the sections of the test file.
  1063. $section_text = array('TEST' => '');
  1064. $fp = fopen($file, "rb") or error("Cannot open test file: $file");
  1065. $bork_info = null;
  1066. if (!feof($fp)) {
  1067. $line = fgets($fp);
  1068. if ($line === false) {
  1069. $bork_info = "cannot read test";
  1070. }
  1071. } else {
  1072. $bork_info = "empty test [$file]";
  1073. }
  1074. if ($bork_info === null && strncmp('--TEST--', $line, 8)) {
  1075. $bork_info = "tests must start with --TEST-- [$file]";
  1076. }
  1077. $section = 'TEST';
  1078. $secfile = false;
  1079. $secdone = false;
  1080. while (!feof($fp)) {
  1081. $line = fgets($fp);
  1082. if ($line === false) {
  1083. break;
  1084. }
  1085. // Match the beginning of a section.
  1086. if (preg_match('/^--([_A-Z]+)--/', $line, $r)) {
  1087. $section = (string) $r[1];
  1088. if (isset($section_text[$section]) && $section_text[$section]) {
  1089. $bork_info = "duplicated $section section";
  1090. }
  1091. // check for unknown sections
  1092. if (!in_array($section, array(
  1093. 'EXPECT', 'EXPECTF', 'EXPECTREGEX', 'EXPECTREGEX_EXTERNAL', 'EXPECT_EXTERNAL', 'EXPECTF_EXTERNAL', 'EXPECTHEADERS',
  1094. 'POST', 'POST_RAW', 'GZIP_POST', 'DEFLATE_POST', 'PUT', 'GET', 'COOKIE', 'ARGS', 'REQUEST', 'HEADERS',
  1095. 'FILE', 'FILEEOF', 'FILE_EXTERNAL', 'REDIRECTTEST',
  1096. 'CAPTURE_STDIO', 'STDIN', 'CGI', 'PHPDBG',
  1097. 'INI', 'ENV', 'EXTENSIONS',
  1098. 'SKIPIF', 'XFAIL', 'CLEAN',
  1099. 'CREDITS', 'DESCRIPTION', 'CONFLICTS',
  1100. ))) {
  1101. $bork_info = 'Unknown section "' . $section . '"';
  1102. }
  1103. $section_text[$section] = '';
  1104. $secfile = $section == 'FILE' || $section == 'FILEEOF' || $section == 'FILE_EXTERNAL';
  1105. $secdone = false;
  1106. continue;
  1107. }
  1108. // Add to the section text.
  1109. if (!$secdone) {
  1110. $section_text[$section] .= $line;
  1111. }
  1112. // End of actual test?
  1113. if ($secfile && preg_match('/^===DONE===\s*$/', $line)) {
  1114. $secdone = true;
  1115. }
  1116. }
  1117. // the redirect section allows a set of tests to be reused outside of
  1118. // a given test dir
  1119. if ($bork_info === null) {
  1120. if (isset($section_text['REDIRECTTEST'])) {
  1121. if ($IN_REDIRECT) {
  1122. $bork_info = "Can't redirect a test from within a redirected test";
  1123. }
  1124. } else {
  1125. if (!isset($section_text['PHPDBG']) && @count($section_text['FILE']) + @count($section_text['FILEEOF']) + @count($section_text['FILE_EXTERNAL']) != 1) {
  1126. $bork_info = "missing section --FILE--";
  1127. }
  1128. if (isset($section_text['FILEEOF'])) {
  1129. $section_text['FILE'] = preg_replace("/[\r\n]+$/", '', $section_text['FILEEOF']);
  1130. unset($section_text['FILEEOF']);
  1131. }
  1132. foreach (array( 'FILE', 'EXPECT', 'EXPECTF', 'EXPECTREGEX' ) as $prefix) {
  1133. $key = $prefix . '_EXTERNAL';
  1134. if (isset($section_text[$key])) {
  1135. // don't allow tests to retrieve files from anywhere but this subdirectory
  1136. $section_text[$key] = dirname($file) . '/' . trim(str_replace('..', '', $section_text[$key]));
  1137. if (file_exists($section_text[$key])) {
  1138. $section_text[$prefix] = file_get_contents($section_text[$key], FILE_BINARY);
  1139. unset($section_text[$key]);
  1140. } else {
  1141. $bork_info = "could not load --" . $key . "-- " . dirname($file) . '/' . trim($section_text[$key]);
  1142. }
  1143. }
  1144. }
  1145. if ((@count($section_text['EXPECT']) + @count($section_text['EXPECTF']) + @count($section_text['EXPECTREGEX'])) != 1) {
  1146. $bork_info = "missing section --EXPECT--, --EXPECTF-- or --EXPECTREGEX--";
  1147. }
  1148. }
  1149. }
  1150. fclose($fp);
  1151. $shortname = str_replace(TEST_PHP_SRCDIR . '/', '', $file);
  1152. $tested_file = $shortname;
  1153. if ($bork_info !== null) {
  1154. show_result("BORK", $bork_info, $tested_file);
  1155. $PHP_FAILED_TESTS['BORKED'][] = array (
  1156. 'name' => $file,
  1157. 'test_name' => '',
  1158. 'output' => '',
  1159. 'diff' => '',
  1160. 'info' => "$bork_info [$file]",
  1161. );
  1162. junit_mark_test_as('BORK', $shortname, $tested_file, 0, $bork_info);
  1163. return 'BORKED';
  1164. }
  1165. if (isset($section_text['CAPTURE_STDIO'])) {
  1166. $captureStdIn = stripos($section_text['CAPTURE_STDIO'], 'STDIN') !== false;
  1167. $captureStdOut = stripos($section_text['CAPTURE_STDIO'], 'STDOUT') !== false;
  1168. $captureStdErr = stripos($section_text['CAPTURE_STDIO'], 'STDERR') !== false;
  1169. } else {
  1170. $captureStdIn = true;
  1171. $captureStdOut = true;
  1172. $captureStdErr = true;
  1173. }
  1174. if ($captureStdOut && $captureStdErr) {
  1175. $cmdRedirect = ' 2>&1';
  1176. } else {
  1177. $cmdRedirect = '';
  1178. }
  1179. $tested = trim($section_text['TEST']);
  1180. /* For GET/POST/PUT tests, check if cgi sapi is available and if it is, use it. */
  1181. if (array_key_exists('CGI', $section_text) || !empty($section_text['GET']) || !empty($section_text['POST']) || !empty($section_text['GZIP_POST']) || !empty($section_text['DEFLATE_POST']) || !empty($section_text['POST_RAW']) || !empty($section_text['PUT']) || !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) {
  1182. if (isset($php_cgi)) {
  1183. $php = $php_cgi . ' -C ';
  1184. } else if (!strncasecmp(PHP_OS, "win", 3) && file_exists(dirname($php) . "/php-cgi.exe")) {
  1185. $php = realpath(dirname($php) . "/php-cgi.exe") . ' -C ';
  1186. } else {
  1187. if (file_exists(dirname($php) . "/../../sapi/cgi/php-cgi")) {
  1188. $php = realpath(dirname($php) . "/../../sapi/cgi/php-cgi") . ' -C ';
  1189. } else if (file_exists("./sapi/cgi/php-cgi")) {
  1190. $php = realpath("./sapi/cgi/php-cgi") . ' -C ';
  1191. } else if (file_exists(dirname($php) . "/php-cgi")) {
  1192. $php = realpath(dirname($php) . "/php-cgi") . ' -C ';
  1193. } else {
  1194. show_result('SKIP', $tested, $tested_file, "reason: CGI not available");
  1195. junit_init_suite(junit_get_suitename_for($shortname));
  1196. junit_mark_test_as('SKIP', $shortname, $tested, 0, 'CGI not available');
  1197. return 'SKIPPED';
  1198. }
  1199. }
  1200. $uses_cgi = true;
  1201. }
  1202. /* For phpdbg tests, check if phpdbg sapi is available and if it is, use it. */
  1203. if (array_key_exists('PHPDBG', $section_text)) {
  1204. if (!isset($section_text['STDIN'])) {
  1205. $section_text['STDIN'] = $section_text['PHPDBG']."\n";
  1206. }
  1207. if (isset($phpdbg)) {
  1208. $php = $phpdbg . ' -qIb';
  1209. } else {
  1210. show_result('SKIP', $tested, $tested_file, "reason: phpdbg not available");
  1211. junit_init_suite(junit_get_suitename_for($shortname));
  1212. junit_mark_test_as('SKIP', $shortname, $tested, 0, 'phpdbg not available');
  1213. return 'SKIPPED';
  1214. }
  1215. }
  1216. if (!$SHOW_ONLY_GROUPS) {
  1217. show_test($test_idx, $shortname);
  1218. }
  1219. if (is_array($IN_REDIRECT)) {
  1220. $temp_dir = $test_dir = $IN_REDIRECT['dir'];
  1221. } else {
  1222. $temp_dir = $test_dir = realpath(dirname($file));
  1223. }
  1224. if ($temp_source && $temp_target) {
  1225. $temp_dir = str_replace($temp_source, $temp_target, $temp_dir);
  1226. }
  1227. $main_file_name = basename($file,'phpt');
  1228. $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'diff';
  1229. $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'log';
  1230. $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'exp';
  1231. $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'out';
  1232. $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'mem';
  1233. $sh_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'sh';
  1234. $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'php';
  1235. $test_file = $test_dir . DIRECTORY_SEPARATOR . $main_file_name . 'php';
  1236. $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'skip.php';
  1237. $test_skipif = $test_dir . DIRECTORY_SEPARATOR . $main_file_name . 'skip.php';
  1238. $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'clean.php';
  1239. $test_clean = $test_dir . DIRECTORY_SEPARATOR . $main_file_name . 'clean.php';
  1240. $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('/phpt.');
  1241. $tmp_relative_file = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $test_file) . 't';
  1242. if ($temp_source && $temp_target) {
  1243. $temp_skipif .= 's';
  1244. $temp_file .= 's';
  1245. $temp_clean .= 's';
  1246. $copy_file = $temp_dir . DIRECTORY_SEPARATOR . basename(is_array($file) ? $file[1] : $file) . '.phps';
  1247. if (!is_dir(dirname($copy_file))) {
  1248. mkdir(dirname($copy_file), 0777, true) or error("Cannot create output directory - " . dirname($copy_file));
  1249. }
  1250. if (isset($section_text['FILE'])) {
  1251. save_text($copy_file, $section_text['FILE']);
  1252. }
  1253. $temp_filenames = array(
  1254. 'file' => $copy_file,
  1255. 'diff' => $diff_filename,
  1256. 'log' => $log_filename,
  1257. 'exp' => $exp_filename,
  1258. 'out' => $output_filename,
  1259. 'mem' => $memcheck_filename,
  1260. 'sh' => $sh_filename,
  1261. 'php' => $temp_file,
  1262. 'skip' => $temp_skipif,
  1263. 'clean'=> $temp_clean);
  1264. }
  1265. if (is_array($IN_REDIRECT)) {
  1266. $tested = $IN_REDIRECT['prefix'] . ' ' . trim($section_text['TEST']);
  1267. $tested_file = $tmp_relative_file;
  1268. }
  1269. // unlink old test results
  1270. @unlink($diff_filename);
  1271. @unlink($log_filename);
  1272. @unlink($exp_filename);
  1273. @unlink($output_filename);
  1274. @unlink($memcheck_filename);
  1275. @unlink($sh_filename);
  1276. @unlink($temp_file);
  1277. @unlink($test_file);
  1278. @unlink($temp_skipif);
  1279. @unlink($test_skipif);
  1280. @unlink($tmp_post);
  1281. @unlink($temp_clean);
  1282. @unlink($test_clean);
  1283. // Reset environment from any previous test.
  1284. $env['REDIRECT_STATUS'] = '';
  1285. $env['QUERY_STRING'] = '';
  1286. $env['PATH_TRANSLATED'] = '';
  1287. $env['SCRIPT_FILENAME'] = '';
  1288. $env['REQUEST_METHOD'] = '';
  1289. $env['CONTENT_TYPE'] = '';
  1290. $env['CONTENT_LENGTH'] = '';
  1291. $env['TZ'] = '';
  1292. if (!empty($section_text['ENV'])) {
  1293. foreach(explode("\n", trim($section_text['ENV'])) as $e) {
  1294. $e = explode('=', trim($e), 2);
  1295. if (!empty($e[0]) && isset($e[1])) {
  1296. $env[$e[0]] = $e[1];
  1297. }
  1298. }
  1299. }
  1300. // Default ini settings
  1301. $ini_settings = array();
  1302. // Additional required extensions
  1303. if (array_key_exists('EXTENSIONS', $section_text)) {
  1304. $ext_params = array();
  1305. settings2array($ini_overwrites, $ext_params);
  1306. settings2params($ext_params);
  1307. $ext_dir=`$php $pass_options $ext_params -d display_errors=0 -r "echo ini_get('extension_dir');"`;
  1308. $extensions = preg_split("/[\n\r]+/", trim($section_text['EXTENSIONS']));
  1309. $loaded = explode(",", `$php $pass_options $ext_params -d display_errors=0 -r "echo implode(',', get_loaded_extensions());"`);
  1310. $ext_prefix = substr(PHP_OS, 0, 3) === "WIN" ? "php_" : "";
  1311. foreach ($extensions as $req_ext) {
  1312. if (!in_array($req_ext, $loaded)) {
  1313. if ($req_ext == 'opcache') {
  1314. $ini_settings['zend_extension'][] = $ext_dir . DIRECTORY_SEPARATOR . $ext_prefix . $req_ext . '.' . PHP_SHLIB_SUFFIX;
  1315. } else {
  1316. $ini_settings['extension'][] = $ext_dir . DIRECTORY_SEPARATOR . $ext_prefix . $req_ext . '.' . PHP_SHLIB_SUFFIX;
  1317. }
  1318. }
  1319. }
  1320. }
  1321. // additional ini overwrites
  1322. //$ini_overwrites[] = 'setting=value';
  1323. settings2array($ini_overwrites, $ini_settings);
  1324. // Any special ini settings
  1325. // these may overwrite the test defaults...
  1326. if (array_key_exists('INI', $section_text)) {
  1327. $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']);
  1328. $section_text['INI'] = str_replace('{TMP}', sys_get_temp_dir(), $section_text['INI']);
  1329. settings2array(preg_split( "/[\n\r]+/", $section_text['INI']), $ini_settings);
  1330. }
  1331. settings2params($ini_settings);
  1332. $env['TEST_PHP_EXTRA_ARGS'] = $pass_options . ' ' . $ini_settings;
  1333. // Check if test should be skipped.
  1334. $info = '';
  1335. $warn = false;
  1336. if (array_key_exists('SKIPIF', $section_text)) {
  1337. if (trim($section_text['SKIPIF'])) {
  1338. show_file_block('skip', $section_text['SKIPIF']);
  1339. save_text($test_skipif, $section_text['SKIPIF'], $temp_skipif);
  1340. $extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  1341. "unset REQUEST_METHOD; unset QUERY_STRING; unset PATH_TRANSLATED; unset SCRIPT_FILENAME; unset REQUEST_METHOD;": "";
  1342. if ($valgrind) {
  1343. $env['USE_ZEND_ALLOC'] = '0';
  1344. $env['ZEND_DONT_UNLOAD_MODULES'] = 1;
  1345. } else {
  1346. $env['USE_ZEND_ALLOC'] = '1';
  1347. $env['ZEND_DONT_UNLOAD_MODULES'] = 0;
  1348. }
  1349. junit_start_timer($shortname);
  1350. $output = system_with_timeout("$extra $php $pass_options -q $ini_settings $no_file_cache -d display_errors=0 \"$test_skipif\"", $env);
  1351. junit_finish_timer($shortname);
  1352. if (!$cfg['keep']['skip']) {
  1353. @unlink($test_skipif);
  1354. }
  1355. if (!strncasecmp('skip', ltrim($output), 4)) {
  1356. if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) {
  1357. show_result('SKIP', $tested, $tested_file, "reason: $m[1]", $temp_filenames);
  1358. } else {
  1359. show_result('SKIP', $tested, $tested_file, '', $temp_filenames);
  1360. }
  1361. if (!$cfg['keep']['skip']) {
  1362. @unlink($test_skipif);
  1363. }
  1364. $message = !empty($m[1]) ? $m[1] : '';
  1365. junit_mark_test_as('SKIP', $shortname, $tested, null, $message);
  1366. return 'SKIPPED';
  1367. }
  1368. if (!strncasecmp('info', ltrim($output), 4)) {
  1369. if (preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) {
  1370. $info = " (info: $m[1])";
  1371. }
  1372. }
  1373. if (!strncasecmp('warn', ltrim($output), 4)) {
  1374. if (preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) {
  1375. $warn = true; /* only if there is a reason */
  1376. $info = " (warn: $m[1])";
  1377. }
  1378. }
  1379. if (!strncasecmp('xfail', ltrim($output), 5)) {
  1380. // Pretend we have an XFAIL section
  1381. $section_text['XFAIL'] = trim(substr(ltrim($output), 5));
  1382. }
  1383. }
  1384. }
  1385. if (!extension_loaded("zlib")
  1386. && ( array_key_exists("GZIP_POST", $section_text)
  1387. || array_key_exists("DEFLATE_POST", $section_text))
  1388. ) {
  1389. $message = "ext/zlib required";
  1390. show_result('SKIP', $tested, $tested_file, "reason: $message", $temp_filenames);
  1391. junit_mark_test_as('SKIP', $shortname, $tested, null, $message);
  1392. return 'SKIPPED';
  1393. }
  1394. if (isset($section_text['REDIRECTTEST'])) {
  1395. $test_files = array();
  1396. $IN_REDIRECT = eval($section_text['REDIRECTTEST']);
  1397. $IN_REDIRECT['via'] = "via [$shortname]\n\t";
  1398. $IN_REDIRECT['dir'] = realpath(dirname($file));
  1399. $IN_REDIRECT['prefix'] = trim($section_text['TEST']);
  1400. if (!empty($IN_REDIRECT['TESTS'])) {
  1401. if (is_array($org_file)) {
  1402. $test_files[] = $org_file[1];
  1403. } else {
  1404. $GLOBALS['test_files'] = $test_files;
  1405. find_files($IN_REDIRECT['TESTS']);
  1406. foreach($GLOBALS['test_files'] as $f) {
  1407. $test_files[] = array($f, $file);
  1408. }
  1409. }
  1410. $test_cnt += @count($test_files) - 1;
  1411. $test_idx--;
  1412. show_redirect_start($IN_REDIRECT['TESTS'], $tested, $tested_file);
  1413. // set up environment
  1414. $redirenv = array_merge($environment, $IN_REDIRECT['ENV']);
  1415. $redirenv['REDIR_TEST_DIR'] = realpath($IN_REDIRECT['TESTS']) . DIRECTORY_SEPARATOR;
  1416. usort($test_files, "test_sort");
  1417. run_all_tests($test_files, $redirenv, $tested);
  1418. show_redirect_ends($IN_REDIRECT['TESTS'], $tested, $tested_file);
  1419. // a redirected test never fails
  1420. $IN_REDIRECT = false;
  1421. junit_mark_test_as('PASS', $shortname, $tested);
  1422. return 'REDIR';
  1423. } else {
  1424. $bork_info = "Redirect info must contain exactly one TEST string to be used as redirect directory.";
  1425. show_result("BORK", $bork_info, '', $temp_filenames);
  1426. $PHP_FAILED_TESTS['BORKED'][] = array (
  1427. 'name' => $file,
  1428. 'test_name' => '',
  1429. 'output' => '',
  1430. 'diff' => '',
  1431. 'info' => "$bork_info [$file]",
  1432. );
  1433. }
  1434. }
  1435. if (is_array($org_file) || @count($section_text['REDIRECTTEST']) == 1) {
  1436. if (is_array($org_file)) {
  1437. $file = $org_file[0];
  1438. }
  1439. $bork_info = "Redirected test did not contain redirection info";
  1440. show_result("BORK", $bork_info, '', $temp_filenames);
  1441. $PHP_FAILED_TESTS['BORKED'][] = array (
  1442. 'name' => $file,
  1443. 'test_name' => '',
  1444. 'output' => '',
  1445. 'diff' => '',
  1446. 'info' => "$bork_info [$file]",
  1447. );
  1448. junit_mark_test_as('BORK', $shortname, $tested, null, $bork_info);
  1449. return 'BORKED';
  1450. }
  1451. // We've satisfied the preconditions - run the test!
  1452. if (isset($section_text['FILE'])) {
  1453. show_file_block('php', $section_text['FILE'], 'TEST');
  1454. save_text($test_file, $section_text['FILE'], $temp_file);
  1455. } else {
  1456. $test_file = $temp_file = "";
  1457. }
  1458. if (array_key_exists('GET', $section_text)) {
  1459. $query_string = trim($section_text['GET']);
  1460. } else {
  1461. $query_string = '';
  1462. }
  1463. $env['REDIRECT_STATUS'] = '1';
  1464. if (empty($env['QUERY_STRING'])) {
  1465. $env['QUERY_STRING'] = $query_string;
  1466. }
  1467. if (empty($env['PATH_TRANSLATED'])) {
  1468. $env['PATH_TRANSLATED'] = $test_file;
  1469. }
  1470. if (empty($env['SCRIPT_FILENAME'])) {
  1471. $env['SCRIPT_FILENAME'] = $test_file;
  1472. }
  1473. if (array_key_exists('COOKIE', $section_text)) {
  1474. $env['HTTP_COOKIE'] = trim($section_text['COOKIE']);
  1475. } else {
  1476. $env['HTTP_COOKIE'] = '';
  1477. }
  1478. $args = isset($section_text['ARGS']) ? ' -- ' . $section_text['ARGS'] : '';
  1479. if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) {
  1480. $post = trim($section_text['POST_RAW']);
  1481. $raw_lines = explode("\n", $post);
  1482. $request = '';
  1483. $started = false;
  1484. foreach ($raw_lines as $line) {
  1485. if (empty($env['CONTENT_TYPE']) && preg_match('/^Content-Type:(.*)/i', $line, $res)) {
  1486. $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1]));
  1487. continue;
  1488. }
  1489. if ($started) {
  1490. $request .= "\n";
  1491. }
  1492. $started = true;
  1493. $request .= $line;
  1494. }
  1495. $env['CONTENT_LENGTH'] = strlen($request);
  1496. $env['REQUEST_METHOD'] = 'POST';
  1497. if (empty($request)) {
  1498. junit_mark_test_as('BORK', $shortname, $tested, null, 'empty $request');
  1499. return 'BORKED';
  1500. }
  1501. save_text($tmp_post, $request);
  1502. $cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
  1503. } elseif (array_key_exists('PUT', $section_text) && !empty($section_text['PUT'])) {
  1504. $post = trim($section_text['PUT']);
  1505. $raw_lines = explode("\n", $post);
  1506. $request = '';
  1507. $started = false;
  1508. foreach ($raw_lines as $line) {
  1509. if (empty($env['CONTENT_TYPE']) && preg_match('/^Content-Type:(.*)/i', $line, $res)) {
  1510. $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1]));
  1511. continue;
  1512. }
  1513. if ($started) {
  1514. $request .= "\n";
  1515. }
  1516. $started = true;
  1517. $request .= $line;
  1518. }
  1519. $env['CONTENT_LENGTH'] = strlen($request);
  1520. $env['REQUEST_METHOD'] = 'PUT';
  1521. if (empty($request)) {
  1522. junit_mark_test_as('BORK', $shortname, $tested, null, 'empty $request');
  1523. return 'BORKED';
  1524. }
  1525. save_text($tmp_post, $request);
  1526. $cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
  1527. } else if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
  1528. $post = trim($section_text['POST']);
  1529. $content_length = strlen($post);
  1530. save_text($tmp_post, $post);
  1531. $env['REQUEST_METHOD'] = 'POST';
  1532. if (empty($env['CONTENT_TYPE'])) {
  1533. $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  1534. }
  1535. if (empty($env['CONTENT_LENGTH'])) {
  1536. $env['CONTENT_LENGTH'] = $content_length;
  1537. }
  1538. $cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
  1539. } else if (array_key_exists('GZIP_POST', $section_text) && !empty($section_text['GZIP_POST'])) {
  1540. $post = trim($section_text['GZIP_POST']);
  1541. $post = gzencode($post, 9, FORCE_GZIP);
  1542. $env['HTTP_CONTENT_ENCODING'] = 'gzip';
  1543. save_text($tmp_post, $post);
  1544. $content_length = strlen($post);
  1545. $env['REQUEST_METHOD'] = 'POST';
  1546. $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  1547. $env['CONTENT_LENGTH'] = $content_length;
  1548. $cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
  1549. } else if (array_key_exists('DEFLATE_POST', $section_text) && !empty($section_text['DEFLATE_POST'])) {
  1550. $post = trim($section_text['DEFLATE_POST']);
  1551. $post = gzcompress($post, 9);
  1552. $env['HTTP_CONTENT_ENCODING'] = 'deflate';
  1553. save_text($tmp_post, $post);
  1554. $content_length = strlen($post);
  1555. $env['REQUEST_METHOD'] = 'POST';
  1556. $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  1557. $env['CONTENT_LENGTH'] = $content_length;
  1558. $cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
  1559. } else {
  1560. $env['REQUEST_METHOD'] = 'GET';
  1561. $env['CONTENT_TYPE'] = '';
  1562. $env['CONTENT_LENGTH'] = '';
  1563. $cmd = "$php $pass_options $ini_settings -f \"$test_file\" $args$cmdRedirect";
  1564. }
  1565. if ($valgrind) {
  1566. $env['USE_ZEND_ALLOC'] = '0';
  1567. $env['ZEND_DONT_UNLOAD_MODULES'] = 1;
  1568. $cmd = $valgrind->wrapCommand($cmd, $memcheck_filename, strpos($test_file, "pcre") !== false);
  1569. } else {
  1570. $env['USE_ZEND_ALLOC'] = '1';
  1571. $env['ZEND_DONT_UNLOAD_MODULES'] = 0;
  1572. }
  1573. if ($DETAILED) echo "
  1574. CONTENT_LENGTH = " . $env['CONTENT_LENGTH'] . "
  1575. CONTENT_TYPE = " . $env['CONTENT_TYPE'] . "
  1576. PATH_TRANSLATED = " . $env['PATH_TRANSLATED'] . "
  1577. QUERY_STRING = " . $env['QUERY_STRING'] . "
  1578. REDIRECT_STATUS = " . $env['REDIRECT_STATUS'] . "
  1579. REQUEST_METHOD = " . $env['REQUEST_METHOD'] . "
  1580. SCRIPT_FILENAME = " . $env['SCRIPT_FILENAME'] . "
  1581. HTTP_COOKIE = " . $env['HTTP_COOKIE'] . "
  1582. COMMAND $cmd
  1583. ";
  1584. junit_start_timer($shortname);
  1585. $hrtime = hrtime();
  1586. $startTime = $hrtime[0]*1000000000 + $hrtime[1];
  1587. $out = system_with_timeout($cmd, $env, $section_text['STDIN'] ?? null, $captureStdIn, $captureStdOut, $captureStdErr);
  1588. junit_finish_timer($shortname);
  1589. $hrtime = hrtime();
  1590. $time = $hrtime[0]*1000000000 + $hrtime[1] - $startTime;
  1591. if ($time >= $slow_min_ms * 1000000) {
  1592. $PHP_FAILED_TESTS['SLOW'][] = array(
  1593. 'name' => $file,
  1594. 'test_name' => (is_array($IN_REDIRECT) ? $IN_REDIRECT['via'] : '') . $tested . " [$tested_file]",
  1595. 'output' => '',
  1596. 'diff' => '',
  1597. 'info' => $time / 1000000000,
  1598. );
  1599. }
  1600. if (array_key_exists('CLEAN', $section_text) && (!$no_clean || $cfg['keep']['clean'])) {
  1601. if (trim($section_text['CLEAN'])) {
  1602. show_file_block('clean', $section_text['CLEAN']);
  1603. save_text($test_clean, trim($section_text['CLEAN']), $temp_clean);
  1604. if (!$no_clean) {
  1605. $clean_params = array();
  1606. settings2array($ini_overwrites, $clean_params);
  1607. settings2params($clean_params);
  1608. $extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  1609. "unset REQUEST_METHOD; unset QUERY_STRING; unset PATH_TRANSLATED; unset SCRIPT_FILENAME; unset REQUEST_METHOD;": "";
  1610. system_with_timeout("$extra $php $pass_options -q $clean_params $no_file_cache \"$test_clean\"", $env);
  1611. }
  1612. if (!$cfg['keep']['clean']) {
  1613. @unlink($test_clean);
  1614. }
  1615. }
  1616. }
  1617. $leaked = false;
  1618. $passed = false;
  1619. if ($valgrind) { // leak check
  1620. $leaked = filesize($memcheck_filename) > 0;
  1621. if (!$leaked) {
  1622. @unlink($memcheck_filename);
  1623. }
  1624. }
  1625. // Does the output match what is expected?
  1626. $output = preg_replace("/\r\n/", "\n", trim($out));
  1627. /* when using CGI, strip the headers from the output */
  1628. $headers = array();
  1629. if (!empty($uses_cgi) && preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $out, $match)) {
  1630. $output = trim($match[2]);
  1631. $rh = preg_split("/[\n\r]+/", $match[1]);
  1632. foreach ($rh as $line) {
  1633. if (strpos($line, ':') !== false) {
  1634. $line = explode(':', $line, 2);
  1635. $headers[trim($line[0])] = trim($line[1]);
  1636. }
  1637. }
  1638. }
  1639. $failed_headers = false;
  1640. if (isset($section_text['EXPECTHEADERS'])) {
  1641. $want = array();
  1642. $wanted_headers = array();
  1643. $lines = preg_split("/[\n\r]+/", $section_text['EXPECTHEADERS']);
  1644. foreach($lines as $line) {
  1645. if (strpos($line, ':') !== false) {
  1646. $line = explode(':', $line, 2);
  1647. $want[trim($line[0])] = trim($line[1]);
  1648. $wanted_headers[] = trim($line[0]) . ': ' . trim($line[1]);
  1649. }
  1650. }
  1651. $output_headers = array();
  1652. foreach($want as $k => $v) {
  1653. if (isset($headers[$k])) {
  1654. $output_headers[] = $k . ': ' . $headers[$k];
  1655. }
  1656. if (!isset($headers[$k]) || $headers[$k] != $v) {
  1657. $failed_headers = true;
  1658. }
  1659. }
  1660. ksort($wanted_headers);
  1661. $wanted_headers = implode("\n", $wanted_headers);
  1662. ksort($output_headers);
  1663. $output_headers = implode("\n", $output_headers);
  1664. }
  1665. show_file_block('out', $output);
  1666. if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
  1667. if (isset($section_text['EXPECTF'])) {
  1668. $wanted = trim($section_text['EXPECTF']);
  1669. } else {
  1670. $wanted = trim($section_text['EXPECTREGEX']);
  1671. }
  1672. show_file_block('exp', $wanted);
  1673. $wanted_re = preg_replace('/\r\n/', "\n", $wanted);
  1674. if (isset($section_text['EXPECTF'])) {
  1675. // do preg_quote, but miss out any %r delimited sections
  1676. $temp = "";
  1677. $r = "%r";
  1678. $startOffset = 0;
  1679. $length = strlen($wanted_re);
  1680. while($startOffset < $length) {
  1681. $start = strpos($wanted_re, $r, $startOffset);
  1682. if ($start !== false) {
  1683. // we have found a start tag
  1684. $end = strpos($wanted_re, $r, $start+2);
  1685. if ($end === false) {
  1686. // unbalanced tag, ignore it.
  1687. $end = $start = $length;
  1688. }
  1689. } else {
  1690. // no more %r sections
  1691. $start = $end = $length;
  1692. }
  1693. // quote a non re portion of the string
  1694. $temp .= preg_quote(substr($wanted_re, $startOffset, $start - $startOffset), '/');
  1695. // add the re unquoted.
  1696. if ($end > $start) {
  1697. $temp .= '(' . substr($wanted_re, $start+2, $end - $start-2). ')';
  1698. }
  1699. $startOffset = $end + 2;
  1700. }
  1701. $wanted_re = $temp;
  1702. // Stick to basics
  1703. $wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
  1704. $wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
  1705. $wanted_re = str_replace('%S', '[^\r\n]*', $wanted_re);
  1706. $wanted_re = str_replace('%a', '.+', $wanted_re);
  1707. $wanted_re = str_replace('%A', '.*', $wanted_re);
  1708. $wanted_re = str_replace('%w', '\s*', $wanted_re);
  1709. $wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
  1710. $wanted_re = str_replace('%d', '\d+', $wanted_re);
  1711. $wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
  1712. $wanted_re = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $wanted_re);
  1713. $wanted_re = str_replace('%c', '.', $wanted_re);
  1714. // %f allows two points "-.0.0" but that is the best *simple* expression
  1715. }
  1716. /* DEBUG YOUR REGEX HERE
  1717. var_dump($wanted_re);
  1718. print(str_repeat('=', 80) . "\n");
  1719. var_dump($output);
  1720. */
  1721. if (preg_match("/^$wanted_re\$/s", $output)) {
  1722. $passed = true;
  1723. if (!$cfg['keep']['php']) {
  1724. @unlink($test_file);
  1725. }
  1726. if (!$leaked && !$failed_headers) {
  1727. if (isset($section_text['XFAIL'] )) {
  1728. $warn = true;
  1729. $info = " (warn: XFAIL section but test passes)";
  1730. }else {
  1731. show_result("PASS", $tested, $tested_file, '', $temp_filenames);
  1732. junit_mark_test_as('PASS', $shortname, $tested);
  1733. return 'PASSED';
  1734. }
  1735. }
  1736. }
  1737. } else {
  1738. $wanted = trim($section_text['EXPECT']);
  1739. $wanted = preg_replace('/\r\n/',"\n", $wanted);
  1740. show_file_block('exp', $wanted);
  1741. // compare and leave on success
  1742. if (!strcmp($output, $wanted)) {
  1743. $passed = true;
  1744. if (!$cfg['keep']['php']) {
  1745. @unlink($test_file);
  1746. }
  1747. if (!$leaked && !$failed_headers) {
  1748. if (isset($section_text['XFAIL'] )) {
  1749. $warn = true;
  1750. $info = " (warn: XFAIL section but test passes)";
  1751. }else {
  1752. show_result("PASS", $tested, $tested_file, '', $temp_filenames);
  1753. junit_mark_test_as('PASS', $shortname, $tested);
  1754. return 'PASSED';
  1755. }
  1756. }
  1757. }
  1758. $wanted_re = null;
  1759. }
  1760. // Test failed so we need to report details.
  1761. if ($failed_headers) {
  1762. $passed = false;
  1763. $wanted = $wanted_headers . "\n--HEADERS--\n" . $wanted;
  1764. $output = $output_headers . "\n--HEADERS--\n" . $output;
  1765. if (isset($wanted_re)) {
  1766. $wanted_re = preg_quote($wanted_headers . "\n--HEADERS--\n", '/') . $wanted_re;
  1767. }
  1768. }
  1769. if ($leaked) {
  1770. $restype[] = 'LEAK';
  1771. }
  1772. if ($warn) {
  1773. $restype[] = 'WARN';
  1774. }
  1775. if ($passed) {
  1776. @unlink($tmp_post);
  1777. }
  1778. if (!$passed) {
  1779. if (isset($section_text['XFAIL'])) {
  1780. $restype[] = 'XFAIL';
  1781. $info = ' XFAIL REASON: ' . rtrim($section_text['XFAIL']);
  1782. } else {
  1783. $restype[] = 'FAIL';
  1784. }
  1785. }
  1786. if (!$passed) {
  1787. // write .exp
  1788. if (strpos($log_format, 'E') !== false && file_put_contents($exp_filename, $wanted, FILE_BINARY) === false) {
  1789. error("Cannot create expected test output - $exp_filename");
  1790. }
  1791. // write .out
  1792. if (strpos($log_format, 'O') !== false && file_put_contents($output_filename, $output, FILE_BINARY) === false) {
  1793. error("Cannot create test output - $output_filename");
  1794. }
  1795. // write .diff
  1796. $diff = generate_diff($wanted, $wanted_re, $output);
  1797. if (is_array($IN_REDIRECT)) {
  1798. $diff = "# original source file: $shortname\n" . $diff;
  1799. }
  1800. show_file_block('diff', $diff);
  1801. if (strpos($log_format, 'D') !== false && file_put_contents($diff_filename, $diff, FILE_BINARY) === false) {
  1802. error("Cannot create test diff - $diff_filename");
  1803. }
  1804. // write .sh
  1805. if (strpos($log_format, 'S') !== false && file_put_contents($sh_filename, "#!/bin/sh
  1806. {$cmd}
  1807. ", FILE_BINARY) === false) {
  1808. error("Cannot create test shell script - $sh_filename");
  1809. }
  1810. chmod($sh_filename, 0755);
  1811. // write .log
  1812. if (strpos($log_format, 'L') !== false && file_put_contents($log_filename, "
  1813. ---- EXPECTED OUTPUT
  1814. $wanted
  1815. ---- ACTUAL OUTPUT
  1816. $output
  1817. ---- FAILED
  1818. ", FILE_BINARY) === false) {
  1819. error("Cannot create test log - $log_filename");
  1820. error_report($file, $log_filename, $tested);
  1821. }
  1822. }
  1823. if ($valgrind && $leaked && $cfg["show"]["mem"]) {
  1824. show_file_block('mem', file_get_contents($memcheck_filename));
  1825. }
  1826. show_result(implode('&', $restype), $tested, $tested_file, $info, $temp_filenames);
  1827. foreach ($restype as $type) {
  1828. $PHP_FAILED_TESTS[$type.'ED'][] = array (
  1829. 'name' => $file,
  1830. 'test_name' => (is_array($IN_REDIRECT) ? $IN_REDIRECT['via'] : '') . $tested . " [$tested_file]",
  1831. 'output' => $output_filename,
  1832. 'diff' => $diff_filename,
  1833. 'info' => $info,
  1834. );
  1835. }
  1836. $diff = empty($diff) ? '' : preg_replace('/\e/', '<esc>', $diff);
  1837. junit_mark_test_as($restype, str_replace(TEST_PHP_SRCDIR . '/', '', $tested_file), $tested, null, $info, $diff);
  1838. return $restype[0] . 'ED';
  1839. }
  1840. function comp_line($l1, $l2, $is_reg)
  1841. {
  1842. if ($is_reg) {
  1843. return preg_match('/^'. $l1 . '$/s', $l2);
  1844. } else {
  1845. return !strcmp($l1, $l2);
  1846. }
  1847. }
  1848. function count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $idx2, $cnt1, $cnt2, $steps)
  1849. {
  1850. $equal = 0;
  1851. while ($idx1 < $cnt1 && $idx2 < $cnt2 && comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
  1852. $idx1++;
  1853. $idx2++;
  1854. $equal++;
  1855. $steps--;
  1856. }
  1857. if (--$steps > 0) {
  1858. $eq1 = 0;
  1859. $st = $steps / 2;
  1860. for ($ofs1 = $idx1 + 1; $ofs1 < $cnt1 && $st-- > 0; $ofs1++) {
  1861. $eq = @count_array_diff($ar1, $ar2, $is_reg, $w, $ofs1, $idx2, $cnt1, $cnt2, $st);
  1862. if ($eq > $eq1) {
  1863. $eq1 = $eq;
  1864. }
  1865. }
  1866. $eq2 = 0;
  1867. $st = $steps;
  1868. for ($ofs2 = $idx2 + 1; $ofs2 < $cnt2 && $st-- > 0; $ofs2++) {
  1869. $eq = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $ofs2, $cnt1, $cnt2, $st);
  1870. if ($eq > $eq2) {
  1871. $eq2 = $eq;
  1872. }
  1873. }
  1874. if ($eq1 > $eq2) {
  1875. $equal += $eq1;
  1876. } else if ($eq2 > 0) {
  1877. $equal += $eq2;
  1878. }
  1879. }
  1880. return $equal;
  1881. }
  1882. function generate_array_diff($ar1, $ar2, $is_reg, $w)
  1883. {
  1884. $idx1 = 0; $cnt1 = @count($ar1);
  1885. $idx2 = 0; $cnt2 = @count($ar2);
  1886. $diff = array();
  1887. $old1 = array();
  1888. $old2 = array();
  1889. while ($idx1 < $cnt1 && $idx2 < $cnt2) {
  1890. if (comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
  1891. $idx1++;
  1892. $idx2++;
  1893. continue;
  1894. } else {
  1895. $c1 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1+1, $idx2, $cnt1, $cnt2, 10);
  1896. $c2 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $idx2+1, $cnt1, $cnt2, 10);
  1897. if ($c1 > $c2) {
  1898. $old1[$idx1] = sprintf("%03d- ", $idx1+1) . $w[$idx1++];
  1899. } else if ($c2 > 0) {
  1900. $old2[$idx2] = sprintf("%03d+ ", $idx2+1) . $ar2[$idx2++];
  1901. } else {
  1902. $old1[$idx1] = sprintf("%03d- ", $idx1+1) . $w[$idx1++];
  1903. $old2[$idx2] = sprintf("%03d+ ", $idx2+1) . $ar2[$idx2++];
  1904. }
  1905. }
  1906. }
  1907. reset($old1); $k1 = key($old1); $l1 = -2;
  1908. reset($old2); $k2 = key($old2); $l2 = -2;
  1909. while ($k1 !== null || $k2 !== null) {
  1910. if ($k1 == $l1 + 1 || $k2 === null) {
  1911. $l1 = $k1;
  1912. $diff[] = current($old1);
  1913. $k1 = next($old1) ? key($old1) : null;
  1914. } else if ($k2 == $l2 + 1 || $k1 === null) {
  1915. $l2 = $k2;
  1916. $diff[] = current($old2);
  1917. $k2 = next($old2) ? key($old2) : null;
  1918. } else if ($k1 < $k2) {
  1919. $l1 = $k1;
  1920. $diff[] = current($old1);
  1921. $k1 = next($old1) ? key($old1) : null;
  1922. } else {
  1923. $l2 = $k2;
  1924. $diff[] = current($old2);
  1925. $k2 = next($old2) ? key($old2) : null;
  1926. }
  1927. }
  1928. while ($idx1 < $cnt1) {
  1929. $diff[] = sprintf("%03d- ", $idx1 + 1) . $w[$idx1++];
  1930. }
  1931. while ($idx2 < $cnt2) {
  1932. $diff[] = sprintf("%03d+ ", $idx2 + 1) . $ar2[$idx2++];
  1933. }
  1934. return $diff;
  1935. }
  1936. function generate_diff($wanted, $wanted_re, $output)
  1937. {
  1938. $w = explode("\n", $wanted);
  1939. $o = explode("\n", $output);
  1940. $r = is_null($wanted_re) ? $w : explode("\n", $wanted_re);
  1941. $diff = generate_array_diff($r, $o, !is_null($wanted_re), $w);
  1942. return implode("\r\n", $diff);
  1943. }
  1944. function error($message)
  1945. {
  1946. echo "ERROR: {$message}\n";
  1947. exit(1);
  1948. }
  1949. function settings2array($settings, &$ini_settings)
  1950. {
  1951. foreach($settings as $setting) {
  1952. if (strpos($setting, '=') !== false) {
  1953. $setting = explode("=", $setting, 2);
  1954. $name = trim($setting[0]);
  1955. $value = trim($setting[1]);
  1956. if ($name == 'extension' || $name == 'zend_extension') {
  1957. if (!isset($ini_settings[$name])) {
  1958. $ini_settings[$name] = array();
  1959. }
  1960. $ini_settings[$name][] = $value;
  1961. } else {
  1962. $ini_settings[$name] = $value;
  1963. }
  1964. }
  1965. }
  1966. }
  1967. function settings2params(&$ini_settings)
  1968. {
  1969. $settings = '';
  1970. foreach($ini_settings as $name => $value) {
  1971. if (is_array($value)) {
  1972. foreach($value as $val) {
  1973. $val = addslashes($val);
  1974. $settings .= " -d \"$name=$val\"";
  1975. }
  1976. } else {
  1977. if (substr(PHP_OS, 0, 3) == "WIN" && !empty($value) && $value{0} == '"') {
  1978. $len = strlen($value);
  1979. if ($value{$len - 1} == '"') {
  1980. $value{0} = "'";
  1981. $value{$len - 1} = "'";
  1982. }
  1983. } else {
  1984. $value = addslashes($value);
  1985. }
  1986. $settings .= " -d \"$name=$value\"";
  1987. }
  1988. }
  1989. $ini_settings = $settings;
  1990. }
  1991. function compute_summary()
  1992. {
  1993. global $n_total, $test_results, $ignored_by_ext, $sum_results, $percent_results;
  1994. $n_total = count($test_results);
  1995. $n_total += $ignored_by_ext;
  1996. $sum_results = array(
  1997. 'PASSED' => 0,
  1998. 'WARNED' => 0,
  1999. 'SKIPPED' => 0,
  2000. 'FAILED' => 0,
  2001. 'BORKED' => 0,
  2002. 'LEAKED' => 0,
  2003. 'XFAILED' => 0
  2004. );
  2005. foreach ($test_results as $v) {
  2006. $sum_results[$v]++;
  2007. }
  2008. $sum_results['SKIPPED'] += $ignored_by_ext;
  2009. $percent_results = array();
  2010. foreach ($sum_results as $v => $n) {
  2011. $percent_results[$v] = (100.0 * $n) / $n_total;
  2012. }
  2013. }
  2014. function get_summary($show_ext_summary, $show_html)
  2015. {
  2016. global $exts_skipped, $exts_tested, $n_total, $sum_results, $percent_results, $end_time, $start_time, $failed_test_summary, $PHP_FAILED_TESTS, $valgrind;
  2017. $x_total = $n_total - $sum_results['SKIPPED'] - $sum_results['BORKED'];
  2018. if ($x_total) {
  2019. $x_warned = (100.0 * $sum_results['WARNED']) / $x_total;
  2020. $x_failed = (100.0 * $sum_results['FAILED']) / $x_total;
  2021. $x_xfailed = (100.0 * $sum_results['XFAILED']) / $x_total;
  2022. $x_leaked = (100.0 * $sum_results['LEAKED']) / $x_total;
  2023. $x_passed = (100.0 * $sum_results['PASSED']) / $x_total;
  2024. } else {
  2025. $x_warned = $x_failed = $x_passed = $x_leaked = $x_xfailed = 0;
  2026. }
  2027. $summary = '';
  2028. if ($show_html) {
  2029. $summary .= "<pre>\n";
  2030. }
  2031. if ($show_ext_summary) {
  2032. $summary .= '
  2033. =====================================================================
  2034. TEST RESULT SUMMARY
  2035. ---------------------------------------------------------------------
  2036. Exts skipped : ' . sprintf('%4d', $exts_skipped) . '
  2037. Exts tested : ' . sprintf('%4d', $exts_tested) . '
  2038. ---------------------------------------------------------------------
  2039. ';
  2040. }
  2041. $summary .= '
  2042. Number of tests : ' . sprintf('%4d', $n_total) . ' ' . sprintf('%8d', $x_total);
  2043. if ($sum_results['BORKED']) {
  2044. $summary .= '
  2045. Tests borked : ' . sprintf('%4d (%5.1f%%)', $sum_results['BORKED'], $percent_results['BORKED']) . ' --------';
  2046. }
  2047. $summary .= '
  2048. Tests skipped : ' . sprintf('%4d (%5.1f%%)', $sum_results['SKIPPED'], $percent_results['SKIPPED']) . ' --------
  2049. Tests warned : ' . sprintf('%4d (%5.1f%%)', $sum_results['WARNED'], $percent_results['WARNED']) . ' ' . sprintf('(%5.1f%%)', $x_warned) . '
  2050. Tests failed : ' . sprintf('%4d (%5.1f%%)', $sum_results['FAILED'], $percent_results['FAILED']) . ' ' . sprintf('(%5.1f%%)', $x_failed) . '
  2051. Expected fail : ' . sprintf('%4d (%5.1f%%)', $sum_results['XFAILED'], $percent_results['XFAILED']) . ' ' . sprintf('(%5.1f%%)', $x_xfailed);
  2052. if ($valgrind) {
  2053. $summary .= '
  2054. Tests leaked : ' . sprintf('%4d (%5.1f%%)', $sum_results['LEAKED'], $percent_results['LEAKED']) . ' ' . sprintf('(%5.1f%%)', $x_leaked);
  2055. }
  2056. $summary .= '
  2057. Tests passed : ' . sprintf('%4d (%5.1f%%)', $sum_results['PASSED'], $percent_results['PASSED']) . ' ' . sprintf('(%5.1f%%)', $x_passed) . '
  2058. ---------------------------------------------------------------------
  2059. Time taken : ' . sprintf('%4d seconds', $end_time - $start_time) . '
  2060. =====================================================================
  2061. ';
  2062. $failed_test_summary = '';
  2063. if (count($PHP_FAILED_TESTS['SLOW'])) {
  2064. usort($PHP_FAILED_TESTS['SLOW'], function($a, $b) {
  2065. return $a['info'] < $b['info'] ? 1 : -1;
  2066. });
  2067. $failed_test_summary .= '
  2068. =====================================================================
  2069. SLOW TEST SUMMARY
  2070. ---------------------------------------------------------------------
  2071. ';
  2072. foreach ($PHP_FAILED_TESTS['SLOW'] as $failed_test_data) {
  2073. $failed_test_summary .= sprintf('(%.3f s) ', $failed_test_data['info']) . $failed_test_data['test_name'] . "\n";
  2074. }
  2075. $failed_test_summary .= "=====================================================================\n";
  2076. }
  2077. if (count($PHP_FAILED_TESTS['XFAILED'])) {
  2078. $failed_test_summary .= '
  2079. =====================================================================
  2080. EXPECTED FAILED TEST SUMMARY
  2081. ---------------------------------------------------------------------
  2082. ';
  2083. foreach ($PHP_FAILED_TESTS['XFAILED'] as $failed_test_data) {
  2084. $failed_test_summary .= $failed_test_data['test_name'] . $failed_test_data['info'] . "\n";
  2085. }
  2086. $failed_test_summary .= "=====================================================================\n";
  2087. }
  2088. if (count($PHP_FAILED_TESTS['BORKED'])) {
  2089. $failed_test_summary .= '
  2090. =====================================================================
  2091. BORKED TEST SUMMARY
  2092. ---------------------------------------------------------------------
  2093. ';
  2094. foreach ($PHP_FAILED_TESTS['BORKED'] as $failed_test_data) {
  2095. $failed_test_summary .= $failed_test_data['info'] . "\n";
  2096. }
  2097. $failed_test_summary .= "=====================================================================\n";
  2098. }
  2099. if (count($PHP_FAILED_TESTS['FAILED'])) {
  2100. $failed_test_summary .= '
  2101. =====================================================================
  2102. FAILED TEST SUMMARY
  2103. ---------------------------------------------------------------------
  2104. ';
  2105. foreach ($PHP_FAILED_TESTS['FAILED'] as $failed_test_data) {
  2106. $failed_test_summary .= $failed_test_data['test_name'] . $failed_test_data['info'] . "\n";
  2107. }
  2108. $failed_test_summary .= "=====================================================================\n";
  2109. }
  2110. if (count($PHP_FAILED_TESTS['WARNED'])) {
  2111. $failed_test_summary .= '
  2112. =====================================================================
  2113. WARNED TEST SUMMARY
  2114. ---------------------------------------------------------------------
  2115. ';
  2116. foreach ($PHP_FAILED_TESTS['WARNED'] as $failed_test_data) {
  2117. $failed_test_summary .= $failed_test_data['test_name'] . $failed_test_data['info'] . "\n";
  2118. }
  2119. $failed_test_summary .= "=====================================================================\n";
  2120. }
  2121. if (count($PHP_FAILED_TESTS['LEAKED'])) {
  2122. $failed_test_summary .= '
  2123. =====================================================================
  2124. LEAKED TEST SUMMARY
  2125. ---------------------------------------------------------------------
  2126. ';
  2127. foreach ($PHP_FAILED_TESTS['LEAKED'] as $failed_test_data) {
  2128. $failed_test_summary .= $failed_test_data['test_name'] . $failed_test_data['info'] . "\n";
  2129. }
  2130. $failed_test_summary .= "=====================================================================\n";
  2131. }
  2132. if ($failed_test_summary && !getenv('NO_PHPTEST_SUMMARY')) {
  2133. $summary .= $failed_test_summary;
  2134. }
  2135. if ($show_html) {
  2136. $summary .= "</pre>";
  2137. }
  2138. return $summary;
  2139. }
  2140. function show_start($start_time)
  2141. {
  2142. global $html_output, $html_file;
  2143. if ($html_output) {
  2144. fwrite($html_file, "<h2>Time Start: " . date('Y-m-d H:i:s', $start_time) . "</h2>\n");
  2145. fwrite($html_file, "<table>\n");
  2146. }
  2147. echo "TIME START " . date('Y-m-d H:i:s', $start_time) . "\n=====================================================================\n";
  2148. }
  2149. function show_end($end_time)
  2150. {
  2151. global $html_output, $html_file;
  2152. if ($html_output) {
  2153. fwrite($html_file, "</table>\n");
  2154. fwrite($html_file, "<h2>Time End: " . date('Y-m-d H:i:s', $end_time) . "</h2>\n");
  2155. }
  2156. echo "=====================================================================\nTIME END " . date('Y-m-d H:i:s', $end_time) . "\n";
  2157. }
  2158. function show_summary()
  2159. {
  2160. global $html_output, $html_file;
  2161. if ($html_output) {
  2162. fwrite($html_file, "<hr/>\n" . get_summary(true, true));
  2163. }
  2164. echo get_summary(true, false);
  2165. }
  2166. function show_redirect_start($tests, $tested, $tested_file)
  2167. {
  2168. global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
  2169. if ($html_output) {
  2170. fwrite($html_file, "<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) begin</td></tr>\n");
  2171. }
  2172. if (!$SHOW_ONLY_GROUPS || in_array('REDIRECT', $SHOW_ONLY_GROUPS)) {
  2173. echo "REDIRECT $tests ($tested [$tested_file]) begin\n";
  2174. } else {
  2175. // Write over the last line to avoid random trailing chars on next echo
  2176. echo str_repeat(" ", $line_length), "\r";
  2177. }
  2178. }
  2179. function show_redirect_ends($tests, $tested, $tested_file)
  2180. {
  2181. global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
  2182. if ($html_output) {
  2183. fwrite($html_file, "<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) done</td></tr>\n");
  2184. }
  2185. if (!$SHOW_ONLY_GROUPS || in_array('REDIRECT', $SHOW_ONLY_GROUPS)) {
  2186. echo "REDIRECT $tests ($tested [$tested_file]) done\n";
  2187. } else {
  2188. // Write over the last line to avoid random trailing chars on next echo
  2189. echo str_repeat(" ", $line_length), "\r";
  2190. }
  2191. }
  2192. function show_test($test_idx, $shortname)
  2193. {
  2194. global $test_cnt;
  2195. global $line_length;
  2196. $str = "TEST $test_idx/$test_cnt [$shortname]\r";
  2197. $line_length = strlen($str);
  2198. echo $str;
  2199. flush();
  2200. }
  2201. function show_result($result, $tested, $tested_file, $extra = '', $temp_filenames = null)
  2202. {
  2203. global $html_output, $html_file, $temp_target, $temp_urlbase, $line_length, $SHOW_ONLY_GROUPS;
  2204. if (!$SHOW_ONLY_GROUPS || in_array($result, $SHOW_ONLY_GROUPS)) {
  2205. echo "$result $tested [$tested_file] $extra\n";
  2206. } else if (!$SHOW_ONLY_GROUPS) {
  2207. // Write over the last line to avoid random trailing chars on next echo
  2208. echo str_repeat(" ", $line_length), "\r";
  2209. }
  2210. if ($html_output) {
  2211. if (isset($temp_filenames['file']) && file_exists($temp_filenames['file'])) {
  2212. $url = str_replace($temp_target, $temp_urlbase, $temp_filenames['file']);
  2213. $tested = "<a href='$url'>$tested</a>";
  2214. }
  2215. if (isset($temp_filenames['skip']) && file_exists($temp_filenames['skip'])) {
  2216. if (empty($extra)) {
  2217. $extra = "skipif";
  2218. }
  2219. $url = str_replace($temp_target, $temp_urlbase, $temp_filenames['skip']);
  2220. $extra = "<a href='$url'>$extra</a>";
  2221. } else if (empty($extra)) {
  2222. $extra = "&nbsp;";
  2223. }
  2224. if (isset($temp_filenames['diff']) && file_exists($temp_filenames['diff'])) {
  2225. $url = str_replace($temp_target, $temp_urlbase, $temp_filenames['diff']);
  2226. $diff = "<a href='$url'>diff</a>";
  2227. } else {
  2228. $diff = "&nbsp;";
  2229. }
  2230. if (isset($temp_filenames['mem']) && file_exists($temp_filenames['mem'])) {
  2231. $url = str_replace($temp_target, $temp_urlbase, $temp_filenames['mem']);
  2232. $mem = "<a href='$url'>leaks</a>";
  2233. } else {
  2234. $mem = "&nbsp;";
  2235. }
  2236. fwrite($html_file,
  2237. "<tr>" .
  2238. "<td>$result</td>" .
  2239. "<td>$tested</td>" .
  2240. "<td>$extra</td>" .
  2241. "<td>$diff</td>" .
  2242. "<td>$mem</td>" .
  2243. "</tr>\n");
  2244. }
  2245. }
  2246. function junit_init() {
  2247. // Check whether a junit log is wanted.
  2248. $JUNIT = getenv('TEST_PHP_JUNIT');
  2249. if (empty($JUNIT)) {
  2250. $JUNIT = FALSE;
  2251. } elseif (!$fp = fopen($JUNIT, 'w')) {
  2252. error("Failed to open $JUNIT for writing.");
  2253. } else {
  2254. $JUNIT = array(
  2255. 'fp' => $fp,
  2256. 'name' => 'PHP',
  2257. 'test_total' => 0,
  2258. 'test_pass' => 0,
  2259. 'test_fail' => 0,
  2260. 'test_error' => 0,
  2261. 'test_skip' => 0,
  2262. 'test_warn' => 0,
  2263. 'execution_time'=> 0,
  2264. 'suites' => array(),
  2265. 'files' => array()
  2266. );
  2267. }
  2268. $GLOBALS['JUNIT'] = $JUNIT;
  2269. }
  2270. function junit_save_xml() {
  2271. global $JUNIT;
  2272. if (!junit_enabled()) return;
  2273. $xml = '<' . '?' . 'xml version="1.0" encoding="UTF-8"' . '?' . '>'. PHP_EOL;
  2274. $xml .= sprintf(
  2275. '<testsuites name="%s" tests="%s" failures="%d" errors="%d" skip="%d" time="%s">' . PHP_EOL,
  2276. $JUNIT['name'], $JUNIT['test_total'], $JUNIT['test_fail'], $JUNIT['test_error'], $JUNIT['test_skip'],
  2277. $JUNIT['execution_time']
  2278. );
  2279. $xml .= junit_get_suite_xml();
  2280. $xml .= '</testsuites>';
  2281. fwrite($JUNIT['fp'], $xml);
  2282. }
  2283. function junit_get_suite_xml($suite_name = '') {
  2284. global $JUNIT;
  2285. $result = "";
  2286. foreach ($JUNIT['suites'] as $suite_name => $suite) {
  2287. $result .= sprintf(
  2288. '<testsuite name="%s" tests="%s" failures="%d" errors="%d" skip="%d" time="%s">' . PHP_EOL,
  2289. $suite['name'], $suite['test_total'], $suite['test_fail'], $suite['test_error'], $suite['test_skip'],
  2290. $suite['execution_time']
  2291. );
  2292. if (!empty($suite_name)) {
  2293. foreach($suite['files'] as $file) {
  2294. $result .= $JUNIT['files'][$file]['xml'];
  2295. }
  2296. }
  2297. $result .= '</testsuite>' . PHP_EOL;
  2298. }
  2299. return $result;
  2300. }
  2301. function junit_enabled() {
  2302. global $JUNIT;
  2303. return !empty($JUNIT);
  2304. }
  2305. /**
  2306. * @param array|string $type
  2307. * @param string $file_name
  2308. * @param string $test_name
  2309. * @param int|string $time
  2310. * @param string $message
  2311. * @param string $details
  2312. * @return void
  2313. */
  2314. function junit_mark_test_as($type, $file_name, $test_name, $time = null, $message = '', $details = '') {
  2315. global $JUNIT;
  2316. if (!junit_enabled()) return;
  2317. $suite = junit_get_suitename_for($file_name);
  2318. junit_suite_record($suite, 'test_total');
  2319. $time = $time ?? junit_get_timer($file_name);
  2320. junit_suite_record($suite, 'execution_time', $time);
  2321. $escaped_details = htmlspecialchars($details, ENT_QUOTES, 'UTF-8');
  2322. $escaped_details = preg_replace_callback('/[\0-\x08\x0B\x0C\x0E-\x1F]/', function ($c) {
  2323. return sprintf('[[0x%02x]]', ord($c[0]));
  2324. }, $escaped_details);
  2325. $escaped_message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  2326. $escaped_test_name = htmlspecialchars($test_name, ENT_QUOTES);
  2327. $JUNIT['files'][$file_name]['xml'] = "<testcase classname='" . $suite . "." . basename($file_name) . "' name='$escaped_test_name' time='$time'>\n";
  2328. if (is_array($type)) {
  2329. $output_type = $type[0] . 'ED';
  2330. $temp = array_intersect(array('XFAIL', 'FAIL', 'WARN'), $type);
  2331. $type = reset($temp);
  2332. } else {
  2333. $output_type = $type . 'ED';
  2334. }
  2335. if ('PASS' == $type || 'XFAIL' == $type) {
  2336. junit_suite_record($suite, 'test_pass');
  2337. } elseif ('BORK' == $type) {
  2338. junit_suite_record($suite, 'test_error');
  2339. $JUNIT['files'][$file_name]['xml'] .= "<error type='$output_type' message='$escaped_message'/>\n";
  2340. } elseif ('SKIP' == $type) {
  2341. junit_suite_record($suite, 'test_skip');
  2342. $JUNIT['files'][$file_name]['xml'] .= "<skipped>$escaped_message</skipped>\n";
  2343. } elseif ('WARN' == $type) {
  2344. junit_suite_record($suite, 'test_warn');
  2345. $JUNIT['files'][$file_name]['xml'] .= "<warning>$escaped_message</warning>\n";
  2346. } elseif ('FAIL' == $type) {
  2347. junit_suite_record($suite, 'test_fail');
  2348. $JUNIT['files'][$file_name]['xml'] .= "<failure type='$output_type' message='$escaped_message'>$escaped_details</failure>\n";
  2349. } else {
  2350. junit_suite_record($suite, 'test_error');
  2351. $JUNIT['files'][$file_name]['xml'] .= "<error type='$output_type' message='$escaped_message'>$escaped_details</error>\n";
  2352. }
  2353. $JUNIT['files'][$file_name]['xml'] .= "</testcase>\n";
  2354. }
  2355. function junit_suite_record($suite, $param, $value = 1) {
  2356. global $JUNIT;
  2357. $JUNIT[$param] += $value;
  2358. $JUNIT['suites'][$suite][$param] += $value;
  2359. }
  2360. function junit_get_timer($file_name) {
  2361. global $JUNIT;
  2362. if (!junit_enabled()) return 0;
  2363. if (isset($JUNIT['files'][$file_name]['total'])) {
  2364. return number_format($JUNIT['files'][$file_name]['total'], 4);
  2365. }
  2366. return 0;
  2367. }
  2368. function junit_start_timer($file_name) {
  2369. global $JUNIT;
  2370. if (!junit_enabled()) return;
  2371. if (!isset($JUNIT['files'][$file_name]['start'])) {
  2372. $JUNIT['files'][$file_name]['start'] = microtime(true);
  2373. $suite = junit_get_suitename_for($file_name);
  2374. junit_init_suite($suite);
  2375. $JUNIT['suites'][$suite]['files'][$file_name] = $file_name;
  2376. }
  2377. }
  2378. function junit_get_suitename_for($file_name) {
  2379. return junit_path_to_classname(dirname($file_name));
  2380. }
  2381. function junit_path_to_classname($file_name) {
  2382. global $JUNIT;
  2383. $ret = $JUNIT['name'];
  2384. $_tmp = array();
  2385. // lookup whether we're in the PHP source checkout
  2386. $max = 5;
  2387. if (is_file($file_name)) {
  2388. $dir = dirname(realpath($file_name));
  2389. } else {
  2390. $dir = realpath($file_name);
  2391. }
  2392. do {
  2393. array_unshift($_tmp, basename($dir));
  2394. $chk = $dir . DIRECTORY_SEPARATOR . "main" . DIRECTORY_SEPARATOR . "php_version.h";
  2395. $dir = dirname($dir);
  2396. } while (!file_exists($chk) && --$max > 0);
  2397. if (file_exists($chk)) {
  2398. if ($max) {
  2399. array_shift($_tmp);
  2400. }
  2401. foreach ($_tmp as $p) {
  2402. $ret = $ret . "." . preg_replace(",[^a-z0-9]+,i", ".", $p);
  2403. }
  2404. return $ret;
  2405. }
  2406. return $JUNIT['name'] . '.' . str_replace(array(DIRECTORY_SEPARATOR, '-'), '.', $file_name);
  2407. }
  2408. function junit_init_suite($suite_name) {
  2409. global $JUNIT;
  2410. if (!junit_enabled()) return;
  2411. if (!empty($JUNIT['suites'][$suite_name])) {
  2412. return;
  2413. }
  2414. $JUNIT['suites'][$suite_name] = array(
  2415. 'name' => $suite_name,
  2416. 'test_total' => 0,
  2417. 'test_pass' => 0,
  2418. 'test_fail' => 0,
  2419. 'test_error' => 0,
  2420. 'test_skip' => 0,
  2421. 'suites' => array(),
  2422. 'files' => array(),
  2423. 'execution_time'=> 0,
  2424. );
  2425. }
  2426. function junit_finish_timer($file_name) {
  2427. global $JUNIT;
  2428. if (!junit_enabled()) return;
  2429. if (!isset($JUNIT['files'][$file_name]['start'])) {
  2430. error("Timer for $file_name was not started!");
  2431. }
  2432. if (!isset($JUNIT['files'][$file_name]['total'])) {
  2433. $JUNIT['files'][$file_name]['total'] = 0;
  2434. }
  2435. $start = $JUNIT['files'][$file_name]['start'];
  2436. $JUNIT['files'][$file_name]['total'] += microtime(true) - $start;
  2437. unset($JUNIT['files'][$file_name]['start']);
  2438. }
  2439. class RuntestsValgrind {
  2440. protected $version = '';
  2441. protected $header = '';
  2442. protected $version_3_3_0 = false;
  2443. protected $version_3_8_0 = false;
  2444. public function getVersion() {
  2445. return $this->version;
  2446. }
  2447. public function getHeader() {
  2448. return $this->header;
  2449. }
  2450. public function __construct(array $environment) {
  2451. $header = system_with_timeout('valgrind --version', $environment);
  2452. if (!$header) {
  2453. error("Valgrind returned no version info, cannot proceed.\nPlease check if Valgrind is installed.");
  2454. }
  2455. $count = 0;
  2456. $version = preg_replace("/valgrind-(\d+)\.(\d+)\.(\d+)([.\w_-]+)?(\s+)/", '$1.$2.$3', $header, 1, $count);
  2457. if ($count != 1) {
  2458. error("Valgrind returned invalid version info (\"{$header}\"), cannot proceed.");
  2459. }
  2460. $this->version = $version;
  2461. $this->header = trim($header);
  2462. $this->version_3_3_0 = version_compare($version, '3.3.0', '>=');
  2463. $this->version_3_8_0 = version_compare($version, '3.8.0', '>=');
  2464. }
  2465. public function wrapCommand($cmd, $memcheck_filename, $check_all) {
  2466. $vcmd = 'valgrind -q --tool=memcheck --trace-children=yes';
  2467. if ($check_all) {
  2468. $vcmd .= ' --smc-check=all';
  2469. }
  2470. /* --vex-iropt-register-updates=allregs-at-mem-access is necessary for phpdbg watchpoint tests */
  2471. if ($this->version_3_8_0) {
  2472. /* valgrind 3.3.0+ doesn't have --log-file-exactly option */
  2473. return "$vcmd --vex-iropt-register-updates=allregs-at-mem-access --log-file=$memcheck_filename $cmd";
  2474. } elseif ($this->version_3_3_0) {
  2475. return "$vcmd --vex-iropt-precise-memory-exns=yes --log-file=$memcheck_filename $cmd";
  2476. } else {
  2477. return "$vcmd --vex-iropt-precise-memory-exns=yes --log-file-exactly=$memcheck_filename $cmd";
  2478. }
  2479. }
  2480. }
  2481. /*
  2482. * Local variables:
  2483. * tab-width: 4
  2484. * c-basic-offset: 4
  2485. * End:
  2486. * vim: noet sw=4 ts=4
  2487. */
  2488. ?>