diff options
Diffstat (limited to '')
-rw-r--r-- | test/test_php_application.py | 495 |
1 files changed, 208 insertions, 287 deletions
diff --git a/test/test_php_application.py b/test/test_php_application.py index d8bfade2..18a5c085 100644 --- a/test/test_php_application.py +++ b/test/test_php_application.py @@ -1,10 +1,11 @@ import os +import pytest import re import shutil import time -import unittest from unit.applications.lang.php import TestApplicationPHP +from conftest import option class TestPHPApplication(TestApplicationPHP): prerequisites = {'modules': {'php': 'all'}} @@ -12,30 +13,21 @@ class TestPHPApplication(TestApplicationPHP): def before_disable_functions(self): body = self.get()['body'] - self.assertRegex(body, r'time: \d+', 'disable_functions before time') - self.assertRegex(body, r'exec: \/\w+', 'disable_functions before exec') + assert re.search(r'time: \d+', body), 'disable_functions before time' + assert re.search(r'exec: \/\w+', body), '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', - ), + assert 'success' in 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() + pytest.skip('opcache is not supported') - self.assertEqual(opcache, val, 'opcache value') + assert opcache == val, 'opcache value' def test_php_application_variables(self): self.load('variables') @@ -50,140 +42,122 @@ class TestPHPApplication(TestApplicationPHP): 'Connection': 'close', }, body=body, - url='/index.php/blah?var=val' + url='/index.php/blah?var=val', ) - self.assertEqual(resp['status'], 200, 'status') + assert resp['status'] == 200, 'status' headers = resp['headers'] header_server = headers.pop('Server') - self.assertRegex(header_server, r'Unit/[\d\.]+', 'server header') - self.assertEqual( - headers.pop('Server-Software'), - header_server, - 'server software header', - ) + assert re.search(r'Unit/[\d\.]+', header_server), 'server header' + assert ( + headers.pop('Server-Software') == header_server + ), 'server software header' date = headers.pop('Date') - self.assertEqual(date[-4:], ' GMT', 'date header timezone') - self.assertLess( - abs(self.date_to_sec_epoch(date) - self.sec_epoch()), - 5, - 'date header', - ) + assert date[-4:] == ' GMT', 'date header timezone' + assert ( + abs(self.date_to_sec_epoch(date) - self.sec_epoch()) < 5 + ), 'date header' if 'X-Powered-By' in headers: headers.pop('X-Powered-By') headers.pop('Content-type') - self.assertDictEqual( - headers, - { - 'Connection': 'close', - 'Content-Length': str(len(body)), - 'Request-Method': 'POST', - 'Path-Info': '/blah', - 'Request-Uri': '/index.php/blah?var=val', - 'Http-Host': 'localhost', - 'Server-Protocol': 'HTTP/1.1', - 'Custom-Header': 'blah', - }, - 'headers', - ) - self.assertEqual(resp['body'], body, 'body') + assert headers == { + 'Connection': 'close', + 'Content-Length': str(len(body)), + 'Request-Method': 'POST', + 'Path-Info': '/blah', + 'Request-Uri': '/index.php/blah?var=val', + 'Http-Host': 'localhost', + 'Server-Protocol': 'HTTP/1.1', + 'Custom-Header': 'blah', + }, 'headers' + assert resp['body'] == body, 'body' def test_php_application_query_string(self): self.load('query_string') resp = self.get(url='/?var1=val1&var2=val2') - self.assertEqual( - resp['headers']['Query-String'], - 'var1=val1&var2=val2', - 'query string', - ) + assert ( + resp['headers']['Query-String'] == 'var1=val1&var2=val2' + ), 'query string' def test_php_application_query_string_empty(self): self.load('query_string') resp = self.get(url='/?') - self.assertEqual(resp['status'], 200, 'query string empty status') - self.assertEqual( - resp['headers']['Query-String'], '', 'query string empty' - ) + assert resp['status'] == 200, 'query string empty status' + assert resp['headers']['Query-String'] == '', 'query string empty' def test_php_application_query_string_absent(self): self.load('query_string') resp = self.get() - self.assertEqual(resp['status'], 200, 'query string absent status') - self.assertEqual( - resp['headers']['Query-String'], '', 'query string absent' - ) + assert resp['status'] == 200, 'query string absent status' + assert resp['headers']['Query-String'] == '', 'query string absent' def test_php_application_phpinfo(self): self.load('phpinfo') resp = self.get() - self.assertEqual(resp['status'], 200, 'status') - self.assertNotEqual(resp['body'], '', 'body not empty') + assert resp['status'] == 200, 'status' + assert resp['body'] != '', 'body not empty' def test_php_application_header_status(self): self.load('header') - self.assertEqual( + assert ( self.get( headers={ 'Host': 'localhost', 'Connection': 'close', 'X-Header': 'HTTP/1.1 404 Not Found', } - )['status'], - 404, - 'status', - ) + )['status'] + == 404 + ), 'status' - self.assertEqual( + assert ( self.get( headers={ 'Host': 'localhost', 'Connection': 'close', 'X-Header': 'http/1.1 404 Not Found', } - )['status'], - 404, - 'status case insensitive', - ) + )['status'] + == 404 + ), 'status case insensitive' - self.assertEqual( + assert ( self.get( headers={ 'Host': 'localhost', 'Connection': 'close', 'X-Header': 'HTTP/ 404 Not Found', } - )['status'], - 404, - 'status version empty', - ) - + )['status'] + == 404 + ), 'status version empty' def test_php_application_404(self): self.load('404') resp = self.get() - self.assertEqual(resp['status'], 404, '404 status') - self.assertRegex( - resp['body'], r'<title>404 Not Found</title>', '404 body' - ) + assert resp['status'] == 404, '404 status' + assert re.search( + r'<title>404 Not Found</title>', resp['body'] + ), '404 body' def test_php_application_keepalive_body(self): self.load('mirror') - self.assertEqual(self.get()['status'], 200, 'init') + assert self.get()['status'] == 200, 'init' body = '0123456789' * 500 (resp, sock) = self.post( @@ -197,7 +171,7 @@ class TestPHPApplication(TestApplicationPHP): read_timeout=1, ) - self.assertEqual(resp['body'], body, 'keep-alive 1') + assert resp['body'] == body, 'keep-alive 1' body = '0123456789' resp = self.post( @@ -210,22 +184,22 @@ class TestPHPApplication(TestApplicationPHP): body=body, ) - self.assertEqual(resp['body'], body, 'keep-alive 2') + assert resp['body'] == body, 'keep-alive 2' def test_php_application_conditional(self): self.load('conditional') - self.assertRegex(self.get()['body'], r'True', 'conditional true') - self.assertRegex(self.post()['body'], r'False', 'conditional false') + assert re.search(r'True', self.get()['body']), 'conditional true' + assert re.search(r'False', self.post()['body']), 'conditional false' def test_php_application_get_variables(self): self.load('get_variables') resp = self.get(url='/?var1=val1&var2=&var3') - self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'GET variables') - self.assertEqual(resp['headers']['X-Var-2'], '1', 'GET variables 2') - self.assertEqual(resp['headers']['X-Var-3'], '1', 'GET variables 3') - self.assertEqual(resp['headers']['X-Var-4'], '', 'GET variables 4') + assert resp['headers']['X-Var-1'] == 'val1', 'GET variables' + assert resp['headers']['X-Var-2'] == '1', 'GET variables 2' + assert resp['headers']['X-Var-3'] == '1', 'GET variables 3' + assert resp['headers']['X-Var-4'] == '', 'GET variables 4' def test_php_application_post_variables(self): self.load('post_variables') @@ -238,9 +212,9 @@ class TestPHPApplication(TestApplicationPHP): }, body='var1=val1&var2=', ) - self.assertEqual(resp['headers']['X-Var-1'], 'val1', 'POST variables') - self.assertEqual(resp['headers']['X-Var-2'], '1', 'POST variables 2') - self.assertEqual(resp['headers']['X-Var-3'], '', 'POST variables 3') + assert resp['headers']['X-Var-1'] == 'val1', 'POST variables' + assert resp['headers']['X-Var-2'] == '1', 'POST variables 2' + assert resp['headers']['X-Var-3'] == '', 'POST variables 3' def test_php_application_cookies(self): self.load('cookies') @@ -253,41 +227,32 @@ class TestPHPApplication(TestApplicationPHP): } ) - self.assertEqual(resp['headers']['X-Cookie-1'], 'val', 'cookie') - self.assertEqual(resp['headers']['X-Cookie-2'], 'val2', 'cookie') + assert resp['headers']['X-Cookie-1'] == 'val', 'cookie' + assert resp['headers']['X-Cookie-2'] == 'val2', 'cookie' def test_php_application_ini_precision(self): self.load('ini_precision') - self.assertNotEqual( - self.get()['headers']['X-Precision'], '4', 'ini value default' - ) + assert self.get()['headers']['X-Precision'] != '4', 'ini value default' self.conf( {"file": "ini/php.ini"}, 'applications/ini_precision/options' ) - self.assertEqual( - self.get()['headers']['X-File'], - self.current_dir + '/php/ini_precision/ini/php.ini', - 'ini file', - ) - self.assertEqual( - self.get()['headers']['X-Precision'], '4', 'ini value' - ) + assert ( + self.get()['headers']['X-File'] + == option.test_dir + '/php/ini_precision/ini/php.ini' + ), 'ini file' + assert self.get()['headers']['X-Precision'] == '4', 'ini value' - @unittest.skip('not yet') + @pytest.mark.skip('not yet') def test_php_application_ini_admin_user(self): self.load('ini_precision') - self.assertIn( - 'error', - self.conf( - {"user": {"precision": "4"}, "admin": {"precision": "5"}}, - 'applications/ini_precision/options', - ), - 'ini admin user', - ) + assert 'error' in self.conf( + {"user": {"precision": "4"}, "admin": {"precision": "5"}}, + 'applications/ini_precision/options', + ), 'ini admin user' def test_php_application_ini_admin(self): self.load('ini_precision') @@ -297,9 +262,7 @@ class TestPHPApplication(TestApplicationPHP): 'applications/ini_precision/options', ) - self.assertEqual( - self.get()['headers']['X-Precision'], '5', 'ini value admin' - ) + assert self.get()['headers']['X-Precision'] == '5', 'ini value admin' def test_php_application_ini_user(self): self.load('ini_precision') @@ -309,9 +272,7 @@ class TestPHPApplication(TestApplicationPHP): 'applications/ini_precision/options', ) - self.assertEqual( - self.get()['headers']['X-Precision'], '5', 'ini value user' - ) + assert self.get()['headers']['X-Precision'] == '5', 'ini value user' def test_php_application_ini_user_2(self): self.load('ini_precision') @@ -320,17 +281,13 @@ class TestPHPApplication(TestApplicationPHP): {"file": "ini/php.ini"}, 'applications/ini_precision/options' ) - self.assertEqual( - self.get()['headers']['X-Precision'], '4', 'ini user file' - ) + assert self.get()['headers']['X-Precision'] == '4', 'ini user file' self.conf( {"precision": "5"}, 'applications/ini_precision/options/user' ) - self.assertEqual( - self.get()['headers']['X-Precision'], '5', 'ini value user' - ) + assert self.get()['headers']['X-Precision'] == '5', 'ini value user' def test_php_application_ini_set_admin(self): self.load('ini_precision') @@ -339,11 +296,9 @@ class TestPHPApplication(TestApplicationPHP): {"admin": {"precision": "5"}}, 'applications/ini_precision/options' ) - self.assertEqual( - self.get(url='/?precision=6')['headers']['X-Precision'], - '5', - 'ini set admin', - ) + assert ( + self.get(url='/?precision=6')['headers']['X-Precision'] == '5' + ), 'ini set admin' def test_php_application_ini_set_user(self): self.load('ini_precision') @@ -352,11 +307,9 @@ class TestPHPApplication(TestApplicationPHP): {"user": {"precision": "5"}}, 'applications/ini_precision/options' ) - self.assertEqual( - self.get(url='/?precision=6')['headers']['X-Precision'], - '6', - 'ini set user', - ) + assert ( + self.get(url='/?precision=6')['headers']['X-Precision'] == '6' + ), 'ini set user' def test_php_application_ini_repeat(self): self.load('ini_precision') @@ -365,13 +318,9 @@ class TestPHPApplication(TestApplicationPHP): {"user": {"precision": "5"}}, 'applications/ini_precision/options' ) - self.assertEqual( - self.get()['headers']['X-Precision'], '5', 'ini value' - ) + assert self.get()['headers']['X-Precision'] == '5', 'ini value' - self.assertEqual( - self.get()['headers']['X-Precision'], '5', 'ini value repeat' - ) + assert self.get()['headers']['X-Precision'] == '5', 'ini value repeat' def test_php_application_disable_functions_exec(self): self.load('time_exec') @@ -385,8 +334,8 @@ class TestPHPApplication(TestApplicationPHP): body = self.get()['body'] - self.assertRegex(body, r'time: \d+', 'disable_functions time') - self.assertNotRegex(body, r'exec: \/\w+', 'disable_functions exec') + assert re.search(r'time: \d+', body), 'disable_functions time' + assert not re.search(r'exec: \/\w+', body), 'disable_functions exec' def test_php_application_disable_functions_comma(self): self.load('time_exec') @@ -400,10 +349,12 @@ class TestPHPApplication(TestApplicationPHP): body = self.get()['body'] - self.assertNotRegex(body, r'time: \d+', 'disable_functions comma time') - self.assertNotRegex( - body, r'exec: \/\w+', 'disable_functions comma exec' - ) + assert not re.search( + r'time: \d+', body + ), 'disable_functions comma time' + assert not re.search( + r'exec: \/\w+', body + ), 'disable_functions comma exec' def test_php_application_disable_functions_space(self): self.load('time_exec') @@ -417,10 +368,12 @@ class TestPHPApplication(TestApplicationPHP): body = self.get()['body'] - self.assertNotRegex(body, r'time: \d+', 'disable_functions space time') - self.assertNotRegex( - body, r'exec: \/\w+', 'disable_functions space exec' - ) + assert not re.search( + r'time: \d+', body + ), 'disable_functions space time' + assert not re.search( + r'exec: \/\w+', body + ), 'disable_functions space exec' def test_php_application_disable_functions_user(self): self.load('time_exec') @@ -434,10 +387,10 @@ class TestPHPApplication(TestApplicationPHP): body = self.get()['body'] - self.assertRegex(body, r'time: \d+', 'disable_functions user time') - self.assertNotRegex( - body, r'exec: \/\w+', 'disable_functions user exec' - ) + assert re.search(r'time: \d+', body), 'disable_functions user time' + assert not re.search( + r'exec: \/\w+', body + ), 'disable_functions user exec' def test_php_application_disable_functions_nonexistent(self): self.load('time_exec') @@ -451,187 +404,165 @@ class TestPHPApplication(TestApplicationPHP): body = self.get()['body'] - self.assertRegex( - body, r'time: \d+', 'disable_functions nonexistent time' - ) - self.assertRegex( - body, r'exec: \/\w+', 'disable_functions nonexistent exec' - ) + assert re.search( + r'time: \d+', body + ), 'disable_functions nonexistent time' + assert re.search( + r'exec: \/\w+', body + ), 'disable_functions nonexistent exec' def test_php_application_disable_classes(self): self.load('date_time') - self.assertRegex( - self.get()['body'], r'012345', 'disable_classes before' - ) + assert re.search( + r'012345', self.get()['body'] + ), 'disable_classes before' self.conf( {"admin": {"disable_classes": "DateTime"}}, 'applications/date_time/options', ) - self.assertNotRegex( - self.get()['body'], r'012345', 'disable_classes before' - ) + assert not re.search( + r'012345', self.get()['body'] + ), 'disable_classes before' def test_php_application_disable_classes_user(self): self.load('date_time') - self.assertRegex( - self.get()['body'], r'012345', 'disable_classes before' - ) + assert re.search( + r'012345', self.get()['body'] + ), 'disable_classes before' self.conf( {"user": {"disable_classes": "DateTime"}}, 'applications/date_time/options', ) - self.assertNotRegex( - self.get()['body'], r'012345', 'disable_classes before' - ) + assert not re.search( + r'012345', self.get()['body'] + ), 'disable_classes before' def test_php_application_error_log(self): self.load('error_log') - self.assertEqual(self.get()['status'], 200, 'status') + assert self.get()['status'] == 200, 'status' time.sleep(1) - self.assertEqual(self.get()['status'], 200, 'status 2') + assert self.get()['status'] == 200, 'status 2' self.stop() pattern = r'\d{4}\/\d\d\/\d\d\s\d\d:.+\[notice\].+Error in application' - self.assertIsNotNone(self.wait_for_record(pattern), 'errors print') + assert self.wait_for_record(pattern) is not None, 'errors print' - with open(self.testdir + '/unit.log', 'r', errors='ignore') as f: + with open(self.temp_dir + '/unit.log', 'r', errors='ignore') as f: errs = re.findall(pattern, f.read()) - self.assertEqual(len(errs), 2, 'error_log count') + assert len(errs) == 2, 'error_log count' date = errs[0].split('[')[0] date2 = errs[1].split('[')[0] - self.assertNotEqual(date, date2, 'date diff') + assert date != date2, 'date diff' def test_php_application_script(self): - self.assertIn( - 'success', - self.conf( - { - "listeners": {"*:7080": {"pass": "applications/script"}}, - "applications": { - "script": { - "type": "php", - "processes": {"spare": 0}, - "root": self.current_dir + "/php/script", - "script": "phpinfo.php", - } - }, - } - ), - 'configure script', - ) + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "applications/script"}}, + "applications": { + "script": { + "type": "php", + "processes": {"spare": 0}, + "root": option.test_dir + "/php/script", + "script": "phpinfo.php", + } + }, + } + ), 'configure script' resp = self.get() - self.assertEqual(resp['status'], 200, 'status') - self.assertNotEqual(resp['body'], '', 'body not empty') + assert resp['status'] == 200, 'status' + assert resp['body'] != '', 'body not empty' def test_php_application_index_default(self): - self.assertIn( - 'success', - self.conf( - { - "listeners": {"*:7080": {"pass": "applications/phpinfo"}}, - "applications": { - "phpinfo": { - "type": "php", - "processes": {"spare": 0}, - "root": self.current_dir + "/php/phpinfo", - } - }, - } - ), - 'configure index default', - ) + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "applications/phpinfo"}}, + "applications": { + "phpinfo": { + "type": "php", + "processes": {"spare": 0}, + "root": option.test_dir + "/php/phpinfo", + } + }, + } + ), 'configure index default' resp = self.get() - self.assertEqual(resp['status'], 200, 'status') - self.assertNotEqual(resp['body'], '', 'body not empty') + assert resp['status'] == 200, 'status' + assert resp['body'] != '', 'body not empty' def test_php_application_extension_check(self): self.load('phpinfo') - self.assertNotEqual( - self.get(url='/index.wrong')['status'], 200, 'status' - ) + assert self.get(url='/index.wrong')['status'] != 200, 'status' - new_root = self.testdir + "/php" + new_root = self.temp_dir + "/php" os.mkdir(new_root) - shutil.copy(self.current_dir + '/php/phpinfo/index.wrong', new_root) - - self.assertIn( - 'success', - self.conf( - { - "listeners": {"*:7080": {"pass": "applications/phpinfo"}}, - "applications": { - "phpinfo": { - "type": "php", - "processes": {"spare": 0}, - "root": new_root, - "working_directory": new_root, - } - }, - } - ), - 'configure new root', - ) + shutil.copy(option.test_dir + '/php/phpinfo/index.wrong', new_root) + + assert 'success' in self.conf( + { + "listeners": {"*:7080": {"pass": "applications/phpinfo"}}, + "applications": { + "phpinfo": { + "type": "php", + "processes": {"spare": 0}, + "root": new_root, + "working_directory": new_root, + } + }, + } + ), 'configure new root' resp = self.get() - self.assertNotEqual( - str(resp['status']) + resp['body'], '200', 'status new root' - ) + assert 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') + assert 'success' in self.conf_delete( + 'applications/cwd/working_directory' ) - script_cwd = self.current_dir + '/php/cwd' + script_cwd = option.test_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', - ), + assert resp['status'] == 200, 'status ok' + assert resp['body'] == script_cwd, 'default cwd' + + assert 'success' in self.conf( + '"' + option.test_dir + '"', 'applications/cwd/working_directory', ) resp = self.get() - self.assertEqual(resp['status'], 200, 'status ok') - self.assertEqual(resp['body'], script_cwd, 'wdir cwd') + assert resp['status'] == 200, 'status ok' + assert 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') + assert resp['status'] == 200, 'status ok' + assert 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') + assert resp['status'] == 200, 'status ok' + assert resp['body'] == script_cwd, 'cwd restored' resp = self.get(url='/subdir/') - self.assertEqual( - resp['body'], script_cwd + '/subdir', 'cwd subdir', - ) + assert resp['body'] == script_cwd + '/subdir', 'cwd subdir' def test_php_application_cwd_root(self): self.load('cwd') @@ -650,26 +581,20 @@ class TestPHPApplication(TestApplicationPHP): def run_php_application_cwd_script_tests(self): self.load('cwd') - script_cwd = self.current_dir + '/php/cwd' + script_cwd = option.test_dir + '/php/cwd' - self.assertIn( - 'success', self.conf_delete('applications/cwd/working_directory') + assert 'success' in self.conf_delete( + 'applications/cwd/working_directory' ) - self.assertIn( - 'success', self.conf('"index.php"', 'applications/cwd/script') - ) + assert 'success' in self.conf('"index.php"', 'applications/cwd/script') - self.assertEqual( - self.get()['body'], script_cwd, 'default cwd', - ) + assert self.get()['body'] == script_cwd, 'default cwd' - self.assertEqual( - self.get(url='/?chdir=/')['body'], '/', 'cwd after chdir', - ) + assert self.get(url='/?chdir=/')['body'] == '/', 'cwd after chdir' # cwd must be restored - self.assertEqual(self.get()['body'], script_cwd, 'cwd restored') + assert self.get()['body'] == script_cwd, 'cwd restored' def test_php_application_cwd_script(self): self.load('cwd') @@ -688,14 +613,10 @@ class TestPHPApplication(TestApplicationPHP): 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') + assert self.get()['body'] == 'test', 'relative path' + assert ( + self.get(url='/?chdir=/')['body'] != 'test' + ), 'relative path w/ chdir' -if __name__ == '__main__': - TestPHPApplication.main() + assert self.get()['body'] == 'test', 'relative path 2' |