2016-01-04 21:17:58 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-08-30 17:07:29 +02:00
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
2017-04-19 10:04:32 +02:00
|
|
|
import git
|
2014-08-30 17:07:29 +02:00
|
|
|
import inspect
|
2015-07-22 08:19:56 +02:00
|
|
|
import logging
|
2014-08-30 17:07:29 +02:00
|
|
|
import optparse
|
|
|
|
import os
|
2017-04-19 10:04:32 +02:00
|
|
|
import shutil
|
2014-08-30 17:07:29 +02:00
|
|
|
import sys
|
2017-04-19 10:04:32 +02:00
|
|
|
import tempfile
|
2014-08-30 17:07:29 +02:00
|
|
|
import unittest
|
2017-04-19 10:04:32 +02:00
|
|
|
import yaml
|
2016-01-04 21:31:22 +01:00
|
|
|
from binascii import unhexlify
|
2014-08-30 17:07:29 +02:00
|
|
|
|
2015-07-22 08:19:56 +02:00
|
|
|
localmodule = os.path.realpath(
|
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
|
2014-08-30 17:07:29 +02:00
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
if localmodule not in sys.path:
|
2015-07-22 08:19:56 +02:00
|
|
|
sys.path.insert(0, localmodule)
|
2014-08-30 17:07:29 +02:00
|
|
|
|
|
|
|
import fdroidserver.common
|
2017-04-19 10:04:32 +02:00
|
|
|
import fdroidserver.metadata
|
2014-08-30 17:07:29 +02:00
|
|
|
import fdroidserver.update
|
2015-01-20 22:43:55 +01:00
|
|
|
from fdroidserver.common import FDroidPopen
|
2014-08-30 17:07:29 +02:00
|
|
|
|
2015-07-22 08:19:56 +02:00
|
|
|
|
2014-08-30 17:07:29 +02:00
|
|
|
class UpdateTest(unittest.TestCase):
|
|
|
|
'''fdroid update'''
|
|
|
|
|
2017-04-13 23:36:46 +02:00
|
|
|
def testInsertStoreMetadata(self):
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
config['accepted_formats'] = ('txt', 'yml')
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
fdroidserver.update.options = fdroidserver.common.options
|
|
|
|
os.chdir(os.path.join(localmodule, 'tests'))
|
|
|
|
|
2017-05-16 12:25:42 +02:00
|
|
|
shutil.rmtree(os.path.join('repo', 'info.guardianproject.urzip'), ignore_errors=True)
|
|
|
|
|
2017-04-13 23:36:46 +02:00
|
|
|
apps = dict()
|
|
|
|
for packageName in ('info.guardianproject.urzip', 'org.videolan.vlc', 'obb.mainpatch.current'):
|
|
|
|
apps[packageName] = dict()
|
|
|
|
apps[packageName]['id'] = packageName
|
|
|
|
apps[packageName]['CurrentVersionCode'] = 0xcafebeef
|
|
|
|
apps['info.guardianproject.urzip']['CurrentVersionCode'] = 100
|
|
|
|
fdroidserver.update.insert_localized_app_metadata(apps)
|
|
|
|
|
2017-05-16 12:25:42 +02:00
|
|
|
appdir = os.path.join('repo', 'info.guardianproject.urzip', 'en-US')
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(appdir, 'icon.png')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(appdir, 'featureGraphic.png')))
|
|
|
|
|
2017-04-13 23:36:46 +02:00
|
|
|
self.assertEqual(3, len(apps))
|
|
|
|
for packageName, app in apps.items():
|
|
|
|
self.assertTrue('localized' in app)
|
|
|
|
self.assertTrue('en-US' in app['localized'])
|
|
|
|
self.assertEqual(1, len(app['localized']))
|
|
|
|
if packageName == 'info.guardianproject.urzip':
|
2017-05-16 12:25:42 +02:00
|
|
|
self.assertEqual(7, len(app['localized']['en-US']))
|
2017-04-27 21:12:49 +02:00
|
|
|
self.assertEqual('full description\n', app['localized']['en-US']['description'])
|
|
|
|
self.assertEqual('title\n', app['localized']['en-US']['name'])
|
|
|
|
self.assertEqual('short description\n', app['localized']['en-US']['summary'])
|
|
|
|
self.assertEqual('video\n', app['localized']['en-US']['video'])
|
2017-05-16 12:25:42 +02:00
|
|
|
self.assertEqual('icon.png', app['localized']['en-US']['icon'])
|
|
|
|
self.assertEqual('featureGraphic.png', app['localized']['en-US']['featureGraphic'])
|
2017-04-27 21:12:49 +02:00
|
|
|
self.assertEqual('100\n', app['localized']['en-US']['whatsNew'])
|
2017-04-13 23:36:46 +02:00
|
|
|
elif packageName == 'org.videolan.vlc':
|
|
|
|
self.assertEqual('icon.png', app['localized']['en-US']['icon'])
|
|
|
|
self.assertEqual(9, len(app['localized']['en-US']['phoneScreenshots']))
|
|
|
|
self.assertEqual(15, len(app['localized']['en-US']['sevenInchScreenshots']))
|
|
|
|
elif packageName == 'obb.mainpatch.current':
|
|
|
|
self.assertEqual('icon.png', app['localized']['en-US']['icon'])
|
|
|
|
self.assertEqual('featureGraphic.png', app['localized']['en-US']['featureGraphic'])
|
|
|
|
self.assertEqual(1, len(app['localized']['en-US']['phoneScreenshots']))
|
|
|
|
self.assertEqual(1, len(app['localized']['en-US']['sevenInchScreenshots']))
|
|
|
|
|
2017-04-19 10:04:32 +02:00
|
|
|
def test_insert_triple_t_metadata(self):
|
|
|
|
importer = os.path.join(localmodule, 'tests', 'tmp', 'importer')
|
|
|
|
packageName = 'org.fdroid.ci.test.app'
|
|
|
|
if not os.path.isdir(importer):
|
|
|
|
logging.warning('skipping test_insert_triple_t_metadata, import.TestCase must run first!')
|
|
|
|
return
|
|
|
|
tmpdir = os.path.join(localmodule, '.testfiles')
|
|
|
|
if not os.path.exists(tmpdir):
|
|
|
|
os.makedirs(tmpdir)
|
|
|
|
tmptestsdir = tempfile.mkdtemp(prefix='test_insert_triple_t_metadata-', dir=tmpdir)
|
|
|
|
packageDir = os.path.join(tmptestsdir, 'build', packageName)
|
|
|
|
shutil.copytree(importer, packageDir)
|
|
|
|
|
|
|
|
# always use the same commit so these tests work when ci-test-app.git is updated
|
|
|
|
repo = git.Repo(packageDir)
|
|
|
|
for remote in repo.remotes:
|
|
|
|
remote.fetch()
|
|
|
|
repo.git.reset('--hard', 'b9e5d1a0d8d6fc31d4674b2f0514fef10762ed4f')
|
|
|
|
repo.git.clean('-fdx')
|
|
|
|
|
|
|
|
os.mkdir(os.path.join(tmptestsdir, 'metadata'))
|
|
|
|
metadata = dict()
|
|
|
|
metadata['Description'] = 'This is just a test app'
|
|
|
|
with open(os.path.join(tmptestsdir, 'metadata', packageName + '.yml'), 'w') as fp:
|
|
|
|
yaml.dump(metadata, fp)
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
config['accepted_formats'] = ('yml')
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
fdroidserver.update.options = fdroidserver.common.options
|
|
|
|
os.chdir(tmptestsdir)
|
|
|
|
|
|
|
|
apps = fdroidserver.metadata.read_metadata(xref=True)
|
|
|
|
fdroidserver.update.copy_triple_t_store_metadata(apps)
|
|
|
|
|
|
|
|
# TODO ideally, this would compare the whole dict like in metadata.TestCase's test_read_metadata()
|
|
|
|
correctlocales = [
|
|
|
|
'ar', 'ast_ES', 'az', 'ca', 'ca_ES', 'cs-CZ', 'cs_CZ', 'da',
|
|
|
|
'da-DK', 'de', 'de-DE', 'el', 'en-US', 'es', 'es-ES', 'es_ES', 'et',
|
|
|
|
'fi', 'fr', 'fr-FR', 'he_IL', 'hi-IN', 'hi_IN', 'hu', 'id', 'it',
|
|
|
|
'it-IT', 'it_IT', 'iw-IL', 'ja', 'ja-JP', 'kn_IN', 'ko', 'ko-KR',
|
|
|
|
'ko_KR', 'lt', 'nb', 'nb_NO', 'nl', 'nl-NL', 'no', 'pl', 'pl-PL',
|
|
|
|
'pl_PL', 'pt', 'pt-BR', 'pt-PT', 'pt_BR', 'ro', 'ro_RO', 'ru-RU',
|
|
|
|
'ru_RU', 'sv-SE', 'sv_SE', 'te', 'tr', 'tr-TR', 'uk', 'uk_UA', 'vi',
|
|
|
|
'vi_VN', 'zh-CN', 'zh_CN', 'zh_TW',
|
|
|
|
]
|
|
|
|
locales = sorted(list(apps['org.fdroid.ci.test.app']['localized'].keys()))
|
|
|
|
self.assertEqual(correctlocales, locales)
|
|
|
|
|
2014-08-30 17:07:29 +02:00
|
|
|
def javagetsig(self, apkfile):
|
|
|
|
getsig_dir = os.path.join(os.path.dirname(__file__), 'getsig')
|
|
|
|
if not os.path.exists(getsig_dir + "/getsig.class"):
|
|
|
|
logging.critical("getsig.class not found. To fix: cd '%s' && ./make.sh" % getsig_dir)
|
|
|
|
sys.exit(1)
|
2015-08-05 14:39:58 +02:00
|
|
|
# FDroidPopen needs some config to work
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.common.config = config
|
2014-08-30 17:07:29 +02:00
|
|
|
p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
|
|
|
|
'getsig', os.path.join(os.getcwd(), apkfile)])
|
|
|
|
sig = None
|
|
|
|
for line in p.output.splitlines():
|
|
|
|
if line.startswith('Result:'):
|
|
|
|
sig = line[7:].strip()
|
|
|
|
break
|
|
|
|
if p.returncode == 0:
|
|
|
|
return sig
|
|
|
|
else:
|
|
|
|
return None
|
2015-07-22 08:19:56 +02:00
|
|
|
|
2014-08-30 17:07:29 +02:00
|
|
|
def testGoodGetsig(self):
|
2016-02-11 20:43:55 +01:00
|
|
|
# config needed to use jarsigner and keytool
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.update.config = config
|
2014-08-30 17:07:29 +02:00
|
|
|
apkfile = os.path.join(os.path.dirname(__file__), 'urzip.apk')
|
|
|
|
sig = self.javagetsig(apkfile)
|
|
|
|
self.assertIsNotNone(sig, "sig is None")
|
|
|
|
pysig = fdroidserver.update.getsig(apkfile)
|
2015-07-22 08:19:56 +02:00
|
|
|
self.assertIsNotNone(pysig, "pysig is None")
|
2016-06-10 12:02:03 +02:00
|
|
|
self.assertEqual(sig, fdroidserver.update.getsig(apkfile),
|
|
|
|
"python sig not equal to java sig!")
|
|
|
|
self.assertEqual(len(sig), len(pysig),
|
|
|
|
"the length of the two sigs are different!")
|
2014-08-30 17:07:29 +02:00
|
|
|
try:
|
2016-06-10 12:02:03 +02:00
|
|
|
self.assertEqual(unhexlify(sig), unhexlify(pysig),
|
|
|
|
"the length of the two sigs are different!")
|
2014-08-30 17:07:29 +02:00
|
|
|
except TypeError as e:
|
2016-01-04 17:28:55 +01:00
|
|
|
print(e)
|
2014-08-30 17:07:29 +02:00
|
|
|
self.assertTrue(False, 'TypeError!')
|
|
|
|
|
|
|
|
def testBadGetsig(self):
|
2016-02-11 20:43:55 +01:00
|
|
|
# config needed to use jarsigner and keytool
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.update.config = config
|
2014-08-30 17:07:29 +02:00
|
|
|
apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')
|
|
|
|
sig = self.javagetsig(apkfile)
|
|
|
|
self.assertIsNone(sig, "sig should be None: " + str(sig))
|
|
|
|
pysig = fdroidserver.update.getsig(apkfile)
|
|
|
|
self.assertIsNone(pysig, "python sig should be None: " + str(sig))
|
|
|
|
|
|
|
|
apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')
|
|
|
|
sig = self.javagetsig(apkfile)
|
|
|
|
self.assertIsNone(sig, "sig should be None: " + str(sig))
|
|
|
|
pysig = fdroidserver.update.getsig(apkfile)
|
|
|
|
self.assertIsNone(pysig, "python sig should be None: " + str(sig))
|
|
|
|
|
2016-06-20 13:41:30 +02:00
|
|
|
def testScanApksAndObbs(self):
|
2017-04-13 23:36:46 +02:00
|
|
|
os.chdir(os.path.join(localmodule, 'tests'))
|
2016-06-14 11:43:07 +02:00
|
|
|
if os.path.basename(os.getcwd()) != 'tests':
|
|
|
|
raise Exception('This test must be run in the "tests/" subdir')
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
config['ndk_paths'] = dict()
|
2016-11-23 15:01:23 +01:00
|
|
|
config['accepted_formats'] = ['json', 'txt', 'yml']
|
2016-06-14 11:43:07 +02:00
|
|
|
fdroidserver.common.config = config
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
|
|
|
|
fdroidserver.update.options = type('', (), {})()
|
|
|
|
fdroidserver.update.options.clean = True
|
2016-06-20 13:41:30 +02:00
|
|
|
fdroidserver.update.options.delete_unknown = True
|
2017-05-31 21:20:35 +02:00
|
|
|
fdroidserver.update.options.rename_apks = False
|
2016-06-14 11:43:07 +02:00
|
|
|
|
2016-06-20 13:41:30 +02:00
|
|
|
apps = fdroidserver.metadata.read_metadata(xref=True)
|
2016-06-14 11:43:07 +02:00
|
|
|
knownapks = fdroidserver.common.KnownApks()
|
2016-10-13 17:28:54 +02:00
|
|
|
apks, cachechanged = fdroidserver.update.scan_apks({}, 'repo', knownapks, False)
|
2017-05-30 14:52:33 +02:00
|
|
|
self.assertEqual(len(apks), 7)
|
2016-06-14 11:43:07 +02:00
|
|
|
apk = apks[0]
|
|
|
|
self.assertEqual(apk['minSdkVersion'], '4')
|
|
|
|
self.assertEqual(apk['targetSdkVersion'], '18')
|
|
|
|
self.assertFalse('maxSdkVersion' in apk)
|
|
|
|
|
2016-06-20 13:41:30 +02:00
|
|
|
fdroidserver.update.insert_obbs('repo', apps, apks)
|
|
|
|
for apk in apks:
|
2016-11-29 13:40:21 +01:00
|
|
|
if apk['packageName'] == 'obb.mainpatch.current':
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertEqual(apk.get('obbMainFile'), 'main.1619.obb.mainpatch.current.obb')
|
|
|
|
self.assertEqual(apk.get('obbPatchFile'), 'patch.1619.obb.mainpatch.current.obb')
|
2016-11-29 13:40:21 +01:00
|
|
|
elif apk['packageName'] == 'obb.main.oldversion':
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertEqual(apk.get('obbMainFile'), 'main.1434483388.obb.main.oldversion.obb')
|
|
|
|
self.assertIsNone(apk.get('obbPatchFile'))
|
2016-11-29 13:40:21 +01:00
|
|
|
elif apk['packageName'] == 'obb.main.twoversions':
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertIsNone(apk.get('obbPatchFile'))
|
2016-11-29 13:40:21 +01:00
|
|
|
if apk['versionCode'] == 1101613:
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertEqual(apk.get('obbMainFile'), 'main.1101613.obb.main.twoversions.obb')
|
2016-11-29 13:40:21 +01:00
|
|
|
elif apk['versionCode'] == 1101615:
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertEqual(apk.get('obbMainFile'), 'main.1101615.obb.main.twoversions.obb')
|
2016-11-29 13:40:21 +01:00
|
|
|
elif apk['versionCode'] == 1101617:
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertEqual(apk.get('obbMainFile'), 'main.1101615.obb.main.twoversions.obb')
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
2016-11-29 13:40:21 +01:00
|
|
|
elif apk['packageName'] == 'info.guardianproject.urzip':
|
2016-06-20 13:41:30 +02:00
|
|
|
self.assertIsNone(apk.get('obbMainFile'))
|
|
|
|
self.assertIsNone(apk.get('obbPatchFile'))
|
|
|
|
|
2017-04-13 14:18:48 +02:00
|
|
|
def testScanApkMetadata(self):
|
|
|
|
|
|
|
|
def _build_yaml_representer(dumper, data):
|
|
|
|
'''Creates a YAML representation of a Build instance'''
|
|
|
|
return dumper.represent_dict(data)
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
if os.path.basename(os.getcwd()) != 'tests':
|
|
|
|
raise Exception('This test must be run in the "tests/" subdir')
|
|
|
|
|
|
|
|
config['ndk_paths'] = dict()
|
|
|
|
config['accepted_formats'] = ['json', 'txt', 'yml']
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
|
|
|
|
fdroidserver.update.options = type('', (), {})()
|
|
|
|
fdroidserver.update.options.clean = True
|
2017-05-16 12:25:42 +02:00
|
|
|
fdroidserver.update.options.rename_apks = False
|
2017-04-13 14:18:48 +02:00
|
|
|
fdroidserver.update.options.delete_unknown = True
|
|
|
|
|
|
|
|
for icon_dir in fdroidserver.update.get_all_icon_dirs('repo'):
|
|
|
|
if not os.path.exists(icon_dir):
|
|
|
|
os.makedirs(icon_dir)
|
|
|
|
|
|
|
|
knownapks = fdroidserver.common.KnownApks()
|
|
|
|
apkList = ['../urzip.apk', '../org.dyndns.fules.ck_20.apk']
|
|
|
|
|
|
|
|
for apkName in apkList:
|
|
|
|
_, apk, cachechanged = fdroidserver.update.scan_apk({}, apkName, 'repo', knownapks, False)
|
|
|
|
# Don't care about the date added to the repo and relative apkName
|
|
|
|
del apk['added']
|
|
|
|
del apk['apkName']
|
|
|
|
# avoid AAPT application name bug
|
|
|
|
del apk['name']
|
|
|
|
|
|
|
|
savepath = os.path.join('metadata', 'apk', apk['packageName'] + '.yaml')
|
|
|
|
# Uncomment to save APK metadata
|
|
|
|
# with open(savepath, 'w') as f:
|
|
|
|
# yaml.add_representer(fdroidserver.metadata.Build, _build_yaml_representer)
|
|
|
|
# yaml.dump(apk, f, default_flow_style=False)
|
|
|
|
|
|
|
|
with open(savepath, 'r') as f:
|
|
|
|
frompickle = yaml.load(f)
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(apk, frompickle)
|
|
|
|
|
2017-04-03 18:07:49 +02:00
|
|
|
def test_scan_invalid_apk(self):
|
2017-04-13 23:36:46 +02:00
|
|
|
os.chdir(os.path.join(localmodule, 'tests'))
|
2017-04-03 18:07:49 +02:00
|
|
|
if os.path.basename(os.getcwd()) != 'tests':
|
|
|
|
raise Exception('This test must be run in the "tests/" subdir')
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
fdroidserver.update.config = config
|
|
|
|
fdroidserver.update.options.delete_unknown = False
|
|
|
|
|
|
|
|
knownapks = fdroidserver.common.KnownApks()
|
|
|
|
apk = 'fake.ota.update_1234.zip' # this is not an APK, scanning should fail
|
|
|
|
(skip, apk, cachechanged) = fdroidserver.update.scan_apk({}, apk, 'repo', knownapks, False)
|
|
|
|
|
|
|
|
self.assertTrue(skip)
|
|
|
|
self.assertIsNone(apk)
|
|
|
|
self.assertFalse(cachechanged)
|
|
|
|
|
2014-08-30 17:07:29 +02:00
|
|
|
|
|
|
|
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(UpdateTest))
|
|
|
|
unittest.main()
|