Passed
Pull Request — master (#419)
by
unknown
01:24
created

tests.test_preferencehandler   F

Complexity

Total Complexity 67

Size/Duplication

Total Lines 1226
Duplicated Lines 19.49 %

Importance

Changes 0
Metric Value
eloc 885
dl 239
loc 1226
rs 2.755
c 0
b 0
f 0
wmc 67

43 Methods

Rating   Name   Duplication   Size   Complexity  
A PreferenceHandlerTestCase.test_set_main_kbindex() 0 12 1
A PreferenceHandlerTestCase.test_process_vts_not_found() 0 15 1
A PreferenceHandlerTestCase.test_process_vts_bad_param_id() 0 13 1
A PreferenceHandlerTestCase.test_process_vts() 0 16 1
A PreferenceHandlerTestCase.test_set_plugins_true() 0 17 1
A PreferenceHandlerTestCase.test_set_plugins_false() 0 12 1
A PreferenceHandlerTestCase.test_prepare_alive_test_no_enum_no_alive_test() 0 17 2
A PreferenceHandlerTestCase.test_snmp_no_priv_alg_but_pw_credentials() 28 28 1
A PreferenceHandlerTestCase.test_prepare_nvt_prefs_no_prefs() 0 10 1
A PreferenceHandlerTestCase.test_set_ports() 0 14 1
A PreferenceHandlerTestCase.test_set_alive_no_setting() 16 16 2
B PreferenceHandlerTestCase.test_build_credentials() 0 60 1
A PreferenceHandlerTestCase.test_set_alive_pinghost() 0 28 3
A PreferenceHandlerTestCase.test_snmp_missing_auth_alg_credentials() 26 26 1
A PreferenceHandlerTestCase.test_prepare_nvt_prefs() 0 18 1
A PreferenceHandlerTestCase.test_missing_ssh_port_credentials() 0 20 1
A PreferenceHandlerTestCase.test_set_host_options_none() 0 14 1
A PreferenceHandlerTestCase.test_build_alive_test_opt() 0 24 1
A PreferenceHandlerTestCase.test_build_credentials_ssh_up() 0 23 1
A PreferenceHandlerTestCase.test_snmp_unknown_auth_alg_credentials() 28 28 1
C PreferenceHandlerTestCase.test_set_boreas_alive_test_with_settings() 0 90 7
A PreferenceHandlerTestCase.test_missing_type_for_ssh_credentials() 26 26 1
A PreferenceHandlerTestCase.test_set_reverse_lookup_opt() 0 17 1
A PreferenceHandlerTestCase.test_ssh_port_out_of_range_credentials() 23 23 1
A PreferenceHandlerTestCase.test_set_target() 0 14 1
B PreferenceHandlerTestCase.test_alive_test_methods_to_bit_field() 0 73 1
A PreferenceHandlerTestCase.test_set_scan_params() 0 25 1
A PreferenceHandlerTestCase.test_set_ports_invalid() 0 10 1
A PreferenceHandlerTestCase.test_build_alive_test_opt_empty() 0 16 1
A PreferenceHandlerTestCase.test_set_boreas_alive_test_enum_has_precedence() 0 19 2
A PreferenceHandlerTestCase.test_set_alive_no_invalid_alive_test_no_enum() 0 17 2
A PreferenceHandlerTestCase.test_set_alive_no_invalid_alive_test() 17 17 2
A PreferenceHandlerTestCase.test_set_credentials() 0 39 1
A PreferenceHandlerTestCase.test_set_credentials_empty() 0 14 1
A PreferenceHandlerTestCase.test_set_bad_service_credentials() 24 24 1
D PreferenceHandlerTestCase.test_set_boreas_alive_test_not_as_enum() 0 140 10
A PreferenceHandlerTestCase.test_build_alive_test_opt_fail_1() 0 10 1
A PreferenceHandlerTestCase.test_prepare_alive_test_not_supplied_as_enum() 0 26 2
A PreferenceHandlerTestCase.test_snmp_unknown_priv_alg_credentials() 0 30 1
A PreferenceHandlerTestCase.test_set_bad_ssh_port_credentials() 23 23 1
A PreferenceHandlerTestCase.test_set_boreas_alive_test_without_settings() 0 13 2
A PreferenceHandlerTestCase.test_set_host_options() 0 16 1
A PreferenceHandlerTestCase.test_bad_type_for_ssh_credentials() 28 28 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like tests.test_preferencehandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2021 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 unittest import TestCase
23
from unittest.mock import call, patch, Mock, MagicMock
24
from collections import OrderedDict
25
26
from ospd.vts import Vts
27
28
from tests.dummydaemon import DummyDaemon
29
from tests.helper import assert_called_once
30
31
import ospd_openvas.db
32
33
from ospd_openvas.openvas import Openvas
34
from ospd_openvas.preferencehandler import (
35
    AliveTest,
36
    BOREAS_SETTING_NAME,
37
    BOREAS_ALIVE_TEST,
38
    BOREAS_ALIVE_TEST_PORTS,
39
    PreferenceHandler,
40
    alive_test_methods_to_bit_field,
41
)
42
43
44
class PreferenceHandlerTestCase(TestCase):
45
    @patch('ospd_openvas.db.KbDB')
46
    def test_process_vts_not_found(self, mock_kb):
47
        w = DummyDaemon()
48
        logging.Logger.warning = Mock()
49
50
        vts = {
51
            '1.3.6.1.4.1.25623.1.0.100065': {'3': 'new value'},
52
            'vt_groups': ['family=debian', 'family=general'],
53
        }
54
55
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti)
56
        w.nvti.get_nvt_metadata.return_value = None
