PHP容器化demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2929 lines
95 KiB

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