Passed
Pull Request — master (#84)
by
unknown
01:55
created

TestNVTICache.test_get_nvt_timeout()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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