chiark / gitweb /
basebox config
[fdroidserver.git] / tests / publish.TestCase
1 #!/usr/bin/env python3
2
3 #
4 #  command which created the keystore used in this test case:
5 #
6 #  $ for ALIAS in 'repokey a163ec9b d2d51ff2 dc3b169e 78688a0f'; \
7 #        do keytool -genkey -keystore dummy-keystore.jks \
8 #        -alias $ALIAS -keyalg 'RSA' -keysize '2048' \
9 #        -validity '10000' -storepass 123456 \
10 #        -keypass 123456 -dname 'CN=test, OU=F-Droid'; done
11 #
12
13 import inspect
14 import logging
15 import optparse
16 import os
17 import sys
18 import unittest
19 import tempfile
20 import textwrap
21
22 localmodule = os.path.realpath(
23     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
24 print('localmodule: ' + localmodule)
25 if localmodule not in sys.path:
26     sys.path.insert(0, localmodule)
27
28 from fdroidserver import publish
29 from fdroidserver import common
30 from fdroidserver.exception import FDroidException
31
32
33 class PublishTest(unittest.TestCase):
34     '''fdroidserver/publish.py'''
35
36     def setUp(self):
37         logging.basicConfig(level=logging.DEBUG)
38         self.basedir = os.path.join(localmodule, 'tests')
39         self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
40         if not os.path.exists(self.tmpdir):
41             os.makedirs(self.tmpdir)
42         os.chdir(self.basedir)
43
44     def test_key_alias(self):
45         publish.config = {}
46         self.assertEqual('a163ec9b', publish.key_alias('com.example.app'))
47         self.assertEqual('d2d51ff2', publish.key_alias('com.example.anotherapp'))
48         self.assertEqual('dc3b169e', publish.key_alias('org.test.testy'))
49         self.assertEqual('78688a0f', publish.key_alias('org.org.org'))
50
51         publish.config = {'keyaliases': {'yep.app': '@org.org.org',
52                                          'com.example.app': '1a2b3c4d'}}
53         self.assertEqual('78688a0f', publish.key_alias('yep.app'))
54         self.assertEqual('1a2b3c4d', publish.key_alias('com.example.app'))
55
56     def test_read_fingerprints_from_keystore(self):
57         common.config = {}
58         common.fill_config_defaults(common.config)
59         publish.config = common.config
60         publish.config['keystorepass'] = '123456'
61         publish.config['keypass'] = '123456'
62         publish.config['keystore'] = 'dummy-keystore.jks'
63
64         expected = {'78688a0f': '277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82',
65                     'd2d51ff2': 'fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3',
66                     'dc3b169e': '6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c',
67                     'a163ec9b': 'd34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4',
68                     'repokey': 'c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41'}
69         result = publish.read_fingerprints_from_keystore()
70         self.maxDiff = None
71         self.assertEqual(expected, result)
72
73     def test_store_and_load_fdroid_signing_key_fingerprints(self):
74         common.config = {}
75         common.fill_config_defaults(common.config)
76         publish.config = common.config
77         publish.config['keystorepass'] = '123456'
78         publish.config['keypass'] = '123456'
79         publish.config['keystore'] = os.path.join(os.getcwd(),
80                                                   'dummy-keystore.jks')
81         publish.config['repo_keyalias'] = 'repokey'
82
83         appids = ['com.example.app',
84                   'net.unavailable',
85                   'org.test.testy',
86                   'com.example.anotherapp',
87                   'org.org.org']
88
89         testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
90         os.chdir(testdir)
91         with open('config.py', 'w') as f:
92             pass
93
94         publish.store_stats_fdroid_signing_key_fingerprints(appids, indent=2)
95
96         self.maxDiff = None
97         expected = {
98             "com.example.anotherapp": {
99                 "signer": "fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3"
100             },
101             "com.example.app": {
102                 "signer": "d34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4"
103             },
104             "org.org.org": {
105                 "signer": "277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82"
106             },
107             "org.test.testy": {
108                 "signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c"
109             }
110         }
111         self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints())
112
113         with open('config.py', 'r') as f:
114             self.assertEqual(textwrap.dedent('''\
115
116                 repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41"
117                 '''), f.read())
118
119     def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self):
120         common.config = {}
121         common.fill_config_defaults(common.config)
122         publish.config = common.config
123         publish.config['keystorepass'] = '123456'
124         publish.config['keypass'] = '123456'
125         publish.config['keystore'] = os.path.join(os.getcwd(),
126                                                   'dummy-keystore.jks')
127         publish.config['repo_keyalias'] = 'repokey'
128         publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad'
129
130         testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
131         os.chdir(testdir)
132         publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2)
133         with self.assertRaises(FDroidException):
134             common.load_stats_fdroid_signing_key_fingerprints()
135
136
137 if __name__ == "__main__":
138     parser = optparse.OptionParser()
139     parser.add_option("-v", "--verbose", action="store_true", default=False,
140                       help="Spew out even more information than normal")
141     (common.options, args) = parser.parse_args(['--verbose'])
142
143     newSuite = unittest.TestSuite()
144     newSuite.addTest(unittest.makeSuite(PublishTest))
145     unittest.main(failfast=False)