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, InvalidArgument, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208 import EntityType, AggregateStatistic, SortOrder |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpGetAggregatesTestMixin: |
25
|
|
|
def test_get_aggregates(self): |
26
|
|
|
""" |
27
|
|
|
Test basic get_aggregates calls with only resource_type except special |
28
|
|
|
cases for audit, policy, scan_config and task. |
29
|
|
|
""" |
30
|
|
|
self.gmp.get_aggregates(EntityType.ALERT) |
31
|
|
|
|
32
|
|
|
self.connection.send.has_been_called_with( |
33
|
|
|
'<get_aggregates type="alert"/>' |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
self.gmp.get_aggregates(resource_type=EntityType.CERT_BUND_ADV) |
37
|
|
|
|
38
|
|
|
self.connection.send.has_been_called_with( |
39
|
|
|
'<get_aggregates type="cert_bund_adv"/>' |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
self.gmp.get_aggregates(EntityType.CPE) |
43
|
|
|
|
44
|
|
|
self.connection.send.has_been_called_with( |
45
|
|
|
'<get_aggregates type="cpe"/>' |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
self.gmp.get_aggregates(EntityType.CVE) |
49
|
|
|
|
50
|
|
|
self.connection.send.has_been_called_with( |
51
|
|
|
'<get_aggregates type="cve"/>' |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
self.gmp.get_aggregates(EntityType.DFN_CERT_ADV) |
55
|
|
|
|
56
|
|
|
self.connection.send.has_been_called_with( |
57
|
|
|
'<get_aggregates type="dfn_cert_adv"/>' |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
self.gmp.get_aggregates(EntityType.HOST) |
61
|
|
|
|
62
|
|
|
self.connection.send.has_been_called_with( |
63
|
|
|
'<get_aggregates type="host"/>' |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
self.gmp.get_aggregates(EntityType.NOTE) |
67
|
|
|
|
68
|
|
|
self.connection.send.has_been_called_with( |
69
|
|
|
'<get_aggregates type="note"/>' |
70
|
|
|
) |
71
|
|
|
|
72
|
|
|
self.gmp.get_aggregates(EntityType.NVT) |
73
|
|
|
|
74
|
|
|
self.connection.send.has_been_called_with( |
75
|
|
|
'<get_aggregates type="nvt"/>' |
76
|
|
|
) |
77
|
|
|
|
78
|
|
|
self.gmp.get_aggregates(EntityType.OPERATING_SYSTEM) |
79
|
|
|
|
80
|
|
|
self.connection.send.has_been_called_with('<get_aggregates type="os"/>') |
81
|
|
|
|
82
|
|
|
self.gmp.get_aggregates(EntityType.OVALDEF) |
83
|
|
|
|
84
|
|
|
self.connection.send.has_been_called_with( |
85
|
|
|
'<get_aggregates type="ovaldef"/>' |
86
|
|
|
) |
87
|
|
|
|
88
|
|
|
self.gmp.get_aggregates(EntityType.OVERRIDE) |
89
|
|
|
|
90
|
|
|
self.connection.send.has_been_called_with( |
91
|
|
|
'<get_aggregates type="override"/>' |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
self.gmp.get_aggregates(EntityType.REPORT) |
95
|
|
|
|
96
|
|
|
self.connection.send.has_been_called_with( |
97
|
|
|
'<get_aggregates type="report"/>' |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
self.gmp.get_aggregates(EntityType.RESULT) |
101
|
|
|
|
102
|
|
|
self.connection.send.has_been_called_with( |
103
|
|
|
'<get_aggregates type="result"/>' |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
def test_get_aggregates_resource_types_with_usage_type(self): |
107
|
|
|
""" |
108
|
|
|
Test special cases of resource_type in get_aggregates calls that |
109
|
|
|
should add a usage_type parameter: audit, policy, scan_config and task. |
110
|
|
|
""" |
111
|
|
|
self.gmp.get_aggregates(EntityType.AUDIT) |
112
|
|
|
|
113
|
|
|
self.connection.send.has_been_called_with( |
114
|
|
|
'<get_aggregates usage_type="audit" type="task"/>' |
115
|
|
|
) |
116
|
|
|
|
117
|
|
|
self.gmp.get_aggregates(EntityType.POLICY) |
118
|
|
|
|
119
|
|
|
self.connection.send.has_been_called_with( |
120
|
|
|
'<get_aggregates usage_type="policy" type="config"/>' |
121
|
|
|
) |
122
|
|
|
|
123
|
|
|
self.gmp.get_aggregates(EntityType.SCAN_CONFIG) |
124
|
|
|
|
125
|
|
|
self.connection.send.has_been_called_with( |
126
|
|
|
'<get_aggregates usage_type="scan" type="config"/>' |
127
|
|
|
) |
128
|
|
|
|
129
|
|
|
self.gmp.get_aggregates(EntityType.TASK) |
130
|
|
|
|
131
|
|
|
self.connection.send.has_been_called_with( |
132
|
|
|
'<get_aggregates usage_type="scan" type="task"/>' |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
def test_get_aggregates_missing_resource_type(self): |
136
|
|
|
""" |
137
|
|
|
Test get_aggregates calls with missing resource_type |
138
|
|
|
""" |
139
|
|
|
with self.assertRaises(RequiredArgument): |
140
|
|
|
self.gmp.get_aggregates(resource_type=None) |
141
|
|
|
|
142
|
|
|
with self.assertRaises(RequiredArgument): |
143
|
|
|
self.gmp.get_aggregates(resource_type='') |
144
|
|
|
|
145
|
|
|
with self.assertRaises(RequiredArgument): |
146
|
|
|
self.gmp.get_aggregates('') |
147
|
|
|
|
148
|
|
|
def test_get_aggregates_invalid_resource_type(self): |
149
|
|
|
""" |
150
|
|
|
Test get_aggregates calls with invalid resource_type |
151
|
|
|
""" |
152
|
|
|
with self.assertRaises(InvalidArgumentType): |
153
|
|
|
self.gmp.get_aggregates(resource_type='foo') |
154
|
|
|
|
155
|
|
|
def test_get_aggregates_sort_criteria(self): |
156
|
|
|
""" |
157
|
|
|
Test get_aggregates calls with sort_criteria given as strings |
158
|
|
|
""" |
159
|
|
|
self.gmp.get_aggregates( |
160
|
|
|
EntityType.NVT, |
161
|
|
|
group_column='family', |
162
|
|
|
sort_criteria=[ |
163
|
|
|
{'field': 'severity', 'stat': 'mean', 'order': 'descending'}, |
164
|
|
|
{'stat': 'count', 'order': 'descending'}, |
165
|
|
|
{'field': 'family', 'order': 'ascending'}, |
166
|
|
|
], |
167
|
|
|
data_columns=['severity'], |
168
|
|
|
) |
169
|
|
|
|
170
|
|
|
self.connection.send.has_been_called_with( |
171
|
|
|
'<get_aggregates type="nvt" group_column="family">' |
172
|
|
|
'<sort field="severity" stat="mean" order="descending"/>' |
173
|
|
|
'<sort stat="count" order="descending"/>' |
174
|
|
|
'<sort field="family" order="ascending"/>' |
175
|
|
|
'<data_column>severity</data_column>' |
176
|
|
|
'</get_aggregates>' |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
def test_get_aggregates_sort_criteria_enum(self): |
180
|
|
|
""" |
181
|
|
|
Test get_aggregates calls with sort_criteria given as enums |
182
|
|
|
""" |
183
|
|
|
self.gmp.get_aggregates( |
184
|
|
|
EntityType.NVT, |
185
|
|
|
group_column='family', |
186
|
|
|
sort_criteria=[ |
187
|
|
|
{ |
188
|
|
|
'field': 'severity', |
189
|
|
|
'stat': AggregateStatistic.MEAN, |
190
|
|
|
'order': SortOrder.DESCENDING, |
191
|
|
|
} |
192
|
|
|
], |
193
|
|
|
data_columns=['severity'], |
194
|
|
|
) |
195
|
|
|
|
196
|
|
|
self.connection.send.has_been_called_with( |
197
|
|
|
'<get_aggregates type="nvt" group_column="family">' |
198
|
|
|
'<sort field="severity" stat="mean" order="descending"/>' |
199
|
|
|
'<data_column>severity</data_column>' |
200
|
|
|
'</get_aggregates>' |
201
|
|
|
) |
202
|
|
|
|
203
|
|
|
def test_get_aggregates_invalid_sort_criteria(self): |
204
|
|
|
""" |
205
|
|
|
Test get_aggregates calls with invalid sort_criteria |
206
|
|
|
""" |
207
|
|
|
with self.assertRaises(InvalidArgumentType): |
208
|
|
|
self.gmp.get_aggregates( |
209
|
|
|
resource_type=EntityType.ALERT, sort_criteria='INVALID' |
210
|
|
|
) |
211
|
|
|
|
212
|
|
|
with self.assertRaises(InvalidArgumentType): |
213
|
|
|
self.gmp.get_aggregates( |
214
|
|
|
resource_type=EntityType.ALERT, sort_criteria=['INVALID'] |
215
|
|
|
) |
216
|
|
|
|
217
|
|
|
with self.assertRaises(InvalidArgument): |
218
|
|
|
self.gmp.get_aggregates( |
219
|
|
|
resource_type=EntityType.ALERT, |
220
|
|
|
sort_criteria=[{'stat': 'INVALID'}], |
221
|
|
|
) |
222
|
|
|
|
223
|
|
|
with self.assertRaises(InvalidArgument): |
224
|
|
|
self.gmp.get_aggregates( |
225
|
|
|
resource_type=EntityType.ALERT, |
226
|
|
|
sort_criteria=[{'order': 'INVALID'}], |
227
|
|
|
) |
228
|
|
|
|
229
|
|
|
def test_get_aggregates_group_limits(self): |
230
|
|
|
""" |
231
|
|
|
Test get_aggregates calls with group limits (first_group, max_groups) |
232
|
|
|
""" |
233
|
|
|
self.gmp.get_aggregates(EntityType.CPE, first_group=20, max_groups=25) |
234
|
|
|
|
235
|
|
|
self.connection.send.has_been_called_with( |
236
|
|
|
'<get_aggregates type="cpe" first_group="20" max_groups="25"/>' |
237
|
|
|
) |
238
|
|
|
|
239
|
|
|
def test_get_aggregates_invalid_group_limits(self): |
240
|
|
|
""" |
241
|
|
|
Test get_aggregates calls with invalid group limits |
242
|
|
|
""" |
243
|
|
|
with self.assertRaises(InvalidArgumentType): |
244
|
|
|
self.gmp.get_aggregates( |
245
|
|
|
EntityType.CPE, first_group="INVALID", max_groups=25 |
246
|
|
|
) |
247
|
|
|
|
248
|
|
|
with self.assertRaises(InvalidArgumentType): |
249
|
|
|
self.gmp.get_aggregates( |
250
|
|
|
EntityType.CPE, first_group=1, max_groups="INVALID" |
251
|
|
|
) |
252
|
|
|
|
253
|
|
|
def test_get_aggregates_data_columns(self): |
254
|
|
|
""" |
255
|
|
|
Test get_aggregates calls with data_columns |
256
|
|
|
""" |
257
|
|
|
self.gmp.get_aggregates( |
258
|
|
|
EntityType.CPE, data_columns=['severity', 'cves'] |
259
|
|
|
) |
260
|
|
|
|
261
|
|
|
self.connection.send.has_been_called_with( |
262
|
|
|
'<get_aggregates type="cpe">' |
263
|
|
|
'<data_column>severity</data_column>' |
264
|
|
|
'<data_column>cves</data_column>' |
265
|
|
|
'</get_aggregates>' |
266
|
|
|
) |
267
|
|
|
|
268
|
|
|
def test_get_aggregates_invalid_data_columns(self): |
269
|
|
|
""" |
270
|
|
|
Test get_aggregates calls with invalid data_columns |
271
|
|
|
""" |
272
|
|
|
with self.assertRaises(InvalidArgumentType): |
273
|
|
|
self.gmp.get_aggregates( |
274
|
|
|
resource_type=EntityType.ALERT, data_columns='INVALID' |
275
|
|
|
) |
276
|
|
|
|
277
|
|
|
def test_get_aggregates_group_column(self): |
278
|
|
|
""" |
279
|
|
|
Test get_aggregates calls with group_column |
280
|
|
|
""" |
281
|
|
|
self.gmp.get_aggregates(EntityType.NVT, group_column='family') |
282
|
|
|
|
283
|
|
|
self.connection.send.has_been_called_with( |
284
|
|
|
'<get_aggregates type="nvt" group_column="family"/>' |
285
|
|
|
) |
286
|
|
|
|
287
|
|
|
def test_get_aggregates_subgroup_column(self): |
288
|
|
|
""" |
289
|
|
|
Test get_aggregates calls with subgroup_column |
290
|
|
|
""" |
291
|
|
|
self.gmp.get_aggregates( |
292
|
|
|
EntityType.NVT, |
293
|
|
|
group_column='family', |
294
|
|
|
subgroup_column='solution_type', |
295
|
|
|
) |
296
|
|
|
|
297
|
|
|
self.connection.send.has_been_called_with( |
298
|
|
|
'<get_aggregates type="nvt" group_column="family"' |
299
|
|
|
' subgroup_column="solution_type"/>' |
300
|
|
|
) |
301
|
|
|
|
302
|
|
|
def test_get_aggregates_missing_group_column(self): |
303
|
|
|
""" |
304
|
|
|
Test get_aggregates calls with group_column missing |
305
|
|
|
if subgroup_column was given. |
306
|
|
|
""" |
307
|
|
|
with self.assertRaises(RequiredArgument): |
308
|
|
|
self.gmp.get_aggregates( |
309
|
|
|
resource_type=EntityType.NVT, subgroup_column='solution_type' |
310
|
|
|
) |
311
|
|
|
|
312
|
|
|
with self.assertRaises(RequiredArgument): |
313
|
|
|
self.gmp.get_aggregates( |
314
|
|
|
resource_type=EntityType.NVT, |
315
|
|
|
group_column='', |
316
|
|
|
subgroup_column='solution_type', |
317
|
|
|
) |
318
|
|
|
|
319
|
|
|
def test_get_aggregates_text_columns(self): |
320
|
|
|
""" |
321
|
|
|
Test get_aggregates calls with text_columns |
322
|
|
|
""" |
323
|
|
|
self.gmp.get_aggregates( |
324
|
|
|
EntityType.SCAN_CONFIG, |
325
|
|
|
group_column="uuid", |
326
|
|
|
text_columns=['name', 'comment'], |
327
|
|
|
) |
328
|
|
|
|
329
|
|
|
self.connection.send.has_been_called_with( |
330
|
|
|
'<get_aggregates usage_type="scan" type="config"' |
331
|
|
|
' group_column="uuid">' |
332
|
|
|
'<text_column>name</text_column>' |
333
|
|
|
'<text_column>comment</text_column>' |
334
|
|
|
'</get_aggregates>' |
335
|
|
|
) |
336
|
|
|
|
337
|
|
|
def test_get_aggregates_invalid_text_columns(self): |
338
|
|
|
""" |
339
|
|
|
Test get_aggregates calls with invalid text_columns |
340
|
|
|
""" |
341
|
|
|
with self.assertRaises(InvalidArgumentType): |
342
|
|
|
self.gmp.get_aggregates( |
343
|
|
|
resource_type=EntityType.ALERT, text_columns='INVALID' |
344
|
|
|
) |
345
|
|
|
|
346
|
|
|
def test_get_aggregates_mode(self): |
347
|
|
|
""" |
348
|
|
|
Test get_aggregates calls with mode |
349
|
|
|
""" |
350
|
|
|
self.gmp.get_aggregates( |
351
|
|
|
EntityType.NVT, group_column='name', mode='word_counts' |
352
|
|
|
) |
353
|
|
|
|
354
|
|
|
self.connection.send.has_been_called_with( |
355
|
|
|
'<get_aggregates type="nvt" group_column="name"' |
356
|
|
|
' mode="word_counts"/>' |
357
|
|
|
) |
358
|
|
|
|