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 decimal import Decimal |
20
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument, InvalidArgumentType |
22
|
|
|
|
23
|
|
|
from gvm.protocols.gmpv208 import SeverityLevel |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class GmpCreateOverrideTestMixin: |
27
|
|
|
def test_create_override(self): |
28
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1') |
29
|
|
|
|
30
|
|
|
self.connection.send.has_been_called_with( |
31
|
|
|
'<create_override>' |
32
|
|
|
'<text>foo</text>' |
33
|
|
|
'<nvt oid="oid1"/>' |
34
|
|
|
'</create_override>' |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
def test_create_override_missing_text(self): |
38
|
|
|
with self.assertRaises(RequiredArgument): |
39
|
|
|
self.gmp.create_override(None, 'od1') |
40
|
|
|
|
41
|
|
|
with self.assertRaises(RequiredArgument): |
42
|
|
|
self.gmp.create_override('', 'oid1') |
43
|
|
|
|
44
|
|
|
def test_create_override_missing_nvt_oid(self): |
45
|
|
|
with self.assertRaises(RequiredArgument): |
46
|
|
|
self.gmp.create_override('foo', None) |
47
|
|
|
|
48
|
|
|
with self.assertRaises(RequiredArgument): |
49
|
|
|
self.gmp.create_override('foo', '') |
50
|
|
|
|
51
|
|
|
def test_create_override_with_hosts(self): |
52
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', hosts=[]) |
53
|
|
|
|
54
|
|
|
self.connection.send.has_been_called_with( |
55
|
|
|
'<create_override>' |
56
|
|
|
'<text>foo</text>' |
57
|
|
|
'<nvt oid="oid1"/>' |
58
|
|
|
'</create_override>' |
59
|
|
|
) |
60
|
|
|
|
61
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', hosts=['h1', 'h2']) |
62
|
|
|
|
63
|
|
|
self.connection.send.has_been_called_with( |
64
|
|
|
'<create_override>' |
65
|
|
|
'<text>foo</text>' |
66
|
|
|
'<nvt oid="oid1"/>' |
67
|
|
|
'<hosts>h1,h2</hosts>' |
68
|
|
|
'</create_override>' |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
def test_create_override_with_port(self): |
72
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', port='666') |
73
|
|
|
|
74
|
|
|
self.connection.send.has_been_called_with( |
75
|
|
|
'<create_override>' |
76
|
|
|
'<text>foo</text>' |
77
|
|
|
'<nvt oid="oid1"/>' |
78
|
|
|
'<port>666</port>' |
79
|
|
|
'</create_override>' |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', port=666) |
83
|
|
|
|
84
|
|
|
self.connection.send.has_been_called_with( |
85
|
|
|
'<create_override>' |
86
|
|
|
'<text>foo</text>' |
87
|
|
|
'<nvt oid="oid1"/>' |
88
|
|
|
'<port>666</port>' |
89
|
|
|
'</create_override>' |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
def test_create_override_with_result_id(self): |
93
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', result_id='r1') |
94
|
|
|
|
95
|
|
|
self.connection.send.has_been_called_with( |
96
|
|
|
'<create_override>' |
97
|
|
|
'<text>foo</text>' |
98
|
|
|
'<nvt oid="oid1"/>' |
99
|
|
|
'<result id="r1"/>' |
100
|
|
|
'</create_override>' |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
def test_create_override_with_task_id(self): |
104
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', task_id='t1') |
105
|
|
|
|
106
|
|
|
self.connection.send.has_been_called_with( |
107
|
|
|
'<create_override>' |
108
|
|
|
'<text>foo</text>' |
109
|
|
|
'<nvt oid="oid1"/>' |
110
|
|
|
'<task id="t1"/>' |
111
|
|
|
'</create_override>' |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
def test_create_override_with_severity(self): |
115
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', severity='5.5') |
116
|
|
|
|
117
|
|
|
self.connection.send.has_been_called_with( |
118
|
|
|
'<create_override>' |
119
|
|
|
'<text>foo</text>' |
120
|
|
|
'<nvt oid="oid1"/>' |
121
|
|
|
'<severity>5.5</severity>' |
122
|
|
|
'</create_override>' |
123
|
|
|
) |
124
|
|
|
|
125
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', severity=5.5) |
126
|
|
|
|
127
|
|
|
self.connection.send.has_been_called_with( |
128
|
|
|
'<create_override>' |
129
|
|
|
'<text>foo</text>' |
130
|
|
|
'<nvt oid="oid1"/>' |
131
|
|
|
'<severity>5.5</severity>' |
132
|
|
|
'</create_override>' |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', severity=Decimal(5.5)) |
136
|
|
|
|
137
|
|
|
self.connection.send.has_been_called_with( |
138
|
|
|
'<create_override>' |
139
|
|
|
'<text>foo</text>' |
140
|
|
|
'<nvt oid="oid1"/>' |
141
|
|
|
'<severity>5.5</severity>' |
142
|
|
|
'</create_override>' |
143
|
|
|
) |
144
|
|
|
|
145
|
|
|
def test_create_override_with_new_severity(self): |
146
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', new_severity='5.5') |
147
|
|
|
|
148
|
|
|
self.connection.send.has_been_called_with( |
149
|
|
|
'<create_override>' |
150
|
|
|
'<text>foo</text>' |
151
|
|
|
'<nvt oid="oid1"/>' |
152
|
|
|
'<new_severity>5.5</new_severity>' |
153
|
|
|
'</create_override>' |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', new_severity=5.5) |
157
|
|
|
|
158
|
|
|
self.connection.send.has_been_called_with( |
159
|
|
|
'<create_override>' |
160
|
|
|
'<text>foo</text>' |
161
|
|
|
'<nvt oid="oid1"/>' |
162
|
|
|
'<new_severity>5.5</new_severity>' |
163
|
|
|
'</create_override>' |
164
|
|
|
) |
165
|
|
|
|
166
|
|
|
self.gmp.create_override( |
167
|
|
|
'foo', nvt_oid='oid1', new_severity=Decimal(5.5) |
168
|
|
|
) |
169
|
|
|
|
170
|
|
|
self.connection.send.has_been_called_with( |
171
|
|
|
'<create_override>' |
172
|
|
|
'<text>foo</text>' |
173
|
|
|
'<nvt oid="oid1"/>' |
174
|
|
|
'<new_severity>5.5</new_severity>' |
175
|
|
|
'</create_override>' |
176
|
|
|
) |
177
|
|
|
|
178
|
|
|
def test_create_override_with_threat(self): |
179
|
|
|
self.gmp.create_override( |
180
|
|
|
'foo', nvt_oid='oid1', threat=SeverityLevel.HIGH |
181
|
|
|
) |
182
|
|
|
|
183
|
|
|
self.connection.send.has_been_called_with( |
184
|
|
|
'<create_override>' |
185
|
|
|
'<text>foo</text>' |
186
|
|
|
'<nvt oid="oid1"/>' |
187
|
|
|
'<threat>High</threat>' |
188
|
|
|
'</create_override>' |
189
|
|
|
) |
190
|
|
|
|
191
|
|
|
def test_create_override_invalid_threat(self): |
192
|
|
|
with self.assertRaises(InvalidArgumentType): |
193
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', threat='') |
194
|
|
|
|
195
|
|
|
with self.assertRaises(InvalidArgumentType): |
196
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', threat='foo') |
197
|
|
|
|
198
|
|
|
def test_create_override_with_new_threat(self): |
199
|
|
|
self.gmp.create_override( |
200
|
|
|
'foo', nvt_oid='oid1', new_threat=SeverityLevel.HIGH |
201
|
|
|
) |
202
|
|
|
|
203
|
|
|
self.connection.send.has_been_called_with( |
204
|
|
|
'<create_override>' |
205
|
|
|
'<text>foo</text>' |
206
|
|
|
'<nvt oid="oid1"/>' |
207
|
|
|
'<new_threat>High</new_threat>' |
208
|
|
|
'</create_override>' |
209
|
|
|
) |
210
|
|
|
|
211
|
|
|
def test_create_override_invalid_new_threat(self): |
212
|
|
|
with self.assertRaises(InvalidArgumentType): |
213
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', new_threat='') |
214
|
|
|
|
215
|
|
|
with self.assertRaises(InvalidArgumentType): |
216
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', new_threat='foo') |
217
|
|
|
|
218
|
|
|
def test_create_override_with_days_active(self): |
219
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', days_active=0) |
220
|
|
|
|
221
|
|
|
self.connection.send.has_been_called_with( |
222
|
|
|
'<create_override>' |
223
|
|
|
'<text>foo</text>' |
224
|
|
|
'<nvt oid="oid1"/>' |
225
|
|
|
'<active>0</active>' |
226
|
|
|
'</create_override>' |
227
|
|
|
) |
228
|
|
|
|
229
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', days_active=-1) |
230
|
|
|
|
231
|
|
|
self.connection.send.has_been_called_with( |
232
|
|
|
'<create_override>' |
233
|
|
|
'<text>foo</text>' |
234
|
|
|
'<nvt oid="oid1"/>' |
235
|
|
|
'<active>-1</active>' |
236
|
|
|
'</create_override>' |
237
|
|
|
) |
238
|
|
|
|
239
|
|
|
self.gmp.create_override('foo', nvt_oid='oid1', days_active=3600) |
240
|
|
|
|
241
|
|
|
self.connection.send.has_been_called_with( |
242
|
|
|
'<create_override>' |
243
|
|
|
'<text>foo</text>' |
244
|
|
|
'<nvt oid="oid1"/>' |
245
|
|
|
'<active>3600</active>' |
246
|
|
|
'</create_override>' |
247
|
|
|
) |
248
|
|
|
|