1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-03 17:50:11 +02:00

easy changes to black code format in test cases

This does not change areas of code that should be manually reformatted.
This commit is contained in:
Hans-Christoph Steiner 2021-06-07 11:49:21 +02:00
parent d95a3029a8
commit d05ff9db1d
No known key found for this signature in database
GPG Key ID: 3E177817BA1B9BFA
18 changed files with 1144 additions and 564 deletions

View File

@ -242,12 +242,15 @@ class BuildTest(unittest.TestCase):
os.mkdir('bin') os.mkdir('bin')
os.mkdir('gen') os.mkdir('gen')
with open('build.xml', 'w') as fp: with open('build.xml', 'w') as fp:
fp.write(textwrap.dedent( fp.write(
textwrap.dedent(
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?> """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="clean" name="mockapp"> <project basedir="." default="clean" name="mockapp">
<target name="release"/> <target name="release"/>
<target name="clean"/> <target name="clean"/>
</project>""")) </project>"""
)
)
def make_fake_apk(output, build): def make_fake_apk(output, build):
with open(build.output, 'w') as fp: with open(build.output, 'w') as fp:
@ -277,7 +280,9 @@ class BuildTest(unittest.TestCase):
self.assertFalse(os.path.exists('gradle-wrapper.jar')) self.assertFalse(os.path.exists('gradle-wrapper.jar'))
def test_scan_with_extlib(self): def test_scan_with_extlib(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir("build") os.mkdir("build")
@ -293,9 +298,11 @@ class BuildTest(unittest.TestCase):
os.makedirs("extlib/android") os.makedirs("extlib/android")
# write a fake binary jar file the scanner should definitely error on # write a fake binary jar file the scanner should definitely error on
with open('extlib/android/android-support-v4r11.jar', 'wb') as file: with open('extlib/android/android-support-v4r11.jar', 'wb') as file:
file.write(b'PK\x03\x04\x14\x00\x08\x00\x08\x00-\x0eiA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\x00\x04\x00META-INF/\xfe\xca\x00\x00\x03\x00PK\x07\x08\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00') file.write(
b'PK\x03\x04\x14\x00\x08\x00\x08\x00-\x0eiA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\x00\x04\x00META-INF/\xfe\xca\x00\x00\x03\x00PK\x07\x08\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'
)
class FakeVcs(): class FakeVcs:
# no need to change to the correct commit here # no need to change to the correct commit here
def gotorevision(self, rev, refresh=True): def gotorevision(self, rev, refresh=True):
pass pass
@ -307,15 +314,17 @@ class BuildTest(unittest.TestCase):
build.extlibs = [] build.extlibs = []
os.makedirs('build/libs') os.makedirs('build/libs')
shutil.copy('extlib/android/android-support-v4r11.jar', 'build/libs') shutil.copy('extlib/android/android-support-v4r11.jar', 'build/libs')
fdroidserver.common.prepare_source(FakeVcs(), app, build, fdroidserver.common.prepare_source(
"build", "ignore", "extlib") FakeVcs(), app, build, "build", "ignore", "extlib"
)
count = fdroidserver.scanner.scan_source("build", build) count = fdroidserver.scanner.scan_source("build", build)
self.assertEqual(1, count, "Should produce a scanner error without extlib") self.assertEqual(1, count, "Should produce a scanner error without extlib")
# Now try again as an extlib # Now try again as an extlib
build.extlibs = ['android/android-support-v4r11.jar'] build.extlibs = ['android/android-support-v4r11.jar']
fdroidserver.common.prepare_source(FakeVcs(), app, build, fdroidserver.common.prepare_source(
"build", "ignore", "extlib") FakeVcs(), app, build, "build", "ignore", "extlib"
)
count = fdroidserver.scanner.scan_source("build", build) count = fdroidserver.scanner.scan_source("build", build)
self.assertEqual(0, count, "Shouldn't error on jar from extlib") self.assertEqual(0, count, "Shouldn't error on jar from extlib")
@ -335,25 +344,31 @@ class BuildTest(unittest.TestCase):
os.mkdir('metadata') os.mkdir('metadata')
appid = 'info.guardianproject.checkey' appid = 'info.guardianproject.checkey'
metadata_file = os.path.join('metadata', appid + '.yml') metadata_file = os.path.join('metadata', appid + '.yml')
shutil.copy(os.path.join(self.basedir, metadata_file), shutil.copy(os.path.join(self.basedir, metadata_file), 'metadata')
'metadata')
with open(metadata_file) as fp: with open(metadata_file) as fp:
app = fdroidserver.metadata.App(yaml.safe_load(fp)) app = fdroidserver.metadata.App(yaml.safe_load(fp))
app['RepoType'] = 'git' app['RepoType'] = 'git'
app['Binaries'] = 'https://example.com/fdroid/repo/info.guardianproject.checkey_%v.apk' app[
build = fdroidserver.metadata.Build({ 'Binaries'
] = 'https://example.com/fdroid/repo/info.guardianproject.checkey_%v.apk'
build = fdroidserver.metadata.Build(
{
'versionCode': 123, 'versionCode': 123,
'versionName': '1.2.3', 'versionName': '1.2.3',
'commit': '1.2.3', 'commit': '1.2.3',
'disable': False, 'disable': False,
}) }
)
app['Builds'] = [build] app['Builds'] = [build]
fdroidserver.metadata.write_metadata(metadata_file, app) fdroidserver.metadata.write_metadata(metadata_file, app)
os.makedirs(os.path.join('unsigned', 'binaries')) os.makedirs(os.path.join('unsigned', 'binaries'))
production_result = os.path.join('unsigned', '%s_%d.apk' % (appid, build['versionCode'])) production_result = os.path.join(
production_compare_file = os.path.join('unsigned', 'binaries', 'unsigned', '%s_%d.apk' % (appid, build['versionCode'])
'%s_%d.binary.apk' % (appid, build['versionCode'])) )
production_compare_file = os.path.join(
'unsigned', 'binaries', '%s_%d.binary.apk' % (appid, build['versionCode'])
)
os.makedirs(os.path.join('tmp', 'binaries')) os.makedirs(os.path.join('tmp', 'binaries'))
test_result = os.path.join('tmp', '%s_%d.apk' % (appid, build['versionCode'])) test_result = os.path.join('tmp', '%s_%d.apk' % (appid, build['versionCode']))
test_compare_file = os.path.join( test_compare_file = os.path.join(
@ -385,7 +400,9 @@ class BuildTest(unittest.TestCase):
# failed comparison # failed comparison
open(production_result, 'w').close() open(production_result, 'w').close()
open(production_compare_file, 'w').close() open(production_compare_file, 'w').close()
with mock.patch('fdroidserver.common.verify_apks', lambda *args: 'failed'): with mock.patch(
'fdroidserver.common.verify_apks', lambda *args: 'failed'
):
fdroidserver.build.main() fdroidserver.build.main()
self.assertFalse(os.path.exists(production_result)) self.assertFalse(os.path.exists(production_result))
self.assertFalse(os.path.exists(production_compare_file)) self.assertFalse(os.path.exists(production_compare_file))
@ -403,7 +420,9 @@ class BuildTest(unittest.TestCase):
# failed comparison # failed comparison
open(test_result, 'w').close() open(test_result, 'w').close()
open(test_compare_file, 'w').close() open(test_compare_file, 'w').close()
with mock.patch('fdroidserver.common.verify_apks', lambda *args: 'failed'): with mock.patch(
'fdroidserver.common.verify_apks', lambda *args: 'failed'
):
fdroidserver.build.main() fdroidserver.build.main()
self.assertTrue(os.path.exists(test_result)) self.assertTrue(os.path.exists(test_result))
self.assertFalse(os.path.exists(test_compare_file)) self.assertFalse(os.path.exists(test_compare_file))
@ -415,8 +434,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -12,7 +12,8 @@ from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -190,8 +191,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -25,7 +25,8 @@ from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -60,9 +61,17 @@ class CommonTest(unittest.TestCase):
os.environ['ANDROID_HOME'] = self.android_home os.environ['ANDROID_HOME'] = self.android_home
def test_parse_human_readable_size(self): def test_parse_human_readable_size(self):
for k, v in ((9827, 9827), (123.456, 123), ('123b', 123), ('1.2', 1), for k, v in (
('10.43 KiB', 10680), ('11GB', 11000000000), ('59kb', 59000), (9827, 9827),
('343.1 mb', 343100000), ('99.9GiB', 107266808217)): (123.456, 123),
('123b', 123),
('1.2', 1),
('10.43 KiB', 10680),
('11GB', 11000000000),
('59kb', 59000),
('343.1 mb', 343100000),
('99.9GiB', 107266808217),
):
self.assertEqual(fdroidserver.common.parse_human_readable_size(k), v) self.assertEqual(fdroidserver.common.parse_human_readable_size(k), v)
for v in ((12, 123), '0xfff', [], None, '12,123', '123GG', '982374bb', self): for v in ((12, 123), '0xfff', [], None, '12,123', '123GG', '982374bb', self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -74,21 +83,26 @@ class CommonTest(unittest.TestCase):
fdroidserver.common.assert_config_keystore({}) fdroidserver.common.assert_config_keystore({})
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
c = {'repo_keyalias': 'localhost', c = {
'repo_keyalias': 'localhost',
'keystore': 'keystore.jks', 'keystore': 'keystore.jks',
'keystorepass': '12345', 'keystorepass': '12345',
'keypass': '12345'} 'keypass': '12345',
}
with open('keystore.jks', 'w'): with open('keystore.jks', 'w'):
pass pass
fdroidserver.common.assert_config_keystore(c) fdroidserver.common.assert_config_keystore(c)
def _set_build_tools(self): def _set_build_tools(self):
build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools') build_tools = os.path.join(
fdroidserver.common.config['sdk_path'], 'build-tools'
)
if os.path.exists(build_tools): if os.path.exists(build_tools):
for f in sorted(os.listdir(build_tools), reverse=True): for f in sorted(os.listdir(build_tools), reverse=True):
versioned = os.path.join(build_tools, f) versioned = os.path.join(build_tools, f)
if os.path.isdir(versioned) \ if os.path.isdir(versioned) and os.path.isfile(
and os.path.isfile(os.path.join(versioned, 'apksigner')): os.path.join(versioned, 'apksigner')
):
break break
return True return True
else: else:
@ -121,20 +135,28 @@ class CommonTest(unittest.TestCase):
print('no build-tools found: ' + build_tools) print('no build-tools found: ' + build_tools)
def test_find_java_root_path(self): def test_find_java_root_path(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
all_pathlists = [ all_pathlists = [
([ # Debian (
[ # Debian
'/usr/lib/jvm/java-1.5.0-gcj-5-amd64', '/usr/lib/jvm/java-1.5.0-gcj-5-amd64',
'/usr/lib/jvm/java-8-openjdk-amd64', '/usr/lib/jvm/java-8-openjdk-amd64',
'/usr/lib/jvm/java-1.8.0-openjdk-amd64', '/usr/lib/jvm/java-1.8.0-openjdk-amd64',
], '/usr/lib/jvm/java-8-openjdk-amd64'), ],
([ # OSX '/usr/lib/jvm/java-8-openjdk-amd64',
),
(
[ # OSX
'/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk', '/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk',
'/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk', '/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk',
'/System/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk', '/System/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk',
], '/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk'), ],
'/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk',
),
] ]
for pathlist, choice in all_pathlists: for pathlist, choice in all_pathlists:
@ -166,8 +188,10 @@ class CommonTest(unittest.TestCase):
testfiles.append(os.path.join(self.basedir, 'urzip-badsig.apk')) testfiles.append(os.path.join(self.basedir, 'urzip-badsig.apk'))
testfiles.append(os.path.join(self.basedir, 'urzip-badcert.apk')) testfiles.append(os.path.join(self.basedir, 'urzip-badcert.apk'))
for apkfile in testfiles: for apkfile in testfiles:
self.assertTrue(fdroidserver.common.is_apk_and_debuggable(apkfile), self.assertTrue(
"debuggable APK state was not properly parsed!") fdroidserver.common.is_apk_and_debuggable(apkfile),
"debuggable APK state was not properly parsed!",
)
# these are set NOT debuggable # these are set NOT debuggable
testfiles = [] testfiles = []
@ -175,8 +199,10 @@ class CommonTest(unittest.TestCase):
testfiles.append(os.path.join(self.basedir, 'urzip-release-unsigned.apk')) testfiles.append(os.path.join(self.basedir, 'urzip-release-unsigned.apk'))
testfiles.append(os.path.join(self.basedir, 'v2.only.sig_2.apk')) testfiles.append(os.path.join(self.basedir, 'v2.only.sig_2.apk'))
for apkfile in testfiles: for apkfile in testfiles:
self.assertFalse(fdroidserver.common.is_apk_and_debuggable(apkfile), self.assertFalse(
"debuggable APK state was not properly parsed!") fdroidserver.common.is_apk_and_debuggable(apkfile),
"debuggable APK state was not properly parsed!",
)
VALID_STRICT_PACKAGE_NAMES = [ VALID_STRICT_PACKAGE_NAMES = [
"An.stop", "An.stop",
@ -204,23 +230,33 @@ class CommonTest(unittest.TestCase):
def test_is_valid_package_name(self): def test_is_valid_package_name(self):
for name in self.VALID_STRICT_PACKAGE_NAMES + [ for name in self.VALID_STRICT_PACKAGE_NAMES + [
"_SpeedoMeterApp.main", "_SpeedoMeterApp.main",
"05041684efd9b16c2888b1eddbadd0359f655f311b89bdd1737f560a10d20fb8"]: "05041684efd9b16c2888b1eddbadd0359f655f311b89bdd1737f560a10d20fb8",
self.assertTrue(fdroidserver.common.is_valid_package_name(name), ]:
"{0} should be a valid package name".format(name)) self.assertTrue(
for name in ["0rg.fdroid.fdroid", fdroidserver.common.is_valid_package_name(name),
"{0} should be a valid package name".format(name),
)
for name in [
"0rg.fdroid.fdroid",
".f_droid.fdr0ID", ".f_droid.fdr0ID",
"trailingdot.", "trailingdot.",
"org.fdroid/fdroid", "org.fdroid/fdroid",
"/org.fdroid.fdroid"]: "/org.fdroid.fdroid",
self.assertFalse(fdroidserver.common.is_valid_package_name(name), ]:
"{0} should not be a valid package name".format(name)) self.assertFalse(
fdroidserver.common.is_valid_package_name(name),
"{0} should not be a valid package name".format(name),
)
def test_is_strict_application_id(self): def test_is_strict_application_id(self):
"""see also tests/valid-package-names/""" """see also tests/valid-package-names/"""
for name in self.VALID_STRICT_PACKAGE_NAMES: for name in self.VALID_STRICT_PACKAGE_NAMES:
self.assertTrue(fdroidserver.common.is_strict_application_id(name), self.assertTrue(
"{0} should be a strict application id".format(name)) fdroidserver.common.is_strict_application_id(name),
for name in ["0rg.fdroid.fdroid", "{0} should be a strict application id".format(name),
)
for name in [
"0rg.fdroid.fdroid",
".f_droid.fdr0ID", ".f_droid.fdr0ID",
"oneword", "oneword",
"trailingdot.", "trailingdot.",
@ -228,19 +264,28 @@ class CommonTest(unittest.TestCase):
"org.fdroid/fdroid", "org.fdroid/fdroid",
"/org.fdroid.fdroid", "/org.fdroid.fdroid",
"_SpeedoMeterApp.main", "_SpeedoMeterApp.main",
"05041684efd9b16c2888b1eddbadd0359f655f311b89bdd1737f560a10d20fb8"]: "05041684efd9b16c2888b1eddbadd0359f655f311b89bdd1737f560a10d20fb8",
self.assertFalse(fdroidserver.common.is_strict_application_id(name), ]:
"{0} should not be a strict application id".format(name)) self.assertFalse(
fdroidserver.common.is_strict_application_id(name),
"{0} should not be a strict application id".format(name),
)
def test_prepare_sources(self): def test_prepare_sources(self):
testint = 99999999 testint = 99999999
teststr = 'FAKE_STR_FOR_TESTING' teststr = 'FAKE_STR_FOR_TESTING'
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
shutil.copytree(os.path.join(self.basedir, 'source-files'), prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
os.path.join(testdir, 'source-files')) )
shutil.copytree(
os.path.join(self.basedir, 'source-files'),
os.path.join(testdir, 'source-files'),
)
fdroidclient_testdir = os.path.join(testdir, 'source-files', 'fdroid', 'fdroidclient') fdroidclient_testdir = os.path.join(
testdir, 'source-files', 'fdroid', 'fdroidclient'
)
config = dict() config = dict()
config['sdk_path'] = os.getenv('ANDROID_HOME') config['sdk_path'] = os.getenv('ANDROID_HOME')
@ -257,7 +302,7 @@ class CommonTest(unittest.TestCase):
build.versionName = teststr build.versionName = teststr
build.versionCode = testint build.versionCode = testint
class FakeVcs(): class FakeVcs:
# no need to change to the correct commit here # no need to change to the correct commit here
def gotorevision(self, rev, refresh=True): def gotorevision(self, rev, refresh=True):
pass pass
@ -271,19 +316,29 @@ class CommonTest(unittest.TestCase):
with open(os.path.join(fdroidclient_testdir, 'build.gradle'), 'r') as f: with open(os.path.join(fdroidclient_testdir, 'build.gradle'), 'r') as f:
filedata = f.read() filedata = f.read()
self.assertIsNotNone(re.search(r"\s+compileSdkVersion %s\s+" % testint, filedata)) self.assertIsNotNone(
re.search(r"\s+compileSdkVersion %s\s+" % testint, filedata)
)
with open(os.path.join(fdroidclient_testdir, 'AndroidManifest.xml')) as f: with open(os.path.join(fdroidclient_testdir, 'AndroidManifest.xml')) as f:
filedata = f.read() filedata = f.read()
self.assertIsNone(re.search('android:debuggable', filedata)) self.assertIsNone(re.search('android:debuggable', filedata))
self.assertIsNotNone(re.search('android:versionName="%s"' % build.versionName, filedata)) self.assertIsNotNone(
self.assertIsNotNone(re.search('android:versionCode="%s"' % build.versionCode, filedata)) re.search('android:versionName="%s"' % build.versionName, filedata)
)
self.assertIsNotNone(
re.search('android:versionCode="%s"' % build.versionCode, filedata)
)
def test_prepare_sources_with_prebuild_subdir(self): def test_prepare_sources_with_prebuild_subdir(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
app_build_dir = os.path.join(testdir, 'build', 'com.example') app_build_dir = os.path.join(testdir, 'build', 'com.example')
shutil.copytree(os.path.join(self.basedir, 'source-files', 'fdroid', 'fdroidclient'), shutil.copytree(
app_build_dir) os.path.join(self.basedir, 'source-files', 'fdroid', 'fdroidclient'),
app_build_dir,
)
subdir = 'baz/bar' subdir = 'baz/bar'
subdir_path = os.path.join(app_build_dir, subdir) subdir_path = os.path.join(app_build_dir, subdir)
@ -315,10 +370,10 @@ class CommonTest(unittest.TestCase):
build.prebuild = 'test -d $$FakeSrcLib$$/testdirshouldexist' # actual test condition build.prebuild = 'test -d $$FakeSrcLib$$/testdirshouldexist' # actual test condition
build.srclibs = [srclibname + '@1.2.3'] build.srclibs = [srclibname + '@1.2.3']
build.subdir = subdir build.subdir = subdir
build.versionCode = 0xcafe build.versionCode = 0xCAFE
build.versionName = 'vCAFE' build.versionName = 'vCAFE'
class FakeVcs(): class FakeVcs:
# no need to change to the correct commit here # no need to change to the correct commit here
def gotorevision(self, rev, refresh=True): def gotorevision(self, rev, refresh=True):
pass pass
@ -333,7 +388,9 @@ class CommonTest(unittest.TestCase):
def test_prepare_sources_refresh(self): def test_prepare_sources_refresh(self):
packageName = 'org.fdroid.ci.test.app' packageName = 'org.fdroid.ci.test.app'
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
print('testdir', testdir) print('testdir', testdir)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('build') os.mkdir('build')
@ -379,14 +436,18 @@ class CommonTest(unittest.TestCase):
fdroidserver.signindex.config = config fdroidserver.signindex.config = config
sourcedir = os.path.join(self.basedir, 'signindex') sourcedir = os.path.join(self.basedir, 'signindex')
testsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testsdir = tempfile.mkdtemp(
for f in ('testy.jar', 'guardianproject.jar',): prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
for f in ('testy.jar', 'guardianproject.jar'):
sourcefile = os.path.join(sourcedir, f) sourcefile = os.path.join(sourcedir, f)
testfile = os.path.join(testsdir, f) testfile = os.path.join(testsdir, f)
shutil.copy(sourcefile, testsdir) shutil.copy(sourcefile, testsdir)
fdroidserver.signindex.sign_jar(testfile) fdroidserver.signindex.sign_jar(testfile)
# these should be resigned, and therefore different # these should be resigned, and therefore different
self.assertNotEqual(open(sourcefile, 'rb').read(), open(testfile, 'rb').read()) self.assertNotEqual(
open(sourcefile, 'rb').read(), open(testfile, 'rb').read()
)
def test_verify_apk_signature(self): def test_verify_apk_signature(self):
config = fdroidserver.common.read_config(fdroidserver.common.options) config = fdroidserver.common.read_config(fdroidserver.common.options)
@ -450,13 +511,17 @@ class CommonTest(unittest.TestCase):
sourceapk = os.path.join(self.basedir, 'urzip.apk') sourceapk = os.path.join(self.basedir, 'urzip.apk')
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
print('testdir', testdir) print('testdir', testdir)
copyapk = os.path.join(testdir, 'urzip-copy.apk') copyapk = os.path.join(testdir, 'urzip-copy.apk')
shutil.copy(sourceapk, copyapk) shutil.copy(sourceapk, copyapk)
self.assertTrue(fdroidserver.common.verify_apk_signature(copyapk)) self.assertTrue(fdroidserver.common.verify_apk_signature(copyapk))
self.assertIsNone(fdroidserver.common.verify_apks(sourceapk, copyapk, self.tmpdir)) self.assertIsNone(
fdroidserver.common.verify_apks(sourceapk, copyapk, self.tmpdir)
)
unsignedapk = os.path.join(testdir, 'urzip-unsigned.apk') unsignedapk = os.path.join(testdir, 'urzip-unsigned.apk')
with ZipFile(sourceapk, 'r') as apk: with ZipFile(sourceapk, 'r') as apk:
@ -464,7 +529,9 @@ class CommonTest(unittest.TestCase):
for info in apk.infolist(): for info in apk.infolist():
if not info.filename.startswith('META-INF/'): if not info.filename.startswith('META-INF/'):
testapk.writestr(info, apk.read(info.filename)) testapk.writestr(info, apk.read(info.filename))
self.assertIsNone(fdroidserver.common.verify_apks(sourceapk, unsignedapk, self.tmpdir)) self.assertIsNone(
fdroidserver.common.verify_apks(sourceapk, unsignedapk, self.tmpdir)
)
twosigapk = os.path.join(testdir, 'urzip-twosig.apk') twosigapk = os.path.join(testdir, 'urzip-twosig.apk')
otherapk = ZipFile(os.path.join(self.basedir, 'urzip-release.apk'), 'r') otherapk = ZipFile(os.path.join(self.basedir, 'urzip-release.apk'), 'r')
@ -482,7 +549,9 @@ class CommonTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpPath: with tempfile.TemporaryDirectory() as tmpPath:
cfgPath = os.path.join(tmpPath, 'config.py') cfgPath = os.path.join(tmpPath, 'config.py')
with open(cfgPath, 'w') as f: with open(cfgPath, 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
# abc # abc
# test = 'example value' # test = 'example value'
default_me= '%%%' default_me= '%%%'
@ -491,7 +560,9 @@ class CommonTest(unittest.TestCase):
do_not_touch = "good value" do_not_touch = "good value"
default_me="!!!" default_me="!!!"
key="123" # inline""")) key="123" # inline"""
)
)
cfg = {'key': '111', 'default_me_orig': 'orig'} cfg = {'key': '111', 'default_me_orig': 'orig'}
fdroidserver.common.write_to_config(cfg, 'key', config_file=cfgPath) fdroidserver.common.write_to_config(cfg, 'key', config_file=cfgPath)
@ -500,7 +571,10 @@ class CommonTest(unittest.TestCase):
fdroidserver.common.write_to_config(cfg, 'new_key', value='new', config_file=cfgPath) fdroidserver.common.write_to_config(cfg, 'new_key', value='new', config_file=cfgPath)
with open(cfgPath, 'r') as f: with open(cfgPath, 'r') as f:
self.assertEqual(f.read(), textwrap.dedent("""\ self.assertEqual(
f.read(),
textwrap.dedent(
"""\
# abc # abc
test = 'test value' test = 'test value'
default_me = 'orig' default_me = 'orig'
@ -511,7 +585,9 @@ class CommonTest(unittest.TestCase):
key = "111" # inline key = "111" # inline
new_key = "new" new_key = "new"
""")) """
),
)
def test_write_to_config_when_empty(self): def test_write_to_config_when_empty(self):
with tempfile.TemporaryDirectory() as tmpPath: with tempfile.TemporaryDirectory() as tmpPath:
@ -520,10 +596,15 @@ class CommonTest(unittest.TestCase):
pass pass
fdroidserver.common.write_to_config({}, 'key', 'val', cfgPath) fdroidserver.common.write_to_config({}, 'key', 'val', cfgPath)
with open(cfgPath, 'r') as f: with open(cfgPath, 'r') as f:
self.assertEqual(f.read(), textwrap.dedent("""\ self.assertEqual(
f.read(),
textwrap.dedent(
"""\
key = "val" key = "val"
""")) """
),
)
def test_apk_name_regex(self): def test_apk_name_regex(self):
good = [ good = [
@ -626,7 +707,9 @@ class CommonTest(unittest.TestCase):
def test_find_apksigner_config_overrides(self): def test_find_apksigner_config_overrides(self):
"""apksigner should come from config before any auto-detection""" """apksigner should come from config before any auto-detection"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
android_home = os.path.join(testdir, 'ANDROID_HOME') android_home = os.path.join(testdir, 'ANDROID_HOME')
do_not_use = os.path.join(android_home, 'build-tools', '30.0.3', 'apksigner') do_not_use = os.path.join(android_home, 'build-tools', '30.0.3', 'apksigner')
@ -643,7 +726,9 @@ class CommonTest(unittest.TestCase):
def test_find_apksigner_prefer_path(self): def test_find_apksigner_prefer_path(self):
"""apksigner should come from PATH before ANDROID_HOME""" """apksigner should come from PATH before ANDROID_HOME"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
apksigner = os.path.join(testdir, 'apksigner') apksigner = os.path.join(testdir, 'apksigner')
@ -666,7 +751,9 @@ class CommonTest(unittest.TestCase):
def test_find_apksigner_prefer_newest(self): def test_find_apksigner_prefer_newest(self):
"""apksigner should be the newest available in ANDROID_HOME""" """apksigner should be the newest available in ANDROID_HOME"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
android_home = os.path.join(testdir, 'ANDROID_HOME') android_home = os.path.join(testdir, 'ANDROID_HOME')
@ -688,7 +775,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual(apksigner, config.get('apksigner')) self.assertEqual(apksigner, config.get('apksigner'))
def test_find_apksigner_system_package_android_home(self): def test_find_apksigner_system_package_android_home(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
android_home = os.getenv('ANDROID_HOME') android_home = os.getenv('ANDROID_HOME')
if not android_home or not os.path.isdir(android_home): if not android_home or not os.path.isdir(android_home):
@ -716,7 +805,9 @@ class CommonTest(unittest.TestCase):
fdroidserver.common.config = config fdroidserver.common.config = config
fdroidserver.signindex.config = config fdroidserver.signindex.config = config
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
unsigned = os.path.join(testdir, 'urzip-release-unsigned.apk') unsigned = os.path.join(testdir, 'urzip-release-unsigned.apk')
signed = os.path.join(testdir, 'urzip-release.apk') signed = os.path.join(testdir, 'urzip-release.apk')
shutil.copy(os.path.join(self.basedir, 'urzip-release-unsigned.apk'), testdir) shutil.copy(os.path.join(self.basedir, 'urzip-release-unsigned.apk'), testdir)
@ -731,8 +822,10 @@ class CommonTest(unittest.TestCase):
# now sign an APK with minSdkVersion >= 18 # now sign an APK with minSdkVersion >= 18
unsigned = os.path.join(testdir, 'duplicate.permisssions_9999999-unsigned.apk') unsigned = os.path.join(testdir, 'duplicate.permisssions_9999999-unsigned.apk')
signed = os.path.join(testdir, 'duplicate.permisssions_9999999.apk') signed = os.path.join(testdir, 'duplicate.permisssions_9999999.apk')
shutil.copy(os.path.join(self.basedir, 'repo', 'duplicate.permisssions_9999999.apk'), shutil.copy(
os.path.join(unsigned)) os.path.join(self.basedir, 'repo', 'duplicate.permisssions_9999999.apk'),
os.path.join(unsigned),
)
fdroidserver.common.apk_strip_v1_signatures(unsigned, strip_manifest=True) fdroidserver.common.apk_strip_v1_signatures(unsigned, strip_manifest=True)
fdroidserver.common.sign_apk(unsigned, signed, config['keyalias']) fdroidserver.common.sign_apk(unsigned, signed, config['keyalias'])
self.assertTrue(os.path.isfile(signed)) self.assertTrue(os.path.isfile(signed))
@ -1158,7 +1251,12 @@ class CommonTest(unittest.TestCase):
'org.tasks': 'app', 'org.tasks': 'app',
'ut.ewh.audiometrytest': 'app', 'ut.ewh.audiometrytest': 'app',
} }
for f in ('cn.wildfirechat.chat', 'com.anpmech.launcher', 'org.tasks', 'ut.ewh.audiometrytest'): for f in (
'cn.wildfirechat.chat',
'com.anpmech.launcher',
'org.tasks',
'ut.ewh.audiometrytest',
):
build_dir = os.path.join('source-files', f) build_dir = os.path.join('source-files', f)
paths = fdroidserver.common.get_all_gradle_and_manifests(build_dir) paths = fdroidserver.common.get_all_gradle_and_manifests(build_dir)
logging.info(paths) logging.info(paths)
@ -1182,21 +1280,27 @@ class CommonTest(unittest.TestCase):
self.assertEqual(fdroidserver.common.parse_srclib_spec('@multi@at-signs@')) self.assertEqual(fdroidserver.common.parse_srclib_spec('@multi@at-signs@'))
def test_bad_urls(self): def test_bad_urls(self):
for url in ('asdf', for url in (
'asdf',
'file://thing.git', 'file://thing.git',
'https:///github.com/my/project', 'https:///github.com/my/project',
'git:///so/many/slashes', 'git:///so/many/slashes',
'ssh:/notabug.org/missing/a/slash', 'ssh:/notabug.org/missing/a/slash',
'git:notabug.org/missing/some/slashes', 'git:notabug.org/missing/some/slashes',
'https//github.com/bar/baz'): 'https//github.com/bar/baz',
):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
fdroidserver.common.get_app_from_url(url) fdroidserver.common.get_app_from_url(url)
def test_remove_signing_keys(self): def test_remove_signing_keys(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
print(testdir) print(testdir)
shutil.copytree(os.path.join(self.basedir, 'source-files'), shutil.copytree(
os.path.join(testdir, 'source-files')) os.path.join(self.basedir, 'source-files'),
os.path.join(testdir, 'source-files'),
)
os.chdir(testdir) os.chdir(testdir)
with_signingConfigs = [ with_signingConfigs = [
'source-files/com.seafile.seadroid2/app/build.gradle', 'source-files/com.seafile.seadroid2/app/build.gradle',
@ -1279,11 +1383,16 @@ class CommonTest(unittest.TestCase):
def test_deploy_build_log_with_rsync_with_id_file(self): def test_deploy_build_log_with_rsync_with_id_file(self):
mocklogcontent = bytes(textwrap.dedent("""\ mocklogcontent = bytes(
textwrap.dedent(
"""\
build started build started
building... building...
build completed build completed
profit!"""), 'utf-8') profit!"""
),
'utf-8',
)
fdroidserver.common.options = mock.Mock() fdroidserver.common.options = mock.Mock()
fdroidserver.common.options.verbose = False fdroidserver.common.options.verbose = False
@ -1291,7 +1400,8 @@ class CommonTest(unittest.TestCase):
fdroidserver.common.config = {} fdroidserver.common.config = {}
fdroidserver.common.config['serverwebroot'] = [ fdroidserver.common.config['serverwebroot'] = [
'example.com:/var/www/fdroid/', 'example.com:/var/www/fdroid/',
'example.com:/var/www/fbot/'] 'example.com:/var/www/fbot/',
]
fdroidserver.common.config['deploy_process_logs'] = True fdroidserver.common.config['deploy_process_logs'] = True
fdroidserver.common.config['identity_file'] = 'ssh/id_rsa' fdroidserver.common.config['identity_file'] = 'ssh/id_rsa'
@ -1344,7 +1454,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual(f.read(), mocklogcontent) self.assertEqual(f.read(), mocklogcontent)
def test_deploy_status_json(self): def test_deploy_status_json(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
fakesubcommand = 'fakesubcommand' fakesubcommand = 'fakesubcommand'
fake_timestamp = 1234567890 fake_timestamp = 1234567890
@ -1468,42 +1580,54 @@ class CommonTest(unittest.TestCase):
def test_run_yamllint_wellformed(self): def test_run_yamllint_wellformed(self):
try: try:
import yamllint.config import yamllint.config
yamllint.config # make pyflakes ignore this yamllint.config # make pyflakes ignore this
except ImportError: except ImportError:
self.skipTest('yamllint not installed') self.skipTest('yamllint not installed')
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('wellformed.yml', 'w') as f: with open('wellformed.yml', 'w') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
yaml: yaml:
file: file:
- for - for
- test - test
purposeses: true purposeses: true
''')) '''
)
)
result = fdroidserver.common.run_yamllint('wellformed.yml') result = fdroidserver.common.run_yamllint('wellformed.yml')
self.assertEqual(result, '') self.assertEqual(result, '')
def test_run_yamllint_malformed(self): def test_run_yamllint_malformed(self):
try: try:
import yamllint.config import yamllint.config
yamllint.config # make pyflakes ignore this yamllint.config # make pyflakes ignore this
except ImportError: except ImportError:
self.skipTest('yamllint not installed') self.skipTest('yamllint not installed')
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('malformed.yml', 'w') as f: with open('malformed.yml', 'w') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
yaml: yaml:
- that - that
fails fails
- test - test
''')) '''
)
)
result = fdroidserver.common.run_yamllint('malformed.yml') result = fdroidserver.common.run_yamllint('malformed.yml')
self.assertIsNotNone(result) self.assertIsNotNone(result)
self.assertNotEqual(result, '') self.assertNotEqual(result, '')
def test_with_no_config(self): def test_with_no_config(self):
"""It should set defaults if no config file is found""" """It should set defaults if no config file is found"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
self.assertFalse(os.path.exists('config.yml')) self.assertFalse(os.path.exists('config.yml'))
self.assertFalse(os.path.exists('config.py')) self.assertFalse(os.path.exists('config.py'))
@ -1513,7 +1637,9 @@ class CommonTest(unittest.TestCase):
def test_with_config_yml(self): def test_with_config_yml(self):
"""Make sure it is possible to use config.yml alone.""" """Make sure it is possible to use config.yml alone."""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('apksigner: yml') fp.write('apksigner: yml')
@ -1524,7 +1650,9 @@ class CommonTest(unittest.TestCase):
def test_with_config_yml_with_env_var(self): def test_with_config_yml_with_env_var(self):
"""Make sure it is possible to use config.yml alone.""" """Make sure it is possible to use config.yml alone."""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.environ['SECRET'] = 'mysecretpassword' os.environ['SECRET'] = 'mysecretpassword'
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
@ -1536,7 +1664,9 @@ class CommonTest(unittest.TestCase):
def test_with_config_py(self): def test_with_config_py(self):
"""Make sure it is still possible to use config.py alone.""" """Make sure it is still possible to use config.py alone."""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.py', 'w') as fp: with open('config.py', 'w') as fp:
fp.write('apksigner = "py"') fp.write('apksigner = "py"')
@ -1547,7 +1677,9 @@ class CommonTest(unittest.TestCase):
def test_config_perm_warning(self): def test_config_perm_warning(self):
"""Exercise the code path that issues a warning about unsafe permissions.""" """Exercise the code path that issues a warning about unsafe permissions."""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('keystore: foo.jks') fp.write('keystore: foo.jks')
@ -1565,7 +1697,9 @@ class CommonTest(unittest.TestCase):
def test_with_both_config_yml_py(self): def test_with_both_config_yml_py(self):
"""If config.yml and config.py are present, config.py should be ignored.""" """If config.yml and config.py are present, config.py should be ignored."""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('apksigner: yml') fp.write('apksigner: yml')
@ -1577,7 +1711,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual('yml', config.get('apksigner')) self.assertEqual('yml', config.get('apksigner'))
def test_write_to_config_yml(self): def test_write_to_config_yml(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('apksigner: yml') fp.write('apksigner: yml')
@ -1594,7 +1730,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual('mysecretpassword', config['keypass']) self.assertEqual('mysecretpassword', config['keypass'])
def test_write_to_config_py(self): def test_write_to_config_py(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.py', 'w') as fp: with open('config.py', 'w') as fp:
fp.write('apksigner = "py"') fp.write('apksigner = "py"')
@ -1609,7 +1747,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual('mysecretpassword', config['keypass']) self.assertEqual('mysecretpassword', config['keypass'])
def test_config_dict_with_int_keys(self): def test_config_dict_with_int_keys(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('java_paths:\n 8: /usr/lib/jvm/java-8-openjdk\n') fp.write('java_paths:\n 8: /usr/lib/jvm/java-8-openjdk\n')
@ -1646,7 +1786,9 @@ class CommonTest(unittest.TestCase):
self.assertEqual(subcommand, data['subcommand']) self.assertEqual(subcommand, data['subcommand'])
def test_setup_status_output_in_git_repo(self): def test_setup_status_output_in_git_repo(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
logging.getLogger('git.cmd').setLevel(logging.INFO) logging.getLogger('git.cmd').setLevel(logging.INFO)
@ -1722,25 +1864,25 @@ class CommonTest(unittest.TestCase):
allow_vercodes = False allow_vercodes = False
self.assertEqual( self.assertEqual(
{'org.fdroid.fdroid': []}, {'org.fdroid.fdroid': []},
fdroidserver.common.read_pkg_args(['org.fdroid.fdroid'], allow_vercodes) fdroidserver.common.read_pkg_args(['org.fdroid.fdroid'], allow_vercodes),
) )
self.assertNotEqual( self.assertNotEqual(
{'com.example': ['123456']}, {'com.example': ['123456']},
fdroidserver.common.read_pkg_args(['com.example:123456'], allow_vercodes) fdroidserver.common.read_pkg_args(['com.example:123456'], allow_vercodes),
) )
allow_vercodes = True allow_vercodes = True
self.assertEqual( self.assertEqual(
{'org.fdroid.fdroid': []}, {'org.fdroid.fdroid': []},
fdroidserver.common.read_pkg_args(['org.fdroid.fdroid'], allow_vercodes) fdroidserver.common.read_pkg_args(['org.fdroid.fdroid'], allow_vercodes),
) )
self.assertEqual( self.assertEqual(
{'com.example': ['123456']}, {'com.example': ['123456']},
fdroidserver.common.read_pkg_args(['com.example:123456'], allow_vercodes) fdroidserver.common.read_pkg_args(['com.example:123456'], allow_vercodes),
) )
self.assertEqual( self.assertEqual(
{'org.debian_kit': ['6']}, {'org.debian_kit': ['6']},
fdroidserver.common.read_pkg_args(['org.debian_kit_6.apk'], allow_vercodes) fdroidserver.common.read_pkg_args(['org.debian_kit_6.apk'], allow_vercodes),
) )
appid_versionCode_pairs = ( appid_versionCode_pairs = (
'org.fdroid.fdroid:1', 'org.fdroid.fdroid:1',
@ -1757,11 +1899,13 @@ class CommonTest(unittest.TestCase):
) )
self.assertEqual( self.assertEqual(
{'com.example': ['67890'], 'org.c_base.c_beam': ['29']}, {'com.example': ['67890'], 'org.c_base.c_beam': ['29']},
fdroidserver.common.read_pkg_args(appid_versionCode_pairs, allow_vercodes) fdroidserver.common.read_pkg_args(appid_versionCode_pairs, allow_vercodes),
) )
def test_apk_strip_v1_signatures(self): def test_apk_strip_v1_signatures(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
before = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk') before = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk')
after = os.path.join(testdir, 'after.apk') after = os.path.join(testdir, 'after.apk')
shutil.copy(before, after) shutil.copy(before, after)
@ -1780,9 +1924,9 @@ class CommonTest(unittest.TestCase):
os.path.join('metadata', appid, 'signatures', vc, '28969C09.RSA'), os.path.join('metadata', appid, 'signatures', vc, '28969C09.RSA'),
os.path.join('metadata', appid, 'signatures', vc, '28969C09.SF'), os.path.join('metadata', appid, 'signatures', vc, '28969C09.SF'),
os.path.join('metadata', appid, 'signatures', vc, 'MANIFEST.MF'), os.path.join('metadata', appid, 'signatures', vc, 'MANIFEST.MF'),
None None,
), ),
fdroidserver.common.metadata_find_developer_signing_files(appid, vc) fdroidserver.common.metadata_find_developer_signing_files(appid, vc),
) )
vc = '134' vc = '134'
@ -1791,9 +1935,9 @@ class CommonTest(unittest.TestCase):
os.path.join('metadata', appid, 'signatures', vc, '28969C09.RSA'), os.path.join('metadata', appid, 'signatures', vc, '28969C09.RSA'),
os.path.join('metadata', appid, 'signatures', vc, '28969C09.SF'), os.path.join('metadata', appid, 'signatures', vc, '28969C09.SF'),
os.path.join('metadata', appid, 'signatures', vc, 'MANIFEST.MF'), os.path.join('metadata', appid, 'signatures', vc, 'MANIFEST.MF'),
None None,
), ),
fdroidserver.common.metadata_find_developer_signing_files(appid, vc) fdroidserver.common.metadata_find_developer_signing_files(appid, vc),
) )
def test_auto_install_ndk(self): def test_auto_install_ndk(self):
@ -1883,8 +2027,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -10,7 +10,8 @@ import unittest
from unittest import mock from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -47,26 +48,42 @@ class DeployTest(unittest.TestCase):
def update_server_webroot_call(cmd): def update_server_webroot_call(cmd):
nonlocal call_iteration nonlocal call_iteration
if call_iteration == 0: if call_iteration == 0:
self.assertListEqual(cmd, ['rsync', self.assertListEqual(
cmd,
[
'rsync',
'--archive', '--archive',
'--delete-after', '--delete-after',
'--safe-links', '--safe-links',
'--quiet', '--quiet',
'--exclude', 'repo/index.xml', '--exclude',
'--exclude', 'repo/index.jar', 'repo/index.xml',
'--exclude', 'repo/index-v1.jar', '--exclude',
'repo/index.jar',
'--exclude',
'repo/index-v1.jar',
'repo', 'repo',
'example.com:/var/www/fdroid']) 'example.com:/var/www/fdroid',
],
)
elif call_iteration == 1: elif call_iteration == 1:
self.assertListEqual(cmd, ['rsync', self.assertListEqual(
cmd,
[
'rsync',
'--archive', '--archive',
'--delete-after', '--delete-after',
'--safe-links', '--safe-links',
'--quiet', '--quiet',
'repo', 'repo',
serverwebroot]) serverwebroot,
],
)
elif call_iteration == 2: elif call_iteration == 2:
self.assertListEqual(cmd, ['rsync', self.assertListEqual(
cmd,
[
'rsync',
'--archive', '--archive',
'--delete-after', '--delete-after',
'--safe-links', '--safe-links',
@ -74,7 +91,9 @@ class DeployTest(unittest.TestCase):
'Sym.apk', 'Sym.apk',
'Sym.apk.asc', 'Sym.apk.asc',
'Sym.apk.sig', 'Sym.apk.sig',
'example.com:/var/www/fdroid']) 'example.com:/var/www/fdroid',
],
)
else: else:
self.fail('unexpected subprocess.call invocation') self.fail('unexpected subprocess.call invocation')
call_iteration += 1 call_iteration += 1
@ -86,8 +105,7 @@ class DeployTest(unittest.TestCase):
os.symlink('repo/com.example.sym.apk.asc', 'Sym.apk.asc') os.symlink('repo/com.example.sym.apk.asc', 'Sym.apk.asc')
os.symlink('repo/com.example.sym.apk.sig', 'Sym.apk.sig') os.symlink('repo/com.example.sym.apk.sig', 'Sym.apk.sig')
with mock.patch('subprocess.call', side_effect=update_server_webroot_call): with mock.patch('subprocess.call', side_effect=update_server_webroot_call):
fdroidserver.deploy.update_serverwebroot(serverwebroot, fdroidserver.deploy.update_serverwebroot(serverwebroot, repo_section)
repo_section)
self.assertEqual(call_iteration, 3, 'expected 3 invocations of subprocess.call') self.assertEqual(call_iteration, 3, 'expected 3 invocations of subprocess.call')
def test_update_serverwebroot_with_id_file(self): def test_update_serverwebroot_with_id_file(self):
@ -108,7 +126,10 @@ class DeployTest(unittest.TestCase):
def update_server_webroot_call(cmd): def update_server_webroot_call(cmd):
nonlocal call_iteration nonlocal call_iteration
if call_iteration == 0: if call_iteration == 0:
self.assertListEqual(cmd, ['rsync', self.assertListEqual(
cmd,
[
'rsync',
'--archive', '--archive',
'--delete-after', '--delete-after',
'--safe-links', '--safe-links',
@ -116,13 +137,21 @@ class DeployTest(unittest.TestCase):
'-e', '-e',
'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i ' 'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i '
+ fdroidserver.deploy.config['identity_file'], + fdroidserver.deploy.config['identity_file'],
'--exclude', 'archive/index.xml', '--exclude',
'--exclude', 'archive/index.jar', 'archive/index.xml',
'--exclude', 'archive/index-v1.jar', '--exclude',
'archive/index.jar',
'--exclude',
'archive/index-v1.jar',
'archive', 'archive',
serverwebroot]) serverwebroot,
],
)
elif call_iteration == 1: elif call_iteration == 1:
self.assertListEqual(cmd, ['rsync', self.assertListEqual(
cmd,
[
'rsync',
'--archive', '--archive',
'--delete-after', '--delete-after',
'--safe-links', '--safe-links',
@ -131,18 +160,21 @@ class DeployTest(unittest.TestCase):
'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i ' 'ssh -oBatchMode=yes -oIdentitiesOnly=yes -i '
+ fdroidserver.deploy.config['identity_file'], + fdroidserver.deploy.config['identity_file'],
'archive', 'archive',
serverwebroot]) serverwebroot,
],
)
else: else:
self.fail('unexpected subprocess.call invocation') self.fail('unexpected subprocess.call invocation')
call_iteration += 1 call_iteration += 1
return 0 return 0
with mock.patch('subprocess.call', side_effect=update_server_webroot_call): with mock.patch('subprocess.call', side_effect=update_server_webroot_call):
fdroidserver.deploy.update_serverwebroot(serverwebroot, fdroidserver.deploy.update_serverwebroot(serverwebroot, repo_section)
repo_section)
self.assertEqual(call_iteration, 2, 'expected 2 invocations of subprocess.call') self.assertEqual(call_iteration, 2, 'expected 2 invocations of subprocess.call')
@unittest.skipIf(not os.getenv('VIRUSTOTAL_API_KEY'), 'VIRUSTOTAL_API_KEY is not set') @unittest.skipIf(
not os.getenv('VIRUSTOTAL_API_KEY'), 'VIRUSTOTAL_API_KEY is not set'
)
def test_upload_to_virustotal(self): def test_upload_to_virustotal(self):
fdroidserver.deploy.options.verbose = True fdroidserver.deploy.options.verbose = True
virustotal_apikey = os.getenv('VIRUSTOTAL_API_KEY') virustotal_apikey = os.getenv('VIRUSTOTAL_API_KEY')
@ -157,8 +189,7 @@ class DeployTest(unittest.TestCase):
('https://salsa.debian.org/foo/repo', 'salsa'), ('https://salsa.debian.org/foo/repo', 'salsa'),
): ):
self.assertEqual( self.assertEqual(
name, name, fdroidserver.deploy.REMOTE_HOSTNAME_REGEX.sub(r'\1', remote_url)
fdroidserver.deploy.REMOTE_HOSTNAME_REGEX.sub(r'\1', remote_url)
) )
@ -166,8 +197,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -9,7 +9,8 @@ import sys
import unittest import unittest
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -57,8 +58,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.exception.options, args) = parser.parse_args(['--verbose']) (fdroidserver.exception.options, args) = parser.parse_args(['--verbose'])
fdroidserver.common.options = fdroidserver.exception.options fdroidserver.common.options = fdroidserver.exception.options

