1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018-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
|
|
|
from gvm.errors import RequiredArgument, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208.entities.targets import AliveTest |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpModifyTargetTestMixin: |
25
|
|
|
def test_modify_target(self): |
26
|
|
|
self.gmp.modify_target(target_id='t1') |
27
|
|
|
|
28
|
|
|
self.connection.send.has_been_called_with( |
29
|
|
|
'<modify_target target_id="t1"/>' |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
def test_modify_target_missing_target_id(self): |
33
|
|
|
with self.assertRaises(RequiredArgument): |
34
|
|
|
self.gmp.modify_target(target_id=None) |
35
|
|
|
|
36
|
|
|
with self.assertRaises(RequiredArgument): |
37
|
|
|
self.gmp.modify_target(target_id='') |
38
|
|
|
|
39
|
|
|
def test_modify_target_with_comment(self): |
40
|
|
|
self.gmp.modify_target(target_id='t1', comment='foo') |
41
|
|
|
|
42
|
|
|
self.connection.send.has_been_called_with( |
43
|
|
|
'<modify_target target_id="t1">' |
44
|
|
|
'<comment>foo</comment>' |
45
|
|
|
'</modify_target>' |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
def test_modify_target_with_hosts(self): |
49
|
|
|
self.gmp.modify_target(target_id='t1', hosts=['foo']) |
50
|
|
|
|
51
|
|
|
self.connection.send.has_been_called_with( |
52
|
|
|
'<modify_target target_id="t1">' |
53
|
|
|
'<hosts>foo</hosts>' |
54
|
|
|
'<exclude_hosts></exclude_hosts>' |
55
|
|
|
'</modify_target>' |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
self.gmp.modify_target(target_id='t1', hosts=['foo', 'bar']) |
59
|
|
|
|
60
|
|
|
self.connection.send.has_been_called_with( |
61
|
|
|
'<modify_target target_id="t1">' |
62
|
|
|
'<hosts>foo,bar</hosts>' |
63
|
|
|
'<exclude_hosts></exclude_hosts>' |
64
|
|
|
'</modify_target>' |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
def test_modify_target_with_name(self): |
68
|
|
|
self.gmp.modify_target(target_id='t1', name='foo') |
69
|
|
|
|
70
|
|
|
self.connection.send.has_been_called_with( |
71
|
|
|
'<modify_target target_id="t1">' |
72
|
|
|
'<name>foo</name>' |
73
|
|
|
'</modify_target>' |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
def test_modify_target_with_exclude_hosts(self): |
77
|
|
|
self.gmp.modify_target(target_id='t1', exclude_hosts=['foo']) |
78
|
|
|
|
79
|
|
|
self.connection.send.has_been_called_with( |
80
|
|
|
'<modify_target target_id="t1">' |
81
|
|
|
'<exclude_hosts>foo</exclude_hosts>' |
82
|
|
|
'</modify_target>' |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
self.gmp.modify_target(target_id='t1', exclude_hosts=['foo', 'bar']) |
86
|
|
|
|
87
|
|
|
self.connection.send.has_been_called_with( |
88
|
|
|
'<modify_target target_id="t1">' |
89
|
|
|
'<exclude_hosts>foo,bar</exclude_hosts>' |
90
|
|
|
'</modify_target>' |
91
|
|
|
) |
92
|
|
|
|
93
|
|
|
def test_modify_target_with_ssh_credential(self): |
94
|
|
|
self.gmp.modify_target(target_id='t1', ssh_credential_id='c1') |
95
|
|
|
|
96
|
|
|
self.connection.send.has_been_called_with( |
97
|
|
|
'<modify_target target_id="t1">' |
98
|
|
|
'<ssh_credential id="c1"/>' |
99
|
|
|
'</modify_target>' |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
def test_modify_target_with_ssh_credential_port(self): |
103
|
|
|
self.gmp.modify_target( |
104
|
|
|
target_id='t1', ssh_credential_id='c1', ssh_credential_port=123 |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
self.connection.send.has_been_called_with( |
108
|
|
|
'<modify_target target_id="t1">' |
109
|
|
|
'<ssh_credential id="c1">' |
110
|
|
|
'<port>123</port>' |
111
|
|
|
'</ssh_credential>' |
112
|
|
|
'</modify_target>' |
113
|
|
|
) |
114
|
|
|
|
115
|
|
|
def test_modify_target_with_smb_credential_id(self): |
116
|
|
|
self.gmp.modify_target(target_id='t1', smb_credential_id='c1') |
117
|
|
|
|
118
|
|
|
self.connection.send.has_been_called_with( |
119
|
|
|
'<modify_target target_id="t1">' |
120
|
|
|
'<smb_credential id="c1"/>' |
121
|
|
|
'</modify_target>' |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
def test_modify_target_with_esxi_credential_id(self): |
125
|
|
|
self.gmp.modify_target(target_id='t1', esxi_credential_id='c1') |
126
|
|
|
|
127
|
|
|
self.connection.send.has_been_called_with( |
128
|
|
|
'<modify_target target_id="t1">' |
129
|
|
|
'<esxi_credential id="c1"/>' |
130
|
|
|
'</modify_target>' |
131
|
|
|
) |
132
|
|
|
|
133
|
|
|
def test_modify_target_with_snmp_credential_id(self): |
134
|
|
|
self.gmp.modify_target(target_id='t1', snmp_credential_id='c1') |
135
|
|
|
|
136
|
|
|
self.connection.send.has_been_called_with( |
137
|
|
|
'<modify_target target_id="t1">' |
138
|
|
|
'<snmp_credential id="c1"/>' |
139
|
|
|
'</modify_target>' |
140
|
|
|
) |
141
|
|
|
|
142
|
|
|
def test_modify_target_with_alive_tests(self): |
143
|
|
|
self.gmp.modify_target(target_id='t1', alive_test=AliveTest.ICMP_PING) |
144
|
|
|
|
145
|
|
|
self.connection.send.has_been_called_with( |
146
|
|
|
'<modify_target target_id="t1">' |
147
|
|
|
'<alive_tests>ICMP Ping</alive_tests>' |
148
|
|
|
'</modify_target>' |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
def test_modify_target_invalid_alive_tests(self): |
152
|
|
|
with self.assertRaises(InvalidArgumentType): |
153
|
|
|
self.gmp.modify_target(target_id='t1', alive_test='foo') |
154
|
|
|
|
155
|
|
|
def test_modify_target_with_reverse_lookup_only(self): |
156
|
|
|
self.gmp.modify_target(target_id='t1', reverse_lookup_only=True) |
157
|
|
|
|
158
|
|
|
self.connection.send.has_been_called_with( |
159
|
|
|
'<modify_target target_id="t1">' |
160
|
|
|
'<reverse_lookup_only>1</reverse_lookup_only>' |
161
|
|
|
'</modify_target>' |
162
|
|
|
) |
163
|
|
|
|
164
|
|
|
self.gmp.modify_target(target_id='t1', reverse_lookup_only=False) |
165
|
|
|
|
166
|
|
|
self.connection.send.has_been_called_with( |
167
|
|
|
'<modify_target target_id="t1">' |
168
|
|
|
'<reverse_lookup_only>0</reverse_lookup_only>' |
169
|
|
|
'</modify_target>' |
170
|
|
|
) |
171
|
|
|
|
172
|
|
|
def test_modify_target_with_reverse_lookup_unify(self): |
173
|
|
|
self.gmp.modify_target(target_id='t1', reverse_lookup_unify=True) |
174
|
|
|
|
175
|
|
|
self.connection.send.has_been_called_with( |
176
|
|
|
'<modify_target target_id="t1">' |
177
|
|
|
'<reverse_lookup_unify>1</reverse_lookup_unify>' |
178
|
|
|
'</modify_target>' |
179
|
|
|
) |
180
|
|
|
|
181
|
|
|
self.gmp.modify_target(target_id='t1', reverse_lookup_unify=False) |
182
|
|
|
|
183
|
|
|
self.connection.send.has_been_called_with( |
184
|
|
|
'<modify_target target_id="t1">' |
185
|
|
|
'<reverse_lookup_unify>0</reverse_lookup_unify>' |
186
|
|
|
'</modify_target>' |
187
|
|
|
) |
188
|
|
|
|
189
|
|
|
def test_modify_target_with_port_list_id(self): |
190
|
|
|
self.gmp.modify_target(target_id='t1', port_list_id='pl1') |
191
|
|
|
|
192
|
|
|
self.connection.send.has_been_called_with( |
193
|
|
|
'<modify_target target_id="t1">' |
194
|
|
|
'<port_list id="pl1"/>' |
195
|
|
|
'</modify_target>' |
196
|
|
|
) |
197
|
|
|
|