2021-05-05 11:36:57 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from unit.applications.proto import TestApplicationProto
|
2023-02-21 17:21:29 +00:00
|
|
|
from unit.option import option
|
2021-05-05 11:36:57 +00:00
|
|
|
|
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
class TestStaticChroot(TestApplicationProto):
|
2021-05-05 11:36:57 +00:00
|
|
|
prerequisites = {'features': ['chroot']}
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_method_fixture(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
os.makedirs(f'{temp_dir}/assets/dir')
|
|
|
|
Path(f'{temp_dir}/assets/index.html').write_text('0123456789')
|
|
|
|
Path(f'{temp_dir}/assets/dir/file').write_text('blah')
|
2021-05-05 11:36:57 +00:00
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
self.test_path = f'/{os.path.relpath(Path(__file__))}'
|
2021-05-05 11:36:57 +00:00
|
|
|
|
|
|
|
self._load_conf(
|
|
|
|
{
|
|
|
|
"listeners": {"*:7080": {"pass": "routes"}},
|
2023-02-21 17:21:29 +00:00
|
|
|
"routes": [{"action": {"share": f'{temp_dir}/assets$uri'}}],
|
2021-05-05 11:36:57 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
def update_action(self, chroot, share=f'{option.temp_dir}/assets$uri'):
|
2021-10-05 11:43:05 +00:00
|
|
|
return self.conf(
|
2023-02-21 17:21:29 +00:00
|
|
|
{'chroot': chroot, 'share': share},
|
2022-04-11 20:05:14 +00:00
|
|
|
'routes/0/action',
|
2021-10-05 11:43:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_custom(self, uri, host):
|
2022-04-11 20:05:14 +00:00
|
|
|
return self.get(url=uri, headers={'Host': host, 'Connection': 'close'})[
|
|
|
|
'status'
|
|
|
|
]
|
2021-10-05 11:43:05 +00:00
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
def test_static_chroot(self, temp_dir):
|
2021-05-05 11:36:57 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
|
|
|
|
assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/dir')
|
2021-05-05 11:36:57 +00:00
|
|
|
|
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'chroot'
|
|
|
|
assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
|
|
|
|
assert self.get(url='/file')['status'] == 403, 'chroot 403'
|
|
|
|
|
2021-10-05 11:43:05 +00:00
|
|
|
def test_share_chroot_array(self, temp_dir):
|
2022-06-30 13:40:17 +00:00
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
f'{temp_dir}/assets/dir', ["/blah", f'{temp_dir}/assets$uri']
|
2022-06-30 13:40:17 +00:00
|
|
|
)
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'share array'
|
|
|
|
|
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
f'{temp_dir}/assets/$host',
|
|
|
|
['/blah', f'{temp_dir}/assets$uri'],
|
2021-10-05 11:43:05 +00:00
|
|
|
)
|
|
|
|
assert self.get_custom('/dir/file', 'dir') == 200, 'array variable'
|
|
|
|
|
2022-06-30 13:40:17 +00:00
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
f'{temp_dir}/assets/dir', ['/blah', '/blah2']
|
2022-06-30 13:40:17 +00:00
|
|
|
)
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get()['status'] != 200, 'share array bad'
|
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
def test_static_chroot_permission(self, is_su, temp_dir):
|
2021-06-24 03:01:15 +00:00
|
|
|
if is_su:
|
2023-02-21 17:21:29 +00:00
|
|
|
pytest.skip("does't work under root")
|
2021-06-24 03:01:15 +00:00
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
os.chmod(f'{temp_dir}/assets/dir', 0o100)
|
2021-05-05 11:36:57 +00:00
|
|
|
|
2022-06-30 13:40:17 +00:00
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
f'{temp_dir}/assets/dir'
|
2021-05-05 11:36:57 +00:00
|
|
|
), 'configure chroot'
|
|
|
|
|
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'chroot'
|
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
def test_static_chroot_empty(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action('')
|
2022-06-30 13:40:17 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'empty absolute'
|
2021-05-05 11:36:57 +00:00
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action("", ".$uri")
|
2022-06-30 13:40:17 +00:00
|
|
|
assert self.get(url=self.test_path)['status'] == 200, 'empty relative'
|
2021-05-05 11:36:57 +00:00
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
def test_static_chroot_relative(self, is_su, temp_dir):
|
2021-05-05 11:36:57 +00:00
|
|
|
if is_su:
|
2023-02-21 17:21:29 +00:00
|
|
|
pytest.skip("Does't work under root.")
|
2021-05-05 11:36:57 +00:00
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action('.')
|
2021-05-05 11:36:57 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
|
|
|
|
|
2022-06-30 13:40:17 +00:00
|
|
|
assert 'success' in self.conf({"share": ".$uri"}, 'routes/0/action')
|
2021-05-05 11:36:57 +00:00
|
|
|
assert self.get(url=self.test_path)['status'] == 200, 'relative share'
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(".", ".$uri")
|
2021-05-05 11:36:57 +00:00
|
|
|
assert self.get(url=self.test_path)['status'] == 200, 'relative'
|
|
|
|
|
2021-10-18 00:10:11 +00:00
|
|
|
def test_static_chroot_variables(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/$host')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get_custom('/dir/file', 'dir') == 200
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/${{host}}')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get_custom('/dir/file', 'dir') == 200
|
|
|
|
|
2021-10-18 00:10:11 +00:00
|
|
|
def test_static_chroot_variables_buildin_start(self, temp_dir):
|
2021-10-05 11:43:05 +00:00
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
'$uri/assets/dir',
|
|
|
|
f'{temp_dir}/assets/dir/$host',
|
2021-10-05 11:43:05 +00:00
|
|
|
)
|
|
|
|
assert self.get_custom(temp_dir, 'file') == 200
|
|
|
|
|
2021-10-18 00:10:11 +00:00
|
|
|
def test_static_chroot_variables_buildin_mid(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/$host/dir')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get_custom('/dir/file', 'assets') == 200
|
|
|
|
|
2021-10-18 00:10:11 +00:00
|
|
|
def test_static_chroot_variables_buildin_end(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/$host')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get_custom('/dir/file', 'dir') == 200
|
|
|
|
|
|
|
|
def test_static_chroot_slash(self, temp_dir):
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/dir/')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'slash end'
|
|
|
|
assert self.get(url='/dirxfile')['status'] == 403, 'slash end bad'
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/dir')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'no slash end'
|
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'success' in self.update_action(f'{temp_dir}/assets/dir/')
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'slash end 2'
|
|
|
|
assert self.get(url='/dirxfile')['status'] == 403, 'slash end 2 bad'
|
|
|
|
|
2022-06-30 13:40:17 +00:00
|
|
|
assert 'success' in self.update_action(
|
2023-02-21 17:21:29 +00:00
|
|
|
f'{temp_dir}//assets////dir///', f'{temp_dir}///assets/////$uri'
|
2022-06-30 13:40:17 +00:00
|
|
|
)
|
2021-10-05 11:43:05 +00:00
|
|
|
assert self.get(url='/dir/file')['status'] == 200, 'multiple slashes'
|
|
|
|
|
2021-06-28 21:05:40 +00:00
|
|
|
def test_static_chroot_invalid(self, temp_dir):
|
2021-05-05 11:36:57 +00:00
|
|
|
assert 'error' in self.conf(
|
2022-04-11 20:05:14 +00:00
|
|
|
{"share": temp_dir, "chroot": True},
|
|
|
|
'routes/0/action',
|
2021-05-05 11:36:57 +00:00
|
|
|
), 'configure chroot error'
|
|
|
|
assert 'error' in self.conf(
|
2022-04-11 20:05:14 +00:00
|
|
|
{"share": temp_dir, "symlinks": "True"},
|
|
|
|
'routes/0/action',
|
2021-05-05 11:36:57 +00:00
|
|
|
), 'configure symlink error'
|
|
|
|
assert 'error' in self.conf(
|
2022-04-11 20:05:14 +00:00
|
|
|
{"share": temp_dir, "mount": "True"},
|
|
|
|
'routes/0/action',
|
2021-05-05 11:36:57 +00:00
|
|
|
), 'configure mount error'
|
2021-10-05 11:43:05 +00:00
|
|
|
|
2023-02-21 17:21:29 +00:00
|
|
|
assert 'error' in self.update_action(f'{temp_dir}/assets/d$r$uri')
|
|
|
|
assert 'error' in self.update_action(f'{temp_dir}/assets/$$uri')
|