manticore/tests/test_binaries.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

119 lines
4.8 KiB
Python

import StringIO
import unittest
import sys
import shutil
import tempfile
import os
import hashlib
import subprocess
import time
#logging.basicConfig(filename = "test.log",
# format = "%(asctime)s: %(name)s:%(levelname)s: %(message)s",
# level = logging.DEBUG)
class IntegrationTest(unittest.TestCase):
_multiprocess_can_split_ = True
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 _runWithTimeout(self, procargs, logfile, timeout=1200):
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.assertEqual(po.returncode, 0)
self.assertTrue(secs_used < timeout)
sys.stderr.write("\n")
def testTimeout(self):
dirname = os.path.dirname(__file__)
filename = os.path.abspath(os.path.join(dirname, 'binaries/arguments_linux_amd64'))
self.assertTrue(filename.startswith(os.getcwd()))
filename = filename[len(os.getcwd())+1:]
data = file(filename,'rb').read()
self.assertEqual(len(data), 767152)
self.assertEqual(hashlib.md5(data).hexdigest() , '00fb23e47831a1054ca4a74656035472')
workspace = '%s/workspace'%self.test_dir
t = time.time()
with open(os.path.join(os.pardir, '%s/output.log'%self.test_dir), "w") as output:
subprocess.check_call(['python', '-m', 'manticore',
'--workspace', workspace,
'--timeout', '1',
'--procs', '4',
filename,
'+++++++++'], stdout=output)
self.assertTrue(time.time()-t < 20)
def test_cli_verbosity(self):
"""
Tests that default verbosity produces the expected volume of output
"""
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'binaries/basic_linux_amd64')
output = subprocess.check_output(['python', '-m', 'manticore', filename])
self.assertLessEqual(len(output.splitlines()), 25)
@unittest.skip('TODO(mark); skipping so we can move on with our lives and merge x86_new. ask felipe to fix later.')
def testArgumentsAssertions(self):
dirname = os.path.dirname(__file__)
filename = os.path.abspath(os.path.join(dirname, 'binaries/arguments_linux_amd64'))
self.assertTrue(filename.startswith(os.getcwd()))
filename = filename[len(os.getcwd())+1:]
SE = os.path.join(dirname, '../main.py')
data = file(filename,'rb').read()
self.assertEqual(len(data), 767152)
self.assertEqual(hashlib.md5(data).hexdigest() , '00fb23e47831a1054ca4a74656035472')
workspace = '%s/workspace'%self.test_dir
assertions = '%s/assertions.txt'%self.test_dir
file(assertions,'w').write('0x0000000000401003 ZF == 1')
with open(os.path.join(os.pardir, '%s/output.log'%self.test_dir), "w") as output:
self._runWithTimeout(['python', SE,
'--workspace', workspace,
'--proc', '4',
'--assertions', assertions,
filename,
'+++++++++'], stdout=output)
data = file('%s/visited.txt'%workspace,'r').read()
data = '\n'.join(sorted(set(data.split('\n'))))
self.assertEqual(hashlib.md5(data).hexdigest() , 'c52d7d471ba5c94fcf59936086821a6b')
def testDecree(self):
dirname = os.path.dirname(__file__)
filename = os.path.abspath(os.path.join(dirname, 'binaries/cadet_decree_x86'))
self.assertTrue(filename.startswith(os.getcwd()))
filename = filename[len(os.getcwd())+1:]
SE = os.path.join(dirname, '../main.py')
data = file(filename,'rb').read()
self.assertEqual(len(data), 1828)
self.assertEqual(hashlib.md5(data).hexdigest() , '8955a29d51c1edd39b0e53794ebcf464')
workspace = '%s/workspace'%self.test_dir
self._runWithTimeout(['python', '-m', 'manticore',
'--workspace', workspace,
'--timeout', '20',
'--proc', '4',
'--policy', 'uncovered',
filename], '%s/output.log'%self.test_dir)
data = file('%s/visited.txt'%workspace,'r').read()
visited = len(set(data.split('\n')))
self.assertTrue(visited > 100 )
if __name__ == '__main__':
unittest.main()