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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#!/usr/bin/env python3
# This file is part of Androguard.
#
# Copyright (C) 2010, Anthony Desnos <desnos at t0t0.fr>
# All rights reserved.
#
# Androguard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Androguard. If not, see <http://www.gnu.org/licenses/>.
import sys
import unittest
from struct import pack, unpack, calcsize
from androguard.session import Session
TEST_CASE = 'examples/android/TestsAndroguard/bin/classes.dex'
VALUES = {
'Ltests/androguard/TestActivity; testDouble ()V': [
('<d', -5),
('<d', -4),
('<d', -3),
('<d', -2),
('<d', -1),
('<d', 0),
('<d', 1),
('<d', 2),
('<d', 3),
('<d', 4),
('<d', 5),
('<l', -5),
('<l', -4),
('<l', -3),
('<l', -2),
('<l', -1),
('<l', 0),
('<l', 1),
('<l', 2),
('<l', 3),
('<l', 4),
('<l', 5),
('<f', -5),
('<f', -4),
('<f', -3),
('<f', -2),
('<f', -1),
('<f', 0),
('<f', 1),
('<f', 2),
('<f', 3),
('<f', 4),
('<f', 5),
('<d', 65534),
('<d', 65535),
('<d', 65536),
('<d', 65537),
('<d', 32769),
('<d', 32768),
('<d', 32767),
('<d', 32766),
('<l', 65534),
('<l', 65535),
('<l', 65536),
('<l', 65537),
('<l', 32769),
('<l', 32768),
('<l', 32767),
('<l', 32766),
('<f', 65534),
('<f', 65535),
('<f', 65536),
('<f', 65537),
('<f', 32769),
('<f', 32768),
('<f', 32767),
('<f', 32766),
('<d', 5346952),
('<l', 5346952),
('<f', 5346952),
('<d', 65534.50),
('<d', 65535.50),
('<d', 65536.50),
('<d', 65537.50),
('<d', 32769.50),
('<d', 32768.50),
('<d', 32767.50),
('<d', 32766.50),
('<f', 65534.50),
('<f', 65535.50),
('<f', 65536.50),
('<f', 65537.50),
('<f', 32769.50),
('<f', 32768.50),
('<f', 32767.50),
('<f', 32766.50),
('<f', -5),
('<f', -65535),
('<f', -65536),
# ('<f', -123456789123456789.555555555), --> this value will be -123456790519087104 as float!
('<f', -123456790519087104),
('<d', -123456789123456789.555555555),
# int boom
('<i', -606384730),
# float reboom
('<f', -123456790519087104),
# not in the java file, but appears due to the calculation: boom again
# but the two is added already -> some compiler optimization
('<i', -606384730 + 2),
# the 3.5 from the calculation
('<f', 3.5),
],
}
def format_value(literal, ins, to):
# Need to convert the instruction (which is always signed)
# to the requested format.
formats = dict(i='i', h='i', l='q', s='h')
char = ins.__class__.__name__[-1]
if char not in formats:
raise ValueError("wrong type of instruction")
# Special treatment for const/high16 and const-wide/high16
if char == 'h' and 'wide' in ins.get_name():
formats['h'] = 'q'
# Need to calculate for extra padding bytes
# if the number is negative, trailing \xff are added (sign extension)
print(calcsize(to), calcsize(formats[char]))
packed = pack('<{}'.format(formats[char]), literal)
padding = bytearray()
trailing = bytearray()
print(packed)
if to == '<l' and packed[-1] & 0x80 == 0x80:
# Sign extension
trailing = bytearray([0xff] * (calcsize(to) - calcsize(formats[char])))
elif to == '<l' and packed[-1] & 0x80 == 0x00:
# no sign extension, but requires zero trailing
trailing = bytearray([0] * (calcsize(to) - calcsize(formats[char])))
else:
padding = bytearray([0] * (calcsize(to) - calcsize(formats[char])))
print(ins.__class__.__name__, char, formats[char], to, padding, trailing, literal)
return unpack(to, padding + packed + trailing)[0]
class TypesTest(unittest.TestCase):
def testTypes(self):
s = Session()
with open(TEST_CASE, "rb") as fd:
digest, d, dx = s.addDEX(TEST_CASE, fd.read())
for method in filter(lambda x: x.full_name in VALUES, d.get_methods()):
print("METHOD", method.full_name)
for i in filter(lambda x: 'const' in x.get_name(), method.get_instructions()):
i.show(0)
# ins should only have one literal
self.assertEqual(len(i.get_literals()), 1)
fmt, value = VALUES[method.full_name].pop(0)
converted = format_value(i.get_literals()[0], i, fmt)
print(i.get_literals(), fmt, value, converted)
self.assertEqual(converted, value)
print()
if __name__ == '__main__':
unittest.main()
|