2 # -*- coding: utf-8 -*-
4 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
13 localmodule = os.path.realpath(os.path.join(
14 os.path.dirname(inspect.getfile(inspect.currentframe())),
16 print('localmodule: ' + localmodule)
17 if localmodule not in sys.path:
18 sys.path.insert(0,localmodule)
20 import fdroidserver.common
21 import fdroidserver.metadata
23 class CommonTest(unittest.TestCase):
24 '''fdroidserver/common.py'''
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
38 print 'no build-tools found: ' + build_tools
42 for cmd in ('aapt', 'adb', 'android', 'zipalign'):
43 path = fdroidserver.common.find_sdk_tools_cmd(cmd)
45 self.assertTrue(os.path.exists(path))
46 self.assertTrue(os.path.isfile(path))
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
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
58 build_tools = os.path.join(sdk_path, 'build-tools')
59 if self._set_build_tools():
62 print 'no build-tools found: ' + build_tools
64 def testIsApkDebuggable(self):
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
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
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!")
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",
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))
100 def test_prepare_sources(self):
102 teststr = 'FAKE_STR_FOR_TESTING'
103 testdir = os.path.dirname(__file__)
106 config['sdk_path'] = os.getenv('ANDROID_HOME')
107 config['build_tools'] = 'FAKE_BUILD_TOOLS_VERSION'
108 fdroidserver.common.config = config
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
123 # no need to change to the correct commit here
124 def gotorevision(self, rev, refresh=True):
127 # no srclib info needed, but it could be added...
131 fdroidserver.common.prepare_source(FakeVcs(), app, build, testdir, testdir, testdir)
133 filedata = open(os.path.join(testdir, 'build.gradle')).read()
134 self.assertIsNotNone(re.search("\s+compileSdkVersion %s\s+" % testint, filedata))
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))
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'])
148 newSuite = unittest.TestSuite()
149 newSuite.addTest(unittest.makeSuite(CommonTest))