summaryrefslogtreecommitdiff
path: root/run-tests2.php
blob: 66e133ef25c3e55df2788c47b79a771cb47516ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
<?php
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2002 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Ilia Alshanetsky <ilia@php.net>                             |
   |          Preston L. Bannister <pbannister@php.net>                   |
   |          Marcus Boerger <helly@php.net>                              |
   |          Derick Rethans <derick@php.net>                             |
   |          Sander Roobol <sander@php.net>                              |
   |          John Coggeshall <john@php.net>                              |
   | (based on version by: Stig Bakken <ssb@fast.no>)                     |
   | (based on the PHP 3 test framework by Rasmus Lerdorf)                |
   +----------------------------------------------------------------------+
 */
set_time_limit(0);
ob_implicit_flush();
error_reporting(E_ALL);
define('PHP_QA_EMAIL', 'php-qa@lists.php.net');
define('QA_SUBMISSION_PAGE', 'http://qa.php.net/buildtest-process.php');


class testHarness {
	var $cwd;
	var $TEST_PHP_SRCDIR;
	var $TEST_PHP_EXECUTABLE;
	var $TEST_PHP_LOG_FORMAT;
	var $TEST_PHP_DETAILED;
	var $TEST_PHP_ERROR_STYLE;
	var $REPORT_EXIT_STATUS;
	var $NO_PHPTEST_SUMMARY;
	var $NO_INTERACTION;
	
	var $test_to_run = array();
	var $test_files = array();
	var $test_results = array();
	var $failed_tests = array();
	var $exts_to_test;
	var $exts_tested = 0;
	var $exts_skipped = 0;
	var $ignored_by_ext = 0;
	var $test_dirs = array('tests', 'pear', 'ext');
	var $start_time;
	var $end_time;
	
	var $ddash = "=====================================================================";
	var $sdash = "---------------------------------------------------------------------";

	// Default ini settings
	var $ini_overwrites = array(
			'output_handler'=>'',
			'zlib.output_compression'=>'Off',
			'open_basedir'=>'',
			'safe_mode'=>'0',
			'disable_functions'=>'',
			'output_buffering'=>'Off',
			'error_reporting'=>'2047',
			'display_errors'=>'1',
			'log_errors'=>'0',
			'html_errors'=>'0',
			'track_errors'=>'1',
			'report_memleaks'=>'1',
			'report_zend_debug'=>'0',
			'docref_root'=>'/phpmanual/',
			'docref_ext'=>'.html',
			'error_prepend_string'=>'',
			'error_append_string'=>'',
			'auto_prepend_file'=>'',
			'auto_append_file'=>'',
			'magic_quotes_runtime'=>'0',
		);	
	var $env = array(
		'REDIRECT_STATUS'=>'',
		'QUERY_STRING'=>'',
		'PATH_TRANSLATED'=>'',
		'SCRIPT_FILENAME'=>'',
		'REQUEST_METHOD'=>'',
		'CONTENT_TYPE'=>'',
		'CONTENT_LENGTH'=>'',
		);
	var $info_params = array();

	function testHarness() {
		$this->checkPCRE();
		$this->checkSafeMode();
		$this->removeSensitiveEnvVars();
		$this->initializeConfiguration();
		$this->parseArgs();
		# change to working directory
		if ($this->TEST_PHP_SRCDIR) {
			@chdir($this->TEST_PHP_SRCDIR);
		}
		$this->cwd = getcwd();
		$this->isExecutable();
		$this->getInstalledExtensions();
		$this->contextHeader();
		$this->loadFileList();
		$this->run();
		$this->summarizeResults();
	}

	// Use this function to do any displaying of text, so that
	// things can be over-written as necessary.
	
	function writemsg($msg) {
	    
	    echo $msg;
	    
	}
	
	// Another wrapper function, this one should be used any time
	// a particular test passes or fails
	
	function showstatus($item, $status, $reason = '') {
	    
	    switch($status) {
		
		case 'PASSED':
		
		    $this->writemsg("PASSED: $item ($reason)\n");
		    
		    break;
		    
		case 'FAILED':
		    
		    $this->writemsg("FAILED: $item ($reason)\n");
		    
		    break;
		    
		case 'SKIPPED':
		
		    $this->writemsg("SKIPPED: $item ($reason)\n");
		    break;
		    
	    }
	    
	}
	
	function help()
	{
		return "usage: php run-tests.php [options]\n".
				"  -d testpaths            colon seperate path list\n".
				"  -s path                 to php sources\n".
				"  -p path                 to php executable to be tested\n".
				"  -l logformat            LEOD\n".
				"  -t 0|1                  detailed reporting\n".
				"  -e string               error style\n".
				"  -r 0|1                  report exit status\n".
				"  -n 0|1                  print test summary\n".
				"  -i 0|1                  console interaction\n".
				"  -h                      this help\n";
	}
	
