-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_nvti_cache.py
353 lines (279 loc) · 10.5 KB
/
test_nvti_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2014-2023 Greenbone AG
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=unused-argument, protected-access, invalid-name
""" Unit Test for ospd-openvas """
import logging
from unittest import TestCase
from unittest.mock import patch, Mock, PropertyMock
from pathlib import Path
from ospd_openvas.nvticache import NVTICache, NVTI_CACHE_NAME
from tests.helper import assert_called
@patch('ospd_openvas.nvticache.OpenvasDB')
class TestNVTICache(TestCase):
@patch('ospd_openvas.db.MainDB')
def setUp(self, MockMainDB): # pylint: disable=arguments-differ
self.db = MockMainDB()
self.nvti = NVTICache(self.db)
self.nvti._ctx = 'foo'
def test_set_index(self, MockOpenvasDB):
self.nvti._ctx = None
MockOpenvasDB.find_database_by_pattern.return_value = ('foo', 22)
ctx = self.nvti.ctx
self.assertIsNotNone(ctx)
self.assertEqual(ctx, 'foo')
self.assertEqual(self.nvti.index, 22)
def test_get_feed_version(self, MockOpenvasDB):
MockOpenvasDB.get_single_item.return_value = '1234'
resp = self.nvti.get_feed_version()
self.assertEqual(resp, '1234')
MockOpenvasDB.get_single_item.assert_called_with('foo', NVTI_CACHE_NAME)
def test_get_feed_version_not_available(self, MockOpenvasDB):
pmock = PropertyMock(return_value=123)
type(self.db).max_database_index = pmock
self.nvti._ctx = None
MockOpenvasDB.find_database_by_pattern.return_value = (None, None)
resp = self.nvti.get_feed_version()
self.assertIsNone(resp)
MockOpenvasDB.find_database_by_pattern.assert_called_with(
NVTI_CACHE_NAME, 123
)
def test_get_oids(self, MockOpenvasDB):
MockOpenvasDB.get_filenames_and_oids.return_value = [
('filename', 'oid')
]
resp = self.nvti.get_oids()
self.assertEqual([x for x in resp], [('filename', 'oid')])
def test_parse_metadata_tag_missing_value(self, MockOpenvasDB):
logging.Logger.error = Mock()
tags = 'tag1'
ret = (
NVTICache._parse_metadata_tags( # pylint: disable=protected-access
tags, '1.2.3'
)
)
self.assertEqual(ret, {})
assert_called(logging.Logger.error)
def test_parse_metadata_tag(self, MockOpenvasDB):
tags = 'tag1=value1'
ret = (
NVTICache._parse_metadata_tags( # pylint: disable=protected-access
tags, '1.2.3'
)
)
self.assertEqual(ret, {'tag1': 'value1'})
def test_parse_metadata_tags(self, MockOpenvasDB):
tags = 'tag1=value1|foo=bar'
ret = (
NVTICache._parse_metadata_tags( # pylint: disable=protected-access
tags, '1.2.3'
)
)
self.assertEqual(ret, {'tag1': 'value1', 'foo': 'bar'})
def test_get_nvt_params(self, MockOpenvasDB):
prefs1 = ['1|||dns-fuzz.timelimit|||entry|||default']
prefs2 = ['1|||dns-fuzz.timelimit|||entry|||']
prefs3 = ['1|||dns-fuzz.timelimit|||entry']
out_dict1 = {
'1': {
'id': '1',
'type': 'entry',
'default': 'default',
'name': 'dns-fuzz.timelimit',
'description': 'Description',
},
}
out_dict2 = {
'1': {
'id': '1',
'type': 'entry',
'default': '',
'name': 'dns-fuzz.timelimit',
'description': 'Description',
},
}
MockOpenvasDB.get_list_item.return_value = prefs1
resp = self.nvti.get_nvt_params('1.2.3.4')
self.assertEqual(resp, out_dict1)
MockOpenvasDB.get_list_item.return_value = prefs2
resp = self.nvti.get_nvt_params('1.2.3.4')
self.assertEqual(resp, out_dict2)
MockOpenvasDB.get_list_item.return_value = prefs3
resp = self.nvti.get_nvt_params('1.2.3.4')
self.assertEqual(resp, out_dict2)
def test_get_nvt_metadata(self, MockOpenvasDB):
metadata = [
'mantis_detect.nasl',
'',
'',
'Settings/disable_cgi_scanning',
'',
'Services/www, 80',
'find_service.nasl, http_version.nasl',
'cvss_base_vector=AV:N/AC:L/Au:N/C:N/I:N'
'/A:N|last_modification=1533906565'
'|creation_date=1237458156'
'|summary=Detects the ins'
'talled version of\n Mantis a free popular web-based '
'bugtracking system.\n\n This script sends HTTP GET r'
'equest and try to get the version from the\n respons'
'e, and sets the result in KB.|qod_type=remote_banner',
'',
'',
'URL:http://www.mantisbt.org/',
'3',
'Product detection',
'Mantis Detection',
]
custom = {
'category': '3',
'creation_date': '1237458156',
'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
'dependencies': 'find_service.nasl, http_version.nasl',
'excluded_keys': 'Settings/disable_cgi_scanning',
'family': 'Product detection',
'filename': 'mantis_detect.nasl',
'last_modification': ('1533906565'),
'name': 'Mantis Detection',
'qod_type': 'remote_banner',
'refs': {'xref': ['URL:http://www.mantisbt.org/']},
'required_ports': 'Services/www, 80',
'summary': (
'Detects the installed version of\n Mantis a '
'free popular web-based bugtracking system.\n'
'\n This script sends HTTP GET request and t'
'ry to get the version from the\n response, '
'and sets the result in KB.'
),
'vt_params': {
'0': {
'id': '0',
'type': 'entry',
'name': 'timeout',
'description': 'Description',
'default': '10',
},
'1': {
'id': '1',
'type': 'entry',
'name': 'dns-fuzz.timelimit',
'description': 'Description',
'default': 'default',
},
},
}
prefs1 = [
'0|||timeout|||entry|||10',
'1|||dns-fuzz.timelimit|||entry|||default',
]
MockOpenvasDB.get_list_item.side_effect = [metadata, prefs1]
resp = self.nvti.get_nvt_metadata('1.2.3.4')
self.maxDiff = None
self.assertEqual(resp, custom)
def test_get_nvt_metadata_fail(self, MockOpenvasDB):
MockOpenvasDB.get_list_item.return_value = []
resp = self.nvti.get_nvt_metadata('1.2.3.4')
self.assertIsNone(resp)
def test_get_nvt_refs(self, MockOpenvasDB):
refs = ['', '', 'URL:http://www.mantisbt.org/']
out_dict = {
'cve': [''],
'bid': [''],
'xref': ['URL:http://www.mantisbt.org/'],
}
MockOpenvasDB.get_list_item.return_value = refs
resp = self.nvti.get_nvt_refs('1.2.3.4')
self.assertEqual(resp, out_dict)
def test_get_nvt_refs_fail(self, MockOpenvasDB):
MockOpenvasDB.get_list_item.return_value = []
resp = self.nvti.get_nvt_refs('1.2.3.4')
self.assertIsNone(resp)
def test_get_nvt_prefs(self, MockOpenvasDB):
prefs = ['dns-fuzz.timelimit|||entry|||default']
MockOpenvasDB.get_list_item.return_value = prefs
resp = self.nvti.get_nvt_prefs('1.2.3.4')
self.assertEqual(resp, prefs)
def test_get_nvt_tags(self, MockOpenvasDB):
tag = (
'last_modification=1533906565'
'|creation_date=1517443741|cvss_bas'
'e_vector=AV:N/AC:L/Au:N/C:P/I:P/A:P|solution_type=V'
'endorFix|qod_type=package|affected=rubygems on Debi'
'an Linux|solution_method=DebianAPTUpgrade'
)
out_dict = {
'last_modification': '1533906565',
'creation_date': '1517443741',
'cvss_base_vector': 'AV:N/AC:L/Au:N/C:P/I:P/A:P',
'solution_type': 'VendorFix',
'qod_type': 'package',
'affected': 'rubygems on Debian Linux',
'solution_method': 'DebianAPTUpgrade',
}
MockOpenvasDB.get_single_item.return_value = tag
resp = self.nvti.get_nvt_tags('1.2.3.4')
self.assertEqual(out_dict, resp)
def test_get_nvt_files_count(self, MockOpenvasDB):
MockOpenvasDB.get_key_count.return_value = 20
self.assertEqual(self.nvti.get_nvt_files_count(), 20)
MockOpenvasDB.get_key_count.assert_called_with('foo', 'filename:*')
def test_get_nvt_count(self, MockOpenvasDB):
MockOpenvasDB.get_key_count.return_value = 20
self.assertEqual(self.nvti.get_nvt_count(), 20)
MockOpenvasDB.get_key_count.assert_called_with('foo', 'nvt:*')
def test_flush(self, _MockOpenvasDB):
self.nvti._ctx = Mock()
self.nvti.flush()
self.nvti._ctx.flushdb.assert_called_with()
def test_add_vt(self, MockOpenvasDB):
MockOpenvasDB.add_single_list = Mock()
self.nvti.add_vt_to_cache(
'1234',
[
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
],
)
MockOpenvasDB.add_single_list.assert_called_with(
'foo',
'1234',
[
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
],
)
def test_get_file_checksum(self, MockOpenvasDB):
MockOpenvasDB.get_single_item.return_value = '123456'
path = Path("/tmp/foo.csv")
resp = self.nvti.get_file_checksum(path)
self.assertEqual(resp, '123456')
MockOpenvasDB.get_single_item.assert_called_with(
'foo', "sha256sums:/tmp/foo.csv"
)