View File

@ -14,13 +14,15 @@ import unittest
from unittest import mock from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
import fdroidserver.common import fdroidserver.common
import fdroidserver.metadata import fdroidserver.metadata
# work around the syntax error from: import fdroidserver.import # work around the syntax error from: import fdroidserver.import
import import_proxy import import_proxy
@ -55,19 +57,37 @@ class ImportTest(unittest.TestCase):
self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git') self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git')
def test_get_app_from_url(self): def test_get_app_from_url(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir(os.path.join(testdir, 'tmp')) os.mkdir(os.path.join(testdir, 'tmp'))
tmp_importer = os.path.join(testdir, 'tmp', 'importer') tmp_importer = os.path.join(testdir, 'tmp', 'importer')
data = ( data = (
('cn.wildfirechat.chat', 'https://github.com/wildfirechat/android-chat', '0.6.9', '23'), (
('com.anpmech.launcher', 'https://github.com/KeikaiLauncher/KeikaiLauncher', 'Unknown', None), 'cn.wildfirechat.chat',
('ut.ewh.audiometrytest', 'https://github.com/ReeceStevens/ut_ewh_audiometer_2014', '1.65', '14'), 'https://github.com/wildfirechat/android-chat',
'0.6.9',
'23',
),
(
'com.anpmech.launcher',
'https://github.com/KeikaiLauncher/KeikaiLauncher',
'Unknown',
None,
),
(
'ut.ewh.audiometrytest',
'https://github.com/ReeceStevens/ut_ewh_audiometer_2014',
'1.65',
'14',
),
) )
for appid, url, vn, vc in data: for appid, url, vn, vc in data:
shutil.rmtree(tmp_importer, ignore_errors=True) shutil.rmtree(tmp_importer, ignore_errors=True)
shutil.copytree(os.path.join(self.basedir, 'source-files', appid), shutil.copytree(
tmp_importer) os.path.join(self.basedir, 'source-files', appid), tmp_importer
)
app = fdroidserver.common.get_app_from_url(url) app = fdroidserver.common.get_app_from_url(url)
with mock.patch('fdroidserver.common.getvcs', with mock.patch('fdroidserver.common.getvcs',
@ -92,8 +112,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -15,7 +15,8 @@ import json
import shutil import shutil
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -37,7 +38,6 @@ class Options:
class IndexTest(unittest.TestCase): class IndexTest(unittest.TestCase):
def setUp(self): def setUp(self):
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
self.basedir = os.path.join(localmodule, 'tests') self.basedir = os.path.join(localmodule, 'tests')
@ -55,8 +55,9 @@ class IndexTest(unittest.TestCase):
fdroidserver.signindex.config = config fdroidserver.signindex.config = config
if not os.path.exists('repo/index-v1.jar'): if not os.path.exists('repo/index-v1.jar'):
fdroidserver.signindex.sign_index_v1(os.path.join(self.basedir, 'repo'), fdroidserver.signindex.sign_index_v1(
'index-v1.json') os.path.join(self.basedir, 'repo'), 'index-v1.json'
)
def test_get_public_key_from_jar_succeeds(self): def test_get_public_key_from_jar_succeeds(self):
source_dir = os.path.join(self.basedir, 'signindex') source_dir = os.path.join(self.basedir, 'signindex')
@ -66,9 +67,11 @@ class IndexTest(unittest.TestCase):
_, fingerprint = fdroidserver.index.get_public_key_from_jar(jar) _, fingerprint = fdroidserver.index.get_public_key_from_jar(jar)
# comparing fingerprints should be sufficient # comparing fingerprints should be sufficient
if f == 'testy.jar': if f == 'testy.jar':
self.assertEqual(fingerprint, self.assertEqual(
fingerprint,
'818E469465F96B704E27BE2FEE4C63AB' '818E469465F96B704E27BE2FEE4C63AB'
+ '9F83DDF30E7A34C7371A4728D83B0BC1') + '9F83DDF30E7A34C7371A4728D83B0BC1',
)
if f == 'guardianproject.jar': if f == 'guardianproject.jar':
self.assertTrue(fingerprint == GP_FINGERPRINT) self.assertTrue(fingerprint == GP_FINGERPRINT)
@ -85,12 +88,15 @@ class IndexTest(unittest.TestCase):
def test_download_repo_index_no_jar(self): def test_download_repo_index_no_jar(self):
with self.assertRaises(requests.exceptions.RequestException): with self.assertRaises(requests.exceptions.RequestException):
fdroidserver.index.download_repo_index("http://example.org?fingerprint=nope") fdroidserver.index.download_repo_index(
"http://example.org?fingerprint=nope"
)
def test_get_repo_key_fingerprint(self): def test_get_repo_key_fingerprint(self):
pubkey, fingerprint = fdroidserver.index.extract_pubkey() pubkey, fingerprint = fdroidserver.index.extract_pubkey()
data, public_key, public_key_fingerprint = \ data, public_key, public_key_fingerprint = fdroidserver.index.get_index_from_jar(
fdroidserver.index.get_index_from_jar('repo/index-v1.jar', fingerprint) 'repo/index-v1.jar', fingerprint
)
self.assertIsNotNone(data) self.assertIsNotNone(data)
self.assertIsNotNone(public_key) self.assertIsNotNone(public_key)
self.assertIsNotNone(public_key_fingerprint) self.assertIsNotNone(public_key_fingerprint)
@ -103,8 +109,9 @@ class IndexTest(unittest.TestCase):
def test_get_index_from_jar_with_chars_to_be_stripped(self): def test_get_index_from_jar_with_chars_to_be_stripped(self):
fingerprint = 'NOOOO F4 9A F3 F1 1E FD DF 20 DF FD 70 F5 E3 11 7B 99 76 67 41 67 AD CA 28 0E 6B 19 32 A0 60 1B 26 F6' fingerprint = 'NOOOO F4 9A F3 F1 1E FD DF 20 DF FD 70 F5 E3 11 7B 99 76 67 41 67 AD CA 28 0E 6B 19 32 A0 60 1B 26 F6'
data, public_key, public_key_fingerprint = \ data, public_key, public_key_fingerprint = fdroidserver.index.get_index_from_jar(
fdroidserver.index.get_index_from_jar('repo/index-v1.jar', fingerprint) 'repo/index-v1.jar', fingerprint
)
@patch('requests.head') @patch('requests.head')
def test_download_repo_index_same_etag(self, head): def test_download_repo_index_same_etag(self, head):
@ -243,13 +250,15 @@ class IndexTest(unittest.TestCase):
pass pass
fdroidserver.index.v1_sort_packages( fdroidserver.index.v1_sort_packages(
i, fdroidserver.common.load_stats_fdroid_signing_key_fingerprints()) i, fdroidserver.common.load_stats_fdroid_signing_key_fingerprints()
)
self.maxDiff = None self.maxDiff = None
self.assertEqual(json.dumps(i, indent=2), json.dumps(o, indent=2)) self.assertEqual(json.dumps(i, indent=2), json.dumps(o, indent=2))
def test_make_v0_repo_only(self): def test_make_v0_repo_only(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
os.mkdir('repo') os.mkdir('repo')
repo_icons_dir = os.path.join('repo', 'icons') repo_icons_dir = os.path.join('repo', 'icons')
@ -266,19 +275,24 @@ class IndexTest(unittest.TestCase):
fdroidserver.common.config['repo_pubkey'] = 'ffffffffffffffffffffffffffffffffff' fdroidserver.common.config['repo_pubkey'] = 'ffffffffffffffffffffffffffffffffff'
fdroidserver.index.make_v0({}, [], 'repo', repodict, requestsdict, {}) fdroidserver.index.make_v0({}, [], 'repo', repodict, requestsdict, {})
self.assertTrue(os.path.isdir(repo_icons_dir)) self.assertTrue(os.path.isdir(repo_icons_dir))
self.assertTrue(os.path.exists(os.path.join(repo_icons_dir, self.assertTrue(
fdroidserver.common.default_config['repo_icon']))) os.path.exists(
os.path.join(
repo_icons_dir, fdroidserver.common.default_config['repo_icon']
)
)
)
self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml'))) self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml')))
def test_make_v0(self): def test_make_v0(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
os.mkdir('metadata') os.mkdir('metadata')
os.mkdir('repo') os.mkdir('repo')
metadatafile = 'metadata/info.zwanenburg.caffeinetile.yml' metadatafile = 'metadata/info.zwanenburg.caffeinetile.yml'
shutil.copy(os.path.join(self.basedir, metadatafile), shutil.copy(os.path.join(self.basedir, metadatafile), metadatafile)
metadatafile)
repo_icons_dir = os.path.join('repo', 'icons') repo_icons_dir = os.path.join('repo', 'icons')
self.assertFalse(os.path.isdir(repo_icons_dir)) self.assertFalse(os.path.isdir(repo_icons_dir))
repodict = { repodict = {
@ -296,21 +310,14 @@ class IndexTest(unittest.TestCase):
apk = { apk = {
'hash': 'dbbdd7deadb038862f426b71efe4a64df8c3edf25d669e935f349510e16f65db', 'hash': 'dbbdd7deadb038862f426b71efe4a64df8c3edf25d669e935f349510e16f65db',
'hashType': 'sha256', 'hashType': 'sha256',
'uses-permission': [ 'uses-permission': [['android.permission.WAKE_LOCK', None]],
[
'android.permission.WAKE_LOCK',
None
]
],
'uses-permission-sdk-23': [], 'uses-permission-sdk-23': [],
'features': [], 'features': [],
'icons_src': { 'icons_src': {
'160': 'res/drawable/ic_coffee_on.xml', '160': 'res/drawable/ic_coffee_on.xml',
'-1': 'res/drawable/ic_coffee_on.xml' '-1': 'res/drawable/ic_coffee_on.xml',
},
'icons': {
'160': 'info.zwanenburg.caffeinetile.4.xml'
}, },
'icons': {'160': 'info.zwanenburg.caffeinetile.4.xml'},
'antiFeatures': [], 'antiFeatures': [],
'packageName': 'info.zwanenburg.caffeinetile', 'packageName': 'info.zwanenburg.caffeinetile',
'versionCode': 4, 'versionCode': 4,
@ -330,8 +337,13 @@ class IndexTest(unittest.TestCase):
fdroidserver.common.config['make_current_version_link'] = True fdroidserver.common.config['make_current_version_link'] = True
fdroidserver.index.make_v0(apps, [apk], 'repo', repodict, requestsdict, {}) fdroidserver.index.make_v0(apps, [apk], 'repo', repodict, requestsdict, {})
self.assertTrue(os.path.isdir(repo_icons_dir)) self.assertTrue(os.path.isdir(repo_icons_dir))
self.assertTrue(os.path.exists(os.path.join(repo_icons_dir, self.assertTrue(
fdroidserver.common.default_config['repo_icon']))) os.path.exists(
os.path.join(
repo_icons_dir, fdroidserver.common.default_config['repo_icon']
)
)
)
self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml'))) self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml')))
def test_get_mirror_service_urls(self): def test_get_mirror_service_urls(self):
@ -341,8 +353,10 @@ class IndexTest(unittest.TestCase):
'https://github.com/foo/bar', 'https://github.com/foo/bar',
'https://github.com/foo/bar.git', 'https://github.com/foo/bar.git',
]: ]:
self.assertEqual(['https://raw.githubusercontent.com/foo/bar/master/fdroid'], self.assertEqual(
fdroidserver.index.get_mirror_service_urls(url)) ['https://raw.githubusercontent.com/foo/bar/master/fdroid'],
fdroidserver.index.get_mirror_service_urls(url),
)
for url in [ for url in [
'git@gitlab.com:group/project', 'git@gitlab.com:group/project',
@ -350,13 +364,18 @@ class IndexTest(unittest.TestCase):
'https://gitlab.com/group/project', 'https://gitlab.com/group/project',
'https://gitlab.com/group/project.git', 'https://gitlab.com/group/project.git',
]: ]:
self.assertEqual(['https://group.gitlab.io/project/fdroid', self.assertEqual(
'https://gitlab.com/group/project/-/raw/master/fdroid'], [
fdroidserver.index.get_mirror_service_urls(url)) 'https://group.gitlab.io/project/fdroid',
'https://gitlab.com/group/project/-/raw/master/fdroid',
],
fdroidserver.index.get_mirror_service_urls(url),
)
def test_make_website(self): def test_make_website(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
os.mkdir('metadata') os.mkdir('metadata')
os.mkdir('repo') os.mkdir('repo')
@ -396,21 +415,31 @@ class IndexTest(unittest.TestCase):
self.assertEquals(css, pretty_css) self.assertEquals(css, pretty_css)
def test_v1_sort_packages_with_invalid(self): def test_v1_sort_packages_with_invalid(self):
i = [{'packageName': 'org.smssecure.smssecure', i = [
{
'packageName': 'org.smssecure.smssecure',
'apkName': 'smssecure-custom.fake', 'apkName': 'smssecure-custom.fake',
'signer': None, 'signer': None,
'versionCode': 11111}] 'versionCode': 11111,
}
]
fdroidserver.index.v1_sort_packages( fdroidserver.index.v1_sort_packages(
i, fdroidserver.common.load_stats_fdroid_signing_key_fingerprints()) i, fdroidserver.common.load_stats_fdroid_signing_key_fingerprints()
)
if __name__ == "__main__": if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
Options.verbose = options.verbose Options.verbose = options.verbose

View File

@ -13,7 +13,8 @@ import unittest
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -35,7 +36,9 @@ class InitTest(unittest.TestCase):
fdroidserver.init.config = None fdroidserver.init.config = None
def test_disable_in_config(self): def test_disable_in_config(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.yml', 'w') as fp: with open('config.yml', 'w') as fp:
fp.write('keystore: NONE\n') fp.write('keystore: NONE\n')
@ -53,7 +56,9 @@ class InitTest(unittest.TestCase):
def test_main_in_empty_dir(self): def test_main_in_empty_dir(self):
"""Test that `fdroid init` will find apksigner and add it to the config""" """Test that `fdroid init` will find apksigner and add it to the config"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
shutil.copy(os.path.join(self.basedir, 'keystore.jks'), testdir) shutil.copy(os.path.join(self.basedir, 'keystore.jks'), testdir)
@ -74,8 +79,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.init.options, args) = parser.parse_args(['--verbose']) (fdroidserver.init.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -9,7 +9,8 @@ import sys
import unittest import unittest
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -38,8 +39,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.install.options, args) = parser.parse_args(['--verbose']) (fdroidserver.install.options, args) = parser.parse_args(['--verbose'])
fdroidserver.common.options = fdroidserver.install.options fdroidserver.common.options = fdroidserver.install.options

View File

@ -12,7 +12,8 @@ import tempfile
import unittest import unittest
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -322,8 +323,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.lint.options, args) = parser.parse_args(['--verbose']) (fdroidserver.lint.options, args) = parser.parse_args(['--verbose'])
fdroidserver.common.options = fdroidserver.lint.options fdroidserver.common.options = fdroidserver.lint.options

View File

@ -12,7 +12,8 @@ from unittest import mock
from testcommon import TmpCwd, TmpPyPath from testcommon import TmpCwd, TmpPyPath
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -26,8 +27,10 @@ class MainTest(unittest.TestCase):
def test_COMMANDS_check(self): def test_COMMANDS_check(self):
"""make sure the built in sub-command defs didn't change unintentionally""" """make sure the built in sub-command defs didn't change unintentionally"""
self.assertListEqual([x for x in fdroidserver.__main__.COMMANDS.keys()], self.assertListEqual(
['build', [x for x in fdroidserver.__main__.COMMANDS.keys()],
[
'build',
'init', 'init',
'publish', 'publish',
'gpgsign', 'gpgsign',
@ -46,7 +49,9 @@ class MainTest(unittest.TestCase):
'btlog', 'btlog',
'signatures', 'signatures',
'nightly', 'nightly',
'mirror']) 'mirror',
],
)
def test_call_init(self): def test_call_init(self):
co = mock.Mock() co = mock.Mock()
@ -73,26 +78,34 @@ class MainTest(unittest.TestCase):
def test_find_plugins(self): def test_find_plugins(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy1.py', 'w') as f: with open('fdroid_testy1.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
main = lambda: 'all good'""")) main = lambda: 'all good'"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
plugins = fdroidserver.__main__.find_plugins() plugins = fdroidserver.__main__.find_plugins()
self.assertIn('testy1', plugins.keys()) self.assertIn('testy1', plugins.keys())
self.assertEqual(plugins['testy1']['summary'], 'ttt') self.assertEqual(plugins['testy1']['summary'], 'ttt')
self.assertEqual(__import__(plugins['testy1']['name'], self.assertEqual(
None, __import__(
None, plugins['testy1']['name'], None, None, ['testy1']
['testy1']) ).main(),
.main(), 'all good',
'all good') )
def test_main_plugin_lambda(self): def test_main_plugin_lambda(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy2.py', 'w') as f: with open('fdroid_testy2.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
main = lambda: print('all good')""")) main = lambda: print('all good')"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'testy2']): with mock.patch('sys.argv', ['', 'testy2']):
with mock.patch('sys.exit') as exit_mock: with mock.patch('sys.exit') as exit_mock:
@ -102,10 +115,14 @@ class MainTest(unittest.TestCase):
def test_main_plugin_def(self): def test_main_plugin_def(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy3.py', 'w') as f: with open('fdroid_testy3.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
def main(): def main():
print('all good')""")) print('all good')"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'testy3']): with mock.patch('sys.argv', ['', 'testy3']):
with mock.patch('sys.exit') as exit_mock: with mock.patch('sys.exit') as exit_mock:
@ -116,10 +133,14 @@ class MainTest(unittest.TestCase):
"""making sure broken plugins get their exceptions through""" """making sure broken plugins get their exceptions through"""
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy4.py', 'w') as f: with open('fdroid_testy4.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
def main(): def main():
raise Exception("this plugin is broken")""")) raise Exception("this plugin is broken")"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'testy4']): with mock.patch('sys.argv', ['', 'testy4']):
with self.assertRaisesRegex(Exception, "this plugin is broken"): with self.assertRaisesRegex(Exception, "this plugin is broken"):
@ -131,11 +152,15 @@ class MainTest(unittest.TestCase):
""" """
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy5.py', 'w') as f: with open('fdroid_testy5.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
raise Exception("this plugin is malicious") raise Exception("this plugin is malicious")
def main(): def main():
print("evil things")""")) print("evil things")"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'lint']): with mock.patch('sys.argv', ['', 'lint']):
with mock.patch('sys.exit') as exit_mock: with mock.patch('sys.exit') as exit_mock:
@ -149,10 +174,14 @@ class MainTest(unittest.TestCase):
""" """
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_signatures.py', 'w') as f: with open('fdroid_signatures.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
def main(): def main():
raise("plugin overrides don't get prevent!")""")) raise("plugin overrides don't get prevent!")"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
with mock.patch('sys.argv', ['', 'signatures']): with mock.patch('sys.argv', ['', 'signatures']):
with mock.patch('sys.exit') as exit_mock: with mock.patch('sys.exit') as exit_mock:
@ -162,49 +191,69 @@ class MainTest(unittest.TestCase):
self.assertEqual(exit_mock.call_count, 2) self.assertEqual(exit_mock.call_count, 2)
def test_preparse_plugin_lookup_bad_name(self): def test_preparse_plugin_lookup_bad_name(self):
self.assertRaises(ValueError, self.assertRaises(
ValueError,
fdroidserver.__main__.preparse_plugin, fdroidserver.__main__.preparse_plugin,
"some.package", "/non/existent/module/path") "some.package",
"/non/existent/module/path",
)
def test_preparse_plugin_lookup_bad_path(self): def test_preparse_plugin_lookup_bad_path(self):
self.assertRaises(ValueError, self.assertRaises(
ValueError,
fdroidserver.__main__.preparse_plugin, fdroidserver.__main__.preparse_plugin,
"fake_module_name", "/non/existent/module/path") "fake_module_name",
"/non/existent/module/path",
)
def test_preparse_plugin_lookup_summary_missing(self): def test_preparse_plugin_lookup_summary_missing(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy6.py', 'w') as f: with open('fdroid_testy6.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
main = lambda: print('all good')""")) textwrap.dedent(
"""\
main = lambda: print('all good')"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')] p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
module_dir = p[0][0].path module_dir = p[0][0].path
module_name = p[0][1] module_name = p[0][1]
self.assertRaises(NameError, self.assertRaises(
NameError,
fdroidserver.__main__.preparse_plugin, fdroidserver.__main__.preparse_plugin,
module_name, module_dir) module_name,
module_dir,
)
def test_preparse_plugin_lookup_module_file(self): def test_preparse_plugin_lookup_module_file(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('fdroid_testy7.py', 'w') as f: with open('fdroid_testy7.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
main = lambda: pritn('all good')""")) main = lambda: pritn('all good')"""
)
)
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')] p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
module_path = p[0][0].path module_path = p[0][0].path
module_name = p[0][1] module_name = p[0][1]
d = fdroidserver.__main__.preparse_plugin(module_name, module_path) d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
self.assertDictEqual(d, {'name': 'fdroid_testy7', self.assertDictEqual(d, {'name': 'fdroid_testy7', 'summary': 'ttt'})
'summary': 'ttt'})
def test_preparse_plugin_lookup_module_dir(self): def test_preparse_plugin_lookup_module_dir(self):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir(os.path.join(tmpdir, 'fdroid_testy8')) os.mkdir(os.path.join(tmpdir, 'fdroid_testy8'))
with open('fdroid_testy8/__main__.py', 'w') as f: with open('fdroid_testy8/__main__.py', 'w') as f:
f.write(textwrap.dedent("""\ f.write(
textwrap.dedent(
"""\
fdroid_summary = "ttt" fdroid_summary = "ttt"
main = lambda: print('all good')""")) main = lambda: print('all good')"""
)
)
with open('fdroid_testy8/__init__.py', 'w') as f: with open('fdroid_testy8/__init__.py', 'w') as f:
pass pass
with TmpPyPath(tmpdir): with TmpPyPath(tmpdir):
@ -212,16 +261,20 @@ class MainTest(unittest.TestCase):
module_path = p[0][0].path module_path = p[0][0].path
module_name = p[0][1] module_name = p[0][1]
d = fdroidserver.__main__.preparse_plugin(module_name, module_path) d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
self.assertDictEqual(d, {'name': 'fdroid_testy8', self.assertDictEqual(d, {'name': 'fdroid_testy8', 'summary': 'ttt'})
'summary': 'ttt'})
if __name__ == "__main__": if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(common.options, args) = parser.parse_args(['--verbose']) (common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -24,7 +24,8 @@ except ImportError:
from yaml import SafeLoader from yaml import SafeLoader
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -134,8 +135,12 @@ class MetadataTest(unittest.TestCase):
fdroidserver.metadata.warnings_action = None fdroidserver.metadata.warnings_action = None
apps = fdroidserver.metadata.read_metadata() apps = fdroidserver.metadata.read_metadata()
for appid in ('org.smssecure.smssecure', 'org.adaway', for appid in (
'org.videolan.vlc', 'com.politedroid'): 'org.smssecure.smssecure',
'org.adaway',
'org.videolan.vlc',
'com.politedroid',
):
savepath = os.path.join('metadata', 'dump', appid + '.yaml') savepath = os.path.join('metadata', 'dump', appid + '.yaml')
frommeta = dict(apps[appid]) frommeta = dict(apps[appid])
self.assertTrue(appid in apps) self.assertTrue(appid in apps)
@ -149,7 +154,9 @@ class MetadataTest(unittest.TestCase):
# yaml.dump(frommeta, f, default_flow_style=False) # yaml.dump(frommeta, f, default_flow_style=False)
def test_rewrite_yaml_fakeotaupdate(self): def test_rewrite_yaml_fakeotaupdate(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
fdroidserver.common.config = {'accepted_formats': ['yml']} fdroidserver.common.config = {'accepted_formats': ['yml']}
fdroidserver.metadata.warnings_action = None fdroidserver.metadata.warnings_action = None
@ -157,7 +164,9 @@ class MetadataTest(unittest.TestCase):
allapps = fdroidserver.metadata.read_metadata() allapps = fdroidserver.metadata.read_metadata()
for appid, app in allapps.items(): for appid, app in allapps.items():
if appid == 'fake.ota.update': if appid == 'fake.ota.update':
fdroidserver.metadata.write_metadata(os.path.join(testdir, appid + '.yml'), app) fdroidserver.metadata.write_metadata(
os.path.join(testdir, appid + '.yml'), app
)
# assert rewrite result # assert rewrite result
with open(os.path.join(testdir, 'fake.ota.update.yml'), 'r') as result: with open(os.path.join(testdir, 'fake.ota.update.yml'), 'r') as result:
@ -166,14 +175,18 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(result.read(), orig.read()) self.assertEqual(result.read(), orig.read())
def test_rewrite_yaml_fdroidclient(self): def test_rewrite_yaml_fdroidclient(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
fdroidserver.common.config = {'accepted_formats': ['yml']} fdroidserver.common.config = {'accepted_formats': ['yml']}
# rewrite metadata # rewrite metadata
allapps = fdroidserver.metadata.read_metadata() allapps = fdroidserver.metadata.read_metadata()
for appid, app in allapps.items(): for appid, app in allapps.items():
if appid == 'org.fdroid.fdroid': if appid == 'org.fdroid.fdroid':
fdroidserver.metadata.write_metadata(os.path.join(testdir, appid + '.yml'), app) fdroidserver.metadata.write_metadata(
os.path.join(testdir, appid + '.yml'), app
)
# assert rewrite result # assert rewrite result
with open(os.path.join(testdir, 'org.fdroid.fdroid.yml'), 'r') as result: with open(os.path.join(testdir, 'org.fdroid.fdroid.yml'), 'r') as result:
@ -182,13 +195,17 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(result.read(), orig.read()) self.assertEqual(result.read(), orig.read())
def test_rewrite_yaml_special_build_params(self): def test_rewrite_yaml_special_build_params(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
# rewrite metadata # rewrite metadata
allapps = fdroidserver.metadata.read_metadata() allapps = fdroidserver.metadata.read_metadata()
for appid, app in allapps.items(): for appid, app in allapps.items():
if appid == 'app.with.special.build.params': if appid == 'app.with.special.build.params':
fdroidserver.metadata.write_metadata(os.path.join(testdir, appid + '.yml'), app) fdroidserver.metadata.write_metadata(
os.path.join(testdir, appid + '.yml'), app
)
# assert rewrite result # assert rewrite result
with open(os.path.join(testdir, 'app.with.special.build.params.yml'), 'r') as result: with open(os.path.join(testdir, 'app.with.special.build.params.yml'), 'r') as result:
@ -241,7 +258,9 @@ class MetadataTest(unittest.TestCase):
self.assertEqual('1234567890', yamldata['Builds'][0]['commit']) self.assertEqual('1234567890', yamldata['Builds'][0]['commit'])
def test_read_metadata_sort_by_time(self): def test_read_metadata_sort_by_time(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
metadatadir = os.path.join(testdir, 'metadata') metadatadir = os.path.join(testdir, 'metadata')
os.makedirs(metadatadir) os.makedirs(metadatadir)
@ -265,36 +284,50 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(randomlist, allappids) self.assertEqual(randomlist, allappids)
def test_parse_yaml_metadata_unknown_app_field(self): def test_parse_yaml_metadata_unknown_app_field(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
Builds: [] Builds: []
bad: value""")) bad: value"""
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
with mock.patch('fdroidserver.metadata.warnings_action', 'error'): with mock.patch('fdroidserver.metadata.warnings_action', 'error'):
with self.assertRaises(MetaDataException): with self.assertRaises(MetaDataException):
fdroidserver.metadata.parse_yaml_metadata(mf, {}) fdroidserver.metadata.parse_yaml_metadata(mf, {})
def test_parse_yaml_metadata_unknown_build_flag(self): def test_parse_yaml_metadata_unknown_build_flag(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
Builds: Builds:
- bad: value""")) - bad: value"""
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
with mock.patch('fdroidserver.metadata.warnings_action', 'error'): with mock.patch('fdroidserver.metadata.warnings_action', 'error'):
with self.assertRaises(MetaDataException): with self.assertRaises(MetaDataException):
fdroidserver.metadata.parse_yaml_metadata(mf, {}) fdroidserver.metadata.parse_yaml_metadata(mf, {})
def test_parse_yaml_srclib_corrupt_file(self): def test_parse_yaml_srclib_corrupt_file(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
srclibfile = os.path.join(testdir, 'srclib', 'mock.yml') srclibfile = os.path.join(testdir, 'srclib', 'mock.yml')
os.mkdir(os.path.dirname(srclibfile)) os.mkdir(os.path.dirname(srclibfile))
with open(srclibfile, 'w') as fp: with open(srclibfile, 'w') as fp:
fp.write(textwrap.dedent(""" fp.write(
textwrap.dedent(
"""
- RepoType: git - RepoType: git
- Repo: https://github.com/realm/realm-js.git - Repo: https://github.com/realm/realm-js.git
""")) """
)
)
with mock.patch('fdroidserver.metadata.warnings_action', 'error'): with mock.patch('fdroidserver.metadata.warnings_action', 'error'):
with self.assertRaises(MetaDataException): with self.assertRaises(MetaDataException):
fdroidserver.metadata.parse_yaml_srclib(srclibfile) fdroidserver.metadata.parse_yaml_srclib(srclibfile)
@ -321,7 +354,10 @@ class MetadataTest(unittest.TestCase):
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -341,10 +377,14 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: Tags UpdateCheckMode: Tags
""")) """
),
)
def test_parse_yaml_metadata_prebuild_list(self): def test_parse_yaml_metadata_prebuild_list(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
Builds: Builds:
@ -364,7 +404,9 @@ class MetadataTest(unittest.TestCase):
- ./gradlew someSpecialTask - ./gradlew someSpecialTask
- sed -i 'd/that wrong config/' gradle.properties - sed -i 'd/that wrong config/' gradle.properties
- ./gradlew compile - ./gradlew compile
""")) """
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
mf.seek(0) mf.seek(0)
result = {} result = {}
@ -386,7 +428,9 @@ class MetadataTest(unittest.TestCase):
"./gradlew compile"}]}) "./gradlew compile"}]})
def test_parse_yaml_metadata_prebuild_strings(self): def test_parse_yaml_metadata_prebuild_strings(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
Builds: Builds:
@ -398,7 +442,9 @@ class MetadataTest(unittest.TestCase):
prebuild: npm something && echo 'important setting' >> /a/file prebuild: npm something && echo 'important setting' >> /a/file
build: |- build: |-
./gradlew someSpecialTask && sed -i 'd/that wrong config/' gradle.properties && ./gradlew compile ./gradlew someSpecialTask && sed -i 'd/that wrong config/' gradle.properties && ./gradlew compile
""")) """
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
mf.seek(0) mf.seek(0)
result = {} result = {}
@ -420,7 +466,9 @@ class MetadataTest(unittest.TestCase):
"./gradlew compile"}]}) "./gradlew compile"}]})
def test_parse_yaml_metadata_prebuild_string(self): def test_parse_yaml_metadata_prebuild_string(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
Builds: Builds:
@ -428,7 +476,9 @@ class MetadataTest(unittest.TestCase):
versionName: v0.1.0 versionName: v0.1.0
prebuild: |- prebuild: |-
a && b && sed -i 's,a,b,' a && b && sed -i 's,a,b,'
""")) """
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
mf.seek(0) mf.seek(0)
result = {} result = {}
@ -442,7 +492,9 @@ class MetadataTest(unittest.TestCase):
"sed -i 's,a,b,'"}]}) "sed -i 's,a,b,'"}]})
def test_parse_yaml_provides_should_be_ignored(self): def test_parse_yaml_provides_should_be_ignored(self):
mf = io.StringIO(textwrap.dedent("""\ mf = io.StringIO(
textwrap.dedent(
"""\
Provides: this.is.deprecated Provides: this.is.deprecated
AutoName: F-Droid AutoName: F-Droid
RepoType: git RepoType: git
@ -451,7 +503,9 @@ class MetadataTest(unittest.TestCase):
versionName: v0.1.0 versionName: v0.1.0
prebuild: |- prebuild: |-
a && b && sed -i 's,a,b,' a && b && sed -i 's,a,b,'
""")) """
)
)
mf.name = 'mock_filename.yaml' mf.name = 'mock_filename.yaml'
mf.seek(0) mf.seek(0)
result = {} result = {}
@ -475,7 +529,10 @@ class MetadataTest(unittest.TestCase):
app['Builds'].append(build) app['Builds'].append(build)
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -490,7 +547,9 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
""")) """
),
)
def test_write_yaml_1_line_scripts_as_list(self): def test_write_yaml_1_line_scripts_as_list(self):
mf = io.StringIO() mf = io.StringIO()
@ -507,7 +566,10 @@ class MetadataTest(unittest.TestCase):
app['Builds'].append(build) app['Builds'].append(build)
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -522,7 +584,9 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
""")) """
),
)
def test_write_yaml_multiline_scripts_from_list(self): def test_write_yaml_multiline_scripts_from_list(self):
mf = io.StringIO() mf = io.StringIO()
@ -545,7 +609,10 @@ class MetadataTest(unittest.TestCase):
app['Builds'].append(build) app['Builds'].append(build)
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -570,7 +637,9 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
""")) """
),
)
def test_write_yaml_multiline_scripts_from_string(self): def test_write_yaml_multiline_scripts_from_string(self):
mf = io.StringIO() mf = io.StringIO()
@ -587,7 +656,10 @@ class MetadataTest(unittest.TestCase):
app['Builds'].append(build) app['Builds'].append(build)
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -612,7 +684,9 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
""")) """
),
)
def test_write_yaml_make_sure_provides_does_not_get_written(self): def test_write_yaml_make_sure_provides_does_not_get_written(self):
mf = io.StringIO() mf = io.StringIO()
@ -627,7 +701,10 @@ class MetadataTest(unittest.TestCase):
app['Builds'].append(build) app['Builds'].append(build)
fdroidserver.metadata.write_yaml(mf, app) fdroidserver.metadata.write_yaml(mf, app)
mf.seek(0) mf.seek(0)
self.assertEqual(mf.read(), textwrap.dedent("""\ self.assertEqual(
mf.read(),
textwrap.dedent(
"""\
Categories: Categories:
- None - None
License: Unknown License: Unknown
@ -640,7 +717,9 @@ class MetadataTest(unittest.TestCase):
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
""")) """
),
)
def test_parse_yaml_srclib_unknown_key(self): def test_parse_yaml_srclib_unknown_key(self):
fdroidserver.metadata.warnings_action = 'error' fdroidserver.metadata.warnings_action = 'error'
@ -685,7 +764,9 @@ class MetadataTest(unittest.TestCase):
fdroidserver.metadata.warnings_action = 'error' fdroidserver.metadata.warnings_action = 'error'
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('simple.yml', 'w', encoding='utf-8') as f: with open('simple.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
# this should be simple # this should be simple
RepoType: git RepoType: git
@ -695,7 +776,9 @@ class MetadataTest(unittest.TestCase):
Subdir: Subdir:
Prepare: Prepare:
''')) '''
)
)
srclib = fdroidserver.metadata.parse_yaml_srclib('simple.yml') srclib = fdroidserver.metadata.parse_yaml_srclib('simple.yml')
self.assertDictEqual({'Repo': 'https://git.host/repo.git', self.assertDictEqual({'Repo': 'https://git.host/repo.git',
'RepoType': 'git', 'RepoType': 'git',
@ -707,13 +790,17 @@ class MetadataTest(unittest.TestCase):
fdroidserver.metadata.warnings_action = 'error' fdroidserver.metadata.warnings_action = 'error'
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
with open('Changelog-cketti.yml', 'w', encoding='utf-8') as f: with open('Changelog-cketti.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
RepoType: git RepoType: git
Repo: https://github.com/cketti/ckChangeLog Repo: https://github.com/cketti/ckChangeLog
Subdir: library,ckChangeLog/src/main Subdir: library,ckChangeLog/src/main
Prepare: "[ -f project.properties ] || echo 'source.dir=java' > ant.properties && echo -e 'android.library=true\\\\ntarget=android-19' > project.properties" Prepare: "[ -f project.properties ] || echo 'source.dir=java' > ant.properties && echo -e 'android.library=true\\\\ntarget=android-19' > project.properties"
''')) '''
)
)
srclib = fdroidserver.metadata.parse_yaml_srclib('Changelog-cketti.yml') srclib = fdroidserver.metadata.parse_yaml_srclib('Changelog-cketti.yml')
self.assertDictEqual(srclib, self.assertDictEqual(srclib,
{'Repo': 'https://github.com/cketti/ckChangeLog', {'Repo': 'https://github.com/cketti/ckChangeLog',
@ -729,7 +816,9 @@ class MetadataTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir('srclibs') os.mkdir('srclibs')
with open('srclibs/with-list.yml', 'w', encoding='utf-8') as f: with open('srclibs/with-list.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
# this should be simple # this should be simple
RepoType: git RepoType: git
Repo: https://git.host/repo.git Repo: https://git.host/repo.git
@ -745,7 +834,9 @@ class MetadataTest(unittest.TestCase):
Prepare: Prepare:
There is a difference between knowing the path There is a difference between knowing the path
and walking the path. and walking the path.
''')) '''
)
)
fdroidserver.metadata.read_srclibs() fdroidserver.metadata.read_srclibs()
self.maxDiff = None self.maxDiff = None
self.assertDictEqual(fdroidserver.metadata.srclibs, self.assertDictEqual(fdroidserver.metadata.srclibs,
@ -767,7 +858,9 @@ class MetadataTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir('srclibs') os.mkdir('srclibs')
with open('srclibs/with-list.yml', 'w', encoding='utf-8') as f: with open('srclibs/with-list.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
# this should be simple # this should be simple
RepoType: git RepoType: git
Repo: https://git.host/repo.git Repo: https://git.host/repo.git
@ -782,7 +875,9 @@ class MetadataTest(unittest.TestCase):
- But until we do, these people are still a part of that system and that makes them our enemy. - But until we do, these people are still a part of that system and that makes them our enemy.
- You have to understand, most of these people are not ready to be unplugged. - You have to understand, most of these people are not ready to be unplugged.
- And many of them are so inert, so hopelessly dependent on the system that they will fight to protect it. - And many of them are so inert, so hopelessly dependent on the system that they will fight to protect it.
''')) '''
)
)
fdroidserver.metadata.read_srclibs() fdroidserver.metadata.read_srclibs()
self.maxDiff = None self.maxDiff = None
self.assertDictEqual(fdroidserver.metadata.srclibs, self.assertDictEqual(fdroidserver.metadata.srclibs,
@ -804,19 +899,27 @@ class MetadataTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir('srclibs') os.mkdir('srclibs')
with open('srclibs/simple.yml', 'w', encoding='utf-8') as f: with open('srclibs/simple.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
RepoType: git RepoType: git
Repo: https://git.host/repo.git Repo: https://git.host/repo.git
''')) '''
)
)
with open('srclibs/simple-wb.yml', 'w', encoding='utf-8') as f: with open('srclibs/simple-wb.yml', 'w', encoding='utf-8') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
# this should be simple # this should be simple
RepoType: git RepoType: git
Repo: https://git.host/repo.git Repo: https://git.host/repo.git
Subdir: Subdir:
Prepare: Prepare:
''')) '''
)
)
fdroidserver.metadata.read_srclibs() fdroidserver.metadata.read_srclibs()
self.assertDictEqual(fdroidserver.metadata.srclibs, self.assertDictEqual(fdroidserver.metadata.srclibs,
{'simple-wb': {'RepoType': 'git', {'simple-wb': {'RepoType': 'git',
@ -833,8 +936,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -23,7 +23,8 @@ import textwrap
from unittest import mock from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -56,8 +57,9 @@ class PublishTest(unittest.TestCase):
self.assertEqual('ee8807d2', publish.key_alias("org.schabi.newpipe")) self.assertEqual('ee8807d2', publish.key_alias("org.schabi.newpipe"))
self.assertEqual('b53c7e11', publish.key_alias("de.grobox.liberario")) self.assertEqual('b53c7e11', publish.key_alias("de.grobox.liberario"))
publish.config = {'keyaliases': {'yep.app': '@org.org.org', publish.config = {
'com.example.app': '1a2b3c4d'}} 'keyaliases': {'yep.app': '@org.org.org', 'com.example.app': '1a2b3c4d'}
}
self.assertEqual('78688a0f', publish.key_alias('yep.app')) self.assertEqual('78688a0f', publish.key_alias('yep.app'))
self.assertEqual('1a2b3c4d', publish.key_alias('com.example.app')) self.assertEqual('1a2b3c4d', publish.key_alias('com.example.app'))
@ -69,11 +71,13 @@ class PublishTest(unittest.TestCase):
publish.config['keypass'] = '123456' publish.config['keypass'] = '123456'
publish.config['keystore'] = 'dummy-keystore.jks' publish.config['keystore'] = 'dummy-keystore.jks'
expected = {'78688a0f': '277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82', expected = {
'78688a0f': '277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82',
'd2d51ff2': 'fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3', 'd2d51ff2': 'fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3',
'dc3b169e': '6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c', 'dc3b169e': '6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c',
'a163ec9b': 'd34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4', 'a163ec9b': 'd34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4',
'repokey': 'c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41'} 'repokey': 'c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41',
}
result = publish.read_fingerprints_from_keystore() result = publish.read_fingerprints_from_keystore()
self.maxDiff = None self.maxDiff = None
self.assertEqual(expected, result) self.assertEqual(expected, result)
@ -84,17 +88,20 @@ class PublishTest(unittest.TestCase):
publish.config = common.config publish.config = common.config
publish.config['keystorepass'] = '123456' publish.config['keystorepass'] = '123456'
publish.config['keypass'] = '123456' publish.config['keypass'] = '123456'
publish.config['keystore'] = os.path.join(os.getcwd(), publish.config['keystore'] = os.path.join(os.getcwd(), 'dummy-keystore.jks')
'dummy-keystore.jks')
publish.config['repo_keyalias'] = 'repokey' publish.config['repo_keyalias'] = 'repokey'
appids = ['com.example.app', appids = [
'com.example.app',
'net.unavailable', 'net.unavailable',
'org.test.testy', 'org.test.testy',
'com.example.anotherapp', 'com.example.anotherapp',
'org.org.org'] 'org.org.org',
]
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
with open('config.py', 'w') as f: with open('config.py', 'w') as f:
pass pass
@ -114,15 +121,20 @@ class PublishTest(unittest.TestCase):
}, },
"org.test.testy": { "org.test.testy": {
"signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c" "signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c"
} },
} }
self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints()) self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints())
with open('config.py', 'r') as f: with open('config.py', 'r') as f:
self.assertEqual(textwrap.dedent('''\ self.assertEqual(
textwrap.dedent(
'''\
repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41" repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41"
'''), f.read()) '''
),
f.read(),
)
def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self): def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self):
common.config = {} common.config = {}
@ -130,12 +142,13 @@ class PublishTest(unittest.TestCase):
publish.config = common.config publish.config = common.config
publish.config['keystorepass'] = '123456' publish.config['keystorepass'] = '123456'
publish.config['keypass'] = '123456' publish.config['keypass'] = '123456'
publish.config['keystore'] = os.path.join(os.getcwd(), publish.config['keystore'] = os.path.join(os.getcwd(), 'dummy-keystore.jks')
'dummy-keystore.jks')
publish.config['repo_keyalias'] = 'repokey' publish.config['repo_keyalias'] = 'repokey'
publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad' publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad'
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2) publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2)
with self.assertRaises(FDroidException): with self.assertRaises(FDroidException):
@ -149,7 +162,9 @@ class PublishTest(unittest.TestCase):
publish.config['repo_keyalias'] = 'sova' publish.config['repo_keyalias'] = 'sova'
publish.config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' publish.config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
publish.config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' publish.config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
shutil.copy('keystore.jks', testdir) shutil.copy('keystore.jks', testdir)
os.mkdir(os.path.join(testdir, 'repo')) os.mkdir(os.path.join(testdir, 'repo'))
@ -170,6 +185,7 @@ class PublishTest(unittest.TestCase):
def test_check_for_key_collisions(self): def test_check_for_key_collisions(self):
from fdroidserver.metadata import App from fdroidserver.metadata import App
common.config = {} common.config = {}
common.fill_config_defaults(common.config) common.fill_config_defaults(common.config)
publish.config = common.config publish.config = common.config
@ -253,11 +269,12 @@ class PublishTest(unittest.TestCase):
self.assertEqual(publish.config['keytool'], data['keytool']) self.assertEqual(publish.config['keytool'], data['keytool'])
def test_sign_then_implant_signature(self): def test_sign_then_implant_signature(self):
class Options: class Options:
verbose = False verbose = False
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
config = common.read_config(Options) config = common.read_config(Options)
@ -322,8 +339,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(common.options, args) = parser.parse_args(['--verbose']) (common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -12,7 +12,8 @@ from unittest import mock
from testcommon import TmpCwd from testcommon import TmpCwd
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -47,24 +48,34 @@ class RewriteMetaTest(unittest.TestCase):
rewritemeta.main() rewritemeta.main()
with open('metadata/a.yml') as f: with open('metadata/a.yml') as f:
self.assertEqual(f.read(), textwrap.dedent('''\ self.assertEqual(
f.read(),
textwrap.dedent(
'''\
License: Unknown License: Unknown
AutoName: a AutoName: a
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
''')) '''
),
)
with open('metadata/b.yml') as f: with open('metadata/b.yml') as f:
self.assertEqual(f.read(), textwrap.dedent('''\ self.assertEqual(
f.read(),
textwrap.dedent(
'''\
License: Unknown License: Unknown
AutoName: b AutoName: b
AutoUpdateMode: None AutoUpdateMode: None
UpdateCheckMode: None UpdateCheckMode: None
''')) '''
),
)
def test_rewrite_scenario_yml_no_ruamel(self): def test_rewrite_scenario_yml_no_ruamel(self):
sys.argv = ['rewritemeta', 'a'] sys.argv = ['rewritemeta', 'a']
@ -81,16 +92,26 @@ class RewriteMetaTest(unittest.TestCase):
rewritemeta.main() rewritemeta.main()
with open('metadata/a.yml') as f: with open('metadata/a.yml') as f:
self.assertEqual(f.read(), textwrap.dedent('''\ self.assertEqual(
AutoName: a''')) f.read(),
textwrap.dedent(
'''\
AutoName: a'''
),
)
if __name__ == "__main__": if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(common.options, args) = parser.parse_args(['--verbose']) (common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -15,7 +15,8 @@ import yaml
from unittest import mock from unittest import mock
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -27,7 +28,6 @@ import fdroidserver.scanner
class ScannerTest(unittest.TestCase): class ScannerTest(unittest.TestCase):
def setUp(self): def setUp(self):
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
self.basedir = os.path.join(localmodule, 'tests') self.basedir = os.path.join(localmodule, 'tests')
@ -53,8 +53,9 @@ class ScannerTest(unittest.TestCase):
build = fdroidserver.metadata.Build() build = fdroidserver.metadata.Build()
fatal_problems = fdroidserver.scanner.scan_source(d, build) fatal_problems = fdroidserver.scanner.scan_source(d, build)
should = projects.get(os.path.basename(d), 0) should = projects.get(os.path.basename(d), 0)
self.assertEqual(should, fatal_problems, self.assertEqual(
"%s should have %d errors!" % (d, should)) should, fatal_problems, "%s should have %d errors!" % (d, should)
)
def test_get_gradle_compile_commands(self): def test_get_gradle_compile_commands(self):
test_files = [ test_files = [
@ -85,13 +86,17 @@ class ScannerTest(unittest.TestCase):
def test_scan_source_files_sneaky_maven(self): def test_scan_source_files_sneaky_maven(self):
"""Check for sneaking in banned maven repos""" """Check for sneaking in banned maven repos"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
fdroidserver.scanner.config = None fdroidserver.scanner.config = None
fdroidserver.scanner.options = mock.Mock() fdroidserver.scanner.options = mock.Mock()
fdroidserver.scanner.options.json = True fdroidserver.scanner.options.json = True
with open('build.gradle', 'w') as fp: with open('build.gradle', 'w') as fp:
fp.write(textwrap.dedent(""" fp.write(
textwrap.dedent(
"""
maven { maven {
"https://jitpack.io" "https://jitpack.io"
url 'https://maven.fabric.io/public' url 'https://maven.fabric.io/public'
@ -100,7 +105,9 @@ class ScannerTest(unittest.TestCase):
"https://maven.google.com" "https://maven.google.com"
setUrl('https://evilcorp.com/maven') setUrl('https://evilcorp.com/maven')
} }
""")) """
)
)
count = fdroidserver.scanner.scan_source(testdir) count = fdroidserver.scanner.scan_source(testdir)
self.assertEqual(2, count, 'there should be this many errors') self.assertEqual(2, count, 'there should be this many errors')
@ -111,7 +118,9 @@ class ScannerTest(unittest.TestCase):
difference between absolute and relative paths. difference between absolute and relative paths.
""" """
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
build_dir = os.path.join('build', 'fake.app') build_dir = os.path.join('build', 'fake.app')
abs_build_dir = os.path.join(testdir, build_dir) abs_build_dir = os.path.join(testdir, build_dir)
os.makedirs(abs_build_dir, exist_ok=True) os.makedirs(abs_build_dir, exist_ok=True)
@ -130,21 +139,13 @@ class ScannerTest(unittest.TestCase):
'static.a', 'static.a',
'src/test/resources/classes.dex', 'src/test/resources/classes.dex',
] ]
remove = [ remove = ['gradle-wrapper.jar', 'gradlew', 'gradlew.bat']
'gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
]
os.makedirs('src/test/resources', exist_ok=True) os.makedirs('src/test/resources', exist_ok=True)
for f in keep + remove: for f in keep + remove:
with open(f, 'w') as fp: with open(f, 'w') as fp:
fp.write('placeholder') fp.write('placeholder')
self.assertTrue(os.path.exists(f)) self.assertTrue(os.path.exists(f))
binaries = [ binaries = ['binary.out', 'fake.png', 'snippet.png']
'binary.out',
'fake.png',
'snippet.png',
]
with open('binary.out', 'wb') as fp: with open('binary.out', 'wb') as fp:
fp.write(b'\x00\x00') fp.write(b'\x00\x00')
fp.write(uuid.uuid4().bytes) fp.write(uuid.uuid4().bytes)
@ -152,10 +153,12 @@ class ScannerTest(unittest.TestCase):
os.chmod('fake.png', 0o755) os.chmod('fake.png', 0o755)
os.system('ls -l binary.out') os.system('ls -l binary.out')
with open('snippet.png', 'wb') as fp: with open('snippet.png', 'wb') as fp:
fp.write(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00' fp.write(
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00'
b'\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sB' b'\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sB'
b'IT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\tpHYs\x00\x00\n' b'IT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\tpHYs\x00\x00\n'
b'a\x00\x00\na\x01\xfc\xccJ%\x00\x00\x00\x19tEXtSoftware') b'a\x00\x00\na\x01\xfc\xccJ%\x00\x00\x00\x19tEXtSoftware'
)
os.chmod('snippet.png', 0o755) os.chmod('snippet.png', 0o755)
os.system('ls -l fake.png') os.system('ls -l fake.png')
@ -199,7 +202,9 @@ class ScannerTest(unittest.TestCase):
def test_build_local_scanner(self): def test_build_local_scanner(self):
"""`fdroid build` calls scanner functions, test them here""" """`fdroid build` calls scanner functions, test them here"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
config = dict() config = dict()
@ -229,12 +234,15 @@ class ScannerTest(unittest.TestCase):
self.assertTrue(os.path.exists(f)) self.assertTrue(os.path.exists(f))
with open('build.xml', 'w') as fp: with open('build.xml', 'w') as fp:
fp.write(textwrap.dedent( fp.write(
textwrap.dedent(
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?> """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="clean" name="mockapp"> <project basedir="." default="clean" name="mockapp">
<target name="release"/> <target name="release"/>
<target name="clean"/> <target name="clean"/>
</project>""")) </project>"""
)
)
def make_fake_apk(output, build): def make_fake_apk(output, build):
with open(build.output, 'w') as fp: with open(build.output, 'w') as fp:
@ -272,21 +280,27 @@ class ScannerTest(unittest.TestCase):
def test_scan_gradle_file_with_multiple_problems(self): def test_scan_gradle_file_with_multiple_problems(self):
"""Check that the scanner can handle scandelete with gradle files with multiple problems""" """Check that the scanner can handle scandelete with gradle files with multiple problems"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
fdroidserver.scanner.config = None fdroidserver.scanner.config = None
fdroidserver.scanner.options = mock.Mock() fdroidserver.scanner.options = mock.Mock()
build = fdroidserver.metadata.Build() build = fdroidserver.metadata.Build()
build.scandelete = ['build.gradle'] build.scandelete = ['build.gradle']
with open('build.gradle', 'w') as fp: with open('build.gradle', 'w') as fp:
fp.write(textwrap.dedent(""" fp.write(
textwrap.dedent(
"""
maven { maven {
url 'https://maven.fabric.io/public' url 'https://maven.fabric.io/public'
} }
maven { maven {
url 'https://evilcorp.com/maven' url 'https://evilcorp.com/maven'
} }
""")) """
)
)
count = fdroidserver.scanner.scan_source(testdir, build) count = fdroidserver.scanner.scan_source(testdir, build)
self.assertFalse(os.path.exists("build.gradle")) self.assertFalse(os.path.exists("build.gradle"))
self.assertEqual(0, count, 'there should be this many errors') self.assertEqual(0, count, 'there should be this many errors')
@ -296,8 +310,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -10,7 +10,8 @@ import logging
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -20,7 +21,6 @@ from fdroidserver import common, signatures
class SignaturesTest(unittest.TestCase): class SignaturesTest(unittest.TestCase):
def setUp(self): def setUp(self):
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
common.config = None common.config = None
@ -60,8 +60,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(common.options, args) = parser.parse_args(['--verbose']) (common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -43,7 +43,8 @@ except ImportError:
from yaml import Loader as FullLoader from yaml import Loader as FullLoader
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -55,11 +56,7 @@ import fdroidserver.update
from fdroidserver.common import FDroidPopen from fdroidserver.common import FDroidPopen
DONATION_FIELDS = ( DONATION_FIELDS = ('Donate', 'Liberapay', 'OpenCollective')
'Donate',
'Liberapay',
'OpenCollective',
)
class Options: class Options:
@ -77,6 +74,7 @@ class UpdateTest(unittest.TestCase):
logging.getLogger('androguard.axml').setLevel(logging.INFO) logging.getLogger('androguard.axml').setLevel(logging.INFO)
logging.getLogger('androguard.core.api_specific_resources').setLevel(logging.INFO) logging.getLogger('androguard.core.api_specific_resources').setLevel(logging.INFO)
from PIL import PngImagePlugin from PIL import PngImagePlugin
logging.getLogger(PngImagePlugin.__name__).setLevel(logging.INFO) logging.getLogger(PngImagePlugin.__name__).setLevel(logging.INFO)
self.basedir = os.path.join(localmodule, 'tests') self.basedir = os.path.join(localmodule, 'tests')
self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
@ -114,12 +112,17 @@ class UpdateTest(unittest.TestCase):
shutil.copystat(testfile, cpfile) shutil.copystat(testfile, cpfile)
apps = dict() apps = dict()
for packageName in ('info.guardianproject.urzip', 'org.videolan.vlc', 'obb.mainpatch.current', for packageName in (
'com.nextcloud.client', 'com.nextcloud.client.dev', 'info.guardianproject.urzip',
'eu.siacs.conversations'): 'org.videolan.vlc',
'obb.mainpatch.current',
'com.nextcloud.client',
'com.nextcloud.client.dev',
'eu.siacs.conversations',
):
apps[packageName] = fdroidserver.metadata.App() apps[packageName] = fdroidserver.metadata.App()
apps[packageName]['id'] = packageName apps[packageName]['id'] = packageName
apps[packageName]['CurrentVersionCode'] = 0xcafebeef apps[packageName]['CurrentVersionCode'] = 0xCAFEBEEF
apps['info.guardianproject.urzip']['CurrentVersionCode'] = 100 apps['info.guardianproject.urzip']['CurrentVersionCode'] = 100
@ -367,8 +370,9 @@ class UpdateTest(unittest.TestCase):
def test_insert_triple_t_2_metadata(self): def test_insert_triple_t_2_metadata(self):
packageName = 'org.piwigo.android' packageName = 'org.piwigo.android'
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.rmdir(tmptestsdir) os.rmdir(tmptestsdir)
shutil.copytree(os.path.join(self.basedir, 'triple-t-2'), tmptestsdir) shutil.copytree(os.path.join(self.basedir, 'triple-t-2'), tmptestsdir)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
@ -414,8 +418,7 @@ class UpdateTest(unittest.TestCase):
config = dict() config = dict()
fdroidserver.common.fill_config_defaults(config) fdroidserver.common.fill_config_defaults(config)
fdroidserver.common.config = config fdroidserver.common.config = config
p = FDroidPopen(['java', '-cp', 'getsig', p = FDroidPopen(['java', '-cp', 'getsig', 'getsig', apkfile])
'getsig', apkfile])
sig = None sig = None
for line in p.output.splitlines(): for line in p.output.splitlines():
if line.startswith('Result:'): if line.startswith('Result:'):
@ -509,7 +512,9 @@ class UpdateTest(unittest.TestCase):
def testScanApksAndObbs(self): def testScanApksAndObbs(self):
os.chdir(os.path.join(localmodule, 'tests')) os.chdir(os.path.join(localmodule, 'tests'))
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo')
shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata') shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata')
@ -567,7 +572,9 @@ class UpdateTest(unittest.TestCase):
def test_apkcache_json(self): def test_apkcache_json(self):
"""test the migration from pickle to json""" """test the migration from pickle to json"""
os.chdir(os.path.join(localmodule, 'tests')) os.chdir(os.path.join(localmodule, 'tests'))
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo')
config = dict() config = dict()
@ -608,7 +615,9 @@ class UpdateTest(unittest.TestCase):
fdroidserver.common.config = config fdroidserver.common.config = config
fdroidserver.update.config = config fdroidserver.update.config = config
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('repo') os.mkdir('repo')
os.mkdir('stats') os.mkdir('stats')
@ -625,8 +634,10 @@ class UpdateTest(unittest.TestCase):
self.assertEqual(filename, info['apkName']) self.assertEqual(filename, info['apkName'])
self.assertEqual(datetime, type(info['added'])) self.assertEqual(datetime, type(info['added']))
self.assertEqual(os.path.getsize(os.path.join('repo', filename)), info['size']) self.assertEqual(os.path.getsize(os.path.join('repo', filename)), info['size'])
self.assertEqual('531190bdbc07e77d5577249949106f32dac7f62d38d66d66c3ae058be53a729d', self.assertEqual(
info['hash']) '531190bdbc07e77d5577249949106f32dac7f62d38d66d66c3ae058be53a729d',
info['hash'],
)
def test_read_added_date_from_all_apks(self): def test_read_added_date_from_all_apks(self):
config = dict() config = dict()
@ -770,7 +781,9 @@ class UpdateTest(unittest.TestCase):
fdroidserver.common.fill_config_defaults(config) fdroidserver.common.fill_config_defaults(config)
fdroidserver.common.config = config fdroidserver.common.config = config
fdroidserver.update.config = config fdroidserver.update.config = config
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('repo') os.mkdir('repo')
apkfile = 'repo/badzip_1.apk' apkfile = 'repo/badzip_1.apk'
@ -780,7 +793,6 @@ class UpdateTest(unittest.TestCase):
fdroidserver.update.scan_apk(apkfile) fdroidserver.update.scan_apk(apkfile)
def test_process_apk(self): def test_process_apk(self):
def _build_yaml_representer(dumper, data): def _build_yaml_representer(dumper, data):
'''Creates a YAML representation of a Build instance''' '''Creates a YAML representation of a Build instance'''
return dumper.represent_dict(data) return dumper.represent_dict(data)
@ -820,8 +832,9 @@ class UpdateTest(unittest.TestCase):
if apkName == '../org.dyndns.fules.ck_20.apk': if apkName == '../org.dyndns.fules.ck_20.apk':
self.assertEqual(apk['icon'], 'org.dyndns.fules.ck.20.png') self.assertEqual(apk['icon'], 'org.dyndns.fules.ck.20.png')
for density in fdroidserver.update.screen_densities: for density in fdroidserver.update.screen_densities:
icon_path = os.path.join(fdroidserver.update.get_icon_dir('repo', density), icon_path = os.path.join(
apk['icon']) fdroidserver.update.get_icon_dir('repo', density), apk['icon']
)
self.assertTrue(os.path.isfile(icon_path)) self.assertTrue(os.path.isfile(icon_path))
self.assertTrue(os.path.getsize(icon_path) > 1) self.assertTrue(os.path.getsize(icon_path) > 1)
@ -863,8 +876,9 @@ class UpdateTest(unittest.TestCase):
knownapks = fdroidserver.common.KnownApks() knownapks = fdroidserver.common.KnownApks()
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
print('tmptestsdir', tmptestsdir) print('tmptestsdir', tmptestsdir)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
os.mkdir('repo') os.mkdir('repo')
@ -873,7 +887,7 @@ class UpdateTest(unittest.TestCase):
fdroidserver.update.process_apks({}, 'repo', knownapks) fdroidserver.update.process_apks({}, 'repo', knownapks)
fdroidserver.update.process_apks({}, 'archive', knownapks) fdroidserver.update.process_apks({}, 'archive', knownapks)
disabledsigs = ['org.bitbucket.tickytacky.mirrormirror_2.apk', ] disabledsigs = ['org.bitbucket.tickytacky.mirrormirror_2.apk']
for apkName in disabledsigs: for apkName in disabledsigs:
shutil.copy(os.path.join(self.basedir, apkName), shutil.copy(os.path.join(self.basedir, apkName),
os.path.join(tmptestsdir, 'repo')) os.path.join(tmptestsdir, 'repo'))
@ -964,7 +978,9 @@ class UpdateTest(unittest.TestCase):
def test_translate_per_build_anti_features(self): def test_translate_per_build_anti_features(self):
os.chdir(os.path.join(localmodule, 'tests')) os.chdir(os.path.join(localmodule, 'tests'))
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo')
shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata') shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata')
@ -995,8 +1011,9 @@ class UpdateTest(unittest.TestCase):
self.assertTrue(foundtest) self.assertTrue(foundtest)
def test_create_metadata_from_template(self): def test_create_metadata_from_template(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
print('tmptestsdir', tmptestsdir) print('tmptestsdir', tmptestsdir)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
os.mkdir('repo') os.mkdir('repo')
@ -1097,8 +1114,9 @@ class UpdateTest(unittest.TestCase):
assert icons_src == {} assert icons_src == {}
def test_strip_and_copy_image(self): def test_strip_and_copy_image(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
in_file = os.path.join(self.basedir, 'metadata', 'info.guardianproject.urzip', 'en-US', 'images', 'icon.png') in_file = os.path.join(self.basedir, 'metadata', 'info.guardianproject.urzip', 'en-US', 'images', 'icon.png')
out_file = os.path.join(tmptestsdir, 'icon.png') out_file = os.path.join(tmptestsdir, 'icon.png')
@ -1115,7 +1133,9 @@ class UpdateTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
os.mkdir('metadata') os.mkdir('metadata')
with open('template.yml', 'w') as f: with open('template.yml', 'w') as f:
f.write(textwrap.dedent('''\ f.write(
textwrap.dedent(
'''\
Disabled: Disabled:
License: License:
AuthorName: AuthorName:
@ -1149,13 +1169,17 @@ class UpdateTest(unittest.TestCase):
CurrentVersion: CurrentVersion:
CurrentVersionCode: CurrentVersionCode:
NoSourceSince: NoSourceSince:
''')) '''
)
)
fdroidserver.update.create_metadata_from_template(apk) fdroidserver.update.create_metadata_from_template(apk)
with open(os.path.join('metadata', 'rocks.janicerand.yml')) as f: with open(os.path.join('metadata', 'rocks.janicerand.yml')) as f:
metadata_content = yaml.load(f, Loader=SafeLoader) metadata_content = yaml.load(f, Loader=SafeLoader)
self.maxDiff = None self.maxDiff = None
self.assertDictEqual(metadata_content, self.assertDictEqual(
{'ArchivePolicy': '', metadata_content,
{
'ArchivePolicy': '',
'AuthorEmail': '', 'AuthorEmail': '',
'AuthorName': '', 'AuthorName': '',
'AuthorWebSite': '', 'AuthorWebSite': '',
@ -1187,13 +1211,18 @@ class UpdateTest(unittest.TestCase):
'UpdateCheckMode': '', 'UpdateCheckMode': '',
'UpdateCheckName': '', 'UpdateCheckName': '',
'VercodeOperation': '', 'VercodeOperation': '',
'WebSite': ''}) 'WebSite': '',
},
)
def test_insert_funding_yml_donation_links(self): def test_insert_funding_yml_donation_links(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('build') os.mkdir('build')
content = textwrap.dedent(""" content = textwrap.dedent(
"""
community_bridge: '' community_bridge: ''
custom: [LINK1, LINK2] custom: [LINK1, LINK2]
github: USERNAME github: USERNAME
@ -1203,7 +1232,8 @@ class UpdateTest(unittest.TestCase):
open_collective: USERNAME open_collective: USERNAME
otechie: USERNAME otechie: USERNAME
patreon: USERNAME patreon: USERNAME
""") """
)
app = fdroidserver.metadata.App() app = fdroidserver.metadata.App()
app.id = 'fake.app.id' app.id = 'fake.app.id'
apps = {app.id: app} apps = {app.id: app}
@ -1230,7 +1260,9 @@ class UpdateTest(unittest.TestCase):
def test_insert_funding_yml_donation_links_one_at_a_time(self): def test_insert_funding_yml_donation_links_one_at_a_time(self):
"""Exercise the FUNDING.yml code one entry at a time""" """Exercise the FUNDING.yml code one entry at a time"""
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('build') os.mkdir('build')
@ -1242,7 +1274,8 @@ class UpdateTest(unittest.TestCase):
for field in DONATION_FIELDS: for field in DONATION_FIELDS:
self.assertIsNone(app.get(field)) self.assertIsNone(app.get(field))
content = textwrap.dedent(""" content = textwrap.dedent(
"""
community_bridge: 'blah-de-blah' community_bridge: 'blah-de-blah'
github: USERNAME github: USERNAME
issuehunt: USERNAME issuehunt: USERNAME
@ -1250,7 +1283,8 @@ class UpdateTest(unittest.TestCase):
liberapay: USERNAME liberapay: USERNAME
open_collective: USERNAME open_collective: USERNAME
patreon: USERNAME patreon: USERNAME
""") """
)
for line in content.split('\n'): for line in content.split('\n'):
if not line: if not line:
continue continue
@ -1270,7 +1304,9 @@ class UpdateTest(unittest.TestCase):
self.assertEqual(app.get('Donate', '').split('/')[-1], v) self.assertEqual(app.get('Donate', '').split('/')[-1], v)
def test_insert_funding_yml_donation_links_with_corrupt_file(self): def test_insert_funding_yml_donation_links_with_corrupt_file(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
os.mkdir('build') os.mkdir('build')
app = fdroidserver.metadata.App() app = fdroidserver.metadata.App()
@ -1278,11 +1314,15 @@ class UpdateTest(unittest.TestCase):
apps = {app.id: app} apps = {app.id: app}
os.mkdir(os.path.join('build', app.id)) os.mkdir(os.path.join('build', app.id))
with open(os.path.join('build', app.id, 'FUNDING.yml'), 'w') as fp: with open(os.path.join('build', app.id, 'FUNDING.yml'), 'w') as fp:
fp.write(textwrap.dedent(""" fp.write(
textwrap.dedent(
"""
opencollective: foo opencollective: foo
custom: [] custom: []
liberapay: : liberapay: :
""")) """
)
)
fdroidserver.update.insert_funding_yml_donation_links(apps) fdroidserver.update.insert_funding_yml_donation_links(apps)
for field in DONATION_FIELDS: for field in DONATION_FIELDS:
self.assertIsNone(app.get(field)) self.assertIsNone(app.get(field))
@ -1311,8 +1351,9 @@ class UpdateTest(unittest.TestCase):
self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(['first', 'second'])) self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(['first', 'second']))
def test_set_localized_text_entry(self): def test_set_localized_text_entry(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
config = dict() config = dict()
fdroidserver.common.fill_config_defaults(config) fdroidserver.common.fill_config_defaults(config)
@ -1342,8 +1383,9 @@ class UpdateTest(unittest.TestCase):
self.assertIsNone(app['localized'].get(locale, {}).get(key)) self.assertIsNone(app['localized'].get(locale, {}).get(key))
def test_set_author_entry(self): def test_set_author_entry(self):
tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, tmptestsdir = tempfile.mkdtemp(
dir=self.tmpdir) prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(tmptestsdir) os.chdir(tmptestsdir)
config = dict() config = dict()
fdroidserver.common.fill_config_defaults(config) fdroidserver.common.fill_config_defaults(config)
@ -1411,8 +1453,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()

View File

@ -13,7 +13,8 @@ import unittest
from git import Repo from git import Repo
localmodule = os.path.realpath( localmodule = os.path.realpath(
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')) os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
)
print('localmodule: ' + localmodule) print('localmodule: ' + localmodule)
if localmodule not in sys.path: if localmodule not in sys.path:
sys.path.insert(0, localmodule) sys.path.insert(0, localmodule)
@ -36,7 +37,9 @@ class VCSTest(unittest.TestCase):
os.chdir(self.basedir) os.chdir(self.basedir)
def test_remote_set_head_can_fail(self): def test_remote_set_head_can_fail(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) testdir = tempfile.mkdtemp(
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
)
os.chdir(testdir) os.chdir(testdir)
# First create an upstream repo with one commit # First create an upstream repo with one commit
upstream_repo = Repo.init("upstream_repo") upstream_repo = Repo.init("upstream_repo")
@ -80,8 +83,13 @@ if __name__ == "__main__":
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option(
help="Spew out even more information than normal") "-v",
"--verbose",
action="store_true",
default=False,
help="Spew out even more information than normal",
)
(fdroidserver.common.options, args) = parser.parse_args(['--verbose']) (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()