2022-04-25 22:25:32 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
localmodule = os.path.realpath(
|
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
|
|
|
)
|
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
if localmodule not in sys.path:
|
|
|
|
sys.path.insert(0, localmodule)
|
|
|
|
|
2022-05-17 12:57:44 +02:00
|
|
|
from fdroidserver import common, signindex, update
|
2022-04-25 22:25:32 +02:00
|
|
|
from pathlib import Path
|
2022-05-17 12:57:44 +02:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
|
|
class Options:
|
|
|
|
allow_disabled_algorithms = False
|
|
|
|
clean = False
|
|
|
|
delete_unknown = False
|
|
|
|
nosign = False
|
|
|
|
pretty = True
|
|
|
|
rename_apks = False
|
|
|
|
verbose = False
|
2022-04-25 22:25:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SignindexTest(unittest.TestCase):
|
|
|
|
|
|
|
|
basedir = Path(__file__).resolve().parent
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
signindex.config = None
|
|
|
|
config = common.read_config(common.options)
|
|
|
|
config['jarsigner'] = common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
config['verbose'] = True
|
|
|
|
config['keystore'] = str(self.basedir / 'keystore.jks')
|
|
|
|
config['repo_keyalias'] = 'sova'
|
|
|
|
config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
|
|
|
|
config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI='
|
|
|
|
signindex.config = config
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
self.tempdir = tempfile.TemporaryDirectory()
|
|
|
|
os.chdir(self.tempdir.name)
|
|
|
|
self.repodir = Path('repo')
|
|
|
|
self.repodir.mkdir()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.tempdir.cleanup()
|
|
|
|
|
2022-05-23 12:39:17 +02:00
|
|
|
def test_sign_index(self):
|
2022-04-25 22:25:32 +02:00
|
|
|
shutil.copy(str(self.basedir / 'repo/index-v1.json'), 'repo')
|
2022-05-23 12:39:17 +02:00
|
|
|
signindex.sign_index(str(self.repodir), 'index-v1.json')
|
2022-04-25 22:25:32 +02:00
|
|
|
self.assertTrue((self.repodir / 'index-v1.jar').exists())
|
2022-05-17 12:57:44 +02:00
|
|
|
self.assertTrue((self.repodir / 'index-v1.json').exists())
|
2022-04-25 22:25:32 +02:00
|
|
|
|
2022-05-23 12:39:17 +02:00
|
|
|
def test_sign_index_corrupt(self):
|
2022-04-25 22:25:32 +02:00
|
|
|
with open('repo/index-v1.json', 'w') as fp:
|
|
|
|
fp.write('corrupt JSON!')
|
|
|
|
with self.assertRaises(json.decoder.JSONDecodeError, msg='error on bad JSON'):
|
2022-05-23 12:39:17 +02:00
|
|
|
signindex.sign_index(str(self.repodir), 'index-v1.json')
|
2022-04-25 22:25:32 +02:00
|
|
|
|
2022-05-17 12:57:44 +02:00
|
|
|
def test_signindex(self):
|
|
|
|
os.mkdir('archive')
|
|
|
|
metadata = Path('metadata')
|
|
|
|
metadata.mkdir()
|
|
|
|
with (metadata / 'info.guardianproject.urzip.yml').open('w') as fp:
|
|
|
|
fp.write('# placeholder')
|
|
|
|
shutil.copy(str(self.basedir / 'urzip.apk'), 'repo')
|
|
|
|
index_files = []
|
|
|
|
for f in ('index.xml', 'index.jar', 'index-v1.json', 'index-v1.jar'):
|
|
|
|
for section in (Path('repo'), Path('archive')):
|
|
|
|
path = section / f
|
|
|
|
self.assertFalse(path.exists(), '%s should not exist yet!' % path)
|
|
|
|
index_files.append(path)
|
|
|
|
common.options = Options
|
|
|
|
with patch('sys.argv', ['fdroid update']):
|
|
|
|
update.main()
|
|
|
|
with patch('sys.argv', ['fdroid signindex', '--verbose']):
|
|
|
|
signindex.main()
|
|
|
|
for f in index_files:
|
|
|
|
self.assertTrue(f.exists(), '%s should exist!' % f)
|
|
|
|
|
2022-04-25 22:25:32 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
|
|
|
(common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(SignindexTest))
|
|
|
|
unittest.main(failfast=False)
|