57
        p._process_vts(vts)
58
59
        assert_called_once(logging.Logger.warning)
60
61
    def test_process_vts_bad_param_id(self):
62
        w = DummyDaemon()
63
64
        vts = {
65
            '1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'},
66
            'vt_groups': ['family=debian', 'family=general'],
67
        }
68
69
        p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti)
70
71
        ret = p._process_vts(vts)
72
73
        self.assertFalse(ret[1])
74
75
    def test_process_vts(self):
76
        w = DummyDaemon()
77
78
        vts = {
79
            '1.3.6.1.4.1.25623.1.0.100061': {'1': 'new value'},
80
            'vt_groups': ['family=debian', 'family=general'],
81
        }
82
        vt_out = (
83
            ['1.3.6.1.4.1.25623.1.0.100061'],
84
            {'1.3.6.1.4.1.25623.1.0.100061:1:entry:Data length :': 'new value'},
85
        )
86
87
        p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti)
88
        ret = p._process_vts(vts)
89
90
        self.assertEqual(ret, vt_out)
91
92
    @patch('ospd_openvas.db.KbDB')
93
    def test_set_plugins_false(self, mock_kb):
94
        w = DummyDaemon()
95
96
        w.scan_collection.get_vts = Mock()
97
        w.scan_collection.get_vts.return_value = {}
98
99
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti)
100
        p.kbdb.add_scan_preferences = Mock()
101
        r = p.prepare_plugins_for_openvas()
102
103
        self.assertFalse(r)
104
105
    @patch('ospd_openvas.db.KbDB')
106
    def test_set_plugins_true(self, mock_kb):
107
        w = DummyDaemon()
108
109
        vts = {
110
            '1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'},
111
            'vt_groups': ['family=debian', 'family=general'],
112
        }
113
114
        w.scan_collection.get_vts = Mock()
115
        w.scan_collection.get_vts.return_value = vts
116
117
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti)
118
        p.kbdb.add_scan_preferences = Mock()
119
        r = p.prepare_plugins_for_openvas()
120
121
        self.assertTrue(r)
122
123
    def test_build_credentials_ssh_up(self):
124
        w = DummyDaemon()
125
126
        cred_out = [
127
            'auth_port_ssh|||22',
128
            '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username',
129
            '1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass',
130
            '1.3.6.1.4.1.25623.1.0.103591:7:entry:SSH privilege login name:|||',
131
            '1.3.6.1.4.1.25623.1.0.103591:8:password:SSH privilege password:|||',
132
        ]
133
        cred_dict = {
134
            'ssh': {
135
                'type': 'up',
136
                'port': '22',
137
                'username': 'username',
138
                'password': 'pass',
139
            }
140
        }
141
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
142
143
        ret = p.build_credentials_as_prefs(cred_dict)
144
145
        self.assertCountEqual(ret, cred_out)
146
147
    def test_build_credentials(self):
148
        w = DummyDaemon()
149
150
        cred_out = [
151
            '1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username',
152
            '1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:|||pass',
153
            'auth_port_ssh|||22',
154
            '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username',
155
            '1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:|||pass',
156
            '1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||',
157
            '1.3.6.1.4.1.25623.1.0.103591:7:entry:SSH privilege login name:|||',
158
            '1.3.6.1.4.1.25623.1.0.103591:8:password:SSH privilege password:|||',
159
            '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username',
160
            '1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass',
161
            '1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:some comunity',
162
            '1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username',
163
            '1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass',
164
            '1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:some auth algo',
165
            '1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:privacy pass',
166
            '1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:privacy algo',
167
        ]
168
        cred_dict = {
169
            'ssh': {
170
                'type': 'usk',
171
                'port': '22',
172
                'username': 'username',
173
                'password': 'pass',
174
                'private': 'some key',
175
                'priv_username': 'su_user',
176
                'priv_password': 'su_pass',
177
            },
178
            'smb': {'type': 'up', 'username': 'username', 'password': 'pass'},
179
            'esxi': {
180
                'type': 'up',
181
                'username': 'username',
182
                'password': 'pass',
183
            },
184
            'snmp': {
185
                'type': 'snmp',
186
                'username': 'username',
187
                'password': 'pass',
188
                'community': 'some comunity',
189
                'auth_algorithm': 'md5',
190
                'privacy_password': 'privacy pass',
191
                'privacy_algorithm': 'aes',
192
            },
193
        }
194
195
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
196
        ret = p.build_credentials_as_prefs(cred_dict)
197
198
        self.assertEqual(len(ret), len(cred_out))
199
        self.assertIn('auth_port_ssh|||22', ret)
200
        self.assertIn(
201
            '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username',
202
            ret,
203
        )
204
        self.assertIn(
205
            '1.3.6.1.4.1.25623.1.0.103591:8:password:SSH privilege password:|||su_pass',
206
            ret,
207
        )
208
209
    def test_build_alive_test_opt_empty(self):
210
        w = DummyDaemon()
211
212
        target_options_dict = {'alive_test': '0'}
