aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_snekbox.py
blob: 0a05881f9a33b20b1f9cd8945a9304721e740565 (plain) (blame)
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
import unittest
import pytest
import os
import json

from snekbox import Snekbox
from rmq import Rmq

r = Rmq()

snek = Snekbox()


class SnekTests(unittest.TestCase):
    def test_nsjail(self):
        result = snek.python3('print("test")')
        self.assertEquals(result.strip(), 'test')

    # def test_memory_error(self):
    #     code = ('x = "*"\n'
    #             'while True:\n'
    #             '    x = x * 99\n')
    #     result = snek.python3(code)
    #     self.assertEquals(result.strip(), 'timed out or memory limit exceeded')

    def test_timeout(self):
        code = ('x = "*"\n'
        'while True:\n'
        '    try:\n'
        '        x = x * 99\n'
        '    except:\n'
        '        continue\n')

        result = snek.python3(code)
        self.assertEquals(result.strip(), 'timed out or memory limit exceeded')

    def test_kill(self):
        code = ('import subprocess\n'
                'print(subprocess.check_output("kill -9 6", shell=True).decode())')
        result = snek.python3(code)
        if 'ModuleNotFoundError' in result.strip():
            self.assertIn('ModuleNotFoundError', result.strip())
        else:
            self.assertIn('(PIDs left: 0)', result.strip())

    def test_forkbomb(self):
        code = ('import os\n'
                'while 1:\n'
                '    os.fork()')
        result = snek.python3(code)
        self.assertIn('Resource temporarily unavailable', result.strip())

    # def test_juan_golf(self):  # in honour of Juan
    #     code = ("exec(type((lambda:0).code)(0,1,0,0,0,b'\x09\x00',(),(),(),'','',1,b''))")
    #     result = snek.python3(code)
    #     self.assertEquals('ValueError: embedded null byte', result.strip())

#class RMQTests(unittest.TestCase):
#    @pytest.mark.dependency()
#    def test_a_publish(self):
#        message = json.dumps({"snekid": "test", "message": "print('test')"})
#        result = r.publish(message, queue='input')
#        self.assertTrue(result)

    # @pytest.mark.dependency(depends=["RMQTests::test_a_publish"])
    # def test_b_consume(self):
    #     result = r.consume(callback=snek.message_handler, queue='input', run_once=True)
    #     self.assertEquals(result[2], b'{"snekid": "test", "message": "print(\'test\')"}')