chiark / gitweb /
common: use python instead of calling out to 'rm'
[fdroidserver.git] / tests / signatures.TestCase
1 #!/usr/bin/env python3
2
3 import inspect
4 import optparse
5 import os
6 import sys
7 import unittest
8 import hashlib
9 import logging
10 from tempfile import TemporaryDirectory
11
12 localmodule = os.path.realpath(
13     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
14 print('localmodule: ' + localmodule)
15 if localmodule not in sys.path:
16     sys.path.insert(0, localmodule)
17
18 from testcommon import TmpCwd
19 from fdroidserver import common, signatures
20
21
22 class SignaturesTest(unittest.TestCase):
23
24     def setUp(self):
25         logging.basicConfig(level=logging.DEBUG)
26         common.config = None
27         config = common.read_config(common.options)
28         config['jarsigner'] = common.find_sdk_tools_cmd('jarsigner')
29         config['verbose'] = True
30         common.config = config
31
32     def test_main(self):
33
34         # option fixture class:
35         class OptionsFixture:
36             APK = [os.path.abspath(os.path.join('repo', 'com.politedroid_3.apk'))]
37
38         with TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
39             signatures.extract(common.config, OptionsFixture())
40
41             # check if extracted signatures are where they are supposed to be
42             # also verify weather if extracted file contian what they should
43             filesAndHashes = (
44                 (os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'MANIFEST.MF'),
45                  '7dcd83f0c41a75457fd2311bf3c4578f80d684362d74ba8dc52838d353f31cf2'),
46                 (os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'RELEASE.RSA'),
47                  '883ef3d5a6e0bf69d2a58d9e255a7930f08a49abc38e216ed054943c99c8fdb4'),
48                 (os.path.join('metadata', 'com.politedroid', 'signatures', '3', 'RELEASE.SF'),
49                  '99fbb3211ef5d7c1253f3a7ad4836eadc9905103ce6a75916c40de2831958284'),
50             )
51             for path, checksum in filesAndHashes:
52                 self.assertTrue(os.path.isfile(path))
53                 with open(path, 'rb') as f:
54                     self.assertEqual(hashlib.sha256(f.read()).hexdigest(), checksum)
55
56
57 if __name__ == "__main__":
58     parser = optparse.OptionParser()
59     parser.add_option("-v", "--verbose", action="store_true", default=False,
60                       help="Spew out even more information than normal")
61     (common.options, args) = parser.parse_args(['--verbose'])
62
63     newSuite = unittest.TestSuite()
64     newSuite.addTest(unittest.makeSuite(SignaturesTest))
65     unittest.main(failfast=False)