Completed
Push — master ( f91f3e...c1f827 )
by
unknown
17s queued 11s
created

RootArgumentsParserTest.test_gmp_password_after_subparser()   A

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nop 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import os
20
import sys
21
import unittest
22
23
from pathlib import Path
24
25
from gvm.connections import DEFAULT_UNIX_SOCKET_PATH, DEFAULT_TIMEOUT
26
27
from gvmtools.parser import CliParser
28
29
from . import SuppressOutput
30
31
__here__ = Path(__file__).parent.resolve()
32
33
34
class ConfigParserTestCase(unittest.TestCase):
35
    def setUp(self):
36
        self.test_config_path = __here__ / 'test.cfg'
37
38
        self.assertTrue(self.test_config_path.is_file())
39
40
        self.parser = CliParser('TestParser', 'test.log')
41
42
    def test_socket_defaults_from_config(self):
43
        args = self.parser.parse_args(
44
            ['--config', str(self.test_config_path), 'socket']
45
        )
46
47
        self.assertEqual(args.foo, 'bar')
48
        self.assertEqual(args.timeout, 1000)
49
        self.assertEqual(args.gmp_password, 'bar')
50
        self.assertEqual(args.gmp_username, 'bar')
51
        self.assertEqual(args.socketpath, '/foo/bar.sock')
52
53
    def test_ssh_defaults_from_config(self):
54
        args = self.parser.parse_args(
55
            ['--config', str(self.test_config_path), 'ssh', '--hostname', 'foo']
56
        )
57
58
        self.assertEqual(args.foo, 'bar')
59
        self.assertEqual(args.timeout, 1000)
60
        self.assertEqual(args.gmp_password, 'bar')
61
        self.assertEqual(args.gmp_username, 'bar')
62
        self.assertEqual(args.ssh_password, 'lorem')
63
        self.assertEqual(args.ssh_username, 'ipsum')
64
        self.assertEqual(args.port, 123)
65
66
    def test_tls_defaults_from_config(self):
67
        args = self.parser.parse_args(
68
            ['--config', str(self.test_config_path), 'tls', '--hostname', 'foo']
69
        )
70
71
        self.assertEqual(args.foo, 'bar')
72
        self.assertEqual(args.timeout, 1000)
73
        self.assertEqual(args.gmp_password, 'bar')
74
        self.assertEqual(args.gmp_username, 'bar')
75
        self.assertEqual(args.certfile, 'foo.cert')
76
        self.assertEqual(args.keyfile, 'foo.key')
77
        self.assertEqual(args.cafile, 'foo.ca')
78
        self.assertEqual(args.port, 123)
79
80
81
class IgnoreConfigParserTestCase(unittest.TestCase):
82
    def test_unkown_config_file(self):
83
        test_config_path = __here__ / 'foo.cfg'
84
85
        self.assertFalse(test_config_path.is_file())
86
87
        self.parser = CliParser('TestParser', 'test.log')
88
89
        args = self.parser.parse_args(
90
            ['--config', str(test_config_path), 'socket']
91
        )
92
93
        self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
94
        self.assertEqual(args.gmp_password, '')
95
        self.assertEqual(args.gmp_username, '')
96
        self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)
97
98
    def test_unkown_config_file_in_unkown_dir(self):
99
        test_config_path = __here__ / 'foo' / 'foo.cfg'
100
101
        self.assertFalse(test_config_path.is_file())
102
103
        self.parser = CliParser('TestParser', 'test.log')
104
105
        args = self.parser.parse_args(
106
            ['--config', str(test_config_path), 'socket']
107
        )
108
109
        self.assertEqual(args.timeout, DEFAULT_TIMEOUT)
110
        self.assertEqual(args.gmp_password, '')
111
        self.assertEqual(args.gmp_username, '')
112
        self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)
113
114
115
class ParserTestCase(unittest.TestCase):
116
    def setUp(self):
117
        self.parser = CliParser(
118
            'TestParser', 'test.log', ignore_config=True, prog='gvm-test-cli'
119
        )
120
121
122
class RootArgumentsParserTest(ParserTestCase):
123
    def test_config(self):
124
        args = self.parser.parse_args(['--config', 'foo.cfg', 'socket'])
125
        self.assertEqual(args.config, 'foo.cfg')
126
127
    def test_defaults(self):
128
        args = self.parser.parse_args(['socket'])
129
        self.assertEqual(args.config, '~/.config/gvm-tools.conf')
130
        self.assertEqual(args.gmp_password, '')
131
        self.assertEqual(args.gmp_username, '')
132
        self.assertEqual(args.timeout, 60)
133
        self.assertIsNone(args.loglevel)
134
135
    def test_loglevel(self):
136
        args = self.parser.parse_args(['--log', 'ERROR', 'socket'])