	function parseArgs() {
		global $argc;
		global $argv;
		
		if (!isset($argv)) {
			$argv = $_SERVER['argv'];
			$argc = $_SERVER['argc'];
		}
		if (!isset($argc) || $argc < 2) return;
	
		for ($i=1; $i<$argc;) {
			if ($argv[$i][0] != '-') continue;
			$opt = $argv[$i++][1];
			$value = 0;
			if (@$argv[$i][0] != '-') {
				@$value = $argv[$i++];
			}
			switch($opt) {
				case 'd':
					$this->test_dirs = array();
					$paths = split(':|;',$value);
					foreach($paths as $path) {
						$this->test_dirs[] = realpath($path);
					}
					break;
				case 's':
					$this->TEST_PHP_SRCDIR = $value;
					break;
				case 'p':
					$this->TEST_PHP_EXECUTABLE = $value;
					break;
				case 'l':
					$this->TEST_PHP_LOG_FORMAT = $value;
					break;
				case 't':
					$this->TEST_PHP_DETAILED = $value;
					break;
				case 'e':
					$this->TEST_PHP_ERROR_STYLE = strtoupper($value);
					break;
				case 'r':
					$this->REPORT_EXIT_STATUS = $value;
					break;
				case 'n':
					$this->NO_PHPTEST_SUMMARY = $value;
					break;
				case 'i':
					$this->NO_INTERACTION = $value;
					break;
				case 'h':
					print $this->help();
					exit(0);
			}
		}
	}
	
	function removeSensitiveEnvVars()
	{
		# delete sensitive env vars
		putenv('SSH_CLIENT=deleted');
		putenv('SSH_AUTH_SOCK=deleted');
		putenv('SSH_TTY=deleted');
	}
	
	function initializeConfiguration()
	{
		# get config from environment
		$this->TEST_PHP_SRCDIR = getenv('TEST_PHP_SRCDIR');
		$this->TEST_PHP_EXECUTABLE = getenv('TEST_PHP_EXECUTABLE');
		$this->TEST_PHP_LOG_FORMAT = getenv('TEST_PHP_LOG_FORMAT');
		if (!$this->TEST_PHP_LOG_FORMAT) $this->TEST_PHP_LOG_FORMAT = 'LEOD';
		$this->TEST_PHP_DETAILED = getenv('TEST_PHP_DETAILED');
		$this->TEST_PHP_ERROR_STYLE = strtoupper(getenv('TEST_PHP_ERROR_STYLE'));
		$this->REPORT_EXIT_STATUS = getenv('REPORT_EXIT_STATUS');
		$this->NO_PHPTEST_SUMMARY = getenv('NO_PHPTEST_SUMMARY');
		$this->NO_INTERACTION = getenv('NO_INTERACTION');
	}

	function getInstalledExtensions()
	{
		// get the list of installed extensions
		$this->exts_to_test = get_loaded_extensions();
		$this->exts_tested = count($this->exts_to_test);
		sort($this->exts_to_test);
	}

