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