#! /usr/bin/env vpython3 # Copyright 2016 The Chromium Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import unittest from xml.etree import ElementTree from pylib.utils import dexdump # pylint: disable=protected-access emptyAnnotations = dexdump.Annotations(classAnnotations={}, methodsAnnotations={}) class DexdumpXMLParseTest(unittest.TestCase): def testParseAnnotations(self): example_xml_string = ( '\n' 'Class #1 annotations:\n' 'Annotations on class\n' ' VISIBILITY_RUNTIME Ldalvik/annotation/AppModeFull; value=Alpha\n' 'Annotations on method #512 \'example\'\n' ' VISIBILITY_SYSTEM Ldalvik/annotation/Signature; value=Bravo\n' ' VISIBILITY_RUNTIME Ldalvik/annotation/Test;\n' ' VISIBILITY_RUNTIME Ldalvik/annotation/Test2; value=Charlie\n' ' VISIBILITY_RUNTIME Ldalvik/annotation/Test3; A=B x B={ C D }\n' ' VISIBILITY_RUNTIME Ldalvik/annotation/Test4; A=B x B={ C D } C=D\n' '\n' '\n' '\n' '\n' '\n') actual = dexdump._ParseAnnotations(example_xml_string) expected = { 1: dexdump.Annotations( classAnnotations={'AppModeFull': { 'value': 'Alpha' }}, methodsAnnotations={ 'example': { 'Test': None, 'Test2': { 'value': 'Charlie' }, 'Test3': { 'A': 'B x', 'B': ['C', 'D'] }, 'Test4': { 'A': 'B x', 'B': ['C', 'D'], 'C': 'D' }, } }, ) } self.assertEqual(expected, actual) def testParseRootXmlNode(self): example_xml_string = ('' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '') actual = dexdump._ParseRootNode(ElementTree.fromstring(example_xml_string), {}) expected = { 'com.foo.bar1': { 'classes': { 'Class1': { 'methods': ['class1Method1', 'class1Method2'], 'superclass': 'java.lang.Object', 'is_abstract': False, 'annotations': emptyAnnotations, }, 'Class2': { 'methods': ['class2Method1'], 'superclass': 'java.lang.Object', 'is_abstract': True, 'annotations': emptyAnnotations, } }, }, 'com.foo.bar2': { 'classes': {} }, 'com.foo.bar3': { 'classes': {} }, } self.assertEqual(expected, actual) def testParsePackageNode(self): example_xml_string = ( '' '' '' '' '' '') (actual, classCount) = dexdump._ParsePackageNode( ElementTree.fromstring(example_xml_string), 0, {}) expected = { 'classes': { 'Class1': { 'methods': [], 'superclass': 'java.lang.Object', 'is_abstract': False, 'annotations': emptyAnnotations, }, 'Class2': { 'methods': [], 'superclass': 'java.lang.Object', 'is_abstract': True, 'annotations': emptyAnnotations, }, }, } self.assertEqual(expected, actual) self.assertEqual(classCount, 2) def testParseClassNode(self): example_xml_string = ('' '' '' '' '' '' '' '') actual = dexdump._ParseClassNode(ElementTree.fromstring(example_xml_string), 0, {}) expected = { 'methods': ['method1', 'method2'], 'superclass': 'java.lang.Object', 'is_abstract': False, 'annotations': emptyAnnotations, } self.assertEqual(expected, actual) if __name__ == '__main__': unittest.main()