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
|
|
|
|
148
|
|
|
class SocketParserTestCase(ParserTestCase): |
149
|
|
|
def test_defaults(self): |
150
|
|
|
args = self.parser.parse_args(['socket']) |
151
|
|
|
self.assertIsNone(args.sockpath) |
152
|
|
|
self.assertEqual(args.socketpath, '/usr/local/var/run/gvmd.sock') |
153
|
|
|
|
154
|
|
|
def test_connection_type(self): |
155
|
|
|
args = self.parser.parse_args(['socket']) |
156
|
|
|
self.assertEqual(args.connection_type, 'socket') |
157
|
|
|
|
158
|
|
|
def test_sockpath(self): |
159
|
|
|
args = self.parser.parse_args(['socket', '--sockpath', 'foo.sock']) |
160
|
|
|
self.assertEqual(args.sockpath, 'foo.sock') |
161
|
|
|
|
162
|
|
|
def test_socketpath(self): |
163
|
|
|
args = self.parser.parse_args(['socket', '--socketpath', 'foo.sock']) |
164
|
|
|
self.assertEqual(args.socketpath, 'foo.sock') |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
class SshParserTestCase(ParserTestCase): |
168
|
|
|
def test_defaults(self): |
169
|
|
|
args = self.parser.parse_args(['ssh', '--hostname=foo']) |
170
|
|
|
self.assertEqual(args.port, 22) |
171
|
|
|
self.assertEqual(args.ssh_username, 'gmp') |
172
|
|
|
self.assertEqual(args.ssh_password, 'gmp') |
173
|
|
|
|
174
|
|
|
def test_connection_type(self): |
175
|
|
|
args = self.parser.parse_args(['ssh', '--hostname=foo']) |
176
|
|
|
self.assertEqual(args.connection_type, 'ssh') |
177
|
|
|
|
178
|
|
|
def test_hostname(self): |
179
|
|
|
args = self.parser.parse_args(['ssh', '--hostname', 'foo']) |
180
|
|
|
self.assertEqual(args.hostname, 'foo') |
181
|
|
|
|
182
|
|
|
def test_port(self): |
183
|
|
|
args = self.parser.parse_args( |
184
|
|
|
['ssh', '--hostname', 'foo', '--port', '123'] |
185
|
|
|
) |
186
|
|
|
self.assertEqual(args.port, 123) |
187
|
|
|
|
188
|
|
|
def test_ssh_username(self): |
189
|
|
|
args = self.parser.parse_args( |
190
|
|
|
['ssh', '--hostname', 'foo', '--ssh-username', 'foo'] |
191
|
|
|
) |
192
|
|
|
self.assertEqual(args.ssh_username, 'foo') |
193
|
|
|
|
194
|
|
|
def test_ssh_password(self): |
195
|
|
|
args = self.parser.parse_args( |
196
|
|
|
['ssh', '--hostname', 'foo', '--ssh-password', 'foo'] |
197
|
|
|
) |
198
|
|
|
self.assertEqual(args.ssh_password, 'foo') |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
class TlsParserTestCase(ParserTestCase): |
202
|
|
|
def test_defaults(self): |
203
|
|
|
args = self.parser.parse_args(['tls', '--hostname=foo']) |
204
|
|
|
self.assertIsNone(args.certfile) |
205
|
|
|
self.assertIsNone(args.keyfile) |
206
|
|
|
self.assertIsNone(args.cafile) |
207
|
|
|
self.assertEqual(args.port, 9390) |
208
|
|
|
|
209
|
|
|
def test_connection_type(self): |
210
|
|
|
args = self.parser.parse_args(['tls', '--hostname=foo']) |
211
|
|
|
self.assertEqual(args.connection_type, 'tls') |
212
|
|
|
|
213
|
|
|
def test_hostname(self): |
214
|
|
|
args = self.parser.parse_args(['tls', '--hostname', 'foo']) |
215
|
|
|
self.assertEqual(args.hostname, 'foo') |
216
|
|
|
|
217
|
|
|
def test_port(self): |
218
|
|
|
args = self.parser.parse_args( |
219
|
|
|
['tls', '--hostname', 'foo', '--port', '123'] |
220
|
|
|
) |
221
|
|
|
self.assertEqual(args.port, 123) |
222
|
|
|
|
223
|
|
|
def test_certfile(self): |
224
|
|
|
args = self.parser.parse_args( |
225
|
|
|
['tls', '--hostname', 'foo', '--certfile', 'foo.cert'] |
226
|
|
|
) |
227
|
|
|
self.assertEqual(args.certfile, 'foo.cert') |
228
|
|
|
|
229
|
|
|
def test_keyfile(self): |
230
|
|
|
args = self.parser.parse_args( |
231
|
|
|
['tls', '--hostname', 'foo', '--keyfile', 'foo.key'] |
232
|
|
|
) |
233
|
|
|
self.assertEqual(args.keyfile, 'foo.key') |
234
|
|
|
|
235
|
|
|
def test_cafile(self): |
236
|
|
|
args = self.parser.parse_args( |
237
|
|
|
['tls', '--hostname', 'foo', '--cafile', 'foo.ca'] |
238
|
|
|
) |
239
|
|
|
self.assertEqual(args.cafile, 'foo.ca') |
240
|
|
|
|
241
|
|
|
def test_no_credentials(self): |
242
|
|
|
args = self.parser.parse_args( |
243
|
|
|
['tls', '--hostname', 'foo', '--no-credentials'] |
244
|
|
|
) |
245
|
|
|
self.assertTrue(args.no_credentials) |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
class CustomizeParserTestCase(ParserTestCase): |
249
|
|
|
def test_add_optional_argument(self): |
250
|
|
|
self.parser.add_argument('--foo', type=int) |
251
|
|
|
|
252
|
|
|
args = self.parser.parse_args(['socket', '--foo', '123']) |
253
|
|
|
self.assertEqual(args.foo, 123) |
254
|
|
|
|
255
|
|
|
args = self.parser.parse_args( |
256
|
|
|
['ssh', '--hostname', 'bar', '--foo', '123'] |
257
|
|
|
) |
258
|
|
|
self.assertEqual(args.foo, 123) |
259
|
|
|
|
260
|
|
|
args = self.parser.parse_args( |
261
|
|
|
['tls', '--hostname', 'bar', '--foo', '123'] |
262
|
|
|
) |
263
|
|
|
self.assertEqual(args.foo, 123) |
264
|
|
|
|
265
|
|
|
def test_add_positional_argument(self): |
266
|
|
|
self.parser.add_argument('foo', type=int) |
267
|
|
|
args = self.parser.parse_args(['socket', '123']) |
268
|
|
|
|
269
|
|
|
self.assertEqual(args.foo, 123) |
270
|
|
|
|
271
|
|
|
def test_add_protocol_argument(self): |
272
|
|
|
self.parser.add_protocol_argument() |
273
|
|
|
|
274
|
|
|
args = self.parser.parse_args(['socket']) |
275
|
|
|
self.assertEqual(args.protocol, 'GMP') |
276
|
|
|
|
277
|
|
|
args = self.parser.parse_args(['--protocol', 'OSP', 'socket']) |
278
|
|
|
|
279
|
|
|
self.assertEqual(args.protocol, 'OSP') |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
class HelpFormattingParserTestCase(ParserTestCase): |
283
|
|
|
# pylint: disable=protected-access |
284
|
|
|
maxDiff = None |
285
|
|
|
|
286
|
|
|
def _snapshot_path(self, name): |
287
|
|
|
return __here__ / '{}.snap'.format(name) |
288
|
|
|
|
289
|
|
|
def _load_snapshot(self, path): |
290
|
|
|
return path.read_text() |
291
|
|
|
|
292
|
|
|
def _write_snapshot(self, path, output): |
293
|
|
|
path.write_text(output) |
294
|
|
|
|
295
|
|
|
def assert_snapshot(self, name, output): |
296
|
|
|
path = self._snapshot_path(name) |
297
|
|
|
|
298
|
|
|
if not path.exists(): |
299
|
|
|
path.write_text(output) |
300
|
|
|
|
301
|
|
|
content = path.read_text() |
302
|
|
|
self.assertEqual(content, output, 'Snapshot differs from output') |
303
|
|
|
|
304
|
|
|
def test_root_help(self): |
305
|
|
|
help_output = self.parser._parser.format_help() |
306
|
|
|
self.assert_snapshot('root_help', help_output) |
307
|
|
|
|
308
|
|
|
def test_socket_help(self): |
309
|
|
|
help_output = self.parser._parser_socket.format_help() |
310
|
|
|
self.assert_snapshot('socket_help', help_output) |
311
|
|
|
|
312
|
|
|
def test_ssh_help(self): |
313
|
|
|
help_output = self.parser._parser_ssh.format_help() |
314
|
|
|
self.assert_snapshot('ssh_help', help_output) |
315
|
|
|
|
316
|
|
|
def test_tls_help(self): |
317
|
|
|
help_output = self.parser._parser_tls.format_help() |
318
|
|
|
self.assert_snapshot('tls_help', help_output) |
319
|
|
|
|