chiark / gitweb /
Merge branch 'update-install' 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 re
10 import sys
11 import unittest
12
13 localmodule = os.path.realpath(os.path.join(
14         os.path.dirname(inspect.getfile(inspect.currentframe())),
15         '..'))
16 print('localmodule: ' + localmodule)
17 if localmodule not in sys.path:
18     sys.path.insert(0,localmodule)
19
20 import fdroidserver.common
21 import fdroidserver.metadata
22
23 class CommonTest(unittest.TestCase):
24     '''fdroidserver/common.py'''
25
26     def _set_build_tools(self):
27         build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools')
28         if os.path.exists(build_tools):
29             fdroidserver.common.config['build_tools'] = ''
30             for f in sorted(os.listdir(build_tools), reverse=True):
31                 versioned = os.path.join(build_tools, f)
32                 if os.path.isdir(versioned) \
33                         and os.path.isfile(os.path.join(versioned, 'aapt')):
34                     fdroidserver.common.config['build_tools'] = versioned
35                     break
36             return True
37         else:
38             print 'no build-tools found: ' + build_tools
39             return False
40
41     def _find_all(self):
42         for cmd in ('aapt', 'adb', 'android', 'zipalign'):
43             path = fdroidserver.common.find_sdk_tools_cmd(cmd)
44             if path is not None:
45                 self.assertTrue(os.path.exists(path))
46                 self.assertTrue(os.path.isfile(path))
47
48     def test_find_sdk_tools_cmd(self):
49         fdroidserver.common.config = dict()
50         # TODO add this once everything works without sdk_path set in config
51         #self._find_all()
52         sdk_path = os.getenv('ANDROID_HOME')
53         if os.path.exists(sdk_path):
54             fdroidserver.common.config['sdk_path'] = sdk_path
55             if os.path.exists('/usr/bin/aapt'):
56                 # this test only works when /usr/bin/aapt is installed
57                 self._find_all()
58             build_tools = os.path.join(sdk_path, 'build-tools')
59             if self._set_build_tools():
60                 self._find_all()
61             else:
62                 print 'no build-tools found: ' + build_tools
63
64     def testIsApkDebuggable(self):
65         config = dict()
66         config['sdk_path'] = os.getenv('ANDROID_HOME')
67         fdroidserver.common.config = config
68         self._set_build_tools();
69         config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
70         # these are set debuggable
71         testfiles = []
72         testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip.apk'))
73         testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk'))
74         testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk'))
75         for apkfile in testfiles:
76             debuggable = fdroidserver.common.isApkDebuggable(apkfile, config)
77             self.assertTrue(debuggable,
78                             "debuggable APK state was not properly parsed!")
79         # these are set NOT debuggable
80         testfiles = []
81         testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release.apk'))
82         testfiles.append(os.path.join(os.path.dirname(__file__), 'urzip-release-unsigned.apk'))
83         for apkfile in testfiles:
84             debuggable = fdroidserver.common.isApkDebuggable(apkfile, config)
85             self.assertFalse(debuggable,
86                              "debuggable APK state was not properly parsed!")
87
88     def testPackageNameValidity(self):
89         for name in ["org.fdroid.fdroid",
90                      "org.f_droid.fdr0ID"]:
91             self.assertTrue(fdroidserver.common.is_valid_package_name(name),
92                     "{0} should be a valid package name".format(name))
93         for name in ["0rg.fdroid.fdroid",
94                      ".f_droid.fdr0ID",
95                      "org.fdroid/fdroid",
96                      "/org.fdroid.fdroid"]:
97             self.assertFalse(fdroidserver.common.is_valid_package_name(name),
98                     "{0} should not be a valid package name".format(name))
99
100     def test_prepare_sources(self):
101         testint = 99999999
102         teststr = 'FAKE_STR_FOR_TESTING'
103         testdir = os.path.dirname(__file__)
104
105         config = dict()
106         config['sdk_path'] = os.getenv('ANDROID_HOME')
107         config['build_tools'] = 'FAKE_BUILD_TOOLS_VERSION'
108         fdroidserver.common.config = config
109         app = dict()
110         app['id'] = 'org.fdroid.froid'
111         build = dict(fdroidserver.metadata.flag_defaults)
112         build['commit'] = 'master'
113         build['forceversion'] = True
114         build['forcevercode'] = True
115         build['gradle'] = ['yes']
116         build['ndk_path'] = os.getenv('ANDROID_NDK_HOME')
117         build['target'] = 'android-' + str(testint)
118         build['type'] = 'gradle'
119         build['version'] = teststr
120         build['vercode'] = testint
121
122         class FakeVcs():
123             # no need to change to the correct commit here
124             def gotorevision(self, rev, refresh=True):
125                 pass
126
127             # no srclib info needed, but it could be added...
128             def getsrclib(self):
129                 return None
130
131         fdroidserver.common.prepare_source(FakeVcs(), app, build, testdir, testdir, testdir)
132
133         filedata = open(os.path.join(testdir, 'build.gradle')).read()
134         self.assertIsNotNone(re.search("\s+compileSdkVersion %s\s+" % testint, filedata))
135
136         filedata = open(os.path.join(testdir, 'AndroidManifest.xml')).read()
137         self.assertIsNone(re.search('android:debuggable', filedata))
138         self.assertIsNotNone(re.search('android:versionName="%s"' % build['version'], filedata))
139         self.assertIsNotNone(re.search('android:versionCode="%s"' % build['vercode'], filedata))
140
141
142 if __name__ == "__main__":
143     parser = optparse.OptionParser()
144     parser.add_option("-v", "--verbose", action="store_true", default=False,
145                       help="Spew out even more information than normal")
146     (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
147
148     newSuite = unittest.TestSuite()
149     newSuite.addTest(unittest.makeSuite(CommonTest))
150     unittest.main()