Skip to content

Commit

Permalink
Remove spaces from nsmap + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Praseetha-KR committed Apr 12, 2022
1 parent 447fd44 commit 43ad5ca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyaxmlparser/axmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def nsmap(self):
# Solve 2) & 4) by not including
if s_uri != "" and s_prefix != "":
# solve 1) by using the last one in the list
NSMAP[s_prefix] = s_uri
NSMAP[s_prefix] = s_uri.strip()

return NSMAP

Expand Down
Binary file not shown.
55 changes: 55 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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

Expand All @@ -25,3 +29,54 @@ def test_app_name_extraction():
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))

0 comments on commit 43ad5ca

Please sign in to comment.