	function test_sort($a, $b) {
		global $cwd;
	
		$ta = strpos($a, "{$cwd}/tests")===0 ? 1 + (strpos($a, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
		$tb = strpos($b, "{$cwd}/tests")===0 ? 1 + (strpos($b, "{$cwd}/tests/run-test")===0 ? 1 : 0) : 0;
		if ($ta == $tb) {
			return strcmp($a, $b);
		} else {
			return $tb - $ta;
		}
	}

	function isExecutable() {
		if (!$this->TEST_PHP_EXECUTABLE ||
			(function_exists('is_executable') &&
			!@is_executable($this->TEST_PHP_EXECUTABLE))) {
			$this->error("invalid PHP executable specified by TEST_PHP_EXECUTABLE  = " .
					$this->TEST_PHP_EXECUTABLE);
			return false;
		}
		return true;
	}
	
	function checkPCRE() {
		if (!extension_loaded("pcre")) {
			echo <<<NO_PCRE_ERROR

+-----------------------------------------------------------+
|                       ! ERROR !                           |
| The test-suite requires that you have pcre extension      |
| enabled. To enable this extension either compile your PHP |
| with --with-pcre-regex or if you have compiled pcre as a  |
| shared module load it via php.ini.                        |
+-----------------------------------------------------------+

NO_PCRE_ERROR;
		exit;
		}
	}
	
	function checkSafeMode() {
		if (ini_get('safe_mode')) {
			$safewarn =  <<< SAFE_MODE_WARNING

+-----------------------------------------------------------+
|                       ! WARNING !                         |
| You are running the test-suite with "safe_mode" ENABLED ! |
|                                                           |
| Chances are high that no test will work at all,           |
| depending on how you configured "safe_mode" !             |
+-----------------------------------------------------------+


SAFE_MODE_WARNING;
			writemsg($safewarn);
			return true;
		}
		return false;
	}
	
	function getExecutableInfo()
	{
		$info_file = realpath(dirname(__FILE__)) . '/run-test-info.php';
		@unlink($info_file);
		$php_info = '<?php echo "
PHP_SAPI    : " . PHP_SAPI . "
PHP_VERSION : " . phpversion() . "
ZEND_VERSION: " . zend_version() . "
PHP_OS      : " . PHP_OS . "
INI actual  : " . realpath(get_cfg_var("cfg_file_path")) . "
More .INIs  : " . (function_exists(\'php_ini_scanned_files\') ? str_replace("\n","", php_ini_scanned_files()) : "** not determined **"); ?>';
		$this->save_text($info_file, $php_info);
		$this->settings2array($this->ini_overwrites,$this->info_params);
		$this->settings2params($this->info_params);
		$php_info = `{$this->TEST_PHP_EXECUTABLE} {$this->info_params} -f $info_file`;
		@unlink($info_file);
		return $php_info;
	}
	
	//
	// Write test context information.
	//
	function contextHeader()
	{
		$ini='';
		if (function_exists('php_ini_scanned_files')) {
			$ini=php_ini_scanned_files();
		}

		$info = $this->getExecutableInfo();
		
		$this->writemsg("\n$this->ddash\n".
			"CWD         : {$this->cwd}\n".
			"PHP         : {$this->TEST_PHP_EXECUTABLE} $info\n".
			"Test Dirs   : ");
		foreach ($this->test_dirs as $test_dir) {
			$this->writemsg("$test_dir\n              ");
		}
		$this->writemsg("\n$this->ddash\n");
	}
	
	function loadFileList()
	{
		foreach ($this->test_dirs as $dir) {
			print "searching {$this->cwd}/{$dir}\n";
			$this->findFilesInDir("{$this->cwd}/$dir", ($dir == 'ext'));
			#$this->findFilesInDir($dir, ($dir == 'ext'));
		}
		usort($this->test_files,array($this,"test_sort"));
		$this->writemsg("found ".count($this->test_files)." files\n");
	}
	
	function findFilesInDir($dir,$is_ext_dir=FALSE,$ignore=FALSE)
	{
		$skip = array('.', '..', 'CVS');
		$o = opendir($dir) or $this->error("cannot open directory: $dir");
		while (($name = readdir($o)) !== FALSE) {
			if (in_array($name, $skip)) continue;
			if (is_dir("$dir/$name")) {
				$skip_ext = ($is_ext_dir && !in_array($name, $this->exts_to_test));
				if ($skip_ext) {
					$this->exts_skipped++;
				}
				$this->findFilesInDir("$dir/$name", FALSE, $ignore || $skip_ext);
			}
	
			// Cleanup any left-over tmp files from last run.
			if (substr($name, -4) == '.tmp') {
				@unlink("$dir/$name");
				continue;
			}
	
			// Otherwise we're only interested in *.phpt files.
			if (substr($name, -5) == '.phpt') {
				if ($ignore) {
					$this->ignored_by_ext++;
				} else {
					$testfile = realpath("$dir/$name");
					$this->test_files[] = $testfile;
				}
			}
		}
		closedir($o);
	}
	
	function runHeader()
	{
		$this->writemsg("TIME START " . date('Y-m-d H:i:s', $this->start_time) . "\n".$this->ddash."\n");
		if (count($this->test_to_run)) {
			$this->writemsg("Running selected tests.\n");
		} else {
			$this->writemsg("Running all test files.\n");
		}
	}
	
	// Probably unnecessary for CLI, but used when overloading a
	// web-based test class
	
	function runFooter()
	{
	    
	}
	
	function run()
	{
		$this->start_time = time();
		$this->runHeader();
		// Run selected tests.
		if (count($this->test_to_run)) {
			
			foreach($this->test_to_run as $name=>$runnable) {
				if(!preg_match("/\.phpt$/", $name))
					continue;
				if ($runnable) {
					$this->test_results[$name] = $this->run_test($name);
				}
			}
		} else {
			foreach ($this->test_files as $name) {
				$this->test_results[$name] = $this->run_test($name);
			}
		}
		$this->end_time = time();
		$this->runFooter();
	}

	function summarizeResults()
	{
		if (count($this->test_results) == 0) {
			$this->writemsg("No tests were run.\n");
			return;
		}
		
		$n_total = count($this->test_results);
		$n_total += $this->ignored_by_ext;
		
		$sum_results = array('PASSED'=>0, 'SKIPPED'=>0, 'FAILED'=>0);
		foreach ($this->test_results as $v) {
			$sum_results[$v]++;
		}
		$sum_results['SKIPPED'] += $this->ignored_by_ext;
		$percent_results = array();
		while (list($v,$n) = each($sum_results)) {
			$percent_results[$v] = (100.0 * $n) / $n_total;
		}
		
		$this->writemsg("\n".$this->ddash."\n".
			"TIME END " . date('Y-m-d H:i:s', $this->end_time) . "\n".
			$this->ddash."\n".
			"TEST RESULT SUMMARY\n".
			$this->sdash."\n".
			"Exts skipped    : " . sprintf("%4d",$this->exts_skipped) . "\n".
			"Exts tested     : " . sprintf("%4d",$this->exts_tested) . "\n".
			$this->sdash."\n".
			"Number of tests : " . sprintf("%4d",$n_total) . "\n".
			"Tests skipped   : " . sprintf("%4d (%2.1f%%)",$sum_results['SKIPPED'],$percent_results['SKIPPED']) . "\n".
			"Tests failed    : " . sprintf("%4d (%2.1f%%)",$sum_results['FAILED'],$percent_results['FAILED']) . "\n".
			"Tests passed    : " . sprintf("%4d (%2.1f%%)",$sum_results['PASSED'],$percent_results['PASSED']) . "\n".
			$this->sdash."\n".
			"Time taken      : " . sprintf("%4d seconds", $this->end_time - $this->start_time) . "\n".
			$this->ddash."\n");
		
		$failed_test_summary = '';
		if ($this->failed_tests) {
			$failed_test_summary .= "\n".$this->ddash."\n".
				"FAILED TEST SUMMARY\n".$this->sdash."\n";
			foreach ($this->failed_tests as $failed_test_data) {
				$failed_test_summary .=  $failed_test_data['test_name'] . "\n";
			}
			$failed_test_summary .=  $this->ddash."\n";
		}
		
		if ($failed_test_summary && !$this->NO_PHPTEST_SUMMARY) {
			$this->writemsg($failed_test_summary);
		}

		/* We got failed Tests, offer the user to send and e-mail to QA team, unless NO_INTERACTION is set */
		if ($sum_results['FAILED'] && !$this->NO_INTERACTION) {
			$fp = fopen("php://stdin", "r+");
			$this->writemsg("\nPlease allow this report to be send to the PHP QA\nteam. This will give us a better understanding in how\n");
			$this->writemsg("PHP's test cases are doing.\n");
			$this->writemsg("(choose \"s\" to just save the results to a file)? [Yns]: ");
			flush();
			$user_input = fgets($fp, 10);
			$just_save_results = (strtolower($user_input[0]) == 's');
			
			if ($just_save_results || strlen(trim($user_input)) == 0 || strtolower($user_input[0]) == 'y') {
				/*  
				 * Collect information about the host system for our report
				 * Fetch phpinfo() output so that we can see the PHP enviroment
				 * Make an archive of all the failed tests
				 * Send an email
				 */

				/* Ask the user to provide an email address, so that QA team can contact the user */
				if (!strncasecmp($user_input, 'y', 1) || strlen(trim($user_input)) == 0) {
					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): ";
					flush();
					$fp = fopen("php://stdin", "r+");
					$user_email = trim(fgets($fp, 1024));
					$user_email = str_replace("@", " at ", str_replace(".", " dot ", $user_email));
				}
		
				$failed_tests_data = '';
				$sep = "\n" . str_repeat('=', 80) . "\n";
				
				$failed_tests_data .= $failed_test_summary . "\n";
				$failed_tests_data .= $summary . "\n";
				
				if (sum($this->failed_tests)) {
					foreach ($this->failed_tests as $test_info) {
						$failed_tests_data .= $sep . $test_info['name'];
						$failed_tests_data .= $sep . $this->getFileContents($test_info['output']);
						$failed_tests_data .= $sep . $this->getFileContents($test_info['diff']);
						$failed_tests_data .= $sep . "\n\n";
					}
					$status = "failed";
				} else {
					$status = "success";
				}
				
				$failed_tests_data .= "\n" . $sep . 'BUILD ENVIRONMENT' . $sep;
				$failed_tests_data .= "OS:\n". PHP_OS. "\n\n";
				$automake = $autoconf = $libtool = $compiler = 'N/A';

				if (substr(PHP_OS, 0, 3) != "WIN") {
					$automake = shell_exec('automake --version');
					$autoconf = shell_exec('autoconf --version');
					/* Always use the generated libtool - Mac OSX uses 'glibtool' */
					$libtool = shell_exec('./libtool --version');
					/* Try the most common flags for 'version' */
					$flags = array('-v', '-V', '--version');
					$cc_status=0;
					foreach($flags AS $flag) {
						system(getenv('CC')." $flag >/dev/null 2>&1", $cc_status);
						if($cc_status == 0) {
							$compiler = shell_exec(getenv('CC')." $flag 2>&1");
							break;
						}
					}
				}
				
				$failed_tests_data .= "Automake:\n$automake\n";
				$failed_tests_data .= "Autoconf:\n$autoconf\n";
				$failed_tests_data .= "Libtool:\n$libtool\n";
				$failed_tests_data .= "Compiler:\n$compiler\n";
				$failed_tests_data .= "Bison:\n". @shell_exec('bison --version'). "\n";
				$failed_tests_data .= "\n\n";

				if (isset($user_email)) {
					$failed_tests_data .= "User's E-mail: ".$user_email."\n\n";
				}

				$failed_tests_data .= $sep . "PHPINFO" . $sep;
				$failed_tests_data .= shell_exec($this->TEST_PHP_EXECUTABLE.' -dhtml_errors=0 -i');
				
				$compression = 0;

				if ($just_save_results || !$this->mail_qa_team($failed_tests_data, $compression, $status)) {
					$output_file = 'php_test_results_' . date('Ymd_Hi') . ( $compression ? '.txt.gz' : '.txt' );
					$fp = fopen($output_file, "w");
					fwrite($fp, $failed_tests_data);
					fclose($fp);
				
					if (!$just_save_results)
						echo "\nThe test script was unable to automatically send the report to PHP's QA Team\n";
					echo "Please send ".$output_file." to ".PHP_QA_EMAIL." manually, thank you.\n";
				} else {
					fwrite($fp, "\nThank you for helping to make PHP better.\n");
					fclose($fp);
				}
			}
		}
		 
		if($this->REPORT_EXIT_STATUS == 1 and $sum_results['FAILED']) {
			exit(1);
		}
	}

	function getFileContents($filename)
	{
		$real_filename = realpath($filename);
		if (function_exists('file_get_contents')) {
			return file_get_contents($real_filename);
		}
		
		$fd = fopen($real_filename, "rb");
		if ($fd) {
			$contents = fread($fd, filesize($real_filename));
			fclose($fd);
			return $contents;
		}
		return NULL;
	}

	function settings2array($settings, &$ini_settings)
	{
		foreach($settings as $setting) {
			if (strpos($setting, '=')!==false) {
				$setting = explode("=", $setting, 2);
				$name = trim(strtolower($setting[0]));
				$value = trim($setting[1]);
				$ini_settings[$name] = $value;
			}
		}
	}

	function settings2params(&$ini_settings)
	{
		if (count($ini_settings)) {
			$settings = '';
			foreach($ini_settings as $name => $value) {
				$value = addslashes($value);
				$settings .= " -d \"$name=$value\"";
			}
			$ini_settings = $settings;
		} else {
			$ini_settings = '';
		}
	}

	function getINISettings(&$section_text)
	{
		$ini_settings = $this->ini_overwrites;
		// Any special ini settings 
		// these may overwrite the test defaults...
		if (array_key_exists('INI', $section_text)) {
			$this->settings2array(preg_split( "/[\n\r]+/", $section_text['INI']), $ini_settings);
		}
		return $this->settings2params($ini_settings);
	}
	
	//
	// Load the sections of the test file.
	//
	function getSectionText($file)
	{
		// Load the sections of the test file.
		$section_text = array(
			'TEST'   => '(unnamed test)',
			'SKIPIF' => '',
			'GET'    => '',
			'ARGS'   => '',
			'_FILE'   => $file,
			'_DIR'    => realpath(dirname($file)),
		);
	
		$fp = @fopen($file, "r")
				or $this->error("Cannot open test file: $file");
	
		$section = '';
		while (!feof($fp)) {
			$line = fgets($fp);
	
			// Match the beginning of a section.
			if (ereg('^--([A-Z]+)--',$line,$r)) {
				$section = $r[1];
				$section_text[$section] = '';
				continue;
			}
			
			// Add to the section text.
			$section_text[$section] .= $line;
		}
		fclose($fp);
		return $section_text;
	}
	
	function setEnvironment($env)
	{
		foreach ($env as $name=>$value) {
			putenv("$name=$value");
		}
	}
	
	//
	// Check if test should be skipped.
	//
	function getSkipReason(&$section_text)
	{
		if (array_key_exists('SKIPIF', $section_text)) {
			$tmp_skipif = $section_text['_DIR'] . uniqid('/phpt.');
			@unlink($tmp_skipif);
			if (trim($section_text['SKIPIF'])) {
				$this->save_text($tmp_skipif, $section_text['SKIPIF']);
				$output = `{$this->TEST_PHP_EXECUTABLE} {$this->info_params} $tmp_skipif`;
				@unlink($tmp_skipif);
				if (eregi("^skip", trim($output))){
				
					$reason = (ereg("^skip[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
					$this->showstatus($section_text['TEST'], 'SKIPPED', $reason);
	
					return 'SKIPPED';
				}
				if (eregi("^info", trim($output))) {
					$reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
					if ($reason) {
						$tested .= " (info: $reason)";
					}
				}
			}
		}
		return NULL;
	}

	function execute($commandline)
	{
		$data = "";
		
		$proc = proc_open($commandline, array(
                  0 => array('pipe', 'r'),
                  1 => array('pipe', 'w'),
                  2 => array('pipe', 'w')
                  ), $pipes);
		
		if (!$proc)
                return false;
          fclose($pipes[0]);
	
		while (true) {
			/* hide errors from interrupted syscalls */
			$r = $pipes;
			$w = null;
			$e = null;
			$n = @stream_select($r, $w, $e, 60);
	
			if ($n == 0) {
				/* timed out */
				$data .= "\n ** ERROR: process timed out **\n";
				proc_terminate($proc);
				return $data;
			} else if ($n) {
				$line = fread($pipes[1], 8192);
				if (strlen($line) == 0) {
					/* EOF */
					break;
				}
				$data .= $line;
			}
		}
		$code = proc_close($proc);
		return $data;
	}

	//
	//  Run an individual test case.
	//
	function run_test($file)
	{
		if ($this->TEST_PHP_DETAILED)
			$this->writemsg("\n=================\nTEST $file\n");
	
		$section_text = $this->getSectionText($file);
	
		$shortname = str_replace($this->cwd.'/', '', $file);
		$tested = trim($section_text['TEST'])." [$shortname]";
	
		$tmp_file   = ereg_replace('\.phpt$','.php',$file);
		$tmp_post   = $section_text['_DIR'] . uniqid('/phpt.');
	
		@unlink($tmp_file);
		@unlink($tmp_post);
	
		// unlink old test results	
		@unlink(ereg_replace('\.phpt$','.diff',$file));
		@unlink(ereg_replace('\.phpt$','.log',$file));
		@unlink(ereg_replace('\.phpt$','.exp',$file));
		@unlink(ereg_replace('\.phpt$','.out',$file));
	
		// Reset environment from any previous test.
		$env = $this->env;
		$this->setEnvironment($env);

		$skipreason = $this->getSkipReason($section_text);
		if ($skipreason == 'SKIPPED') return $skipreason;
		
		$ini_overwrites = $this->getINISettings($section_text);
	
		// We've satisfied the preconditions - run the test!
		$this->save_text($tmp_file,$section_text['FILE']);
		if (array_key_exists('GET', $section_text)) {
			$query_string = trim($section_text['GET']);
		} else {
			$query_string = '';
		}
	
		$env['REDIRECT_STATUS']='1';
		$env['QUERY_STRING']=$query_string;
		$env['PATH_TRANSLATED']=$tmp_file;
		$env['SCRIPT_FILENAME']=$tmp_file;
	
		$args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
	
		if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
	
			$post = trim($section_text['POST']);
			$this->save_text($tmp_post,$post);
			$content_length = strlen($post);
	
			$env['REQUEST_METHOD']='POST';
			$env['CONTENT_TYPE']='application/x-www-form-urlencoded';
			$env['CONTENT_LENGTH']=$content_length;
	
			$cmd = "{$this->TEST_PHP_EXECUTABLE} $ini_overwrites -f $tmp_file 2>&1 < $tmp_post";
	
		} else {
	
			$env['REQUEST_METHOD']='GET';
			$env['CONTENT_TYPE']='';
			$env['CONTENT_LENGTH']='';
	
			$cmd = "{$this->TEST_PHP_EXECUTABLE} $ini_overwrites -f $tmp_file$args 2>&1";
		}
	
		if ($this->TEST_PHP_DETAILED)
			$this->writemsg("\nCONTENT_LENGTH  = " . $env['CONTENT_LENGTH'] . 
					"\nCONTENT_TYPE    = " . $env['CONTENT_TYPE'] . 
					"\nPATH_TRANSLATED = " . $env['PATH_TRANSLATED'] . 
					"\nQUERY_STRING    = " . $env['QUERY_STRING'] . 
					"\nREDIRECT_STATUS = " . $env['REDIRECT_STATUS'] . 
					"\nREQUEST_METHOD  = " . $env['REQUEST_METHOD'] . 
					"\nSCRIPT_FILENAME = " . $env['SCRIPT_FILENAME'] . 
					"\nCOMMAND $cmd\n");
	
		$this->setEnvironment($env);
		
		$out = $this->execute($cmd);
	
		@unlink($tmp_post);
	
		// Does the output match what is expected?
		$output = trim($out);
		$output = preg_replace('/\r\n/',"\n",$output);
	
		if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
			if (isset($section_text['EXPECTF'])) {
				$wanted = trim($section_text['EXPECTF']);
			} else {
				$wanted = trim($section_text['EXPECTREGEX']);
			}
			$wanted_re = preg_replace('/\r\n/',"\n",$wanted);
			if (isset($section_text['EXPECTF'])) {
				$wanted_re = preg_quote($wanted_re, '/');
				// Stick to basics
				$wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
				$wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
				$wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
				$wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
				$wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*", $wanted_re);
				$wanted_re = str_replace("%c", ".", $wanted_re);
				// %f allows two points "-.0.0" but that is the best *simple* expression
			}
	/* DEBUG YOUR REGEX HERE
			var_dump($wanted);
			print(str_repeat('=', 80) . "\n");
			var_dump($output);
	*/
			if (preg_match("/^$wanted_re\$/s", $output)) {
				@unlink($tmp_file);
				$this->showstatus($tested, 'PASSED');
				return 'PASSED';
			}
	
		} else {
			$wanted = trim($section_text['EXPECT']);
			$wanted = preg_replace('/\r\n/',"\n",$wanted);
		// compare and leave on success
			$ok = (0 == strcmp($output,$wanted));
			if ($ok) {
				@unlink($tmp_file);
				$this->showstatus($tested, 'PASSED');
				return 'PASSED';
			}
		}
	
		// Test failed so we need to report details.
		$this->showstatus($tested, 'FAILED');
	
		$this->failed_tests[] = array(
							'name' => $file,
							'test_name' => $tested,
							'output' => ereg_replace('\.phpt$','.log', $file),
							'diff'   => ereg_replace('\.phpt$','.diff', $file)
							);
	
		// write .exp
		if (strpos($this->TEST_PHP_LOG_FORMAT,'E') !== FALSE) {
			$logname = ereg_replace('\.phpt$','.exp',$file);
			$this->save_text($logname,$wanted);
		}
	
		// write .out
		if (strpos($this->TEST_PHP_LOG_FORMAT,'O') !== FALSE) {
			$logname = ereg_replace('\.phpt$','.out',$file);
			$this->save_text($logname,$output);
		}
	
		// write .diff
		if (strpos($this->TEST_PHP_LOG_FORMAT,'D') !== FALSE) {
			$logname = ereg_replace('\.phpt$','.diff',$file);
			$this->save_text($logname,$this->generate_diff($wanted,$output));
		}
	
		// write .log
		if (strpos($this->TEST_PHP_LOG_FORMAT,'L') !== FALSE) {
			$logname = ereg_replace('\.phpt$','.log',$file);
			$this->save_text($logname,
						"\n---- EXPECTED OUTPUT\n$wanted\n".
						"---- ACTUAL OUTPUT\n$output\n".
						"---- FAILED\n");
			$this->error_report($file,$logname,$tested);
		}
	
		return 'FAILED';
	}

	//
	// Send Email to QA Team
	//
	function mail_qa_team($data, $compression)
	{
		$url_bits = parse_url(QA_SUBMISSION_PAGE);
		if (empty($url_bits['port'])) $url_bits['port'] = 80;
		
		$data = "php_test_data=" . urlencode(base64_encode(preg_replace("/[\\x00]/", "[0x0]", $data)));
		$data_length = strlen($data);
		
		$fs = fsockopen($url_bits['host'], $url_bits['port'], $errno, $errstr, 10);
		if (!$fs) {
			return FALSE;
		}
	
		$this->writemsg("Posting to {$url_bits['host']} {$url_bits['path']}\n");
		fwrite($fs, "POST ".$url_bits['path']." HTTP/1.1\r\n");
		fwrite($fs, "Host: ".$url_bits['host']."\r\n");
		fwrite($fs, "User-Agent: QA Browser 0.1\r\n");
		fwrite($fs, "Content-Type: application/x-www-form-urlencoded\r\n");
		fwrite($fs, "Content-Length: ".$data_length."\r\n\r\n");
		fwrite($fs, $data);
		fwrite($fs, "\r\n\r\n");
		fclose($fs);
	
		return TRUE;
	} 

	//
	//  Write the given text to a temporary file, and return the filename.
	//
	function save_text($filename,$text)
	{
		$fp = @fopen($filename,'w')
			or $this->error("Cannot open file '" . $filename . "' (save_text)");
		fwrite($fp,$text);
		fclose($fp);
		if (1 < $this->TEST_PHP_DETAILED) {
			$this->writemsg("\nFILE $filename {{{\n$text\n}}}\n");
		}
	}

	//
	//  Write an error in a format recognizable to Emacs or MSVC.
	//
	function error_report($testname,$logname,$tested) 
	{
		$testname = realpath($testname);
		$logname  = realpath($logname);
		switch ($this->TEST_PHP_ERROR_STYLE) {
		default:
		case 'MSVC':
			$this->writemsg($testname . "(1) : $tested\n");
			$this->writemsg($logname . "(1) :  $tested\n");
			break;
		case 'EMACS':
			$this->writemsg($testname . ":1: $tested\n");
			$this->writemsg($logname . ":1:  $tested\n");
			break;
		}
	}

	function generate_diff($wanted,$output)
	{
		$w = explode("\n", $wanted);
		$o = explode("\n", $output);
		$w1 = array_diff_assoc($w,$o);
		$o1 = array_diff_assoc($o,$w);
		$w2 = array();
		$o2 = array();
		foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val;
		foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val;
		$diff = array_merge($w2, $o2);
		ksort($diff);
		return implode("\r\n", $diff);
	}

	function error($message)
	{
		$this->writemsg("ERROR: {$message}\n");
		exit(1);
	}
}

class webHarness extends testHarness {
	