213
214
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
215
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)
216
217
        self.assertEqual(ret, {})
218
219
        # alive test was supplied via separate xml element
220
        w = DummyDaemon()
221
        target_options_dict = {'alive_test_methods': '1', 'icmp': '0'}
222
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
223
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)
224
        self.assertEqual(ret, {})
225
226
    def test_build_alive_test_opt(self):
227
        w = DummyDaemon()
228
229
        alive_test_out = {
230
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no",
231
            "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no",
232
            "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no",
233
            "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes",
234
            "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no",
235
            "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes",
236
        }
237
238
        target_options_dict = {'alive_test': '2'}
239
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
240
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)
241
242
        self.assertEqual(ret, alive_test_out)
243
244
        # alive test was supplied via sepertae xml element
245
        w = DummyDaemon()
246
        target_options_dict = {'alive_test_methods': '1', 'icmp': '1'}
247
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
248
        ret = p.build_alive_test_opt_as_prefs(target_options_dict)
249
        self.assertEqual(ret, alive_test_out)
250
251
    def test_build_alive_test_opt_fail_1(self):
252
        w = DummyDaemon()
253
        logging.Logger.debug = Mock()
254
255
        target_options_dict = {'alive_test': 'a'}
256
        p = PreferenceHandler('1234-1234', None, w.scan_collection, None)
257
        target_options = p.build_alive_test_opt_as_prefs(target_options_dict)
258
259
        assert_called_once(logging.Logger.debug)
260
        self.assertEqual(len(target_options), 0)
261
262
    @patch('ospd_openvas.db.KbDB')
263
    def test_set_target(self, mock_kb):
264
        w = DummyDaemon()
265
266
        w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1')
267
268
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
269
        p.scan_id = '456-789'
270
        p.kbdb.add_scan_preferences = MagicMock()
271
        p.prepare_target_for_openvas()
272
273
        p.kbdb.add_scan_preferences.assert_called_with(
274
            p.scan_id,
275
            ['TARGET|||192.168.0.1'],
276
        )
277
278
    @patch('ospd_openvas.db.KbDB')
279
    def test_set_ports(self, mock_kb):
280
        w = DummyDaemon()
281
282
        w.scan_collection.get_ports = MagicMock(return_value='80,443')
283
284
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
285
        p.scan_id = '456-789'
286
        p.kbdb.add_scan_preferences = MagicMock()
287
        p.prepare_ports_for_openvas()
288
289
        p.kbdb.add_scan_preferences.assert_called_with(
290
            p.scan_id,
291
            ['port_range|||80,443'],
292
        )
293
294
    @patch('ospd_openvas.db.KbDB')
295
    def test_set_ports_invalid(self, mock_kb):
296
        w = DummyDaemon()
297
298
        w.scan_collection.get_ports = MagicMock(return_value='2,-9,4')
299
300
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
301
        p.scan_id = '456-789'
302
        p.kbdb.add_scan_preferences = MagicMock()
303
        self.assertFalse(p.prepare_ports_for_openvas())
304
305
    @patch('ospd_openvas.db.KbDB')
306
    def test_set_main_kbindex(self, mock_kb):
307
        w = DummyDaemon()
308
309
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
310
        p.kbdb.add_scan_preferences = Mock()
311
        p.kbdb.index = 2
312
        p.prepare_main_kbindex_for_openvas()
313
314
        p.kbdb.add_scan_preferences.assert_called_with(
315
            p.scan_id,
316
            ['ov_maindbid|||2'],
317
        )
318
319
    @patch('ospd_openvas.db.KbDB')
320
    def test_set_credentials(self, mock_kb):
321
        w = DummyDaemon()
322
323
        creds = {
324
            'ssh': {
325
                'type': 'up',
326
                'port': '22',
327
                'username': 'username',
328
                'password': 'pass',
329
                'priv_username': "privuser",
330
                'priv_password': "privpass",
331
            },
332
            'smb': {'type': 'up', 'username': 'username', 'password': 'pass'},
333
            'esxi': {
334
                'type': 'up',
335
                'username': 'username',
336
                'password': 'pass',
337
            },
338
            'snmp': {
339
                'type': 'snmp',
340
                'username': 'username',
341
                'password': 'pass',
342
                'community': 'some comunity',
343
                'auth_algorithm': 'md5',
344
                'privacy_password': 'privacy pass',
345
                'privacy_algorithm': 'aes',
346
            },
347
        }
348
349
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
350
351
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
352
        p.scan_id = '456-789'
353
        p.kbdb.add_credentials_to_scan_preferences = MagicMock()
354
        r = p.prepare_credentials_for_openvas()
355
356
        self.assertTrue(r)
357
        assert_called_once(p.kbdb.add_credentials_to_scan_preferences)
358
359 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
360
    def test_set_bad_service_credentials(self, mock_kb):
361
        w = DummyDaemon()
362
363
        # bad cred type shh instead of ssh
364
        creds = {
365
            'shh': {
366
                'type': 'up',
367
                'port': '22',
368
                'username': 'username',
369
                'password': 'pass',
370
            },
371
        }
372
373
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
374
375
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
376
        p.scan_id = '456-789'
377
        p.kbdb.add_scan_preferences = MagicMock()
378
        r = p.prepare_credentials_for_openvas()
379
        e = p.get_error_messages()
380
381
        self.assertFalse(r)
