summaryrefslogtreecommitdiff
path: root/tests/test_all.py
blob: eed6d26fe37d1e6e7d580ce5f0d06328b7a8d8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright (C) 2015-2016  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


'''Functional ('black-box') tests for all 'sandboxlib' backends.'''


import pytest

import os

import sandboxlib
from programs import (
    file_is_writable_test_program, file_or_directory_exists_test_program,
    session_tmpdir)


@pytest.fixture(params=['chroot', 'linux_user_chroot'])
def sandboxlib_executor(request):
    executor = getattr(sandboxlib, request.param)

    if request.param == 'chroot' and os.getuid() != 0:
        pytest.skip('chroot backend can only be used by root users')

    return executor


def test_no_output(sandboxlib_executor):
    '''Test ignoring of stderr/stdout.

    We could use run_sandbox_with_redirection() and not get the 'err' and 'out'
    parameters at all, but we may as well test that they are indeed None.

    '''
    exit, out, err = sandboxlib_executor.run_sandbox(
        ['echo', 'xyzzy'], stdout=None, stderr=None)

    assert exit == 0
    assert out is None
    assert err is None

def test_output(sandboxlib_executor):
    exit, out, err = sandboxlib_executor.run_sandbox(['echo', 'xyzzy'])

    assert exit == 0
    assert out.decode('unicode-escape') == 'xyzzy\n'
    assert err.decode('unicode-escape') == ''

    exit, out, err = sandboxlib_executor.run_sandbox(
        ['sh', '-c', 'echo xyzzy >&2; exit 11'])

    assert exit == 11
    assert out.decode('unicode-escape') == ''
    assert err.decode('unicode-escape') == 'xyzzy\n'

def test_output_redirection(sandboxlib_executor, tmpdir):
    outlog_fp = str(tmpdir.join('outlog.txt'))
    errlog_fp = str(tmpdir.join('errlog.txt'))
    with open(outlog_fp, 'w') as outlog, open(errlog_fp, 'w') as errlog:
        exit = sandboxlib_executor.run_sandbox_with_redirection(
             ['sh', '-c', 'echo abcde; echo xyzzy >&2'],
             stdout=outlog, stderr=errlog)

    with open(outlog_fp) as outlog, open(errlog_fp) as errlog:
        assert outlog.read() == 'abcde\n'
        assert errlog.read() == 'xyzzy\n'

    with open(outlog_fp, 'w') as outlog, open(errlog_fp, 'w') as errlog:
        exit = sandboxlib_executor.run_sandbox_with_redirection(
             ['sh', '-c', 'echo abcde; echo xyzzy >&2'],
             stdout=outlog, stderr=sandboxlib.STDOUT)

    with open(outlog_fp) as outlog:
        assert outlog.read() == 'abcde\nxyzzy\n'

def test_current_working_directory(sandboxlib_executor, tmpdir):
    exit, out, err = sandboxlib_executor.run_sandbox(
        ['pwd'], cwd=str(tmpdir))

    assert exit == 0
    assert out.decode('unicode-escape') == '%s\n' % str(tmpdir)
    assert err.decode('unicode-escape') == ''

def test_environment(sandboxlib_executor):
    exit, out, err = sandboxlib_executor.run_sandbox(
        ['env'], env={'foo': 'bar'})

    assert exit == 0
    assert out.decode('unicode-escape') == 'foo=bar\n'
    assert err.decode('unicode-escape') == ''

def test_isolated_mounts(sandboxlib_executor):
    if sandboxlib_executor == sandboxlib.chroot:
        pytest.skip('chroot backend does not support mounts isolation')
    elif sandboxlib_executor == sandboxlib.linux_user_chroot:
       # linux-user-chroot always creates a new mount namespace
       pass

def test_isolated_network(sandboxlib_executor):
    if sandboxlib_executor == sandboxlib.chroot:
        pytest.skip('chroot backend does not support network isolation')

    exit, out, err = sandboxlib_executor.run_sandbox(
        ['sh', '-c', 'cat /proc/net/dev | sed 1,2d | cut -f1 -d:'],
        network='isolated')

    assert exit == 0
    assert out.decode('unicode-escape').strip() == 'lo'
    assert err.decode('unicode-escape') == ''


class TestMounts(object):
    @pytest.fixture()
    def mounts_test_sandbox(self, tmpdir,
                            file_or_directory_exists_test_program):
        sandbox_path = tmpdir.mkdir('sandbox')

        bin_path = sandbox_path.mkdir('bin')

        file_or_directory_exists_test_program.copy(bin_path)
        bin_path.join('test-file-or-directory-exists').chmod(0o755)

        return sandbox_path

    def test_mount_proc(self, sandboxlib_executor, mounts_test_sandbox):
        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-or-directory-exists', '/proc'],
            filesystem_root=str(mounts_test_sandbox),
            extra_mounts=[(None, '/proc', 'proc')])

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == "/proc exists"
        assert exit == 0

    def test_mount_tmpfs(self, sandboxlib_executor, mounts_test_sandbox):
        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-or-directory-exists', '/dev/shm'],
            filesystem_root=str(mounts_test_sandbox),
            extra_mounts=[(None, '/dev/shm', 'tmpfs')])

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == "/dev/shm exists"
        assert exit == 0

    def test_invalid_mount_specs(self, sandboxlib_executor):
        with pytest.raises(AssertionError) as excinfo:
            exit, out, err = sandboxlib_executor.run_sandbox(
                ['true'], extra_mounts=[('proc', None, 'tmpfs')])

            assert excinfo.value.message == (
                "Mount point empty in mount entry ('proc', None, 'tmpfs')")

        with pytest.raises(AssertionError) as excinfo:
            exit, out, err = sandboxlib_executor.run_sandbox(
                ['true'], extra_mounts=[('proc', 'tmpfs')])

            assert excinfo.value.message == (
                "Invalid mount entry in 'extra_mounts': ('proc', 'tmpfs')")


