xref: /unit/test/test_static_chroot.py (revision 1960:a5c08e4a9946)
1import os
2from pathlib import Path
3
4import pytest
5
6from unit.applications.proto import TestApplicationProto
7
8
9class TestStaticChroot(TestApplicationProto):
10    prerequisites = {'features': ['chroot']}
11
12    @pytest.fixture(autouse=True)
13    def setup_method_fixture(self, temp_dir):
14        os.makedirs(temp_dir + '/assets/dir')
15        Path(temp_dir + '/assets/index.html').write_text('0123456789')
16        Path(temp_dir + '/assets/dir/file').write_text('blah')
17
18        test = Path(__file__)
19        self.test_path = '/' + test.parent.name + '/' + test.name
20
21        self._load_conf(
22            {
23                "listeners": {"*:7080": {"pass": "routes"}},
24                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
25            }
26        )
27
28    def test_static_chroot(self, temp_dir):
29        assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
30        assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
31
32        assert 'success' in self.conf(
33            {
34                "share": temp_dir + "/assets$uri",
35                "chroot": temp_dir + "/assets/dir",
36            },
37            'routes/0/action',
38        ), 'configure chroot'
39
40        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
41        assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
42        assert self.get(url='/file')['status'] == 403, 'chroot 403'
43
44    def test_static_chroot_permission(self, is_su, temp_dir):
45        if is_su:
46            pytest.skip('does\'t work under root')
47
48        os.chmod(temp_dir + '/assets/dir', 0o100)
49
50        assert 'success' in self.conf(
51            {
52                "share": temp_dir + "/assets$uri",
53                "chroot": temp_dir + "/assets/dir",
54            },
55            'routes/0/action',
56        ), 'configure chroot'
57
58        assert self.get(url='/dir/file')['status'] == 200, 'chroot'
59
60    def test_static_chroot_empty(self, temp_dir):
61        assert 'success' in self.conf(
62            {"share": temp_dir + "/assets$uri", "chroot": ""},
63            'routes/0/action',
64        ), 'configure chroot empty absolute'
65
66        assert (
67            self.get(url='/dir/file')['status'] == 200
68        ), 'chroot empty absolute'
69
70        assert 'success' in self.conf(
71            {"share": ".$uri", "chroot": ""}, 'routes/0/action',
72        ), 'configure chroot empty relative'
73
74        assert (
75            self.get(url=self.test_path)['status'] == 200
76        ), 'chroot empty relative'
77
78    def test_static_chroot_relative(self, is_su, temp_dir):
79        if is_su:
80            pytest.skip('does\'t work under root')
81
82        assert 'success' in self.conf(
83            {"share": temp_dir + "/assets$uri", "chroot": "."},
84            'routes/0/action',
85        ), 'configure relative chroot'
86
87        assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
88
89        assert 'success' in self.conf(
90            {"share": ".$uri"}, 'routes/0/action',
91        ), 'configure relative share'
92
93        assert self.get(url=self.test_path)['status'] == 200, 'relative share'
94
95        assert 'success' in self.conf(
96            {"share": ".$uri", "chroot": "."}, 'routes/0/action',
97        ), 'configure relative'
98
99        assert self.get(url=self.test_path)['status'] == 200, 'relative'
100
101    def test_static_chroot_invalid(self, temp_dir):
102        assert 'error' in self.conf(
103            {"share": temp_dir, "chroot": True}, 'routes/0/action',
104        ), 'configure chroot error'
105        assert 'error' in self.conf(
106            {"share": temp_dir, "symlinks": "True"}, 'routes/0/action',
107        ), 'configure symlink error'
108        assert 'error' in self.conf(
109            {"share": temp_dir, "mount": "True"}, 'routes/0/action',
110        ), 'configure mount error'
111