Completed
Push — master ( 494987...e33655 )
by Juan José
23s queued 10s
created

tests.helper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 117
dl 0
loc 193
rs 10
c 0
b 0
f 0
wmc 24

16 Methods

Rating   Name   Duplication   Size   Complexity  
A DummyWrapper.get_summary_vt_as_xml_str() 0 5 1
A DummyWrapper.get_refs_vt_as_xml_str() 0 7 1
A DummyWrapper.get_insight_vt_as_xml_str() 0 5 1
A DummyWrapper.get_dependencies_vt_as_xml_str() 0 10 1
A DummyWrapper.__init__() 0 4 1
A DummyWrapper.get_detection_vt_as_xml_str() 0 7 1
A DummyWrapper.get_creation_time_vt_as_xml_str() 0 7 1
A DummyWrapper.get_affected_vt_as_xml_str() 0 5 1
A DummyWrapper.get_severities_vt_as_xml_str() 0 9 1
A DummyWrapper.get_impact_vt_as_xml_str() 0 5 1
A DummyWrapper.get_params_vt_as_xml_str() 0 4 1
A DummyWrapper.get_modification_time_vt_as_xml_str() 0 9 1
A DummyWrapper.check() 0 2 1
A DummyWrapper.get_custom_vt_as_xml_str() 0 3 1
A DummyWrapper.get_solution_vt_as_xml_str() 0 7 1
B DummyWrapper.exec_scan() 0 43 6

1 Function

Rating   Name   Duplication   Size   Complexity  
A assert_called() 0 11 3
1
# Copyright (C) 2020 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
import time
20
21
from unittest.mock import Mock
22
23
from ospd.ospd import OSPDaemon
24
25
26
def assert_called(mock: Mock):
27
    if hasattr(mock, 'assert_called'):
28
        return mock.assert_called()
29
30
    if not mock.call_count == 1:
31
        msg = "Expected '%s' to have been called once. Called %s times.%s" % (
32
            mock._mock_name or 'mock',  # pylint: disable=protected-access
33
            mock.call_count,
34
            mock._calls_repr(),  # pylint: disable=protected-access
35
        )
36
        raise AssertionError(msg)
37
38
39
class DummyWrapper(OSPDaemon):
40
    def __init__(self, results, checkresult=True):
41
        super().__init__()
42
        self.checkresult = checkresult
43
        self.results = results
44
45
    def check(self):
46
        return self.checkresult
47
48
    @staticmethod
49
    def get_custom_vt_as_xml_str(vt_id, custom):
50
        return '<custom><mytest>static test</mytest></custom>'
51
52
    @staticmethod
53
    def get_params_vt_as_xml_str(vt_id, vt_params):
54
        return (
55
            '<params><param id="abc" type="string">'
56
            '<name>ABC</name><description>Test ABC</description>'
57
            '<default>yes</default></param>'
58
            '<param id="def" type="string">'
59
            '<name>DEF</name><description>Test DEF</description>'
60
            '<default>no</default></param></params>'
61
        )
62
63
    @staticmethod
64
    def get_refs_vt_as_xml_str(vt_id, vt_refs):
65
        response = (
66
            '<refs><ref type="cve" id="CVE-2010-4480"/>'
67
            '<ref type="url" id="http://example.com"/></refs>'
68
        )
69
        return response
70
71
    @staticmethod
72
    def get_dependencies_vt_as_xml_str(vt_id, vt_dependencies):
73
        response = (
74
            '<dependencies>'
75
            '<dependency vt_id="1.3.6.1.4.1.25623.1.0.50282" />'
76
            '<dependency vt_id="1.3.6.1.4.1.25623.1.0.50283" />'
77
            '</dependencies>'
78
        )
79
80
        return response
81
82
    @staticmethod
83
    def get_severities_vt_as_xml_str(vt_id, severities):
84
        response = (
85
            '<severities><severity cvss_base="5.0" cvss_'
86
            'type="cvss_base_v2">AV:N/AC:L/Au:N/C:N/I:N/'
87
            'A:P</severity></severities>'
88
        )
89
90
        return response
91
92
    @staticmethod
93
    def get_detection_vt_as_xml_str(
94
        vt_id, detection=None, qod_type=None, qod=None
95
    ):
96
        response = '<detection qod_type="package">some detection</detection>'
97
98
        return response
99
100
    @staticmethod
101
    def get_summary_vt_as_xml_str(vt_id, summary):
102
        response = '<summary>Some summary</summary>'
103
104
        return response
105
106
    @staticmethod
107
    def get_affected_vt_as_xml_str(vt_id, affected):
108
        response = '<affected>Some affected</affected>'
109
110
        return response
111
112
    @staticmethod
113
    def get_impact_vt_as_xml_str(vt_id, impact):
114
        response = '<impact>Some impact</impact>'
115
116
        return response
117
118
    @staticmethod
119
    def get_insight_vt_as_xml_str(vt_id, insight):
120
        response = '<insight>Some insight</insight>'
121
122
        return response
123
124
    @staticmethod
125
    def get_solution_vt_as_xml_str(
126
        vt_id, solution, solution_type=None, solution_method=None
127
    ):
128
        response = '<solution>Some solution</solution>'
129
130
        return response
131
132
    @staticmethod
133
    def get_creation_time_vt_as_xml_str(
134
        vt_id, creation_time
135
    ):  # pylint: disable=arguments-differ
136
        response = '<creation_time>%s</creation_time>' % creation_time
137
138
        return response
139
140
    @staticmethod
141
    def get_modification_time_vt_as_xml_str(
142
        vt_id, modification_time
143
    ):  # pylint: disable=arguments-differ
144
        response = (
145
            '<modification_time>%s</modification_time>' % modification_time
146
        )
147
148
        return response
149
150
    def exec_scan(self, scan_id, target):
151
        time.sleep(0.01)
152
        for res in self.results:
153
            if res.result_type == 'log':
154
                self.add_scan_log(
155
                    scan_id,
156
                    res.host or target,
157
                    res.hostname,
158
                    res.name,
159
                    res.value,
160
                    res.port,
161
                )
162
            if res.result_type == 'error':
163
                self.add_scan_error(
164
                    scan_id,
165
                    res.host or target,
166
                    res.hostname,
167
                    res.name,
168
                    res.value,
169
                    res.port,
170
                )
171
            elif res.result_type == 'host-detail':
172
                self.add_scan_host_detail(
173
                    scan_id,
174
                    res.host or target,
175
                    res.hostname,
176
                    res.name,
177
                    res.value,
178
                )
179
            elif res.result_type == 'alarm':
180
                self.add_scan_alarm(
181
                    scan_id,
182
                    res.host or target,
183
                    res.hostname,
184
                    res.name,
185
                    res.value,
186
                    res.port,
187
                    res.test_id,
188
                    res.severity,
189
                    res.qod,
190
                )
191
            else:
192
                raise ValueError(res.result_type)
193