Passed
Pull Request — master (#318)
by Juan José
01:03
created

TestNVTICache.test_get_nvt_files_count()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: AGPL-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 Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (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 Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20
# pylint: disable=unused-argument, protected-access, invalid-name
21
22
""" Unit Test for ospd-openvas """
23
24
import logging
25
26
from unittest import TestCase
27
from unittest.mock import patch, Mock, PropertyMock
28
29
from ospd_openvas.errors import OspdOpenvasError
30
from ospd_openvas.nvticache import NVTICache, NVTI_CACHE_NAME
31
32
from tests.helper import assert_called
33
34
35
@patch('ospd_openvas.nvticache.OpenvasDB')
36
class TestNVTICache(TestCase):
37
    @patch('ospd_openvas.db.MainDB')
38
    def setUp(self, MockMainDB):  # pylint: disable=arguments-differ
39
        self.db = MockMainDB()
40
        self.nvti = NVTICache(self.db)
41
        self.nvti._ctx = 'foo'
42
43
    def test_set_index(self, MockOpenvasDB):
44
        self.nvti._ctx = None
45
46
        MockOpenvasDB.find_database_by_pattern.return_value = ('foo', 22)
47
48
        ctx = self.nvti.ctx
49
50
        self.assertIsNotNone(ctx)
51
        self.assertEqual(ctx, 'foo')
52
        self.assertEqual(self.nvti.index, 22)
53
54
    def test_get_feed_version(self, MockOpenvasDB):
55
        MockOpenvasDB.get_single_item.return_value = '1234'
56
57
        resp = self.nvti.get_feed_version()
58
59
        self.assertEqual(resp, '1234')
60
        MockOpenvasDB.get_single_item.assert_called_with('foo', NVTI_CACHE_NAME)
61
62
    def test_get_feed_version_not_available(self, MockOpenvasDB):
63
        pmock = PropertyMock(return_value=123)
64
        type(self.db).max_database_index = pmock
65
        self.nvti._ctx = None
66
67
        MockOpenvasDB.find_database_by_pattern.return_value = (None, None)
68
69
        resp = self.nvti.get_feed_version()
70
71
        self.assertIsNone(resp)
72
        MockOpenvasDB.find_database_by_pattern.assert_called_with(
73
            NVTI_CACHE_NAME, 123
74
        )
75
76
    def test_get_oids(self, MockOpenvasDB):
77
        MockOpenvasDB.get_filenames_and_oids.return_value = ['oids']
78
79
        resp = self.nvti.get_oids()
80
81
        self.assertEqual(resp, ['oids'])
82
83
    def test_parse_metadata_tag_missing_value(self, MockOpenvasDB):
84
        logging.Logger.error = Mock()
85
86
        tags = 'tag1'
87
        ret = NVTICache._parse_metadata_tags(  # pylint: disable=protected-access
88
            tags, '1.2.3'
89
        )
90
91
        self.assertEqual(ret, {})
92
        assert_called(logging.Logger.error)
93
94
    def test_parse_metadata_tag(self, MockOpenvasDB):
95
        tags = 'tag1=value1'
96
        ret = NVTICache._parse_metadata_tags(  # pylint: disable=protected-access
97
            tags, '1.2.3'
98
        )
99
100
        self.assertEqual(ret, {'tag1': 'value1'})
101
102
    def test_parse_metadata_tags(self, MockOpenvasDB):
103
        tags = 'tag1=value1|foo=bar'
104
        ret = NVTICache._parse_metadata_tags(  # pylint: disable=protected-access
105
            tags, '1.2.3'
106
        )
107
108
        self.assertEqual(ret, {'tag1': 'value1', 'foo': 'bar'})
109
110
    def test_get_nvt_params(self, MockOpenvasDB):
111
        prefs1 = ['1|||dns-fuzz.timelimit|||entry|||default']
112
        prefs2 = ['1|||dns-fuzz.timelimit|||entry|||']
113
        prefs3 = ['1|||dns-fuzz.timelimit|||entry']
114
115
        out_dict1 = {
116
            '1': {
117
                'id': '1',
118
                'type': 'entry',
119
                'default': 'default',
120
                'name': 'dns-fuzz.timelimit',
121
                'description': 'Description',
122
            },
123
        }
124
125
        out_dict2 = {
126
            '1': {
127
                'id': '1',
128
                'type': 'entry',
129
                'default': '',
130
                'name': 'dns-fuzz.timelimit',
131
                'description': 'Description',
132
            },
133
        }
134
135
        MockOpenvasDB.get_list_item.return_value = prefs1
136
137
        resp = self.nvti.get_nvt_params('1.2.3.4')
138
        self.assertEqual(resp, out_dict1)
139
140
        MockOpenvasDB.get_list_item.return_value = prefs2
141
142
        resp = self.nvti.get_nvt_params('1.2.3.4')
143
        self.assertEqual(resp, out_dict2)
144
145
        MockOpenvasDB.get_list_item.return_value = prefs3
146
147
        resp = self.nvti.get_nvt_params('1.2.3.4')
148
        self.assertEqual(resp, out_dict2)
149
150
    def test_get_nvt_metadata(self, MockOpenvasDB):
151
        metadata = [
152
            'mantis_detect.nasl',
153
            '',
154
            '',
155
            'Settings/disable_cgi_scanning',
156
            '',
157
            'Services/www, 80',
158
            'find_service.nasl, http_version.nasl',
159
            'cvss_base_vector=AV:N/AC:L/Au:N/C:N/I:N'
160
            '/A:N|last_modification=1533906565'
161
            '|creation_date=1237458156'
162
            '|summary=Detects the ins'
163
            'talled version of\n  Mantis a free popular web-based '
164
            'bugtracking system.\n\n  This script sends HTTP GET r'
165
            'equest and try to get the version from the\n  respons'
166
            'e, and sets the result in KB.|qod_type=remote_banner',
167
            '',
168
            '',
169
            'URL:http://www.mantisbt.org/',
170
            '3',
171
            '10',
172
            'Product detection',
173
            'Mantis Detection',
174
        ]
175
176
        custom = {
177
            'category': '3',
178
            'creation_date': '1237458156',
179
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
180
            'dependencies': 'find_service.nasl, http_version.nasl',
181
            'excluded_keys': 'Settings/disable_cgi_scanning',
182
            'family': 'Product detection',
183
            'filename': 'mantis_detect.nasl',
184
            'last_modification': ('1533906565'),
185
            'name': 'Mantis Detection',
186
            'qod_type': 'remote_banner',
187
            'refs': {'xref': ['URL:http://www.mantisbt.org/']},
188
            'required_ports': 'Services/www, 80',
189
            'summary': (
190
                'Detects the installed version of\n  Mantis a '
191
                'free popular web-based bugtracking system.\n'
192
                '\n  This script sends HTTP GET request and t'
193
                'ry to get the version from the\n  response, '
194
                'and sets the result in KB.'
195
            ),
196
            'vt_params': {
197
                '0': {
198
                    'id': '0',
199
                    'type': 'entry',
200
                    'name': 'timeout',
201
                    'description': 'Script Timeout',
202
                    'default': '10',
203
                },
204
                '1': {
205
                    'id': '1',
206
                    'type': 'entry',
207
                    'name': 'dns-fuzz.timelimit',
208
                    'description': 'Description',
209
                    'default': 'default',
210
                },
211
            },
212
        }
213
214
        prefs1 = ['1|||dns-fuzz.timelimit|||entry|||default']
215
216
        MockOpenvasDB.get_list_item.side_effect = [metadata, prefs1]
217
        resp = self.nvti.get_nvt_metadata('1.2.3.4')
218
        self.maxDiff = None
219
        self.assertEqual(resp, custom)
220
221
    def test_get_nvt_metadata_fail(self, MockOpenvasDB):
222
        MockOpenvasDB.get_list_item.return_value = []
223
224
        resp = self.nvti.get_nvt_metadata('1.2.3.4')
225
226
        self.assertIsNone(resp)
227
228
    def test_get_nvt_refs(self, MockOpenvasDB):
229
        refs = ['', '', 'URL:http://www.mantisbt.org/']
230
        out_dict = {
231
            'cve': [''],
232
            'bid': [''],
233
            'xref': ['URL:http://www.mantisbt.org/'],
234
        }
235
236
        MockOpenvasDB.get_list_item.return_value = refs
237
238
        resp = self.nvti.get_nvt_refs('1.2.3.4')
239
240
        self.assertEqual(resp, out_dict)
241
242
    def test_get_nvt_refs_fail(self, MockOpenvasDB):
243
        MockOpenvasDB.get_list_item.return_value = []
244
245
        resp = self.nvti.get_nvt_refs('1.2.3.4')
246
247
        self.assertIsNone(resp)
248
249
    def test_get_nvt_prefs(self, MockOpenvasDB):
250
        prefs = ['dns-fuzz.timelimit|||entry|||default']
251
252
        MockOpenvasDB.get_list_item.return_value = prefs
253
254
        resp = self.nvti.get_nvt_prefs('1.2.3.4')
255
256
        self.assertEqual(resp, prefs)
257
258
    def test_get_nvt_timeout(self, MockOpenvasDB):
259
        MockOpenvasDB.get_single_item.return_value = '300'
260
261
        resp = self.nvti.get_nvt_timeout('1.2.3.4')
262
263
        self.assertEqual(resp, '300')
264
265
    def test_get_nvt_tags(self, MockOpenvasDB):
266
        tag = (
267
            'last_modification=1533906565'
268
            '|creation_date=1517443741|cvss_bas'
269
            'e_vector=AV:N/AC:L/Au:N/C:P/I:P/A:P|solution_type=V'
270
            'endorFix|qod_type=package|affected=rubygems on Debi'
271
            'an Linux|solution_method=DebianAPTUpgrade'
272
        )
273
274
        out_dict = {
275
            'last_modification': '1533906565',
276
            'creation_date': '1517443741',
277
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:P/I:P/A:P',
278
            'solution_type': 'VendorFix',
279
            'qod_type': 'package',
280
            'affected': 'rubygems on Debian Linux',
281
            'solution_method': 'DebianAPTUpgrade',
282
        }
283
284
        MockOpenvasDB.get_single_item.return_value = tag
285
286
        resp = self.nvti.get_nvt_tags('1.2.3.4')
287
288
        self.assertEqual(out_dict, resp)
289
290
    def test_get_nvt_files_count(self, MockOpenvasDB):
291
        MockOpenvasDB.get_key_count.return_value = 20
292
293
        self.assertEqual(self.nvti.get_nvt_files_count(), 20)
294
        MockOpenvasDB.get_key_count.assert_called_with('foo', 'filename:*')
295
296
    def test_get_nvt_count(self, MockOpenvasDB):
297
        MockOpenvasDB.get_key_count.return_value = 20
298
299
        self.assertEqual(self.nvti.get_nvt_count(), 20)
300
        MockOpenvasDB.get_key_count.assert_called_with('foo', 'nvt:*')
301
302
    def test_force_reload(self, _MockOpenvasDB):
303
        self.nvti.force_reload()
304
305
        self.db.release_database.assert_called_with(self.nvti)
306
307
    def test_flush(self, _MockOpenvasDB):
308
        self.nvti._ctx = Mock()
309
310
        self.nvti.flush()
311
312
        self.nvti._ctx.flushdb.assert_called_with()
313