diff --git a/tools/python_tests/permissions_test.py b/tools/python_tests/permissions_test.py index cabd53a923..ee4819a718 100755 --- a/tools/python_tests/permissions_test.py +++ b/tools/python_tests/permissions_test.py @@ -1,10 +1,11 @@ #!/usr/bin/env python +import unittest + import logging import subprocess -import time -from argparse import ArgumentParser -from os.path import abspath, join +from os.path import abspath, join, dirname +from os import listdir EXPECTED_PERMISSIONS = { "uses-permission: android.permission.WRITE_EXTERNAL_STORAGE", @@ -26,24 +27,8 @@ EXPECTED_PERMISSIONS = { } -def get_args(): - parser = ArgumentParser( - description="A test for checking that no new permissions have been added to the APK by a third library" - ) - - parser.add_argument( - "-a", "--aapt-path", - help="Path to the aapt executable.", - dest="aapt", default=None - ) - - parser.add_argument( - "-i", "--input-apk", - help="Path to the apk to test.", - dest="apk", default=None - ) - - return parser.parse_args() +def new_lines(iterable): + return "\n".join(iterable) def exec_shell(executable, flags): @@ -60,66 +45,69 @@ def exec_shell(executable, flags): return filter(None, out.splitlines()) -def new_lines(iterable): - return "\n".join(iterable) +class TestPermissions(unittest.TestCase): + def setUp(self): + self.omim_path = self.find_omim_path() + self.aapt = self.find_aapt() + self.apk = self.find_apks() -def find_aapt(args): - my_path = abspath(__file__) - omim_path = my_path[:my_path.index("omim") + len("omim")] - local_props_path = join(omim_path, "android", "local.properties") - logging.info("local props: {}".format(local_props_path)) - with open(local_props_path) as props: - for line in filter(lambda s: s != "" and not s.startswith("#"), map(lambda s: s.strip(), props)): - if line.startswith("sdk.dir"): - path = line.split("=")[1].strip() - args.aapt = join(path, "platforms", "android-6", "tools", "aapt") + def contains_correct_files(self, my_path): + dir_contents = listdir(my_path) + return ( + "strings.txt" in dir_contents or + "android" in dir_contents or + "drape_frontend" in dir_contents + ) -def check_permissions(permissions): - if permissions != EXPECTED_PERMISSIONS: - logging.info("\nExpected:\n{0}".format(new_lines(EXPECTED_PERMISSIONS))) - logging.info("\nActual: \n{0}".format(new_lines(permissions))) - logging.info( - "\nAdded:\n{0}".format( - new_lines(permissions - EXPECTED_PERMISSIONS) + def find_omim_path(self): + my_path = abspath(join(dirname(__file__), "..", "..")) + if self.contains_correct_files(my_path): + return my_path + raise IOError("Couldn't find a candidate for the project root path") + + + def find_apks(self): + apk_path = join(self.omim_path, "android", "build", "outputs", "apk") + apk = filter( + lambda s: "universal" in s and "unaligned" not in s, + listdir(apk_path) + )[0] + return join(apk_path, apk) + + + def find_aapt(self): + local_props_path = join(self.omim_path, "android", "local.properties") + logging.info("local props: {}".format(local_props_path)) + with open(local_props_path) as props: + for line in filter(lambda s: s != "" and not s.startswith("#"), map(lambda s: s.strip(), props)): + if line.startswith("sdk.dir"): + path = line.split("=")[1].strip() + return join(path, "platforms", "android-6", "tools", "aapt") + return None + + + def test_permissions(self): + """Check whether we have added or removed any permissions""" + permissions = exec_shell(self.aapt, "dump permissions {0}".format(self.apk)) + permissions = set( + filter( + lambda s: s.startswith("permission:") or s.startswith("uses-permission:"), + permissions ) ) - logging.info( - "\nRemoved:\n{0}".format( - new_lines(EXPECTED_PERMISSIONS - permissions) - ) - ) - logging.info("FAILED") - return False - logging.info("OK") - return True + description = "Expected: {}\n\nActual: {}\n\n:Added: {}\n\nRemoved: {}".format( + new_lines(EXPECTED_PERMISSIONS), + new_lines(permissions), + new_lines(permissions - EXPECTED_PERMISSIONS), + new_lines(EXPECTED_PERMISSIONS - permissions) + ) + + self.assertEqual(EXPECTED_PERMISSIONS, permissions, description) if __name__ == "__main__": - start_time = time.time() - logging.basicConfig(level=logging.DEBUG, format="%(msg)s") - logging.info("Running {0}".format(__file__)) - - args = get_args() - - if args.aapt is None: - find_aapt(args) - - logging.info("Using aapt at: {}".format(args.aapt)) - - permissions = exec_shell(args.aapt, "dump permissions {0}".format(args.apk)) - permissions = set( - filter( - lambda s: s.startswith("permission:") or s.startswith("uses-permission:"), - permissions - ) - ) - - check = check_permissions(permissions) - logging.info("Test took {} ms".format((time.time() - start_time) * 1000)) - if not check: - exit(1) - - + unittest.main() + exit(1)