382
        self.assertIn("Unknown service type for credential: shh", e)
383
384 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
385
    def test_set_bad_ssh_port_credentials(self, mock_kb):
386
        w = DummyDaemon()
387
388
        creds = {
389
            'ssh': {
390
                'type': 'up',
391
                'port': 'ab',
392
                'username': 'username',
393
                'password': 'pass',
394
            },
395
        }
396
397
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
398
399
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
400
        p.scan_id = '456-789'
401
        p.kbdb.add_scan_preferences = MagicMock()
402
        r = p.prepare_credentials_for_openvas()
403
        e = p.get_error_messages()
404
405
        self.assertFalse(r)
406
        self.assertIn("Port for SSH 'ab' is not a valid number.", e)
407
408
    @patch('ospd_openvas.db.KbDB')
409
    def test_missing_ssh_port_credentials(self, mock_kb):
410
        w = DummyDaemon()
411
412
        creds = {
413
            'ssh': {
414
                'type': 'up',
415
                'username': 'username',
416
                'password': 'pass',
417
            },
418
        }
419
420
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
421
422
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
423
        p.scan_id = '456-789'
424
        p.kbdb.add_scan_preferences = MagicMock()
425
        r = p.prepare_credentials_for_openvas()
426
427
        self.assertTrue(r)
428
429 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
430
    def test_ssh_port_out_of_range_credentials(self, mock_kb):
431
        w = DummyDaemon()
432
433
        creds = {
434
            'ssh': {
435
                'type': 'up',
436
                'port': '65536',
437
                'username': 'username',
438
                'password': 'pass',
439
            },
440
        }
441
442
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
443
444
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
445
        p.scan_id = '456-789'
446
        p.kbdb.add_scan_preferences = MagicMock()
447
        r = p.prepare_credentials_for_openvas()
448
        e = p.get_error_messages()
449
450
        self.assertFalse(r)
451
        self.assertIn("Port for SSH is out of range (1-65535): 65536", e)
452
453 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
454
    def test_bad_type_for_ssh_credentials(self, mock_kb):
455
        w = DummyDaemon()
456
457
        creds = {
458
            'ssh': {
459
                'type': 'ups',
460
                'port': '22',
461
                'username': 'username',
462
                'password': 'pass',
463
            },
464
        }
465
466
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
467
468
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
469
        p.scan_id = '456-789'
470
        p.kbdb.add_scan_preferences = MagicMock()
471
        r = p.prepare_credentials_for_openvas()
472
        e = p.get_error_messages()
473
474
        self.assertFalse(r)
475
        self.assertIn(
476
            "Unknown Credential Type for SSH: "
477
            + "ups"
478
            + ". Use 'up' for Username + Password"
479
            + " or 'usk' for Username + SSH Key.",
480
            e,
481
        )
482
483 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
484
    def test_missing_type_for_ssh_credentials(self, mock_kb):
485
        w = DummyDaemon()
486
487
        creds = {
488
            'ssh': {
489
                'port': '22',
490
                'username': 'username',
491
                'password': 'pass',
492
            },
493
        }
494
495
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
496
497
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
498
        p.scan_id = '456-789'
499
        p.kbdb.add_scan_preferences = MagicMock()
500
        r = p.prepare_credentials_for_openvas()
501
        e = p.get_error_messages()
502
503
        self.assertFalse(r)
504
        self.assertIn(
505
            "Missing Credential Type for SSH."
506
            + " Use 'up' for Username + Password"
507
            + " or 'usk' for Username + SSH Key.",
508
            e,
509
        )
510
511 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
512
    def test_snmp_no_priv_alg_but_pw_credentials(self, mock_kb):
513
        w = DummyDaemon()
514
515
        creds = {
516
            'snmp': {
517
                'type': 'snmp',
518
                'username': 'username',
519
                'password': 'pass',
520
                'community': 'some comunity',
521
                'auth_algorithm': 'sha1',
522
                'privacy_password': 'privacy pass',
523
            },
524
        }
525
526
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
527
528
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
529
        p.scan_id = '456-789'
530
        p.kbdb.add_scan_preferences = MagicMock()
531
        r = p.prepare_credentials_for_openvas()
532
        e = p.get_error_messages()
533
534
        self.assertFalse(r)
535
        self.assertIn(
536
            "When no privacy algorithm is used, the privacy"
537
            + " password also has to be empty.",
538
            e,
539
        )
540
541
    @patch('ospd_openvas.db.KbDB')
542
    def test_snmp_unknown_priv_alg_credentials(self, mock_kb):
543
        w = DummyDaemon()
544
545
        creds = {
546
            'snmp': {
547
                'type': 'snmp',
548
                'username': 'username',
549
                'password': 'pass',
550
                'community': 'some comunity',
551
                'auth_algorithm': 'sha1',
552
                'privacy_password': 'privacy pass',
553
                'privacy_algorithm': 'das',
554
            },
555
        }
556
557
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
558
559
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
560
        p.scan_id = '456-789'
561
        p.kbdb.add_scan_preferences = MagicMock()
562
        r = p.prepare_credentials_for_openvas()
563
        e = p.get_error_messages()
564
565
        self.assertFalse(r)
566
        self.assertIn(
567
            "Unknows privacy algorithm used: "
568
            + "das"
569
            + ". Use 'aes', 'des' or '' (none).",
570
            e,
571
        )
