From 1c2b0844103bbfd76133aac00eaf62022b2735aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Sun, 10 Jul 2022 13:35:26 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20improve=20scanner.scan=5Fapk=20t?= =?UTF-8?q?ests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor test function it a TestCase and split into separate test cases. Fix and improve tests for scanning apks with embedded apks. --- tests/scanner.TestCase | 77 +++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/tests/scanner.TestCase b/tests/scanner.TestCase index 15a75edb..0c554be6 100755 --- a/tests/scanner.TestCase +++ b/tests/scanner.TestCase @@ -204,34 +204,6 @@ class ScannerTest(unittest.TestCase): self.assertTrue(f in files['infos'], f + ' should be removed with an info message') - def test_scan_binary(self): - config = dict() - fdroidserver.common.fill_config_defaults(config) - fdroidserver.common.config = config - fdroidserver.common.options = mock.Mock() - fdroidserver.common.options.verbose = False - - apkfile = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk') - self.assertEqual( - 0, - fdroidserver.scanner.scan_binary(apkfile), - 'Found false positives in binary', - ) - fdroidserver.scanner.CODE_SIGNATURES["java/lang/Object"] = re.compile( - r'.*java/lang/Object', re.IGNORECASE | re.UNICODE - ) - self.assertEqual( - 1, - fdroidserver.scanner.scan_binary(apkfile), - 'Did not find bad code signature in binary', - ) - apkfile = os.path.join(self.basedir, 'apk.embedded_1.apk') - self.assertEqual( - 1, - fdroidserver.scanner.scan_binary(apkfile), - 'Did not find bad code signature in binary', - ) - def test_build_local_scanner(self): """`fdroid build` calls scanner functions, test them here""" testdir = tempfile.mkdtemp( @@ -338,6 +310,54 @@ class ScannerTest(unittest.TestCase): self.assertEqual(0, count, 'there should be this many errors') +class Test_scan_binary(unittest.TestCase): + + def setUp(self): + self.basedir = os.path.join(localmodule, 'tests') + config = dict() + fdroidserver.common.fill_config_defaults(config) + fdroidserver.common.config = config + fdroidserver.common.options = mock.Mock() + + def test_code_signature_match(self): + apkfile = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk') + with mock.patch("fdroidserver.scanner.CODE_SIGNATURES", {"java/lang/Object": re.compile( + r'.*java/lang/Object', re.IGNORECASE | re.UNICODE + )}): + self.assertEqual( + 1, + fdroidserver.scanner.scan_binary(apkfile), + 'Did not find bad code signature in binary', + ) + + def test_embedded_apk_code_signature(self): + apkfile = os.path.join(self.basedir, 'apk.embedded_1.apk') + with mock.patch("fdroidserver.scanner.CODE_SIGNATURES", {"org/bitbucket/tickytacky/mirrormirror/MainActivity": re.compile( + r'.*org/bitbucket/tickytacky/mirrormirror/MainActivity', re.IGNORECASE | re.UNICODE + )}): + self.assertEqual( + 1, + fdroidserver.scanner.scan_binary(apkfile), + 'Did not find bad code signature in binary', + ) + + def test_top_level_signature_embedded_apk_present(self): + apkfile = os.path.join(self.basedir, 'apk.embedded_1.apk') + with mock.patch("fdroidserver.scanner.CODE_SIGNATURES", {"org/fdroid/ci/BuildConfig": re.compile( + r'.*org/fdroid/ci/BuildConfig', re.IGNORECASE | re.UNICODE + )}): + self.assertEqual( + 1, + fdroidserver.scanner.scan_binary(apkfile), + 'Did not find bad code signature in binary', + ) + + def test_ok(self): + apkfile = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk') + result = fdroidserver.scanner.scan_binary(apkfile) + self.assertEqual(0, result, 'Found false positives in binary') + + class Test__exodus_compile_signatures(unittest.TestCase): def setUp(self): @@ -422,6 +442,7 @@ if __name__ == "__main__": newSuite = unittest.TestSuite() newSuite.addTests([ unittest.makeSuite(ScannerTest), + unittest.makeSuite(Test_scan_binary), unittest.makeSuite(Test__exodus_compile_signatures), unittest.makeSuite(Test_load_exodus_trackers_signatures), ])