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

1196 lines
34 KiB

23 years ago
24 years ago
24 years ago
23 years ago
24 years ago
24 years ago
23 years ago
23 years ago
23 years ago
24 years ago
22 years ago
22 years ago
22 years ago
24 years ago
24 years ago
24 years ago
24 years ago
23 years ago
22 years ago
22 years ago
24 years ago
23 years ago
23 years ago
24 years ago
24 years ago
22 years ago
24 years ago
24 years ago
24 years ago
24 years ago
22 years ago
22 years ago
24 years ago
24 years ago
22 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
23 years ago
23 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
23 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
24 years ago
24 years ago
23 years ago
23 years ago
24 years ago
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 5 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2004 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.0 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_0.txt. |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Authors: Ilia Alshanetsky <iliaa@php.net> |
  17. | Preston L. Bannister <pbannister@php.net> |
  18. | Marcus Boerger <helly@php.net> |
  19. | Derick Rethans <derick@php.net> |
  20. | Sander Roobol <sander@php.net> |
  21. | (based on version by: Stig Bakken <ssb@php.net>) |
  22. | (based on the PHP 3 test framework by Rasmus Lerdorf) |
  23. +----------------------------------------------------------------------+
  24. */
  25. /*
  26. Require exact specification of PHP executable to test (no guessing!).
  27. Die if any internal errors encountered in test script.
  28. Regularized output for simpler post-processing of output.
  29. Optionally output error lines indicating the failing test source and log
  30. for direct jump with MSVC or Emacs.
  31. */
  32. /*
  33. * TODO:
  34. * - do not test PEAR components if base class and/or component class cannot be instanciated
  35. */
  36. /* Sanity check to ensure that pcre extension needed by this script is available.
  37. * In the event it is not, print a nice error message indicating that this script will
  38. * not run without it.
  39. */
  40. if (!extension_loaded("pcre")) {
  41. echo <<<NO_PCRE_ERROR
  42. +-----------------------------------------------------------+
  43. | ! ERROR ! |
  44. | The test-suite requires that you have pcre extension |
  45. | enabled. To enable this extension either compile your PHP |
  46. | with --with-pcre-regex or if you've compiled pcre as a |
  47. | shared module load it via php.ini. |
  48. +-----------------------------------------------------------+
  49. NO_PCRE_ERROR;
  50. exit;
  51. }
  52. if (!function_exists("proc_open")) {
  53. echo <<<NO_PROC_OPEN_ERROR
  54. +-----------------------------------------------------------+
  55. | ! ERROR ! |
  56. | The test-suite requires that proc_open() is available. |
  57. | Please check if you disabled it in php.ini. |
  58. +-----------------------------------------------------------+
  59. NO_PROC_OPEN_ERROR;
  60. exit;
  61. }
  62. // store current directory
  63. $CUR_DIR = getcwd();
  64. // change into the PHP source directory.
  65. if (getenv('TEST_PHP_SRCDIR')) {
  66. @chdir(getenv('TEST_PHP_SRCDIR'));
  67. }
  68. // Delete some security related environment variables
  69. putenv('SSH_CLIENT=deleted');
  70. putenv('SSH_AUTH_SOCK=deleted');
  71. putenv('SSH_TTY=deleted');
  72. $cwd = getcwd();
  73. set_time_limit(0);
  74. // delete as much output buffers as possible
  75. while(@ob_end_clean());
  76. if (ob_get_level()) echo "Not all buffers were deleted.\n";
  77. error_reporting(E_ALL);
  78. ini_set('magic_quotes_runtime',0); // this would break tests by modifying EXPECT sections
  79. if (ini_get('safe_mode')) {
  80. echo <<< SAFE_MODE_WARNING
  81. +-----------------------------------------------------------+
  82. | ! WARNING ! |
  83. | You are running the test-suite with "safe_mode" ENABLED ! |
  84. | |
  85. | Chances are high that no test will work at all, |
  86. | depending on how you configured "safe_mode" ! |
  87. +-----------------------------------------------------------+
  88. SAFE_MODE_WARNING;
  89. }
  90. // Don't ever guess at the PHP executable location.
  91. // Require the explicit specification.
  92. // Otherwise we could end up testing the wrong file!
  93. if (getenv('TEST_PHP_EXECUTABLE')) {
  94. $php = getenv('TEST_PHP_EXECUTABLE');
  95. if ($php=='auto') {
  96. $php = $cwd.'/sapi/cli/php';
  97. putenv("TEST_PHP_EXECUTABLE=$php");
  98. }
  99. }
  100. if (empty($php) || !file_exists($php)) {
  101. error("environment variable TEST_PHP_EXECUTABLE must be set to specify PHP executable!");
  102. }
  103. if (getenv('TEST_PHP_LOG_FORMAT')) {
  104. $log_format = strtoupper(getenv('TEST_PHP_LOG_FORMAT'));
  105. } else {
  106. $log_format = 'LEOD';
  107. }
  108. if (function_exists('is_executable') && !@is_executable($php)) {
  109. error("invalid PHP executable specified by TEST_PHP_EXECUTABLE = " . $php);
  110. }
  111. // Check whether a detailed log is wanted.
  112. if (getenv('TEST_PHP_DETAILED')) {
  113. define('DETAILED', getenv('TEST_PHP_DETAILED'));
  114. } else {
  115. define('DETAILED', 0);
  116. }
  117. // Check whether user test dirs are requested.
  118. if (getenv('TEST_PHP_USER')) {
  119. $user_tests = explode (',', getenv('TEST_PHP_USER'));
  120. } else {
  121. $user_tests = array();
  122. }
  123. // Get info from php
  124. $info_file = realpath(dirname(__FILE__)) . '/run-test-info.php';
  125. @unlink($info_file);
  126. $php_info = '<?php echo "
  127. PHP_SAPI : " . PHP_SAPI . "
  128. PHP_VERSION : " . phpversion() . "
  129. ZEND_VERSION: " . zend_version() . "
  130. PHP_OS : " . PHP_OS . " - " . php_uname() . "
  131. INI actual : " . realpath(get_cfg_var("cfg_file_path")) . "
  132. More .INIs : " . (function_exists(\'php_ini_scanned_files\') ? str_replace("\n","", php_ini_scanned_files()) : "** not determined **"); ?>';
  133. save_text($info_file, $php_info);
  134. $ini_overwrites = array(
  135. 'output_handler=',
  136. 'zlib.output_compression=Off',
  137. 'open_basedir=',
  138. 'safe_mode=0',
  139. 'disable_functions=',
  140. 'output_buffering=Off',
  141. 'error_reporting=4095',
  142. 'display_errors=1',
  143. 'log_errors=0',
  144. 'html_errors=0',
  145. 'track_errors=1',
  146. 'report_memleaks=1',
  147. 'report_zend_debug=0',
  148. 'docref_root=',
  149. 'docref_ext=.html',
  150. 'error_prepend_string=',
  151. 'error_append_string=',
  152. 'auto_prepend_file=',
  153. 'auto_append_file=',
  154. 'magic_quotes_runtime=0',
  155. 'xdebug.default_enable=0',
  156. 'session.auto_start=0'
  157. );
  158. $info_params = array();
  159. settings2array($ini_overwrites,$info_params);
  160. settings2params($info_params);
  161. $php_info = `$php $info_params $info_file`;
  162. @unlink($info_file);
  163. define('TESTED_PHP_VERSION', `$php -r 'echo PHP_VERSION;'`);
  164. // Write test context information.
  165. function write_information()
  166. {
  167. global $cwd, $php, $php_info, $user_tests;
  168. echo "
  169. =====================================================================
  170. CWD : $cwd
  171. PHP : $php $php_info
  172. Extra dirs : ";
  173. foreach ($user_tests as $test_dir) {
  174. echo "{$test_dir}\n ";
  175. }
  176. echo "
  177. =====================================================================
  178. ";
  179. }
  180. // Determine the tests to be run.
  181. $test_files = array();
  182. $test_results = array();
  183. $PHP_FAILED_TESTS = array();
  184. // If parameters given assume they represent selected tests to run.
  185. $failed_tests_file= false;
  186. $pass_option_n = false;
  187. $pass_options = '';
  188. if (isset($argc) && $argc > 1) {
  189. for ($i=1; $i<$argc; $i++) {
  190. if (substr($argv[$i],0,1) == '-') {
  191. $switch = strtolower(substr($argv[$i],1,1));
  192. switch($switch) {
  193. case 'r':
  194. case 'l':
  195. $test_list = @file($argv[++$i]);
  196. if (is_array($test_list) && count($test_list)) {
  197. $test_files = array_merge($test_files, $test_list);
  198. }
  199. if ($switch != 'l') {
  200. break;
  201. }
  202. $i--;
  203. case 'w':
  204. $failed_tests_file = fopen($argv[++$i], 'w+t');
  205. break;
  206. case 'a':
  207. $failed_tests_file = fopen($argv[++$i], 'a+t');
  208. break;
  209. case 'n':
  210. if (!$pass_option_n) {
  211. $pass_options .= ' -n';
  212. }
  213. $pass_option_n = true;
  214. break;
  215. case 'd':
  216. $ini_overwrites[] = $argv[++$i];
  217. break;
  218. default:
  219. echo "Illegal switch specified!\n";
  220. case "h":
  221. case "help":
  222. case "-help":
  223. echo <<<HELP
  224. Synopsis:
  225. php run-tests.php [options] [files] [directories]
  226. Options:
  227. -l <file> Read the testfiles to be executed from <file>. After the test
  228. has finished all failed tests are written to the same <file>.
  229. If the list is empty and no further test is specified then
  230. all tests are executed.
  231. -r <file> Read the testfiles to be executed from <file>.
  232. -w <file> Write a list of all failed tests to <file>.
  233. -a <file> Same as -w but append rather then truncating <file>.
  234. -n Pass -n option to the php binary (Do not use a php.ini).
  235. -d foo=bar Pass -d option to the php binary (Define INI entry foo
  236. with value 'bar')
  237. -h <file> This Help.
  238. HELP;
  239. exit(1);
  240. }
  241. } else {
  242. $testfile = realpath($argv[$i]);
  243. if (is_dir($testfile)) {
  244. find_files($testfile);
  245. } else if (preg_match("/\.phpt$/", $testfile)) {
  246. $test_files[] = $testfile;
  247. }
  248. }
  249. }
  250. $test_files = array_unique($test_files);
  251. for($i = 0; $i < count($test_files); $i++) {
  252. $test_files[$i] = trim($test_files[$i]);
  253. }
  254. // Run selected tests.
  255. $test_cnt = count($test_files);
  256. if ($test_cnt) {
  257. write_information();
  258. usort($test_files, "test_sort");
  259. echo "Running selected tests.\n";
  260. $start_time = time();
  261. $test_idx = 0;
  262. foreach($test_files AS $name) {
  263. $test_results[$name] = run_test($php,$name,$test_cnt,++$test_idx);
  264. if ($failed_tests_file && ($test_results[$name] == 'FAILED' || $test_results[$name] == 'WARNED')) {
  265. fwrite($failed_tests_file, "$name\n");
  266. }
  267. }
  268. if ($failed_tests_file) {
  269. fclose($failed_tests_file);
  270. }
  271. $end_time = time();
  272. if (count($test_files) > 1) {
  273. echo "
  274. =====================================================================";
  275. compute_summary();
  276. echo get_summary(false);
  277. }
  278. if (getenv('REPORT_EXIT_STATUS') == 1 and ereg('FAILED( |$)', implode(' ', $test_results))) {
  279. exit(1);
  280. }
  281. exit(0);
  282. }
  283. }
  284. write_information();
  285. // Compile a list of all test files (*.phpt).
  286. $test_files = array();
  287. $exts_to_test = get_loaded_extensions();
  288. $exts_tested = count($exts_to_test);
  289. $exts_skipped = 0;
  290. $ignored_by_ext = 0;
  291. sort($exts_to_test);
  292. $test_dirs = array('tests', 'ext');
  293. $optionals = array('pear', 'Zend', 'ZendEngine2');
  294. foreach($optionals as $dir) {
  295. if (@filetype($dir) == 'dir') {
  296. $test_dirs[] = $dir;
  297. }
  298. }
  299. foreach ($test_dirs as $dir) {
  300. find_files("{$cwd}/{$dir}", ($dir == 'ext'));
  301. }
  302. foreach ($user_tests as $dir) {
  303. find_files($dir, ($dir == 'ext'));
  304. }
  305. function find_files($dir,$is_ext_dir=FALSE,$ignore=FALSE)
  306. {
  307. global $test_files, $exts_to_test, $ignored_by_ext, $exts_skipped, $exts_tested;
  308. $o = opendir($dir) or error("cannot open directory: $dir");
  309. while (($name = readdir($o)) !== FALSE) {
  310. if (is_dir("{$dir}/{$name}") && !in_array($name, array('.', '..', 'CVS'))) {
  311. $skip_ext = ($is_ext_dir && !in_array($name, $exts_to_test));
  312. if ($skip_ext) {
  313. $exts_skipped++;
  314. }
  315. find_files("{$dir}/{$name}", FALSE, $ignore || $skip_ext);
  316. }
  317. // Cleanup any left-over tmp files from last run.
  318. if (substr($name, -4) == '.tmp') {
  319. @unlink("$dir/$name");
  320. continue;
  321. }
  322. // Otherwise we're only interested in *.phpt files.
  323. if (substr($name, -5) == '.phpt') {
  324. if ($ignore) {
  325. $ignored_by_ext++;
  326. } else {
  327. $testfile = realpath("{$dir}/{$name}");
  328. $test_files[] = $testfile;
  329. }
  330. }
  331. }
  332. closedir($o);
  333. }
  334. function test_sort($a, $b)
  335. {
  336. global $cwd;
  337. $ta = strpos($a, "{$cwd}/tests")===0 ? 1 + (strpos($a, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  338. $tb = strpos($b, "{$cwd}/tests")===0 ? 1 + (strpos($b, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  339. if ($ta == $tb) {
  340. return strcmp($a, $b);
  341. } else {
  342. return $tb - $ta;
  343. }
  344. }
  345. $test_files = array_unique($test_files);
  346. usort($test_files, "test_sort");
  347. $start_time = time();
  348. echo "TIME START " . date('Y-m-d H:i:s', $start_time) . "
  349. =====================================================================
  350. ";
  351. $test_cnt = count($test_files);
  352. $test_idx = 0;
  353. foreach ($test_files as $name) {
  354. $test_results[$name] = run_test($php,$name,$test_cnt,++$test_idx);
  355. }
  356. $end_time = time();
  357. // Summarize results
  358. if (0 == count($test_results)) {
  359. echo "No tests were run.\n";
  360. return;
  361. }
  362. compute_summary();
  363. echo "
  364. =====================================================================
  365. TIME END " . date('Y-m-d H:i:s', $end_time);
  366. $summary = get_summary(true);
  367. echo $summary;
  368. define('PHP_QA_EMAIL', 'qa-reports@lists.php.net');
  369. define('QA_SUBMISSION_PAGE', 'http://qa.php.net/buildtest-process.php');
  370. /* We got failed Tests, offer the user to send and e-mail to QA team, unless NO_INTERACTION is set */
  371. if (!getenv('NO_INTERACTION')) {
  372. $fp = fopen("php://stdin", "r+");
  373. echo "\nPlease allow this report to be send to the PHP QA\nteam. This will give us a better understanding in how\n";
  374. echo "PHP's test cases are doing.\n";
  375. echo "(choose \"s\" to just save the results to a file)? [Yns]: ";
  376. flush();
  377. $user_input = fgets($fp, 10);
  378. $just_save_results = (strtolower($user_input[0]) == 's');
  379. if ($just_save_results || strlen(trim($user_input)) == 0 || strtolower($user_input[0]) == 'y') {
  380. /*
  381. * Collect information about the host system for our report
  382. * Fetch phpinfo() output so that we can see the PHP enviroment
  383. * Make an archive of all the failed tests
  384. * Send an email
  385. */
  386. /* Ask the user to provide an email address, so that QA team can contact the user */
  387. if (!strncasecmp($user_input, 'y', 1) || strlen(trim($user_input)) == 0) {
  388. 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): ";
  389. flush();
  390. $user_email = trim(fgets($fp, 1024));
  391. $user_email = str_replace("@", " at ", str_replace(".", " dot ", $user_email));
  392. }
  393. $failed_tests_data = '';
  394. $sep = "\n" . str_repeat('=', 80) . "\n";
  395. $failed_tests_data .= $failed_test_summary . "\n";
  396. $failed_tests_data .= $summary . "\n";
  397. if ($sum_results['FAILED']) {
  398. foreach ($PHP_FAILED_TESTS as $test_info) {
  399. $failed_tests_data .= $sep . $test_info['name'] . $test_info['info'];
  400. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['output']));
  401. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['diff']));
  402. $failed_tests_data .= $sep . "\n\n";
  403. }
  404. $status = "failed";
  405. } else {
  406. $status = "success";
  407. }
  408. $failed_tests_data .= "\n" . $sep . 'BUILD ENVIRONMENT' . $sep;
  409. $failed_tests_data .= "OS:\n" . PHP_OS . " - " . php_uname() . "\n\n";
  410. $ldd = $automake = $autoconf = $sys_libtool = $libtool = $compiler = 'N/A';
  411. if (substr(PHP_OS, 0, 3) != "WIN") {
  412. $automake = shell_exec('automake --version');
  413. $autoconf = shell_exec('autoconf --version');
  414. /* Always use the generated libtool - Mac OSX uses 'glibtool' */
  415. $libtool = shell_exec($_SERVER['PWD'] . '/libtool --version');
  416. $sys_libtool = shell_exec('libtool --version');
  417. /* Try the most common flags for 'version' */
  418. $flags = array('-v', '-V', '--version');
  419. $cc_status=0;
  420. foreach($flags AS $flag) {
  421. system(getenv('CC')." $flag >/dev/null 2>&1", $cc_status);
  422. if ($cc_status == 0) {
  423. $compiler = shell_exec(getenv('CC')." $flag 2>&1");
  424. break;
  425. }
  426. }
  427. $ldd = shell_exec("ldd $php");
  428. }
  429. $failed_tests_data .= "Automake:\n$automake\n";
  430. $failed_tests_data .= "Autoconf:\n$autoconf\n";
  431. $failed_tests_data .= "Bundled Libtool:\n$libtool\n";
  432. $failed_tests_data .= "System Libtool:\n$sys_libtool\n";
  433. $failed_tests_data .= "Compiler:\n$compiler\n";
  434. $failed_tests_data .= "Bison:\n". @shell_exec('bison --version'). "\n";
  435. $failed_tests_data .= "Libraries:\n$ldd\n";
  436. $failed_tests_data .= "\n";
  437. if (isset($user_email)) {
  438. $failed_tests_data .= "User's E-mail: ".$user_email."\n\n";
  439. }
  440. $failed_tests_data .= $sep . "PHPINFO" . $sep;
  441. $failed_tests_data .= shell_exec($php.' -dhtml_errors=0 -i');
  442. $compression = 0;
  443. if ($just_save_results || !mail_qa_team($failed_tests_data, $compression, $status)) {
  444. $output_file = $CUR_DIR . '/php_test_results_' . date('Ymd_Hi') . ( $compression ? '.txt.gz' : '.txt' );
  445. $fp = fopen($output_file, "w");
  446. fwrite($fp, $failed_tests_data);
  447. fclose($fp);
  448. if (!$just_save_results) {
  449. echo "\nThe test script was unable to automatically send the report to PHP's QA Team\n";
  450. }
  451. echo "Please send ".$output_file." to ".PHP_QA_EMAIL." manually, thank you.\n";
  452. } else {
  453. fwrite($fp, "\nThank you for helping to make PHP better.\n");
  454. fclose($fp);
  455. }
  456. }
  457. }
  458. if (getenv('REPORT_EXIT_STATUS') == 1 and $sum_results['FAILED']) {
  459. exit(1);
  460. }
  461. //
  462. // Send Email to QA Team
  463. //
  464. function mail_qa_team($data, $compression, $status = FALSE)
  465. {
  466. $url_bits = parse_url(QA_SUBMISSION_PAGE);
  467. if (empty($url_bits['port'])) $url_bits['port'] = 80;
  468. $data = "php_test_data=" . urlencode(base64_encode(preg_replace("/[\\x00]/", "[0x0]", $data)));
  469. $data_length = strlen($data);
  470. $fs = fsockopen($url_bits['host'], $url_bits['port'], $errno, $errstr, 10);
  471. if (!$fs) {
  472. return FALSE;
  473. }
  474. $php_version = urlencode(TESTED_PHP_VERSION);
  475. echo "\nPosting to {$url_bits['host']} {$url_bits['path']}\n";
  476. fwrite($fs, "POST ".$url_bits['path']."?status=$status&version=$php_version HTTP/1.1\r\n");
  477. fwrite($fs, "Host: ".$url_bits['host']."\r\n");
  478. fwrite($fs, "User-Agent: QA Browser 0.1\r\n");
  479. fwrite($fs, "Content-Type: application/x-www-form-urlencoded\r\n");
  480. fwrite($fs, "Content-Length: ".$data_length."\r\n\r\n");
  481. fwrite($fs, $data);
  482. fwrite($fs, "\r\n\r\n");
  483. fclose($fs);
  484. return 1;
  485. }
  486. //
  487. // Write the given text to a temporary file, and return the filename.
  488. //
  489. function save_text($filename,$text)
  490. {
  491. $fp = @fopen($filename,'w') or error("Cannot open file '" . $filename . "' (save_text)");
  492. fwrite($fp,$text);
  493. fclose($fp);
  494. if (1 < DETAILED) echo "
  495. FILE $filename {{{
  496. $text
  497. }}}
  498. ";
  499. }
  500. //
  501. // Write an error in a format recognizable to Emacs or MSVC.
  502. //
  503. function error_report($testname,$logname,$tested)
  504. {
  505. $testname = realpath($testname);
  506. $logname = realpath($logname);
  507. switch (strtoupper(getenv('TEST_PHP_ERROR_STYLE'))) {
  508. case 'MSVC':
  509. echo $testname . "(1) : $tested\n";
  510. echo $logname . "(1) : $tested\n";
  511. break;
  512. case 'EMACS':
  513. echo $testname . ":1: $tested\n";
  514. echo $logname . ":1: $tested\n";
  515. break;
  516. }
  517. }
  518. function system_with_timeout($commandline)
  519. {
  520. $data = "";
  521. $proc = proc_open($commandline, array(
  522. 0 => array('pipe', 'r'),
  523. 1 => array('pipe', 'w'),
  524. 2 => array('pipe', 'w')
  525. ), $pipes, null, null, array("suppress_errors" => true));
  526. if (!$proc)
  527. return false;
  528. fclose($pipes[0]);
  529. while (true) {
  530. /* hide errors from interrupted syscalls */
  531. $r = $pipes;
  532. $w = null;
  533. $e = null;
  534. $n = @stream_select($r, $w, $e, 60);
  535. if ($n === 0) {
  536. /* timed out */
  537. $data .= "\n ** ERROR: process timed out **\n";
  538. proc_terminate($proc);
  539. return $data;
  540. } else if ($n > 0) {
  541. $line = fread($pipes[1], 8192);
  542. if (strlen($line) == 0) {
  543. /* EOF */
  544. break;
  545. }
  546. $data .= $line;
  547. }
  548. }
  549. $stat = proc_get_status($proc);
  550. if ($stat['signaled']) {
  551. $data .= "\nTermsig=".$stat['stopsig'];
  552. }
  553. $code = proc_close($proc);
  554. return $data;
  555. }
  556. //
  557. // Run an individual test case.
  558. //
  559. function run_test($php, $file, $test_cnt, $test_idx)
  560. {
  561. global $log_format, $info_params, $ini_overwrites, $cwd, $PHP_FAILED_TESTS, $pass_options;
  562. if (DETAILED) echo "
  563. =================
  564. TEST $file
  565. ";
  566. // Load the sections of the test file.
  567. $section_text = array(
  568. 'TEST' => '',
  569. 'SKIPIF' => '',
  570. 'GET' => '',
  571. 'ARGS' => '',
  572. );
  573. $fp = @fopen($file, "r") or error("Cannot open test file: $file");
  574. if (!feof($fp)) {
  575. $line = fgets($fp);
  576. } else {
  577. echo "BORK empty test [$file]\n";
  578. return 'BORKED';
  579. }
  580. if (!ereg('^--TEST--',$line,$r)) {
  581. echo "BORK tests must start with --TEST-- [$file]\n";
  582. return 'BORKED';
  583. }
  584. $section = 'TEST';
  585. while (!feof($fp)) {
  586. $line = fgets($fp);
  587. // Match the beginning of a section.
  588. if (ereg('^--([A-Z]+)--',$line,$r)) {
  589. $section = $r[1];
  590. $section_text[$section] = '';
  591. continue;
  592. }
  593. // Add to the section text.
  594. $section_text[$section] .= $line;
  595. }
  596. if (@count($section_text['FILE']) != 1) {
  597. echo "BORK missing section --FILE-- [$file]\n";
  598. return 'BORKED';
  599. }
  600. if ((@count($section_text['EXPECT']) + @count($section_text['EXPECTF']) + @count($section_text['EXPECTREGEX'])) != 1) {
  601. echo "BORK missing section --EXPECT--, --EXPECTF-- or --EXPECTREGEX-- [$file]\n";
  602. return 'BORKED';
  603. }
  604. fclose($fp);
  605. /* For GET/POST tests, check if cgi sapi is available and if it is, use it. */
  606. if ((!empty($section_text['GET']) || !empty($section_text['POST']))) {
  607. if (file_exists("./sapi/cgi/php")) {
  608. $old_php = $php;
  609. $php = realpath("./sapi/cgi/php") . ' -C ';
  610. }
  611. }
  612. $shortname = str_replace($cwd.'/', '', $file);
  613. $tested = trim($section_text['TEST'])." [$shortname]";
  614. echo "TEST $test_idx/$test_cnt [$shortname]\r";
  615. flush();
  616. $tmp = realpath(dirname($file));
  617. $tmp_skipif = $tmp . uniqid('/phpt.');
  618. $tmp_file = ereg_replace('\.phpt$','.php',$file);
  619. $tmp_post = $tmp . uniqid('/phpt.');
  620. @unlink($tmp_skipif);
  621. @unlink($tmp_file);
  622. @unlink($tmp_post);
  623. // unlink old test results
  624. @unlink(ereg_replace('\.phpt$','.diff',$file));
  625. @unlink(ereg_replace('\.phpt$','.log',$file));
  626. @unlink(ereg_replace('\.phpt$','.exp',$file));
  627. @unlink(ereg_replace('\.phpt$','.out',$file));
  628. // Reset environment from any previous test.
  629. putenv("REDIRECT_STATUS=");
  630. putenv("QUERY_STRING=");
  631. putenv("PATH_TRANSLATED=");
  632. putenv("SCRIPT_FILENAME=");
  633. putenv("REQUEST_METHOD=");
  634. putenv("CONTENT_TYPE=");
  635. putenv("CONTENT_LENGTH=");
  636. // Check if test should be skipped.
  637. $info = '';
  638. $warn = false;
  639. if (array_key_exists('SKIPIF', $section_text)) {
  640. if (trim($section_text['SKIPIF'])) {
  641. save_text($tmp_skipif, $section_text['SKIPIF']);
  642. $extra = substr(PHP_OS, 0, 3) !== "WIN" ?
  643. "unset REQUEST_METHOD; unset QUERY_STRING; unset PATH_TRANSLATED; unset SCRIPT_FILENAME; unset REQUEST_METHOD;": "";
  644. $output = system_with_timeout("$extra $php -q $info_params $tmp_skipif");
  645. @unlink($tmp_skipif);
  646. if (eregi("^skip", trim($output))) {
  647. echo "SKIP $tested";
  648. $reason = (eregi("^skip[[:space:]]*(.+)\$", trim($output))) ? eregi_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  649. if ($reason) {
  650. echo " (reason: $reason)\n";
  651. } else {
  652. echo "\n";
  653. }
  654. if (isset($old_php)) {
  655. $php = $old_php;
  656. }
  657. return 'SKIPPED';
  658. }
  659. if (eregi("^info", trim($output))) {
  660. $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  661. if ($reason) {
  662. $info = " (info: $reason)";
  663. }
  664. }
  665. if (eregi("^warn", trim($output))) {
  666. $reason = (ereg("^warn[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^warn[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  667. if ($reason) {
  668. $warn = true; /* only if there is a reason */
  669. $info = " (warn: $reason)";
  670. }
  671. }
  672. }
  673. }
  674. // Default ini settings
  675. $ini_settings = array();
  676. // additional ini overwrites
  677. //$ini_overwrites[] = 'setting=value';
  678. settings2array($ini_overwrites, $ini_settings);
  679. // Any special ini settings
  680. // these may overwrite the test defaults...
  681. if (array_key_exists('INI', $section_text)) {
  682. settings2array(preg_split( "/[\n\r]+/", $section_text['INI']), $ini_settings);
  683. }
  684. settings2params($ini_settings);
  685. // We've satisfied the preconditions - run the test!
  686. save_text($tmp_file,$section_text['FILE']);
  687. if (array_key_exists('GET', $section_text)) {
  688. $query_string = trim($section_text['GET']);
  689. } else {
  690. $query_string = '';
  691. }
  692. putenv("REDIRECT_STATUS=1");
  693. putenv("QUERY_STRING=$query_string");
  694. putenv("PATH_TRANSLATED=$tmp_file");
  695. putenv("SCRIPT_FILENAME=$tmp_file");
  696. $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
  697. if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
  698. $post = trim($section_text['POST']);
  699. save_text($tmp_post,$post);
  700. $content_length = strlen($post);
  701. putenv("REQUEST_METHOD=POST");
  702. putenv("CONTENT_TYPE=application/x-www-form-urlencoded");
  703. putenv("CONTENT_LENGTH=$content_length");
  704. $cmd = "$php$pass_options$ini_settings -f \"$tmp_file\" 2>&1 < $tmp_post";
  705. } else {
  706. putenv("REQUEST_METHOD=GET");
  707. putenv("CONTENT_TYPE=");
  708. putenv("CONTENT_LENGTH=");
  709. $cmd = "$php$pass_options$ini_settings -f \"$tmp_file\" $args 2>&1";
  710. }
  711. if (DETAILED) echo "
  712. CONTENT_LENGTH = " . getenv("CONTENT_LENGTH") . "
  713. CONTENT_TYPE = " . getenv("CONTENT_TYPE") . "
  714. PATH_TRANSLATED = " . getenv("PATH_TRANSLATED") . "
  715. QUERY_STRING = " . getenv("QUERY_STRING") . "
  716. REDIRECT_STATUS = " . getenv("REDIRECT_STATUS") . "
  717. REQUEST_METHOD = " . getenv("REQUEST_METHOD") . "
  718. SCRIPT_FILENAME = " . getenv("SCRIPT_FILENAME") . "
  719. COMMAND $cmd
  720. ";
  721. // $out = `$cmd`;
  722. $out = system_with_timeout($cmd);
  723. @unlink($tmp_post);
  724. // Does the output match what is expected?
  725. $output = trim($out);
  726. $output = preg_replace('/\r\n/',"\n",$output);
  727. /* when using CGI, strip the headers from the output */
  728. if (isset($old_php) && ($pos = strpos($output, "\n\n")) !== FALSE) {
  729. $output = substr($output, ($pos + 2));
  730. }
  731. if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
  732. if (isset($section_text['EXPECTF'])) {
  733. $wanted = trim($section_text['EXPECTF']);
  734. } else {
  735. $wanted = trim($section_text['EXPECTREGEX']);
  736. }
  737. $wanted_re = preg_replace('/\r\n/',"\n",$wanted);
  738. if (isset($section_text['EXPECTF'])) {
  739. $wanted_re = preg_quote($wanted_re, '/');
  740. // Stick to basics
  741. $wanted_re = str_replace("%e", '\\' . DIRECTORY_SEPARATOR, $wanted_re);
  742. $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
  743. $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
  744. $wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
  745. $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
  746. $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re);
  747. $wanted_re = str_replace("%c", ".", $wanted_re);
  748. // %f allows two points "-.0.0" but that is the best *simple* expression
  749. }
  750. /* DEBUG YOUR REGEX HERE
  751. var_dump($wanted_re);
  752. print(str_repeat('=', 80) . "\n");
  753. var_dump($output);
  754. */
  755. if (preg_match("/^$wanted_re\$/s", $output)) {
  756. @unlink($tmp_file);
  757. echo "PASS $tested\n";
  758. if (isset($old_php)) {
  759. $php = $old_php;
  760. }
  761. return 'PASSED';
  762. }
  763. } else {
  764. $wanted = trim($section_text['EXPECT']);
  765. $wanted = preg_replace('/\r\n/',"\n",$wanted);
  766. // compare and leave on success
  767. $ok = (0 == strcmp($output,$wanted));
  768. if ($ok) {
  769. @unlink($tmp_file);
  770. echo "PASS $tested\n";
  771. if (isset($old_php)) {
  772. $php = $old_php;
  773. }
  774. return 'PASSED';
  775. }
  776. $wanted_re = NULL;
  777. }
  778. // Test failed so we need to report details.
  779. if ($warn) {
  780. echo "WARN $tested$info\n";
  781. } else {
  782. echo "FAIL $tested$info\n";
  783. }
  784. $PHP_FAILED_TESTS[] = array(
  785. 'name' => $file,
  786. 'test_name' => $tested,
  787. 'output' => ereg_replace('\.phpt$','.log', $file),
  788. 'diff' => ereg_replace('\.phpt$','.diff', $file),
  789. 'info' => $info
  790. );
  791. // write .exp
  792. if (strpos($log_format,'E') !== FALSE) {
  793. $logname = ereg_replace('\.phpt$','.exp',$file);
  794. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  795. fwrite($log,$wanted);
  796. fclose($log);
  797. }
  798. // write .out
  799. if (strpos($log_format,'O') !== FALSE) {
  800. $logname = ereg_replace('\.phpt$','.out',$file);
  801. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  802. fwrite($log,$output);
  803. fclose($log);
  804. }
  805. // write .diff
  806. if (strpos($log_format,'D') !== FALSE) {
  807. $logname = ereg_replace('\.phpt$','.diff',$file);
  808. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  809. fwrite($log,generate_diff($wanted,$wanted_re,$output));
  810. fclose($log);
  811. }
  812. // write .log
  813. if (strpos($log_format,'L') !== FALSE) {
  814. $logname = ereg_replace('\.phpt$','.log',$file);
  815. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  816. fwrite($log,"
  817. ---- EXPECTED OUTPUT
  818. $wanted
  819. ---- ACTUAL OUTPUT
  820. $output
  821. ---- FAILED
  822. ");
  823. fclose($log);
  824. error_report($file,$logname,$tested);
  825. }
  826. if (isset($old_php)) {
  827. $php = $old_php;
  828. }
  829. return $warn ? 'WARNED' : 'FAILED';
  830. }
  831. function comp_line($l1,$l2,$is_reg)
  832. {
  833. if ($is_reg) {
  834. return preg_match('/^'.$l1.'$/s', $l2);
  835. } else {
  836. return !strcmp($l1, $l2);
  837. }
  838. }
  839. function count_array_diff($ar1,$ar2,$is_reg,$w,$idx1,$idx2,$cnt1,$cnt2,$steps)
  840. {
  841. $equal = 0;
  842. while ($idx1 < $cnt1 && $idx2 < $cnt2 && comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
  843. $idx1++;
  844. $idx2++;
  845. $equal++;
  846. $steps--;
  847. }
  848. if (--$steps > 0) {
  849. $eq1 = 0;
  850. $st = $steps / 2;
  851. for ($ofs1 = $idx1+1; $ofs1 < $cnt1 && $st-- > 0; $ofs1++) {
  852. $eq = count_array_diff($ar1,$ar2,$is_reg,$w,$ofs1,$idx2,$cnt1,$cnt2,$st);
  853. if ($eq > $eq1) {
  854. $eq1 = $eq;
  855. }
  856. }
  857. $eq2 = 0;
  858. $st = $steps;
  859. for ($ofs2 = $idx2+1; $ofs2 < $cnt2 && $st-- > 0; $ofs2++) {
  860. $eq = count_array_diff($ar1,$ar2,$is_reg,$w,$idx1,$ofs2,$cnt1,$cnt2,$st);
  861. if ($eq > $eq2) {
  862. $eq2 = $eq;
  863. }
  864. }
  865. if ($eq1 > $eq2) {
  866. $equal += $eq1;
  867. } else if ($eq2 > 0) {
  868. $equal += $eq2;
  869. }
  870. }
  871. return $equal;
  872. }
  873. function generate_array_diff($ar1,$ar2,$is_reg,$w)
  874. {
  875. $idx1 = 0; $ofs1 = 0; $cnt1 = count($ar1);
  876. $idx2 = 0; $ofs2 = 0; $cnt2 = count($ar2);
  877. $diff = array();
  878. $old1 = array();
  879. $old2 = array();
  880. while ($idx1 < $cnt1 && $idx2 < $cnt2) {
  881. if (comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
  882. $idx1++;
  883. $idx2++;
  884. continue;
  885. } else {
  886. $c1 = count_array_diff($ar1,$ar2,$is_reg,$w,$idx1+1,$idx2,$cnt1,$cnt2,10);
  887. $c2 = count_array_diff($ar1,$ar2,$is_reg,$w,$idx1,$idx2+1,$cnt1,$cnt2,10);
  888. if ($c1 > $c2) {
  889. $old1[$idx1] = sprintf("%03d- ", $idx1+1).$w[$idx1++];
  890. $last = 1;
  891. } else if ($c2 > 0) {
  892. $old2[$idx2] = sprintf("%03d+ ", $idx2+1).$ar2[$idx2++];
  893. $last = 2;
  894. } else {
  895. $old1[$idx1] = sprintf("%03d- ", $idx1+1).$w[$idx1++];
  896. $old2[$idx2] = sprintf("%03d+ ", $idx2+1).$ar2[$idx2++];
  897. }
  898. }
  899. }
  900. reset($old1); $k1 = key($old1); $l1 = -2;
  901. reset($old2); $k2 = key($old2); $l2 = -2;
  902. while ($k1 !== NULL || $k2 !== NULL) {
  903. if ($k1 == $l1+1 || $k2 === NULL) {
  904. $l1 = $k1;
  905. $diff[] = current($old1);
  906. $k1 = next($old1) ? key($old1) : NULL;
  907. } else if ($k2 == $l2+1 || $k1 === NULL) {
  908. $l2 = $k2;
  909. $diff[] = current($old2);
  910. $k2 = next($old2) ? key($old2) : NULL;
  911. } else if ($k1 < $k2) {
  912. $l1 = $k1;
  913. $diff[] = current($old1);
  914. $k1 = next($old1) ? key($old1) : NULL;
  915. } else {
  916. $l2 = $k2;
  917. $diff[] = current($old2);
  918. $k2 = next($old2) ? key($old2) : NULL;
  919. }
  920. }
  921. while ($idx1 < $cnt1) {
  922. $diff[] = sprintf("%03d- ", $idx1+1).$w[$idx1++];
  923. }
  924. while ($idx2 < $cnt2) {
  925. $diff[] = sprintf("%03d+ ", $idx2+1).$ar2[$idx2++];
  926. }
  927. return $diff;
  928. }
  929. function generate_diff($wanted,$wanted_re,$output)
  930. {
  931. $w = explode("\n", $wanted);
  932. $o = explode("\n", $output);
  933. $r = is_null($wanted_re) ? $w : explode("\n", $wanted_re);
  934. $diff = generate_array_diff($r,$o,!is_null($wanted_re),$w);
  935. return implode("\r\n", $diff);
  936. }
  937. function error($message)
  938. {
  939. echo "ERROR: {$message}\n";
  940. exit(1);
  941. }
  942. function settings2array($settings, &$ini_settings)
  943. {
  944. foreach($settings as $setting) {
  945. if (strpos($setting, '=')!==false) {
  946. $setting = explode("=", $setting, 2);
  947. $name = trim(strtolower($setting[0]));
  948. $value = trim($setting[1]);
  949. $ini_settings[$name] = $value;
  950. }
  951. }
  952. }
  953. function settings2params(&$ini_settings)
  954. {
  955. $settings = '';
  956. foreach($ini_settings as $name => $value) {
  957. $value = addslashes($value);
  958. $settings .= " -d \"$name=$value\"";
  959. }
  960. $ini_settings = $settings;
  961. }
  962. function compute_summary()
  963. {
  964. global $n_total, $test_results, $ignored_by_ext, $sum_results, $percent_results;
  965. $n_total = count($test_results);
  966. $n_total += $ignored_by_ext;
  967. $sum_results = array('PASSED'=>0, 'WARNED'=>0, 'SKIPPED'=>0, 'FAILED'=>0, 'BORKED'=>0);
  968. foreach ($test_results as $v) {
  969. $sum_results[$v]++;
  970. }
  971. $sum_results['SKIPPED'] += $ignored_by_ext;
  972. $percent_results = array();
  973. while (list($v,$n) = each($sum_results)) {
  974. $percent_results[$v] = (100.0 * $n) / $n_total;
  975. }
  976. }
  977. function get_summary($show_ext_summary)
  978. {
  979. global $exts_skipped, $exts_tested, $n_total, $sum_results, $percent_results, $end_time, $start_time, $failed_test_summary, $PHP_FAILED_TESTS;
  980. $x_total = $n_total - $sum_results['SKIPPED'] - $sum_results['BORKED'];
  981. if ($x_total) {
  982. $x_warned = (100.0 * $sum_results['WARNED']) / $x_total;
  983. $x_failed = (100.0 * $sum_results['FAILED']) / $x_total;
  984. $x_passed = (100.0 * $sum_results['PASSED']) / $x_total;
  985. } else {
  986. $x_warned = $x_failed = $x_passed = 0;
  987. }
  988. $summary = "";
  989. if ($show_ext_summary) {
  990. $summary .= "
  991. =====================================================================
  992. TEST RESULT SUMMARY
  993. ---------------------------------------------------------------------
  994. Exts skipped : " . sprintf("%4d",$exts_skipped) . "
  995. Exts tested : " . sprintf("%4d",$exts_tested) . "
  996. ---------------------------------------------------------------------
  997. ";
  998. }
  999. $summary .= "
  1000. Number of tests : " . sprintf("%4d",$n_total). " " . sprintf("%8d",$x_total);
  1001. if ($sum_results['BORKED']) {
  1002. $summary .= "
  1003. Tests borked : " . sprintf("%4d (%3.1f%%)",$sum_results['BORKED'],$percent_results['BORKED']) . " --------";
  1004. }
  1005. $summary .= "
  1006. Tests skipped : " . sprintf("%4d (%3.1f%%)",$sum_results['SKIPPED'],$percent_results['SKIPPED']) . " --------
  1007. Tests warned : " . sprintf("%4d (%3.1f%%)",$sum_results['WARNED'],$percent_results['WARNED']) . " " . sprintf("(%3.1f%%)",$x_warned) . "
  1008. Tests failed : " . sprintf("%4d (%3.1f%%)",$sum_results['FAILED'],$percent_results['FAILED']) . " " . sprintf("(%3.1f%%)",$x_failed) . "
  1009. Tests passed : " . sprintf("%4d (%3.1f%%)",$sum_results['PASSED'],$percent_results['PASSED']) . " " . sprintf("(%3.1f%%)",$x_passed) . "
  1010. ---------------------------------------------------------------------
  1011. Time taken : " . sprintf("%4d seconds", $end_time - $start_time) . "
  1012. =====================================================================
  1013. ";
  1014. $failed_test_summary = '';
  1015. if (count($PHP_FAILED_TESTS)) {
  1016. $failed_test_summary .= "
  1017. =====================================================================
  1018. FAILED TEST SUMMARY
  1019. ---------------------------------------------------------------------
  1020. ";
  1021. foreach ($PHP_FAILED_TESTS as $failed_test_data) {
  1022. $failed_test_summary .= $failed_test_data['test_name'] . $failed_test_data['info'] . "\n";
  1023. }
  1024. $failed_test_summary .= "=====================================================================\n";
  1025. }
  1026. if ($failed_test_summary && !getenv('NO_PHPTEST_SUMMARY')) {
  1027. $summary .= $failed_test_summary;
  1028. }
  1029. return $summary;
  1030. }
  1031. /*
  1032. * Local variables:
  1033. * tab-width: 4
  1034. * c-basic-offset: 4
  1035. * End:
  1036. * vim: noet sw=4 ts=4
  1037. */
  1038. ?>