class TestWriteablePaths(object):
    @pytest.fixture()
    def writable_paths_test_sandbox(self, tmpdir,
                                    file_is_writable_test_program):
        sandbox_path = tmpdir.mkdir('sandbox')

        bin_path = sandbox_path.mkdir('bin')

        file_is_writable_test_program.copy(bin_path)
        bin_path.join('test-file-is-writable').chmod(0o755)

        data_path = sandbox_path.mkdir('data')
        data_path = data_path.mkdir('1')
        data_path.join('canary').write("Please don't overwrite me.")

        return sandbox_path

    def test_none_writable(self, sandboxlib_executor,
                           writable_paths_test_sandbox):
        if sandboxlib_executor == sandboxlib.chroot:
            pytest.xfail("chroot backend doesn't support read-only paths.")

        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-is-writable', '/data/1/canary'],
            filesystem_root=str(writable_paths_test_sandbox),
            filesystem_writable_paths='none')

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == \
            "Couldn't open /data/1/canary for writing."
        assert exit == 1

    def test_some_writable(self, sandboxlib_executor,
                           writable_paths_test_sandbox):
        if sandboxlib_executor == sandboxlib.chroot:
            pytest.xfail("chroot backend doesn't support read-only paths.")

        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-is-writable', '/data/1/canary'],
            filesystem_root=str(writable_paths_test_sandbox),
            filesystem_writable_paths=['/data/1'])

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == \
            "Wrote data to /data/1/canary."
        assert exit == 0

    def test_all_writable(self, sandboxlib_executor,
                          writable_paths_test_sandbox):
        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-is-writable', '/data/1/canary'],
            filesystem_root=str(writable_paths_test_sandbox),
            filesystem_writable_paths='all')

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == \
            "Wrote data to /data/1/canary."
        assert exit == 0

    def test_mount_point_not_writable(self, sandboxlib_executor,
                                      writable_paths_test_sandbox):
        if sandboxlib_executor == sandboxlib.chroot:
            pytest.xfail("chroot backend doesn't support read-only paths.")

        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-is-writable', '/data/1/canary'],
            filesystem_root=str(writable_paths_test_sandbox),
            filesystem_writable_paths='none')

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == \
            "Couldn't open /data/1/canary for writing."
        assert exit == 1

    def test_mount_point_writable(self, sandboxlib_executor,
                                  writable_paths_test_sandbox):
        if sandboxlib_executor == sandboxlib.chroot:
            pytest.xfail("chroot backend doesn't support read-only paths.")

        exit, out, err = sandboxlib_executor.run_sandbox(
            ['/bin/test-file-is-writable', '/data/1/canary'],
            filesystem_root=str(writable_paths_test_sandbox),
            filesystem_writable_paths=['/data'])

        assert err.decode('unicode-escape') == ''
        assert out.decode('unicode-escape') == \
            "Wrote data to /data/1/canary."
        assert exit == 0


def test_executor_for_platform():
    '''Simple test of backend autodetection.'''
    executor = sandboxlib.executor_for_platform()
    test_output(executor)

def test_sandboxlib_backend_env_var(sandboxlib_executor):
    executor_name = sandboxlib_executor.__name__.split('.')[-1]
    os.environ["SANDBOXLIB_BACKEND"] = executor_name
    executor = sandboxlib.executor_for_platform()
    assert executor == sandboxlib_executor

def test_sandboxlib_backend_env_var_unknown_executor():
    executor = sandboxlib.executor_for_platform()
    os.environ["SANDBOXLIB_BACKEND"] = "unknown"
    assert executor == sandboxlib.executor_for_platform()

def test_degrade_config_for_capabilities(sandboxlib_executor):
    '''Simple test of adjusting configuration for a given backend.'''
    in_config = {
        'mounts': 'isolated',
        'network': 'isolated',
        'filesystem_writable_paths': ['/tmp']
    }

    out_config = sandboxlib_executor.degrade_config_for_capabilities(
        in_config, warn=True)

    if sandboxlib_executor == sandboxlib.chroot:
        assert out_config == {
            'mounts': 'undefined',
            'network': 'undefined',
            'filesystem_writable_paths': 'all'
        }
    elif sandboxlib_executor == sandboxlib.linux_user_chroot:
        assert out_config == in_config