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