137
        self.assertEqual(args.loglevel, 'ERROR')
138
139
    def test_loglevel_after_subparser(self):
140
        with SuppressOutput(suppress_stderr=True):
141
            with self.assertRaises(SystemExit):
142
                self.parser.parse_args(['socket', '--log', 'ERROR'])
143
144
    def test_timeout(self):
145
        args = self.parser.parse_args(['--timeout', '1000', 'socket'])
146
        self.assertEqual(args.timeout, 1000)
147
148
    def test_timeout_after_subparser(self):
149
        with SuppressOutput(suppress_stderr=True):
150
            with self.assertRaises(SystemExit):
151
                self.parser.parse_args(['socket', '--timeout', '1000'])
152
153
    def test_gmp_username(self):
154
        args = self.parser.parse_args(['--gmp-username', 'foo', 'socket'])
155
        self.assertEqual(args.gmp_username, 'foo')
156
157
    def test_gmp_username_after_subparser(self):
158
        with SuppressOutput(suppress_stderr=True):
159
            with self.assertRaises(SystemExit):
160
                self.parser.parse_args(['socket', '--gmp-username', 'foo'])
161
162
    def test_gmp_password(self):
163
        args = self.parser.parse_args(['--gmp-password', 'foo', 'socket'])
164
        self.assertEqual(args.gmp_password, 'foo')
165
166
    def test_gmp_password_after_subparser(self):
167
        with SuppressOutput(suppress_stderr=True):
168
            with self.assertRaises(SystemExit):
169
                self.parser.parse_args(['socket', '--gmp-password', 'foo'])
170
171
    def test_with_unknown_args(self):
172
        args, script_args = self.parser.parse_known_args(
173
            ['--gmp-password', 'foo', 'socket', '--bar', '--bar2']
174
        )
175
        self.assertEqual(args.gmp_password, 'foo')
176
        self.assertEqual(script_args, ['--bar', '--bar2'])
177
178
179
class SocketParserTestCase(ParserTestCase):
180
    def test_defaults(self):
181
        args = self.parser.parse_args(['socket'])
182
        self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH)
183
184
    def test_connection_type(self):
185
        args = self.parser.parse_args(['socket'])
186
        self.assertEqual(args.connection_type, 'socket')
187
188
    def test_sockpath(self):
189
        args = self.parser.parse_args(['socket', '--sockpath', 'foo.sock'])
190
        self.assertEqual(args.socketpath, 'foo.sock')
191
192
    def test_socketpath(self):
193
        args = self.parser.parse_args(['socket', '--socketpath', 'foo.sock'])
194
        self.assertEqual(args.socketpath, 'foo.sock')
195
196
197
class SshParserTestCase(ParserTestCase):
198
    def test_defaults(self):
199
        args = self.parser.parse_args(['ssh', '--hostname=foo'])
200
        self.assertEqual(args.port, 22)
201
        self.assertEqual(args.ssh_username, 'gmp')
202
        self.assertEqual(args.ssh_password, 'gmp')
203
204
    def test_connection_type(self):
205
        args = self.parser.parse_args(['ssh', '--hostname=foo'])
206
        self.assertEqual(args.connection_type, 'ssh')
207
208
    def test_hostname(self):
209
        args = self.parser.parse_args(['ssh', '--hostname', 'foo'])
210
        self.assertEqual(args.hostname, 'foo')
211
212
    def test_port(self):
213
        args = self.parser.parse_args(
214
            ['ssh', '--hostname', 'foo', '--port', '123']
215
        )
216
        self.assertEqual(args.port, 123)
217
218
    def test_ssh_username(self):
219
        args = self.parser.parse_args(
220
            ['ssh', '--hostname', 'foo', '--ssh-username', 'foo']
221
        )
222
        self.assertEqual(args.ssh_username, 'foo')
223
224
    def test_ssh_password(self):
225
        args = self.parser.parse_args(
226
            ['ssh', '--hostname', 'foo', '--ssh-password', 'foo']
227
        )
228
        self.assertEqual(args.ssh_password, 'foo')
229
230
231
class TlsParserTestCase(ParserTestCase):
232
    def test_defaults(self):
233
        args = self.parser.parse_args(['tls', '--hostname=foo'])
234
        self.assertIsNone(args.certfile)
235
        self.assertIsNone(args.keyfile)
236
        self.assertIsNone(args.cafile)
237
        self.assertEqual(args.port, 9390)
238
239
    def test_connection_type(self):
240
        args = self.parser.parse_args(['tls', '--hostname=foo'])
241
        self.assertEqual(args.connection_type, 'tls')
242
243
    def test_hostname(self):
244
        args = self.parser.parse_args(['tls', '--hostname', 'foo'])
245
        self.assertEqual(args.hostname, 'foo')
246
247
    def test_port(self):
