-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
69 lines (55 loc) · 1.95 KB
/
tests.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# some tests for cval
from cval import cval, IllegalSource, SuspiciousSource, Warning
# ONLY USED FOR TESTING
import os
# disable module importing
# :NOTE: modules is False by default, and the reason we allow function calls
# is to see the error given when trying to import a module.
try: cval('__import__("os")', calls=True, modules=False)
except IllegalSource:
print("Passed test 1")
# allow certain modules
if cval('__import__("os")', allowed_modules=["os"], allowed_calls=["import"]) == os:
print("Passed test 2")
# allow certain function calls
cval('print("Passed test 3")', allowed_calls=["print"])
# block access to global variables
foo = "bar"
def foobar():
try: cval('print(foo)', globals=globals(), allowed_calls=["print"]) # Will not be able to access "foo"
except SuspiciousSource:
print("Passed test 4")
foobar()
# allow some access to global variables
foo = "bar"
def foobar():
cval('print(foo, end=": ")', globals=globals(), allowed_global_vars=["foo"], allowed_calls=["print"])
print("Passed test 5")
foobar()
# alternativly allow access to all global variables
foo = "bar"
bar = "foo"
def foobar():
cval('print(bar+foo, end=": ")', globals=globals(), allowed_global_vars=["*"], allowed_calls=["print"])
print("Passed test 6")
foobar()
# block access to local variables
def fizzbuzz():
fizz = "buzz"
try: cval('print(fizz)', locals=locals()) # Will not be able to access "fizz"
except SuspiciousSource:
print("Passed test 7")
fizzbuzz()
# allow some access to local variables
def fizzbuzz():
fizz = "buzz"
cval('print(fizz, end=": ")', locals=locals(), allowed_local_vars=["fizz"], allowed_calls=["print"])
print("Passed test 8")
fizzbuzz()
# alternativly allow access to all local variables
def fizzbuzz():
fizz = "buzz"
buzz = "fizz"
cval('print(buzz+fizz, end=": ")', locals=locals(), allowed_local_vars=["*"], allowed_calls=["print"])
print("Passed test 9")
fizzbuzz()