2016-01-04 21:17:58 +01:00
|
|
|
|
#!/usr/bin/env python3
|
2014-12-08 22:55:14 +01:00
|
|
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
|
|
|
|
|
import inspect
|
2017-09-25 16:28:22 +02:00
|
|
|
|
import logging
|
2014-12-08 22:55:14 +01:00
|
|
|
|
import optparse
|
|
|
|
|
import os
|
2015-07-30 22:13:36 +02:00
|
|
|
|
import re
|
2015-08-11 15:33:03 +02:00
|
|
|
|
import shutil
|
2014-12-08 22:55:14 +01:00
|
|
|
|
import sys
|
2015-08-11 15:33:03 +02:00
|
|
|
|
import tempfile
|
2014-12-08 22:55:14 +01:00
|
|
|
|
import unittest
|
2017-04-02 12:08:01 +02:00
|
|
|
|
import textwrap
|
2017-09-25 16:34:25 +02:00
|
|
|
|
import yaml
|
2016-12-19 16:54:32 +01:00
|
|
|
|
from zipfile import ZipFile
|
2014-12-08 22:55:14 +01:00
|
|
|
|
|
2017-03-29 17:11:40 +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-12-08 22:55:14 +01:00
|
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
|
if localmodule not in sys.path:
|
2015-07-22 08:19:56 +02:00
|
|
|
|
sys.path.insert(0, localmodule)
|
2014-12-08 22:55:14 +01:00
|
|
|
|
|
2017-04-02 12:08:01 +02:00
|
|
|
|
import fdroidserver.signindex
|
2014-12-08 22:55:14 +01:00
|
|
|
|
import fdroidserver.common
|
2015-07-30 22:13:36 +02:00
|
|
|
|
import fdroidserver.metadata
|
2017-10-16 18:11:57 +02:00
|
|
|
|
from testcommon import TmpCwd
|
2017-09-12 18:17:46 +02:00
|
|
|
|
from fdroidserver.exception import FDroidException
|
2014-12-08 22:55:14 +01:00
|
|
|
|
|
2015-07-22 08:19:56 +02:00
|
|
|
|
|
2014-12-08 22:55:14 +01:00
|
|
|
|
class CommonTest(unittest.TestCase):
|
|
|
|
|
'''fdroidserver/common.py'''
|
|
|
|
|
|
2017-09-25 16:28:22 +02:00
|
|
|
|
def setUp(self):
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
self.basedir = os.path.join(localmodule, 'tests')
|
|
|
|
|
self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
|
|
|
|
|
if not os.path.exists(self.tmpdir):
|
|
|
|
|
os.makedirs(self.tmpdir)
|
|
|
|
|
os.chdir(self.basedir)
|
|
|
|
|
|
2017-10-16 18:11:57 +02:00
|
|
|
|
def test_assert_config_keystore(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
|
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
|
fdroidserver.common.assert_config_keystore({})
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
|
|
|
|
c = {'repo_keyalias': 'localhost',
|
|
|
|
|
'keystore': 'keystore.jks',
|
|
|
|
|
'keystorepass': '12345',
|
|
|
|
|
'keypass': '12345'}
|
|
|
|
|
with open('keystore.jks', 'w'):
|
|
|
|
|
pass
|
|
|
|
|
fdroidserver.common.assert_config_keystore(c)
|
|
|
|
|
|
2014-12-09 14:12:41 +01:00
|
|
|
|
def _set_build_tools(self):
|
|
|
|
|
build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools')
|
|
|
|
|
if os.path.exists(build_tools):
|
|
|
|
|
fdroidserver.common.config['build_tools'] = ''
|
|
|
|
|
for f in sorted(os.listdir(build_tools), reverse=True):
|
|
|
|
|
versioned = os.path.join(build_tools, f)
|
|
|
|
|
if os.path.isdir(versioned) \
|
|
|
|
|
and os.path.isfile(os.path.join(versioned, 'aapt')):
|
|
|
|
|
fdroidserver.common.config['build_tools'] = versioned
|
|
|
|
|
break
|
|
|
|
|
return True
|
|
|
|
|
else:
|
2016-01-04 17:28:55 +01:00
|
|
|
|
print('no build-tools found: ' + build_tools)
|
2014-12-09 14:12:41 +01:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _find_all(self):
|
2017-11-08 13:51:03 +01:00
|
|
|
|
tools = ['aapt', 'adb', 'zipalign']
|
|
|
|
|
if os.path.exists(os.path.join(os.getenv('ANDROID_HOME'), 'tools', 'android')):
|
|
|
|
|
tools.append('android')
|
|
|
|
|
for cmd in tools:
|
2014-12-09 14:12:41 +01:00
|
|
|
|
path = fdroidserver.common.find_sdk_tools_cmd(cmd)
|
|
|
|
|
if path is not None:
|
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
|
self.assertTrue(os.path.isfile(path))
|
|
|
|
|
|
|
|
|
|
def test_find_sdk_tools_cmd(self):
|
|
|
|
|
fdroidserver.common.config = dict()
|
|
|
|
|
# TODO add this once everything works without sdk_path set in config
|
2015-07-22 08:19:56 +02:00
|
|
|
|
# self._find_all()
|
2014-12-09 14:12:41 +01:00
|
|
|
|
sdk_path = os.getenv('ANDROID_HOME')
|
|
|
|
|
if os.path.exists(sdk_path):
|
|
|
|
|
fdroidserver.common.config['sdk_path'] = sdk_path
|
|
|
|
|
if os.path.exists('/usr/bin/aapt'):
|
|
|
|
|
# this test only works when /usr/bin/aapt is installed
|
|
|
|
|
self._find_all()
|
|
|
|
|
build_tools = os.path.join(sdk_path, 'build-tools')
|
|
|
|
|
if self._set_build_tools():
|
|
|
|
|
self._find_all()
|
|
|
|
|
else:
|
2016-01-04 17:28:55 +01:00
|
|
|
|
print('no build-tools found: ' + build_tools)
|
2014-12-09 14:12:41 +01:00
|
|
|
|
|
2017-10-20 11:35:48 +02:00
|
|
|
|
def test_find_java_root_path(self):
|
2017-11-30 09:59:02 +01:00
|
|
|
|
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
|
|
|
|
os.chdir(testdir)
|
2017-10-20 11:35:48 +02:00
|
|
|
|
|
|
|
|
|
all_pathlists = [
|
|
|
|
|
([ # Debian
|
|
|
|
|
'/usr/lib/jvm/java-1.5.0-gcj-5-amd64',
|
|
|
|
|
'/usr/lib/jvm/java-8-openjdk-amd64',
|
|
|
|
|
'/usr/lib/jvm/java-1.8.0-openjdk-amd64',
|
|
|
|
|
], '/usr/lib/jvm/java-8-openjdk-amd64'),
|
|
|
|
|
([ # OSX
|
|
|
|
|
'/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk',
|
|
|
|
|
'/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk',
|
|
|
|
|
'/System/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk',
|
|
|
|
|
], '/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for pathlist, choice in all_pathlists:
|
|
|
|
|
# strip leading / to make relative paths to test without root
|
|
|
|
|
pathlist = [p[1:] for p in pathlist]
|
|
|
|
|
|
|
|
|
|
# create test file used in common._add_java_paths_to_config()
|
|
|
|
|
for p in pathlist:
|
|
|
|
|
if p.startswith('/System') or p.startswith('/Library'):
|
|
|
|
|
basedir = os.path.join(p, 'Contents', 'Home', 'bin')
|
|
|
|
|
else:
|
|
|
|
|
basedir = os.path.join(p, 'bin')
|
|
|
|
|
os.makedirs(basedir)
|
|
|
|
|
open(os.path.join(basedir, 'javac'), 'w').close()
|
|
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
|
config['java_paths'] = dict()
|
|
|
|
|
fdroidserver.common._add_java_paths_to_config(pathlist, config)
|
|
|
|
|
self.assertEqual(config['java_paths']['8'], choice[1:])
|
|
|
|
|
|
2014-12-08 22:55:14 +01:00
|
|
|
|
def testIsApkDebuggable(self):
|
|
|
|
|
config = dict()
|
2015-08-05 14:39:58 +02:00
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
2014-12-09 14:12:41 +01:00
|
|
|
|
fdroidserver.common.config = config
|
2015-07-22 08:19:56 +02:00
|
|
|
|
self._set_build_tools()
|
2014-12-09 14:12:41 +01:00
|
|
|
|
config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
|
2014-12-08 22:55:14 +01:00
|
|
|
|
# these are set debuggable
|
|
|
|
|
testfiles = []
|
2017-09-25 16:28:22 +02:00
|
|
|
|
testfiles.append(os.path.join(self.basedir, 'urzip.apk'))
|
|
|
|
|
testfiles.append(os.path.join(self.basedir, 'urzip-badsig.apk'))
|
|
|
|
|
testfiles.append(os.path.join(self.basedir, 'urzip-badcert.apk'))
|
2014-12-08 22:55:14 +01:00
|
|
|
|
for apkfile in testfiles:
|
2017-04-13 12:30:04 +02:00
|
|
|
|
debuggable = fdroidserver.common.isApkAndDebuggable(apkfile)
|
2014-12-08 22:55:14 +01:00
|
|
|
|
self.assertTrue(debuggable,
|
|
|
|
|
"debuggable APK state was not properly parsed!")
|
|
|
|
|
# these are set NOT debuggable
|
|
|
|
|
testfiles = []
|
2017-09-25 16:28:22 +02:00
|
|
|
|
testfiles.append(os.path.join(self.basedir, 'urzip-release.apk'))
|
|
|
|
|
testfiles.append(os.path.join(self.basedir, 'urzip-release-unsigned.apk'))
|
2014-12-08 22:55:14 +01:00
|
|
|
|
for apkfile in testfiles:
|
2017-04-13 12:30:04 +02:00
|
|
|
|
debuggable = fdroidserver.common.isApkAndDebuggable(apkfile)
|
2014-12-08 22:55:14 +01:00
|
|
|
|
self.assertFalse(debuggable,
|
|
|
|
|
"debuggable APK state was not properly parsed!")
|
|
|
|
|
|
2015-01-26 19:29:39 +01:00
|
|
|
|
def testPackageNameValidity(self):
|
|
|
|
|
for name in ["org.fdroid.fdroid",
|
|
|
|
|
"org.f_droid.fdr0ID"]:
|
|
|
|
|
self.assertTrue(fdroidserver.common.is_valid_package_name(name),
|
2015-07-22 08:19:56 +02:00
|
|
|
|
"{0} should be a valid package name".format(name))
|
2015-01-26 19:29:39 +01:00
|
|
|
|
for name in ["0rg.fdroid.fdroid",
|
|
|
|
|
".f_droid.fdr0ID",
|
|
|
|
|
"org.fdroid/fdroid",
|
|
|
|
|
"/org.fdroid.fdroid"]:
|
|
|
|
|
self.assertFalse(fdroidserver.common.is_valid_package_name(name),
|
2015-07-22 08:19:56 +02:00
|
|
|
|
"{0} should not be a valid package name".format(name))
|
2014-12-08 22:55:14 +01:00
|
|
|
|
|
2015-07-30 22:13:36 +02:00
|
|
|
|
def test_prepare_sources(self):
|
|
|
|
|
testint = 99999999
|
|
|
|
|
teststr = 'FAKE_STR_FOR_TESTING'
|
2015-08-11 15:33:03 +02:00
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
|
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
2017-09-25 16:28:22 +02:00
|
|
|
|
shutil.copytree(os.path.join(self.basedir, 'source-files'),
|
2017-11-30 10:14:38 +01:00
|
|
|
|
os.path.join(testdir, 'source-files'))
|
2015-08-11 15:33:03 +02:00
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
|
fdroidclient_testdir = os.path.join(testdir, 'source-files', 'fdroid', 'fdroidclient')
|
2015-07-30 22:13:36 +02:00
|
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
|
config['sdk_path'] = os.getenv('ANDROID_HOME')
|
2015-11-28 17:55:27 +01:00
|
|
|
|
config['ndk_paths'] = {'r10d': os.getenv('ANDROID_NDK_HOME')}
|
2015-07-30 22:13:36 +02:00
|
|
|
|
config['build_tools'] = 'FAKE_BUILD_TOOLS_VERSION'
|
|
|
|
|
fdroidserver.common.config = config
|
2015-11-28 13:09:47 +01:00
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
|
app.id = 'org.fdroid.froid'
|
2015-11-28 17:55:27 +01:00
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
|
build.commit = 'master'
|
|
|
|
|
build.forceversion = True
|
|
|
|
|
build.forcevercode = True
|
|
|
|
|
build.gradle = ['yes']
|
|
|
|
|
build.target = 'android-' + str(testint)
|
2016-11-23 17:52:04 +01:00
|
|
|
|
build.versionName = teststr
|
|
|
|
|
build.versionCode = testint
|
2015-07-30 22:13:36 +02:00
|
|
|
|
|
|
|
|
|
class FakeVcs():
|
|
|
|
|
# no need to change to the correct commit here
|
|
|
|
|
def gotorevision(self, rev, refresh=True):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# no srclib info needed, but it could be added...
|
|
|
|
|
def getsrclib(self):
|
|
|
|
|
return None
|
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
|
fdroidserver.common.prepare_source(FakeVcs(), app, build,
|
|
|
|
|
fdroidclient_testdir, fdroidclient_testdir, fdroidclient_testdir)
|
2015-07-30 22:13:36 +02:00
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
|
with open(os.path.join(fdroidclient_testdir, 'build.gradle'), 'r') as f:
|
2015-10-08 13:20:35 +02:00
|
|
|
|
filedata = f.read()
|
2015-07-30 22:13:36 +02:00
|
|
|
|
self.assertIsNotNone(re.search("\s+compileSdkVersion %s\s+" % testint, filedata))
|
|
|
|
|
|
2017-11-30 10:14:38 +01:00
|
|
|
|
with open(os.path.join(fdroidclient_testdir, 'AndroidManifest.xml')) as f:
|
2015-10-08 13:20:35 +02:00
|
|
|
|
filedata = f.read()
|
2015-07-30 22:13:36 +02:00
|
|
|
|
self.assertIsNone(re.search('android:debuggable', filedata))
|
2016-11-23 17:52:04 +01:00
|
|
|
|
self.assertIsNotNone(re.search('android:versionName="%s"' % build.versionName, filedata))
|
|
|
|
|
self.assertIsNotNone(re.search('android:versionCode="%s"' % build.versionCode, filedata))
|
2015-07-30 22:13:36 +02:00
|
|
|
|
|
2017-09-25 16:34:25 +02:00
|
|
|
|
def test_prepare_sources_refresh(self):
|
|
|
|
|
packageName = 'org.fdroid.ci.test.app'
|
2017-11-30 09:59:02 +01:00
|
|
|
|
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
2017-09-25 16:34:25 +02:00
|
|
|
|
print('testdir', testdir)
|
|
|
|
|
os.chdir(testdir)
|
|
|
|
|
os.mkdir('build')
|
|
|
|
|
os.mkdir('metadata')
|
|
|
|
|
|
|
|
|
|
# use a local copy if available to avoid hitting the network
|
|
|
|
|
tmprepo = os.path.join(self.basedir, 'tmp', 'importer')
|
|
|
|
|
if os.path.exists(tmprepo):
|
|
|
|
|
git_url = tmprepo
|
|
|
|
|
else:
|
|
|
|
|
git_url = 'https://gitlab.com/fdroid/ci-test-app.git'
|
|
|
|
|
|
|
|
|
|
metadata = dict()
|
|
|
|
|
metadata['Description'] = 'This is just a test app'
|
|
|
|
|
metadata['RepoType'] = 'git'
|
|
|
|
|
metadata['Repo'] = git_url
|
|
|
|
|
with open(os.path.join('metadata', packageName + '.yml'), 'w') as fp:
|
|
|
|
|
yaml.dump(metadata, fp)
|
|
|
|
|
|
|
|
|
|
gitrepo = os.path.join(testdir, 'build', packageName)
|
|
|
|
|
vcs0 = fdroidserver.common.getvcs('git', git_url, gitrepo)
|
|
|
|
|
vcs0.gotorevision('0.3', refresh=True)
|
|
|
|
|
vcs1 = fdroidserver.common.getvcs('git', git_url, gitrepo)
|
|
|
|
|
vcs1.gotorevision('0.3', refresh=False)
|
|
|
|
|
|
2016-02-17 21:45:06 +01:00
|
|
|
|
def test_fdroid_popen_stderr_redirect(self):
|
2017-09-25 16:28:22 +02:00
|
|
|
|
config = dict()
|
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
|
2016-02-17 21:45:06 +01:00
|
|
|
|
commands = ['sh', '-c', 'echo stdout message && echo stderr message 1>&2']
|
|
|
|
|
|
|
|
|
|
p = fdroidserver.common.FDroidPopen(commands)
|
|
|
|
|
self.assertEqual(p.output, 'stdout message\nstderr message\n')
|
|
|
|
|
|
|
|
|
|
p = fdroidserver.common.FDroidPopen(commands, stderr_to_stdout=False)
|
|
|
|
|
self.assertEqual(p.output, 'stdout message\n')
|
2015-07-30 22:13:36 +02:00
|
|
|
|
|
2017-03-15 21:23:44 +01:00
|
|
|
|
def test_signjar(self):
|
|
|
|
|
fdroidserver.common.config = None
|
|
|
|
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
|
|
|
|
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
|
fdroidserver.common.config = config
|
2017-03-29 17:11:40 +02:00
|
|
|
|
fdroidserver.signindex.config = config
|
2017-03-15 21:23:44 +01:00
|
|
|
|
|
2017-09-25 16:28:22 +02:00
|
|
|
|
sourcedir = os.path.join(self.basedir, 'signindex')
|
2017-11-30 09:59:02 +01:00
|
|
|
|
testsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
2017-03-15 21:23:44 +01:00
|
|
|
|
for f in ('testy.jar', 'guardianproject.jar',):
|
|
|
|
|
sourcefile = os.path.join(sourcedir, f)
|
|
|
|
|
testfile = os.path.join(testsdir, f)
|
|
|
|
|
shutil.copy(sourcefile, testsdir)
|
2017-03-29 17:11:40 +02:00
|
|
|
|
fdroidserver.signindex.sign_jar(testfile)
|
2017-03-15 21:23:44 +01:00
|
|
|
|
# these should be resigned, and therefore different
|
|
|
|
|
self.assertNotEqual(open(sourcefile, 'rb').read(), open(testfile, 'rb').read())
|
|
|
|
|
|
2017-01-09 17:35:58 +01:00
|
|
|
|
def test_verify_apk_signature(self):
|
|
|
|
|
fdroidserver.common.config = None
|
|
|
|
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
|
|
|
|
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
|
|
|
|
|
self.assertTrue(fdroidserver.common.verify_apk_signature('urzip.apk'))
|
|
|
|
|
self.assertFalse(fdroidserver.common.verify_apk_signature('urzip-badcert.apk'))
|
|
|
|
|
self.assertFalse(fdroidserver.common.verify_apk_signature('urzip-badsig.apk'))
|
|
|
|
|
self.assertTrue(fdroidserver.common.verify_apk_signature('urzip-release.apk'))
|
|
|
|
|
self.assertFalse(fdroidserver.common.verify_apk_signature('urzip-release-unsigned.apk'))
|
|
|
|
|
|
2016-12-19 16:54:32 +01:00
|
|
|
|
def test_verify_apks(self):
|
|
|
|
|
fdroidserver.common.config = None
|
|
|
|
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
|
|
|
|
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
|
2017-09-25 16:28:22 +02:00
|
|
|
|
sourceapk = os.path.join(self.basedir, 'urzip.apk')
|
2016-12-19 16:54:32 +01:00
|
|
|
|
|
2017-11-30 09:59:02 +01:00
|
|
|
|
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
2016-12-19 16:54:32 +01:00
|
|
|
|
print('testdir', testdir)
|
|
|
|
|
|
|
|
|
|
copyapk = os.path.join(testdir, 'urzip-copy.apk')
|
|
|
|
|
shutil.copy(sourceapk, copyapk)
|
|
|
|
|
self.assertTrue(fdroidserver.common.verify_apk_signature(copyapk))
|
2017-09-25 16:28:22 +02:00
|
|
|
|
self.assertIsNone(fdroidserver.common.verify_apks(sourceapk, copyapk, self.tmpdir))
|
2016-12-19 16:54:32 +01:00
|
|
|
|
|
|
|
|
|
unsignedapk = os.path.join(testdir, 'urzip-unsigned.apk')
|
|
|
|
|
with ZipFile(sourceapk, 'r') as apk:
|
|
|
|
|
with ZipFile(unsignedapk, 'w') as testapk:
|
|
|
|
|
for info in apk.infolist():
|
|
|
|
|
if not info.filename.startswith('META-INF/'):
|
|
|
|
|
testapk.writestr(info, apk.read(info.filename))
|
2017-09-25 16:28:22 +02:00
|
|
|
|
self.assertIsNone(fdroidserver.common.verify_apks(sourceapk, unsignedapk, self.tmpdir))
|
2016-12-19 16:54:32 +01:00
|
|
|
|
|
|
|
|
|
twosigapk = os.path.join(testdir, 'urzip-twosig.apk')
|
2017-09-25 16:28:22 +02:00
|
|
|
|
otherapk = ZipFile(os.path.join(self.basedir, 'urzip-release.apk'), 'r')
|
2016-12-19 16:54:32 +01:00
|
|
|
|
with ZipFile(sourceapk, 'r') as apk:
|
|
|
|
|
with ZipFile(twosigapk, 'w') as testapk:
|
|
|
|
|
for info in apk.infolist():
|
|
|
|
|
testapk.writestr(info, apk.read(info.filename))
|
|
|
|
|
if info.filename.startswith('META-INF/'):
|
|
|
|
|
testapk.writestr(info, otherapk.read(info.filename))
|
|
|
|
|
otherapk.close()
|
|
|
|
|
self.assertFalse(fdroidserver.common.verify_apk_signature(twosigapk))
|
2017-09-25 16:28:22 +02:00
|
|
|
|
self.assertIsNone(fdroidserver.common.verify_apks(sourceapk, twosigapk, self.tmpdir))
|
2016-12-19 16:54:32 +01:00
|
|
|
|
|
2017-04-02 12:08:01 +02:00
|
|
|
|
def test_write_to_config(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpPath:
|
|
|
|
|
cfgPath = os.path.join(tmpPath, 'config.py')
|
|
|
|
|
with open(cfgPath, 'w', encoding='utf-8') as f:
|
|
|
|
|
f.write(textwrap.dedent("""\
|
|
|
|
|
# abc
|
|
|
|
|
# test = 'example value'
|
|
|
|
|
default_me= '%%%'
|
|
|
|
|
|
|
|
|
|
# comment
|
|
|
|
|
do_not_touch = "good value"
|
|
|
|
|
default_me="!!!"
|
|
|
|
|
|
|
|
|
|
key="123" # inline"""))
|
|
|
|
|
|
|
|
|
|
cfg = {'key': '111', 'default_me_orig': 'orig'}
|
|
|
|
|
fdroidserver.common.write_to_config(cfg, 'key', config_file=cfgPath)
|
|
|
|
|
fdroidserver.common.write_to_config(cfg, 'default_me', config_file=cfgPath)
|
|
|
|
|
fdroidserver.common.write_to_config(cfg, 'test', value='test value', config_file=cfgPath)
|
|
|
|
|
fdroidserver.common.write_to_config(cfg, 'new_key', value='new', config_file=cfgPath)
|
|
|
|
|
|
|
|
|
|
with open(cfgPath, 'r', encoding='utf-8') as f:
|
|
|
|
|
self.assertEqual(f.read(), textwrap.dedent("""\
|
|
|
|
|
# abc
|
|
|
|
|
test = 'test value'
|
|
|
|
|
default_me = 'orig'
|
|
|
|
|
|
|
|
|
|
# comment
|
|
|
|
|
do_not_touch = "good value"
|
|
|
|
|
|
|
|
|
|
key = "111" # inline
|
|
|
|
|
|
|
|
|
|
new_key = "new"
|
|
|
|
|
"""))
|
|
|
|
|
|
|
|
|
|
def test_write_to_config_when_empty(self):
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpPath:
|
|
|
|
|
cfgPath = os.path.join(tmpPath, 'config.py')
|
|
|
|
|
with open(cfgPath, 'w') as f:
|
|
|
|
|
pass
|
|
|
|
|
fdroidserver.common.write_to_config({}, 'key', 'val', cfgPath)
|
|
|
|
|
with open(cfgPath, 'r', encoding='utf-8') as f:
|
|
|
|
|
self.assertEqual(f.read(), textwrap.dedent("""\
|
|
|
|
|
|
|
|
|
|
key = "val"
|
|
|
|
|
"""))
|
|
|
|
|
|
2017-05-31 23:02:28 +02:00
|
|
|
|
def test_apk_name_regex(self):
|
|
|
|
|
good = [
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_-123456.apk',
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_123456_abcdef0.apk',
|
|
|
|
|
'urzip_-123456.apk',
|
|
|
|
|
'a0_0.apk',
|
|
|
|
|
'Z0_0.apk',
|
|
|
|
|
'a0_0_abcdef0.apk',
|
|
|
|
|
'a_a_a_a_0_abcdef0.apk',
|
|
|
|
|
'a_____0.apk',
|
|
|
|
|
'a_____123456_abcdef0.apk',
|
|
|
|
|
'org.fdroid.fdroid_123456.apk',
|
|
|
|
|
# valid, but "_99999" is part of packageName rather than versionCode
|
|
|
|
|
'org.fdroid.fdroid_99999_123456.apk',
|
|
|
|
|
# should be valid, but I can't figure out the regex since \w includes digits
|
|
|
|
|
# 'πÇÇπÇÇ现代汉语通用字българскиعربي1234ö_0_123bafd.apk',
|
|
|
|
|
]
|
|
|
|
|
for name in good:
|
|
|
|
|
m = fdroidserver.common.APK_NAME_REGEX.match(name)
|
|
|
|
|
self.assertIsNotNone(m)
|
|
|
|
|
self.assertIn(m.group(2), ('-123456', '0', '123456'))
|
|
|
|
|
self.assertIn(m.group(3), ('abcdef0', None))
|
|
|
|
|
|
|
|
|
|
bad = [
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_123456_abcdefg.apk',
|
|
|
|
|
'urzip-_-198274.apk',
|
|
|
|
|
'urzip-_0_123bafd.apk',
|
|
|
|
|
'no spaces allowed_123.apk',
|
|
|
|
|
'0_0.apk',
|
|
|
|
|
'0_0_abcdef0.apk',
|
|
|
|
|
]
|
|
|
|
|
for name in bad:
|
|
|
|
|
self.assertIsNone(fdroidserver.common.APK_NAME_REGEX.match(name))
|
|
|
|
|
|
|
|
|
|
def test_standard_file_name_regex(self):
|
|
|
|
|
good = [
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_-123456.mp3',
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_123456.mov',
|
|
|
|
|
'Document_-123456.pdf',
|
|
|
|
|
'WTF_0.MOV',
|
|
|
|
|
'Z0_0.ebk',
|
|
|
|
|
'a_a_a_a_0.txt',
|
|
|
|
|
'org.fdroid.fdroid.privileged.ota_123456.zip',
|
|
|
|
|
'πÇÇπÇÇ现代汉语通用字българскиعربي1234ö_0.jpeg',
|
|
|
|
|
'a_____0.PNG',
|
|
|
|
|
# valid, but "_99999" is part of packageName rather than versionCode
|
|
|
|
|
'a_____99999_123456.zip',
|
|
|
|
|
'org.fdroid.fdroid_99999_123456.zip',
|
|
|
|
|
]
|
|
|
|
|
for name in good:
|
|
|
|
|
m = fdroidserver.common.STANDARD_FILE_NAME_REGEX.match(name)
|
|
|
|
|
self.assertIsNotNone(m)
|
|
|
|
|
self.assertIn(m.group(2), ('-123456', '0', '123456'))
|
|
|
|
|
|
|
|
|
|
bad = [
|
|
|
|
|
'urzipπÇÇπÇÇ现代汉语通用字българскиعربي1234ö_abcdefg.JPEG',
|
|
|
|
|
'urzip-_-198274.zip',
|
|
|
|
|
'urzip-_123bafd.pdf',
|
|
|
|
|
'no spaces allowed_123.foobar',
|
|
|
|
|
'a_____0.',
|
|
|
|
|
]
|
|
|
|
|
for name in bad:
|
|
|
|
|
self.assertIsNone(fdroidserver.common.STANDARD_FILE_NAME_REGEX.match(name))
|
|
|
|
|
|
2017-09-23 09:02:50 +02:00
|
|
|
|
def test_apk_signer_fingerprint(self):
|
|
|
|
|
|
|
|
|
|
# fingerprints fetched with: keytool -printcert -file ____.RSA
|
|
|
|
|
testapks = (('repo/obb.main.oldversion_1444412523.apk',
|
|
|
|
|
'818e469465f96b704e27be2fee4c63ab9f83ddf30e7a34c7371a4728d83b0bc1'),
|
|
|
|
|
('repo/obb.main.twoversions_1101613.apk',
|
|
|
|
|
'32a23624c201b949f085996ba5ed53d40f703aca4989476949cae891022e0ed6'),
|
|
|
|
|
('repo/obb.main.twoversions_1101617.apk',
|
|
|
|
|
'32a23624c201b949f085996ba5ed53d40f703aca4989476949cae891022e0ed6'))
|
|
|
|
|
|
|
|
|
|
for apkfile, keytoolcertfingerprint in testapks:
|
|
|
|
|
self.assertEqual(keytoolcertfingerprint,
|
|
|
|
|
fdroidserver.common.apk_signer_fingerprint(apkfile))
|
|
|
|
|
|
2017-09-23 09:36:22 +02:00
|
|
|
|
def test_apk_signer_fingerprint_short(self):
|
|
|
|
|
|
|
|
|
|
# fingerprints fetched with: keytool -printcert -file ____.RSA
|
|
|
|
|
testapks = (('repo/obb.main.oldversion_1444412523.apk', '818e469'),
|
|
|
|
|
('repo/obb.main.twoversions_1101613.apk', '32a2362'),
|
|
|
|
|
('repo/obb.main.twoversions_1101617.apk', '32a2362'))
|
|
|
|
|
|
|
|
|
|
for apkfile, keytoolcertfingerprint in testapks:
|
|
|
|
|
self.assertEqual(keytoolcertfingerprint,
|
|
|
|
|
fdroidserver.common.apk_signer_fingerprint_short(apkfile))
|
|
|
|
|
|
2017-11-30 21:10:41 +01:00
|
|
|
|
def test_sign_apk(self):
|
|
|
|
|
fdroidserver.common.config = None
|
|
|
|
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
|
|
|
|
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
|
config['keyalias'] = 'sova'
|
|
|
|
|
config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
|
|
|
|
|
config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
|
|
|
|
|
config['keystore'] = os.path.join(self.basedir, 'keystore.jks')
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
fdroidserver.signindex.config = config
|
|
|
|
|
|
|
|
|
|
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
|
|
|
|
|
unsigned = os.path.join(testdir, 'urzip-release-unsigned.apk')
|
|
|
|
|
signed = os.path.join(testdir, 'urzip-release.apk')
|
|
|
|
|
|
|
|
|
|
self.assertFalse(fdroidserver.common.verify_apk_signature(unsigned))
|
|
|
|
|
|
|
|
|
|
shutil.copy(os.path.join(self.basedir, 'urzip-release-unsigned.apk'), testdir)
|
|
|
|
|
fdroidserver.common.sign_apk(unsigned, signed, config['keyalias'])
|
|
|
|
|
self.assertTrue(os.path.isfile(signed))
|
|
|
|
|
self.assertFalse(os.path.isfile(unsigned))
|
|
|
|
|
self.assertTrue(fdroidserver.common.verify_apk_signature(signed))
|
|
|
|
|
|
2017-09-12 18:17:46 +02:00
|
|
|
|
def test_get_api_id_aapt(self):
|
|
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
self._set_build_tools()
|
|
|
|
|
config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
|
|
|
|
|
|
|
|
|
|
appid, vercode, vername = fdroidserver.common.get_apk_id_aapt('repo/obb.main.twoversions_1101613.apk')
|
|
|
|
|
self.assertEqual('obb.main.twoversions', appid)
|
|
|
|
|
self.assertEqual('1101613', vercode)
|
|
|
|
|
self.assertEqual('0.1', vername)
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
|
fdroidserver.common.get_apk_id_aapt('nope')
|
|
|
|
|
|
2017-11-30 21:10:41 +01:00
|
|
|
|
def test_get_minSdkVersion_aapt(self):
|
|
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
|
self._set_build_tools()
|
|
|
|
|
config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
|
|
|
|
|
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('bad-unicode-πÇÇ现代通用字-български-عربي1.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('org.bitbucket.tickytacky.mirrormirror_1.apk')
|
|
|
|
|
self.assertEqual(14, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('org.bitbucket.tickytacky.mirrormirror_2.apk')
|
|
|
|
|
self.assertEqual(14, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('org.bitbucket.tickytacky.mirrormirror_3.apk')
|
|
|
|
|
self.assertEqual(14, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('org.bitbucket.tickytacky.mirrormirror_4.apk')
|
|
|
|
|
self.assertEqual(14, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('org.dyndns.fules.ck_20.apk')
|
|
|
|
|
self.assertEqual(7, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('urzip.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('urzip-badcert.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('urzip-badsig.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('urzip-release.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('urzip-release-unsigned.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/com.politedroid_3.apk')
|
|
|
|
|
self.assertEqual(3, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/com.politedroid_4.apk')
|
|
|
|
|
self.assertEqual(3, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/com.politedroid_5.apk')
|
|
|
|
|
self.assertEqual(3, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/com.politedroid_6.apk')
|
|
|
|
|
self.assertEqual(14, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.main.oldversion_1444412523.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.mainpatch.current_1619_another-release-key.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.mainpatch.current_1619.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.main.twoversions_1101613.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.main.twoversions_1101615.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/obb.main.twoversions_1101617.apk')
|
|
|
|
|
self.assertEqual(4, minSdkVersion)
|
|
|
|
|
minSdkVersion = fdroidserver.common.get_minSdkVersion_aapt('repo/urzip-; Рахма́нинов, [rɐxˈmanʲɪnəf] سيرجي_رخمانينوف 谢尔盖·.apk')
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
|
fdroidserver.common.get_minSdkVersion_aapt('nope')
|
|
|
|
|
|
2017-09-23 13:10:52 +02:00
|
|
|
|
def test_apk_release_name(self):
|
|
|
|
|
appid, vercode, sigfp = fdroidserver.common.apk_parse_release_filename('com.serwylo.lexica_905.apk')
|
|
|
|
|
self.assertEqual(appid, 'com.serwylo.lexica')
|
|
|
|
|
self.assertEqual(vercode, '905')
|
|
|
|
|
self.assertEqual(sigfp, None)
|
|
|
|
|
|
|
|
|
|
appid, vercode, sigfp = fdroidserver.common.apk_parse_release_filename('com.serwylo.lexica_905_c82e0f6.apk')
|
|
|
|
|
self.assertEqual(appid, 'com.serwylo.lexica')
|
|
|
|
|
self.assertEqual(vercode, '905')
|
|
|
|
|
self.assertEqual(sigfp, 'c82e0f6')
|
|
|
|
|
|
|
|
|
|
appid, vercode, sigfp = fdroidserver.common.apk_parse_release_filename('beverly_hills-90210.apk')
|
|
|
|
|
self.assertEqual(appid, None)
|
|
|
|
|
self.assertEqual(vercode, None)
|
|
|
|
|
self.assertEqual(sigfp, None)
|
|
|
|
|
|
2017-07-04 10:53:16 +02:00
|
|
|
|
def test_metadata_find_developer_signature(self):
|
|
|
|
|
sig = fdroidserver.common.metadata_find_developer_signature('org.smssecure.smssecure')
|
|
|
|
|
self.assertEqual('b30bb971af0d134866e158ec748fcd553df97c150f58b0a963190bbafbeb0868', sig)
|
|
|
|
|
|
2017-11-30 11:12:18 +01:00
|
|
|
|
def test_parse_androidmanifests(self):
|
|
|
|
|
source_files_dir = os.path.join(os.path.dirname(__file__), 'source-files')
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
|
app.id = 'org.fdroid.fdroid'
|
|
|
|
|
paths = [
|
|
|
|
|
os.path.join(source_files_dir, 'fdroid', 'fdroidclient', 'AndroidManifest.xml'),
|
|
|
|
|
os.path.join(source_files_dir, 'fdroid', 'fdroidclient', 'build.gradle'),
|
|
|
|
|
]
|
|
|
|
|
for path in paths:
|
|
|
|
|
self.assertTrue(os.path.isfile(path))
|
2017-11-30 13:42:37 +01:00
|
|
|
|
self.assertEqual(('0.94-test', '940', 'org.fdroid.fdroid'),
|
2017-11-30 11:12:18 +01:00
|
|
|
|
fdroidserver.common.parse_androidmanifests(paths, app))
|
|
|
|
|
|
|
|
|
|
def test_parse_androidmanifests_with_flavor(self):
|
|
|
|
|
source_files_dir = os.path.join(os.path.dirname(__file__), 'source-files')
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
|
build.gradle = ['devVersion']
|
|
|
|
|
app.builds = [build]
|
|
|
|
|
app.id = 'org.fdroid.fdroid.dev'
|
|
|
|
|
paths = [
|
|
|
|
|
os.path.join(source_files_dir, 'fdroid', 'fdroidclient', 'AndroidManifest.xml'),
|
|
|
|
|
os.path.join(source_files_dir, 'fdroid', 'fdroidclient', 'build.gradle'),
|
|
|
|
|
]
|
|
|
|
|
for path in paths:
|
|
|
|
|
self.assertTrue(os.path.isfile(path))
|
|
|
|
|
self.assertEqual(('0.95-dev', '949', 'org.fdroid.fdroid.dev'),
|
|
|
|
|
fdroidserver.common.parse_androidmanifests(paths, app))
|
|
|
|
|
|
2017-11-30 13:42:37 +01:00
|
|
|
|
|
2014-12-08 22:55:14 +01: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(CommonTest))
|
2017-11-30 10:14:38 +01:00
|
|
|
|
unittest.main(failfast=False)
|