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.

826 lines
24 KiB

25 years ago
25 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
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
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
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 4 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2002 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 2.02 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available at through the world-wide-web at |
  11. | http://www.php.net/license/2_02.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@fast.no>) |
  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 avaliable.
  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. // change into the PHP source directory.
  53. if (getenv('TEST_PHP_SRCDIR')) {
  54. @chdir(getenv('TEST_PHP_SRCDIR'));
  55. }
  56. // Delete some security related environment variables
  57. putenv('SSH_CLIENT=deleted');
  58. putenv('SSH_AUTH_SOCK=deleted');
  59. putenv('SSH_TTY=deleted');
  60. $cwd = getcwd();
  61. set_time_limit(0);
  62. // delete as much output buffers as possible
  63. while(@ob_end_clean());
  64. if (ob_get_level()) echo "Not all buffers were deleted.\n";
  65. error_reporting(E_ALL);
  66. ini_set('magic_quotes_runtime',0); // this would break tests by modifying EXPECT sections
  67. if (ini_get('safe_mode')) {
  68. echo <<< SAFE_MODE_WARNING
  69. +-----------------------------------------------------------+
  70. | ! WARNING ! |
  71. | You are running the test-suite with "safe_mode" ENABLED ! |
  72. | |
  73. | Chances are high that no test will work at all, |
  74. | depending on how you configured "safe_mode" ! |
  75. +-----------------------------------------------------------+
  76. SAFE_MODE_WARNING;
  77. }
  78. // Don't ever guess at the PHP executable location.
  79. // Require the explicit specification.
  80. // Otherwise we could end up testing the wrong file!
  81. if (getenv('TEST_PHP_EXECUTABLE')) {
  82. $php = getenv('TEST_PHP_EXECUTABLE');
  83. } else {
  84. error("environment variable TEST_PHP_EXECUTABLE must be set to specify PHP executable!");
  85. }
  86. if (getenv('TEST_PHP_LOG_FORMAT')) {
  87. $log_format = strtoupper(getenv('TEST_PHP_LOG_FORMAT'));
  88. } else {
  89. $log_format = 'LEOD';
  90. }
  91. if (function_exists('is_executable') && !@is_executable($php)) {
  92. error("invalid PHP executable specified by TEST_PHP_EXECUTABLE = " . $php);
  93. }
  94. // Check whether a detailed log is wanted.
  95. if (getenv('TEST_PHP_DETAILED')) {
  96. define('DETAILED', getenv('TEST_PHP_DETAILED'));
  97. } else {
  98. define('DETAILED', 0);
  99. }
  100. // Check whether user test dirs are requested.
  101. if (getenv('TEST_PHP_USER')) {
  102. $user_tests = explode (',', getenv('TEST_PHP_USER'));
  103. } else {
  104. $user_tests = array();
  105. }
  106. // Get info from php
  107. $info_file = realpath(dirname(__FILE__)) . '/run-test-info.php';
  108. @unlink($info_file);
  109. $php_info = '<?php echo "
  110. PHP_SAPI : " . PHP_SAPI . "
  111. PHP_VERSION : " . phpversion() . "
  112. ZEND_VERSION: " . zend_version() . "
  113. PHP_OS : " . PHP_OS . "
  114. INI actual : " . realpath(get_cfg_var("cfg_file_path")) . "
  115. More .INIs : " . (function_exists(\'php_ini_scanned_files\') ? str_replace("\n","", php_ini_scanned_files()) : "** not determined **"); ?>';
  116. save_text($info_file, $php_info);
  117. $ini_overwrites = array(
  118. 'output_handler=',
  119. 'zlib.output_compression=Off',
  120. 'open_basedir=',
  121. 'safe_mode=0',
  122. 'disable_functions=',
  123. 'output_buffering=Off',
  124. 'error_reporting=2047',
  125. 'display_errors=1',
  126. 'log_errors=0',
  127. 'html_errors=0',
  128. 'track_errors=1',
  129. 'report_memleaks=1',
  130. 'report_zend_debug=0',
  131. 'docref_root=/phpmanual/',
  132. 'docref_ext=.html',
  133. 'error_prepend_string=',
  134. 'error_append_string=',
  135. 'auto_prepend_file=',
  136. 'auto_append_file=',
  137. 'magic_quotes_runtime=0',
  138. );
  139. $info_params = array();
  140. settings2array($ini_overwrites,$info_params);
  141. settings2params($info_params);
  142. $php_info = `$php $info_params $info_file`;
  143. @unlink($info_file);
  144. // Write test context information.
  145. echo "
  146. =====================================================================
  147. CWD : $cwd
  148. PHP : $php $php_info
  149. Extra dirs : ";
  150. foreach ($user_tests as $test_dir) {
  151. echo "{$test_dir}\n ";
  152. }
  153. echo "
  154. =====================================================================
  155. ";
  156. // Determine the tests to be run.
  157. $test_files = array();
  158. $test_results = array();
  159. $GLOBALS['__PHP_FAILED_TESTS__'] = array();
  160. // If parameters given assume they represent selected tests to run.
  161. if (isset($argc) && $argc > 1) {
  162. for ($i=1; $i<$argc; $i++) {
  163. $testfile = realpath($argv[$i]);
  164. if (is_dir($testfile)) {
  165. find_files($testfile);
  166. } else if(preg_match("/\.phpt$/", $testfile)) {
  167. $test_files[] = $testfile;
  168. }
  169. }
  170. // Run selected tests.
  171. if (count($test_files)) {
  172. usort($test_files, "test_sort");
  173. echo "Running selected tests.\n";
  174. foreach($test_files AS $name) {
  175. $test_results[$name] = run_test($php,$name);
  176. }
  177. if(getenv('REPORT_EXIT_STATUS') == 1 and ereg('FAILED( |$)', implode(' ', $test_results))) {
  178. exit(1);
  179. }
  180. exit(0);
  181. }
  182. }
  183. // Compile a list of all test files (*.phpt).
  184. $test_files = array();
  185. $exts_to_test = get_loaded_extensions();
  186. $exts_tested = count($exts_to_test);
  187. $exts_skipped = 0;
  188. $ignored_by_ext = 0;
  189. sort($exts_to_test);
  190. $test_dirs = array('tests', 'pear', 'ext');
  191. foreach ($test_dirs as $dir) {
  192. find_files("{$cwd}/{$dir}", ($dir == 'ext'));
  193. }
  194. foreach ($user_tests as $dir) {
  195. find_files($dir, ($dir == 'ext'));
  196. }
  197. function find_files($dir,$is_ext_dir=FALSE,$ignore=FALSE)
  198. {
  199. global $test_files, $exts_to_test, $ignored_by_ext, $exts_skipped, $exts_tested;
  200. $o = opendir($dir) or error("cannot open directory: $dir");
  201. while (($name = readdir($o)) !== FALSE) {
  202. if (is_dir("{$dir}/{$name}") && !in_array($name, array('.', '..', 'CVS'))) {
  203. $skip_ext = ($is_ext_dir && !in_array($name, $exts_to_test));
  204. if ($skip_ext) {
  205. $exts_skipped++;
  206. }
  207. find_files("{$dir}/{$name}", FALSE, $ignore || $skip_ext);
  208. }
  209. // Cleanup any left-over tmp files from last run.
  210. if (substr($name, -4) == '.tmp') {
  211. @unlink("$dir/$name");
  212. continue;
  213. }
  214. // Otherwise we're only interested in *.phpt files.
  215. if (substr($name, -5) == '.phpt') {
  216. if ($ignore) {
  217. $ignored_by_ext++;
  218. } else {
  219. $testfile = realpath("{$dir}/{$name}");
  220. $test_files[] = $testfile;
  221. }
  222. }
  223. }
  224. closedir($o);
  225. }
  226. function test_sort($a, $b) {
  227. global $cwd;
  228. $ta = strpos($a, "{$cwd}/tests")===0 ? 1 + (strpos($a, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  229. $tb = strpos($b, "{$cwd}/tests")===0 ? 1 + (strpos($b, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
  230. if ($ta == $tb) {
  231. return strcmp($a, $b);
  232. } else {
  233. return $tb - $ta;
  234. }
  235. }
  236. usort($test_files, "test_sort");
  237. $start_time = time();
  238. echo "TIME START " . date('Y-m-d H:i:s', $start_time) . "
  239. =====================================================================
  240. ";
  241. foreach ($test_files as $name) {
  242. $test_results[$name] = run_test($php,$name);
  243. }
  244. $end_time = time();
  245. // Summarize results
  246. if (0 == count($test_results)) {
  247. echo "No tests were run.\n";
  248. return;
  249. }
  250. $n_total = count($test_results);
  251. $n_total += $ignored_by_ext;
  252. $sum_results = array('PASSED'=>0, 'SKIPPED'=>0, 'FAILED'=>0);
  253. foreach ($test_results as $v) {
  254. $sum_results[$v]++;
  255. }
  256. $sum_results['SKIPPED'] += $ignored_by_ext;
  257. $percent_results = array();
  258. while (list($v,$n) = each($sum_results)) {
  259. $percent_results[$v] = (100.0 * $n) / $n_total;
  260. }
  261. echo "
  262. =====================================================================
  263. TIME END " . date('Y-m-d H:i:s', $end_time);
  264. $summary = "
  265. =====================================================================
  266. TEST RESULT SUMMARY
  267. ---------------------------------------------------------------------
  268. Exts skipped : " . sprintf("%4d",$exts_skipped) . "
  269. Exts tested : " . sprintf("%4d",$exts_tested) . "
  270. ---------------------------------------------------------------------
  271. Number of tests : " . sprintf("%4d",$n_total) . "
  272. Tests skipped : " . sprintf("%4d (%2.1f%%)",$sum_results['SKIPPED'],$percent_results['SKIPPED']) . "
  273. Tests failed : " . sprintf("%4d (%2.1f%%)",$sum_results['FAILED'],$percent_results['FAILED']) . "
  274. Tests passed : " . sprintf("%4d (%2.1f%%)",$sum_results['PASSED'],$percent_results['PASSED']) . "
  275. ---------------------------------------------------------------------
  276. Time taken : " . sprintf("%4d seconds", $end_time - $start_time) . "
  277. =====================================================================
  278. ";
  279. echo $summary;
  280. $failed_test_summary = '';
  281. if (count($GLOBALS['__PHP_FAILED_TESTS__'])) {
  282. $failed_test_summary .= "
  283. =====================================================================
  284. FAILED TEST SUMMARY
  285. ---------------------------------------------------------------------
  286. ";
  287. foreach ($GLOBALS['__PHP_FAILED_TESTS__'] as $failed_test_data) {
  288. $failed_test_summary .= $failed_test_data['test_name'] . "\n";
  289. }
  290. $failed_test_summary .= "=====================================================================\n";
  291. }
  292. if ($failed_test_summary && !getenv('NO_PHPTEST_SUMMARY')) {
  293. echo $failed_test_summary;
  294. }
  295. define('PHP_QA_EMAIL', 'php-qa@lists.php.net');
  296. define('QA_SUBMISSION_PAGE', 'http://qa.php.net/buildtest-process.php');
  297. /* We got failed Tests, offer the user to send and e-mail to QA team, unless NO_INTERACTION is set */
  298. if (!getenv('NO_INTERACTION')) {
  299. $fp = fopen("php://stdin", "r+");
  300. echo "\nPlease allow this report to be send to the PHP QA\nteam. This will give us a better understanding in how\n";
  301. echo "PHP's test cases are doing.\n";
  302. echo "(choose \"s\" to just save the results to a file)? [Yns]: ";
  303. flush();
  304. $user_input = fgets($fp, 10);
  305. $just_save_results = (strtolower($user_input[0]) == 's');
  306. if ($just_save_results || strlen(trim($user_input)) == 0 || strtolower($user_input[0]) == 'y') {
  307. /*
  308. * Collect information about the host system for our report
  309. * Fetch phpinfo() output so that we can see the PHP enviroment
  310. * Make an archive of all the failed tests
  311. * Send an email
  312. */
  313. /* Ask the user to provide an email address, so that QA team can contact the user */
  314. if (!strncasecmp($user_input, 'y', 1) || strlen(trim($user_input)) == 0) {
  315. 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): ";
  316. flush();
  317. $fp = fopen("php://stdin", "r+");
  318. $user_email = trim(fgets($fp, 1024));
  319. $user_email = str_replace("@", " at ", str_replace(".", " dot ", $user_email));
  320. }
  321. $failed_tests_data = '';
  322. $sep = "\n" . str_repeat('=', 80) . "\n";
  323. $failed_tests_data .= $failed_test_summary . "\n";
  324. $failed_tests_data .= $summary . "\n";
  325. if ($sum_results['FAILED']) {
  326. foreach ($GLOBALS['__PHP_FAILED_TESTS__'] as $test_info) {
  327. $failed_tests_data .= $sep . $test_info['name'];
  328. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['output']));
  329. $failed_tests_data .= $sep . file_get_contents(realpath($test_info['diff']));
  330. $failed_tests_data .= $sep . "\n\n";
  331. }
  332. $status = "failed";
  333. } else {
  334. $status = "success";
  335. }
  336. $failed_tests_data .= "\n" . $sep . 'BUILD ENVIRONMENT' . $sep;
  337. $failed_tests_data .= "OS:\n". PHP_OS. "\n\n";
  338. $automake = $autoconf = $libtool = $compiler = 'N/A';
  339. if (substr(PHP_OS, 0, 3) != "WIN") {
  340. $automake = shell_exec('automake --version');
  341. $autoconf = shell_exec('autoconf --version');
  342. /* Always use the generated libtool - Mac OSX uses 'glibtool' */
  343. $libtool = shell_exec('./libtool --version');
  344. /* Try the most common flags for 'version' */
  345. $flags = array('-v', '-V', '--version');
  346. $cc_status=0;
  347. foreach($flags AS $flag) {
  348. system(getenv('CC')." $flag >/dev/null 2>&1", $cc_status);
  349. if($cc_status == 0) {
  350. $compiler = shell_exec(getenv('CC')." $flag 2>&1");
  351. break;
  352. }
  353. }
  354. }
  355. $failed_tests_data .= "Automake:\n$automake\n";
  356. $failed_tests_data .= "Autoconf:\n$autoconf\n";
  357. $failed_tests_data .= "Libtool:\n$libtool\n";
  358. $failed_tests_data .= "Compiler:\n$compiler\n";
  359. $failed_tests_data .= "Bison:\n". @shell_exec('bison --version'). "\n";
  360. $failed_tests_data .= "\n";
  361. if (isset($user_email)) {
  362. $failed_tests_data .= "User's E-mail: ".$user_email."\n\n";
  363. }
  364. $failed_tests_data .= $sep . "PHPINFO" . $sep;
  365. $failed_tests_data .= shell_exec($php.' -dhtml_errors=0 -i');
  366. $compression = 0;
  367. if ($just_save_results || !mail_qa_team($failed_tests_data, $compression, $status)) {
  368. $output_file = 'php_test_results_' . date('Ymd') . ( $compression ? '.txt.gz' : '.txt' );
  369. $fp = fopen($output_file, "w");
  370. fwrite($fp, $failed_tests_data);
  371. fclose($fp);
  372. if (!$just_save_results)
  373. echo "\nThe test script was unable to automatically send the report to PHP's QA Team\n";
  374. echo "Please send ".$output_file." to ".PHP_QA_EMAIL." manually, thank you.\n";
  375. } else {
  376. fwrite($fp, "\nThank you for helping to make PHP better.\n");
  377. fclose($fp);
  378. }
  379. }
  380. }
  381. if(getenv('REPORT_EXIT_STATUS') == 1 and $sum_results['FAILED']) {
  382. exit(1);
  383. }
  384. //
  385. // Send Email to QA Team
  386. //
  387. function mail_qa_team($data, $compression, $status = FALSE)
  388. {
  389. $url_bits = parse_url(QA_SUBMISSION_PAGE);
  390. if (empty($url_bits['port'])) $url_bits['port'] = 80;
  391. $data = "php_test_data=" . urlencode(base64_encode(preg_replace("/[\\x00]/", "[0x0]", $data)));
  392. $data_length = strlen($data);
  393. $fs = fsockopen($url_bits['host'], $url_bits['port'], $errno, $errstr, 10);
  394. if (!$fs) {
  395. return FALSE;
  396. }
  397. echo "\nPosting to {$url_bits['host']} {$url_bits['path']}\n";
  398. fwrite($fs, "POST ".$url_bits['path']."?status=$status HTTP/1.1\r\n");
  399. fwrite($fs, "Host: ".$url_bits['host']."\r\n");
  400. fwrite($fs, "User-Agent: QA Browser 0.1\r\n");
  401. fwrite($fs, "Content-Type: application/x-www-form-urlencoded\r\n");
  402. fwrite($fs, "Content-Length: ".$data_length."\r\n\r\n");
  403. fwrite($fs, $data);
  404. fwrite($fs, "\r\n\r\n");
  405. fclose($fs);
  406. return 1;
  407. }
  408. //
  409. // Write the given text to a temporary file, and return the filename.
  410. //
  411. function save_text($filename,$text)
  412. {
  413. $fp = @fopen($filename,'w') or error("Cannot open file '" . $filename . "' (save_text)");
  414. fwrite($fp,$text);
  415. fclose($fp);
  416. if (1 < DETAILED) echo "
  417. FILE $filename {{{
  418. $text
  419. }}}
  420. ";
  421. }
  422. //
  423. // Write an error in a format recognizable to Emacs or MSVC.
  424. //
  425. function error_report($testname,$logname,$tested)
  426. {
  427. $testname = realpath($testname);
  428. $logname = realpath($logname);
  429. switch (strtoupper(getenv('TEST_PHP_ERROR_STYLE'))) {
  430. case 'MSVC':
  431. echo $testname . "(1) : $tested\n";
  432. echo $logname . "(1) : $tested\n";
  433. break;
  434. case 'EMACS':
  435. echo $testname . ":1: $tested\n";
  436. echo $logname . ":1: $tested\n";
  437. break;
  438. }
  439. }
  440. //
  441. // Run an individual test case.
  442. //
  443. function run_test($php,$file)
  444. {
  445. global $log_format, $info_params, $ini_overwrites;
  446. if (DETAILED) echo "
  447. =================
  448. TEST $file
  449. ";
  450. // Load the sections of the test file.
  451. $section_text = array(
  452. 'TEST' => '(unnamed test)',
  453. 'SKIPIF' => '',
  454. 'GET' => '',
  455. 'ARGS' => '',
  456. );
  457. $fp = @fopen($file, "r") or error("Cannot open test file: $file");
  458. $section = '';
  459. while (!feof($fp)) {
  460. $line = fgets($fp);
  461. // Match the beginning of a section.
  462. if (ereg('^--([A-Z]+)--',$line,$r)) {
  463. $section = $r[1];
  464. $section_text[$section] = '';
  465. continue;
  466. }
  467. // Add to the section text.
  468. $section_text[$section] .= $line;
  469. }
  470. fclose($fp);
  471. $shortname = str_replace($GLOBALS['cwd'].'/', '', $file);
  472. $tested = trim($section_text['TEST'])." [$shortname]";
  473. $tmp = realpath(dirname($file));
  474. $tmp_skipif = $tmp . uniqid('/phpt.');
  475. $tmp_file = ereg_replace('\.phpt$','.php',$file);
  476. $tmp_post = $tmp . uniqid('/phpt.');
  477. @unlink($tmp_skipif);
  478. @unlink($tmp_file);
  479. @unlink($tmp_post);
  480. // unlink old test results
  481. @unlink(ereg_replace('\.phpt$','.diff',$file));
  482. @unlink(ereg_replace('\.phpt$','.log',$file));
  483. @unlink(ereg_replace('\.phpt$','.exp',$file));
  484. @unlink(ereg_replace('\.phpt$','.out',$file));
  485. // Reset environment from any previous test.
  486. putenv("REDIRECT_STATUS=");
  487. putenv("QUERY_STRING=");
  488. putenv("PATH_TRANSLATED=");
  489. putenv("SCRIPT_FILENAME=");
  490. putenv("REQUEST_METHOD=");
  491. putenv("CONTENT_TYPE=");
  492. putenv("CONTENT_LENGTH=");
  493. // Check if test should be skipped.
  494. if (array_key_exists('SKIPIF', $section_text)) {
  495. if (trim($section_text['SKIPIF'])) {
  496. save_text($tmp_skipif, $section_text['SKIPIF']);
  497. $output = `$php $info_params $tmp_skipif`;
  498. @unlink($tmp_skipif);
  499. if (eregi("^skip", trim($output))) {
  500. echo "SKIP $tested";
  501. $reason = (ereg("^skip[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  502. if ($reason) {
  503. echo " (reason: $reason)\n";
  504. } else {
  505. echo "\n";
  506. }
  507. return 'SKIPPED';
  508. }
  509. if (eregi("^info", trim($output))) {
  510. $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
  511. if ($reason) {
  512. $tested .= " (info: $reason)";
  513. }
  514. }
  515. }
  516. }
  517. // Default ini settings
  518. $ini_settings = array();
  519. // additional ini overwrites
  520. //$ini_overwrites[] = 'setting=value';
  521. settings2array($ini_overwrites, $ini_settings);
  522. // Any special ini settings
  523. // these may overwrite the test defaults...
  524. if (array_key_exists('INI', $section_text)) {
  525. settings2array(preg_split( "/[\n\r]+/", $section_text['INI']), $ini_settings);
  526. }
  527. settings2params($ini_settings);
  528. // We've satisfied the preconditions - run the test!
  529. save_text($tmp_file,$section_text['FILE']);
  530. if (array_key_exists('GET', $section_text)) {
  531. $query_string = trim($section_text['GET']);
  532. } else {
  533. $query_string = '';
  534. }
  535. putenv("REDIRECT_STATUS=1");
  536. putenv("QUERY_STRING=$query_string");
  537. putenv("PATH_TRANSLATED=$tmp_file");
  538. putenv("SCRIPT_FILENAME=$tmp_file");
  539. $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
  540. if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
  541. $post = trim($section_text['POST']);
  542. save_text($tmp_post,$post);
  543. $content_length = strlen($post);
  544. putenv("REQUEST_METHOD=POST");
  545. putenv("CONTENT_TYPE=application/x-www-form-urlencoded");
  546. putenv("CONTENT_LENGTH=$content_length");
  547. $cmd = "$php$ini_settings -f $tmp_file 2>&1 < $tmp_post";
  548. } else {
  549. putenv("REQUEST_METHOD=GET");
  550. putenv("CONTENT_TYPE=");
  551. putenv("CONTENT_LENGTH=");
  552. $cmd = "$php$ini_settings -f $tmp_file$args 2>&1";
  553. }
  554. if (DETAILED) echo "
  555. CONTENT_LENGTH = " . getenv("CONTENT_LENGTH") . "
  556. CONTENT_TYPE = " . getenv("CONTENT_TYPE") . "
  557. PATH_TRANSLATED = " . getenv("PATH_TRANSLATED") . "
  558. QUERY_STRING = " . getenv("QUERY_STRING") . "
  559. REDIRECT_STATUS = " . getenv("REDIRECT_STATUS") . "
  560. REQUEST_METHOD = " . getenv("REQUEST_METHOD") . "
  561. SCRIPT_FILENAME = " . getenv("SCRIPT_FILENAME") . "
  562. COMMAND $cmd
  563. ";
  564. $out = `$cmd`;
  565. @unlink($tmp_post);
  566. // Does the output match what is expected?
  567. $output = trim($out);
  568. $output = preg_replace('/\r\n/',"\n",$output);
  569. if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
  570. if (isset($section_text['EXPECTF'])) {
  571. $wanted = trim($section_text['EXPECTF']);
  572. } else {
  573. $wanted = trim($section_text['EXPECTREGEX']);
  574. }
  575. $wanted_re = preg_replace('/\r\n/',"\n",$wanted);
  576. if (isset($section_text['EXPECTF'])) {
  577. $wanted_re = preg_quote($wanted_re, '/');
  578. // Stick to basics
  579. $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
  580. $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
  581. $wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
  582. $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
  583. $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*", $wanted_re);
  584. $wanted_re = str_replace("%c", ".", $wanted_re);
  585. // %f allows two points "-.0.0" but that is the best *simple* expression
  586. }
  587. /* DEBUG YOUR REGEX HERE
  588. var_dump($wanted_re);
  589. print(str_repeat('=', 80) . "\n");
  590. var_dump($output);
  591. */
  592. if (preg_match("/^$wanted_re\$/s", $output)) {
  593. @unlink($tmp_file);
  594. echo "PASS $tested\n";
  595. return 'PASSED';
  596. }
  597. } else {
  598. $wanted = trim($section_text['EXPECT']);
  599. $wanted = preg_replace('/\r\n/',"\n",$wanted);
  600. // compare and leave on success
  601. $ok = (0 == strcmp($output,$wanted));
  602. if ($ok) {
  603. @unlink($tmp_file);
  604. echo "PASS $tested\n";
  605. return 'PASSED';
  606. }
  607. }
  608. // Test failed so we need to report details.
  609. echo "FAIL $tested\n";
  610. $GLOBALS['__PHP_FAILED_TESTS__'][] = array(
  611. 'name' => $file,
  612. 'test_name' => $tested,
  613. 'output' => ereg_replace('\.phpt$','.log', $file),
  614. 'diff' => ereg_replace('\.phpt$','.diff', $file)
  615. );
  616. // write .exp
  617. if (strpos($log_format,'E') !== FALSE) {
  618. $logname = ereg_replace('\.phpt$','.exp',$file);
  619. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  620. fwrite($log,$wanted);
  621. fclose($log);
  622. }
  623. // write .out
  624. if (strpos($log_format,'O') !== FALSE) {
  625. $logname = ereg_replace('\.phpt$','.out',$file);
  626. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  627. fwrite($log,$output);
  628. fclose($log);
  629. }
  630. // write .diff
  631. if (strpos($log_format,'D') !== FALSE) {
  632. $logname = ereg_replace('\.phpt$','.diff',$file);
  633. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  634. fwrite($log,generate_diff($wanted,$output));
  635. fclose($log);
  636. }
  637. // write .log
  638. if (strpos($log_format,'L') !== FALSE) {
  639. $logname = ereg_replace('\.phpt$','.log',$file);
  640. $log = fopen($logname,'w') or error("Cannot create test log - $logname");
  641. fwrite($log,"
  642. ---- EXPECTED OUTPUT
  643. $wanted
  644. ---- ACTUAL OUTPUT
  645. $output
  646. ---- FAILED
  647. ");
  648. fclose($log);
  649. error_report($file,$logname,$tested);
  650. }
  651. return 'FAILED';
  652. }
  653. function generate_diff($wanted,$output)
  654. {
  655. $w = explode("\n", $wanted);
  656. $o = explode("\n", $output);
  657. $w1 = array_diff_assoc($w,$o);
  658. $o1 = array_diff_assoc($o,$w);
  659. $w2 = array();
  660. $o2 = array();
  661. foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val;
  662. foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val;
  663. $diff = array_merge($w2, $o2);
  664. ksort($diff);
  665. return implode("\r\n", $diff);
  666. }
  667. function error($message)
  668. {
  669. echo "ERROR: {$message}\n";
  670. exit(1);
  671. }
  672. function settings2array($settings, &$ini_settings)
  673. {
  674. foreach($settings as $setting) {
  675. if (strpos($setting, '=')!==false) {
  676. $setting = explode("=", $setting, 2);
  677. $name = trim(strtolower($setting[0]));
  678. $value = trim($setting[1]);
  679. $ini_settings[$name] = $value;
  680. }
  681. }
  682. }
  683. function settings2params(&$ini_settings)
  684. {
  685. if (count($ini_settings)) {
  686. $settings = '';
  687. foreach($ini_settings as $name => $value) {
  688. $value = addslashes($value);
  689. $settings .= " -d \"$name=$value\"";
  690. }
  691. $ini_settings = $settings;
  692. } else {
  693. $ini_settings = '';
  694. }
  695. }
  696. /*
  697. * Local variables:
  698. * tab-width: 4
  699. * c-basic-offset: 4
  700. * End:
  701. * vim600: fdm=marker
  702. * vim: noet sw=4 ts=4
  703. */
  704. ?>