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 unittest |
20
|
|
|
|
21
|
|
|
from pathlib import Path |
22
|
|
|
|
23
|
|
|
from gvm.connections import DEFAULT_UNIX_SOCKET_PATH, DEFAULT_TIMEOUT |
24
|
|
|
|
25
|
|
|
from gvmtools.parser import CliParser |
26
|
|
|
|
27
|
|
|
__here__ = Path(__file__).parent.resolve() |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class ConfigParserTestCase(unittest.TestCase): |
31
|
|
|
def setUp(self): |
32
|
|
|
self.test_config_path = __here__ / 'test.cfg' |
33
|
|
|
|
34
|
|
|
self.assertTrue(self.test_config_path.is_file()) |
35
|
|
|
|
36
|
|
|
self.parser = CliParser('TestParser', 'test.log') |
37
|
|
|
|
38
|
|
|
def test_socket_defaults_from_config(self): |
39
|
|
|
args = self.parser.parse_args( |
40
|
|
|
['--config', str(self.test_config_path), 'socket'] |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
self.assertEqual(args.foo, 'bar') |
44
|
|
|
self.assertEqual(args.timeout, 1000) |
45
|
|
|
self.assertEqual(args.gmp_password, 'bar') |
46
|
|
|
self.assertEqual(args.gmp_username, 'bar') |
47
|
|
|
self.assertEqual(args.socketpath, '/foo/bar.sock') |
48
|
|
|
|
49
|
|
|
def test_ssh_defaults_from_config(self): |
50
|
|
|
args = self.parser.parse_args( |
51
|
|
|
['--config', str(self.test_config_path), 'ssh', '--hostname', 'foo'] |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
self.assertEqual(args.foo, 'bar') |
55
|
|
|
self.assertEqual(args.timeout, 1000) |
56
|
|
|
self.assertEqual(args.gmp_password, 'bar') |
57
|
|
|
self.assertEqual(args.gmp_username, 'bar') |
58
|
|
|
self.assertEqual(args.ssh_password, 'lorem') |
59
|
|
|
self.assertEqual(args.ssh_username, 'ipsum') |
60
|
|
|
self.assertEqual(args.port, 123) |
61
|
|
|
|
62
|
|
|
def test_tls_defaults_from_config(self): |
63
|
|
|
args = self.parser.parse_args( |
64
|
|
|
['--config', str(self.test_config_path), 'tls', '--hostname', 'foo'] |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
self.assertEqual(args.foo, 'bar') |
68
|
|
|
self.assertEqual(args.timeout, 1000) |
69
|
|
|
self.assertEqual(args.gmp_password, 'bar') |
70
|
|
|
self.assertEqual(args.gmp_username, 'bar') |
71
|
|
|
self.assertEqual(args.certfile, 'foo.cert') |
72
|
|
|
self.assertEqual(args.keyfile, 'foo.key') |
73
|
|
|
self.assertEqual(args.cafile, 'foo.ca') |
74
|
|
|
self.assertEqual(args.port, 123) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
class IgnoreConfigParserTestCase(unittest.TestCase): |
78
|
|
|
def test_unkown_config_file(self): |
79
|
|
|
test_config_path = __here__ / 'foo.cfg' |
80
|
|
|
|
81
|
|
|
self.assertFalse(test_config_path.is_file()) |
82
|
|
|
|
83
|
|
|
self.parser = CliParser('TestParser', 'test.log') |
84
|
|
|
|
85
|
|
|
args = self.parser.parse_args( |
86
|
|
|
['--config', str(test_config_path), 'socket'] |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
self.assertEqual(args.timeout, DEFAULT_TIMEOUT) |
90
|
|
|
self.assertEqual(args.gmp_password, '') |
91
|
|
|
self.assertEqual(args.gmp_username, '') |
92
|
|
|
self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH) |
93
|
|
|
|
94
|
|
|
def test_unkown_config_file_in_unkown_dir(self): |
95
|
|
|
test_config_path = __here__ / 'foo' / 'foo.cfg' |
96
|
|
|
|
97
|
|
|
self.assertFalse(test_config_path.is_file()) |
98
|
|
|
|
99
|
|
|
self.parser = CliParser('TestParser', 'test.log') |
100
|
|
|
|
101
|
|
|
args = self.parser.parse_args( |
102
|
|
|
['--config', str(test_config_path), 'socket'] |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
self.assertEqual(args.timeout, DEFAULT_TIMEOUT) |
106
|
|
|
self.assertEqual(args.gmp_password, '') |
107
|
|
|
self.assertEqual(args.gmp_username, '') |
108
|
|
|
self.assertEqual(args.socketpath, DEFAULT_UNIX_SOCKET_PATH) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class ParserTestCase(unittest.TestCase): |
112
|
|
|
def setUp(self): |
113
|
|
|
self.parser = CliParser( |
114
|
|
|
'TestParser', 'test.log', ignore_config=True, prog='gvm-test-cli' |
115
|
|
|
) |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
class RootArgumentsParserTest(ParserTestCase): |
119
|
|
|
def test_config(self): |
120
|
|
|
args = self.parser.parse_args(['--config', 'foo.cfg', 'socket']) |
121
|
|
|
self.assertEqual(args.config, 'foo.cfg') |
122
|
|
|
|
123
|
|
|
def test_defaults(self): |
124
|
|
|
args = self.parser.parse_args(['socket']) |
125
|
|
|
self.assertEqual(args.config, '~/.config/gvm-tools.conf') |
126
|
|
|
self.assertEqual(args.gmp_password, '') |
127
|
|
|
self.assertEqual(args.gmp_username, '') |
128
|
|
|
self.assertEqual(args.timeout, 60) |
129
|
|
|
self.assertIsNone(args.loglevel) |
130
|
|
|
|
131
|
|
|
def test_loglevel(self): |
132
|
|
|
args = self.parser.parse_args(['--log', 'ERROR', 'socket']) |
133
|
|
|
self.assertEqual(args.loglevel, 'ERROR') |
134
|
|
|
|
135
|
|
|
def test_timeout(self): |
136
|
|
|
args = self.parser.parse_args(['--timeout', '1000', 'socket']) |
137
|
|
|
self.assertEqual(args.timeout, 1000) |
138
|
|
|
|
139
|
|
|
def test_gmp_username(self): |
140
|
|
|
args = self.parser.parse_args(['--gmp-username', 'foo', 'socket']) |
141
|
|
|
self.assertEqual(args.gmp_username, 'foo') |
142
|
|
|
|
143
|
|
|
def test_gmp_password(self): |
144
|
|
|
args = self.parser.parse_args(['--gmp-password', 'foo', 'socket']) |
145
|
|
|
self.assertEqual(args.gmp_password, 'foo') |
146
|
|
|
|
147
|
|
|
def test_with_unknown_args(self): |
148
|
|
|
args, script_args = self.parser.parse_known_args( |
149
|
|
|
['--gmp-password', 'foo', 'socket', '--bar', '--bar2']) |
150
|
|
|
self.assertEqual(args.gmp_password, 'foo') |
151
|
|
|
self.assertEqual(script_args, ['--bar', '--bar2']) |
152
|
|
|
|
153
|
|
|
class SocketParserTestCase(ParserTestCase): |
154
|
|
|
def test_defaults(self): |
155
|
|
|
args = self.parser.parse_args(['socket']) |
156
|
|
|
self.assertIsNone(args.sockpath) |
157
|
|
|
self.assertEqual(args.socketpath, '/usr/local/var/run/gvmd.sock') |
158
|
|
|
|
159
|
|
|
def test_connection_type(self): |
160
|
|
|
args = self.parser.parse_args(['socket']) |
161
|
|
|
self.assertEqual(args.connection_type, 'socket') |
162
|
|
|
|
163
|
|
|
def test_sockpath(self): |
164
|
|
|
args = self.parser.parse_args(['socket', '--sockpath', 'foo.sock']) |
165
|
|
|
self.assertEqual(args.sockpath, 'foo.sock') |
166
|
|
|
|
167
|
|
|
def test_socketpath(self): |
168
|
|
|
args = self.parser.parse_args(['socket', '--socketpath', 'foo.sock']) |
169
|
|
|
self.assertEqual(args.socketpath, 'foo.sock') |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
class SshParserTestCase(ParserTestCase): |
173
|
|
|
def test_defaults(self): |
174
|
|
|
args = self.parser.parse_args(['ssh', '--hostname=foo']) |
175
|
|
|
self.assertEqual(args.port, 22) |
176
|
|
|
self.assertEqual(args.ssh_username, 'gmp') |
177
|
|
|
self.assertEqual(args.ssh_password, 'gmp') |
178
|
|
|
|
179
|
|
|
def test_connection_type(self): |
180
|
|
|
args = self.parser.parse_args(['ssh', '--hostname=foo']) |
181
|
|
|
self.assertEqual(args.connection_type, 'ssh') |
182
|
|
|
|
183
|
|
|
def test_hostname(self): |
184
|
|
|
args = self.parser.parse_args(['ssh', '--hostname', 'foo']) |
185
|
|
|
self.assertEqual(args.hostname, 'foo') |
186
|
|
|
|
187
|
|
|
def test_port(self): |
188
|
|
|
args = self.parser.parse_args( |
189
|
|
|
['ssh', '--hostname', 'foo', '--port', '123'] |
190
|
|
|
) |
191
|
|
|
self.assertEqual(args.port, 123) |
192
|
|
|
|
193
|
|
|
def test_ssh_username(self): |
194
|
|
|
args = self.parser.parse_args( |
195
|
|
|
['ssh', '--hostname', 'foo', '--ssh-username', 'foo'] |
196
|
|
|
) |
197
|
|
|
self.assertEqual(args.ssh_username, 'foo') |
198
|
|
|
|
199
|
|
|
def test_ssh_password(self): |
200
|
|
|
args = self.parser.parse_args( |
201
|
|
|
['ssh', '--hostname', 'foo', '--ssh-password', 'foo'] |
202
|
|
|
) |
203
|
|
|
self.assertEqual(args.ssh_password, 'foo') |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class TlsParserTestCase(ParserTestCase): |
207
|
|
|
def test_defaults(self): |
208
|
|
|
args = self.parser.parse_args(['tls', '--hostname=foo']) |
209
|
|
|
self.assertIsNone(args.certfile) |
210
|
|
|
self.assertIsNone(args.keyfile) |
211
|
|
|
self.assertIsNone(args.cafile) |
212
|
|
|
self.assertEqual(args.port, 9390) |
213
|
|
|
|
214
|
|
|
def test_connection_type(self): |
215
|
|
|
args = self.parser.parse_args(['tls', '--hostname=foo']) |
216
|
|
|
self.assertEqual(args.connection_type, 'tls') |
217
|
|
|
|
218
|
|
|
def test_hostname(self): |
219
|
|
|
args = self.parser.parse_args(['tls', '--hostname', 'foo']) |
220
|
|
|
self.assertEqual(args.hostname, 'foo') |
221
|
|
|
|
222
|
|
|
def test_port(self): |
223
|
|
|
args = self.parser.parse_args( |
224
|
|
|
['tls', '--hostname', 'foo', '--port', '123'] |
225
|
|
|
) |
226
|
|
|
self.assertEqual(args.port, 123) |
227
|
|
|
|
228
|
|
|
def test_certfile(self): |
229
|
|
|
args = self.parser.parse_args( |
230
|
|
|
['tls', '--hostname', 'foo', '--certfile', 'foo.cert'] |
231
|
|
|
) |
232
|
|
|
self.assertEqual(args.certfile, 'foo.cert') |
233
|
|
|
|
234
|
|
|
def test_keyfile(self): |
235
|
|
|
args = self.parser.parse_args( |
236
|
|
|
['tls', '--hostname', 'foo', '--keyfile', 'foo.key'] |
237
|
|
|
) |
238
|
|
|
self.assertEqual(args.keyfile, 'foo.key') |
239
|
|
|
|
240
|
|
|
def test_cafile(self): |
241
|
|
|
args = self.parser.parse_args( |
242
|
|
|
['tls', '--hostname', 'foo', '--cafile', 'foo.ca'] |
243
|
|
|
) |
244
|
|
|
self.assertEqual(args.cafile, 'foo.ca') |
245
|
|
|
|
246
|
|
|
def test_no_credentials(self): |
247
|
|
|
args = self.parser.parse_args( |
248
|
|
|
['tls', '--hostname', 'foo', '--no-credentials'] |
249
|
|
|
) |
250
|
|
|
self.assertTrue(args.no_credentials) |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
class CustomizeParserTestCase(ParserTestCase): |
254
|
|
|
def test_add_optional_argument(self): |
255
|
|
|
self.parser.add_argument('--foo', type=int) |
256
|
|
|
|
257
|
|
|
args = self.parser.parse_args(['socket', '--foo', '123']) |
258
|
|
|
self.assertEqual(args.foo, 123) |
259
|
|
|
|
260
|
|
|
args = self.parser.parse_args( |
261
|
|
|
['ssh', '--hostname', 'bar', '--foo', '123'] |
262
|
|
|
) |
263
|
|
|
self.assertEqual(args.foo, 123) |
264
|
|
|
|
265
|
|
|
args = self.parser.parse_args( |
266
|
|
|
['tls', '--hostname', 'bar', '--foo', '123'] |
267
|
|
|
) |
268
|
|
|
self.assertEqual(args.foo, 123) |
269
|
|
|
|
270
|
|
|
def test_add_positional_argument(self): |
271
|
|
|
self.parser.add_argument('foo', type=int) |
272
|
|
|
args = self.parser.parse_args(['socket', '123']) |
273
|
|
|
|
274
|
|
|
self.assertEqual(args.foo, 123) |
275
|
|
|
|
276
|
|
|
def test_add_protocol_argument(self): |
277
|
|
|
self.parser.add_protocol_argument() |
278
|
|
|
|
279
|
|
|
args = self.parser.parse_args(['socket']) |
280
|
|
|
self.assertEqual(args.protocol, 'GMP') |
281
|
|
|
|
282
|
|
|
args = self.parser.parse_args(['--protocol', 'OSP', 'socket']) |
283
|
|
|
|
284
|
|
|
self.assertEqual(args.protocol, 'OSP') |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
class HelpFormattingParserTestCase(ParserTestCase): |
288
|
|
|
# pylint: disable=protected-access |
289
|
|
|
maxDiff = None |
290
|
|
|
|
291
|
|
|
def _snapshot_path(self, name): |
292
|
|
|
return __here__ / '{}.snap'.format(name) |
293
|
|
|
|
294
|
|
|
def _load_snapshot(self, path): |
295
|
|
|
return path.read_text() |
296
|
|
|
|
297
|
|
|
def _write_snapshot(self, path, output): |
298
|
|
|
path.write_text(output) |
299
|
|
|
|
300
|
|
|
def assert_snapshot(self, name, output): |
301
|
|
|
path = self._snapshot_path(name) |
302
|
|
|
|
303
|
|
|
if not path.exists(): |
304
|
|
|
path.write_text(output) |
305
|
|
|
|
306
|
|
|
content = path.read_text() |
307
|
|
|
self.assertEqual(content, output, 'Snapshot differs from output') |
308
|
|
|
|
309
|
|
|
def test_root_help(self): |
310
|
|
|
help_output = self.parser._parser.format_help() |
311
|
|
|
self.assert_snapshot('root_help', help_output) |
312
|
|
|
|
313
|
|
|
def test_socket_help(self): |
314
|
|
|
help_output = self.parser._parser_socket.format_help() |
315
|
|
|
self.assert_snapshot('socket_help', help_output) |
316
|
|
|
|
317
|
|
|
def test_ssh_help(self): |
318
|
|
|
help_output = self.parser._parser_ssh.format_help() |
319
|
|
|
self.assert_snapshot('ssh_help', help_output) |
320
|
|
|
|
321
|
|
|
def test_tls_help(self): |
322
|
|
|
help_output = self.parser._parser_tls.format_help() |
323
|
|
|
self.assert_snapshot('tls_help', help_output) |
324
|
|
|
|