xref: /unit/test/test_static_chroot.py (revision 1902:17547cf0ed39)
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"}}],
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",
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",
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", "chroot": ""}, 'routes/0/action',
63        ), 'configure chroot empty absolute'
64
65        assert (
66            self.get(url='/dir/file')['status'] == 200
67        ), 'chroot empty absolute'
68
69        assert 'success' in self.conf(
70            {"share": ".", "chroot": ""}, 'routes/0/action',
71        ), 'configure chroot empty relative'
72
73        assert (
74            self.get(url=self.test_path)['status'] == 200
75        ), 'chroot empty relative'
76
77    def test_static_chroot_relative(self, is_su, temp_dir):
78        if is_su:
79            pytest.skip('does\'t work under root')
80
81        assert 'success' in self.conf(
82            {"share": temp_dir + "/assets", "chroot": "."}, 'routes/0/action',
83        ), 'configure relative chroot'
84
85        assert self.get(url='/dir/file')['status'] == 403, 'relative chroot'
86
87        assert 'success' in self.conf(
88            {"share": "."}, 'routes/0/action',
89        ), 'configure relative share'
90
91        assert self.get(url=self.test_path)['status'] == 200, 'relative share'
92
93        assert 'success' in self.conf(
94            {"share": ".", "chroot": "."}, 'routes/0/action',
95        ), 'configure relative'
96
97        assert self.get(url=self.test_path)['status'] == 200, 'relative'
98
99    def test_static_chroot_invalid(self, temp_dir):
100        assert 'error' in self.conf(
101            {"share": temp_dir, "chroot": True}, 'routes/0/action',
102        ), 'configure chroot error'
103        assert 'error' in self.conf(
104            {"share": temp_dir, "symlinks": "True"}, 'routes/0/action',
105        ), 'configure symlink error'
106        assert 'error' in self.conf(
107            {"share": temp_dir, "mount": "True"}, 'routes/0/action',
108        ), 'configure mount error'
109