Esempio n. 1
0
    def test_parse_when_zipfile_fails(self, mock_file_parser, mock_zipfile):
        mock_file_parser.is_zip_file.return_value = True
        mock_file_parser.return_value.parse.return_value = any_file()
        mock_zipfile.side_effect = BadZipFile()

        with self.assertRaises(BadZipFile):
            ApkParser().parse("any-file-path", extended_processing=False)
Esempio n. 2
0
    def test_parse_when_dex_parser_fails(self, mock_zipfile, mock_mkdtemp,
                                         mock_rmtree, mock_file_parser,
                                         mock_manifest_parser,
                                         mock_cert_parser, mock_dex_parser):
        tmp_directory = Mock()
        file = any_file(filename="any-apk-file")
        manifest = any_file(filename="any-manifest-file-name")
        cert = any_file(filename="any-cert-file-name")
        mock_zipfile.return_value.__enter__.return_value.namelist.return_value = [
            "any-manifest-file-name", "any-cert-file-name", "any-dex-file-name"
        ]
        mock_mkdtemp.return_value = tmp_directory
        mock_file_parser.is_zip_file.return_value = True
        mock_file_parser.return_value.parse.return_value = file
        mock_manifest_parser.looks_like_manifest.side_effect = [
            True, False, False
        ]
        mock_manifest_parser.return_value.parse.return_value = manifest
        mock_cert_parser.looks_like_cert.side_effect = [True, False]
        mock_cert_parser.return_value.parse.return_value = cert
        mock_dex_parser.looks_like_dex.side_effect = [True]
        mock_dex_parser.return_value.parse.side_effect = FileParsingError()

        with self.assertRaises(ApkParsingError):
            ApkParser().parse("any-file-path", extended_processing=True)
        mock_rmtree.assert_called_with(tmp_directory)
Esempio n. 3
0
    def test_parse_when_no_file_present(self, mock_file_parser, mock_zipfile,
                                        mock_mkdtemp, mock_rmtree):
        tmp_directory = Mock()
        mock_file_parser.is_zip_file.return_value = True
        mock_file_parser.return_value.parse.return_value = any_file()
        mock_zipfile.return_value.__enter__.return_value.namelist.return_value = []
        mock_mkdtemp.return_value = tmp_directory

        with self.assertRaises(ApkParsingError):
            ApkParser().parse("any-file-path", extended_processing=False)
        mock_rmtree.assert_called_with(tmp_directory)
Esempio n. 4
0
def read_file(filepath: str, extended_processing: bool) -> Optional[APK]:
    apk = None
    logger.debug("Reading %s...", filepath)
    try:
        apk = ApkParser(logger).parse(filepath, extended_processing)
    except ApkParsingError:
        logger.error("The target file ('%s') must be an APK package!",
                     filepath)
    except FileParsingError:
        logger.error(
            "The target file ('%s') must be an existing, readable file!",
            filepath)
    return apk
Esempio n. 5
0
    def test_parse_with_extended_processing(self, mock_zipfile, mock_mkdtemp,
                                            mock_rmtree, mock_file_parser,
                                            mock_manifest_parser,
                                            mock_cert_parser, mock_dex_parser,
                                            mock_aapt):
        tmp_directory = Mock()
        file = any_file(filename="any-apk-file")
        manifest = any_file(filename="any-manifest-file-name")
        cert = any_file(filename="any-cert-file-name")
        dex = any_file(filename="any-dex-file-name")
        resource = any_file(filename="any-resource-file")
        mock_zipfile.return_value.__enter__.return_value.namelist.return_value = [
            "any-manifest-file-name", "any-cert-file-name",
            "any-dex-file-name", "any-resource-file", "any-resource-directory"
        ]
        mock_mkdtemp.return_value = tmp_directory
        mock_file_parser.is_zip_file.return_value = True
        mock_file_parser.is_directory.side_effect = [False, True]
        mock_file_parser.return_value.parse.side_effect = [file, resource]
        mock_manifest_parser.looks_like_manifest.side_effect = [
            True, False, False, False, False
        ]
        mock_manifest_parser.return_value.parse.return_value = manifest
        mock_cert_parser.looks_like_cert.side_effect = [
            True, False, False, False
        ]
        mock_cert_parser.return_value.parse.return_value = cert
        mock_dex_parser.looks_like_dex.side_effect = [True, False, False]
        mock_dex_parser.return_value.parse.return_value = dex
        mock_aapt.get_app_name.return_value = "any-app-name"

        apk = ApkParser().parse("any-file-path", extended_processing=True)

        mock_mkdtemp.assert_called_with(".ninjadroid")
        mock_zipfile.assert_called_with("any-file-path")
        mock_rmtree.assert_called_with(tmp_directory)
        assert_file_equal(self, expected=file, actual=apk)
        self.assert_apk_equal(apk=apk,
                              app_name="any-app-name",
                              manifest=manifest,
                              cert=cert,
                              dex_files=[dex],
                              other_files=[resource])
Esempio n. 6
0
    def test_looks_like_apk(self, filename, expected, mock_file_parser):
        mock_file_parser.is_zip_file.return_value = expected

        result = ApkParser.looks_like_apk(filename)

        self.assertEqual(expected, result)