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