2 # -*- coding: utf-8 -*-
4 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
12 localmodule = os.path.realpath(os.path.join(
13 os.path.dirname(inspect.getfile(inspect.currentframe())),
15 print('localmodule: ' + localmodule)
16 if localmodule not in sys.path:
17 sys.path.insert(0,localmodule)
19 import fdroidserver.common
21 class CommonTest(unittest.TestCase):
22 '''fdroidserver/common.py'''
24 def _set_build_tools(self):
25 build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools')
26 if os.path.exists(build_tools):
27 fdroidserver.common.config['build_tools'] = ''
28 for f in sorted(os.listdir(build_tools), reverse=True):
29 versioned = os.path.join(build_tools, f)
30 if os.path.isdir(versioned) \
31 and os.path.isfile(os.path.join(versioned, 'aapt')):
32 fdroidserver.common.config['build_tools'] = versioned
36 print 'no build-tools found: ' + build_tools
40 for cmd in ('aapt', 'adb', 'android', 'zipalign'):
41 path = fdroidserver.common.find_sdk_tools_cmd(cmd)
43 self.assertTrue(os.path.exists(path))
44 self.assertTrue(os.path.isfile(path))
46 def test_find_sdk_tools_cmd(self):
47 fdroidserver.common.config = dict()
48 # TODO add this once everything works without sdk_path set in config
50 sdk_path = os.getenv('ANDROID_HOME')
51 if os.path.exists(sdk_path):
52 fdroidserver.common.config['sdk_path'] = sdk_path
53 if os.path.exists('/usr/bin/aapt'):
54 # this test only works when /usr/bin/aapt is installed
56 build_tools = os.path.join(sdk_path, 'build-tools')
57 if self._set_build_tools():
60 print 'no build-tools found: ' + build_tools
62 def testIsApkDebuggable(self):
64 config['sdk_path'] = os.getenv('ANDROID_HOME')
65 fdroidserver.common.config = config
66 self._set_build_tools();
67 config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
68 # these are set debuggable
70 testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip.apk'))
71 testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk'))
72 testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk'))
73 for apkfile in testfiles:
74 debuggable = fdroidserver.common.isApkDebuggable(apkfile, config)
75 self.assertTrue(debuggable,
76 "debuggable APK state was not properly parsed!")
77 # these are set NOT debuggable
79 testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release.apk'))
80 testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release-unsigned.apk'))
81 for apkfile in testfiles:
82 debuggable = fdroidserver.common.isApkDebuggable(apkfile, config)
83 self.assertFalse(debuggable,
84 "debuggable APK state was not properly parsed!")
87 if __name__ == "__main__":
88 parser = optparse.OptionParser()
89 parser.add_option("-v", "--verbose", action="store_true", default=False,
90 help="Spew out even more information than normal")
91 (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
93 newSuite = unittest.TestSuite()
94 newSuite.addTest(unittest.makeSuite(CommonTest))