	var $textdata;
	
	function checkSafeMode() {
		if (ini_get('safe_mode')) {
		
?>
<CENTER>
<TABLE CELLPADDING=5 CELLSPACING=0 BORDER=1>
<TR>
<TD BGCOLOR="#FE8C97" ALIGN=CENTER><B>WARNING</B>
<HR NOSHADE COLOR=#000000>
You are running this test-suite with "safe_mode" <B>ENABLED</B>!<BR><BR>
Chances are high that none of the tests will work at all, depending on
how you configured "safe_mode".
</TD>
</TR>
</TABLE>
</CENTER>
<?php
		return true;
	}
	return false;
	}
	
	function runHeader() {
?>
<TABLE CELLPADDING=3 CELLSPACING=0 BORDER=0 STYLE="border: thin solid black;">
<TR>
	<TD>TESTED FUNCTIONALITY</TD>
	<TD>RESULT</TD>
</TR>
<?php

	}

	function runFooter() {


?>
<TR>
<TD COLSPAN=2 ALIGN=CENTER><FONT SIZE=3>Additional Notes</FONT><HR><?php $this->displaymsg(); ?></TD>
</TR>
</TABLE><BR><BR>
<?php 
	}
	
	function error($message)
	{
		$this->writemsg("ERROR: {$message}\n");
		exit(1);
	}
	