572
573 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
574
    def test_snmp_missing_auth_alg_credentials(self, mock_kb):
575
        w = DummyDaemon()
576
577
        creds = {
578
            'snmp': {
579
                'type': 'snmp',
580
                'username': 'username',
581
                'password': 'pass',
582
                'community': 'some comunity',
583
            },
584
        }
585
586
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
587
588
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
589
        p.scan_id = '456-789'
590
        p.kbdb.add_scan_preferences = MagicMock()
591
        r = p.prepare_credentials_for_openvas()
592
        e = p.get_error_messages()
593
594
        self.assertFalse(r)
595
        self.assertIn(
596
            "Missing authentification algorithm for SNMP."
597
            + " Use 'md5' or 'sha1'.",
598
            e,
599
        )
600
601 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
602
    def test_snmp_unknown_auth_alg_credentials(self, mock_kb):
603
        w = DummyDaemon()
604
605
        creds = {
606
            'snmp': {
607
                'type': 'snmp',
608
                'username': 'username',
609
                'password': 'pass',
610
                'community': 'some comunity',
611
                'auth_algorithm': 'sha2',
612
            },
613
        }
614
615
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
616
617
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
618
        p.scan_id = '456-789'
619
        p.kbdb.add_scan_preferences = MagicMock()
620
        r = p.prepare_credentials_for_openvas()
621
        e = p.get_error_messages()
622
623
        self.assertFalse(r)
624
        self.assertIn(
625
            "Unknown authentification algorithm: "
626
            + "sha2"
627
            + ". Use 'md5' or 'sha1'.",
628
            e,
629
        )
630
631
    @patch('ospd_openvas.db.KbDB')
632
    def test_set_credentials_empty(self, mock_kb):
633
        w = DummyDaemon()
634
635
        creds = {}
636
637
        w.scan_collection.get_credentials = MagicMock(return_value=creds)
638
639
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
640
        p.scan_id = '456-789'
641
        p.kbdb.add_scan_preferences = MagicMock()
642
        r = p.prepare_credentials_for_openvas()
643
644
        self.assertTrue(r)
645
646
    @patch('ospd_openvas.db.KbDB')
647
    def test_set_host_options(self, mock_kb):
648
        w = DummyDaemon()
649
650
        exc = '192.168.0.1'
651
652
        w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc)
653
654
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
655
        p.scan_id = '456-789'
656
        p.kbdb.add_scan_preferences = MagicMock()
657
        p.prepare_host_options_for_openvas()
658
659
        p.kbdb.add_scan_preferences.assert_called_with(
660
            p.scan_id,
661
            ['exclude_hosts|||192.168.0.1'],
662
        )
663
664
    @patch('ospd_openvas.db.KbDB')
665
    def test_set_host_options_none(self, mock_kb):
666
        w = DummyDaemon()
667
668
        exc = ''
669
670
        w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc)
671
672
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
673
        p.scan_id = '456-789'
674
        p.kbdb.add_scan_preferences = MagicMock()
675
        p.prepare_host_options_for_openvas()
676
677
        p.kbdb.add_scan_preferences.assert_not_called()
678
679
    @patch('ospd_openvas.db.KbDB')
680
    def test_set_scan_params(self, mock_kb):
681
        w = DummyDaemon()
682
683
        OSPD_PARAMS_MOCK = {
684
            'drop_privileges': {
685
                'type': 'boolean',
686
                'name': 'drop_privileges',
687
                'default': 0,
688
                'mandatory': 1,
689
                'description': '',
690
            },
691
        }
692
693
        opt = {'drop_privileges': 1}
694
695
        w.scan_collection.get_options = MagicMock(return_value=opt)
696
697
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
698
        p.scan_id = '456-789'
699
        p.kbdb.add_scan_preferences = MagicMock()
700
        p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK)
701
702
        p.kbdb.add_scan_preferences.assert_called_with(
703
            p.scan_id, ['drop_privileges|||yes']
704
        )
705
706
    @patch('ospd_openvas.db.KbDB')
707
    def test_set_reverse_lookup_opt(self, mock_kb):
708
        w = DummyDaemon()
709
710
        t_opt = {'reverse_lookup_only': 1}
711
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
712
713
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
714
        p.scan_id = '456-789'
715
        p.kbdb.add_scan_preferences = MagicMock()
716
        p.prepare_reverse_lookup_opt_for_openvas()
717
718
        p.kbdb.add_scan_preferences.assert_called_with(
719
            p.scan_id,
720
            [
721
                'reverse_lookup_only|||yes',
722
                'reverse_lookup_unify|||no',
723
            ],
724
        )
725
726
    @patch('ospd_openvas.db.KbDB')
727
    def test_set_boreas_alive_test_with_settings(self, mock_kb):
728
        # No Boreas config setting (BOREAS_SETTING_NAME) set
729
        w = DummyDaemon()
730
        ov_setting = {'not_the_correct_setting': 1}
731
        t_opt = {}
732
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
733
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
734
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
735
            p.scan_id = '456-789'
736
            p.kbdb.add_scan_preferences = MagicMock()
737
            p.prepare_boreas_alive_test()
738
739
            p.kbdb.add_scan_preferences.assert_not_called()
740
741
        # Boreas config setting set but invalid alive_test.
742
        w = DummyDaemon()
743
        t_opt = {'alive_test': "error"}
