forked from ideasman42/py_safe_math_eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
56 lines (37 loc) · 1.35 KB
/
test.py
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
import unittest
from safe_eval import safe_eval
math_namespace = {}
math_namespace.update(__import__("math").__dict__)
class TestSafeEval_Secure(unittest.TestCase):
"""
Every one of these tests must evaluate and evaluate to a correct value.
"""
def test_logic_01(self):
self.assertEqual(safe_eval("bool(2)"), True)
def test_logic_02(self):
self.assertEqual(safe_eval("True is not False"), True)
def test_math_01(self):
self.assertEqual(safe_eval("1 + 1"), 2)
def test_math_02(self):
self.assertEqual(safe_eval("2 ** 13 // 4"), 2048)
def test_math_module_01(self):
self.assertEqual(safe_eval("sqrt(9)", globals=math_namespace), 3)
class TestSafeEval_Insecure(unittest.TestCase):
"""
Every one of these tests must raise a runtime error
and should _never_ execute.
Note that these tests shouldn't break the system if they _do_ execute.
"""
def _expr_fails(self, expr):
with self.assertRaises(RuntimeError):
safe_eval(expr)
def test_open_01(self):
self._expr_fails("open()")
def test_open_02(self):
self._expr_fails("open('test', 'r')")
def test_import_01(self):
self._expr_fails("__import__('os')")
def test_attr_01(self):
self._expr_fails("''.__class__")
if __name__ == '__main__':
unittest.main()