	// Use this function to do any displaying of text, so that
	// things can be over-written as necessary.
	
	function writemsg($msg) {
		
		$this->textdata = $this->textdata . $msg;
		
	}
	
	function displaymsg() {
	
?>
<TEXTAREA ROWS=10 COLS=80><?=$this->textdata?></TEXTAREA>
<?php
	}
	
	// Another wrapper function, this one should be used any time
	// a particular test passes or fails
	
	function showstatus($item, $status, $reason = '') 
	{
		static $color = "#FAE998";
		
		$color = ($color == "#FAE998") ? "#FFFFFF" : "#FAE998";
		
		switch($status) {
		
			case 'PASSED':
			
?>
<TR>
<TD BGCOLOR=<?=$color?>><?=$item?></TD>
<TD VALIGN=CENTER ALIGN=CENTER BGCOLOR=<?=$color?> ROWSPAN=2><FONT COLOR=#00FF00>PASSED</FONT></TD>
</TR>
<TR>
<TD BGCOLOR=<?=$color?>>Notes: <?=htmlentities($reason)?></TD>
</TR>
<?php
				
				break;
				
			case 'FAILED':
				
?>
<TR>
<TD BGCOLOR=<?=$color?>><?=$item?></TD>
<TD VALIGN=CENTER ALIGN=CENTER BGCOLOR=<?=$color?> ROWSPAN=2><FONT COLOR=#FF0000>FAILED</FONT></TD>
</TR>
<TR>
<TD BGCOLOR=<?=$color?>>Notes: <?=htmlentities($reason)?></TD>
</TR>
<?php
				
				break;
				
			case 'SKIPPED':
			
?>
<TR>
<TD BGCOLOR=<?=$color?>><?=$item?></TD>
<TD VALIGN=CENTER ALIGN=CENTER BGCOLOR=<?=$color?> ROWSPAN=2><FONT COLOR=#000000>SKIPPED</FONT></TD>
</TR>
<TR>
<TD BGCOLOR=<?=$color?>>Notes: <?=htmlentities($reason)?></TD>
</TR>
<?php
				break;
			
		}
	
	}
}

$test = new testHarness();
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: fdm=marker
 * vim: noet sw=4 ts=4
 */
?>