PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/php/run-tests.php

#
PHP | 2042 lines | 1847 code | 122 blank | 73 comment | 233 complexity | b3645192681f15d056cb74381522c59e MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | PHP Version 5 |
  6. +----------------------------------------------------------------------+
  7. | Copyright (c) 1997-2008 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: run-tests.php,v 1.226.2.37.2.45 2008/03/13 13:51:40 felipe Exp $ */
  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. // store current directory
  54. $CUR_DIR = getcwd();
  55. // change into the PHP source directory.
  56. if (getenv('TEST_PHP_SRCDIR')) {
  57. @chdir(getenv('TEST_PHP_SRCDIR'));
  58. }
  59. // Delete some security related environment variables
  60. putenv('SSH_CLIENT=deleted');
  61. putenv('SSH_AUTH_SOCK=deleted');
  62. putenv('SSH_TTY=deleted');
  63. putenv('SSH_CONNECTION=deleted');
  64. $cwd = getcwd();
  65. set_time_limit(0);
  66. $valgrind_version = 0;
  67. // delete as much output buffers as possible
  68. while(@ob_end_clean());
  69. if (ob_get_level()) echo "Not all buffers were deleted.\n";
  70. error_reporting(E_ALL);
  71. ini_set('magic_quotes_runtime',0); // this would break tests by modifying EXPECT sections
  72. if (ini_get('safe_mode')) {
  73. echo <<< SAFE_MODE_WARNING
  74. +-----------------------------------------------------------+
  75. | ! WARNING ! |
  76. | You are running the test-suite with "safe_mode" ENABLED ! |
  77. | |
  78. | Chances are high that no test will work at all, |
  79. | depending on how you configured "safe_mode" ! |
  80. +-----------------------------------------------------------+
  81. SAFE_MODE_WARNING;
  82. }
  83. $environment = isset($_ENV) ? $_ENV : array();
  84. // Don't ever guess at the PHP executable location.
  85. // Require the explicit specification.
  86. // Otherwise we could end up testing the wrong file!
  87. $php = NULL;
  88. $php_cgi = NULL;
  89. if (getenv('TEST_PHP_EXECUTABLE')) {
  90. $php = getenv('TEST_PHP_EXECUTABLE');
  91. if ($php=='auto') {
  92. $php = $cwd.'/sapi/cli/php';
  93. putenv("TEST_PHP_EXECUTABLE=$php");
  94. if (!getenv('TEST_PHP_CGI_EXECUTABLE')) {
  95. $php_cgi = $cwd.'/sapi/cgi/php-cgi';
  96. if (file_exists($php_cgi)) {
  97. putenv("TEST_PHP_CGI_EXECUTABLE=$php_cgi");
  98. } else {
  99. $php_cgi = NULL;
  100. }
  101. }
  102. }
  103. $environment['TEST_PHP_EXECUTABLE'] = $php;
  104. }
  105. if (getenv('TEST_PHP_CGI_EXECUTABLE')) {
  106. $php_cgi = getenv('TEST_PHP_CGI_EXECUTABLE');
  107. if ($php_cgi=='auto') {
  108. $php_cgi = $cwd.'/sapi/cgi/php-cgi';
  109. putenv("TEST_PHP_CGI_EXECUTABLE=$php_cgi");
  110. }
  111. $environment['TEST_PHP_CGI_EXECUTABLE'] = $php_cgi;
  112. }
  113. if ($argc !=2 || ($argv[1] != '-h' && $argv[1] != '-help' && $argv != '--help'))
  114. {
  115. if (empty($php) || !file_exists($php)) {
  116. error("environment variable TEST_PHP_EXECUTABLE must be set to specify PHP executable!");
  117. }
  118. if (function_exists('is_executable') && !@is_executable($php)) {
  119. error("invalid PHP executable specified by TEST_PHP_EXECUTABLE = " . $php);
  120. }
  121. }
  122. if (getenv('TEST_PHP_LOG_FORMAT')) {
  123. $log_format = strtoupper(getenv('TEST_PHP_LOG_FORMAT'));
  124. } else {
  125. $log_format = 'LEOD';
  126. }
  127. // Check whether a detailed log is wanted.
  128. if (getenv('TEST_PHP_DETAILED')) {
  129. $DETAILED = getenv('TEST_PHP_DETAILED');
  130. } else {
  131. $DETAILED = 0;
  132. }
  133. // Check whether user test dirs are requested.
  134. if (getenv('TEST_PHP_USER')) {
  135. $user_tests = explode (',', getenv('TEST_PHP_USER'));
  136. } else {
  137. $user_tests = array();
  138. }
  139. $exts_to_test = array();
  140. $ini_overwrites = array(
  141. 'output_handler=',
  142. 'open_basedir=',
  143. 'safe_mode=0',
  144. 'disable_functions=',
  145. 'output_buffering=Off',
  146. 'error_reporting=8191',
  147. 'display_errors=1',
  148. 'display_startup_errors=1',
  149. 'log_errors=0',
  150. 'html_errors=0',
  151. 'track_errors=1',
  152. 'report_memleaks=1',
  153. 'report_zend_debug=0',
  154. 'docref_root=',
  155. 'docref_ext=.html',
  156. 'error_prepend_string=',
  157. 'error_append_string=',
  158. 'auto_prepend_file=',
  159. 'auto_append_file=',
  160. 'magic_quotes_runtime=0',
  161. 'ignore_repeated_errors=0',
  162. );
  163. function write_information($show_html)
  164. {
  165. global $cwd, $php, $php_cgi, $php_info, $user_tests, $ini_overwrites, $pass_options, $exts_to_test;
  166. // Get info from php
  167. $info_file = realpath(dirname(__FILE__)) . '/run-test-info.php';
  168. @unlink($info_file);
  169. $php_info = '<?php echo "
  170. PHP_SAPI : " . PHP_SAPI . "
  171. PHP_VERSION : " . phpversion() . "
  172. ZEND_VERSION: " . zend_version() . "
  173. PHP_OS : " . PHP_OS . " - " . php_uname() . "
  174. INI actual : " . realpath(get_cfg_var("cfg_file_path")) . "
  175. More .INIs : " . (function_exists(\'php_ini_scanned_files\') ? str_replace("\n","", php_ini_scanned_files()) : "** not determined **"); ?>';
  176. save_text($info_file, $php_info);
  177. $info_params = array();
  178. settings2array($ini_overwrites,$info_params);
  179. settings2params($info_params);
  180. $php_info = `$php $pass_options $info_params "$info_file"`;
  181. if ($php_cgi && $php != $php_cgi) {
  182. $php_info_cgi = `$php_cgi $pass_options $info_params -q "$info_file"`;
  183. $php_info_sep = "\n---------------------------------------------------------------------";
  184. $php_cgi_info = "$php_info_sep\nPHP : $php_cgi $php_info_cgi$php_info_sep";
  185. } else {
  186. $php_cgi_info = '';
  187. }
  188. @unlink($info_file);
  189. define('TESTED_PHP_VERSION', `$php -r "echo PHP_VERSION;"`);
  190. // load list of enabled extensions
  191. save_text($info_file, '<?php echo join(",",get_loaded_extensions()); ?>');
  192. $exts_to_test = explode(',',`$php $pass_options $info_params "$info_file"`);
  193. // check for extensions that need special handling and regenerate
  194. $info_params_ex = array(
  195. 'session' => array('session.auto_start=0'),
  196. 'tidy' => array('tidy.clean_output=0'),
  197. 'zlib' => array('zlib.output_compression=Off'),
  198. 'xdebug' => array('xdebug.default_enable=0'),
  199. );
  200. foreach($info_params_ex as $ext => $ini_overwrites_ex) {
  201. if (in_array($ext, $exts_to_test)) {
  202. $ini_overwrites = array_merge($ini_overwrites, $ini_overwrites_ex);
  203. }
  204. }
  205. @unlink($info_file);
  206. // Write test context information.
  207. echo "
  208. =====================================================================
  209. PHP : $php $php_info $php_cgi_info
  210. CWD : $cwd
  211. Extra dirs : ";
  212. foreach ($user_tests as $test_dir) {
  213. echo "{$test_dir}\n ";
  214. }
  215. echo "
  216. =====================================================================
  217. ";
  218. }
  219. // Determine the tests to be run.
  220. $test_files = array();
  221. $redir_tests = array();
  222. $test_results = array();
  223. $PHP_FAILED_TESTS = array('BORKED' => array(), 'FAILED' => array(), 'WARNED' => array(), 'LEAKED' => array());
  224. // If parameters given assume they represent selected tests to run.
  225. $failed_tests_file= false;
  226. $pass_option_n = false;
  227. $pass_options = '';
  228. $compression = 0;
  229. $output_file = $CUR_DIR . '/php_test_results_' . @date('Ymd_Hi') . '.txt';
  230. if ($compression) {
  231. $output_file = 'compress.zlib://' . $output_file . '.gz';
  232. }
  233. $just_save_results = false;
  234. $leak_check = false;
  235. $html_output = false;
  236. $html_file = null;
  237. $temp_source = null;
  238. $temp_target = null;
  239. $temp_urlbase = null;
  240. $conf_passed = null;
  241. $no_clean = false;
  242. $cfgtypes = array('show', 'keep');
  243. $cfgfiles = array('skip', 'php', 'clean');
  244. $cfg = array();
  245. foreach($cfgtypes as $type) {
  246. $cfg[$type] = array();
  247. foreach($cfgfiles as $file) {
  248. $cfg[$type][$file] = false;
  249. }
  250. }
  251. if (getenv('TEST_PHP_ARGS'))
  252. {
  253. if (!isset($argc) || !$argc || !isset($argv))
  254. {
  255. $argv = array(__FILE__);
  256. }
  257. $argv = array_merge($argv, split(' ', getenv('TEST_PHP_ARGS')));
  258. $argc = count($argv);
  259. }
  260. if (isset($argc) && $argc > 1) {
  261. for ($i=1; $i<$argc; $i++) {
  262. $is_switch = false;
  263. $switch = substr($argv[$i],1,1);
  264. $repeat = substr($argv[$i],0,1) == '-';
  265. while ($repeat) {
  266. $repeat = false;
  267. if (!$is_switch) {
  268. $switch = substr($argv[$i],1,1);
  269. }
  270. $is_switch = true;
  271. switch($switch) {
  272. case 'r':
  273. case 'l':
  274. $test_list = @file($argv[++$i]);
  275. if ($test_list) {
  276. foreach($test_list as $test) {
  277. $matches = array();
  278. if (preg_match('/^#.*\[(.*)\]\:\s+(.*)$/', $test, $matches)) {
  279. $redir_tests[] = array($matches[1], $matches[2]);
  280. } else if (strlen($test)) {
  281. $test_files[] = trim($test);
  282. }
  283. }
  284. }
  285. if ($switch != 'l') {
  286. break;
  287. }
  288. $i--;
  289. // break left intentionally
  290. case 'w':
  291. $failed_tests_file = fopen($argv[++$i], 'w+t');
  292. break;
  293. case 'a':
  294. $failed_tests_file = fopen($argv[++$i], 'a+t');
  295. break;
  296. case 'c':
  297. $conf_passed = $argv[++$i];
  298. break;
  299. case 'd':
  300. $ini_overwrites[] = $argv[++$i];
  301. break;
  302. //case 'h'
  303. case '--keep-all':
  304. foreach($cfgfiles as $file) {
  305. $cfg['keep'][$file] = true;
  306. }
  307. break;
  308. case '--keep-skip':
  309. $cfg['keep']['skip'] = true;
  310. break;
  311. case '--keep-php':
  312. $cfg['keep']['php'] = true;
  313. break;
  314. case '--keep-clean':
  315. $cfg['keep']['clean'] = true;
  316. break;
  317. //case 'l'
  318. case 'm':
  319. $leak_check = true;
  320. break;
  321. case 'n':
  322. if (!$pass_option_n) {
  323. $pass_options .= ' -n';
  324. }
  325. $pass_option_n = true;
  326. break;
  327. case 'N':
  328. // this is always native
  329. break;
  330. case '--no-clean':
  331. $no_clean = true;
  332. break;
  333. case 'q':
  334. putenv('NO_INTERACTION=1');
  335. break;
  336. //case 'r'
  337. case 's':
  338. $output_file = $argv[++$i];
  339. $just_save_results = true;
  340. break;
  341. case '--show-all':
  342. foreach($cfgfiles as $file) {
  343. $cfg['show'][$file] = true;
  344. }
  345. break;
  346. case '--show-skip':
  347. $cfg['show']['skip'] = true;
  348. break;
  349. case '--show-php':
  350. $cfg['show']['php'] = true;
  351. break;
  352. case '--show-clean':
  353. $cfg['show']['clean'] = true;
  354. break;
  355. case '--temp-source':
  356. $temp_source = $argv[++$i];
  357. break;
  358. case '--temp-target':
  359. $temp_target = $argv[++$i];
  360. if ($temp_urlbase) {
  361. $temp_urlbase = $temp_target;
  362. }
  363. break;
  364. case '--temp-urlbase':
  365. $temp_urlbase = $argv[++$i];
  366. break;
  367. case 'v':
  368. case '--verbose':
  369. $DETAILED = true;
  370. break;
  371. //case 'w'
  372. case '-':
  373. // repeat check with full switch
  374. $switch = $argv[$i];
  375. if ($switch != '-') {
  376. $repeat = true;
  377. }
  378. break;
  379. case '--html':
  380. $html_file = @fopen($argv[++$i], 'wt');
  381. $html_output = is_resource($html_file);
  382. break;
  383. case '--version':
  384. echo '$Revision: 1.226.2.37.2.45 $'."\n";
  385. exit(1);
  386. case 'u':
  387. case 'U':
  388. // Allow using u or U for forward compatibility
  389. break;
  390. default:
  391. echo "Illegal switch '$switch' specified!\n";
  392. case 'h':
  393. case '-help':
  394. case '--help':
  395. echo <<<HELP
  396. Synopsis:
  397. php run-tests.php [options] [files] [directories]
  398. Options:
  399. -l <file> Read the testfiles to be executed from <file>. After the test
  400. has finished all failed tests are written to the same <file>.
  401. If the list is empty and no further test is specified then
  402. all tests are executed (same as: -r <file> -w <file>).
  403. -r <file> Read the testfiles to be executed from <file>.
  404. -w <file> Write a list of all failed tests to <file>.
  405. -a <file> Same as -w but append rather then truncating <file>.
  406. -c <file> Look for php.ini in directory <file> or use <file> as ini.
  407. -n Pass -n option to the php binary (Do not use a php.ini).
  408. -d foo=bar Pass -d option to the php binary (Define INI entry foo
  409. with value 'bar').
  410. -m Test for memory leaks with Valgrind.
  411. -N Always set (Test with unicode_semantics set off in PHP 6).
  412. -s <file> Write output to <file>.
  413. -q Quiet, no user interaction (same as environment NO_INTERACTION).
  414. --verbose
  415. -v Verbose mode.
  416. --help
  417. -h This Help.
  418. --html <file> Generate HTML output.
  419. --temp-source <sdir> --temp-target <tdir> [--temp-urlbase <url>]
  420. Write temporary files to <tdir> by replacing <sdir> from the
  421. filenames to generate with <tdir>. If --html is being used and
  422. <url> given then the generated links are relative and prefixed
  423. with the given url. In general you want to make <sdir> the path
  424. to your source files and <tdir> some pach in your web page
  425. hierarchy with <url> pointing to <tdir>.
  426. --keep-[all|php|skip|clean]
  427. Do not delete 'all' files, 'php' test file, 'skip' or 'clean'
  428. file.
  429. --show-[all|php|skip|clean]
  430. Show 'all' files, 'php' test file, 'skip' or 'clean' file.
  431. --no-clean Do not execute clean section if any.
  432. HELP;
  433. exit(1);
  434. }
  435. }
  436. if (!$is_switch) {
  437. $testfile = realpath($argv[$i]);
  438. if (!$testfile && strpos($argv[$i], '*') !== false && function_exists('glob')) {
  439. if (preg_match("/\.phpt$/", $argv[$i])) {
  440. $pattern_match = glob($argv[$i]);
  441. } else if (preg_match("/\*$/", $argv[$i])) {
  442. $pattern_match = glob($argv[$i] . '.phpt');
  443. } else {
  444. die("bogus test name " . $argv[$i] . "\n");
  445. }
  446. if (is_array($pattern_match)) {
  447. $test_files = array_merge($test_files, $pattern_match);
  448. }
  449. } else if (is_dir($testfile)) {
  450. find_files($testfile);
  451. } else if (preg_match("/\.phpt$/", $testfile)) {
  452. $test_files[] = $testfile;
  453. } else {
  454. die("bogus test name " . $argv[$i] . "\n");
  455. }
  456. }
  457. }
  458. if (strlen($conf_passed))
  459. {
  460. $pass_options .= " -c '$conf_passed'";
  461. }
  462. $test_files = array_unique($test_files);
  463. $test_files = array_merge($test_files, $redir_tests);
  464. // Run selected tests.
  465. $test_cnt = count($test_files);
  466. if ($test_cnt) {
  467. write_information($html_output);
  468. usort($test_files, "test_sort");
  469. $start_time = time();
  470. if (!$html_output) {
  471. echo "Running selected tests.\n";
  472. } else {
  473. show_start($start_time);
  474. }
  475. $test_idx = 0;
  476. run_all_tests($test_files, $environment);
  477. $end_time = time();
  478. if ($html_output) {
  479. show_end($end_time);
  480. }
  481. if ($failed_tests_file) {
  482. fclose($failed_tests_file);
  483. }
  484. if (count($test_files) || count($test_results)) {
  485. compute_summary();
  486. if ($html_output) {
  487. fwrite($html_file, "<hr/>\n" . get_summary(false, true));
  488. }
  489. echo "=====================================================================";
  490. echo get_summary(false, false);
  491. }
  492. if ($html_output) {
  493. fclose($html_file);
  494. }
  495. if (getenv('REPORT_EXIT_STATUS') == 1 and preg_match('/FAILED(?: |$)/', implode(' ', $test_results))) {
  496. exit(1);
  497. }
  498. exit(0);
  499. }
  500. }
  501. write_information($html_output);
  502. // Compile a list of all test files (*.phpt).
  503. $test_files = array();
  504. $exts_tested = count($exts_to_test);
  505. $exts_skipped = 0;
  506. $ignored_by_ext = 0;
  507. sort($exts_to_test);
  508. $test_dirs = array();
  509. $optionals = array('tests', 'ext', 'Zend', 'ZendEngine2', 'sapi/cli', 'sapi/cgi');
  510. foreach($optionals as $dir) {
  511. if (@filetype($dir) == 'dir') {
  512. $test_dirs[] = $dir;
  513. }
  514. }
  515. // Convert extension names to lowercase
  516. foreach ($exts_to_test as $key => $val) {
  517. $exts_to_test[$key] = strtolower($val);
  518. }
  519. foreach ($test_dirs as $dir) {
  520. find_files("{$cwd}/{$dir}", ($dir == 'ext'));
  521. }
  522. foreach ($user_tests as $dir) {
  523. find_files($dir, ($dir == 'ext'));
  524. }
  525. function find_files($dir,$is_ext_dir=FALSE,$ignore=FALSE)
  526. {
  527. global $test_files, $exts_to_test, $ignored_by_ext, $exts_skipped, $exts_tested;
  528. $o = opendir($dir) or error("cannot open directory: $dir");
  529. while (($name = readdir($o)) !== FALSE) {
  530. if (is_dir("{$dir}/{$name}") && !in_array($name, array('.', '..', 'CVS'))) {
  531. $skip_ext = ($is_ext_dir && !in_array(strtolower($name), $exts_to_test));
  532. if ($skip_ext) {
  533. $exts_skipped++;
  534. }
  535. find_files("{$dir}/{$name}", FALSE, $ignore || $skip_ext);
  536. }
  537. // Cleanup any left-over tmp files from last run.
  538. if (substr($name, -4) == '.tmp') {
  539. @unlink("$dir/$name");
  540. continue;
  541. }
  542. // Otherwise we're only interested in *.phpt files.
  543. if (substr($name, -5) == '.phpt') {
  544. if ($ignore) {
  545. $ignored_by_ext++;
  546. } else {
  547. $testfile = realpath("{$dir}/{$name}");
  548. $test_files[] = $testfile;
  549. }
  550. }
  551. }
  552. closedir($o);
  553. }
  554. function test_name($name)
  555. {
  556. if (is_array($name)) {
  557. return $name[0] . ':' . $name[1];
  558. } else {
  559. return $name;
  560. }
  561. }
  562. function test_sort($a, $b)
  563. {
  564. global $cwd;
  565. $a = test_name($a);
  566. $b = test_name($b);
  567. $ta = strpos($a, "{$cwd}/tests")===0 ? 1 + (strpos($a, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  568. $tb = strpos($b, "{$cwd}/tests")===0 ? 1 + (strpos($b, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  569. if ($ta == $tb) {
  570. return strcmp($a, $b);
  571. } else {
  572. return $tb - $ta;
  573. }
  574. }
  575. $test_files = array_unique($test_files);
  576. usort($test_files, "test_sort");
  577. $start_time = time();
  578. show_start($start_time);
  579. $test_cnt = count($test_files);
  580. $test_idx = 0;
  581. run_all_tests($test_files, $environment);
  582. $end_time = time();
  583. if ($failed_tests_file) {
  584. fclose($failed_tests_file);
  585. }
  586. // Summarize results
  587. if (0 == count($test_results)) {
  588. echo "No tests were run.\n";
  589. return;
  590. }
  591. compute_summary();
  592. show_end($end_time);
  593. show_summary();
  594. if ($html_output) {
  595. fclose($html_file);
  596. }
  597. define('PHP_QA_EMAIL', 'qa-reports@lists.php.net');
  598. define('QA_SUBMISSION_PAGE', 'http://qa.php.net/buildtest-process.php');
  599. /* We got failed Tests, offer the user to send an e-mail to QA team, unless NO_INTERACTION is set */
  600. if (!getenv('NO_INTERACTION')) {
  601. $fp = fopen("php://stdin", "r+");
  602. if ($sum_results['FAILED'] || $sum_results['BORKED'] || $sum_results['WARNED'] || $sum_results['LEAKED']) {
  603. echo "\nYou may have found a problem in PHP.";
  604. }
  605. echo "\nWe would like to send this report automatically to the\n";
  606. echo "PHP QA team, to give us a better understanding of how\nthe test cases are doing. If you don't want to send it\n";
  607. echo "immediately, you can choose \"s\" to save the report to\na file that you can send us later.\n";
  608. echo "Do you want to send this report now? [Yns]: ";
  609. flush();
  610. $user_input = fgets($fp, 10);
  611. $just_save_results = (strtolower($user_input[0]) == 's');
  612. }
  613. if ($just_save_results || !getenv('NO_INTERACTION')) {
  614. if ($just_save_results || strlen(trim($user_input)) == 0 || strtolower($user_input[0]) == 'y') {
  615. /*
  616. * Collect information about the host system for our report
  617. * Fetch phpinfo() output so that we can see the PHP enviroment
  618. * Make an archive of all the failed tests
  619. * Send an email
  620. */
  621. if ($just_save_results)
  622. {
  623. $user_input = 's';
  624. }
  625. /* Ask the user to provide an email address, so that QA team can contact the user */
  626. if (!strncasecmp($user_input, 'y', 1) || strlen(trim($user_input)) == 0) {
  627. 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): ";
  628. flush();
  629. $user_email = trim(fgets($fp, 1024));
  630. $user_email = str_replace("@", " at ", str_replace(".", " dot ", $user_email));
  631. }
  632. $failed_tests_data = '';
  633. $sep = "\n" . str_repeat('=', 80) . "\n";
  634. $failed_tests_data .= $failed_test_summary . "\n";
  635. $failed_tests_data .= get_summary(true, false) . "\n";
  636. if ($sum_results['FAILED']) {
  637. foreach ($PHP_FAILED_TESTS['FAILED'] as $test_info) {
  638. $failed_tests_data .= $sep . $test_info['name'] . $test_info['info'];
  639. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['output']));
  640. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['diff']));
  641. $failed_tests_data .= $sep . "\n\n";
  642. }
  643. $status = "failed";
  644. } else {
  645. $status = "success";
  646. }
  647. $failed_tests_data .= "\n" . $sep . 'BUILD ENVIRONMENT' . $sep;
  648. $failed_tests_data .= "OS:\n" . PHP_OS . " - " . php_uname() . "\n\n";
  649. $ldd = $autoconf = $sys_libtool = $libtool = $compiler = 'N/A';
  650. if (substr(PHP_OS, 0, 3) != "WIN") {
  651. /* If PHP_AUTOCONF is set, use it; otherwise, use 'autoconf'. */
  652. if (getenv('PHP_AUTOCONF')) {
  653. $autoconf = shell_exec(getenv('PHP_AUTOCONF') . ' --version');
  654. } else {
  655. $autoconf = shell_exec('autoconf --version');
  656. }
  657. /* Always use the generated libtool - Mac OSX uses 'glibtool' */
  658. $libtool = shell_exec($CUR_DIR . '/libtool --version');
  659. /* Use shtool to find out if there is glibtool present (MacOSX) */
  660. $sys_libtool_path = shell_exec(dirname(__FILE__) . '/build/shtool path glibtool libtool');
  661. if ($sys_libtool_path) {
  662. $sys_libtool = shell_exec(str_replace("\n", "", $sys_libtool_path) . ' --version');
  663. }
  664. /* Try the most common flags for 'version' */
  665. $flags = array('-v', '-V', '--version');
  666. $cc_status=0;
  667. foreach($flags AS $flag) {
  668. system(getenv('CC')." $flag >/dev/null 2>&1", $cc_status);
  669. if ($cc_status == 0) {
  670. $compiler = shell_exec(getenv('CC')." $flag 2>&1");
  671. break;
  672. }
  673. }
  674. $ldd = shell_exec("ldd $php 2>/dev/null");
  675. }
  676. $failed_tests_data .= "Autoconf:\n$autoconf\n";
  677. $failed_tests_data .= "Bundled Libtool:\n$libtool\n";
  678. $failed_tests_data .= "System Libtool:\n$sys_libtool\n";
  679. $failed_tests_data .= "Compiler:\n$compiler\n";
  680. $failed_tests_data .= "Bison:\n". @shell_exec('bison --version 2>/dev/null'). "\n";
  681. $failed_tests_data .= "Libraries:\n$ldd\n";
  682. $failed_tests_data .= "\n";
  683. if (isset($user_email)) {
  684. $failed_tests_data .= "User's E-mail: ".$user_email."\n\n";
  685. }
  686. $failed_tests_data .= $sep . "PHPINFO" . $sep;
  687. $failed_tests_data .= shell_exec($php.' -dhtml_errors=0 -i');
  688. if ($just_save_results || !mail_qa_team($failed_tests_data, $compression, $status)) {
  689. file_put_contents($output_file, $failed_tests_data);
  690. if (!$just_save_results) {
  691. echo "\nThe test script was unable to automatically send the report to PHP's QA Team\n";
  692. }
  693. echo "Please send ".$output_file." to ".PHP_QA_EMAIL." manually, thank you.\n";
  694. } else {
  695. fwrite($fp, "\nThank you for helping to make PHP better.\n");
  696. fclose($fp);
  697. }
  698. }
  699. }
  700. if (getenv('REPORT_EXIT_STATUS') == 1 and $sum_results['FAILED']) {
  701. exit(1);
  702. }
  703. exit(0);
  704. //
  705. // Send Email to QA Team
  706. //
  707. function mail_qa_team($data, $compression, $status = FALSE)
  708. {
  709. $url_bits = parse_url(QA_SUBMISSION_PAGE);
  710. if (empty($url_bits['port'])) $url_bits['port'] = 80;
  711. $data = "php_test_data=" . urlencode(base64_encode(str_replace("\00", '[0x0]', $data)));
  712. $data_length = strlen($data);
  713. $fs = fsockopen($url_bits['host'], $url_bits['port'], $errno, $errstr, 10);
  714. if (!$fs) {
  715. return FALSE;
  716. }
  717. $php_version = urlencode(TESTED_PHP_VERSION);
  718. echo "\nPosting to {$url_bits['host']} {$url_bits['path']}\n";
  719. fwrite($fs, "POST ".$url_bits['path']."?status=$status&version=$php_version HTTP/1.1\r\n");
  720. fwrite($fs, "Host: ".$url_bits['host']."\r\n");
  721. fwrite($fs, "User-Agent: QA Browser 0.1\r\n");
  722. fwrite($fs, "Content-Type: application/x-www-form-urlencoded\r\n");
  723. fwrite($fs, "Content-Length: ".$data_length."\r\n\r\n");
  724. fwrite($fs, $data);
  725. fwrite($fs, "\r\n\r\n");
  726. fclose($fs);
  727. return 1;
  728. }
  729. //
  730. // Write the given text to a temporary file, and return the filename.
  731. //
  732. function save_text($filename, $text, $filename_copy = null)
  733. {
  734. global $DETAILED;
  735. if ($filename_copy && $filename_copy != $filename) {
  736. if (@file_put_contents($filename_copy, $text) === false) {
  737. error("Cannot open file '" . $filename_copy . "' (save_text)");
  738. }
  739. }
  740. if (@file_put_contents($filename, $text) === false) {
  741. error("Cannot open file '" . $filename . "' (save_text)");
  742. }
  743. if (1 < $DETAILED) echo "
  744. FILE $filename {{{
  745. $text
  746. }}}
  747. ";
  748. }
  749. //
  750. // Write an error in a format recognizable to Emacs or MSVC.
  751. //
  752. function error_report($testname, $logname, $tested)
  753. {
  754. $testname = realpath($testname);
  755. $logname = realpath($logname);
  756. switch (strtoupper(getenv('TEST_PHP_ERROR_STYLE'))) {
  757. case 'MSVC':
  758. echo $testname . "(1) : $tested\n";
  759. echo $logname . "(1) : $tested\n";
  760. break;
  761. case 'EMACS':
  762. echo $testname . ":1: $tested\n";
  763. echo $logname . ":1: $tested\n";
  764. break;
  765. }
  766. }
  767. function system_with_timeout($commandline, $env = null, $stdin = null)
  768. {
  769. global $leak_check;
  770. echo("$commandline\n");
  771. $data = "";
  772. $proc = proc_open($commandline, array(
  773. 0 => array('pipe', 'r'),
  774. 1 => array('pipe', 'w'),
  775. 2 => array('pipe', 'w')
  776. ), $pipes, null, $env, array("suppress_errors" => true));
  777. if (!$proc)
  778. return false;
  779. if (is_string($stdin)) {
  780. fwrite($pipes[0], $stdin);
  781. }
  782. fclose($pipes[0]);
  783. while (true) {
  784. /* hide errors from interrupted syscalls */
  785. $r = $pipes;
  786. $w = null;
  787. $e = null;
  788. $n = @stream_select($r, $w, $e, $leak_check ? 300 : 60);
  789. if ($n === 0) {
  790. /* timed out */
  791. $data .= "\n ** ERROR: process timed out **\n";
  792. proc_terminate($proc);
  793. return $data;
  794. } else if ($n > 0) {
  795. $line = fread($pipes[1], 8192);
  796. if (strlen($line) == 0) {
  797. /* EOF */
  798. break;
  799. }
  800. $data .= $line;
  801. }
  802. }
  803. $stat = proc_get_status($proc);
  804. if ($stat['signaled']) {
  805. $data .= "\nTermsig=".$stat['stopsig'];
  806. }
  807. $code = proc_close($proc);
  808. return $data;
  809. }
  810. function run_all_tests($test_files, $env, $redir_tested = NULL)
  811. {
  812. global $test_results, $failed_tests_file, $php, $test_cnt, $test_idx;
  813. foreach($test_files as $name)
  814. {
  815. if (is_array($name))
  816. {
  817. $index = "# $name[1]: $name[0]";
  818. if ($redir_tested)
  819. {
  820. $name = $name[0];
  821. }
  822. }
  823. else if ($redir_tested)
  824. {
  825. $index = "# $redir_tested: $name";
  826. }
  827. else
  828. {
  829. $index = $name;
  830. }
  831. $test_idx++;
  832. $result = run_test($php, $name, $env);
  833. if (!is_array($name) && $result != 'REDIR')
  834. {
  835. $test_results[$index] = $result;
  836. if ($failed_tests_file && ($result == 'FAILED' || $result == 'WARNED' || $result == 'LEAKED'))
  837. {
  838. fwrite($failed_tests_file, "$index\n");
  839. }
  840. }
  841. }
  842. }
  843. //
  844. // Run an individual test case.
  845. //
  846. function run_test($php, $file, $env)
  847. {
  848. global $log_format, $info_params, $ini_overwrites, $cwd, $PHP_FAILED_TESTS;
  849. global $pass_options, $DETAILED, $IN_REDIRECT, $test_cnt, $test_idx;
  850. global $leak_check, $temp_source, $temp_target, $cfg, $environment;
  851. global $no_clean;
  852. global $valgrind_version;
  853. $temp_filenames = null;
  854. $org_file = $file;
  855. if (isset($env['TEST_PHP_CGI_EXECUTABLE'])) {
  856. $php_cgi = $env['TEST_PHP_CGI_EXECUTABLE'];
  857. }
  858. if (is_array($file)) $file = $file[0];
  859. if ($DETAILED) echo "
  860. =================
  861. TEST $file
  862. ";
  863. // Load the sections of the test file.
  864. $section_text = array(
  865. 'TEST' => '',
  866. 'SKIPIF' => '',
  867. 'GET' => '',
  868. 'COOKIE' => '',
  869. 'POST_RAW' => '',
  870. 'POST' => '',
  871. 'UPLOAD' => '',
  872. 'ARGS' => '',
  873. );
  874. $fp = fopen($file, "rt") or error("Cannot open test file: $file");
  875. $borked = false;
  876. $bork_info = '';
  877. if (!feof($fp)) {
  878. $line = fgets($fp);
  879. } else {
  880. $bork_info = "empty test [$file]";
  881. $borked = true;
  882. }
  883. if (strncmp('--TEST--', $line, 8)) {
  884. $bork_info = "tests must start with --TEST-- [$file]";
  885. $borked = true;
  886. }
  887. $section = 'TEST';
  888. $secfile = false;
  889. $secdone = false;
  890. while (!feof($fp)) {
  891. $line = fgets($fp);
  892. // Match the beginning of a section.
  893. if (preg_match('/^--([_A-Z]+)--/', $line, $r)) {
  894. $section = $r[1];
  895. $section_text[$section] = '';
  896. $secfile = $section == 'FILE' || $section == 'FILEEOF' || $section == 'FILE_EXTERNAL';
  897. $secdone = false;
  898. continue;
  899. }
  900. // Add to the section text.
  901. if (!$secdone) {
  902. $section_text[$section] .= $line;
  903. }
  904. // End of actual test?
  905. if ($secfile && preg_match('/^===DONE===$/', $line)) {
  906. $secdone = true;
  907. }
  908. }
  909. // the redirect section allows a set of tests to be reused outside of
  910. // a given test dir
  911. if (@count($section_text['REDIRECTTEST']) == 1) {
  912. if ($IN_REDIRECT) {
  913. $borked = true;
  914. $bork_info = "Can't redirect a test from within a redirected test";
  915. } else {
  916. $borked = false;
  917. }
  918. } else {
  919. if (@count($section_text['FILE']) + @count($section_text['FILEEOF']) + @count($section_text['FILE_EXTERNAL']) != 1) {
  920. $bork_info = "missing section --FILE--";
  921. $borked = true;
  922. }
  923. if (@count($section_text['FILEEOF']) == 1) {
  924. $section_text['FILE'] = preg_replace("/[\r\n]+$/", '', $section_text['FILEEOF']);
  925. unset($section_text['FILEEOF']);
  926. }
  927. if (@count($section_text['FILE_EXTERNAL']) == 1) {
  928. // don't allow tests to retrieve files from anywhere but this subdirectory
  929. $section_text['FILE_EXTERNAL'] = dirname($file) . '/' . trim(str_replace('..', '', $section_text['FILE_EXTERNAL']));
  930. if (@file_exists($section_text['FILE_EXTERNAL'])) {
  931. $section_text['FILE'] = file_get_contents($section_text['FILE_EXTERNAL']);
  932. unset($section_text['FILE_EXTERNAL']);
  933. } else {
  934. $bork_info = "could not load --FILE_EXTERNAL-- " . dirname($file) . '/' . trim($section_text['FILE_EXTERNAL']);
  935. $borked = true;
  936. }
  937. }
  938. if ((@count($section_text['EXPECT']) + @count($section_text['EXPECTF']) + @count($section_text['EXPECTREGEX'])) != 1) {
  939. $bork_info = "missing section --EXPECT--, --EXPECTF-- or --EXPECTREGEX--";
  940. $borked = true;
  941. }
  942. }
  943. fclose($fp);
  944. $shortname = str_replace($cwd.'/', '', $file);
  945. $tested_file = $shortname;
  946. if ($borked) {
  947. show_result("BORK", $bork_info, $tested_file);
  948. $PHP_FAILED_TESTS['BORKED'][] = array (
  949. 'name' => $file,
  950. 'test_name' => '',
  951. 'output' => '',
  952. 'diff' => '',
  953. 'info' => "$bork_info [$file]",
  954. );
  955. return 'BORKED';
  956. }
  957. $tested = trim($section_text['TEST']);
  958. /* For GET/POST tests, check if cgi sapi is available and if it is, use it. */
  959. if (!empty($section_text['GET']) || !empty($section_text['POST']) || !empty($section_text['POST_RAW']) || !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) {
  960. if (isset($php_cgi)) {
  961. $old_php = $php;
  962. $php = $php_cgi .' -C ';
  963. } elseif (!strncasecmp(PHP_OS, "win", 3) && file_exists(dirname($php) ."/php-cgi.exe")) {
  964. $old_php = $php;
  965. $php = realpath(dirname($php) ."/php-cgi.exe") .' -C ';
  966. } else {
  967. if (file_exists(dirname($php)."/../../sapi/cgi/php-cgi")) {
  968. $old_php = $php;
  969. $php = realpath(dirname($php)."/../../sapi/cgi/php-cgi") . ' -C ';
  970. } else if (file_exists("./sapi/cgi/php-cgi")) {
  971. $old_php = $php;
  972. $php = realpath("./sapi/cgi/php-cgi") . ' -C ';
  973. } else {
  974. show_result("SKIP", $tested, $tested_file, "reason: CGI not available");
  975. return 'SKIPPED';
  976. }
  977. }
  978. }
  979. show_test($test_idx, $shortname);
  980. if (is_array($IN_REDIRECT)) {
  981. $temp_dir = $test_dir = $IN_REDIRECT['dir'];
  982. } else {
  983. $temp_dir = $test_dir = realpath(dirname($file));
  984. }
  985. if ($temp_source && $temp_target) {
  986. $temp_dir = str_replace($temp_source, $temp_target, $temp_dir);
  987. }
  988. $main_file_name = basename($file,'phpt');
  989. $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'diff';
  990. $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'log';
  991. $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'exp';
  992. $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'out';
  993. $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'mem';
  994. $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'php';
  995. $test_file = $test_dir . DIRECTORY_SEPARATOR . $main_file_name.'php';
  996. $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php';
  997. $test_skipif = $test_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php';
  998. $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php';
  999. $test_clean = $test_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php';
  1000. $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('/phpt.');
  1001. $tmp_relative_file = str_replace(dirname(__FILE__).DIRECTORY_SEPARATOR, '', $test_file) . 't';
  1002. if ($temp_source && $temp_target) {
  1003. $temp_skipif .= 's';
  1004. $temp_file .= 's';
  1005. $temp_clean .= 's';
  1006. $copy_file = $temp_dir . DIRECTORY_SEPARATOR . basename(is_array($file) ? $file[1] : $file).'.phps';
  1007. if (!is_dir(dirname($copy_file))) {
  1008. @mkdir(dirname($copy_file), 0777, true) or error("Cannot create output directory - " . dirname($copy_file));
  1009. }
  1010. if (isset($section_text['FILE'])) {
  1011. save_text($copy_file, $section_text['FILE']);
  1012. }
  1013. $temp_filenames = array(
  1014. 'file' => $copy_file,
  1015. 'diff' => $diff_filename,
  1016. 'log' => $log_filename,
  1017. 'exp' => $exp_filename,
  1018. 'out' => $output_filename,
  1019. 'mem' => $memcheck_filename,
  1020. 'php' => $temp_file,
  1021. 'skip' => $temp_skipif,
  1022. 'clean'=> $temp_clean);
  1023. }
  1024. if (is_array($IN_REDIRECT)) {
  1025. $tested = $IN_REDIRECT['prefix'] . ' ' . trim($section_text['TEST']);
  1026. $tested_file = $tmp_relative_file;
  1027. $section_text['FILE'] = "# original source file: $shortname\n" . $section_text['FILE'];
  1028. }
  1029. // unlink old test results
  1030. @unlink($diff_filename);
  1031. @unlink($log_filename);
  1032. @unlink($exp_filename);
  1033. @unlink($output_filename);
  1034. @unlink($memcheck_filename);
  1035. @unlink($temp_file);
  1036. @unlink($test_file);
  1037. @unlink($temp_skipif);
  1038. @unlink($test_skipif);
  1039. @unlink($tmp_post);
  1040. @unlink($temp_clean);
  1041. @unlink($test_clean);
  1042. // Reset environment from any previous test.
  1043. $env['REDIRECT_STATUS']='';
  1044. $env['QUERY_STRING']='';
  1045. $env['PATH_TRANSLATED']='';
  1046. $env['SCRIPT_FILENAME']='';
  1047. $env['REQUEST_METHOD']='';
  1048. $env['CONTENT_TYPE']='';
  1049. $env['CONTENT_LENGTH']='';
  1050. if (!empty($section_text['ENV'])) {
  1051. foreach(explode("\n", trim($section_text['ENV'])) as $e) {
  1052. $e = explode('=',trim($e),2);
  1053. if (!empty($e[0]) && isset($e[1])) {
  1054. $env[$e[0]] = $e[1];
  1055. }
  1056. }
  1057. }
  1058. // Default ini settings
  1059. $ini_settings = array();
  1060. // additional ini overwrites
  1061. //$ini_overwrites[] = 'setting=value';
  1062. settings2array($ini_overwrites, $ini_settings);
  1063. // Any special ini settings
  1064. // these may overwrite the test defaults...
  1065. if (array_key_exists('INI', $section_text)) {
  1066. if (strpos($section_text['INI'], '{PWD}') !== false) {
  1067. $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']);
  1068. }
  1069. settings2array(preg_split( "/[\n\r]+/", $section_text['INI']), $ini_settings);
  1070. }
  1071. settings2params($ini_settings);
  1072. // Check if test should be skipped.
  1073. $info = '';
  1074. $warn = false;
  1075. if (array_key_exists('SKIPIF', $section_text)) {
  1076. if (trim($section_text['SKIPIF'])) {
  1077. if ($cfg['show']['skip']) {
  1078. echo "\n========SKIP========\n";
  1079. echo $section_text['SKIPIF'];
  1080. echo "========DONE========\n";
  1081. }
  1082. save_text($test_skipif, $section_text['SKIPIF'], $temp_skipif);
  1083. $extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  1084. "unset REQUEST_METHOD; unset QUERY_STRING; unset PATH_TRANSLATED; unset SCRIPT_FILENAME; unset REQUEST_METHOD;": "";
  1085. if ($leak_check) {
  1086. $env['USE_ZEND_ALLOC'] = '0';
  1087. } else {
  1088. $env['USE_ZEND_ALLOC'] = '1';
  1089. }
  1090. $output = system_with_timeout("$extra $php $pass_options -q $ini_settings $test_skipif", $env);
  1091. if (!$cfg['keep']['skip']) {
  1092. @unlink($test_skipif);
  1093. }
  1094. if (!strncasecmp('skip', ltrim($output), 4)) {
  1095. if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) {
  1096. show_result("SKIP", $tested, $tested_file, "reason: $m[1]", $temp_filenames);
  1097. } else {
  1098. show_result("SKIP", $tested, $tested_file, '', $temp_filenames);
  1099. }
  1100. if (isset($old_php)) {
  1101. $php = $old_php;
  1102. }
  1103. if (!$cfg['keep']['skip']) {
  1104. @unlink($test_skipif);
  1105. }
  1106. return 'SKIPPED';
  1107. }
  1108. if (!strncasecmp('info', ltrim($output), 4)) {
  1109. if (preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) {
  1110. $info = " (info: $m[1])";
  1111. }
  1112. }
  1113. if (!strncasecmp('warn', ltrim($output), 4)) {
  1114. if (preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) {
  1115. $warn = true; /* only if there is a reason */
  1116. $info = " (warn: $m[1])";
  1117. }
  1118. }
  1119. }
  1120. }
  1121. if (@count($section_text['REDIRECTTEST']) == 1) {
  1122. $test_files = array();
  1123. $IN_REDIRECT = eval($section_text['REDIRECTTEST']);
  1124. $IN_REDIRECT['via'] = "via [$shortname]\n\t";
  1125. $IN_REDIRECT['dir'] = realpath(dirname($file));
  1126. $IN_REDIRECT['prefix'] = trim($section_text['TEST']);
  1127. if (@count($IN_REDIRECT['TESTS']) == 1) {
  1128. if (is_array($org_file)) {
  1129. $test_files[] = $org_file[1];
  1130. } else {
  1131. $GLOBALS['test_files'] = $test_files;
  1132. find_files($IN_REDIRECT['TESTS']);
  1133. foreach($GLOBALS['test_files'] as $f) {
  1134. $test_files[] = array($f, $file);
  1135. }
  1136. }
  1137. $test_cnt += count($test_files) - 1;
  1138. $test_idx--;
  1139. show_redirect_start($IN_REDIRECT['TESTS'], $tested, $tested_file);
  1140. // set up environment
  1141. $redirenv = array_merge($environment, $IN_REDIRECT['ENV']);
  1142. $redirenv['REDIR_TEST_DIR'] = realpath($IN_REDIRECT['TESTS']) . DIRECTORY_SEPARATOR;
  1143. usort($test_files, "test_sort");
  1144. run_all_tests($test_files, $redirenv, $tested);
  1145. show_redirect_ends($IN_REDIRECT['TESTS'], $tested, $tested_file);
  1146. // a redirected test never fails
  1147. $IN_REDIRECT = false;
  1148. return 'REDIR';
  1149. } else {
  1150. $bork_info = "Redirect info must contain exactly one TEST string to be used as redirect directory.";
  1151. show_result("BORK", $bork_info, '', $temp_filenames);
  1152. $PHP_FAILED_TESTS['BORKED'][] = array (
  1153. 'name' => $file,
  1154. 'test_name' => '',
  1155. 'output' => '',
  1156. 'diff' => '',
  1157. 'info' => "$bork_info [$file]",
  1158. 'unicode'=> $unicode_semantics,
  1159. );
  1160. }
  1161. }
  1162. if (is_array($org_file) || @count($section_text['REDIRECTTEST']) == 1) {
  1163. if (is_array($org_file)) $file = $org_file[0];
  1164. $bork_info = "Redirected test did not contain redirection info";
  1165. show_result("BORK", $bork_info, '', $temp_filenames);
  1166. $PHP_FAILED_TESTS['BORKED'][] = array (
  1167. 'name' => $file,
  1168. 'test_name' => '',
  1169. 'output' => '',
  1170. 'diff' => '',
  1171. 'info' => "$bork_info [$file]",
  1172. );
  1173. //$test_cnt -= 1; // Only if is_array($org_file) ?
  1174. //$test_idx--;
  1175. return 'BORKED';
  1176. }
  1177. // We've satisfied the preconditions - run the test!
  1178. if ($cfg['show']['php']) {
  1179. echo "\n========TEST========\n";
  1180. echo $section_text['FILE'];
  1181. echo "========DONE========\n";
  1182. }
  1183. save_text($test_file, $section_text['FILE'], $temp_file);
  1184. if (array_key_exists('GET', $section_text)) {
  1185. $query_string = trim($section_text['GET']);
  1186. } else {
  1187. $query_string = '';
  1188. }
  1189. $env['REDIRECT_STATUS'] = '1';
  1190. $env['QUERY_STRING'] = $query_string;
  1191. $env['PATH_TRANSLATED'] = $test_file;
  1192. $env['SCRIPT_FILENAME'] = $test_file;
  1193. if (array_key_exists('COOKIE', $section_text)) {
  1194. $env['HTTP_COOKIE'] = trim($section_text['COOKIE']);
  1195. } else {
  1196. $env['HTTP_COOKIE'] = '';
  1197. }
  1198. $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
  1199. if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) {
  1200. $post = trim($section_text['POST_RAW']);
  1201. $raw_lines = explode("\n", $post);
  1202. $request = '';
  1203. $started = false;
  1204. foreach ($raw_lines as $line) {
  1205. if (empty($env['CONTENT_TYPE']) && preg_match('/^Content-Type:(.*)/i', $line, $res)) {
  1206. $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1]));
  1207. continue;
  1208. }
  1209. if ($started) $request .= "\n";
  1210. $started = true;
  1211. $request .= $line;
  1212. }
  1213. $env['CONTENT_LENGTH'] = strlen($request);
  1214. $env['REQUEST_METHOD'] = 'POST';
  1215. if (empty($request)) {
  1216. return 'BORKED';
  1217. }
  1218. save_text($tmp_post, $request);
  1219. $cmd = "$php$pass_options$ini_settings -f \"$test_file\" 2>&1 < $tmp_post";
  1220. } elseif (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
  1221. $post = trim($section_text['POST']);
  1222. if (array_key_exists('GZIP_POST', $section_text) && function_exists('gzencode')) {
  1223. $post = gzencode($post, 9, FORCE_GZIP);
  1224. $env['HTTP_CONTENT_ENCODING'] = 'gzip';
  1225. } else if (array_key_exists('DEFLATE_POST', $section_text) && function_exists('gzcompress')) {
  1226. $post = gzcompress($post, 9);
  1227. $env['HTTP_CONTENT_ENCODING'] = 'deflate';
  1228. }
  1229. save_text($tmp_post, $post);
  1230. $content_length = strlen($post);
  1231. $env['REQUEST_METHOD'] = 'POST';
  1232. $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  1233. $env['CONTENT_LENGTH'] = $content_length;
  1234. $cmd = "$php$pass_options$ini_settings -f \"$test_file\" 2>&1 < $tmp_post";
  1235. } else {
  1236. $env['REQUEST_METHOD'] = 'GET';
  1237. $env['CONTENT_TYPE'] = '';
  1238. $env['CONTENT_LENGTH'] = '';
  1239. $cmd = "$php$pass_options$ini_settings -f \"$test_file\" $args 2>&1";
  1240. }
  1241. if ($leak_check) {
  1242. $env['USE_ZEND_ALLOC'] = '0';
  1243. if (!$valgrind_version) {
  1244. $valgrind_cmd = "valgrind --version";
  1245. $out = system_with_timeout($valgrind_cmd);
  1246. $replace_count = 0;
  1247. if (!$out) {
  1248. error("Valgrind returned no version info, cannot proceed.\nPlease check if Valgrind is installed.");
  1249. } else {
  1250. $valgrind_version = preg_replace("/valgrind-([0-9])\.([0-9])\.([0-9]+)(?:-\w+)?\s*/", '$1$2$3', $out, 1, $replace_count);
  1251. if ($replace_count != 1 || !is_numeric($valgrind_version)) {
  1252. error("Valgrind returned invalid version info (\"$out\"), cannot proceed.");
  1253. }
  1254. }
  1255. }
  1256. if ($valgrind_version < 330) {
  1257. $cmd = "valgrind -q --tool=memcheck --trace-children=yes --log-file-exactly=$memcheck_filename $cmd";
  1258. } else {
  1259. /* valgrind 3.3.0+ doesn't have --log-file-exactly option */
  1260. $cmd = "valgrind -q --tool=memcheck --trace-children=yes --log-file=$memcheck_filename $cmd";
  1261. }
  1262. } else {
  1263. $env['USE_ZEND_ALLOC'] = '1';
  1264. }
  1265. if ($DETAILED) echo "
  1266. CONTENT_LENGTH = " . $env['CONTENT_LENGTH'] . "
  1267. CONTENT_TYPE = " . $env['CONTENT_TYPE'] . "
  1268. PATH_TRANSLATED = " . $env['PATH_TRANSLATED'] . "
  1269. QUERY_STRING = " . $env['QUERY_STRING'] . "
  1270. REDIRECT_STATUS = " . $env['REDIRECT_STATUS'] . "
  1271. REQUEST_METHOD = " . $env['REQUEST_METHOD'] . "
  1272. SCRIPT_FILENAME = " . $env['SCRIPT_FILENAME'] . "
  1273. HTTP_COOKIE = " . $env['HTTP_COOKIE'] . "
  1274. COMMAND $cmd
  1275. ";
  1276. $out = system_with_timeout($cmd, $env, isset($section_text['STDIN']) ? $section_text['STDIN'] : null);
  1277. if (array_key_exists('CLEAN', $section_text) && (!$no_clean || $cfg['keep']['clean'])) {
  1278. if (trim($section_text['CLEAN'])) {
  1279. if ($cfg['show']['clean']) {
  1280. echo "\n========CLEAN=======\n";
  1281. echo $section_text['CLEAN'];
  1282. echo "========DONE========\n";
  1283. }
  1284. save_text($test_clean, trim($section_text['CLEAN']), $temp_clean);
  1285. if (!$no_clean) {
  1286. $clean_params = array();
  1287. settings2array($ini_overwrites,$clean_params);
  1288. settings2params($clean_params);
  1289. $extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  1290. "unset REQUEST_METHOD; unset QUERY_STRING; unset PATH_TRANSLATED; unset SCRIPT_FILENAME; unset REQUEST_METHOD;": "";
  1291. system_with_timeout("$extra $php $pass_options -q $clean_params $test_clean", $env);
  1292. }
  1293. if (!$cfg['keep']['clean']) {
  1294. @unlink($test_clean);
  1295. }
  1296. }
  1297. }
  1298. @unlink($tmp_post);
  1299. $leaked = false;
  1300. $passed = false;
  1301. if ($leak_check) { // leak check
  1302. $leaked = @filesize($memcheck_filename) > 0;
  1303. if (!$leaked) {
  1304. @unlink($memcheck_filename);
  1305. }
  1306. }
  1307. // Does the output match what is expected?
  1308. $output = str_replace("\r\n", "\n", trim($out));
  1309. /* when using CGI, strip the headers from the output */
  1310. $headers = "";
  1311. if (isset($old_php) && preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $out, $match)) {
  1312. $output = trim($match[2]);
  1313. $rh = preg_split("/[\n\r]+/",$match[1]);
  1314. $headers = array();
  1315. foreach ($rh as $line) {
  1316. if (strpos($line, ':')!==false) {
  1317. $line = explode(':', $line, 2);
  1318. $headers[trim($line[0])] = trim($line[1]);
  1319. }
  1320. }
  1321. }
  1322. $failed_headers = false;
  1323. if (isset($section_text['EXPECTHEADERS'])) {
  1324. $want = array();
  1325. $wanted_headers = array();
  1326. $lines = preg_split("/[\n\r]+/",$section_text['EXPECTHEADERS']);
  1327. foreach($lines as $line) {
  1328. if (strpos($line, ':') !== false) {
  1329. $line = explode(':', $line, 2);
  1330. $want[trim($line[0])] = trim($line[1]);
  1331. $wanted_headers[] = trim($line[0]) . ': ' . trim($line[1]);
  1332. }
  1333. }
  1334. $org_headers = $headers;
  1335. $headers = array();
  1336. $output_headers = array();
  1337. foreach($want as $k => $v) {
  1338. if (isset($org_headers[$k])) {
  1339. $headers = $org_headers[$k];
  1340. $output_headers[] = $k . ': ' . $org_headers[$k];
  1341. }
  1342. if (!isset($org_headers[$k]) || $org_headers[$k] != $v) {
  1343. $failed_headers = true;
  1344. }
  1345. }
  1346. ksort($wanted_headers);
  1347. $wanted_headers = join("\n", $wanted_headers);
  1348. ksort($output_headers);
  1349. $output_headers = join("\n", $output_headers);
  1350. }
  1351. if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
  1352. if (isset($section_text['EXPECTF'])) {
  1353. $wanted = trim($section_text['EXPECTF']);
  1354. } else {
  1355. $wanted = trim($section_text['EXPECTREGEX']);
  1356. }
  1357. $wanted_re = preg_replace('/\r\n/',"\n",$wanted);
  1358. if (isset($section_text['EXPECTF'])) {
  1359. $wanted_re = preg_quote($wanted_re, '/');
  1360. // Stick to basics
  1361. $wanted_re = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $wanted_re);
  1362. $wanted_re = str_replace('%s', '[^\r\n]+', $wanted_re);
  1363. $wanted_re = str_replace('%a', '.+', $wanted_re);
  1364. $wanted_re = str_replace('%w', '\s*', $wanted_re);
  1365. $wanted_re = str_replace('%i', '[+-]?\d+', $wanted_re);
  1366. $wanted_re = str_replace('%d', '\d+', $wanted_re);
  1367. $wanted_re = str_replace('%x', '[0-9a-fA-F]+', $wanted_re);
  1368. $wanted_re = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $wanted_re);
  1369. $wanted_re = str_replace('%c', '.', $wanted_re);
  1370. // %f allows two points "-.0.0" but that is the best *simple* expression
  1371. }
  1372. /* DEBUG YOUR REGEX HERE
  1373. var_dump($wanted_re);
  1374. print(str_repeat('=', 80) . "\n");
  1375. var_dump($output);
  1376. */
  1377. if (preg_match("/^$wanted_re\$/s", $output)) {
  1378. $passed = true;
  1379. if (!$cfg['keep']['php']) {
  1380. @unlink($test_file);
  1381. }
  1382. if (isset($old_php)) {
  1383. $php = $old_php;
  1384. }
  1385. if (!$leaked && !$failed_headers) {
  1386. show_result("PASS", $tested, $tested_file, '', $temp_filenames);
  1387. return 'PASSED';
  1388. }
  1389. }
  1390. } else {
  1391. $wanted = trim($section_text['EXPECT']);
  1392. $wanted = preg_replace('/\r\n/',"\n",$wanted);
  1393. // compare and leave on success
  1394. if (!strcmp($output, $wanted)) {
  1395. $passed = true;
  1396. if (!$cfg['keep']['php']) {
  1397. @unlink($test_file);
  1398. }
  1399. if (isset($old_php)) {
  1400. $php = $old_php;
  1401. }
  1402. if (!$leaked && !$failed_headers) {
  1403. show_result("PASS", $tested, $tested_file, '', $temp_filenames);
  1404. return 'PASSED';
  1405. }
  1406. }
  1407. $wanted_re = NULL;
  1408. }
  1409. // Test failed so we need to report details.
  1410. if ($failed_headers) {
  1411. $passed = false;
  1412. $wanted = $wanted_headers . "\n--HEADERS--\n" . $wanted;
  1413. $output = $output_headers . "\n--HEADERS--\n" . $output;
  1414. if (isset($wanted_re)) {
  1415. $wanted_re = preg_quote($wanted_headers . "\n--HEADERS--\n", '/') . $wanted_re;
  1416. }
  1417. }
  1418. if ($leaked) {
  1419. $restype[] = 'LEAK';
  1420. }
  1421. if ($warn) {
  1422. $restype[] = 'WARN';
  1423. }
  1424. if (!$passed) {
  1425. $restype[] = 'FAIL';
  1426. }
  1427. if (!$passed) {
  1428. // write .exp
  1429. if (strpos($log_format,'E') !== FALSE && file_put_contents($exp_filename, $wanted) === FALSE) {
  1430. error("Cannot create expected test output - $exp_filename");
  1431. }
  1432. // write .out
  1433. if (strpos($log_format,'O') !== FALSE && file_put_contents($output_filename, $output) === FALSE) {
  1434. error("Cannot create test output - $output_filename");
  1435. }
  1436. // write .diff
  1437. if (strpos($log_format,'D') !== FALSE && file_put_contents($diff_filename, generate_diff($wanted,$wanted_re,$output)) === FALSE) {
  1438. error("Cannot create test diff - $diff_filename");
  1439. }
  1440. // write .log
  1441. if (strpos($log_format,'L') !== FALSE && file_put_contents($log_filename, "
  1442. ---- EXPECTED OUTPUT
  1443. $wanted
  1444. ---- ACTUAL OUTPUT
  1445. $output
  1446. ---- FAILED
  1447. ") === FALSE) {
  1448. error("Cannot create test log - $log_filename");
  1449. error_report($file, $log_filename, $tested);
  1450. }
  1451. }
  1452. show_result(implode('&', $restype), $tested, $tested_file, $info, $temp_filenames);
  1453. foreach ($restype as $type) {
  1454. $PHP_FAILED_TESTS[$type.'ED'][] = array (
  1455. 'name' => $file,
  1456. 'test_name' => (is_array($IN_REDIRECT) ? $IN_REDIRECT['via'] : '') . $tested . " [$tested_file]",
  1457. 'output' => $output_filename,
  1458. 'diff' => $diff_filename,
  1459. 'info' => $info,
  1460. );
  1461. }
  1462. if (isset($old_php)) {
  1463. $php = $old_php;
  1464. }
  1465. return $restype[0].'ED';
  1466. }
  1467. function comp_line($l1,$l2,$is_reg)
  1468. {
  1469. if ($is_reg) {
  1470. return preg_match('/^'.$l1.'$/s', $l2);
  1471. } else {
  1472. return !strcmp($l1, $l2);
  1473. }
  1474. }
  1475. function count_array_diff($ar1,$ar2,$is_reg,$w,$idx1,$idx2,$cnt1,$cnt2,$steps)
  1476. {
  1477. $equal = 0;
  1478. while ($idx1 < $cnt1 && $idx2 < $cnt2 && comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
  1479. $idx1++;
  1480. $idx2++;
  1481. $equal++;
  1482. $steps--;
  1483. }
  1484. if (--$steps > 0) {

Large files files are truncated, but you can click here to view the full file