chiark / gitweb /
tests: pre-set failfast as reminder of a handy time saver
[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 optparse
15 import os
16 import sys
17 import unittest
18 import tempfile
19 import textwrap
20
21 localmodule = os.path.realpath(
22     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
23 print('localmodule: ' + localmodule)
24 if localmodule not in sys.path:
25     sys.path.insert(0, localmodule)
26
27 from fdroidserver import publish
28 from fdroidserver import common
29 from fdroidserver.exception import FDroidException
30
31
32 class PublishTest(unittest.TestCase):
33     '''fdroidserver/publish.py'''
34
35     def test_key_alias(self):
36         publish.config = {}
37         self.assertEqual('a163ec9b', publish.key_alias('com.example.app'))
38         self.assertEqual('d2d51ff2', publish.key_alias('com.example.anotherapp'))
39         self.assertEqual('dc3b169e', publish.key_alias('org.test.testy'))
40         self.assertEqual('78688a0f', publish.key_alias('org.org.org'))
41
42         publish.config = {'keyaliases': {'yep.app': '@org.org.org',
43                                          'com.example.app': '1a2b3c4d'}}
44         self.assertEqual('78688a0f', publish.key_alias('yep.app'))
45         self.assertEqual('1a2b3c4d', publish.key_alias('com.example.app'))
46
47     def test_read_fingerprints_from_keystore(self):
48         common.config = {}
49         common.fill_config_defaults(common.config)
50         publish.config = common.config
51         publish.config['keystorepass'] = '123456'
52         publish.config['keypass'] = '123456'
53         publish.config['keystore'] = 'dummy-keystore.jks'
54
55         expected = {'78688a0f': '277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82',
56                     'd2d51ff2': 'fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3',
57                     'dc3b169e': '6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c',
58                     'a163ec9b': 'd34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4',
59                     'repokey': 'c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41'}
60         result = publish.read_fingerprints_from_keystore()
61         self.maxDiff = None
62         self.assertEqual(expected, result)
63
64     def test_store_and_load_fdroid_signing_key_fingerprints(self):
65         common.config = {}
66         common.fill_config_defaults(common.config)
67         publish.config = common.config
68         publish.config['keystorepass'] = '123456'
69         publish.config['keypass'] = '123456'
70         publish.config['keystore'] = os.path.join(os.getcwd(),
71                                                   'dummy-keystore.jks')
72         publish.config['repo_keyalias'] = 'repokey'
73
74         appids = ['com.example.app',
75                   'net.unavailable',
76                   'org.test.testy',
77                   'com.example.anotherapp',
78                   'org.org.org']
79
80         with tempfile.TemporaryDirectory() as tmpdir:
81             orig_cwd = os.getcwd()
82             try:
83                 os.chdir(tmpdir)
84                 with open('config.py', 'w') as f:
85                     pass
86
87                 publish.store_stats_fdroid_signing_key_fingerprints(appids, indent=2)
88
89                 self.maxDiff = None
90                 expected = {
91                     "com.example.anotherapp": {
92                         "signer": "fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3"
93                     },
94                     "com.example.app": {
95                         "signer": "d34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4"
96                     },
97                     "org.org.org": {
98                         "signer": "277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82"
99                     },
100                     "org.test.testy": {
101                         "signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c"
102                     }
103                 }
104                 self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints())
105
106                 with open('config.py', 'r') as f:
107                     self.assertEqual(textwrap.dedent('''\
108
109                         repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41"
110                         '''), f.read())
111             finally:
112                 os.chdir(orig_cwd)
113
114     def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self):
115         common.config = {}
116         common.fill_config_defaults(common.config)
117         publish.config = common.config
118         publish.config['keystorepass'] = '123456'
119         publish.config['keypass'] = '123456'
120         publish.config['keystore'] = os.path.join(os.getcwd(),
121                                                   'dummy-keystore.jks')
122         publish.config['repo_keyalias'] = 'repokey'
123         publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad'
124
125         with tempfile.TemporaryDirectory() as tmpdir:
126             orig_cwd = os.getcwd()
127             try:
128                 os.chdir(tmpdir)
129                 publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2)
130                 with self.assertRaises(FDroidException):
131                     common.load_stats_fdroid_signing_key_fingerprints()
132             finally:
133                 os.chdir(orig_cwd)
134
135
136 if __name__ == "__main__":
137     if os.path.basename(os.getcwd()) != 'tests' and os.path.isdir('tests'):
138         os.chdir('tests')
139
140     parser = optparse.OptionParser()
141     parser.add_option("-v", "--verbose", action="store_true", default=False,
142                       help="Spew out even more information than normal")
143     (common.options, args) = parser.parse_args(['--verbose'])
144
145     newSuite = unittest.TestSuite()
146     newSuite.addTest(unittest.makeSuite(PublishTest))
147     unittest.main(failfast=False)