File: test_parser.py

package info (click to toggle)
pyaxmlparser 0.3.31-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,552 kB
  • sloc: python: 3,089; xml: 2,517; sh: 9; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,578 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
import os.path
import sys
import unittest

from lxml import etree

from pyaxmlparser.arscparser import ARSCParser
from pyaxmlparser.axmlparser import AXMLParser
from pyaxmlparser.axmlprinter import AXMLPrinter
from pyaxmlparser.utils import NS_ANDROID


PATH_INSTALL = "./"
sys.path.append(PATH_INSTALL)
test_apk = 'tests/test_apk/'


def test_app_name_extraction():
    axml_file = os.path.join(test_apk, 'AndroidManifest.xml')
    axml = AXMLPrinter(open(axml_file, 'rb').read()).get_xml_obj()
    app_name_hex = axml.findall(".//application")[0].get(NS_ANDROID + "label")
    appnamehex = '0x' + app_name_hex[1:]

    rsc_file = os.path.join(test_apk, 'resources.arsc')
    rsc = ARSCParser(open(rsc_file, 'rb').read())

    app_name = rsc.get_string(
        rsc.get_packages_names()[0],
        rsc.get_id(rsc.get_packages_names()[0], int(appnamehex, 0))[1]
    )
    assert app_name == ['app_name', 'Evie']


class AXMLParserTest(unittest.TestCase):
    def test_should_remove_spaces_from_namespace_uri(self):
        manifest_path = os.path.join(
            test_apk,
            "AndroidManifest_invalid_namespace.xml"
        )
        fd = open(manifest_path, "rb")
        manifest_content = fd.read()
        fd.close()

        axml = AXMLParser(raw_buff=manifest_content)
        next(axml)

        namespaces = {}
        for k,v in axml.namespaces:
            namespaces[axml.sb[k]] = axml.sb[v]

        self.assertTrue(axml.is_valid())

        # spaces exist in namespaces, but removed in nsmap
        self.assertEqual(
            namespaces["android"],
            "http://schemas.android.com/apk/res/android"
        )
        self.assertEqual(
            namespaces["app"],
            " http://schemas.android.com/apk/res-auto "
        )
        self.assertEqual(
            namespaces["dist"],
            " http://schemas.android.com/apk/distribution"
        )
        self.assertEqual(
            namespaces["tools"],
            "http://schemas.android.com/tools "
        )
        self.assertDictEqual(axml.nsmap, {
            "android": "http://schemas.android.com/apk/res/android",
            "app": "http://schemas.android.com/apk/res-auto",
            "dist": "http://schemas.android.com/apk/distribution",
            "tools": "http://schemas.android.com/tools"
        })

        # nsmap should be parsable by etree
        try:
            etree.Element("manifest", nsmap=axml.nsmap)
        except ValueError as e:
            # Eg: Invalid namespace URI "http://schemas.android.com/tools "
            raise self.fail(str(e))