-
Notifications
You must be signed in to change notification settings - Fork 67
/
test.py
96 lines (75 loc) · 3.63 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from hbctool import hbc as hbcl, hasm
from .translator import assemble, disassemble
import unittest
import re
import pathlib
import json
basepath = pathlib.Path(__file__).parent.absolute()
class TestHBC74(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestHBC74, self).__init__(*args, **kwargs)
self.hbc = hbcl.load(open(f"{basepath}/example/index.android.bundle", "rb"))
self.objdump = open(f"{basepath}/example/objdump.out", "r").read()
self.pretty = open(f"{basepath}/example/pretty.out", "r").read()
self.raw = open(f"{basepath}/example/raw.out", "r").read()
def test_get_function(self):
target_offsets = re.findall(r"([0-9a-f]+) \<_[0-9]+\>", self.objdump)
target_args = re.findall(r"Function<(.*?)>([0-9]+)\(([0-9]+) params, ([0-9]+) registers,\s?([0-9]+) symbols\):", self.pretty)
functionCount = self.hbc.getFunctionCount()
self.assertEqual(functionCount, len(target_offsets))
self.assertEqual(functionCount, len(target_args))
for i in range(functionCount):
target_offset = target_offsets[i]
target_functionName, _, target_paramCount, target_registerCount, target_symbolCount = target_args[i]
try:
functionName, paramCount, registerCount, symbolCount, _, funcHeader = self.hbc.getFunction(i)
except AssertionError:
self.fail()
self.assertEqual(functionName, target_functionName)
self.assertEqual(paramCount, int(target_paramCount))
self.assertEqual(registerCount, int(target_registerCount))
self.assertEqual(symbolCount, int(target_symbolCount))
self.assertEqual(funcHeader["offset"], int(target_offset, 16))
def test_get_string(self):
target_strings = re.findall(r"[is][0-9]+\[([UTFASCI16-]+), ([0-9]+)..([0-9-]+)\].*?:\s?(.*)", self.pretty)
stringCount = self.hbc.getStringCount()
self.assertEqual(stringCount, len(target_strings))
for i in range(stringCount):
val, header = self.hbc.getString(i)
isUTF16, offset, length = header
t, target_start, target_end, target_val = target_strings[i]
target_isUTF16 = t == "UTF-16"
target_offset = int(target_start)
target_length = int(target_end) - target_offset + 1
self.assertEqual(isUTF16, target_isUTF16)
self.assertEqual(offset, target_offset)
self.assertEqual(length, target_length)
# TODO : Implement this please
# self.assertEqual(val, target_val)
def test_translator(self):
functionCount = self.hbc.getFunctionCount()
for i in range(functionCount):
_, _, _, _, bc, _ = self.hbc.getFunction(i, disasm=False)
self.assertEqual(assemble(disassemble(bc)), bc)
class TestParser74(unittest.TestCase):
def test_hbc(self):
f = open(f"{basepath}/example/index.android.bundle", "rb")
hbc = hbcl.load(f)
f.close()
f = open("/tmp/hbctool_test.android.bundle", "wb")
hbcl.dump(hbc, f)
f.close()
f = open("hbc/hbc74/example/index.android.bundle", "rb")
a = f.read()
f.close()
f = open("/tmp/hbctool_test.android.bundle", "rb")
b = f.read()
f.close()
self.assertEqual(a, b)
def test_hasm(self):
f = open(f"{basepath}/example/index.android.bundle", "rb")
a = hbcl.load(f)
f.close()
hasm.dump(a, "/tmp/hbctool_test", force=True)
b = hasm.load("/tmp/hbctool_test")
self.assertEqual(json.dumps(a.getObj()), json.dumps(b.getObj()))