manticore/tests/test_memdumps.py
JP Smith e32701f978 Refactor logging (#140)
* begin refactoring logging to use manticore object

* fix verbosity things

* fix logging when in scripts

* remove m.log_file now that it's no longer used

* remove unnecessary init_logging usage and unused log_debug variable

* accidentally deleted the wrong line

* re-hide init_logging

* remove old log_debug

* remove unnecessary API stuff and refactor around that

* re-introduce logging PR

* fix Popen

* lost a line in merge

* fix test_binaries

* Log to stdout instead of stderr

* implement mark's changes
2017-04-19 01:21:14 -05:00

108 lines
3.1 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:
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 = ['manticore', '--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)
def testSimpleParse(self):
self._runManticore("simple_parse")
def testSimpleDeref(self):
self._runManticore("simple_bad_deref")
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(mark); skipping so we can move on with our lives and merge x86_new. ask felipe to fix later.')
def testWin32API(self):
self._runManticore("win32_api_test")
def testAPIInterception(self):
self._runManticore("api_interception")
if __name__ == '__main__':
unittest.main()