PreferenceHandlerTestCase.test_snmp_unknown_auth_alg_credentials()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 30
Code Lines 23

Duplication

Lines 30
Ratio 100 %

Importance

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