def test_parse_when_minidom_fails_with_apk_path_and_extended_processing( self, mock_file, mock_file_parser, mock_minidom, mock_aapt): file = any_file(filename="AndroidManifest.xml") mock_parser_instance = any_file_parser(file=file) mock_file_parser.return_value = mock_parser_instance mock_minidom.parse.side_effect = ExpatError() mock_aapt.get_apk_info.return_value = self.any_aapt_apk_info( package_name="any-package-name", version_code=1, version_name="any-version-name", sdk_max="20", sdk_min="10", sdk_target="15") mock_aapt.get_app_permissions.return_value = [ "any-permission-0", "any-permission-1", "any-permission-2" ] mock_aapt.get_manifest_info.return_value = self.any_aapt_manifest_info( activities=["any-activity-name"], services=["any-service-name"], receivers=["any-broadcast-receiver-name"]) manifest = self.sut.parse(filepath="any-file-path", binary=False, apk_path="any_apk_path", extended_processing=True) assert_file_parser_called_once_with(mock_parser_instance, filepath="any-file-path", filename="AndroidManifest.xml") mock_file.assert_called_with("any-file-path", "rb") mock_minidom.parse.assert_called_with("any-file-path") mock_aapt.get_apk_info.assert_called_with("any_apk_path") mock_aapt.get_app_permissions.assert_called_with("any_apk_path") mock_aapt.get_manifest_info.assert_called_with("any_apk_path") assert_file_equal(self, expected=file, actual=manifest) self.assert_manifest_equal( manifest=manifest, package_name="any-package-name", version=AppVersion(code=1, name="any-version-name"), sdk=AppSdk(min_version="10", target_version="15", max_version="20"), permissions=[ "any-permission-0", "any-permission-1", "any-permission-2" ], activities=[AppActivity(name="any-activity-name")], services=[AppService(name="any-service-name")], receivers=[ AppBroadcastReceiver(name="any-broadcast-receiver-name") ])
def test_manifest_as_dict_without_extended_processing(self): manifest = AndroidManifest( filename="any-file-name", size=10, md5hash="any-file-md5", sha1hash="any-file-sha1", sha256hash="any-file-sha256", sha512hash="any-file-sha512", package_name="any-package-name", version=AppVersion(code=1, name="any-version-name"), sdk=AppSdk(min_version="10", target_version="15", max_version="20"), permissions=["any-permission-1", "any-permission-2"], activities=[], services=[], receivers=[] ) result = manifest.as_dict() # NOTE: no "activities", "services" and "receivers" keys are returned self.assertEqual( { "file": "any-file-name", "size": 10, "md5": "any-file-md5", "sha1": "any-file-sha1", "sha256": "any-file-sha256", "sha512": "any-file-sha512", "package": "any-package-name", "version": { "code": 1, "name": "any-version-name" }, "sdk": { "min": "10", "target": "15", "max": "20" }, "permissions": [ "any-permission-1", "any-permission-2" ] }, result )
def test_parse_binary(self, mock_file, mock_file_parser, mock_axmlprinter, mock_minidom): file = any_file(filename="AndroidManifest.xml") mock_parser_instance = any_file_parser(file=file) mock_file_parser.return_value = mock_parser_instance mock_axmlprinter.return_value = self.any_axmlprinter() mock_minidom.parseString.return_value = self.any_axmlprinter_xml( package_name="any-package-name", version_code="1", version_name="any-version-name", sdk_max="20", sdk_min="10", sdk_target="15", permissions=[ "any-permission-1", "any-permission-2", "any-permission-0" ]) manifest = self.sut.parse(filepath="any-file-path", binary=True, apk_path=None, extended_processing=False) assert_file_parser_called_once_with(mock_parser_instance, filepath="any-file-path", filename="AndroidManifest.xml") mock_file.assert_called_with("any-file-path", "rb") mock_axmlprinter.assert_called_with(ANY) mock_minidom.parseString.assert_called_with("any-axml-raw-value") assert_file_equal(self, expected=file, actual=manifest) self.assert_manifest_equal(manifest=manifest, package_name="any-package-name", version=AppVersion(code=1, name="any-version-name"), sdk=AppSdk(min_version="10", target_version="15", max_version="20"), permissions=[ "any-permission-0", "any-permission-1", "any-permission-2" ], activities=[], services=[], receivers=[])
def test_apk_as_dict(self): apk = APK( filename="any-apk-file-name", size=10, md5hash="any-apk-file-md5", sha1hash="any-apk-file-sha1", sha256hash="any-apk-file-sha256", sha512hash="any-apk-file-sha512", app_name="any-app-name", cert=Cert( filename="any-cert-file-name", size=20, md5hash="any-cert-file-md5", sha1hash="any-cert-file-sha1", sha256hash="any-cert-file-sha256", sha512hash="any-cert-file-sha512", serial_number="any-cert-serial-number", validity=CertValidity( valid_from="any-cert-validity-from", valid_to="any-cert-validity-to" ), fingerprint=CertFingerprint( md5="any-cert-fingerprint-md5", sha1="any-cert-fingerprint-sha1", sha256="any-cert-fingerprint-sha256", signature="any-cert-fingerprint-signature", version="any-cert-fingerprint-version" ), owner=CertParticipant( name="any-cert-owner-name", email="any-cert-owner-email", unit="any-cert-owner-unit", organization="any-cert-owner-organization", city="any-cert-owner-city", state="any-cert-owner-state", country="any-cert-owner-country", domain="any-cert-owner-domain" ), issuer=CertParticipant( name="any-cert-issuer-name", email="any-cert-issuer-email", unit="any-cert-issuer-unit", organization="any-cert-issuer-organization", city="any-cert-issuer-city", state="any-cert-issuer-state", country="any-cert-issuer-country", domain="any-cert-issuer-domain" ) ), manifest=AndroidManifest( filename="any-manifest-file-name", size=30, md5hash="any-manifest-file-md5", sha1hash="any-manifest-file-sha1", sha256hash="any-manifest-file-sha256", sha512hash="any-manifest-file-sha512", package_name="any-package-name", version=AppVersion(code=1, name="any-version-name"), sdk=AppSdk(min_version="10", target_version="15", max_version="20"), permissions=[], activities=[], services=[], receivers=[] ), dex_files=[ Dex( filename="any-dex-file-name", size=40, md5hash="any-dex-file-md5", sha1hash="any-dex-file-sha1", sha256hash="any-dex-file-sha256", sha512hash="any-dex-file-sha512", strings=[], urls=[], shell_commands=[], custom_signatures=[] ) ], other_files=[ any_file( filename="any-resource-file-name", size=50, md5="any-resource-file-md5", sha1="any-resource-file-sha1", sha256="any-resource-file-sha256", sha512="any-resource-file-sha512" ) ] ) result = apk.as_dict() self.assertEqual( { "file": "any-apk-file-name", "size": 10, "md5": "any-apk-file-md5", "sha1": "any-apk-file-sha1", "sha256": "any-apk-file-sha256", "sha512": "any-apk-file-sha512", "name": "any-app-name", "cert": { "file": "any-cert-file-name", "size": 20, "md5": "any-cert-file-md5", "sha1": "any-cert-file-sha1", "sha256": "any-cert-file-sha256", "sha512": "any-cert-file-sha512", "serial_number": "any-cert-serial-number", "validity": { "from": "any-cert-validity-from", "until": "any-cert-validity-to" }, "fingerprint": { "md5": "any-cert-fingerprint-md5", "sha1": "any-cert-fingerprint-sha1", "sha256": "any-cert-fingerprint-sha256", "signature": "any-cert-fingerprint-signature", "version": "any-cert-fingerprint-version" }, "owner": { "name": "any-cert-owner-name", "email": "any-cert-owner-email", "unit": "any-cert-owner-unit", "organization": "any-cert-owner-organization", "city": "any-cert-owner-city", "state": "any-cert-owner-state", "country": "any-cert-owner-country", "domain": "any-cert-owner-domain" }, "issuer": { "name": "any-cert-issuer-name", "email": "any-cert-issuer-email", "unit": "any-cert-issuer-unit", "organization": "any-cert-issuer-organization", "city": "any-cert-issuer-city", "state": "any-cert-issuer-state", "country": "any-cert-issuer-country", "domain": "any-cert-issuer-domain" } }, "manifest": { "file": "any-manifest-file-name", "size": 30, "md5": "any-manifest-file-md5", "sha1": "any-manifest-file-sha1", "sha256": "any-manifest-file-sha256", "sha512": "any-manifest-file-sha512", "package": "any-package-name", "version": { "code": 1, "name": "any-version-name" }, "sdk": { "min": "10", "target": "15", "max": "20" }, "permissions": [] }, "dex": [ { "file": "any-dex-file-name", "size": 40, "md5": "any-dex-file-md5", "sha1": "any-dex-file-sha1", "sha256": "any-dex-file-sha256", "sha512": "any-dex-file-sha512", "strings": [], "urls": [], "shell_commands": [] } ], "other": [ { "file": "any-resource-file-name", "size": 50, "md5": "any-resource-file-md5", "sha1": "any-resource-file-sha1", "sha256": "any-resource-file-sha256", "sha512": "any-resource-file-sha512", } ] }, result )
def test_manifest_as_dict(self): manifest = AndroidManifest( filename="any-file-name", size=10, md5hash="any-file-md5", sha1hash="any-file-sha1", sha256hash="any-file-sha256", sha512hash="any-file-sha512", package_name="any-package-name", version=AppVersion(code=1, name="any-version-name"), sdk=AppSdk(min_version="10", target_version="15", max_version="20"), permissions=["any-permission-1", "any-permission-2"], activities=[ AppActivity( name="any-activity-name-1", metadata=[ { "name": "any-activity-metadata-name", "value": "any-activity-metadata-value" } ], intent_filters=[ { "priority": "100", "action": ["any-activity-intent-filter-action"], "category": ["any-activity-intent-filter-category"], "data": [ {"scheme": "any-activity-intent-filter-data-scheme"}, {"mimeType": "any-activity-intent-filter-data-mimetype"} ] } ], parent_name="any-activity-parent-name", launch_mode="1", no_history="true", ), AppActivity(name="any-activity-name-2") ], services=[ AppService( name="any-service-name-1", metadata=[ { "name": "any-service-metadata-name", "value": "any-service-metadata-value" } ], intent_filters=[ { "priority": "100", "action": ["any-service-intent-filter-action"], "category": ["any-service-intent-filter-category"], "data": [ {"scheme": "any-service-intent-filter-data-scheme"}, {"mimeType": "any-service-intent-filter-data-mimetype"} ] } ], enabled=False, exported=True, process="any-service-process", isolated_process=True ), AppService(name="any-service-name-2") ], receivers=[ AppBroadcastReceiver( name="any-broadcast-receiver-name-1", metadata=[ { "name": "any-broadcast-receiver-metadata-name", "value": "any-broadcast-receiver-metadata-value" } ], intent_filters=[ { "priority": "100", "action": ["any-broadcast-receiver-intent-filter-action"], "category": ["any-broadcast-receiver-intent-filter-category"], "data": [ {"scheme": "any-broadcast-receiver-intent-filter-data-scheme"}, {"mimeType": "any-broadcast-receiver-intent-filter-data-mimetype"} ] } ], enabled=True, exported=False ), AppBroadcastReceiver(name="any-broadcast-receiver-name-2") ] ) result = manifest.as_dict() self.assertEqual( { "file": "any-file-name", "size": 10, "md5": "any-file-md5", "sha1": "any-file-sha1", "sha256": "any-file-sha256", "sha512": "any-file-sha512", "package": "any-package-name", "version": { "code": 1, "name": "any-version-name" }, "sdk": { "min": "10", "target": "15", "max": "20" }, "permissions": [ "any-permission-1", "any-permission-2" ], "activities": [ { "name": "any-activity-name-1", "meta-data": [ { "name": "any-activity-metadata-name", "value": "any-activity-metadata-value" } ], "intent-filter": [ { "priority": "100", "action": ["any-activity-intent-filter-action"], "category": ["any-activity-intent-filter-category"], "data": [ { "scheme": "any-activity-intent-filter-data-scheme" }, { "mimeType": "any-activity-intent-filter-data-mimetype" } ] } ], "parentActivityName": "any-activity-parent-name", "launchMode": "1", "noHistory": "true" }, { "name": "any-activity-name-2" } ], "services": [ { "name": "any-service-name-1", "meta-data": [ { "name": "any-service-metadata-name", "value": "any-service-metadata-value" } ], "intent-filter": [ { "priority": "100", "action": ["any-service-intent-filter-action"], "category": ["any-service-intent-filter-category"], "data": [ { "scheme": "any-service-intent-filter-data-scheme" }, { "mimeType": "any-service-intent-filter-data-mimetype" } ] } ], "enabled": False, "exported": True, "process": "any-service-process", "isolatedProcess": True }, { "name": "any-service-name-2" } ], "receivers": [ { "name": "any-broadcast-receiver-name-1", "meta-data": [ { "name": "any-broadcast-receiver-metadata-name", "value": "any-broadcast-receiver-metadata-value" } ], "intent-filter": [ { "priority": "100", "action": ["any-broadcast-receiver-intent-filter-action"], "category": ["any-broadcast-receiver-intent-filter-category"], "data": [ { "scheme": "any-broadcast-receiver-intent-filter-data-scheme" }, { "mimeType": "any-broadcast-receiver-intent-filter-data-mimetype" } ] } ], "enabled": True, "exported": False }, { "name": "any-broadcast-receiver-name-2" } ] }, result )