Completed
Push — master ( 489d84...6ebf24 )
by Juan José
13s
created

TestNVTICache.test_get_nvt_params()   B

Complexity

Conditions 5

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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