diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-02-25 11:09:54 +0100 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-03-05 16:52:49 +0100 |
| commit | 02bbb30efea4980c9d133947cbbf69fb599071ad (patch) | |
| tree | 3fea6826fcc5354840a27cb1dc99ff31eef81896 /test/core | |
| parent | eab809932871f91d6f4aa770fc321af9e156e0f5 (diff) | |
| download | focaccia-miasm-02bbb30efea4980c9d133947cbbf69fb599071ad.tar.gz focaccia-miasm-02bbb30efea4980c9d133947cbbf69fb599071ad.zip | |
Support python2/python3
Diffstat (limited to 'test/core')
| -rw-r--r-- | test/core/asmblock.py | 43 | ||||
| -rw-r--r-- | test/core/graph.py | 15 | ||||
| -rwxr-xr-x | test/core/interval.py | 11 | ||||
| -rw-r--r-- | test/core/locationdb.py | 7 | ||||
| -rwxr-xr-x | test/core/parse_asm.py | 5 | ||||
| -rw-r--r-- | test/core/sembuilder.py | 19 | ||||
| -rwxr-xr-x | test/core/test_types.py | 67 | ||||
| -rwxr-xr-x | test/core/utils.py | 8 |
8 files changed, 95 insertions, 80 deletions
diff --git a/test/core/asmblock.py b/test/core/asmblock.py index c3e1d11d..48e81e78 100644 --- a/test/core/asmblock.py +++ b/test/core/asmblock.py @@ -1,5 +1,10 @@ +from __future__ import print_function +from builtins import map from pdb import pm +from future.utils import viewitems + +from miasm2.core.utils import decode_hex from miasm2.analysis.machine import Machine from miasm2.analysis.binary import Container from miasm2.core.asmblock import AsmCFG, AsmConstraint, AsmBlock, \ @@ -9,7 +14,7 @@ from miasm2.core.graph import DiGraphSimplifier, MatchGraphJoker from miasm2.expression.expression import ExprId # Initial data: from 'samples/simple_test.bin' -data = "5589e583ec10837d08007509c745fc01100000eb73837d08017709c745fc02100000eb64837d08057709c745fc03100000eb55837d080774138b450801c083f80e7509c745fc04100000eb3c8b450801c083f80e7509c745fc05100000eb298b450883e03085c07409c745fc06100000eb16837d08427509c745fc07100000eb07c745fc081000008b45fcc9c3".decode("hex") +data = decode_hex("5589e583ec10837d08007509c745fc01100000eb73837d08017709c745fc02100000eb64837d08057709c745fc03100000eb55837d080774138b450801c083f80e7509c745fc04100000eb3c8b450801c083f80e7509c745fc05100000eb298b450883e03085c07409c745fc06100000eb16837d08427509c745fc07100000eb07c745fc081000008b45fcc9c3") cont = Container.from_string(data) # Test Disasm engine @@ -18,12 +23,12 @@ mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) ## Disassembly of one block first_block = mdis.dis_block(0) assert len(first_block.lines) == 5 -print first_block +print(first_block) ## Test redisassemble asmcfg first_block_bis = mdis.dis_block(0) assert len(first_block.lines) == len(first_block_bis.lines) -print first_block_bis +print(first_block_bis) ## Disassembly of several block, with cache asmcfg = mdis.dis_multiblock(0) @@ -214,7 +219,7 @@ assert len(asmcfg.pendings) == 0 asmcfg.sanity_check() # Test block_merge -data2 = "31c0eb0c31c9750c31d2eb0c31ffebf831dbebf031edebfc31f6ebf031e4c3".decode("hex") +data2 = decode_hex("31c0eb0c31c9750c31d2eb0c31ffebf831dbebf031edebfc31f6ebf031e4c3") cont2 = Container.from_string(data2) mdis = machine.dis_engine(cont2.bin_stream, loc_db=cont2.loc_db) ## Elements to merge @@ -234,35 +239,35 @@ assert len(asmcfg) == 5 assert len(list(asmcfg.get_bad_blocks())) == 1 ### Check "special" asmcfg entry_asmcfg = asmcfg.heads() -bad_block_lbl = (lbl for lbl in entry_asmcfg - if isinstance(asmcfg.loc_key_to_block(lbl), AsmBlockBad)).next() +bad_block_lbl = next((lbl for lbl in entry_asmcfg + if isinstance(asmcfg.loc_key_to_block(lbl), AsmBlockBad))) entry_asmcfg.remove(bad_block_lbl) -alone_block = (asmcfg.loc_key_to_block(lbl) for lbl in entry_asmcfg - if len(asmcfg.successors(lbl)) == 0).next() +alone_block = next((asmcfg.loc_key_to_block(lbl) for lbl in entry_asmcfg + if len(asmcfg.successors(lbl)) == 0)) entry_asmcfg.remove(alone_block.loc_key) assert alone_block.lines[-1].name == "RET" assert len(alone_block.lines) == 2 ### Check resulting function entry_block = asmcfg.loc_key_to_block(entry_asmcfg.pop()) assert len(entry_block.lines) == 4 -assert map(str, entry_block.lines) == ['XOR EAX, EAX', +assert list(map(str, entry_block.lines)) == ['XOR EAX, EAX', 'XOR EBX, EBX', 'XOR ECX, ECX', 'JNZ loc_key_3'] assert len(asmcfg.successors(entry_block.loc_key)) == 2 assert len(entry_block.bto) == 2 -nextb = asmcfg.loc_key_to_block((cons.loc_key for cons in entry_block.bto - if cons.c_t == AsmConstraint.c_next).next()) -tob = asmcfg.loc_key_to_block((cons.loc_key for cons in entry_block.bto - if cons.c_t == AsmConstraint.c_to).next()) +nextb = asmcfg.loc_key_to_block(next((cons.loc_key for cons in entry_block.bto + if cons.c_t == AsmConstraint.c_next))) +tob = asmcfg.loc_key_to_block(next((cons.loc_key for cons in entry_block.bto + if cons.c_t == AsmConstraint.c_to))) assert len(nextb.lines) == 4 -assert map(str, nextb.lines) == ['XOR EDX, EDX', +assert list(map(str, nextb.lines)) == ['XOR EDX, EDX', 'XOR ESI, ESI', 'XOR EDI, EDI', 'JMP loc_key_4'] assert asmcfg.successors(nextb.loc_key) == [nextb.loc_key] assert len(tob.lines) == 2 -assert map(str, tob.lines) == ['XOR EBP, EBP', +assert list(map(str, tob.lines)) == ['XOR EBP, EBP', 'JMP loc_key_3'] assert asmcfg.successors(tob.loc_key) == [tob.loc_key] @@ -283,13 +288,13 @@ asmcfg.apply_splitting(mdis.loc_db) assert len(asmcfg) == 6 assert len(asmcfg.pendings) == 0 assert len(entry_block.lines) == 2 -assert map(str, entry_block.lines) == ['XOR EAX, EAX', +assert list(map(str, entry_block.lines)) == ['XOR EAX, EAX', 'XOR EBX, EBX'] assert len(asmcfg.successors(entry_block.loc_key)) == 1 lbl_newb = asmcfg.successors(entry_block.loc_key)[0] newb = asmcfg.loc_key_to_block(lbl_newb) assert len(newb.lines) == 2 -assert map(str, newb.lines) == ['XOR ECX, ECX', +assert list(map(str, newb.lines)) == ['XOR ECX, ECX', 'JNZ loc_key_3'] preds = asmcfg.predecessors(lbl_newb) assert len(preds) == 2 @@ -300,7 +305,7 @@ assert asmcfg.edges2constraint[(tob.loc_key, lbl_newb)] == AsmConstraint.c_to # Check double block split -data = "74097405b8020000007405b803000000b804000000c3".decode('hex') +data = decode_hex("74097405b8020000007405b803000000b804000000c3") cont = Container.from_string(data) mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) asmcfg = mdis.dis_multiblock(0) @@ -322,7 +327,7 @@ matcher += bbl0 >> bblB solutions = list(matcher.match(asmcfg)) assert len(solutions) == 1 solution = solutions.pop() -for jbbl, label in solution.iteritems(): +for jbbl, label in viewitems(solution): offset = mdis.loc_db.get_location_offset(label) assert offset == int(jbbl._name, 16) diff --git a/test/core/graph.py b/test/core/graph.py index b71c3d51..484591b7 100644 --- a/test/core/graph.py +++ b/test/core/graph.py @@ -1,3 +1,4 @@ +from __future__ import print_function from miasm2.core.graph import * g = DiGraph() @@ -9,13 +10,13 @@ g.add_edge('a', 'c') g.add_edge('a', 'c') g.add_edge('c', 'c') -print g +print(g) -print [x for x in g.successors('a')] -print [x for x in g.predecessors('a')] -print [x for x in g.predecessors('b')] -print [x for x in g.predecessors('c')] -print [x for x in g.successors('c')] +print([x for x in g.successors('a')]) +print([x for x in g.predecessors('a')]) +print([x for x in g.predecessors('b')]) +print([x for x in g.predecessors('c')]) +print([x for x in g.successors('c')]) """ @@ -226,7 +227,7 @@ j2 = MatchGraphJoker(name="son") ### Check '>>' helper matcher = j1 >> j2 >> j1 ### Check __str__ -print matcher +print(matcher) ### Ensure form assert isinstance(matcher, MatchGraph) assert len(matcher.nodes()) == 2 diff --git a/test/core/interval.py b/test/core/interval.py index 97d45a39..76c95d66 100755 --- a/test/core/interval.py +++ b/test/core/interval.py @@ -1,6 +1,7 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- +from builtins import range from miasm2.core.interval import * from random import randint from pdb import pm @@ -107,7 +108,7 @@ assert(i_empty.hull() == (None, None)) def gen_random_interval(l=100): r = [] - for j in xrange(5): + for j in range(5): a = randint(0, l) b = a + randint(0, l) r.append((a, b)) @@ -117,7 +118,7 @@ def gen_random_interval(l=100): def check_add(r1, r2): i_sum = interval(r1) + interval(r2) for a, b in r1 + r2: - for i in xrange(a, b + 1): + for i in range(a, b + 1): assert(i in i_sum) @@ -126,7 +127,7 @@ def check_sub(r1, r2): i2 = interval(r2) i_sub = i1 - i2 for a, b in r1: - for i in xrange(a, b + 1): + for i in range(a, b + 1): if i in i2: assert(i not in i_sub) else: @@ -138,14 +139,14 @@ def check_and(r1, r2): i2 = interval(r2) i_and = i1 & i2 for a, b in r1: - for i in xrange(a, b + 1): + for i in range(a, b + 1): if i in i2: assert(i in i_and) else: assert(i not in i_and) -for i in xrange(1000): +for i in range(1000): r1 = gen_random_interval() r2 = gen_random_interval() r3 = gen_random_interval() diff --git a/test/core/locationdb.py b/test/core/locationdb.py index 61bc4563..3db760d8 100644 --- a/test/core/locationdb.py +++ b/test/core/locationdb.py @@ -1,3 +1,4 @@ +from builtins import str from miasm2.core.locationdb import LocationDB @@ -57,9 +58,9 @@ loc_db.consistency_check() # Names manipulation loc_key5 = loc_db.add_location() -name1 = "name1" -name2 = "name2" -name3 = "name3" +name1 = b"name1" +name2 = b"name2" +name3 = b"name3" assert len(loc_db.get_location_names(loc_key5)) == 0 loc_db.add_location_name(loc_key5, name1) loc_db.add_location_name(loc_key5, name2) diff --git a/test/core/parse_asm.py b/test/core/parse_asm.py index ddb195d2..ade9040d 100755 --- a/test/core/parse_asm.py +++ b/test/core/parse_asm.py @@ -1,6 +1,7 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- +from builtins import range import unittest @@ -69,7 +70,7 @@ class TestParseAsm(unittest.TestCase): asmcfg, loc_db) lbls = [] - for i in xrange(6): + for i in range(6): lbls.append(loc_db.get_name_location('lbl%d' % i)) # align test offset = loc_db.get_location_offset(lbls[5]) @@ -97,7 +98,7 @@ class TestParseAsm(unittest.TestCase): asmcfg, loc_db = parse_txt(mn_x86, 32, ASM0) lbls = [] - for i in xrange(2): + for i in range(2): lbls.append(loc_db.get_name_location('lbl%d' % i)) lbl2block = {} for block in asmcfg.blocks: diff --git a/test/core/sembuilder.py b/test/core/sembuilder.py index f7a96b89..53e9e60e 100644 --- a/test/core/sembuilder.py +++ b/test/core/sembuilder.py @@ -1,3 +1,4 @@ +from __future__ import print_function import inspect from pdb import pm @@ -50,18 +51,18 @@ ir = IR(loc_db) instr = Instr() res = test(ir, instr, a, b, c) -print "[+] Returned:" -print res -print "[+] DocString:", test.__doc__ +print("[+] Returned:") +print(res) +print("[+] DocString:", test.__doc__) -print "[+] Cur instr:" +print("[+] Cur instr:") for statement in res[0]: - print statement + print(statement) -print "[+] Blocks:" +print("[+] Blocks:") for irb in res[1]: - print irb.loc_key + print(irb.loc_key) for assignblk in irb: for expr in assignblk: - print expr - print + print(expr) + print() diff --git a/test/core/test_types.py b/test/core/test_types.py index 92867748..e3914185 100755 --- a/test/core/test_types.py +++ b/test/core/test_types.py @@ -2,8 +2,11 @@ # miasm2.core.types tests +from __future__ import print_function +from builtins import range import struct +from miasm2.core.utils import int_to_byte from miasm2.analysis.machine import Machine from miasm2.core.types import MemStruct, Num, Ptr, Str, \ Array, RawStruct, Union, \ @@ -40,7 +43,7 @@ addr_str = 0x1100 addr_str2 = 0x1200 addr_str3 = 0x1300 # Initialize all mem with 0xaa -jitter.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, "\xaa"*size) +jitter.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, b"\xaa"*size) # MemStruct tests @@ -64,7 +67,7 @@ assert mstruct.flags == 0 assert mstruct.other.val == 0 assert mstruct.s.val == 0 assert mstruct.i.val == 0 -mstruct.memset('\x11') +mstruct.memset(b'\x11') assert mstruct.num == 0x11111111 assert mstruct.flags == 0x11 assert mstruct.other.val == 0x11111111 @@ -122,10 +125,10 @@ assert memval == 8 memstr = Str().lval(jitter.vm, addr_str) memstr.val = "" assert memstr.val == "" -assert jitter.vm.get_mem(memstr.get_addr(), 1) == '\x00' +assert jitter.vm.get_mem(memstr.get_addr(), 1) == b'\x00' memstr.val = "lala" -assert jitter.vm.get_mem(memstr.get_addr(), memstr.get_size()) == 'lala\x00' -jitter.vm.set_mem(memstr.get_addr(), 'MIAMs\x00') +assert jitter.vm.get_mem(memstr.get_addr(), memstr.get_size()) == b'lala\x00' +jitter.vm.set_mem(memstr.get_addr(), b'MIAMs\x00') assert memstr.val == 'MIAMs' ## Ptr(Str()) manipulations @@ -148,7 +151,7 @@ memstr3 = Str("utf16").lval(jitter.vm, addr_str3) memstr3.val = "That's all folks!" assert memstr3.get_addr() != memstr.get_addr() assert memstr3.get_size() != memstr.get_size() # Size is different -assert str(memstr3) != str(memstr) # Mem representation is different +assert bytes(memstr3) != bytes(memstr) # Mem representation is different assert memstr3 != memstr # Encoding is different, so they are not eq assert memstr3.val == memstr.val # But the python value is the same @@ -163,13 +166,13 @@ memarray = Array(Num("I")).lval(jitter.vm, alloc_addr) memarray[0] = 0x02 assert memarray[0] == 0x02 assert jitter.vm.get_mem(memarray.get_addr(), - Num("I").size) == '\x02\x00\x00\x00' + Num("I").size) == b'\x02\x00\x00\x00' memarray[2] = 0xbbbbbbbb assert memarray[2] == 0xbbbbbbbb assert jitter.vm.get_mem(memarray.get_addr() + 2 * Num("I").size, - Num("I").size) == '\xbb\xbb\xbb\xbb' + Num("I").size) == b'\xbb\xbb\xbb\xbb' try: - s = str(memarray) + s = bytes(memarray) assert False, "Should raise" except (NotImplementedError, ValueError): pass @@ -194,16 +197,16 @@ except ValueError: memsarray = Array(Num("I"), 10).lval(jitter.vm) # And Array(type, size).lval generates statically sized types assert memsarray.sizeof() == Num("I").size * 10 -memsarray.memset('\xcc') +memsarray.memset(b'\xcc') assert memsarray[0] == 0xcccccccc assert len(memsarray) == 10 * 4 -assert str(memsarray) == '\xcc' * (4 * 10) +assert bytes(memsarray) == b'\xcc' * (4 * 10) for val in memsarray: assert val == 0xcccccccc assert list(memsarray) == [0xcccccccc] * 10 memsarray[0] = 2 assert memsarray[0] == 2 -assert str(memsarray) == '\x02\x00\x00\x00' + '\xcc' * (4 * 9) +assert bytes(memsarray) == b'\x02\x00\x00\x00' + b'\xcc' * (4 * 9) # Atypical fields (RawStruct and Array) @@ -214,7 +217,7 @@ class MyStruct2(MemStruct): ] ms2 = MyStruct2(jitter.vm) -ms2.memset('\xaa') +ms2.memset(b'\xaa') assert len(ms2) == 15 ## RawStruct @@ -241,7 +244,7 @@ for val in ms2.s2: ### Field assignment (MemSizedArray) array2 = Array(Num("B"), 10).lval(jitter.vm) -jitter.vm.set_mem(array2.get_addr(), '\x02'*10) +jitter.vm.set_mem(array2.get_addr(), b'\x02'*10) for val in array2: assert val == 2 ms2.s2 = array2 @@ -272,7 +275,7 @@ assert cont.one == 0 assert cont.last == 0 assert cont.instruct.foo == 0 assert cont.instruct.bar == 0 -cont.memset('\x11') +cont.memset(b'\x11') assert cont.one == 0x11 assert cont.last == 0x11 assert cont.instruct.foo == 0x11 @@ -286,7 +289,7 @@ assert cont.one == 0x01 assert cont.instruct.foo == 0x02 assert cont.instruct.bar == 0x03 assert cont.last == 0x04 -assert jitter.vm.get_mem(cont.get_addr(), len(cont)) == '\x01\x02\x03\x04' +assert jitter.vm.get_mem(cont.get_addr(), len(cont)) == b'\x01\x02\x03\x04' # Union test @@ -301,7 +304,7 @@ class UniStruct(MemStruct): ] uni = UniStruct(jitter.vm) -jitter.vm.set_mem(uni.get_addr(), ''.join(chr(x) for x in xrange(len(uni)))) +jitter.vm.set_mem(uni.get_addr(), b''.join(int_to_byte(x) for x in range(len(uni)))) assert len(uni) == 6 # 1 + max(InStruct.sizeof(), 4) + 1 assert uni.one == 0x00 assert uni.union.instruct.foo == 0x01 @@ -535,18 +538,18 @@ for idx, off in ((0, 0), (1, 2), (30, 60)): # Repr tests -print "Some struct reprs:\n" -print repr(mstruct), '\n' -print repr(ms2), '\n' -print repr(cont), '\n' -print repr(uni), '\n' -print repr(bit), '\n' -print repr(ideas), '\n' -print repr(Array(MyStruct2.get_type(), 2).lval(jitter.vm, addr)), '\n' -print repr(Num("f").lval(jitter.vm, addr)), '\n' -print repr(memarray) -print repr(memsarray) -print repr(memstr) -print repr(memstr3) - -print "\nOk" # That's all folks! +print("Some struct reprs:\n") +print(repr(mstruct), '\n') +print(repr(ms2), '\n') +print(repr(cont), '\n') +print(repr(uni), '\n') +print(repr(bit), '\n') +print(repr(ideas), '\n') +print(repr(Array(MyStruct2.get_type(), 2).lval(jitter.vm, addr)), '\n') +print(repr(Num("f").lval(jitter.vm, addr)), '\n') +print(repr(memarray)) +print(repr(memsarray)) +print(repr(memstr)) +print(repr(memstr3)) + +print("\nOk") # That's all folks! diff --git a/test/core/utils.py b/test/core/utils.py index b506f904..6f69fdf1 100755 --- a/test/core/utils.py +++ b/test/core/utils.py @@ -2,6 +2,8 @@ #-*- coding:utf-8 -*- +from __future__ import print_function +from builtins import range import unittest @@ -12,7 +14,7 @@ class TestUtils(unittest.TestCase): # Use a callback def logger(key): - print "DELETE", key + print("DELETE", key) # Create a 5/2 dictionary bd = BoundedDict(5, 2, initialdata={"element": "value"}, @@ -26,9 +28,9 @@ class TestUtils(unittest.TestCase): # Increase 'element2' use _ = bd["element2"] - for i in xrange(6): + for i in range(6): bd[i] = i - print "Insert %d -> %s" % (i, bd) + print("Insert %d -> %s" % (i, bd)) assert(len(bd) == 2) |