1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2020-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 collections import OrderedDict |
20
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
22
|
|
|
|
23
|
|
|
from gvm.protocols.gmpv208 import HostsOrdering |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class GmpModifyAuditTestMixin: |
27
|
|
|
def test_modify_task(self): |
28
|
|
|
self.gmp.modify_audit('t1') |
29
|
|
|
|
30
|
|
|
self.connection.send.has_been_called_with('<modify_task task_id="t1"/>') |
31
|
|
|
|
32
|
|
|
def test_modify_audit_missing_task_id(self): |
33
|
|
|
with self.assertRaises(RequiredArgument): |
34
|
|
|
self.gmp.modify_audit(None) |
35
|
|
|
|
36
|
|
|
with self.assertRaises(RequiredArgument): |
37
|
|
|
self.gmp.modify_audit('') |
38
|
|
|
|
39
|
|
|
with self.assertRaises(RequiredArgument): |
40
|
|
|
self.gmp.modify_audit(audit_id='') |
41
|
|
|
|
42
|
|
|
def test_modify_audit_with_name(self): |
43
|
|
|
self.gmp.modify_audit(audit_id='t1', name='foo') |
44
|
|
|
|
45
|
|
|
self.connection.send.has_been_called_with( |
46
|
|
|
'<modify_task task_id="t1">' '<name>foo</name>' '</modify_task>' |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
def test_modify_audit_with_policy_id(self): |
50
|
|
|
self.gmp.modify_audit(audit_id='t1', policy_id='c1') |
51
|
|
|
|
52
|
|
|
self.connection.send.has_been_called_with( |
53
|
|
|
'<modify_task task_id="t1">' '<config id="c1"/>' '</modify_task>' |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
def test_modify_audit_with_target_id(self): |
57
|
|
|
self.gmp.modify_audit(audit_id='t1', target_id='t1') |
58
|
|
|
|
59
|
|
|
self.connection.send.has_been_called_with( |
60
|
|
|
'<modify_task task_id="t1">' '<target id="t1"/>' '</modify_task>' |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
def test_modify_audit_with_scanner_id(self): |
64
|
|
|
self.gmp.modify_audit(audit_id='t1', scanner_id='s1') |
65
|
|
|
|
66
|
|
|
self.connection.send.has_been_called_with( |
67
|
|
|
'<modify_task task_id="t1">' '<scanner id="s1"/>' '</modify_task>' |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
def test_modify_audit_with_schedule_id(self): |
71
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_id='s1') |
72
|
|
|
|
73
|
|
|
self.connection.send.has_been_called_with( |
74
|
|
|
'<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>' |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
def test_modify_audit_with_comment(self): |
78
|
|
|
self.gmp.modify_audit(audit_id='t1', comment='bar') |
79
|
|
|
|
80
|
|
|
self.connection.send.has_been_called_with( |
81
|
|
|
'<modify_task task_id="t1">' |
82
|
|
|
'<comment>bar</comment>' |
83
|
|
|
'</modify_task>' |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
def test_modify_audit_with_alerts_ids(self): |
87
|
|
|
self.gmp.modify_audit(audit_id='t1', alert_ids=['a1', 'a2', 'a3']) |
88
|
|
|
|
89
|
|
|
self.connection.send.has_been_called_with( |
90
|
|
|
'<modify_task task_id="t1">' |
91
|
|
|
'<alert id="a1"/>' |
92
|
|
|
'<alert id="a2"/>' |
93
|
|
|
'<alert id="a3"/>' |
94
|
|
|
'</modify_task>' |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
def test_modify_audit_invalid_alerts_ids(self): |
98
|
|
|
with self.assertRaises(InvalidArgumentType): |
99
|
|
|
self.gmp.modify_audit(audit_id='t1', alert_ids='') |
100
|
|
|
|
101
|
|
|
with self.assertRaises(InvalidArgumentType): |
102
|
|
|
self.gmp.modify_audit(audit_id='t1', alert_ids='a1') |
103
|
|
|
|
104
|
|
|
def test_modify_audit_with_empty_alert_ids(self): |
105
|
|
|
self.gmp.modify_audit(audit_id='t1', alert_ids=[]) |
106
|
|
|
|
107
|
|
|
self.connection.send.has_been_called_with( |
108
|
|
|
'<modify_task task_id="t1">' '<alert id="0"/>' '</modify_task>' |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
def test_modify_audit_with_alterable(self): |
112
|
|
|
self.gmp.modify_audit(audit_id='t1', alterable=True) |
113
|
|
|
|
114
|
|
|
self.connection.send.has_been_called_with( |
115
|
|
|
'<modify_task task_id="t1">' |
116
|
|
|
'<alterable>1</alterable>' |
117
|
|
|
'</modify_task>' |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
self.gmp.modify_audit(audit_id='t1', alterable=False) |
121
|
|
|
|
122
|
|
|
self.connection.send.has_been_called_with( |
123
|
|
|
'<modify_task task_id="t1">' |
124
|
|
|
'<alterable>0</alterable>' |
125
|
|
|
'</modify_task>' |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
def test_modify_audit_with_hosts_ordering(self): |
129
|
|
|
self.gmp.modify_audit( |
130
|
|
|
audit_id='t1', hosts_ordering=HostsOrdering.REVERSE |
131
|
|
|
) |
132
|
|
|
|
133
|
|
|
self.connection.send.has_been_called_with( |
134
|
|
|
'<modify_task task_id="t1">' |
135
|
|
|
'<hosts_ordering>reverse</hosts_ordering>' |
136
|
|
|
'</modify_task>' |
137
|
|
|
) |
138
|
|
|
|
139
|
|
|
def test_modify_audit_invalid_hosts_ordering(self): |
140
|
|
|
with self.assertRaises(InvalidArgumentType): |
141
|
|
|
self.gmp.modify_audit(audit_id='t1', hosts_ordering='foo') |
142
|
|
|
|
143
|
|
|
def test_modify_audit_with_schedule(self): |
144
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_id='s1') |
145
|
|
|
|
146
|
|
|
self.connection.send.has_been_called_with( |
147
|
|
|
'<modify_task task_id="t1">' '<schedule id="s1"/>' '</modify_task>' |
148
|
|
|
) |
149
|
|
|
|
150
|
|
|
def test_modify_audit_with_schedule_periods(self): |
151
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_periods=0) |
152
|
|
|
|
153
|
|
|
self.connection.send.has_been_called_with( |
154
|
|
|
'<modify_task task_id="t1">' |
155
|
|
|
'<schedule_periods>0</schedule_periods>' |
156
|
|
|
'</modify_task>' |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_periods=5) |
160
|
|
|
|
161
|
|
|
self.connection.send.has_been_called_with( |
162
|
|
|
'<modify_task task_id="t1">' |
163
|
|
|
'<schedule_periods>5</schedule_periods>' |
164
|
|
|
'</modify_task>' |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
def test_modify_audit_invalid_schedule_periods(self): |
168
|
|
|
with self.assertRaises(InvalidArgument): |
169
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_periods='foo') |
170
|
|
|
|
171
|
|
|
with self.assertRaises(InvalidArgument): |
172
|
|
|
self.gmp.modify_audit(audit_id='t1', schedule_periods=-1) |
173
|
|
|
|
174
|
|
|
def test_modify_audit_with_observers(self): |
175
|
|
|
self.gmp.modify_audit(audit_id='t1', observers=['u1', 'u2']) |
176
|
|
|
|
177
|
|
|
self.connection.send.has_been_called_with( |
178
|
|
|
'<modify_task task_id="t1">' |
179
|
|
|
'<observers>u1,u2</observers>' |
180
|
|
|
'</modify_task>' |
181
|
|
|
) |
182
|
|
|
|
183
|
|
|
def test_modify_audit_invalid_observers(self): |
184
|
|
|
with self.assertRaises(InvalidArgumentType): |
185
|
|
|
self.gmp.modify_audit(audit_id='t1', observers='') |
186
|
|
|
|
187
|
|
|
with self.assertRaises(InvalidArgumentType): |
188
|
|
|
self.gmp.modify_audit(audit_id='t1', observers='foo') |
189
|
|
|
|
190
|
|
|
def test_modify_audit_with_preferences(self): |
191
|
|
|
self.gmp.modify_audit( |
192
|
|
|
audit_id='t1', |
193
|
|
|
preferences=OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')]), |
194
|
|
|
) |
195
|
|
|
|
196
|
|
|
self.connection.send.has_been_called_with( |
197
|
|
|
'<modify_task task_id="t1">' |
198
|
|
|
'<preferences>' |
199
|
|
|
'<preference>' |
200
|
|
|
'<scanner_name>foo</scanner_name>' |
201
|
|
|
'<value>bar</value>' |
202
|
|
|
'</preference>' |
203
|
|
|
'<preference>' |
204
|
|
|
'<scanner_name>lorem</scanner_name>' |
205
|
|
|
'<value>ipsum</value>' |
206
|
|
|
'</preference>' |
207
|
|
|
'</preferences>' |
208
|
|
|
'</modify_task>' |
209
|
|
|
) |
210
|
|
|
|
211
|
|
|
def test_modify_audit_invalid_preferences(self): |
212
|
|
|
with self.assertRaises(InvalidArgumentType): |
213
|
|
|
self.gmp.modify_audit(audit_id='t1', preferences='') |
214
|
|
|
|
215
|
|
|
with self.assertRaises(InvalidArgumentType): |
216
|
|
|
self.gmp.modify_audit(audit_id='t1', preferences=['foo', 'bar']) |
217
|
|
|
|