File: test_main.py

package info (click to toggle)
fdroidserver 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,948 kB
  • sloc: python: 34,139; xml: 2,186; sh: 1,362; java: 293; makefile: 54; javascript: 23
file content (246 lines) | stat: -rwxr-xr-x 9,784 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3

import os
import pkgutil
import textwrap
import unittest
import tempfile
from unittest import mock

import fdroidserver.__main__
from .shared_test_code import TmpCwd, TmpPyPath


class MainTest(unittest.TestCase):
    '''this tests fdroid.py'''

    def test_COMMANDS_check(self):
        """make sure the built in sub-command defs didn't change unintentionally"""
        self.assertListEqual(
            [x for x in fdroidserver.__main__.COMMANDS],
            [
                'build',
                'init',
                'publish',
                'gpgsign',
                'update',
                'deploy',
                'verify',
                'checkupdates',
                'import',
                'install',
                'readmeta',
                'rewritemeta',
                'lint',
                'scanner',
                'signindex',
                'btlog',
                'signatures',
                'nightly',
                'mirror',
            ],
        )

    def test_call_init(self):
        co = mock.Mock()
        with mock.patch('sys.argv', ['', 'init', '-h']):
            with mock.patch('fdroidserver.init.main', co):
                with mock.patch('sys.exit') as exit_mock:
                    fdroidserver.__main__.main()
                    # note: this is sloppy, if `init` changes
                    # this might need changing too
                    exit_mock.assert_called_once_with(0)
        co.assert_called_once_with()

    def test_call_deploy(self):
        co = mock.Mock()
        with mock.patch('sys.argv', ['', 'deploy', '-h']):
            with mock.patch('fdroidserver.deploy.main', co):
                with mock.patch('sys.exit') as exit_mock:
                    fdroidserver.__main__.main()
                    # note: this is sloppy, if `deploy` changes
                    # this might need changing too
                    exit_mock.assert_called_once_with(0)
        co.assert_called_once_with()

    def test_find_plugins(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy1.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        main = lambda: 'all good'"""
                    )
                )
            with TmpPyPath(tmpdir):
                plugins = fdroidserver.__main__.find_plugins()
                self.assertIn('testy1', plugins.keys())
                self.assertEqual(plugins['testy1']['summary'], 'ttt')
                self.assertEqual(
                    __import__(
                        plugins['testy1']['name'], None, None, ['testy1']
                    ).main(),
                    'all good',
                )

    def test_main_plugin_lambda(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy2.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        main = lambda: print('all good')"""
                    )
                )
            with TmpPyPath(tmpdir):
                with mock.patch('sys.argv', ['', 'testy2']):
                    with mock.patch('sys.exit') as exit_mock:
                        fdroidserver.__main__.main()
                        exit_mock.assert_called_once_with(0)

    def test_main_plugin_def(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy3.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        def main():
                            print('all good')"""
                    )
                )
            with TmpPyPath(tmpdir):
                with mock.patch('sys.argv', ['', 'testy3']):
                    with mock.patch('sys.exit') as exit_mock:
                        fdroidserver.__main__.main()
                        exit_mock.assert_called_once_with(0)

    def test_main_broken_plugin(self):
        """making sure broken plugins get their exceptions through"""
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy4.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        def main():
                            raise Exception("this plugin is broken")"""
                    )
                )
            with TmpPyPath(tmpdir):
                with mock.patch('sys.argv', ['', 'testy4']):
                    with self.assertRaisesRegex(Exception, "this plugin is broken"):
                        fdroidserver.__main__.main()

    def test_main_malicious_plugin(self):
        """The purpose of this test is to make sure code in plugins
        doesn't get executed unintentionally.
        """
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy5.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        raise Exception("this plugin is malicious")
                        def main():
                            print("evil things")"""
                    )
                )
            with TmpPyPath(tmpdir):
                with mock.patch('sys.argv', ['', 'lint']):
                    with mock.patch('sys.exit') as exit_mock:
                        fdroidserver.__main__.main()
                        # note: this is sloppy, if `lint` changes
                        # this might need changing too
                        exit_mock.assert_called_once_with(0)

    def test_main_prevent_plugin_override(self):
        """making sure build-in subcommands cannot be overridden by plugins"""
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_signatures.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        def main():
                            raise("plugin overrides don't get prevent!")"""
                    )
                )
            with TmpPyPath(tmpdir):
                with mock.patch('sys.argv', ['', 'signatures']):
                    with mock.patch('sys.exit') as exit_mock:
                        fdroidserver.__main__.main()
                        # note: this is sloppy, if `signatures` changes
                        # this might need changing too
                        self.assertEqual(exit_mock.call_count, 2)

    def test_preparse_plugin_lookup_bad_name(self):
        self.assertRaises(
            ValueError,
            fdroidserver.__main__.preparse_plugin,
            "some.package",
            "/non/existent/module/path",
        )

    def test_preparse_plugin_lookup_bad_path(self):
        self.assertRaises(
            ValueError,
            fdroidserver.__main__.preparse_plugin,
            "fake_module_name",
            "/non/existent/module/path",
        )

    def test_preparse_plugin_lookup_summary_missing(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy6.py', 'w') as f:
                f.write("main = lambda: print('all good')")
            with TmpPyPath(tmpdir):
                p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
                module_dir = p[0][0].path
                module_name = p[0][1]
                self.assertRaises(
                    NameError,
                    fdroidserver.__main__.preparse_plugin,
                    module_name,
                    module_dir,
                )

    def test_preparse_plugin_lookup_module_file(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            with open('fdroid_testy7.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        main = lambda: pritn('all good')"""
                    )
                )
            with TmpPyPath(tmpdir):
                p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
                module_path = p[0][0].path
                module_name = p[0][1]
                d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
            self.assertDictEqual(d, {'name': 'fdroid_testy7', 'summary': 'ttt'})

    def test_preparse_plugin_lookup_module_dir(self):
        with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
            os.mkdir(os.path.join(tmpdir, 'fdroid_testy8'))
            with open('fdroid_testy8/__main__.py', 'w') as f:
                f.write(
                    textwrap.dedent(
                        """\
                        fdroid_summary = "ttt"
                        main = lambda: print('all good')"""
                    )
                )
            with open('fdroid_testy8/__init__.py', 'w') as f:
                pass
            with TmpPyPath(tmpdir):
                p = [x for x in pkgutil.iter_modules() if x[1].startswith('fdroid_')]
                module_path = p[0][0].path
                module_name = p[0][1]
                d = fdroidserver.__main__.preparse_plugin(module_name, module_path)
            self.assertDictEqual(d, {'name': 'fdroid_testy8', 'summary': 'ttt'})