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), ])