Completed
Push — master ( 83c525...50e9f2 )
by Juan José
20s queued 12s
created

VtHelperTestCase.test_get_single_vt_severity_cvssv2()   B

Complexity

Conditions 1

Size

Total Lines 57
Code Lines 48

Duplication

Lines 57
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 48
nop 1
dl 57
loc 57
rs 8.7018
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
import logging
21
22
from hashlib import sha256
23
from unittest import TestCase
24
25
from tests.dummydaemon import DummyDaemon
26
from tests.helper import assert_called_once
27
28
from ospd_openvas.vthelper import VtHelper
29
30
31
class VtHelperTestCase(TestCase):
32
    def test_get_single_vt(self):
33
        w = DummyDaemon()
34
        vthelper = VtHelper(w.nvti)
35
        res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
36
37
        assert_called_once(w.nvti.get_nvt_metadata)
38
        self.assertEqual("Mantis Detection", res.get('name'))
39
40
    def test_calculate_vts_collection_hash_no_params(self):
41
        w = DummyDaemon()
42
        vthelper = VtHelper(w.nvti)
43
        hash_out = vthelper.calculate_vts_collection_hash()
44
45
        vt_hash_str = (
46
            '1.3.6.1.4.1.25623.1.0.10006115339065651Data '
47
            + 'length :2Do not randomize the  order  in  which '
48
            + 'ports are scannedno'
49
        )
50
51
        vt_hash = sha256()
52
        vt_hash.update(vt_hash_str.encode('utf-8'))
53
        hash_test = vt_hash.hexdigest()
54
55
        self.assertEqual(hash_test, hash_out)
56
57
    def test_get_vt_iterator(self):
58
        w = DummyDaemon()
59
        vthelper = VtHelper(w.nvti)
60
61
        vt = ["1.3.6.1.4.1.25623.1.0.100061"]
62
63
        for key, _ in vthelper.get_vt_iterator():
64
            self.assertIn(key, vt)
65
66
    def test_get_vt_iterator_with_filter(self):
67
        w = DummyDaemon()
68
        vthelper = VtHelper(w.nvti)
69
70
        vt = ["1.3.6.1.4.1.25623.1.0.100061"]
71
72
        vtout = w.VTS["1.3.6.1.4.1.25623.1.0.100061"]
73
74
        for key, vt_dict in vthelper.get_vt_iterator(vt_selection=vt):
75
            self.assertIn(key, vt)
76
            for key in vtout:
77
                self.assertIn(key, vt_dict)
78
79
    def test_get_vt_iterator_with_filter_no_vt(self):
80
        w = DummyDaemon()
81
        vthelper = VtHelper(w.nvti)
82
        w.nvti.get_nvt_metadata.return_value = None
83
        vt = ["1.3.6.1.4.1.25623.1.0.100065"]
84
85
        for _, values in vthelper.get_vt_iterator(vt_selection=vt):
86
            self.assertIs(values, None)
87
88
    def test_get_vt_iterator_with_filter_no_vt(self):
89
        w = DummyDaemon()
90
        vthelper = VtHelper(w.nvti)
91
        w.nvti.get_nvt_metadata.return_value = None
92
        vt = ["1.3.6.1.4.1.25623.1.0.100065"]
93
94
        for _, values in vthelper.get_vt_iterator(vt_selection=vt):
95
            self.assertIs(values, None)
