|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2018 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 gvm.errors import RequiredArgument, InvalidArgument |
|
22
|
|
|
from gvm.protocols.gmpv7 import Gmp |
|
23
|
|
|
|
|
24
|
|
|
from .. import MockConnection |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class GMPCreateTargetCommandTestCase(unittest.TestCase): |
|
28
|
|
|
|
|
29
|
|
|
def setUp(self): |
|
30
|
|
|
self.connection = MockConnection() |
|
31
|
|
|
self.gmp = Gmp(self.connection) |
|
32
|
|
|
|
|
33
|
|
|
def test_create_target_with_make_unique(self): |
|
34
|
|
|
self.gmp.create_target( |
|
35
|
|
|
'foo', |
|
36
|
|
|
make_unique=True, |
|
37
|
|
|
hosts=['foo', 'bar'], |
|
38
|
|
|
) |
|
39
|
|
|
|
|
40
|
|
|
self.connection.send.has_been_called_with( |
|
41
|
|
|
'<create_target>' |
|
42
|
|
|
'<name>foo<make_unique>1</make_unique></name>' |
|
43
|
|
|
'<hosts>foo,bar</hosts>' |
|
44
|
|
|
'</create_target>' |
|
45
|
|
|
) |
|
46
|
|
|
|
|
47
|
|
|
self.gmp.create_target( |
|
48
|
|
|
'foo', |
|
49
|
|
|
make_unique=False, |
|
50
|
|
|
hosts=['foo', 'bar'], |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
self.connection.send.has_been_called_with( |
|
54
|
|
|
'<create_target>' |
|
55
|
|
|
'<name>foo<make_unique>0</make_unique></name>' |
|
56
|
|
|
'<hosts>foo,bar</hosts>' |
|
57
|
|
|
'</create_target>' |
|
58
|
|
|
) |
|
59
|
|
|
|
|
60
|
|
|
def test_create_target_missing_name(self): |
|
61
|
|
|
with self.assertRaises(RequiredArgument): |
|
62
|
|
|
self.gmp.create_target( |
|
63
|
|
|
None, |
|
64
|
|
|
hosts=['foo'], |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
with self.assertRaises(RequiredArgument): |
|
68
|
|
|
self.gmp.create_target( |
|
69
|
|
|
name=None, |
|
70
|
|
|
hosts=['foo'], |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
|
|
with self.assertRaises(RequiredArgument): |
|
74
|
|
|
self.gmp.create_target( |
|
75
|
|
|
'', |
|
76
|
|
|
hosts=['foo'], |
|
77
|
|
|
) |
|
78
|
|
|
|
|
79
|
|
|
def test_create_target_with_asset_hosts_filter(self): |
|
80
|
|
|
self.gmp.create_target( |
|
81
|
|
|
'foo', |
|
82
|
|
|
asset_hosts_filter='name=foo', |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
|
|
self.connection.send.has_been_called_with( |
|
86
|
|
|
'<create_target>' |
|
87
|
|
|
'<name>foo</name>' |
|
88
|
|
|
'<asset_hosts filter="name=foo"/>' |
|
89
|
|
|
'</create_target>' |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
|
|
def test_create_target_missing_hosts(self): |
|
93
|
|
|
with self.assertRaises(RequiredArgument): |
|
94
|
|
|
self.gmp.create_target( |
|
95
|
|
|
name='foo' |
|
96
|
|
|
) |
|
97
|
|
|
|
|
98
|
|
|
def test_create_target_with_comment(self): |
|
99
|
|
|
self.gmp.create_target( |
|
100
|
|
|
'foo', |
|
101
|
|
|
hosts=['foo'], |
|
102
|
|
|
comment='bar', |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
self.connection.send.has_been_called_with( |
|
106
|
|
|
'<create_target>' |
|
107
|
|
|
'<name>foo</name>' |
|
108
|
|
|
'<hosts>foo</hosts>' |
|
109
|
|
|
'<comment>bar</comment>' |
|
110
|
|
|
'</create_target>' |
|
111
|
|
|
) |
|
112
|
|
|
|
|
113
|
|
|
def test_create_target_with_exclude_hosts(self): |
|
114
|
|
|
self.gmp.create_target( |
|
115
|
|
|
'foo', |
|
116
|
|
|
hosts=['foo', 'bar'], |
|
117
|
|
|
exclude_hosts=['bar', 'ipsum'], |
|
118
|
|
|
) |
|
119
|
|
|
|
|
120
|
|
|
self.connection.send.has_been_called_with( |
|
121
|
|
|
'<create_target>' |
|
122
|
|
|
'<name>foo</name>' |
|
123
|
|
|
'<hosts>foo,bar</hosts>' |
|
124
|
|
|
'<exclude_hosts>bar,ipsum</exclude_hosts>' |
|
125
|
|
|
'</create_target>' |
|
126
|
|
|
) |
|
127
|
|
|
|
|
128
|
|
|
def test_create_target_with_ssh_credential(self): |
|
129
|
|
|
self.gmp.create_target( |
|
130
|
|
|
'foo', |
|
131
|
|
|
hosts=['foo'], |
|
132
|
|
|
ssh_credential_id='c1', |
|
133
|
|
|
) |
|
134
|
|
|
|
|
135
|
|
|
self.connection.send.has_been_called_with( |
|
136
|
|
|
'<create_target>' |
|
137
|
|
|
'<name>foo</name>' |
|
138
|
|
|
'<hosts>foo</hosts>' |
|
139
|
|
|
'<ssh_credential id="c1"/>' |
|
140
|
|
|
'</create_target>' |
|
141
|
|
|
) |
|
142
|
|
|
|
|
143
|
|
|
def test_create_target_with_ssh_credential_port(self): |
|
144
|
|
|
self.gmp.create_target( |
|
145
|
|
|
'foo', |
|
146
|
|
|
hosts=['foo'], |
|
147
|
|
|
ssh_credential_id='c1', |
|
148
|
|
|
ssh_credential_port=123, |
|
149
|
|
|
) |
|
150
|
|
|
|
|
151
|
|
|
self.connection.send.has_been_called_with( |
|
152
|
|
|
'<create_target>' |
|
153
|
|
|
'<name>foo</name>' |
|
154
|
|
|
'<hosts>foo</hosts>' |
|
155
|
|
|
'<ssh_credential id="c1">' |
|
156
|
|
|
'<port>123</port>' |
|
157
|
|
|
'</ssh_credential>' |
|
158
|
|
|
'</create_target>' |
|
159
|
|
|
) |
|
160
|
|
|
|
|
161
|
|
|
def test_create_target_with_smb_credential_id(self): |
|
162
|
|
|
self.gmp.create_target( |
|
163
|
|
|
'foo', |
|
164
|
|
|
hosts=['foo'], |
|
165
|
|
|
smb_credential_id='c1', |
|
166
|
|
|
) |
|
167
|
|
|
|
|
168
|
|
|
self.connection.send.has_been_called_with( |
|
169
|
|
|
'<create_target>' |
|
170
|
|
|
'<name>foo</name>' |
|
171
|
|
|
'<hosts>foo</hosts>' |
|
172
|
|
|
'<smb_credential id="c1"/>' |
|
173
|
|
|
'</create_target>' |
|
174
|
|
|
) |
|
175
|
|
|
|
|
176
|
|
|
def test_create_target_with_esxi_credential_id(self): |
|
177
|
|
|
self.gmp.create_target( |
|
178
|
|
|
'foo', |
|
179
|
|
|
hosts=['foo'], |
|
180
|
|
|
esxi_credential_id='c1', |
|
181
|
|
|
) |
|
182
|
|
|
|
|
183
|
|
|
self.connection.send.has_been_called_with( |
|
184
|
|
|
'<create_target>' |
|
185
|
|
|
'<name>foo</name>' |
|
186
|
|
|
'<hosts>foo</hosts>' |
|
187
|
|
|
'<esxi_credential id="c1"/>' |
|
188
|
|
|
'</create_target>' |
|
189
|
|
|
) |
|
190
|
|
|
|
|
191
|
|
|
def test_create_target_with_snmp_credential_id(self): |
|
192
|
|
|
self.gmp.create_target( |
|
193
|
|
|
'foo', |
|
194
|
|
|
hosts=['foo'], |
|
195
|
|
|
snmp_credential_id='c1', |
|
196
|
|
|
) |
|
197
|
|
|
|
|
198
|
|
|
self.connection.send.has_been_called_with( |
|
199
|
|
|
'<create_target>' |
|
200
|
|
|
'<name>foo</name>' |
|
201
|
|
|
'<hosts>foo</hosts>' |
|
202
|
|
|
'<snmp_credential id="c1"/>' |
|
203
|
|
|
'</create_target>' |
|
204
|
|
|
) |
|
205
|
|
|
|
|
206
|
|
|
def test_create_target_with_alive_tests(self): |
|
207
|
|
|
self.gmp.create_target( |
|
208
|
|
|
'foo', |
|
209
|
|
|
hosts=['foo'], |
|
210
|
|
|
alive_tests='ICMP Ping', |
|
211
|
|
|
) |
|
212
|
|
|
|
|
213
|
|
|
self.connection.send.has_been_called_with( |
|
214
|
|
|
'<create_target>' |
|
215
|
|
|
'<name>foo</name>' |
|
216
|
|
|
'<hosts>foo</hosts>' |
|
217
|
|
|
'<alive_tests>ICMP Ping</alive_tests>' |
|
218
|
|
|
'</create_target>' |
|
219
|
|
|
) |
|
220
|
|
|
|
|
221
|
|
|
def test_create_target_invalid_alive_tests(self): |
|
222
|
|
|
with self.assertRaises(InvalidArgument): |
|
223
|
|
|
self.gmp.create_target( |
|
224
|
|
|
'foo', |
|
225
|
|
|
hosts=['foo'], |
|
226
|
|
|
alive_tests='foo', |
|
227
|
|
|
) |
|
228
|
|
|
|
|
229
|
|
|
def test_create_target_with_reverse_lookup_only(self): |
|
230
|
|
|
self.gmp.create_target( |
|
231
|
|
|
'foo', |
|
232
|
|
|
hosts=['foo'], |
|
233
|
|
|
reverse_lookup_only=True, |
|
234
|
|
|
) |
|
235
|
|
|
|
|
236
|
|
|
self.connection.send.has_been_called_with( |
|
237
|
|
|
'<create_target>' |
|
238
|
|
|
'<name>foo</name>' |
|
239
|
|
|
'<hosts>foo</hosts>' |
|
240
|
|
|
'<reverse_lookup_only>1</reverse_lookup_only>' |
|
241
|
|
|
'</create_target>' |
|
242
|
|
|
) |
|
243
|
|
|
|
|
244
|
|
|
self.gmp.create_target( |
|
245
|
|
|
'foo', |
|
246
|
|
|
hosts=['foo'], |
|
247
|
|
|
reverse_lookup_only=False, |
|
248
|
|
|
) |
|
249
|
|
|
|
|
250
|
|
|
self.connection.send.has_been_called_with( |
|
251
|
|
|
'<create_target>' |
|
252
|
|
|
'<name>foo</name>' |
|
253
|
|
|
'<hosts>foo</hosts>' |
|
254
|
|
|
'<reverse_lookup_only>0</reverse_lookup_only>' |
|
255
|
|
|
'</create_target>' |
|
256
|
|
|
) |
|
257
|
|
|
|
|
258
|
|
|
def test_create_target_with_reverse_lookup_unify(self): |
|
259
|
|
|
self.gmp.create_target( |
|
260
|
|
|
'foo', |
|
261
|
|
|
hosts=['foo'], |
|
262
|
|
|
reverse_lookup_unify=True, |
|
263
|
|
|
) |
|
264
|
|
|
|
|
265
|
|
|
self.connection.send.has_been_called_with( |
|
266
|
|
|
'<create_target>' |
|
267
|
|
|
'<name>foo</name>' |
|
268
|
|
|
'<hosts>foo</hosts>' |
|
269
|
|
|
'<reverse_lookup_unify>1</reverse_lookup_unify>' |
|
270
|
|
|
'</create_target>' |
|
271
|
|
|
) |
|
272
|
|
|
|
|
273
|
|
|
self.gmp.create_target( |
|
274
|
|
|
'foo', |
|
275
|
|
|
hosts=['foo'], |
|
276
|
|
|
reverse_lookup_unify=False, |
|
277
|
|
|
) |
|
278
|
|
|
|
|
279
|
|
|
self.connection.send.has_been_called_with( |
|
280
|
|
|
'<create_target>' |
|
281
|
|
|
'<name>foo</name>' |
|
282
|
|
|
'<hosts>foo</hosts>' |
|
283
|
|
|
'<reverse_lookup_unify>0</reverse_lookup_unify>' |
|
284
|
|
|
'</create_target>' |
|
285
|
|
|
) |
|
286
|
|
|
|
|
287
|
|
|
def test_create_target_with_port_range(self): |
|
288
|
|
|
self.gmp.create_target( |
|
289
|
|
|
'foo', |
|
290
|
|
|
hosts=['foo'], |
|
291
|
|
|
port_range='bar', |
|
292
|
|
|
) |
|
293
|
|
|
|
|
294
|
|
|
self.connection.send.has_been_called_with( |
|
295
|
|
|
'<create_target>' |
|
296
|
|
|
'<name>foo</name>' |
|
297
|
|
|
'<hosts>foo</hosts>' |
|
298
|
|
|
'<port_range>bar</port_range>' |
|
299
|
|
|
'</create_target>' |
|
300
|
|
|
) |
|
301
|
|
|
|
|
302
|
|
|
def test_create_target_with_port_list_id(self): |
|
303
|
|
|
self.gmp.create_target( |
|
304
|
|
|
'foo', |
|
305
|
|
|
hosts=['foo'], |
|
306
|
|
|
port_list_id='pl1', |
|
307
|
|
|
) |
|
308
|
|
|
|
|
309
|
|
|
self.connection.send.has_been_called_with( |
|
310
|
|
|
'<create_target>' |
|
311
|
|
|
'<name>foo</name>' |
|
312
|
|
|
'<hosts>foo</hosts>' |
|
313
|
|
|
'<port_list id="pl1"/>' |
|
314
|
|
|
'</create_target>' |
|
315
|
|
|
) |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
if __name__ == '__main__': |
|
319
|
|
|
unittest.main() |
|
320
|
|
|
|