PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Library/php53/lib/php/build/run-tests.php

http://github.com/jyr/MNPP
PHP | 2329 lines | 1915 code | 305 blank | 109 comment | 339 complexity | 0e82fe6f8bb23e53e9799f19dd18d64e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause

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

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