added signatures subcommand

This commit is contained in:
Michael Pöhn 2017-09-06 15:54:16 +02:00
parent be874b1134
commit 3e6dfacf6c
5 changed files with 238 additions and 1 deletions

65
tests/signatures.TestCase Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python3
import inspect
import optparse
import os
import sys
import unittest
import hashlib
import logging
from tempfile import TemporaryDirectory
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)
from testcommon import TmpCwd
from fdroidserver import common, signatures
class SignaturesTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.DEBUG)
common.config = None
config = common.read_config(common.options)
config['jarsigner'] = common.find_sdk_tools_cmd('jarsigner')
config['verbose'] = True
common.config = config
def test_main(self):
# option fixture class:
class OptionsFixture:
APK = [os.path.abspath(os.path.join('repo', 'com.politedroid_3.apk'))]
with TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
signatures.extract(common.config, OptionsFixture())
# check if extracted signatures are where they are supposed to be
# also verify weather if extracted file contian what they should
filesAndHashes = (
(os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'MANIFEST.MF'),
'7dcd83f0c41a75457fd2311bf3c4578f80d684362d74ba8dc52838d353f31cf2'),
(os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'RELEASE.RSA'),
'883ef3d5a6e0bf69d2a58d9e255a7930f08a49abc38e216ed054943c99c8fdb4'),
(os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'RELEASE.SF'),
'99fbb3211ef5d7c1253f3a7ad4836eadc9905103ce6a75916c40de2831958284'),
)
for path, checksum in filesAndHashes:
self.assertTrue(os.path.isfile(path))
with open(path, 'rb') as f:
self.assertEqual(hashlib.sha256(f.read()).hexdigest(), checksum)
if __name__ == "__main__":
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(SignaturesTest))
unittest.main()

34
tests/testcommon.py Normal file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env python3
#
# Copyright (C) 2017, Michael Poehn <michael.poehn@fsfe.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
class TmpCwd():
"""Context-manager for temporarily changing the current working
directory.
"""
def __init__(self, new_cwd):
self.new_cwd = new_cwd
def __enter__(self):
self.orig_cwd = os.getcwd()
os.chdir(self.new_cwd)
def __exit__(self, a, b, c):
os.chdir(self.orig_cwd)