96
97 View Code Duplication
    def test_get_single_vt_severity_cvssv3(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
        w = DummyDaemon()
99
        w.nvti.get_nvt_metadata.return_value = {
100
            'category': '3',
101
            'creation_date': '1237458156',
102
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
103
            'severity_vector': 'CVSS:3.0/AV:L/AC:H/PR:H/UI:R/S:U/C:N/I:L/A:L',
104
            'severity_date': '1237458156',
105
            'severity_origin': 'Greenbone',
106
            'excluded_keys': 'Settings/disable_cgi_scanning',
107
            'family': 'Product detection',
108
            'filename': 'mantis_detect.nasl',
109
            'last_modification': ('1533906565'),
110
            'name': 'Mantis Detection',
111
            'qod_type': 'remote_banner',
112
            'required_ports': 'Services/www, 80',
113
            'solution': 'some solution',
114
            'solution_type': 'WillNotFix',
115
            'solution_method': 'DebianAPTUpgrade',
116
            'impact': 'some impact',
117
            'insight': 'some insight',
118
            'summary': ('some summary'),
119
            'affected': 'some affection',
120
            'timeout': '0',
121
            'vt_params': {
122
                '1': {
123
                    'id': '1',
124
                    'default': '',
125
                    'description': 'Description',
126
                    'name': 'Data length :',
127
                    'type': 'entry',
128
                },
129
                '2': {
130
                    'id': '2',
131
                    'default': 'no',
132
                    'description': 'Description',
133
                    'name': 'Do not randomize the  order  in  which ports are scanned',  # pylint: disable=line-too-long
134
                    'type': 'checkbox',
135
                },
136
            },
137
            'refs': {
138
                'bid': [''],
139
                'cve': [''],
140
                'xref': ['URL:http://www.mantisbt.org/'],
141
            },
142
        }
143
144
        vthelper = VtHelper(w.nvti)
145
146
        res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
147
        assert_called_once(w.nvti.get_nvt_metadata)
148
149
        severities = res.get('severities')
150
        self.assertEqual(
151
            "CVSS:3.0/AV:L/AC:H/PR:H/UI:R/S:U/C:N/I:L/A:L",
152
            severities.get('severity_base_vector'),
153
        )
154
        self.assertEqual("cvss_base_v3", severities.get('severity_type'))
155
        self.assertEqual("Greenbone", severities.get('severity_origin'))
156
        self.assertEqual("1237458156", severities.get('severity_date'))
157
158 View Code Duplication
    def test_get_single_vt_severity_cvssv2(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
        w = DummyDaemon()
160
        w.nvti.get_nvt_metadata.return_value = {
161
            'category': '3',
162
            'creation_date': '1237458156',
163
            'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
164
            'excluded_keys': 'Settings/disable_cgi_scanning',
165
            'family': 'Product detection',
166
            'filename': 'mantis_detect.nasl',
167
            'last_modification': ('1533906565'),
168
            'name': 'Mantis Detection',
169
            'qod_type': 'remote_banner',
170
            'required_ports': 'Services/www, 80',
171
            'solution': 'some solution',
172
            'solution_type': 'WillNotFix',
173
            'solution_method': 'DebianAPTUpgrade',
174
            'impact': 'some impact',
175
            'insight': 'some insight',
176
            'summary': ('some summary'),
177
            'affected': 'some affection',
178
            'timeout': '0',
179
            'vt_params': {
180
                '1': {
181
                    'id': '1',
182
                    'default': '',
183
                    'description': 'Description',
184
                    'name': 'Data length :',
185
                    'type': 'entry',
186
                },
187
                '2': {
188
                    'id': '2',
189
                    'default': 'no',
190
                    'description': 'Description',
191
                    'name': 'Do not randomize the  order  in  which ports are scanned',  # pylint: disable=line-too-long
192
                    'type': 'checkbox',
193
                },
194
            },
195
            'refs': {
196
                'bid': [''],
197
                'cve': [''],
198
                'xref': ['URL:http://www.mantisbt.org/'],
199
            },
200
        }
201
202
        vthelper = VtHelper(w.nvti)
203
204
        res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
205
        assert_called_once(w.nvti.get_nvt_metadata)
206
207
        severities = res.get('severities')
208
        self.assertEqual(
209
            "AV:N/AC:L/Au:N/C:N/I:N/A:N",
210
            severities.get('severity_base_vector'),
211
        )
212
        self.assertEqual("cvss_base_v2", severities.get('severity_type'))
213
        self.assertEqual(None, severities.get('severity_origin'))
214
        self.assertEqual("1237458156", severities.get('severity_date'))
215