1#!/usr/bin/env python
2
3import logging
4l = logging.getLogger("angr_tests")
5
6import nose
7import angr
8
9# load the tests
10import os
11test_location = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'binaries', 'tests')
12loop_nolibs = None
13
14def setup_module():
15    global loop_nolibs
16    loop_nolibs = angr.Project(os.path.join(test_location, 'x86_64', 'loop'),  default_analysis_mode='symbolic')
17
18def test_loop_entry():
19    s = loop_nolibs.sim_run(loop_nolibs.exit_to(0x4004f4))
20    s_loop = loop_nolibs.sim_run(loop_nolibs.exit_to(0x40051A, s.exits()[0].state))
21    nose.tools.assert_equals(len(s_loop.exits()), 2)
22    nose.tools.assert_true(s_loop.exits()[0].reachable()) # True
23    nose.tools.assert_false(s_loop.exits()[1].reachable()) # False
24
25def test_loop_escape():
26    loop_addrs = [ 0x40051A, 0x400512 ]
27    s = loop_nolibs.sim_run(loop_nolibs.exit_to(0x4004F4))
28    results = angr.surveyors.Escaper(loop_nolibs, loop_addrs, start=s.exits()[0], loop_iterations=4).run()
29    nose.tools.assert_equal(results.forced[0].addr, 0x400520)
30
31def test_loop_escape_head():
32    loop_addrs = [ 0x40051A, 0x400512 ]
33    s = loop_nolibs.sim_run(loop_nolibs.state_generator.blank_state(address=0x4004F4))
34    first_head = loop_nolibs.surveyors.Explorer(start=s.successors[0], find=0x400512).run().found[0]
35    results = loop_nolibs.surveyors.Escaper(loop_addrs, start=first_head, loop_iterations=4).run()
36    nose.tools.assert_equal(results.forced[0].addr, 0x400520)
37
38if __name__ == '__main__':
39    try:
40        __import__('standard_logging')
41        __import__('angr_debug')
42    except ImportError:
43        pass
44    setup_module()
45    test_loop_escape_head()
46