744
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
745
        ov_setting = {BOREAS_SETTING_NAME: 1}
746
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
747
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
748
            p.scan_id = '456-789'
749
            p.kbdb.add_scan_preferences = MagicMock()
750
            p.prepare_boreas_alive_test()
751
752
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
753
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
754
755
        # ALIVE_TEST_TCP_SYN_SERVICE as alive test.
756
        w = DummyDaemon()
757
        t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE}
758
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
759
        ov_setting = {BOREAS_SETTING_NAME: 1}
760
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
761
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
762
            p.scan_id = '456-789'
763
            p.kbdb.add_scan_preferences = MagicMock()
764
            p.prepare_boreas_alive_test()
765
766
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])]
767
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
768
769
        # ICMP was chosen as alive test.
770
        w = DummyDaemon()
771
        t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP}
772
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
773
        ov_setting = {BOREAS_SETTING_NAME: 1}
774
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
775
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
776
            p.scan_id = '456-789'
777
            p.kbdb.add_scan_preferences = MagicMock()
778
            p.prepare_boreas_alive_test()
779
780
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
781
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
782
783
        # "Scan Config Default" as alive_test.
784
        w = DummyDaemon()
785
        t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT}
786
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
787
        ov_setting = {BOREAS_SETTING_NAME: 1}
788
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
789
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
790
            p.scan_id = '456-789'
791
            p.kbdb.add_scan_preferences = MagicMock()
792
            p.prepare_boreas_alive_test()
793
794
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
795
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
796
797
        # TCP-SYN alive test and dedicated port list for alive scan provided.
798
        w = DummyDaemon()
799
        t_opt = {
800
            'alive_test_ports': "80,137",
801
            'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE,
802
        }
803
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
804
        ov_setting = {BOREAS_SETTING_NAME: 1}
805
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
806
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
807
            p.scan_id = '456-789'
808
            p.kbdb.add_scan_preferences = MagicMock()
809
            p.prepare_boreas_alive_test()
810
811
            calls = [
812
                call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']),
813
                call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']),
814
            ]
815
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
816
817
    @patch('ospd_openvas.db.KbDB')
818
    def test_set_boreas_alive_test_not_as_enum(self, mock_kb):
819
        # No Boreas config setting (BOREAS_SETTING_NAME) set
820
        w = DummyDaemon()
821
        ov_setting = {'not_the_correct_setting': 1}
822
        t_opt = {}
823
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
824
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
825
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
826
            p.scan_id = '456-789'
827
            p.kbdb.add_scan_preferences = MagicMock()
828
            p.prepare_boreas_alive_test()
829
830
            p.kbdb.add_scan_preferences.assert_not_called()
831
832
        # Boreas config setting set but invalid alive_test.
833
        w = DummyDaemon()
834
        t_opt = {'alive_test_methods': "1", 'arp': '-1'}
835
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
836
        ov_setting = {BOREAS_SETTING_NAME: 1}
837
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
838
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
839
            p.scan_id = '456-789'
840
            p.kbdb.add_scan_preferences = MagicMock()
841
            p.prepare_boreas_alive_test()
842
843
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
844
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
845
846
        # ICMP was chosen as alive test.
847
        w = DummyDaemon()
848
        t_opt = {'alive_test_methods': "1", 'icmp': '1'}
849
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
850
        ov_setting = {BOREAS_SETTING_NAME: 1}
851
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
852
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
853
            p.scan_id = '456-789'
854
            p.kbdb.add_scan_preferences = MagicMock()
855
            p.prepare_boreas_alive_test()
856
857
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
858
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
859
860
        # tcp_syn as alive test.
861
        w = DummyDaemon()
862
        t_opt = {'alive_test_methods': "1", 'tcp_syn': '1'}
863
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
864
        ov_setting = {BOREAS_SETTING_NAME: 1}
865
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
866
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
867
            p.scan_id = '456-789'
868
            p.kbdb.add_scan_preferences = MagicMock()
869
            p.prepare_boreas_alive_test()
870
871
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])]
872
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
873
874
        # tcp_ack as alive test.
875
        w = DummyDaemon()
876
        t_opt = {'alive_test_methods': "1", 'tcp_ack': '1'}
877
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
878
        ov_setting = {BOREAS_SETTING_NAME: 1}
879
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
880
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
881
            p.scan_id = '456-789'
882
            p.kbdb.add_scan_preferences = MagicMock()
883
            p.prepare_boreas_alive_test()
884
885
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||1'])]
886
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
887
888
        # arp as alive test.
889
        w = DummyDaemon()
890
        t_opt = {'alive_test_methods': "1", 'arp': '1'}
891
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
892
        ov_setting = {BOREAS_SETTING_NAME: 1}
893
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
894
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
895
            p.scan_id = '456-789'
896
            p.kbdb.add_scan_preferences = MagicMock()
897
            p.prepare_boreas_alive_test()
898
899
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||4'])]
900
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
901
902
        # arp as alive test.
903
        w = DummyDaemon()
904
        t_opt = {'alive_test_methods': "1", 'consider_alive': '1'}
905
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
906
        ov_setting = {BOREAS_SETTING_NAME: 1}
907
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
908
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
909
            p.scan_id = '456-789'
910
            p.kbdb.add_scan_preferences = MagicMock()
911
            p.prepare_boreas_alive_test()
