Tests: chdir() and open() for PHP module.
These tests ensure optimizations in the chdir calls don't break SAPI semantics.
This commit is contained in:
19
test/php/cwd/index.php
Normal file
19
test/php/cwd/index.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (isset($_GET['chdir']) && $_GET['chdir'] != "") {
|
||||||
|
if (!chdir($_GET['chdir'])) {
|
||||||
|
echo "failure to chdir(" . $_GET['chdir'] . ")\n";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$opcache = -1;
|
||||||
|
|
||||||
|
if (function_exists('opcache_get_status')) {
|
||||||
|
$opcache = opcache_get_status()->opcache_enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
header('X-OPcache: ' . $opcache);
|
||||||
|
|
||||||
|
print(getcwd());
|
||||||
|
?>
|
||||||
1
test/php/cwd/subdir/index.php
Normal file
1
test/php/cwd/subdir/index.php
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?php print(getcwd()); ?>
|
||||||
7
test/php/open/index.php
Normal file
7
test/php/open/index.php
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
if (isset($_GET['chdir'])) {
|
||||||
|
chdir($_GET['chdir']);
|
||||||
|
}
|
||||||
|
|
||||||
|
echo file_get_contents('test.txt');
|
||||||
|
?>
|
||||||
1
test/php/open/test.txt
Normal file
1
test/php/open/test.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
test
|
||||||
@@ -13,6 +13,28 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
self.assertRegex(body, r'time: \d+', 'disable_functions before time')
|
self.assertRegex(body, r'time: \d+', 'disable_functions before time')
|
||||||
self.assertRegex(body, r'exec: \/\w+', 'disable_functions before exec')
|
self.assertRegex(body, r'exec: \/\w+', 'disable_functions before exec')
|
||||||
|
|
||||||
|
def set_opcache(self, app, val):
|
||||||
|
self.assertIn(
|
||||||
|
'success',
|
||||||
|
self.conf(
|
||||||
|
{
|
||||||
|
"admin": {
|
||||||
|
"opcache.enable": val,
|
||||||
|
"opcache.enable_cli": val,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'applications/' + app + '/options',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
opcache = self.get()['headers']['X-OPcache']
|
||||||
|
|
||||||
|
if not opcache or opcache == '-1':
|
||||||
|
print('opcache is not supported')
|
||||||
|
raise unittest.SkipTest()
|
||||||
|
|
||||||
|
self.assertEqual(opcache, val, 'opcache value')
|
||||||
|
|
||||||
def test_php_application_variables(self):
|
def test_php_application_variables(self):
|
||||||
self.load('variables')
|
self.load('variables')
|
||||||
|
|
||||||
@@ -466,7 +488,8 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
|
|
||||||
def test_php_application_script(self):
|
def test_php_application_script(self):
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
'success', self.conf(
|
'success',
|
||||||
|
self.conf(
|
||||||
{
|
{
|
||||||
"listeners": {"*:7080": {"pass": "applications/script"}},
|
"listeners": {"*:7080": {"pass": "applications/script"}},
|
||||||
"applications": {
|
"applications": {
|
||||||
@@ -478,7 +501,8 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
), 'configure script'
|
),
|
||||||
|
'configure script',
|
||||||
)
|
)
|
||||||
|
|
||||||
resp = self.get()
|
resp = self.get()
|
||||||
@@ -488,7 +512,8 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
|
|
||||||
def test_php_application_index_default(self):
|
def test_php_application_index_default(self):
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
'success', self.conf(
|
'success',
|
||||||
|
self.conf(
|
||||||
{
|
{
|
||||||
"listeners": {"*:7080": {"pass": "applications/phpinfo"}},
|
"listeners": {"*:7080": {"pass": "applications/phpinfo"}},
|
||||||
"applications": {
|
"applications": {
|
||||||
@@ -499,7 +524,8 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
), 'configure index default'
|
),
|
||||||
|
'configure index default',
|
||||||
)
|
)
|
||||||
|
|
||||||
resp = self.get()
|
resp = self.get()
|
||||||
@@ -541,6 +567,107 @@ class TestPHPApplication(TestApplicationPHP):
|
|||||||
str(resp['status']) + resp['body'], '200', 'status new root'
|
str(resp['status']) + resp['body'], '200', 'status new root'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def run_php_application_cwd_root_tests(self):
|
||||||
|
self.assertIn(
|
||||||
|
'success', self.conf_delete('applications/cwd/working_directory')
|
||||||
|
)
|
||||||
|
|
||||||
|
script_cwd = self.current_dir + '/php/cwd'
|
||||||
|
|
||||||
|
resp = self.get()
|
||||||
|
self.assertEqual(resp['status'], 200, 'status ok')
|
||||||
|
self.assertEqual(resp['body'], script_cwd, 'default cwd')
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
'success',
|
||||||
|
self.conf(
|
||||||
|
'"' + self.current_dir + '"',
|
||||||
|
'applications/cwd/working_directory',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = self.get()
|
||||||
|
self.assertEqual(resp['status'], 200, 'status ok')
|
||||||
|
self.assertEqual(resp['body'], script_cwd, 'wdir cwd')
|
||||||
|
|
||||||
|
resp = self.get(url='/?chdir=/')
|
||||||
|
self.assertEqual(resp['status'], 200, 'status ok')
|
||||||
|
self.assertEqual(resp['body'], '/', 'cwd after chdir')
|
||||||
|
|
||||||
|
# cwd must be restored
|
||||||
|
|
||||||
|
resp = self.get()
|
||||||
|
self.assertEqual(resp['status'], 200, 'status ok')
|
||||||
|
self.assertEqual(resp['body'], script_cwd, 'cwd restored')
|
||||||
|
|
||||||
|
resp = self.get(url='/subdir/')
|
||||||
|
self.assertEqual(
|
||||||
|
resp['body'], script_cwd + '/subdir', 'cwd subdir',
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_php_application_cwd_root(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.run_php_application_cwd_root_tests()
|
||||||
|
|
||||||
|
def test_php_application_cwd_opcache_disabled(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.set_opcache('cwd', '0')
|
||||||
|
self.run_php_application_cwd_root_tests()
|
||||||
|
|
||||||
|
def test_php_application_cwd_opcache_enabled(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.set_opcache('cwd', '1')
|
||||||
|
self.run_php_application_cwd_root_tests()
|
||||||
|
|
||||||
|
def run_php_application_cwd_script_tests(self):
|
||||||
|
self.load('cwd')
|
||||||
|
|
||||||
|
script_cwd = self.current_dir + '/php/cwd'
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
'success', self.conf_delete('applications/cwd/working_directory')
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn(
|
||||||
|
'success', self.conf('"index.php"', 'applications/cwd/script')
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
self.get()['body'], script_cwd, 'default cwd',
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
self.get(url='/?chdir=/')['body'], '/', 'cwd after chdir',
|
||||||
|
)
|
||||||
|
|
||||||
|
# cwd must be restored
|
||||||
|
self.assertEqual(self.get()['body'], script_cwd, 'cwd restored')
|
||||||
|
|
||||||
|
def test_php_application_cwd_script(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.run_php_application_cwd_script_tests()
|
||||||
|
|
||||||
|
def test_php_application_cwd_script_opcache_disabled(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.set_opcache('cwd', '0')
|
||||||
|
self.run_php_application_cwd_script_tests()
|
||||||
|
|
||||||
|
def test_php_application_cwd_script_opcache_enabled(self):
|
||||||
|
self.load('cwd')
|
||||||
|
self.set_opcache('cwd', '1')
|
||||||
|
self.run_php_application_cwd_script_tests()
|
||||||
|
|
||||||
|
def test_php_application_path_relative(self):
|
||||||
|
self.load('open')
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['body'], 'test', 'relative path')
|
||||||
|
|
||||||
|
self.assertNotEqual(
|
||||||
|
self.get(url='/?chdir=/')['body'], 'test', 'relative path w/ chdir'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(self.get()['body'], 'test', 'relative path 2')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
TestPHPApplication.main()
|
TestPHPApplication.main()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from unit.applications.proto import TestApplicationProto
|
|||||||
class TestApplicationPHP(TestApplicationProto):
|
class TestApplicationPHP(TestApplicationProto):
|
||||||
application_type = "php"
|
application_type = "php"
|
||||||
|
|
||||||
def load(self, script, name='index.php', **kwargs):
|
def load(self, script, index='index.php', **kwargs):
|
||||||
script_path = self.current_dir + '/php/' + script
|
script_path = self.current_dir + '/php/' + script
|
||||||
|
|
||||||
self._load_conf(
|
self._load_conf(
|
||||||
@@ -16,7 +16,7 @@ class TestApplicationPHP(TestApplicationProto):
|
|||||||
"processes": {"spare": 0},
|
"processes": {"spare": 0},
|
||||||
"root": script_path,
|
"root": script_path,
|
||||||
"working_directory": script_path,
|
"working_directory": script_path,
|
||||||
"index": name,
|
"index": index,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user