248
        args = self.parser.parse_args(
249
            ['tls', '--hostname', 'foo', '--port', '123']
250
        )
251
        self.assertEqual(args.port, 123)
252
253
    def test_certfile(self):
254
        args = self.parser.parse_args(
255
            ['tls', '--hostname', 'foo', '--certfile', 'foo.cert']
256
        )
257
        self.assertEqual(args.certfile, 'foo.cert')
258
259
    def test_keyfile(self):
260
        args = self.parser.parse_args(
261
            ['tls', '--hostname', 'foo', '--keyfile', 'foo.key']
262
        )
263
        self.assertEqual(args.keyfile, 'foo.key')
264
265
    def test_cafile(self):
266
        args = self.parser.parse_args(
267
            ['tls', '--hostname', 'foo', '--cafile', 'foo.ca']
268
        )
269
        self.assertEqual(args.cafile, 'foo.ca')
270
271
    def test_no_credentials(self):
272
        args = self.parser.parse_args(
273
            ['tls', '--hostname', 'foo', '--no-credentials']
274
        )
275
        self.assertTrue(args.no_credentials)
276
277
278
class CustomizeParserTestCase(ParserTestCase):
279
    def test_add_optional_argument(self):
280
        self.parser.add_argument('--foo', type=int)
281
282
        args = self.parser.parse_args(['socket', '--foo', '123'])
283
        self.assertEqual(args.foo, 123)
284
285
        args = self.parser.parse_args(
286
            ['ssh', '--hostname', 'bar', '--foo', '123']
287
        )
288
        self.assertEqual(args.foo, 123)
289
290
        args = self.parser.parse_args(
291
            ['tls', '--hostname', 'bar', '--foo', '123']
292
        )
293
        self.assertEqual(args.foo, 123)
294
295
    def test_add_positional_argument(self):
296
        self.parser.add_argument('foo', type=int)
297
        args = self.parser.parse_args(['socket', '123'])
298
299
        self.assertEqual(args.foo, 123)
300
301
    def test_add_protocol_argument(self):
302
        self.parser.add_protocol_argument()
303
304
        args = self.parser.parse_args(['socket'])
305
        self.assertEqual(args.protocol, 'GMP')
306
307
        args = self.parser.parse_args(['--protocol', 'OSP', 'socket'])
308
309
        self.assertEqual(args.protocol, 'OSP')
310
311
312
class HelpFormattingParserTestCase(ParserTestCase):
313
    # pylint: disable=protected-access
314
    maxDiff = None
315
    python_version = '.'.join([str(i) for i in sys.version_info[:2]])
316
317
    def setUp(self):
318
        super().setUp()
319
320
        # ensure all tests are using the same terminal width
321
        self.columns = os.environ.get('COLUMNS')
322
        os.environ['COLUMNS'] = '80'
323
324
    def tearDown(self):
325
        super().tearDown()
326
327
        if not self.columns:
328
            del os.environ['COLUMNS']
329
        else:
330
            os.environ['COLUMNS'] = self.columns
331
332
    def _snapshot_specific_path(self, name):
333
        return __here__ / '{}.{}.snap'.format(name, self.python_version)
334
335
    def _snapshot_generic_path(self, name):
336
        return __here__ / '{}.snap'.format(name)
337
338
    def _snapshot_failed_path(self, name):
339
        return __here__ / '{}.{}-failed.snap'.format(name, self.python_version)
340
341
    def _snapshot_path(self, name):
342
        snapshot_specific_path = self._snapshot_specific_path(name)
343
344
        if snapshot_specific_path.exists():
345
            return snapshot_specific_path
346
347
        return self._snapshot_generic_path(name)
348
349
    def assert_snapshot(self, name, output):
350
        path = self._snapshot_path(name)
351
352
        if not path.exists():
353
            path.write_text(output)
354
355
        content = path.read_text()
356
357
        try:
358
            self.assertEqual(content, output, 'Snapshot differs from output')
359
        except AssertionError:
360
            # write new output to snapshot file
361
            # reraise error afterwards
362
            path = self._snapshot_failed_path(name)
363
            path.write_text(output)
364
            raise
365
366
    def test_root_help(self):
367
        help_output = self.parser._parser.format_help()
368
        self.assert_snapshot('root_help', help_output)
369
370
    def test_socket_help(self):
371
        help_output = self.parser._parser_socket.format_help()
372
        self.assert_snapshot('socket_help', help_output)
373
374
    def test_ssh_help(self):
375
        self.parser._set_defaults(None)
376
        help_output = self.parser._parser_ssh.format_help()
377
        self.assert_snapshot('ssh_help', help_output)
378
379
    def test_tls_help(self):
380
        self.parser._set_defaults(None)
381
        help_output = self.parser._parser_tls.format_help()
382
        self.assert_snapshot('tls_help', help_output)
383