/
test_workspace.py
109 lines (85 loc) · 3.24 KB
/
test_workspace.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import unittest
from manticore.core.smtlib import ConstraintSet
from manticore.core.workspace import *
from manticore.native.state import State
from manticore.platforms import linux
from manticore.utils.event import Eventful
class FakeMemory:
def __init__(self):
self._constraints = None
@property
def constraints(self):
return self._constraints
@constraints.setter
def constraints(self, constraints):
self._constraints = constraints
class FakeCpu(Eventful):
def __init__(self):
super().__init__()
self._memory = FakeMemory()
@property
def memory(self):
return self._memory
class FakePlatform:
def __init__(self):
self._constraints = None
self.procs = [FakeCpu()]
@property
def current(self):
return self.procs[0]
@property
def constraints(self):
return self._constraints
@constraints.setter
def constraints(self, constraints):
self._constraints = constraints
for proc in self.procs:
proc.memory.constraints = constraints
class StateTest(unittest.TestCase):
_multiprocess_can_split_ = True
def setUp(self):
if not hasattr(self, 'manager'):
self.manager = SyncManager()
self.manager.start(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN))
dirname = os.path.dirname(__file__)
l = linux.Linux(os.path.join(dirname, 'binaries', 'basic_linux_amd64'))
self.state = State(ConstraintSet(), l)
self.lock = self.manager.Condition()
def test_workspace_save_load(self):
self.state.constraints.add(True)
workspace = Workspace(self.lock, 'mem:')
id_ = workspace.save_state(self.state)
state = workspace.load_state(id_)
# Make sure our memory maps come back through serialization
for left, right in zip(sorted(self.state.mem._maps),
sorted(state.mem._maps)):
self.assertEqual(left.start, right.start)
self.assertEqual(left.end, right.end)
self.assertEqual(left.name, right.name)
# Check constraints
self.assertEqual(str(state.constraints),
str(self.state.constraints))
def test_workspace_id_start_with_zero(self):
workspace = Workspace(self.lock, 'mem:')
id_ = workspace.save_state(self.state)
self.assertEquals(id_, 0)
def test_output(self):
out = ManticoreOutput('mem:')
name = 'mytest'
message = 'custom message'
out.save_testcase(self.state, name, message)
workspace = out._store._data
# Make sure names are constructed correctly
for entry, data in workspace.items():
self.assertTrue(entry.startswith(name))
if 'messages' in entry:
self.assertTrue(message in data)
keys = [x.split('.')[1] for x in workspace.keys()]
for key in self.state.platform.generate_workspace_files():
self.assertIn(key, keys)
# Make sure we log everything we should be logging
self.assertIn('smt', keys)
self.assertIn('trace', keys)
self.assertIn('messages', keys)
self.assertIn('input', keys)
self.assertIn('pkl', keys)