From 07a366a4d645329bb2137d9ec8b63c512b0d0019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Wed, 6 Jul 2022 02:05:58 +0200 Subject: [PATCH] add tests for scanner.load_exodus_trackers_signatures --- fdroidserver/scanner.py | 7 +++--- tests/scanner.TestCase | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/fdroidserver/scanner.py b/fdroidserver/scanner.py index b67e6df2..9aeb0f20 100644 --- a/fdroidserver/scanner.py +++ b/fdroidserver/scanner.py @@ -36,6 +36,7 @@ from . import _ from . import common from . import metadata from .exception import BuildException, VCSException +from . import scanner config = None options = None @@ -161,7 +162,7 @@ def _exodus_compile_signatures(signatures): # taken from exodus_core -def load_trackers_signatures(): +def load_exodus_trackers_signatures(): """ Load trackers signatures from the official Exodus database. @@ -178,7 +179,7 @@ def load_trackers_signatures(): ) ) logging.debug('{} trackers signatures loaded'.format(len(signatures))) - return signatures, _exodus_compile_signatures(signatures) + return signatures, scanner._exodus_compile_signatures(signatures) def scan_binary(apkfile, extract_signatures=None): @@ -544,7 +545,7 @@ def main(): exodus = [] if options.exodus: - exodus = load_trackers_signatures() + exodus = load_exodus_trackers_signatures() appids = [] for apk in options.appid: diff --git a/tests/scanner.TestCase b/tests/scanner.TestCase index 47cccccf..15a75edb 100755 --- a/tests/scanner.TestCase +++ b/tests/scanner.TestCase @@ -359,6 +359,53 @@ class Test__exodus_compile_signatures(unittest.TestCase): self.assertListEqual(result, []) +class Test_load_exodus_trackers_signatures(unittest.TestCase): + + def setUp(self): + self.requests_ret = mock.Mock() + self.requests_ret.json = mock.Mock(return_value={ + "trackers": { + "1": { + "id": 1, + "name": "Steyer Puch 1", + "description": "blah blah blah", + "creation_date": "1956-01-01", + "code_signature": "com.puch.|com.steyer.", + "network_signature": "pst\\.com", + "website": "https://pst.com", + "categories": ["tracker"], + "documentation": [], + }, + "2": { + "id": 2, + "name": "Steyer Puch 2", + "description": "blah blah blah", + "creation_date": "1956-01-01", + "code_signature": "com.puch.|com.steyer.", + "network_signature": "pst\\.com", + "website": "https://pst.com", + "categories": ["tracker"], + "documentation": [], + } + }, + }) + self.requests_func = mock.Mock(return_value=self.requests_ret) + self.compilesig_func = mock.Mock(return_value=9999) + + def test_ok(self): + with mock.patch("requests.get", self.requests_func), mock.patch( + "fdroidserver.scanner._exodus_compile_signatures", self.compilesig_func + ): + result_sigs, result_regex = fdroidserver.scanner.load_exodus_trackers_signatures() + self.requests_func.assert_called_once_with("https://reports.exodus-privacy.eu.org/api/trackers") + self.assertEqual(len(result_sigs), 2) + self.assertEqual(result_sigs[0].id, 1) + self.assertEqual(result_sigs[1].id, 2) + + self.compilesig_func.assert_called_once_with(result_sigs) + self.assertEqual(result_regex, 9999) + + if __name__ == "__main__": os.chdir(os.path.dirname(__file__)) @@ -376,5 +423,6 @@ if __name__ == "__main__": newSuite.addTests([ unittest.makeSuite(ScannerTest), unittest.makeSuite(Test__exodus_compile_signatures), + unittest.makeSuite(Test_load_exodus_trackers_signatures), ]) unittest.main(failfast=False)