Completed
Push — master ( bac420...8e9778 )
by
unknown
18s queued 11s
created

TestNVTICache.test_set_nvticache_str()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 3
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2019 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-2.0-or-later
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the 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 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, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20
# pylint: disable=unused-argument
21
22
""" Unit Test for ospd-openvas """
23
24
from unittest import TestCase
25
from unittest.mock import patch
26
27
from ospd_openvas.db import OpenvasDB
28
from ospd_openvas.nvticache import NVTICache
29
30
31
@patch('ospd_openvas.db.redis.Redis')
32
class TestNVTICache(TestCase):
33
    def setUp(self):
34
        self.db = OpenvasDB()
35
        self.nvti = NVTICache(self.db)
36
37
    def test_get_feed_version(self, mock_redis):
38
        with patch.object(OpenvasDB, 'db_find', return_value=mock_redis):
39
            with patch.object(
40
                OpenvasDB, 'get_single_item', return_value='1234'
41
            ):
42
                resp = self.nvti.get_feed_version()
43
        self.assertEqual(resp, '1234')
44
45
    def test_get_oids(self, mock_redis):
46
        with patch.object(
47
            OpenvasDB, 'get_elem_pattern_by_index', return_value=['oids']
48
        ):
49
            resp = self.nvti.get_oids()
50
        self.assertEqual(resp, ['oids'])
51
52
    def test_parse_metadata_tags(self, mock_redis):
53
        tags = 'tag1'
54
        ret = self.nvti._parse_metadata_tags(  # pylint: disable=protected-access
55
            tags, '1.2.3'
56
        )
57
        self.assertEqual(ret, {})
58
59
    def test_get_nvt_params(self, mock_redis):
60
        prefs = ['1|||dns-fuzz.timelimit|||entry|||default']
61
        prefs1 = ['1|||dns-fuzz.timelimit|||entry|||']
62
63
        timeout = '300'
64
        out_dict = {
65
            '1': {
66
                'id': '1',
67
                'type': 'entry',
68
                'default': 'default',
69
                'name': 'dns-fuzz.timelimit',
70
                'description': 'Description',
71
            },
72
            '0': {
73
                'type': 'entry',
74
                'id': '0',
75
                'default': '300',
76
                'name': 'timeout',
77
                'description': 'Script Timeout',
78
            },
79
        }
80
81
        out_dict1 = {
82
            '1': {
83
                'id': '1',
84
                'type': 'entry',
85
                'default': '',
86
                'name': 'dns-fuzz.timelimit',
87
                'description': 'Description',
88
            },
89
            '0': {
90
                'type': 'entry',
91
                'id': '0',
92
                'default': '300',
93
                'name': 'timeout',
94
                'description': 'Script Timeout',
95
            },
96
        }
97
        with patch.object(OpenvasDB, 'get_kb_context', return_value=mock_redis):
98
            with patch.object(
99
                NVTICache, 'get_nvt_timeout', return_value=timeout
100
            ):
101
                with patch.object(
102
                    NVTICache, 'get_nvt_prefs', return_value=prefs
103
                ):
104
105
                    resp = self.nvti.get_nvt_params('1.2.3.4')
106
107
                with patch.object(
108
                    NVTICache, 'get_nvt_prefs', return_value=prefs1
109
                ):
110
111
                    resp1 = self.nvti.get_nvt_params('1.2.3.4')
112
        self.assertEqual(resp, out_dict)
113
        self.assertEqual(resp1, out_dict1)
114
115
    @patch('ospd_openvas.db.subprocess')
116
    def test_get_nvt_metadata(self, mock_subps, mock_redis):
117
        metadata = [
118
            'mantis_detect.nasl',
119
            '',
120
            '',
121
            'Settings/disable_cgi_scanning',
122
            '',
123
            'Services/www, 80',
124
            'find_service.nasl, http_version.nasl',
125
            'cvss_base_vector=AV:N/AC:L/Au:N/C:N/I:N'
126
            '/A:N|last_modification=1533906565'
127
            '|creation_date=1237458156'
128
            '|summary=Detects the ins'
129
            'talled version of\n  Mantis a free popular web-based '
130
            'bugtracking system.\n\n  This script sends HTTP GET r'
131
            'equest and try to get the version from the\n  respons'
132
            'e, and sets the result in KB.|qod_type=remote_banner',
133
            '',
134
            '',
135
            'URL:http://www.mantisbt.org/',
136
            '3',
137
            '0',
138
            'Product detection',
139
            'Mantis Detection',
140
        ]
141
142
        custom = {
143
            'category': '3',
144
            'creation_date': '1237458156',
145
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
146
            'dependencies': 'find_service.nasl, http_version.nasl',
147
            'excluded_keys': 'Settings/disable_cgi_scanning',
148
            'family': 'Product detection',
149
            'filename': 'mantis_detect.nasl',
150
            'last_modification': ('1533906565'),
151
            'name': 'Mantis Detection',
152
            'qod_type': 'remote_banner',
153
            'required_ports': 'Services/www, 80',
154
            'summary': (
155
                'Detects the installed version of\n  Mantis a '
156
                'free popular web-based bugtracking system.\n'
157
                '\n  This script sends HTTP GET request and t'
158
                'ry to get the version from the\n  response, '
159
                'and sets the result in KB.'
160
            ),
161
            'timeout': '0',
162
        }
