manticore/tests/test_memdumps.py
feliam 520a9be47d Dev - events (#341)
* Wip refactoring

* Executor and exceptions refactor wip wip

* Fixing all_insts auto tests

* Visited and generate testcase now at manticore api level

* Aggregating state statistics into executor statistics

* Wip refactoring

* Executor and exceptions refactor wip wip

* Fixing all_insts auto tests

* Visited and generate testcase now at manticore api level

* Aggregating state statistics into executor statistics

* forwarding events wip

* state setstate fix and setup_stack merge fix

* will_terminate_state fix and tests skipped

* Update all ConcretizeRegister and ConcretizeMemory

* Wip refactoring

* Executor and exceptions refactor wip wip

* Fixing all_insts auto tests

* Visited and generate testcase now at manticore api level

* Aggregating state statistics into executor statistics

* Wip refactoring

* Executor and exceptions refactor wip wip

* Fixing all_insts auto tests

* Visited and generate testcase now at manticore api level

* Aggregating state statistics into executor statistics

* forwarding events wip

* state setstate fix and setup_stack merge fix

* will_terminate_state fix and tests skipped

* Update all ConcretizeRegister and ConcretizeMemory

* Exceptions are crazy crazy crazy

* fix last merge

* Merge merge until it pass

* Instructions count default to 0

* will/did execute/emulate

* Delayed keybpoard interrupt now shutdowns nicely

* fix auto test generator x86

* Undo bad merge

* utterly hopeless

* basic working

* Fix merge bugs and github comments

* Remove unnecesary comment - github comments

* trace_item not used there

* model-platform and system.py fixed

* backup/restore to store/load -- cpu.instruction property

* Slightly better did/will naming and dynamic signal forwarding

* platform.constraints and cpu.instruction as properties

* Fix forward signals getattr

* set las decoded pc at decode_instruction() / reenable instruction_cache

* Signals name convention: did/will/on

* Forward normal signals

* Maintain last decoded pc in abstractcpu

* Changed context manager so it just wont raise interrupt

* Decree now forwards signals and sets constraints

* linux.SymbolicFile does not need to maintain constraints

* remove debbug print

* Assimilating some PR commets

* size_total == size

* better merge of manticore.py

* typo

* Forwarding only specified objects in signal arguments

* Fix few broken tests

* revert + merge

* remove some unused stuff from manticore()

* manticore context <-> executor context

* manticore context <-> executor context2

* context context context

* forgotten return

* Fix basix.arm

* arm bitwise fix

* fix context

* Comment 1

* Comment 2

* Comment 3

* Comment 4

* Comment 5

* Comment 6

* Fix (still needs refactor but it works) profiling

* Fix (still needs refactor but it works) profiling

* The forgotten bit

* Update tests to reflect current output

* Verbosity fix

* Fix verbosity test
2017-06-26 18:06:18 -03:00

113 lines
3.2 KiB
Python

import unittest
import sys
import shutil
import tempfile
import os
import hashlib
import subprocess
import json
import time
#logging.basicConfig(filename = "test.log",
# format = "%(asctime)s: %(name)s:%(levelname)s: %(message)s",
# level = logging.DEBUG)
class IntegrationTest(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
# Remove the directory after the test
shutil.rmtree(self.test_dir)
def _getDumpParams(self, jsonf):
self.assertTrue(os.path.exists(jsonf))
c = open(jsonf, 'r').read()
return json.loads(c)
def _loadVisitedSet(self, visited):
self.assertTrue(os.path.exists(visited))
vitems = open(visited, 'r').read().splitlines()
vitems = map(lambda x: int(x[2:], 16), vitems)
return set(vitems)
def _runWithTimeout(self, procargs, timeout=600):
with open(os.path.join(os.pardir, "logfile"), "w") as output:
#with open(os.path.join(os.pardir, "/dev/stdout"), "w") as output:
po = subprocess.Popen(procargs, stdout=output)
secs_used = 0
while po.poll() is None and secs_used < timeout:
time.sleep(1)
sys.stderr.write("~")
secs_used += 1
self.assertTrue(secs_used < timeout)
sys.stderr.write("\n")
def _runManticore(self, dumpname):
dirname = os.path.dirname(__file__)
dumpdir = os.path.abspath(os.path.join(dirname, 'memdumps', dumpname))
self.assertTrue(os.path.exists(dumpdir))
jsonfile = os.path.join(dumpdir, 'args.json')
params = self._getDumpParams(jsonfile)
workspace = os.path.join(self.test_dir, 'ws_{}'.format(dumpname))
logfile = os.path.join(workspace, "output.log")
dumpfile = os.path.join(dumpdir, params['dump'])
args = ['python', '-m', 'manticore', '--timeout', '400', '--workspace', workspace, dumpfile]
for k,v in params.iteritems():
if k.startswith("--"):
args.extend([k, v.format(dumpdir=dumpdir, workspace=workspace)])
self._runWithTimeout(args, logfile)
efile = os.path.join(dumpdir, params['expected'])
expected = self._loadVisitedSet(efile)
afile = os.path.join(workspace, params['actual'])
actual = self._loadVisitedSet(afile)
self.assertEqual(actual, expected)
@unittest.skip('TODO')
def testSimpleParse(self):
self._runManticore("simple_parse")
@unittest.skip('TODO')
def testSimpleDeref(self):
self._runManticore("simple_bad_deref")
@unittest.skip('TODO')
def testSimpleBufferOverflow(self):
self._runManticore("simple_buffer_overflow")
# generate too many states on memory concretization
#def testSimpleFpu(self):
# self._runManticore("simple_fpu")
# too slow processing REP SCASD
@unittest.skip('TODO')
def testWin32API(self):
self._runManticore("win32_api_test")
@unittest.skip('TODO')
def testAPIInterception(self):
self._runManticore("api_interception")
if __name__ == '__main__':
unittest.main()