912
913
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||8'])]
914
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
915
916
        # all alive test methods
917
        w = DummyDaemon()
918
        t_opt = {
919
            'alive_test_methods': "1",
920
            'icmp': '1',
921
            'tcp_ack': '1',
922
            'tcp_syn': '1',
923
            'arp': '1',
924
            'consider_alive': '1',
925
        }
926
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
927
        ov_setting = {BOREAS_SETTING_NAME: 1}
928
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
929
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
930
            p.scan_id = '456-789'
931
            p.kbdb.add_scan_preferences = MagicMock()
932
            p.prepare_boreas_alive_test()
933
934
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||31'])]
935
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
936
937
        # TCP-SYN alive test and dedicated port list for alive scan provided.
938
        w = DummyDaemon()
939
        t_opt = {
940
            'alive_test_ports': "80,137",
941
            'alive_test_methods': "1",
942
            'tcp_syn': '1',
943
        }
944
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
945
        ov_setting = {BOREAS_SETTING_NAME: 1}
946
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
947
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
948
            p.scan_id = '456-789'
949
            p.kbdb.add_scan_preferences = MagicMock()
950
            p.prepare_boreas_alive_test()
951
952
            calls = [
953
                call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']),
954
                call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']),
955
            ]
956
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
957
958
    @patch('ospd_openvas.db.KbDB')
959
    def test_set_boreas_alive_test_enum_has_precedence(self, mock_kb):
960
        w = DummyDaemon()
961
        t_opt = {
962
            'alive_test_methods': "1",
963
            'consider_alive': '1',
964
            'alive_test': AliveTest.ALIVE_TEST_ICMP,
965
        }
966
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
967
        ov_setting = {BOREAS_SETTING_NAME: 1}
968
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
969
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
970
            p.scan_id = '456-789'
971
            p.kbdb.add_scan_preferences = MagicMock()
972
            p.prepare_boreas_alive_test()
973
974
            # has icmp and not consider_alive
975
            calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])]
976
            p.kbdb.add_scan_preferences.assert_has_calls(calls)
977
978
    @patch('ospd_openvas.db.KbDB')
979
    def test_set_boreas_alive_test_without_settings(self, mock_kb):
980
        w = DummyDaemon()
981
        t_opt = {'alive_test': 16}
982
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
983
        ov_setting = {}
984
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
985
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
986
            p.scan_id = '456-789'
987
            p.kbdb.add_scan_preferences = MagicMock()
988
            p.prepare_boreas_alive_test()
989
990
            p.kbdb.add_scan_preferences.assert_not_called()
991
992 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
993
    def test_set_alive_no_setting(self, mock_kb):
994
        w = DummyDaemon()
995
996
        t_opt = {}
997
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
998
999
        ov_setting = {}
1000
1001
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1002
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1003
            p.scan_id = '456-789'
1004
            p.kbdb.add_scan_preferences = MagicMock()
1005
            p.prepare_alive_test_option_for_openvas()
1006
1007
            p.kbdb.add_scan_preferences.assert_not_called()
1008
1009 View Code Duplication
    @patch('ospd_openvas.db.KbDB')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1010
    def test_set_alive_no_invalid_alive_test(self, mock_kb):
1011
        w = DummyDaemon()
1012
1013
        t_opt = {'alive_test': -1}
1014
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
1015
1016
        ov_setting = {'some_setting': 1}
1017
1018
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1019
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1020
            p._nvts_params = {}
1021
            p.scan_id = '456-789'
1022
            p.kbdb.add_scan_preferences = MagicMock()
1023
            p.prepare_alive_test_option_for_openvas()
1024
1025
            p.kbdb.add_scan_preferences.assert_not_called()
1026
1027
    @patch('ospd_openvas.db.KbDB')
1028
    def test_set_alive_no_invalid_alive_test_no_enum(self, mock_kb):
1029
        w = DummyDaemon()
1030
1031
        t_opt = {'alive_test_methods': '1', 'icmp': '-1'}
1032
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
1033
1034
        ov_setting = {'some_setting': 1}
1035
1036
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1037
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1038
            p._nvts_params = {}
1039
            p.scan_id = '456-789'
1040
            p.kbdb.add_scan_preferences = MagicMock()
1041
            p.prepare_alive_test_option_for_openvas()
1042
1043
            p.kbdb.add_scan_preferences.assert_not_called()
1044
1045
    @patch('ospd_openvas.db.KbDB')
1046
    def test_set_alive_pinghost(self, mock_kb):
1047
        w = DummyDaemon()
1048
1049
        alive_test_out = [
1050
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no",
1051
            "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no",
1052
            "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no",
1053
            "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes",
1054
            "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no",
1055
            "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes",
1056
        ]
1057
1058
        t_opt = {'alive_test': 2}
1059
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
1060
1061
        ov_setting = {'some_setting': 1}
1062
1063
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1064
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1065
            p._nvts_params = {}
1066
            p.scan_id = '456-789'
1067
            p.kbdb.add_scan_preferences = MagicMock()
1068
            p.prepare_alive_test_option_for_openvas()
1069
1070
            for key, value in p._nvts_params.items():
1071
                self.assertTrue(
1072
                    "{0}|||{1}".format(key, value) in alive_test_out
1073
                )
1074
1075
    @patch('ospd_openvas.db.KbDB')