163
164
        mock_subps.check_output.return_value = (
165
            'use_mac_addr = no\ndb_address = '
166
            '/tmp/redis.sock\ndrop_privileges = no'
167
        ).encode()
168
169
        mock_redis.return_value = mock_redis
170
        mock_redis.config_get.return_value = {'databases': '513'}
171
        mock_redis.lrange.return_value = metadata
172
        mock_redis.keys.return_value = 1
173
174
        self.db.db_init()
175
176
        resp = self.nvti.get_nvt_metadata('1.2.3.4')
177
        self.assertEqual(resp, custom)
178
179
    @patch('ospd_openvas.db.subprocess')
180
    def test_get_nvt_metadata_fail(self, mock_subps, mock_redis):
181
        mock_subps.check_output.return_value = (
182
            'use_mac_addr = no\ndb_address = '
183
            '/tmp/redis.sock\ndrop_privileges = no'.encode()
184
        )
185
186
        mock_redis.return_value = mock_redis
187
        mock_redis.config_get.return_value = {'databases': '513'}
188
        mock_redis.lrange.return_value = {}
189
        mock_redis.keys.return_value = 1
190
191
        self.db.db_init()
192
193
        resp = self.nvti.get_nvt_metadata('1.2.3.4')
194
        self.assertEqual(resp, None)
195
196
    @patch('ospd_openvas.db.subprocess')
197
    def test_get_nvt_refs(self, mock_subps, mock_redis):
198
        refs = ['', '', 'URL:http://www.mantisbt.org/']
199
        out_dict = {
200
            'cve': [''],
201
            'bid': [''],
202
            'xref': ['URL:http://www.mantisbt.org/'],
203
        }
204
205
        mock_subps.check_output.return_value = (
206
            'use_mac_addr = no\ndb_address = '
207
            '/tmp/redis.sock\ndrop_privileges = no'
208
        ).encode()
209
210
        mock_redis.return_value = mock_redis
211
        mock_redis.config_get.return_value = {'databases': '513'}
212
        mock_redis.lrange.return_value = refs
213
        mock_redis.keys.return_value = 1
214
215
        self.db.db_init()
216
217
        resp = self.nvti.get_nvt_refs('1.2.3.4')
218
        self.assertEqual(resp, out_dict)
219
220
    @patch('ospd_openvas.db.subprocess')
221
    def test_get_nvt_refs_fail(self, mock_subps, mock_redis):
222
        mock_subps.check_output.return_value = (
223
            'use_mac_addr = no\ndb_address = '
224
            '/tmp/redis.sock\ndrop_privileges = no'.encode()
225
        )
226
227
        mock_redis.return_value = mock_redis
228
        mock_redis.config_get.return_value = {'databases': '513'}
229
        mock_redis.lrange.return_value = {}
230
        mock_redis.keys.return_value = 1
231
232
        self.db.db_init()
233
234
        resp = self.nvti.get_nvt_refs('1.2.3.4')
235
        self.assertEqual(resp, None)
236
237
    def test_get_nvt_prefs(self, mock_redis):
238
        prefs = ['dns-fuzz.timelimit|||entry|||default']
239
        mock_redis.lrange.return_value = prefs
240
        mock_redis.return_value = mock_redis
241
        resp = self.nvti.get_nvt_prefs(mock_redis(), '1.2.3.4')
242
        self.assertEqual(resp, prefs)
243
244
    def test_get_nvt_timeout(self, mock_redis):
245
        mock_redis.lindex.return_value = '300'
246
        mock_redis.return_value = mock_redis
247
        resp = self.nvti.get_nvt_timeout(mock_redis(), '1.2.3.4')
248
        self.assertEqual(resp, '300')
249
250
    def test_get_nvt_tag(self, mock_redis):
251
        tag = (
252
            'last_modification=1533906565'
253
            '|creation_date=1517443741|cvss_bas'
254
            'e_vector=AV:N/AC:L/Au:N/C:P/I:P/A:P|solution_type=V'
255
            'endorFix|qod_type=package|affected=rubygems on Debi'
256
            'an Linux|solution_method=DebianAPTUpgrade'
257
        )
258
259
        out_dict = {
260
            'last_modification': '1533906565',
261
            'creation_date': '1517443741',
262
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:P/I:P/A:P',
263
            'solution_type': 'VendorFix',
264
            'qod_type': 'package',
265
            'affected': 'rubygems on Debian Linux',
266
            'solution_method': 'DebianAPTUpgrade',
267
        }
268
269
        mock_redis.lindex.return_value = tag
270
        mock_redis.return_value = mock_redis
271
272
        resp = self.nvti.get_nvt_tag(mock_redis(), '1.2.3.4')
273
274
        self.assertEqual(out_dict, resp)
275
276
    @patch('ospd_openvas.nvticache.subprocess')
277
    def test_set_nvticache_str(self, mock_subps, mock_redis):
278
        self.assertIsNone(self.nvti.NVTICACHE_STR)
279
280
        mock_subps.check_output.return_value = '20.10\n'.encode()
281
        self.nvti.set_nvticache_str()
282
        self.assertEqual(self.nvti.NVTICACHE_STR, 'nvticache20.10')
283
284
        mock_subps.check_output.return_value = '11.0.1\n'.encode()
285
        with self.assertRaises(SystemExit):
286
            self.nvti.set_nvticache_str()
287