#!/usr/bin/env python2 # -*- coding: utf-8 -*- # http://www.drdobbs.com/testing/unit-testing-with-python/240165163 import inspect import optparse import os import sys import unittest localmodule = os.path.realpath(os.path.join( os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) print('localmodule: ' + localmodule) if localmodule not in sys.path: sys.path.insert(0,localmodule) import fdroidserver.common class CommonTest(unittest.TestCase): '''fdroidserver/common.py''' def _set_build_tools(self): build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools') if os.path.exists(build_tools): fdroidserver.common.config['build_tools'] = '' for f in sorted(os.listdir(build_tools), reverse=True): versioned = os.path.join(build_tools, f) if os.path.isdir(versioned) \ and os.path.isfile(os.path.join(versioned, 'aapt')): fdroidserver.common.config['build_tools'] = versioned break return True else: print 'no build-tools found: ' + build_tools return False def _find_all(self): for cmd in ('aapt', 'adb', 'android', 'zipalign'): path = fdroidserver.common.find_sdk_tools_cmd(cmd) if path is not None: self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.isfile(path)) def test_find_sdk_tools_cmd(self): fdroidserver.common.config = dict() # TODO add this once everything works without sdk_path set in config #self._find_all() sdk_path = os.getenv('ANDROID_HOME') if os.path.exists(sdk_path): fdroidserver.common.config['sdk_path'] = sdk_path if os.path.exists('/usr/bin/aapt'): # this test only works when /usr/bin/aapt is installed self._find_all() build_tools = os.path.join(sdk_path, 'build-tools') if self._set_build_tools(): self._find_all() else: print 'no build-tools found: ' + build_tools def testIsApkDebuggable(self): config = dict() config['sdk_path'] = os.getenv('ANDROID_HOME') fdroidserver.common.config = config self._set_build_tools(); config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt') # these are set debuggable testfiles = [] testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip.apk')) testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')) testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')) for apkfile in testfiles: debuggable = fdroidserver.common.isApkDebuggable(apkfile, config) self.assertTrue(debuggable, "debuggable APK state was not properly parsed!") # these are set NOT debuggable testfiles = [] testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release.apk')) testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release-unsigned.apk')) for apkfile in testfiles: debuggable = fdroidserver.common.isApkDebuggable(apkfile, config) self.assertFalse(debuggable, "debuggable APK state was not properly parsed!") def testPackageNameValidity(self): for name in ["org.fdroid.fdroid", "org.f_droid.fdr0ID"]: self.assertTrue(fdroidserver.common.is_valid_package_name(name), "{0} should be a valid package name".format(name)) for name in ["0rg.fdroid.fdroid", ".f_droid.fdr0ID", "org.fdroid/fdroid", "/org.fdroid.fdroid"]: self.assertFalse(fdroidserver.common.is_valid_package_name(name), "{0} should not be a valid package name".format(name)) if __name__ == "__main__": parser = optparse.OptionParser() parser.add_option("-v", "--verbose", action="store_true", default=False, help="Spew out even more information than normal") (fdroidserver.common.options, args) = parser.parse_args(['--verbose']) newSuite = unittest.TestSuite() newSuite.addTest(unittest.makeSuite(CommonTest)) unittest.main()