1076
    def test_prepare_alive_test_not_supplied_as_enum(self, mock_kb):
1077
        w = DummyDaemon()
1078
1079
        alive_test_out = {
1080
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no",
1081
            "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no",
1082
            "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no",
1083
            "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes",
1084
            "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no",
1085
            "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes",
1086
        }
1087
1088
        t_opt = {'alive_test_methods': '1', 'icmp': '1'}
1089
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
1090
1091
        ov_setting = {'some_setting': 1}
1092
1093
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1094
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1095
            p._nvts_params = {}
1096
            p.scan_id = '456-789'
1097
            p.kbdb.add_scan_preferences = MagicMock()
1098
            p.prepare_alive_test_option_for_openvas()
1099
1100
            self.assertEqual(p._nvts_params, alive_test_out)
1101
1102
    @patch('ospd_openvas.db.KbDB')
1103
    def test_prepare_alive_test_no_enum_no_alive_test(self, mock_kb):
1104
        w = DummyDaemon()
1105
1106
        t_opt = {'alive_test_methods': '1', 'icmp': '0'}
1107
        w.scan_collection.get_target_options = MagicMock(return_value=t_opt)
1108
1109
        ov_setting = {'some_setting': 1}
1110
1111
        with patch.object(Openvas, 'get_settings', return_value=ov_setting):
1112
            p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1113
            p._nvts_params = {}
1114
            p.scan_id = '456-789'
1115
            p.kbdb.add_scan_preferences = MagicMock()
1116
            p.prepare_alive_test_option_for_openvas()
1117
1118
            p.kbdb.add_scan_preferences.assert_not_called()
1119
1120
    def test_alive_test_methods_to_bit_field(self):
1121
1122
        self.assertEqual(
1123
            AliveTest.ALIVE_TEST_TCP_ACK_SERVICE,
1124
            alive_test_methods_to_bit_field(
1125
                icmp=False,
1126
                tcp_ack=True,
1127
                tcp_syn=False,
1128
                arp=False,
1129
                consider_alive=False,
1130
            ),
1131
        )
1132
1133
        self.assertEqual(
1134
            AliveTest.ALIVE_TEST_ICMP,
1135
            alive_test_methods_to_bit_field(
1136
                icmp=True,
1137
                tcp_ack=False,
1138
                tcp_syn=False,
1139
                arp=False,
1140
                consider_alive=False,
1141
            ),
1142
        )
1143
1144
        self.assertEqual(
1145
            AliveTest.ALIVE_TEST_ARP,
1146
            alive_test_methods_to_bit_field(
1147
                icmp=False,
1148
                tcp_ack=False,
1149
                tcp_syn=False,
1150
                arp=True,
1151
                consider_alive=False,
1152
            ),
1153
        )
1154
1155
        self.assertEqual(
1156
            AliveTest.ALIVE_TEST_CONSIDER_ALIVE,
1157
            alive_test_methods_to_bit_field(
1158
                icmp=False,
1159
                tcp_ack=False,
1160
                tcp_syn=False,
1161
                arp=False,
1162
                consider_alive=True,
1163
            ),
1164
        )
1165
1166
        self.assertEqual(
1167
            AliveTest.ALIVE_TEST_TCP_SYN_SERVICE,
1168
            alive_test_methods_to_bit_field(
1169
                icmp=False,
1170
                tcp_ack=False,
1171
                tcp_syn=True,
1172
                arp=False,
1173
                consider_alive=False,
1174
            ),
1175
        )
1176
1177
        all_alive_test_methods = (
1178
            AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT
1179
            | AliveTest.ALIVE_TEST_TCP_ACK_SERVICE
1180
            | AliveTest.ALIVE_TEST_ICMP
1181
            | AliveTest.ALIVE_TEST_ARP
1182
            | AliveTest.ALIVE_TEST_CONSIDER_ALIVE
1183
            | AliveTest.ALIVE_TEST_TCP_SYN_SERVICE
1184
        )
1185
        self.assertEqual(
1186
            all_alive_test_methods,
1187
            alive_test_methods_to_bit_field(
1188
                icmp=True,
1189
                tcp_ack=True,
1190
                tcp_syn=True,
1191
                arp=True,
1192
                consider_alive=True,
1193
            ),
1194
        )
1195
1196
    @patch('ospd_openvas.db.KbDB')
1197
    def test_prepare_nvt_prefs(self, mock_kb):
1198
        w = DummyDaemon()
1199
1200
        alive_test_out = [
1201
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no"
1202
        ]
1203
1204
        p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
1205
        p._nvts_params = {
1206
            "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no"
1207
        }
1208
        p.kbdb.add_scan_preferences = MagicMock()
1209
        p.prepare_nvt_preferences()
1210
1211
        p.kbdb.add_scan_preferences.assert_called_with(
1212
            p.scan_id,
1213
            alive_test_out,
1214
        )
1215
1216
    @patch('ospd_openvas.db.KbDB')
1217
    def test_prepare_nvt_prefs_no_prefs(self, mock_kb):
1218
        w = DummyDaemon()
1219
1220
        p = PreferenceHandler('456-789', mock_kb, w.scan_collection, None)
1221
        p._nvts_params = {}
1222
        p.kbdb.add_scan_preferences = MagicMock()
1223
        p.prepare_nvt_preferences()
1224
1225
        p.kbdb.add_scan_preferences.assert_not_called()
1226