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:
Tiago Natel de Moura
2020-03-03 18:53:26 +00:00
parent 293b0da520
commit 80763b3e64
6 changed files with 161 additions and 6 deletions

19
test/php/cwd/index.php Normal file
View 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());
?>

View File

@@ -0,0 +1 @@
<?php print(getcwd()); ?>

7
test/php/open/index.php Normal file
View 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
View File

@@ -0,0 +1 @@
test

View File

@@ -13,6 +13,28 @@ class TestPHPApplication(TestApplicationPHP):
self.assertRegex(body, r'time: \d+', 'disable_functions before time')
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):
self.load('variables')
@@ -466,7 +488,8 @@ class TestPHPApplication(TestApplicationPHP):
def test_php_application_script(self):
self.assertIn(
'success', self.conf(
'success',
self.conf(
{
"listeners": {"*:7080": {"pass": "applications/script"}},
"applications": {
@@ -478,7 +501,8 @@ class TestPHPApplication(TestApplicationPHP):
}
},
}
), 'configure script'
),
'configure script',
)
resp = self.get()
@@ -488,7 +512,8 @@ class TestPHPApplication(TestApplicationPHP):
def test_php_application_index_default(self):
self.assertIn(
'success', self.conf(
'success',
self.conf(
{
"listeners": {"*:7080": {"pass": "applications/phpinfo"}},
"applications": {
@@ -499,7 +524,8 @@ class TestPHPApplication(TestApplicationPHP):
}
},
}
), 'configure index default'
),
'configure index default',
)
resp = self.get()
@@ -541,6 +567,107 @@ class TestPHPApplication(TestApplicationPHP):
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__':
TestPHPApplication.main()

View File

@@ -4,7 +4,7 @@ from unit.applications.proto import TestApplicationProto
class TestApplicationPHP(TestApplicationProto):
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
self._load_conf(
@@ -16,7 +16,7 @@ class TestApplicationPHP(TestApplicationProto):
"processes": {"spare": 0},
"root": script_path,
"working_directory": script_path,
"index": name,
"index": index,
}
},
},