chiark / gitweb /
Merge branch 'doc-update-v1' into 'master'
[fdroidserver.git] / tests / common.TestCase
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
5
6 import inspect
7 import optparse
8 import os
9 import sys
10 import unittest
11
12 localmodule = os.path.realpath(os.path.join(
13         os.path.dirname(inspect.getfile(inspect.currentframe())),
14         '..'))
15 print('localmodule: ' + localmodule)
16 if localmodule not in sys.path:
17     sys.path.insert(0,localmodule)
18
19 import fdroidserver.common
20
21 class CommonTest(unittest.TestCase):
22     '''fdroidserver/common.py'''
23
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
33                     break
34             return True
35         else:
36             print 'no build-tools found: ' + build_tools
37             return False
38
39     def _find_all(self):
40         for cmd in ('aapt', 'adb', 'android', 'zipalign'):
41             path = fdroidserver.common.find_sdk_tools_cmd(cmd)
42             if path is not None:
43                 self.assertTrue(os.path.exists(path))
44                 self.assertTrue(os.path.isfile(path))
45
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
49         #self._find_all()
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
55                 self._find_all()
56             build_tools = os.path.join(sdk_path, 'build-tools')
57             if self._set_build_tools():
58                 self._find_all()
59             else:
60                 print 'no build-tools found: ' + build_tools
61
62     def testIsApkDebuggable(self):
63         config = dict()
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
69         testfiles = []
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
78         testfiles = []
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!")
85
86
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'])
92
93     newSuite = unittest.TestSuite()
94     newSuite.addTest(unittest.makeSuite(CommonTest))
95     unittest.main()