-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
95 lines (69 loc) · 2.26 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
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
import tempfile
import dojjail
import os
def test_exec():
host = dojjail.Host()
host.run()
assert host.exec(lambda: 1 + 1) == 2
def test_entrypoint():
class TestHost(dojjail.Host):
def entrypoint(self):
return 42
host = TestHost()
host.run()
assert host.wait() == 42
try:
host.wait()
except ChildProcessError:
pass
else:
assert False, "Host not terminated"
def test_exception():
host = dojjail.Host()
host.run()
def raise_exception():
raise Exception("Test exception")
try:
host.exec(raise_exception)
except Exception as e:
assert str(e) == "Test exception"
else:
assert False, "Exception not raised"
def test_busybox():
host = dojjail.BusyBoxFSHost()
host.run()
assert b"bin" in host.exec_shell("ls /").stdout
assert host.exec_shell("ls /tmp").stdout == b""
assert not host.exec(lambda: os.path.exists("/flag"))
host = dojjail.BusyBoxFSHost(flag_source="/flag")
host.run()
assert host.exec(lambda: os.path.exists("/flag"))
assert host.exec_shell("cat /flag") != b""
def test_simplefs():
td = tempfile.mkdtemp()
host = dojjail.SimpleFSHost(src_path=td)
host.run()
print(host.exec(lambda: os.listdir("/")))
def test_networking():
network = dojjail.Network()
host_1 = dojjail.Host()
host_2 = dojjail.Host()
host_3 = dojjail.Host()
network.connect(host_1, host_2)
network.connect(host_2, host_3)
network.run()
def ping(ip):
return f"timeout 1 ping -c 1 {ip}"
assert host_1.exec_shell(ping("10.0.0.1")).returncode == 0
assert host_1.exec_shell(ping("10.0.0.2")).returncode == 0
assert host_1.exec_shell(ping("10.0.0.3")).returncode != 0
assert host_2.exec_shell(ping("10.0.0.1")).returncode == 0
assert host_2.exec_shell(ping("10.0.0.2")).returncode == 0
assert host_2.exec_shell(ping("10.0.0.3")).returncode == 0
assert host_3.exec_shell(ping("10.0.0.1")).returncode != 0
assert host_3.exec_shell(ping("10.0.0.2")).returncode == 0
assert host_3.exec_shell(ping("10.0.0.3")).returncode == 0
def test_seccomp():
host = dojjail.Host(seccomp_block=["getppid"])
host.run()
assert host.exec(lambda: os.getppid()) == -1