Редакция 69 | Только различия | Учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 69 | Редакция 94 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * PEAR_RunTest
|
3 | * PEAR_RunTest
|
4 | *
|
4 | *
|
5 | * PHP versions 4 and 5
|
5 | * PHP versions 4 and 5
|
6 | *
|
6 | *
|
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license
|
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license
|
8 | * that is available through the world-wide-web at the following URI:
|
8 | * that is available through the world-wide-web at the following URI:
|
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
10 | * the PHP License and are unable to obtain it through the web, please
|
10 | * the PHP License and are unable to obtain it through the web, please
|
11 | * send a note to license@php.net so we can mail you a copy immediately.
|
11 | * send a note to license@php.net so we can mail you a copy immediately.
|
12 | *
|
12 | *
|
13 | * @category pear
|
13 | * @category pear
|
14 | * @package PEAR
|
14 | * @package PEAR
|
15 | * @author Tomas V.V.Cox <cox@idecnet.com>
|
15 | * @author Tomas V.V.Cox <cox@idecnet.com>
|
16 | * @author Greg Beaver <cellog@php.net>
|
16 | * @author Greg Beaver <cellog@php.net>
|
17 | * @copyright 1997-2008 The PHP Group
|
17 | * @copyright 1997-2008 The PHP Group
|
18 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
18 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
19 | * @version CVS: $Id: RunTest.php,v 1.67 2008/05/14 02:30:16 cellog Exp $
|
19 | * @version CVS: $Id: RunTest.php,v 1.67 2008/05/14 02:30:16 cellog Exp $
|
20 | * @link http://pear.php.net/package/PEAR
|
20 | * @link http://pear.php.net/package/PEAR
|
21 | * @since File available since Release 1.3.3
|
21 | * @since File available since Release 1.3.3
|
22 | */
|
22 | */
|
23 | 23 | ||
24 | /**
|
24 | /**
|
25 | * for error handling
|
25 | * for error handling
|
26 | */
|
26 | */
|
27 | require_once 'PEAR.php'; |
27 | require_once 'PEAR.php'; |
28 | require_once 'PEAR/Config.php'; |
28 | require_once 'PEAR/Config.php'; |
29 | 29 | ||
30 | define('DETAILED', 1); |
30 | define('DETAILED', 1); |
31 | putenv("PHP_PEAR_RUNTESTS=1"); |
31 | putenv("PHP_PEAR_RUNTESTS=1"); |
32 | 32 | ||
33 | /**
|
33 | /**
|
34 | * Simplified version of PHP's test suite
|
34 | * Simplified version of PHP's test suite
|
35 | *
|
35 | *
|
36 | * Try it with:
|
36 | * Try it with:
|
37 | *
|
37 | *
|
38 | * $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);'
|
38 | * $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);'
|
39 | *
|
39 | *
|
40 | *
|
40 | *
|
41 | * @category pear
|
41 | * @category pear
|
42 | * @package PEAR
|
42 | * @package PEAR
|
43 | * @author Tomas V.V.Cox <cox@idecnet.com>
|
43 | * @author Tomas V.V.Cox <cox@idecnet.com>
|
44 | * @author Greg Beaver <cellog@php.net>
|
44 | * @author Greg Beaver <cellog@php.net>
|
45 | * @copyright 1997-2008 The PHP Group
|
45 | * @copyright 1997-2008 The PHP Group
|
46 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
46 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
47 | * @version Release: 1.7.2
|
47 | * @version Release: 1.7.2
|
48 | * @link http://pear.php.net/package/PEAR
|
48 | * @link http://pear.php.net/package/PEAR
|
49 | * @since Class available since Release 1.3.3
|
49 | * @since Class available since Release 1.3.3
|
50 | */
|
50 | */
|
51 | class PEAR_RunTest
|
51 | class PEAR_RunTest
|
52 | {
|
52 | {
|
53 | var $_headers = array(); |
53 | var $_headers = array(); |
54 | var $_logger; |
54 | var $_logger; |
55 | var $_options; |
55 | var $_options; |
56 | var $_php; |
56 | var $_php; |
57 | var $tests_count; |
57 | var $tests_count; |
58 | var $xdebug_loaded; |
58 | var $xdebug_loaded; |
59 | /**
|
59 | /**
|
60 | * Saved value of php executable, used to reset $_php when we
|
60 | * Saved value of php executable, used to reset $_php when we
|
61 | * have a test that uses cgi
|
61 | * have a test that uses cgi
|
62 | *
|
62 | *
|
63 | * @var unknown_type
|
63 | * @var unknown_type
|
64 | */
|
64 | */
|
65 | var $_savephp; |
65 | var $_savephp; |
66 | var $ini_overwrites = array( |
66 | var $ini_overwrites = array( |
67 | 'output_handler=', |
67 | 'output_handler=', |
68 | 'open_basedir=', |
68 | 'open_basedir=', |
69 | 'safe_mode=0', |
69 | 'safe_mode=0', |
70 | 'disable_functions=', |
70 | 'disable_functions=', |
71 | 'output_buffering=Off', |
71 | 'output_buffering=Off', |
72 | 'display_errors=1', |
72 | 'display_errors=1', |
73 | 'log_errors=0', |
73 | 'log_errors=0', |
74 | 'html_errors=0', |
74 | 'html_errors=0', |
75 | 'track_errors=1', |
75 | 'track_errors=1', |
76 | 'report_memleaks=0', |
76 | 'report_memleaks=0', |
77 | 'report_zend_debug=0', |
77 | 'report_zend_debug=0', |
78 | 'docref_root=', |
78 | 'docref_root=', |
79 | 'docref_ext=.html', |
79 | 'docref_ext=.html', |
80 | 'error_prepend_string=', |
80 | 'error_prepend_string=', |
81 | 'error_append_string=', |
81 | 'error_append_string=', |
82 | 'auto_prepend_file=', |
82 | 'auto_prepend_file=', |
83 | 'auto_append_file=', |
83 | 'auto_append_file=', |
84 | 'magic_quotes_runtime=0', |
84 | 'magic_quotes_runtime=0', |
85 | 'xdebug.default_enable=0', |
85 | 'xdebug.default_enable=0', |
86 | 'allow_url_fopen=1', |
86 | 'allow_url_fopen=1', |
87 | ); |
87 | ); |
88 | 88 | ||
89 | /**
|
89 | /**
|
90 | * An object that supports the PEAR_Common->log() signature, or null
|
90 | * An object that supports the PEAR_Common->log() signature, or null
|
91 | * @param PEAR_Common|null
|
91 | * @param PEAR_Common|null
|
92 | */
|
92 | */
|
93 | function PEAR_RunTest($logger = null, $options = array()) |
93 | function PEAR_RunTest($logger = null, $options = array()) |
94 | {
|
94 | {
|
95 | if (!defined('E_DEPRECATED')) { |
95 | if (!defined('E_DEPRECATED')) { |
96 | define('E_DEPRECATED', 0); |
96 | define('E_DEPRECATED', 0); |
97 | }
|
97 | }
|
98 | if (!defined('E_STRICT')) { |
98 | if (!defined('E_STRICT')) { |
99 | define('E_STRICT', 0); |
99 | define('E_STRICT', 0); |
100 | }
|
100 | }
|
101 | $this->ini_overwrites[] = 'error_reporting=' . (E_ALL & ~(E_DEPRECATED | E_STRICT)); |
101 | $this->ini_overwrites[] = 'error_reporting=' . (E_ALL & ~(E_DEPRECATED | E_STRICT)); |
102 | if (is_null($logger)) { |
102 | if (is_null($logger)) { |
103 | require_once 'PEAR/Common.php'; |
103 | require_once 'PEAR/Common.php'; |
104 | $logger = new PEAR_Common; |
104 | $logger = new PEAR_Common; |
105 | }
|
105 | }
|
106 | $this->_logger = $logger; |
106 | $this->_logger = $logger; |
107 | $this->_options = $options; |
107 | $this->_options = $options; |
108 | 108 | ||
109 | $conf = &PEAR_Config::singleton(); |
109 | $conf = &PEAR_Config::singleton(); |
110 | $this->_php = $conf->get('php_bin'); |
110 | $this->_php = $conf->get('php_bin'); |
111 | }
|
111 | }
|
112 | 112 | ||
113 | /**
|
113 | /**
|
114 | * Taken from php-src/run-tests.php
|
114 | * Taken from php-src/run-tests.php
|
115 | *
|
115 | *
|
116 | * @param string $commandline command name
|
116 | * @param string $commandline command name
|
117 | * @param array $env
|
117 | * @param array $env
|
118 | * @param string $stdin standard input to pass to the command
|
118 | * @param string $stdin standard input to pass to the command
|
119 | * @return unknown
|
119 | * @return unknown
|
120 | */
|
120 | */
|
121 | function system_with_timeout($commandline, $env = null, $stdin = null) |
121 | function system_with_timeout($commandline, $env = null, $stdin = null) |
122 | {
|
122 | {
|
123 | $data = ''; |
123 | $data = ''; |
124 | if (version_compare(phpversion(), '5.0.0', '<')) { |
124 | if (version_compare(phpversion(), '5.0.0', '<')) { |
125 | $proc = proc_open($commandline, array( |
125 | $proc = proc_open($commandline, array( |
126 | 0 => array('pipe', 'r'), |
126 | 0 => array('pipe', 'r'), |
127 | 1 => array('pipe', 'w'), |
127 | 1 => array('pipe', 'w'), |
128 | 2 => array('pipe', 'w') |
128 | 2 => array('pipe', 'w') |
129 | ), $pipes); |
129 | ), $pipes); |
130 | } else { |
130 | } else { |
131 | $proc = proc_open($commandline, array( |
131 | $proc = proc_open($commandline, array( |
132 | 0 => array('pipe', 'r'), |
132 | 0 => array('pipe', 'r'), |
133 | 1 => array('pipe', 'w'), |
133 | 1 => array('pipe', 'w'), |
134 | 2 => array('pipe', 'w') |
134 | 2 => array('pipe', 'w') |
135 | ), $pipes, null, $env, array('suppress_errors' => true)); |
135 | ), $pipes, null, $env, array('suppress_errors' => true)); |
136 | }
|
136 | }
|
137 | 137 | ||
138 | if (!$proc) { |
138 | if (!$proc) { |
139 | return false; |
139 | return false; |
140 | }
|
140 | }
|
141 | 141 | ||
142 | if (is_string($stdin)) { |
142 | if (is_string($stdin)) { |
143 | fwrite($pipes[0], $stdin); |
143 | fwrite($pipes[0], $stdin); |
144 | }
|
144 | }
|
145 | fclose($pipes[0]); |
145 | fclose($pipes[0]); |
146 | 146 | ||
147 | while (true) { |
147 | while (true) { |
148 | /* hide errors from interrupted syscalls */
|
148 | /* hide errors from interrupted syscalls */
|
149 | $r = $pipes; |
149 | $r = $pipes; |
150 | $e = $w = null; |
150 | $e = $w = null; |
151 | $n = @stream_select($r, $w, $e, 60); |
151 | $n = @stream_select($r, $w, $e, 60); |
152 | 152 | ||
153 | if ($n === 0) { |
153 | if ($n === 0) { |
154 | /* timed out */
|
154 | /* timed out */
|
155 | $data .= "\n ** ERROR: process timed out **\n"; |
155 | $data .= "\n ** ERROR: process timed out **\n"; |
156 | proc_terminate($proc); |
156 | proc_terminate($proc); |
157 | return array(1234567890, $data); |
157 | return array(1234567890, $data); |
158 | } else if ($n > 0) { |
158 | } else if ($n > 0) { |
159 | $line = fread($pipes[1], 8192); |
159 | $line = fread($pipes[1], 8192); |
160 | if (strlen($line) == 0) { |
160 | if (strlen($line) == 0) { |
161 | /* EOF */
|
161 | /* EOF */
|
162 | break; |
162 | break; |
163 | }
|
163 | }
|
164 | $data .= $line; |
164 | $data .= $line; |
165 | }
|
165 | }
|
166 | }
|
166 | }
|
167 | if (function_exists('proc_get_status')) { |
167 | if (function_exists('proc_get_status')) { |
168 | $stat = proc_get_status($proc); |
168 | $stat = proc_get_status($proc); |
169 | if ($stat['signaled']) { |
169 | if ($stat['signaled']) { |
170 | $data .= "\nTermsig=".$stat['stopsig']; |
170 | $data .= "\nTermsig=".$stat['stopsig']; |
171 | }
|
171 | }
|
172 | }
|
172 | }
|
173 | $code = proc_close($proc); |
173 | $code = proc_close($proc); |
174 | if (function_exists('proc_get_status')) { |
174 | if (function_exists('proc_get_status')) { |
175 | $code = $stat['exitcode']; |
175 | $code = $stat['exitcode']; |
176 | }
|
176 | }
|
177 | return array($code, $data); |
177 | return array($code, $data); |
178 | }
|
178 | }
|
179 | 179 | ||
180 | /**
|
180 | /**
|
181 | * Turns a PHP INI string into an array
|
181 | * Turns a PHP INI string into an array
|
182 | *
|
182 | *
|
183 | * Turns -d "include_path=/foo/bar" into this:
|
183 | * Turns -d "include_path=/foo/bar" into this:
|
184 | * array(
|
184 | * array(
|
185 | * 'include_path' => array(
|
185 | * 'include_path' => array(
|
186 | * 'operator' => '-d',
|
186 | * 'operator' => '-d',
|
187 | * 'value' => '/foo/bar',
|
187 | * 'value' => '/foo/bar',
|
188 | * )
|
188 | * )
|
189 | * )
|
189 | * )
|
190 | * Works both with quotes and without
|
190 | * Works both with quotes and without
|
191 | *
|
191 | *
|
192 | * @param string an PHP INI string, -d "include_path=/foo/bar"
|
192 | * @param string an PHP INI string, -d "include_path=/foo/bar"
|
193 | * @return array
|
193 | * @return array
|
194 | */
|
194 | */
|
195 | function iniString2array($ini_string) |
195 | function iniString2array($ini_string) |
196 | {
|
196 | {
|
197 | if (!$ini_string) { |
197 | if (!$ini_string) { |
198 | return array(); |
198 | return array(); |
199 | }
|
199 | }
|
200 | $split = preg_split('/[\s]|=/', $ini_string, -1, PREG_SPLIT_NO_EMPTY); |
200 | $split = preg_split('/[\s]|=/', $ini_string, -1, PREG_SPLIT_NO_EMPTY); |
201 | $key = $split[1][0] == '"' ? substr($split[1], 1) : $split[1]; |
201 | $key = $split[1][0] == '"' ? substr($split[1], 1) : $split[1]; |
202 | $value = $split[2][strlen($split[2]) - 1] == '"' ? substr($split[2], 0, -1) : $split[2]; |
202 | $value = $split[2][strlen($split[2]) - 1] == '"' ? substr($split[2], 0, -1) : $split[2]; |
203 | // FIXME review if this is really the struct to go with
|
203 | // FIXME review if this is really the struct to go with
|
204 | $array = array($key => array('operator' => $split[0], 'value' => $value)); |
204 | $array = array($key => array('operator' => $split[0], 'value' => $value)); |
205 | return $array; |
205 | return $array; |
206 | }
|
206 | }
|
207 | 207 | ||
208 | function settings2array($settings, $ini_settings) |
208 | function settings2array($settings, $ini_settings) |
209 | {
|
209 | {
|
210 | foreach ($settings as $setting) { |
210 | foreach ($settings as $setting) { |
211 | if (strpos($setting, '=') !== false) { |
211 | if (strpos($setting, '=') !== false) { |
212 | $setting = explode('=', $setting, 2); |
212 | $setting = explode('=', $setting, 2); |
213 | $name = trim(strtolower($setting[0])); |
213 | $name = trim(strtolower($setting[0])); |
214 | $value = trim($setting[1]); |
214 | $value = trim($setting[1]); |
215 | $ini_settings[$name] = $value; |
215 | $ini_settings[$name] = $value; |
216 | }
|
216 | }
|
217 | }
|
217 | }
|
218 | return $ini_settings; |
218 | return $ini_settings; |
219 | }
|
219 | }
|
220 | 220 | ||
221 | function settings2params($ini_settings) |
221 | function settings2params($ini_settings) |
222 | {
|
222 | {
|
223 | $settings = ''; |
223 | $settings = ''; |
224 | foreach ($ini_settings as $name => $value) { |
224 | foreach ($ini_settings as $name => $value) { |
225 | if (is_array($value)) { |
225 | if (is_array($value)) { |
226 | $operator = $value['operator']; |
226 | $operator = $value['operator']; |
227 | $value = $value['value']; |
227 | $value = $value['value']; |
228 | } else { |
228 | } else { |
229 | $operator = '-d'; |
229 | $operator = '-d'; |
230 | }
|
230 | }
|
231 | $value = addslashes($value); |
231 | $value = addslashes($value); |
232 | $settings .= " $operator \"$name=$value\""; |
232 | $settings .= " $operator \"$name=$value\""; |
233 | }
|
233 | }
|
234 | return $settings; |
234 | return $settings; |
235 | }
|
235 | }
|
236 | 236 | ||
237 | function runPHPUnit($file, $ini_settings = '') |
237 | function runPHPUnit($file, $ini_settings = '') |
238 | {
|
238 | {
|
239 | if (!file_exists($file) && file_exists(getcwd() . DIRECTORY_SEPARATOR . $file)) { |
239 | if (!file_exists($file) && file_exists(getcwd() . DIRECTORY_SEPARATOR . $file)) { |
240 | $file = realpath(getcwd() . DIRECTORY_SEPARATOR . $file); |
240 | $file = realpath(getcwd() . DIRECTORY_SEPARATOR . $file); |
241 | break; |
241 | break; |
242 | } elseif (file_exists($file)) { |
242 | } elseif (file_exists($file)) { |
243 | $file = realpath($file); |
243 | $file = realpath($file); |
244 | }
|
244 | }
|
245 | $cmd = "$this->_php$ini_settings -f $file"; |
245 | $cmd = "$this->_php$ini_settings -f $file"; |
246 | if (isset($this->_logger)) { |
246 | if (isset($this->_logger)) { |
247 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
247 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
248 | }
|
248 | }
|
249 | 249 | ||
250 | $savedir = getcwd(); // in case the test moves us around |
250 | $savedir = getcwd(); // in case the test moves us around |
251 | chdir(dirname($file)); |
251 | chdir(dirname($file)); |
252 | echo `$cmd`; |
252 | echo `$cmd`; |
253 | chdir($savedir); |
253 | chdir($savedir); |
254 | return 'PASSED'; // we have no way of knowing this information so assume passing |
254 | return 'PASSED'; // we have no way of knowing this information so assume passing |
255 | }
|
255 | }
|
256 | 256 | ||
257 | /**
|
257 | /**
|
258 | * Runs an individual test case.
|
258 | * Runs an individual test case.
|
259 | *
|
259 | *
|
260 | * @param string The filename of the test
|
260 | * @param string The filename of the test
|
261 | * @param array|string INI settings to be applied to the test run
|
261 | * @param array|string INI settings to be applied to the test run
|
262 | * @param integer Number what the current running test is of the
|
262 | * @param integer Number what the current running test is of the
|
263 | * whole test suite being runned.
|
263 | * whole test suite being runned.
|
264 | *
|
264 | *
|
265 | * @return string|object Returns PASSED, WARNED, FAILED depending on how the
|
265 | * @return string|object Returns PASSED, WARNED, FAILED depending on how the
|
266 | * test came out.
|
266 | * test came out.
|
267 | * PEAR Error when the tester it self fails
|
267 | * PEAR Error when the tester it self fails
|
268 | */
|
268 | */
|
269 | function run($file, $ini_settings = array(), $test_number = 1) |
269 | function run($file, $ini_settings = array(), $test_number = 1) |
270 | {
|
270 | {
|
271 | if (isset($this->_savephp)) { |
271 | if (isset($this->_savephp)) { |
272 | $this->_php = $this->_savephp; |
272 | $this->_php = $this->_savephp; |
273 | unset($this->_savephp); |
273 | unset($this->_savephp); |
274 | }
|
274 | }
|
275 | if (empty($this->_options['cgi'])) { |
275 | if (empty($this->_options['cgi'])) { |
276 | // try to see if php-cgi is in the path
|
276 | // try to see if php-cgi is in the path
|
277 | $res = $this->system_with_timeout('php-cgi -v'); |
277 | $res = $this->system_with_timeout('php-cgi -v'); |
278 | if (false !== $res && !(is_array($res) && $res === array(127, ''))) { |
278 | if (false !== $res && !(is_array($res) && $res === array(127, ''))) { |
279 | $this->_options['cgi'] = 'php-cgi'; |
279 | $this->_options['cgi'] = 'php-cgi'; |
280 | }
|
280 | }
|
281 | }
|
281 | }
|
282 | if (1 < $len = strlen($this->tests_count)) { |
282 | if (1 < $len = strlen($this->tests_count)) { |
283 | $test_number = str_pad($test_number, $len, ' ', STR_PAD_LEFT); |
283 | $test_number = str_pad($test_number, $len, ' ', STR_PAD_LEFT); |
284 | $test_nr = "[$test_number/$this->tests_count] "; |
284 | $test_nr = "[$test_number/$this->tests_count] "; |
285 | } else { |
285 | } else { |
286 | $test_nr = ''; |
286 | $test_nr = ''; |
287 | }
|
287 | }
|
288 | 288 | ||
289 | $file = realpath($file); |
289 | $file = realpath($file); |
290 | $section_text = $this->_readFile($file); |
290 | $section_text = $this->_readFile($file); |
291 | if (PEAR::isError($section_text)) { |
291 | if (PEAR::isError($section_text)) { |
292 | return $section_text; |
292 | return $section_text; |
293 | }
|
293 | }
|
294 | 294 | ||
295 | if (isset($section_text['POST_RAW']) && isset($section_text['UPLOAD'])) { |
295 | if (isset($section_text['POST_RAW']) && isset($section_text['UPLOAD'])) { |
296 | return PEAR::raiseError("Cannot contain both POST_RAW and UPLOAD in test file: $file"); |
296 | return PEAR::raiseError("Cannot contain both POST_RAW and UPLOAD in test file: $file"); |
297 | }
|
297 | }
|
298 | 298 | ||
299 | $cwd = getcwd(); |
299 | $cwd = getcwd(); |
300 | 300 | ||
301 | $pass_options = ''; |
301 | $pass_options = ''; |
302 | if (!empty($this->_options['ini'])) { |
302 | if (!empty($this->_options['ini'])) { |
303 | $pass_options = $this->_options['ini']; |
303 | $pass_options = $this->_options['ini']; |
304 | }
|
304 | }
|
305 | 305 | ||
306 | if (is_string($ini_settings)) { |
306 | if (is_string($ini_settings)) { |
307 | $ini_settings = $this->iniString2array($ini_settings); |
307 | $ini_settings = $this->iniString2array($ini_settings); |
308 | }
|
308 | }
|
309 | 309 | ||
310 | $ini_settings = $this->settings2array($this->ini_overwrites, $ini_settings); |
310 | $ini_settings = $this->settings2array($this->ini_overwrites, $ini_settings); |
311 | if ($section_text['INI']) { |
311 | if ($section_text['INI']) { |
312 | if (strpos($section_text['INI'], '{PWD}') !== false) { |
312 | if (strpos($section_text['INI'], '{PWD}') !== false) { |
313 | $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']); |
313 | $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']); |
314 | }
|
314 | }
|
315 | $ini = preg_split( "/[\n\r]+/", $section_text['INI']); |
315 | $ini = preg_split( "/[\n\r]+/", $section_text['INI']); |
316 | $ini_settings = $this->settings2array($ini, $ini_settings); |
316 | $ini_settings = $this->settings2array($ini, $ini_settings); |
317 | }
|
317 | }
|
318 | $ini_settings = $this->settings2params($ini_settings); |
318 | $ini_settings = $this->settings2params($ini_settings); |
319 | $shortname = str_replace($cwd . DIRECTORY_SEPARATOR, '', $file); |
319 | $shortname = str_replace($cwd . DIRECTORY_SEPARATOR, '', $file); |
320 | 320 | ||
321 | $tested = trim($section_text['TEST']); |
321 | $tested = trim($section_text['TEST']); |
322 | $tested.= !isset($this->_options['simple']) ? "[$shortname]" : ' '; |
322 | $tested.= !isset($this->_options['simple']) ? "[$shortname]" : ' '; |
323 | 323 | ||
324 | if (!empty($section_text['POST']) || !empty($section_text['POST_RAW']) || |
324 | if (!empty($section_text['POST']) || !empty($section_text['POST_RAW']) || |
325 | !empty($section_text['UPLOAD']) || !empty($section_text['GET']) || |
325 | !empty($section_text['UPLOAD']) || !empty($section_text['GET']) || |
326 | !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) { |
326 | !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) { |
327 | if (empty($this->_options['cgi'])) { |
327 | if (empty($this->_options['cgi'])) { |
328 | if (!isset($this->_options['quiet'])) { |
328 | if (!isset($this->_options['quiet'])) { |
329 | $this->_logger->log(0, "SKIP $test_nr$tested (reason: --cgi option needed for this test, type 'pear help run-tests')"); |
329 | $this->_logger->log(0, "SKIP $test_nr$tested (reason: --cgi option needed for this test, type 'pear help run-tests')"); |
330 | }
|
330 | }
|
331 | if (isset($this->_options['tapoutput'])) { |
331 | if (isset($this->_options['tapoutput'])) { |
332 | return array('ok', ' # skip --cgi option needed for this test, "pear help run-tests" for info'); |
332 | return array('ok', ' # skip --cgi option needed for this test, "pear help run-tests" for info'); |
333 | }
|
333 | }
|
334 | return 'SKIPPED'; |
334 | return 'SKIPPED'; |
335 | }
|
335 | }
|
336 | $this->_savephp = $this->_php; |
336 | $this->_savephp = $this->_php; |
337 | $this->_php = $this->_options['cgi']; |
337 | $this->_php = $this->_options['cgi']; |
338 | }
|
338 | }
|
339 | 339 | ||
340 | $temp_dir = realpath(dirname($file)); |
340 | $temp_dir = realpath(dirname($file)); |
341 | $main_file_name = basename($file, 'phpt'); |
341 | $main_file_name = basename($file, 'phpt'); |
342 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'diff'; |
342 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'diff'; |
343 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'log'; |
343 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'log'; |
344 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'exp'; |
344 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'exp'; |
345 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'out'; |
345 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'out'; |
346 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'mem'; |
346 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'mem'; |
347 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'php'; |
347 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'php'; |
348 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php'; |
348 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php'; |
349 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php'; |
349 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php'; |
350 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
350 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
351 | 351 | ||
352 | // unlink old test results
|
352 | // unlink old test results
|
353 | $this->_cleanupOldFiles($file); |
353 | $this->_cleanupOldFiles($file); |
354 | 354 | ||
355 | // Check if test should be skipped.
|
355 | // Check if test should be skipped.
|
356 | $res = $this->_runSkipIf($section_text, $temp_skipif, $tested, $ini_settings); |
356 | $res = $this->_runSkipIf($section_text, $temp_skipif, $tested, $ini_settings); |
357 | if (count($res) != 2) { |
357 | if (count($res) != 2) { |
358 | return $res; |
358 | return $res; |
359 | }
|
359 | }
|
360 | $info = $res['info']; |
360 | $info = $res['info']; |
361 | $warn = $res['warn']; |
361 | $warn = $res['warn']; |
362 | 362 | ||
363 | // We've satisfied the preconditions - run the test!
|
363 | // We've satisfied the preconditions - run the test!
|
364 | if (isset($this->_options['coverage']) && $this->xdebug_loaded) { |
364 | if (isset($this->_options['coverage']) && $this->xdebug_loaded) { |
365 | $len_f = 5; |
365 | $len_f = 5; |
366 | if (substr($section_text['FILE'], 0, 5) != '<?php' |
366 | if (substr($section_text['FILE'], 0, 5) != '<?php' |
367 | && substr($section_text['FILE'], 0, 2) == '<?') { |
367 | && substr($section_text['FILE'], 0, 2) == '<?') { |
368 | $len_f = 2; |
368 | $len_f = 2; |
369 | }
|
369 | }
|
370 | 370 | ||
371 | $text = '<?php' . "\n" . 'xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);' . "\n"; |
371 | $text = '<?php' . "\n" . 'xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);' . "\n"; |
372 | $new = substr($section_text['FILE'], $len_f, strlen($section_text['FILE'])); |
372 | $new = substr($section_text['FILE'], $len_f, strlen($section_text['FILE'])); |
373 | $text .= substr($new, 0, strrpos($new, '?>')); |
373 | $text .= substr($new, 0, strrpos($new, '?>')); |
374 | $xdebug_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'xdebug'; |
374 | $xdebug_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'xdebug'; |
375 | $text .= "\n" . |
375 | $text .= "\n" . |
376 | "\n" . '$xdebug = var_export(xdebug_get_code_coverage(), true);'; |
376 | "\n" . '$xdebug = var_export(xdebug_get_code_coverage(), true);'; |
377 | if (!function_exists('file_put_contents')) { |
377 | if (!function_exists('file_put_contents')) { |
378 | $text .= "\n" . '$fh = fopen(\'' . $xdebug_file . '\', "wb");' . |
378 | $text .= "\n" . '$fh = fopen(\'' . $xdebug_file . '\', "wb");' . |
379 | "\n" . 'if ($fh !== false) {' . |
379 | "\n" . 'if ($fh !== false) {' . |
380 | "\n" . ' fwrite($fh, $xdebug);' . |
380 | "\n" . ' fwrite($fh, $xdebug);' . |
381 | "\n" . ' fclose($fh);' . |
381 | "\n" . ' fclose($fh);' . |
382 | "\n" . '}'; |
382 | "\n" . '}'; |
383 | } else { |
383 | } else { |
384 | $text .= "\n" . 'file_put_contents(\'' . $xdebug_file . '\', $xdebug);'; |
384 | $text .= "\n" . 'file_put_contents(\'' . $xdebug_file . '\', $xdebug);'; |
385 | }
|
385 | }
|
386 | $text .= "\n" . 'xdebug_stop_code_coverage();' . "\n" . '?>'; |
386 | $text .= "\n" . 'xdebug_stop_code_coverage();' . "\n" . '?>'; |
387 | 387 | ||
388 | $this->save_text($temp_file, $text); |
388 | $this->save_text($temp_file, $text); |
389 | } else { |
389 | } else { |
390 | $this->save_text($temp_file, $section_text['FILE']); |
390 | $this->save_text($temp_file, $section_text['FILE']); |
391 | }
|
391 | }
|
392 | 392 | ||
393 | $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : ''; |
393 | $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : ''; |
394 | $cmd = "$this->_php$ini_settings -f \"$temp_file\" $args 2>&1"; |
394 | $cmd = "$this->_php$ini_settings -f \"$temp_file\" $args 2>&1"; |
395 | if (isset($this->_logger)) { |
395 | if (isset($this->_logger)) { |
396 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
396 | $this->_logger->log(2, 'Running command "' . $cmd . '"'); |
397 | }
|
397 | }
|
398 | 398 | ||
399 | // Reset environment from any previous test.
|
399 | // Reset environment from any previous test.
|
400 | $env = $this->_resetEnv($section_text, $temp_file); |
400 | $env = $this->_resetEnv($section_text, $temp_file); |
401 | 401 | ||
402 | $section_text = $this->_processUpload($section_text, $file); |
402 | $section_text = $this->_processUpload($section_text, $file); |
403 | if (PEAR::isError($section_text)) { |
403 | if (PEAR::isError($section_text)) { |
404 | return $section_text; |
404 | return $section_text; |
405 | }
|
405 | }
|
406 | 406 | ||
407 | if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) { |
407 | if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) { |
408 | $post = trim($section_text['POST_RAW']); |
408 | $post = trim($section_text['POST_RAW']); |
409 | $raw_lines = explode("\n", $post); |
409 | $raw_lines = explode("\n", $post); |
410 | 410 | ||
411 | $request = ''; |
411 | $request = ''; |
412 | $started = false; |
412 | $started = false; |
413 | foreach ($raw_lines as $i => $line) { |
413 | foreach ($raw_lines as $i => $line) { |
414 | if (empty($env['CONTENT_TYPE']) && |
414 | if (empty($env['CONTENT_TYPE']) && |
415 | preg_match('/^Content-Type:(.*)/i', $line, $res)) { |
415 | preg_match('/^Content-Type:(.*)/i', $line, $res)) { |
416 | $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1])); |
416 | $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1])); |
417 | continue; |
417 | continue; |
418 | }
|
418 | }
|
419 | if ($started) { |
419 | if ($started) { |
420 | $request .= "\n"; |
420 | $request .= "\n"; |
421 | }
|
421 | }
|
422 | $started = true; |
422 | $started = true; |
423 | $request .= $line; |
423 | $request .= $line; |
424 | }
|
424 | }
|
425 | 425 | ||
426 | $env['CONTENT_LENGTH'] = strlen($request); |
426 | $env['CONTENT_LENGTH'] = strlen($request); |
427 | $env['REQUEST_METHOD'] = 'POST'; |
427 | $env['REQUEST_METHOD'] = 'POST'; |
428 | 428 | ||
429 | $this->save_text($tmp_post, $request); |
429 | $this->save_text($tmp_post, $request); |
430 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
430 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
431 | } elseif (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
431 | } elseif (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
432 | $post = trim($section_text['POST']); |
432 | $post = trim($section_text['POST']); |
433 | $this->save_text($tmp_post, $post); |
433 | $this->save_text($tmp_post, $post); |
434 | $content_length = strlen($post); |
434 | $content_length = strlen($post); |
435 | 435 | ||
436 | $env['REQUEST_METHOD'] = 'POST'; |
436 | $env['REQUEST_METHOD'] = 'POST'; |
437 | $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'; |
437 | $env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'; |
438 | $env['CONTENT_LENGTH'] = $content_length; |
438 | $env['CONTENT_LENGTH'] = $content_length; |
439 | 439 | ||
440 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
440 | $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post"; |
441 | } else { |
441 | } else { |
442 | $env['REQUEST_METHOD'] = 'GET'; |
442 | $env['REQUEST_METHOD'] = 'GET'; |
443 | $env['CONTENT_TYPE'] = ''; |
443 | $env['CONTENT_TYPE'] = ''; |
444 | $env['CONTENT_LENGTH'] = ''; |
444 | $env['CONTENT_LENGTH'] = ''; |
445 | }
|
445 | }
|
446 | 446 | ||
447 | if (OS_WINDOWS && isset($section_text['RETURNS'])) { |
447 | if (OS_WINDOWS && isset($section_text['RETURNS'])) { |
448 | ob_start(); |
448 | ob_start(); |
449 | system($cmd, $return_value); |
449 | system($cmd, $return_value); |
450 | $out = ob_get_contents(); |
450 | $out = ob_get_contents(); |
451 | ob_end_clean(); |
451 | ob_end_clean(); |
452 | $section_text['RETURNS'] = (int) trim($section_text['RETURNS']); |
452 | $section_text['RETURNS'] = (int) trim($section_text['RETURNS']); |
453 | $returnfail = ($return_value != $section_text['RETURNS']); |
453 | $returnfail = ($return_value != $section_text['RETURNS']); |
454 | } else { |
454 | } else { |
455 | $returnfail = false; |
455 | $returnfail = false; |
456 | $stdin = isset($section_text['STDIN']) ? $section_text['STDIN'] : null; |
456 | $stdin = isset($section_text['STDIN']) ? $section_text['STDIN'] : null; |
457 | $out = $this->system_with_timeout($cmd, $env, $stdin); |
457 | $out = $this->system_with_timeout($cmd, $env, $stdin); |
458 | $return_value = $out[0]; |
458 | $return_value = $out[0]; |
459 | $out = $out[1]; |
459 | $out = $out[1]; |
460 | }
|
460 | }
|
461 | 461 | ||
462 | $output = preg_replace('/\r\n/', "\n", trim($out)); |
462 | $output = preg_replace('/\r\n/', "\n", trim($out)); |
463 | 463 | ||
464 | if (isset($tmp_post) && realpath($tmp_post) && file_exists($tmp_post)) { |
464 | if (isset($tmp_post) && realpath($tmp_post) && file_exists($tmp_post)) { |
465 | @unlink(realpath($tmp_post)); |
465 | @unlink(realpath($tmp_post)); |
466 | }
|
466 | }
|
467 | chdir($cwd); // in case the test moves us around |
467 | chdir($cwd); // in case the test moves us around |
468 | 468 | ||
469 | $this->_testCleanup($section_text, $temp_clean); |
469 | $this->_testCleanup($section_text, $temp_clean); |
470 | 470 | ||
471 | /* when using CGI, strip the headers from the output */
|
471 | /* when using CGI, strip the headers from the output */
|
472 | $output = $this->_stripHeadersCGI($output); |
472 | $output = $this->_stripHeadersCGI($output); |
473 | 473 | ||
474 | if (isset($section_text['EXPECTHEADERS'])) { |
474 | if (isset($section_text['EXPECTHEADERS'])) { |
475 | $testheaders = $this->_processHeaders($section_text['EXPECTHEADERS']); |
475 | $testheaders = $this->_processHeaders($section_text['EXPECTHEADERS']); |
476 | $missing = array_diff_assoc($testheaders, $this->_headers); |
476 | $missing = array_diff_assoc($testheaders, $this->_headers); |
477 | $changed = ''; |
477 | $changed = ''; |
478 | foreach ($missing as $header => $value) { |
478 | foreach ($missing as $header => $value) { |
479 | if (isset($this->_headers[$header])) { |
479 | if (isset($this->_headers[$header])) { |
480 | $changed .= "-$header: $value\n+$header: "; |
480 | $changed .= "-$header: $value\n+$header: "; |
481 | $changed .= $this->_headers[$header]; |
481 | $changed .= $this->_headers[$header]; |
482 | } else { |
482 | } else { |
483 | $changed .= "-$header: $value\n"; |
483 | $changed .= "-$header: $value\n"; |
484 | }
|
484 | }
|
485 | }
|
485 | }
|
486 | if ($missing) { |
486 | if ($missing) { |
487 | // tack on failed headers to output:
|
487 | // tack on failed headers to output:
|
488 | $output .= "\n====EXPECTHEADERS FAILURE====:\n$changed"; |
488 | $output .= "\n====EXPECTHEADERS FAILURE====:\n$changed"; |
489 | }
|
489 | }
|
490 | }
|
490 | }
|
491 | // Does the output match what is expected?
|
491 | // Does the output match what is expected?
|
492 | do { |
492 | do { |
493 | if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) { |
493 | if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) { |
494 | if (isset($section_text['EXPECTF'])) { |
494 | if (isset($section_text['EXPECTF'])) { |
495 | $wanted = trim($section_text['EXPECTF']); |
495 | $wanted = trim($section_text['EXPECTF']); |
496 | } else { |
496 | } else { |
497 | $wanted = trim($section_text['EXPECTREGEX']); |
497 | $wanted = trim($section_text['EXPECTREGEX']); |
498 | }
|
498 | }
|
499 | $wanted_re = preg_replace('/\r\n/', "\n", $wanted); |
499 | $wanted_re = preg_replace('/\r\n/', "\n", $wanted); |
500 | if (isset($section_text['EXPECTF'])) { |
500 | if (isset($section_text['EXPECTF'])) { |
501 | $wanted_re = preg_quote($wanted_re, '/'); |
501 | $wanted_re = preg_quote($wanted_re, '/'); |
502 | // Stick to basics
|
502 | // Stick to basics
|
503 | $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy |
503 | $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy |
504 | $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re); |
504 | $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re); |
505 | $wanted_re = str_replace("%d", "[0-9]+", $wanted_re); |
505 | $wanted_re = str_replace("%d", "[0-9]+", $wanted_re); |
506 | $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re); |
506 | $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re); |
507 | $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re); |
507 | $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re); |
508 | $wanted_re = str_replace("%c", ".", $wanted_re); |
508 | $wanted_re = str_replace("%c", ".", $wanted_re); |
509 | // %f allows two points "-.0.0" but that is the best *simple* expression
|
509 | // %f allows two points "-.0.0" but that is the best *simple* expression
|
510 | }
|
510 | }
|
511 | /* DEBUG YOUR REGEX HERE
|
511 | /* DEBUG YOUR REGEX HERE
|
512 | var_dump($wanted_re);
|
512 | var_dump($wanted_re);
|
513 | print(str_repeat('=', 80) . "\n");
|
513 | print(str_repeat('=', 80) . "\n");
|
514 | var_dump($output);
|
514 | var_dump($output);
|
515 | */
|
515 | */
|
516 | if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) { |
516 | if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) { |
517 | if (file_exists($temp_file)) { |
517 | if (file_exists($temp_file)) { |
518 | unlink($temp_file); |
518 | unlink($temp_file); |
519 | }
|
519 | }
|
520 | if (array_key_exists('FAIL', $section_text)) { |
520 | if (array_key_exists('FAIL', $section_text)) { |
521 | break; |
521 | break; |
522 | }
|
522 | }
|
523 | if (!isset($this->_options['quiet'])) { |
523 | if (!isset($this->_options['quiet'])) { |
524 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
524 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
525 | }
|
525 | }
|
526 | if (isset($this->_options['tapoutput'])) { |
526 | if (isset($this->_options['tapoutput'])) { |
527 | return array('ok', ' - ' . $tested); |
527 | return array('ok', ' - ' . $tested); |
528 | }
|
528 | }
|
529 | return 'PASSED'; |
529 | return 'PASSED'; |
530 | }
|
530 | }
|
531 | } else { |
531 | } else { |
532 | if (isset($section_text['EXPECTFILE'])) { |
532 | if (isset($section_text['EXPECTFILE'])) { |
533 | $f = $temp_dir . '/' . trim($section_text['EXPECTFILE']); |
533 | $f = $temp_dir . '/' . trim($section_text['EXPECTFILE']); |
534 | if (!($fp = @fopen($f, 'rb'))) { |
534 | if (!($fp = @fopen($f, 'rb'))) { |
535 | return PEAR::raiseError('--EXPECTFILE-- section file ' . |
535 | return PEAR::raiseError('--EXPECTFILE-- section file ' . |
536 | $f . ' not found'); |
536 | $f . ' not found'); |
537 | }
|
537 | }
|
538 | fclose($fp); |
538 | fclose($fp); |
539 | $section_text['EXPECT'] = file_get_contents($f); |
539 | $section_text['EXPECT'] = file_get_contents($f); |
540 | }
|
540 | }
|
541 | $wanted = preg_replace('/\r\n/', "\n", trim($section_text['EXPECT'])); |
541 | $wanted = preg_replace('/\r\n/', "\n", trim($section_text['EXPECT'])); |
542 | // compare and leave on success
|
542 | // compare and leave on success
|
543 | if (!$returnfail && 0 == strcmp($output, $wanted)) { |
543 | if (!$returnfail && 0 == strcmp($output, $wanted)) { |
544 | if (file_exists($temp_file)) { |
544 | if (file_exists($temp_file)) { |
545 | unlink($temp_file); |
545 | unlink($temp_file); |
546 | }
|
546 | }
|
547 | if (array_key_exists('FAIL', $section_text)) { |
547 | if (array_key_exists('FAIL', $section_text)) { |
548 | break; |
548 | break; |
549 | }
|
549 | }
|
550 | if (!isset($this->_options['quiet'])) { |
550 | if (!isset($this->_options['quiet'])) { |
551 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
551 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
552 | }
|
552 | }
|
553 | if (isset($this->_options['tapoutput'])) { |
553 | if (isset($this->_options['tapoutput'])) { |
554 | return array('ok', ' - ' . $tested); |
554 | return array('ok', ' - ' . $tested); |
555 | }
|
555 | }
|
556 | return 'PASSED'; |
556 | return 'PASSED'; |
557 | }
|
557 | }
|
558 | }
|
558 | }
|
559 | } while (false); |
559 | } while (false); |
560 | 560 | ||
561 | if (array_key_exists('FAIL', $section_text)) { |
561 | if (array_key_exists('FAIL', $section_text)) { |
562 | // we expect a particular failure
|
562 | // we expect a particular failure
|
563 | // this is only used for testing PEAR_RunTest
|
563 | // this is only used for testing PEAR_RunTest
|
564 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
564 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
565 | $faildiff = $this->generate_diff($wanted, $output, null, $expectf); |
565 | $faildiff = $this->generate_diff($wanted, $output, null, $expectf); |
566 | $faildiff = preg_replace('/\r/', '', $faildiff); |
566 | $faildiff = preg_replace('/\r/', '', $faildiff); |
567 | $wanted = preg_replace('/\r/', '', trim($section_text['FAIL'])); |
567 | $wanted = preg_replace('/\r/', '', trim($section_text['FAIL'])); |
568 | if ($faildiff == $wanted) { |
568 | if ($faildiff == $wanted) { |
569 | if (!isset($this->_options['quiet'])) { |
569 | if (!isset($this->_options['quiet'])) { |
570 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
570 | $this->_logger->log(0, "PASS $test_nr$tested$info"); |
571 | }
|
571 | }
|
572 | if (isset($this->_options['tapoutput'])) { |
572 | if (isset($this->_options['tapoutput'])) { |
573 | return array('ok', ' - ' . $tested); |
573 | return array('ok', ' - ' . $tested); |
574 | }
|
574 | }
|
575 | return 'PASSED'; |
575 | return 'PASSED'; |
576 | }
|
576 | }
|
577 | unset($section_text['EXPECTF']); |
577 | unset($section_text['EXPECTF']); |
578 | $output = $faildiff; |
578 | $output = $faildiff; |
579 | if (isset($section_text['RETURNS'])) { |
579 | if (isset($section_text['RETURNS'])) { |
580 | return PEAR::raiseError('Cannot have both RETURNS and FAIL in the same test: ' . |
580 | return PEAR::raiseError('Cannot have both RETURNS and FAIL in the same test: ' . |
581 | $file); |
581 | $file); |
582 | }
|
582 | }
|
583 | }
|
583 | }
|
584 | 584 | ||
585 | // Test failed so we need to report details.
|
585 | // Test failed so we need to report details.
|
586 | $txt = $warn ? 'WARN ' : 'FAIL '; |
586 | $txt = $warn ? 'WARN ' : 'FAIL '; |
587 | $this->_logger->log(0, $txt . $test_nr . $tested . $info); |
587 | $this->_logger->log(0, $txt . $test_nr . $tested . $info); |
588 | 588 | ||
589 | // write .exp
|
589 | // write .exp
|
590 | $res = $this->_writeLog($exp_filename, $wanted); |
590 | $res = $this->_writeLog($exp_filename, $wanted); |
591 | if (PEAR::isError($res)) { |
591 | if (PEAR::isError($res)) { |
592 | return $res; |
592 | return $res; |
593 | }
|
593 | }
|
594 | 594 | ||
595 | // write .out
|
595 | // write .out
|
596 | $res = $this->_writeLog($output_filename, $output); |
596 | $res = $this->_writeLog($output_filename, $output); |
597 | if (PEAR::isError($res)) { |
597 | if (PEAR::isError($res)) { |
598 | return $res; |
598 | return $res; |
599 | }
|
599 | }
|
600 | 600 | ||
601 | // write .diff
|
601 | // write .diff
|
602 | $returns = isset($section_text['RETURNS']) ? |
602 | $returns = isset($section_text['RETURNS']) ? |
603 | array(trim($section_text['RETURNS']), $return_value) : null; |
603 | array(trim($section_text['RETURNS']), $return_value) : null; |
604 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
604 | $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null; |
605 | $data = $this->generate_diff($wanted, $output, $returns, $expectf); |
605 | $data = $this->generate_diff($wanted, $output, $returns, $expectf); |
606 | $res = $this->_writeLog($diff_filename, $data); |
606 | $res = $this->_writeLog($diff_filename, $data); |
607 | if (PEAR::isError($res)) { |
607 | if (PEAR::isError($res)) { |
608 | return $res; |
608 | return $res; |
609 | }
|
609 | }
|
610 | 610 | ||
611 | // write .log
|
611 | // write .log
|
612 | $data = " |
612 | $data = " |
613 | ---- EXPECTED OUTPUT
|
613 | ---- EXPECTED OUTPUT
|
614 | $wanted
|
614 | $wanted
|
615 | ---- ACTUAL OUTPUT
|
615 | ---- ACTUAL OUTPUT
|
616 | $output
|
616 | $output
|
617 | ---- FAILED
|
617 | ---- FAILED
|
618 | "; |
618 | "; |
619 | 619 | ||
620 | if ($returnfail) { |
620 | if ($returnfail) { |
621 | $data .= " |
621 | $data .= " |
622 | ---- EXPECTED RETURN
|
622 | ---- EXPECTED RETURN
|
623 | $section_text[RETURNS]
|
623 | $section_text[RETURNS]
|
624 | ---- ACTUAL RETURN
|
624 | ---- ACTUAL RETURN
|
625 | $return_value
|
625 | $return_value
|
626 | "; |
626 | "; |
627 | }
|
627 | }
|
628 | 628 | ||
629 | $res = $this->_writeLog($log_filename, $data); |
629 | $res = $this->_writeLog($log_filename, $data); |
630 | if (PEAR::isError($res)) { |
630 | if (PEAR::isError($res)) { |
631 | return $res; |
631 | return $res; |
632 | }
|
632 | }
|
633 | 633 | ||
634 | if (isset($this->_options['tapoutput'])) { |
634 | if (isset($this->_options['tapoutput'])) { |
635 | $wanted = explode("\n", $wanted); |
635 | $wanted = explode("\n", $wanted); |
636 | $wanted = "# Expected output:\n#\n#" . implode("\n#", $wanted); |
636 | $wanted = "# Expected output:\n#\n#" . implode("\n#", $wanted); |
637 | $output = explode("\n", $output); |
637 | $output = explode("\n", $output); |
638 | $output = "#\n#\n# Actual output:\n#\n#" . implode("\n#", $output); |
638 | $output = "#\n#\n# Actual output:\n#\n#" . implode("\n#", $output); |
639 | return array($wanted . $output . 'not ok', ' - ' . $tested); |
639 | return array($wanted . $output . 'not ok', ' - ' . $tested); |
640 | }
|
640 | }
|
641 | return $warn ? 'WARNED' : 'FAILED'; |
641 | return $warn ? 'WARNED' : 'FAILED'; |
642 | }
|
642 | }
|
643 | 643 | ||
644 | function generate_diff($wanted, $output, $rvalue, $wanted_re) |
644 | function generate_diff($wanted, $output, $rvalue, $wanted_re) |
645 | {
|
645 | {
|
646 | $w = explode("\n", $wanted); |
646 | $w = explode("\n", $wanted); |
647 | $o = explode("\n", $output); |
647 | $o = explode("\n", $output); |
648 | $wr = explode("\n", $wanted_re); |
648 | $wr = explode("\n", $wanted_re); |
649 | $w1 = array_diff_assoc($w, $o); |
649 | $w1 = array_diff_assoc($w, $o); |
650 | $o1 = array_diff_assoc($o, $w); |
650 | $o1 = array_diff_assoc($o, $w); |
651 | $o2 = $w2 = array(); |
651 | $o2 = $w2 = array(); |
652 | foreach ($w1 as $idx => $val) { |
652 | foreach ($w1 as $idx => $val) { |
653 | if (!$wanted_re || !isset($wr[$idx]) || !isset($o1[$idx]) || |
653 | if (!$wanted_re || !isset($wr[$idx]) || !isset($o1[$idx]) || |
654 | !preg_match('/^' . $wr[$idx] . '\\z/', $o1[$idx])) { |
654 | !preg_match('/^' . $wr[$idx] . '\\z/', $o1[$idx])) { |
655 | $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . $val; |
655 | $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . $val; |
656 | }
|
656 | }
|
657 | }
|
657 | }
|
658 | foreach ($o1 as $idx => $val) { |
658 | foreach ($o1 as $idx => $val) { |
659 | if (!$wanted_re || !isset($wr[$idx]) || |
659 | if (!$wanted_re || !isset($wr[$idx]) || |
660 | !preg_match('/^' . $wr[$idx] . '\\z/', $val)) { |
660 | !preg_match('/^' . $wr[$idx] . '\\z/', $val)) { |
661 | $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . $val; |
661 | $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . $val; |
662 | }
|
662 | }
|
663 | }
|
663 | }
|
664 | $diff = array_merge($w2, $o2); |
664 | $diff = array_merge($w2, $o2); |
665 | ksort($diff); |
665 | ksort($diff); |
666 | $extra = $rvalue ? "##EXPECTED: $rvalue[0]\r\n##RETURNED: $rvalue[1]" : ''; |
666 | $extra = $rvalue ? "##EXPECTED: $rvalue[0]\r\n##RETURNED: $rvalue[1]" : ''; |
667 | return implode("\r\n", $diff) . $extra; |
667 | return implode("\r\n", $diff) . $extra; |
668 | }
|
668 | }
|
669 | 669 | ||
670 | // Write the given text to a temporary file, and return the filename.
|
670 | // Write the given text to a temporary file, and return the filename.
|
671 | function save_text($filename, $text) |
671 | function save_text($filename, $text) |
672 | {
|
672 | {
|
673 | if (!$fp = fopen($filename, 'w')) { |
673 | if (!$fp = fopen($filename, 'w')) { |
674 | return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)"); |
674 | return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)"); |
675 | }
|
675 | }
|
676 | fwrite($fp, $text); |
676 | fwrite($fp, $text); |
677 | fclose($fp); |
677 | fclose($fp); |
678 | if (1 < DETAILED) echo " |
678 | if (1 < DETAILED) echo " |
679 | FILE $filename {{{
|
679 | FILE $filename {{{
|
680 | $text
|
680 | $text
|
681 | }}}
|
681 | }}}
|
682 | "; |
682 | "; |
683 | }
|
683 | }
|
684 | 684 | ||
685 | function _cleanupOldFiles($file) |
685 | function _cleanupOldFiles($file) |
686 | {
|
686 | {
|
687 | $temp_dir = realpath(dirname($file)); |
687 | $temp_dir = realpath(dirname($file)); |
688 | $mainFileName = basename($file, 'phpt'); |
688 | $mainFileName = basename($file, 'phpt'); |
689 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'diff'; |
689 | $diff_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'diff'; |
690 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'log'; |
690 | $log_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'log'; |
691 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'exp'; |
691 | $exp_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'exp'; |
692 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'out'; |
692 | $output_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'out'; |
693 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'mem'; |
693 | $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'mem'; |
694 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'php'; |
694 | $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'php'; |
695 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'skip.php'; |
695 | $temp_skipif = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'skip.php'; |
696 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'clean.php'; |
696 | $temp_clean = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'clean.php'; |
697 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
697 | $tmp_post = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.'); |
698 | 698 | ||
699 | // unlink old test results
|
699 | // unlink old test results
|
700 | @unlink($diff_filename); |
700 | @unlink($diff_filename); |
701 | @unlink($log_filename); |
701 | @unlink($log_filename); |
702 | @unlink($exp_filename); |
702 | @unlink($exp_filename); |
703 | @unlink($output_filename); |
703 | @unlink($output_filename); |
704 | @unlink($memcheck_filename); |
704 | @unlink($memcheck_filename); |
705 | @unlink($temp_file); |
705 | @unlink($temp_file); |
706 | @unlink($temp_skipif); |
706 | @unlink($temp_skipif); |
707 | @unlink($tmp_post); |
707 | @unlink($tmp_post); |
708 | @unlink($temp_clean); |
708 | @unlink($temp_clean); |
709 | }
|
709 | }
|
710 | 710 | ||
711 | function _runSkipIf($section_text, $temp_skipif, $tested, $ini_settings) |
711 | function _runSkipIf($section_text, $temp_skipif, $tested, $ini_settings) |
712 | {
|
712 | {
|
713 | $info = ''; |
713 | $info = ''; |
714 | $warn = false; |
714 | $warn = false; |
715 | if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) { |
715 | if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) { |
716 | $this->save_text($temp_skipif, $section_text['SKIPIF']); |
716 | $this->save_text($temp_skipif, $section_text['SKIPIF']); |
717 | $output = $this->system_with_timeout("$this->_php$ini_settings -f \"$temp_skipif\""); |
717 | $output = $this->system_with_timeout("$this->_php$ini_settings -f \"$temp_skipif\""); |
718 | $output = $output[1]; |
718 | $output = $output[1]; |
719 | $loutput = ltrim($output); |
719 | $loutput = ltrim($output); |
720 | unlink($temp_skipif); |
720 | unlink($temp_skipif); |
721 | if (!strncasecmp('skip', $loutput, 4)) { |
721 | if (!strncasecmp('skip', $loutput, 4)) { |
722 | $skipreason = "SKIP $tested"; |
722 | $skipreason = "SKIP $tested"; |
723 | if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) { |
723 | if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) { |
724 | $skipreason .= '(reason: ' . $m[1] . ')'; |
724 | $skipreason .= '(reason: ' . $m[1] . ')'; |
725 | }
|
725 | }
|
726 | if (!isset($this->_options['quiet'])) { |
726 | if (!isset($this->_options['quiet'])) { |
727 | $this->_logger->log(0, $skipreason); |
727 | $this->_logger->log(0, $skipreason); |
728 | }
|
728 | }
|
729 | if (isset($this->_options['tapoutput'])) { |
729 | if (isset($this->_options['tapoutput'])) { |
730 | return array('ok', ' # skip ' . $reason); |
730 | return array('ok', ' # skip ' . $reason); |
731 | }
|
731 | }
|
732 | return 'SKIPPED'; |
732 | return 'SKIPPED'; |
733 | }
|
733 | }
|
734 | 734 | ||
735 | if (!strncasecmp('info', $loutput, 4) |
735 | if (!strncasecmp('info', $loutput, 4) |
736 | && preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) { |
736 | && preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) { |
737 | $info = " (info: $m[1])"; |
737 | $info = " (info: $m[1])"; |
738 | }
|
738 | }
|
739 | 739 | ||
740 | if (!strncasecmp('warn', $loutput, 4) |
740 | if (!strncasecmp('warn', $loutput, 4) |
741 | && preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) { |
741 | && preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) { |
742 | $warn = true; /* only if there is a reason */ |
742 | $warn = true; /* only if there is a reason */ |
743 | $info = " (warn: $m[1])"; |
743 | $info = " (warn: $m[1])"; |
744 | }
|
744 | }
|
745 | }
|
745 | }
|
746 | 746 | ||
747 | return array('warn' => $warn, 'info' => $info); |
747 | return array('warn' => $warn, 'info' => $info); |
748 | }
|
748 | }
|
749 | 749 | ||
750 | function _stripHeadersCGI($output) |
750 | function _stripHeadersCGI($output) |
751 | {
|
751 | {
|
752 | $this->headers = array(); |
752 | $this->headers = array(); |
753 | if (!empty($this->_options['cgi']) && |
753 | if (!empty($this->_options['cgi']) && |
754 | $this->_php == $this->_options['cgi'] && |
754 | $this->_php == $this->_options['cgi'] && |
755 | preg_match("/^(.*?)(?:\n\n(.*)|\\z)/s", $output, $match)) { |
755 | preg_match("/^(.*?)(?:\n\n(.*)|\\z)/s", $output, $match)) { |
756 | $output = isset($match[2]) ? trim($match[2]) : ''; |
756 | $output = isset($match[2]) ? trim($match[2]) : ''; |
757 | $this->_headers = $this->_processHeaders($match[1]); |
757 | $this->_headers = $this->_processHeaders($match[1]); |
758 | }
|
758 | }
|
759 | 759 | ||
760 | return $output; |
760 | return $output; |
761 | }
|
761 | }
|
762 | 762 | ||
763 | /**
|
763 | /**
|
764 | * Return an array that can be used with array_diff() to compare headers
|
764 | * Return an array that can be used with array_diff() to compare headers
|
765 | *
|
765 | *
|
766 | * @param string $text
|
766 | * @param string $text
|
767 | */
|
767 | */
|
768 | function _processHeaders($text) |
768 | function _processHeaders($text) |
769 | {
|
769 | {
|
770 | $headers = array(); |
770 | $headers = array(); |
771 | $rh = preg_split("/[\n\r]+/", $text); |
771 | $rh = preg_split("/[\n\r]+/", $text); |
772 | foreach ($rh as $line) { |
772 | foreach ($rh as $line) { |
773 | if (strpos($line, ':')!== false) { |
773 | if (strpos($line, ':')!== false) { |
774 | $line = explode(':', $line, 2); |
774 | $line = explode(':', $line, 2); |
775 | $headers[trim($line[0])] = trim($line[1]); |
775 | $headers[trim($line[0])] = trim($line[1]); |
776 | }
|
776 | }
|
777 | }
|
777 | }
|
778 | return $headers; |
778 | return $headers; |
779 | }
|
779 | }
|
780 | 780 | ||
781 | function _readFile($file) |
781 | function _readFile($file) |
782 | {
|
782 | {
|
783 | // Load the sections of the test file.
|
783 | // Load the sections of the test file.
|
784 | $section_text = array( |
784 | $section_text = array( |
785 | 'TEST' => '(unnamed test)', |
785 | 'TEST' => '(unnamed test)', |
786 | 'SKIPIF' => '', |
786 | 'SKIPIF' => '', |
787 | 'GET' => '', |
787 | 'GET' => '', |
788 | 'COOKIE' => '', |
788 | 'COOKIE' => '', |
789 | 'POST' => '', |
789 | 'POST' => '', |
790 | 'ARGS' => '', |
790 | 'ARGS' => '', |
791 | 'INI' => '', |
791 | 'INI' => '', |
792 | 'CLEAN' => '', |
792 | 'CLEAN' => '', |
793 | ); |
793 | ); |
794 | 794 | ||
795 | if (!is_file($file) || !$fp = fopen($file, "r")) { |
795 | if (!is_file($file) || !$fp = fopen($file, "r")) { |
796 | return PEAR::raiseError("Cannot open test file: $file"); |
796 | return PEAR::raiseError("Cannot open test file: $file"); |
797 | }
|
797 | }
|
798 | 798 | ||
799 | $section = ''; |
799 | $section = ''; |
800 | while (!feof($fp)) { |
800 | while (!feof($fp)) { |
801 | $line = fgets($fp); |
801 | $line = fgets($fp); |
802 | 802 | ||
803 | // Match the beginning of a section.
|
803 | // Match the beginning of a section.
|
804 | if (preg_match('/^--([_A-Z]+)--/', $line, $r)) { |
804 | if (preg_match('/^--([_A-Z]+)--/', $line, $r)) { |
805 | $section = $r[1]; |
805 | $section = $r[1]; |
806 | $section_text[$section] = ''; |
806 | $section_text[$section] = ''; |
807 | continue; |
807 | continue; |
808 | } elseif (empty($section)) { |
808 | } elseif (empty($section)) { |
809 | fclose($fp); |
809 | fclose($fp); |
810 | return PEAR::raiseError("Invalid sections formats in test file: $file"); |
810 | return PEAR::raiseError("Invalid sections formats in test file: $file"); |
811 | }
|
811 | }
|
812 | 812 | ||
813 | // Add to the section text.
|
813 | // Add to the section text.
|
814 | $section_text[$section] .= $line; |
814 | $section_text[$section] .= $line; |
815 | }
|
815 | }
|
816 | fclose($fp); |
816 | fclose($fp); |
817 | 817 | ||
818 | return $section_text; |
818 | return $section_text; |
819 | }
|
819 | }
|
820 | 820 | ||
821 | function _writeLog($logname, $data) |
821 | function _writeLog($logname, $data) |
822 | {
|
822 | {
|
823 | if (!$log = fopen($logname, 'w')) { |
823 | if (!$log = fopen($logname, 'w')) { |
824 | return PEAR::raiseError("Cannot create test log - $logname"); |
824 | return PEAR::raiseError("Cannot create test log - $logname"); |
825 | }
|
825 | }
|
826 | fwrite($log, $data); |
826 | fwrite($log, $data); |
827 | fclose($log); |
827 | fclose($log); |
828 | }
|
828 | }
|
829 | 829 | ||
830 | function _resetEnv($section_text, $temp_file) |
830 | function _resetEnv($section_text, $temp_file) |
831 | {
|
831 | {
|
832 | $env = $_ENV; |
832 | $env = $_ENV; |
833 | $env['REDIRECT_STATUS'] = ''; |
833 | $env['REDIRECT_STATUS'] = ''; |
834 | $env['QUERY_STRING'] = ''; |
834 | $env['QUERY_STRING'] = ''; |
835 | $env['PATH_TRANSLATED'] = ''; |
835 | $env['PATH_TRANSLATED'] = ''; |
836 | $env['SCRIPT_FILENAME'] = ''; |
836 | $env['SCRIPT_FILENAME'] = ''; |
837 | $env['REQUEST_METHOD'] = ''; |
837 | $env['REQUEST_METHOD'] = ''; |
838 | $env['CONTENT_TYPE'] = ''; |
838 | $env['CONTENT_TYPE'] = ''; |
839 | $env['CONTENT_LENGTH'] = ''; |
839 | $env['CONTENT_LENGTH'] = ''; |
840 | if (!empty($section_text['ENV'])) { |
840 | if (!empty($section_text['ENV'])) { |
841 | foreach (explode("\n", trim($section_text['ENV'])) as $e) { |
841 | foreach (explode("\n", trim($section_text['ENV'])) as $e) { |
842 | $e = explode('=', trim($e), 2); |
842 | $e = explode('=', trim($e), 2); |
843 | if (!empty($e[0]) && isset($e[1])) { |
843 | if (!empty($e[0]) && isset($e[1])) { |
844 | $env[$e[0]] = $e[1]; |
844 | $env[$e[0]] = $e[1]; |
845 | }
|
845 | }
|
846 | }
|
846 | }
|
847 | }
|
847 | }
|
848 | if (array_key_exists('GET', $section_text)) { |
848 | if (array_key_exists('GET', $section_text)) { |
849 | $env['QUERY_STRING'] = trim($section_text['GET']); |
849 | $env['QUERY_STRING'] = trim($section_text['GET']); |
850 | } else { |
850 | } else { |
851 | $env['QUERY_STRING'] = ''; |
851 | $env['QUERY_STRING'] = ''; |
852 | }
|
852 | }
|
853 | if (array_key_exists('COOKIE', $section_text)) { |
853 | if (array_key_exists('COOKIE', $section_text)) { |
854 | $env['HTTP_COOKIE'] = trim($section_text['COOKIE']); |
854 | $env['HTTP_COOKIE'] = trim($section_text['COOKIE']); |
855 | } else { |
855 | } else { |
856 | $env['HTTP_COOKIE'] = ''; |
856 | $env['HTTP_COOKIE'] = ''; |
857 | }
|
857 | }
|
858 | $env['REDIRECT_STATUS'] = '1'; |
858 | $env['REDIRECT_STATUS'] = '1'; |
859 | $env['PATH_TRANSLATED'] = $temp_file; |
859 | $env['PATH_TRANSLATED'] = $temp_file; |
860 | $env['SCRIPT_FILENAME'] = $temp_file; |
860 | $env['SCRIPT_FILENAME'] = $temp_file; |
861 | 861 | ||
862 | return $env; |
862 | return $env; |
863 | }
|
863 | }
|
864 | 864 | ||
865 | function _processUpload($section_text, $file) |
865 | function _processUpload($section_text, $file) |
866 | {
|
866 | {
|
867 | if (array_key_exists('UPLOAD', $section_text) && !empty($section_text['UPLOAD'])) { |
867 | if (array_key_exists('UPLOAD', $section_text) && !empty($section_text['UPLOAD'])) { |
868 | $upload_files = trim($section_text['UPLOAD']); |
868 | $upload_files = trim($section_text['UPLOAD']); |
869 | $upload_files = explode("\n", $upload_files); |
869 | $upload_files = explode("\n", $upload_files); |
870 | 870 | ||
871 | $request = "Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737\n" . |
871 | $request = "Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737\n" . |
872 | "-----------------------------20896060251896012921717172737\n"; |
872 | "-----------------------------20896060251896012921717172737\n"; |
873 | foreach ($upload_files as $fileinfo) { |
873 | foreach ($upload_files as $fileinfo) { |
874 | $fileinfo = explode('=', $fileinfo); |
874 | $fileinfo = explode('=', $fileinfo); |
875 | if (count($fileinfo) != 2) { |
875 | if (count($fileinfo) != 2) { |
876 | return PEAR::raiseError("Invalid UPLOAD section in test file: $file"); |
876 | return PEAR::raiseError("Invalid UPLOAD section in test file: $file"); |
877 | }
|
877 | }
|
878 | if (!realpath(dirname($file) . '/' . $fileinfo[1])) { |
878 | if (!realpath(dirname($file) . '/' . $fileinfo[1])) { |
879 | return PEAR::raiseError("File for upload does not exist: $fileinfo[1] " . |
879 | return PEAR::raiseError("File for upload does not exist: $fileinfo[1] " . |
880 | "in test file: $file"); |
880 | "in test file: $file"); |
881 | }
|
881 | }
|
882 | $file_contents = file_get_contents(dirname($file) . '/' . $fileinfo[1]); |
882 | $file_contents = file_get_contents(dirname($file) . '/' . $fileinfo[1]); |
883 | $fileinfo[1] = basename($fileinfo[1]); |
883 | $fileinfo[1] = basename($fileinfo[1]); |
884 | $request .= "Content-Disposition: form-data; name=\"$fileinfo[0]\"; filename=\"$fileinfo[1]\"\n"; |
884 | $request .= "Content-Disposition: form-data; name=\"$fileinfo[0]\"; filename=\"$fileinfo[1]\"\n"; |
885 | $request .= "Content-Type: text/plain\n\n"; |
885 | $request .= "Content-Type: text/plain\n\n"; |
886 | $request .= $file_contents . "\n" . |
886 | $request .= $file_contents . "\n" . |
887 | "-----------------------------20896060251896012921717172737\n"; |
887 | "-----------------------------20896060251896012921717172737\n"; |
888 | }
|
888 | }
|
889 | 889 | ||
890 | if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
890 | if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) { |
891 | // encode POST raw
|
891 | // encode POST raw
|
892 | $post = trim($section_text['POST']); |
892 | $post = trim($section_text['POST']); |
893 | $post = explode('&', $post); |
893 | $post = explode('&', $post); |
894 | foreach ($post as $i => $post_info) { |
894 | foreach ($post as $i => $post_info) { |
895 | $post_info = explode('=', $post_info); |
895 | $post_info = explode('=', $post_info); |
896 | if (count($post_info) != 2) { |
896 | if (count($post_info) != 2) { |
897 | return PEAR::raiseError("Invalid POST data in test file: $file"); |
897 | return PEAR::raiseError("Invalid POST data in test file: $file"); |
898 | }
|
898 | }
|
899 | $post_info[0] = rawurldecode($post_info[0]); |
899 | $post_info[0] = rawurldecode($post_info[0]); |
900 | $post_info[1] = rawurldecode($post_info[1]); |
900 | $post_info[1] = rawurldecode($post_info[1]); |
901 | $post[$i] = $post_info; |
901 | $post[$i] = $post_info; |
902 | }
|
902 | }
|
903 | foreach ($post as $post_info) { |
903 | foreach ($post as $post_info) { |
904 | $request .= "Content-Disposition: form-data; name=\"$post_info[0]\"\n\n"; |
904 | $request .= "Content-Disposition: form-data; name=\"$post_info[0]\"\n\n"; |
905 | $request .= $post_info[1] . "\n" . |
905 | $request .= $post_info[1] . "\n" . |
906 | "-----------------------------20896060251896012921717172737\n"; |
906 | "-----------------------------20896060251896012921717172737\n"; |
907 | }
|
907 | }
|
908 | unset($section_text['POST']); |
908 | unset($section_text['POST']); |
909 | }
|
909 | }
|
910 | $section_text['POST_RAW'] = $request; |
910 | $section_text['POST_RAW'] = $request; |
911 | }
|
911 | }
|
912 | 912 | ||
913 | return $section_text; |
913 | return $section_text; |
914 | }
|
914 | }
|
915 | 915 | ||
916 | function _testCleanup($section_text, $temp_clean) |
916 | function _testCleanup($section_text, $temp_clean) |
917 | {
|
917 | {
|
918 | if ($section_text['CLEAN']) { |
918 | if ($section_text['CLEAN']) { |
919 | // perform test cleanup
|
919 | // perform test cleanup
|
920 | $this->save_text($temp_clean, $section_text['CLEAN']); |
920 | $this->save_text($temp_clean, $section_text['CLEAN']); |
921 | $this->system_with_timeout("$this->_php $temp_clean"); |
921 | $this->system_with_timeout("$this->_php $temp_clean"); |
922 | if (file_exists($temp_clean)) { |
922 | if (file_exists($temp_clean)) { |
923 | unlink($temp_clean); |
923 | unlink($temp_clean); |
924 | }
|
924 | }
|
925 | }
|
925 | }
|
926 | }
|
926 | }
|
927 | }
|
927 | }
|