run-tests.php 78 KB

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