File: test_dexcodeparsing.py

package info (click to toggle)
androguard 4.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,080 kB
  • sloc: python: 25,059; xml: 3,483; javascript: 3,097; java: 1,311; sh: 130; makefile: 3
file content (91 lines) | stat: -rw-r--r-- 2,674 bytes parent folder | download | duplicates (2)
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
import os
import unittest
from binascii import hexlify
from difflib import Differ

import parse_dex

from androguard.core import dex

test_dir = os.path.dirname(os.path.abspath(__file__))


class TestDexCodeParsing(unittest.TestCase):

    def testcode(self):
        skipped_methods = []

        fname = os.path.join(test_dir, 'data/APK/classes.dex')

        parsed = parse_dex.read_dex(fname)

        with open(fname, "rb") as f:
            d = dex.DEX(f.read())

        dif = Differ()

        for m in d.get_encoded_methods():
            if not m.get_code():
                continue

            if m.get_method_idx() in skipped_methods:
                continue

            code = hexlify(m.get_code().get_raw())
            self.assertEqual(
                parsed.methods[m.get_method_idx()],
                code,
                "incorrect code for "
                "[{}]: {} --> {}:\n"
                "{}\ntries_size: {}, insns_size: {}\nSHOULD BE {}\n{}\n{}".format(
                    m.get_method_idx(),
                    m.get_class_name(),
                    m.get_name(),
                    "".join(
                        dif.compare(parsed.methods[m.get_method_idx()], code)
                    ),
                    m.get_code().tries_size,
                    m.get_code().insns_size,
                    hexlify(m.get_code().get_raw()),
                    parsed.methods[m.get_method_idx()],
                    hexlify(m.get_code().code.get_raw()),
                ),
            )

    def testClassManager(self):
        """Test if the classmanager has the same items"""

        from androguard.core.mutf8 import decode

        fname = os.path.join(test_dir, 'data/APK/classes.dex')

        parsed = parse_dex.read_dex(fname)

        with open(fname, "rb") as f:
            d = dex.DEX(f.read())

        cm = d.get_class_manager()

        self.assertFalse(cm.get_odex_format())

        ERR_STR = 'AG:IS: invalid string'

        ## Testing Strings...
        for idx in range(parsed.string_ids_size):
            self.assertNotEqual(cm.get_string(idx), ERR_STR)
            self.assertNotEqual(cm.get_raw_string(idx), ERR_STR)
            self.assertEqual(
                cm.get_raw_string(idx), decode(parsed.str_raw[idx])
            )

        self.assertEqual(cm.get_string(parsed.string_ids_size), ERR_STR)
        self.assertEqual(cm.get_raw_string(parsed.string_ids_size), ERR_STR)

        self.assertEqual(cm.get_string(parsed.string_ids_size + 100), ERR_STR)
        self.assertEqual(
            cm.get_raw_string(parsed.string_ids_size + 100), ERR_STR
        )


if __name__ == '__main__':
    unittest.main()