| Total Complexity | 56 |
| Total Lines | 949 |
| Duplicated Lines | 17.7 % |
| Changes | 0 | ||
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:
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-2020 Greenbone Networks GmbH |
||
| 3 | # |
||
| 4 | # SPDX-License-Identifier: AGPL-3.0-or-later |
||
| 5 | # |
||
| 6 | # This program is free software: you can redistribute it and/or modify |
||
| 7 | # it under the terms of the GNU Affero General Public License as |
||
| 8 | # published by the Free Software Foundation, either version 3 of the |
||
| 9 | # License, or (at your option) any later version. |
||
| 10 | # |
||
| 11 | # This program is distributed in the hope that it will be useful, |
||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | # GNU Affero General Public License for more details. |
||
| 15 | # |
||
| 16 | # You should have received a copy of the GNU Affero General Public License |
||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 18 | |||
| 19 | |||
| 20 | import logging |
||
| 21 | |||
| 22 | from unittest import TestCase |
||
| 23 | from unittest.mock import call, patch, Mock, MagicMock |
||
| 24 | |||
| 25 | from ospd.vts import Vts |
||
| 26 | |||
| 27 | from tests.dummydaemon import DummyDaemon |
||
| 28 | from tests.helper import assert_called_once |
||
| 29 | |||
| 30 | import ospd_openvas.db |
||
| 31 | |||
| 32 | from ospd_openvas.openvas import Openvas |
||
| 33 | from ospd_openvas.preferencehandler import ( |
||
| 34 | AliveTest, |
||
| 35 | BOREAS_SETTING_NAME, |
||
| 36 | BOREAS_ALIVE_TEST, |
||
| 37 | BOREAS_ALIVE_TEST_PORTS, |
||
| 38 | PreferenceHandler, |
||
| 39 | alive_test_methods_to_bit_field, |
||
| 40 | ) |
||
| 41 | |||
| 42 | |||
| 43 | class PreferenceHandlerTestCase(TestCase): |
||
| 44 | @patch('ospd_openvas.db.KbDB') |
||
| 45 | def test_process_vts_not_found(self, mock_kb): |
||
| 46 | w = DummyDaemon() |
||
| 47 | logging.Logger.warning = Mock() |
||
| 48 | |||
| 49 | vts = { |
||
| 50 | '1.3.6.1.4.1.25623.1.0.100065': {'3': 'new value'}, |
||
| 51 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 52 | } |
||
| 53 | |||
| 54 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 55 | w.nvti.get_nvt_metadata.return_value = None |
||
| 56 | |||
| 57 | ov_setting = {'table_driven_lsc': 0} |
||
| 58 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 59 | p._process_vts(vts) |
||
| 60 | |||
| 61 | assert_called_once(logging.Logger.warning) |
||
| 62 | |||
| 63 | def test_process_vts_bad_param_id(self): |
||
| 64 | w = DummyDaemon() |
||
| 65 | |||
| 66 | vts = { |
||
| 67 | '1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
||
| 68 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 69 | } |
||
| 70 | |||
| 71 | p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
||
| 72 | |||
| 73 | ret = p._process_vts(vts) |
||
| 74 | |||
| 75 | self.assertFalse(ret[1]) |
||
| 76 | |||
| 77 | def test_process_vts(self): |
||
| 78 | w = DummyDaemon() |
||
| 79 | |||
| 80 | vts = { |
||
| 81 | '1.3.6.1.4.1.25623.1.0.100061': {'1': 'new value'}, |
||
| 82 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 83 | } |
||
| 84 | vt_out = ( |
||
| 85 | ['1.3.6.1.4.1.25623.1.0.100061'], |
||
| 86 | {'1.3.6.1.4.1.25623.1.0.100061:1:entry:Data length :': 'new value'}, |
||
| 87 | ) |
||
| 88 | |||
| 89 | p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
||
| 90 | ret = p._process_vts(vts) |
||
| 91 | |||
| 92 | self.assertEqual(ret, vt_out) |
||
| 93 | |||
| 94 | @patch('ospd_openvas.preferencehandler.Openvas') |
||
| 95 | @patch('ospd_openvas.preferencehandler.NotusMetadataHandler') |
||
| 96 | def test_get_vts_in_groups(self, MockNotus, MockOpenvas): |
||
| 97 | w = DummyDaemon() |
||
| 98 | openvas = MockOpenvas() |
||
| 99 | openvas.get_settings.return_value = {'table_driven_lsc': 1} |
||
| 100 | notus = MockNotus() |
||
| 101 | notus.get_family_driver_linkers.return_value = { |
||
| 102 | 'some_family': '1.3.6.1.4.1.25623.1.0.1234' |
||
| 103 | } |
||
| 104 | |||
| 105 | filters = ['family=some_family'] |
||
| 106 | vt_out = ['1.3.6.1.4.1.25623.1.0.1234'] |
||
| 107 | |||
| 108 | p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
||
| 109 | ret = p._get_vts_in_groups(filters) |
||
| 110 | |||
| 111 | self.assertEqual(ret, vt_out) |
||
| 112 | |||
| 113 | @patch('ospd_openvas.db.KbDB') |
||
| 114 | def test_set_plugins_false(self, mock_kb): |
||
| 115 | w = DummyDaemon() |
||
| 116 | |||
| 117 | w.scan_collection.get_vts = Mock() |
||
| 118 | w.scan_collection.get_vts.return_value = {} |
||
| 119 | |||
| 120 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 121 | p.kbdb.add_scan_preferences = Mock() |
||
| 122 | r = p.prepare_plugins_for_openvas() |
||
| 123 | |||
| 124 | self.assertFalse(r) |
||
| 125 | |||
| 126 | @patch('ospd_openvas.db.KbDB') |
||
| 127 | def test_set_plugins_true(self, mock_kb): |
||
| 128 | w = DummyDaemon() |
||
| 129 | |||
| 130 | vts = { |
||
| 131 | '1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
||
| 132 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 133 | } |
||
| 134 | |||
| 135 | w.scan_collection.get_vts = Mock() |
||
| 136 | w.scan_collection.get_vts.return_value = vts |
||
| 137 | |||
| 138 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 139 | p.kbdb.add_scan_preferences = Mock() |
||
| 140 | r = p.prepare_plugins_for_openvas() |
||
| 141 | |||
| 142 | self.assertTrue(r) |
||
| 143 | |||
| 144 | def test_build_credentials_ssh_up(self): |
||
| 145 | w = DummyDaemon() |
||
| 146 | |||
| 147 | cred_out = [ |
||
| 148 | 'auth_port_ssh|||22', |
||
| 149 | '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
||
| 150 | '1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass', |
||
| 151 | ] |
||
| 152 | cred_dict = { |
||
| 153 | 'ssh': { |
||
| 154 | 'type': 'up', |
||
| 155 | 'port': '22', |
||
| 156 | 'username': 'username', |
||
| 157 | 'password': 'pass', |
||
| 158 | } |
||
| 159 | } |
||
| 160 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 161 | |||
| 162 | ret = p.build_credentials_as_prefs(cred_dict) |
||
| 163 | |||
| 164 | self.assertEqual(ret, cred_out) |
||
| 165 | |||
| 166 | def test_build_credentials(self): |
||
| 167 | w = DummyDaemon() |
||
| 168 | |||
| 169 | cred_out = [ |
||
| 170 | '1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username', |
||
| 171 | '1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:|||pass', |
||
| 172 | 'auth_port_ssh|||22', |
||
| 173 | '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
||
| 174 | '1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:|||pass', |
||
| 175 | '1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||', |
||
| 176 | '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
||
| 177 | '1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass', |
||
| 178 | '1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:some comunity', |
||
| 179 | '1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username', |
||
| 180 | '1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass', |
||
| 181 | '1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:some auth algo', |
||
| 182 | '1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:privacy pass', |
||
| 183 | '1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:privacy algo', |
||
| 184 | ] |
||
| 185 | cred_dict = { |
||
| 186 | 'ssh': { |
||
| 187 | 'type': 'ssh', |
||
| 188 | 'port': '22', |
||
| 189 | 'username': 'username', |
||
| 190 | 'password': 'pass', |
||
| 191 | }, |
||
| 192 | 'smb': {'type': 'smb', 'username': 'username', 'password': 'pass'}, |
||
| 193 | 'esxi': { |
||
| 194 | 'type': 'esxi', |
||
| 195 | 'username': 'username', |
||
| 196 | 'password': 'pass', |
||
| 197 | }, |
||
| 198 | 'snmp': { |
||
| 199 | 'type': 'snmp', |
||
| 200 | 'username': 'username', |
||
| 201 | 'password': 'pass', |
||
| 202 | 'community': 'some comunity', |
||
| 203 | 'auth_algorithm': 'some auth algo', |
||
| 204 | 'privacy_password': 'privacy pass', |
||
| 205 | 'privacy_algorithm': 'privacy algo', |
||
| 206 | }, |
||
| 207 | } |
||
| 208 | |||
| 209 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 210 | ret = p.build_credentials_as_prefs(cred_dict) |
||
| 211 | |||
| 212 | self.assertEqual(len(ret), len(cred_out)) |
||
| 213 | self.assertIn('auth_port_ssh|||22', cred_out) |
||
| 214 | self.assertIn( |
||
| 215 | '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
||
| 216 | cred_out, |
||
| 217 | ) |
||
| 218 | |||
| 219 | View Code Duplication | def test_build_alive_test_opt_empty(self): |
|
|
|
|||
| 220 | w = DummyDaemon() |
||
| 221 | |||
| 222 | target_options_dict = {'alive_test': '0'} |
||
| 223 | |||
| 224 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 225 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 226 | |||
| 227 | self.assertEqual(ret, []) |
||
| 228 | |||
| 229 | # alive test was supplied via seperate xml element |
||
| 230 | w = DummyDaemon() |
||
| 231 | target_options_dict = {'alive_test_methods': '1', 'icmp': '0'} |
||
| 232 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 233 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 234 | self.assertEqual(ret, []) |
||
| 235 | |||
| 236 | View Code Duplication | def test_build_alive_test_opt(self): |
|
| 237 | w = DummyDaemon() |
||
| 238 | |||
| 239 | alive_test_out = [ |
||
| 240 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
||
| 241 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
||
| 242 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
||
| 243 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
||
| 244 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
||
| 245 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
||
| 246 | ] |
||
| 247 | target_options_dict = {'alive_test': '2'} |
||
| 248 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 249 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 250 | |||
| 251 | self.assertEqual(ret, alive_test_out) |
||
| 252 | |||
| 253 | # alive test was supplied via sepertae xml element |
||
| 254 | w = DummyDaemon() |
||
| 255 | alive_test_out = [ |
||
| 256 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
||
| 257 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
||
| 258 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
||
| 259 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
||
| 260 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
||
| 261 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
||
| 262 | ] |
||
| 263 | target_options_dict = {'alive_test_methods': '1', 'icmp': '1'} |
||
| 264 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 265 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 266 | self.assertEqual(ret, alive_test_out) |
||
| 267 | |||
| 268 | def test_build_alive_test_opt_fail_1(self): |
||
| 269 | w = DummyDaemon() |
||
| 270 | logging.Logger.debug = Mock() |
||
| 271 | |||
| 272 | target_options_dict = {'alive_test': 'a'} |
||
| 273 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 274 | target_options = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 275 | |||
| 276 | assert_called_once(logging.Logger.debug) |
||
| 277 | self.assertEqual(len(target_options), 0) |
||
| 278 | |||
| 279 | @patch('ospd_openvas.db.KbDB') |
||
| 280 | def test_set_target(self, mock_kb): |
||
| 281 | w = DummyDaemon() |
||
| 282 | |||
| 283 | w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1') |
||
| 284 | |||
| 285 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 286 | p.scan_id = '456-789' |
||
| 287 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 288 | p.prepare_target_for_openvas() |
||
| 289 | |||
| 290 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 291 | p.scan_id, |
||
| 292 | ['TARGET|||192.168.0.1'], |
||
| 293 | ) |
||
| 294 | |||
| 295 | @patch('ospd_openvas.db.KbDB') |
||
| 296 | def test_set_ports(self, mock_kb): |
||
| 297 | w = DummyDaemon() |
||
| 298 | |||
| 299 | w.scan_collection.get_ports = MagicMock(return_value='80,443') |
||
| 300 | |||
| 301 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 302 | p.scan_id = '456-789' |
||
| 303 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 304 | p.prepare_ports_for_openvas() |
||
| 305 | |||
| 306 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 307 | p.scan_id, |
||
| 308 | ['port_range|||80,443'], |
||
| 309 | ) |
||
| 310 | |||
| 311 | @patch('ospd_openvas.db.KbDB') |
||
| 312 | def test_set_main_kbindex(self, mock_kb): |
||
| 313 | w = DummyDaemon() |
||
| 314 | |||
| 315 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 316 | p.kbdb.add_scan_preferences = Mock() |
||
| 317 | p.kbdb.index = 2 |
||
| 318 | p.prepare_main_kbindex_for_openvas() |
||
| 319 | |||
| 320 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 321 | p.scan_id, |
||
| 322 | ['ov_maindbid|||2'], |
||
| 323 | ) |
||
| 324 | |||
| 325 | @patch('ospd_openvas.db.KbDB') |
||
| 326 | def test_set_credentials(self, mock_kb): |
||
| 327 | w = DummyDaemon() |
||
| 328 | |||
| 329 | creds = { |
||
| 330 | 'ssh': { |
||
| 331 | 'type': 'ssh', |
||
| 332 | 'port': '22', |
||
| 333 | 'username': 'username', |
||
| 334 | 'password': 'pass', |
||
| 335 | }, |
||
| 336 | 'smb': {'type': 'smb', 'username': 'username', 'password': 'pass'}, |
||
| 337 | 'esxi': { |
||
| 338 | 'type': 'esxi', |
||
| 339 | 'username': 'username', |
||
| 340 | 'password': 'pass', |
||
| 341 | }, |
||
| 342 | 'snmp': { |
||
| 343 | 'type': 'snmp', |
||
| 344 | 'username': 'username', |
||
| 345 | 'password': 'pass', |
||
| 346 | 'community': 'some comunity', |
||
| 347 | 'auth_algorithm': 'some auth algo', |
||
| 348 | 'privacy_password': 'privacy pass', |
||
| 349 | 'privacy_algorithm': 'privacy algo', |
||
| 350 | }, |
||
| 351 | } |
||
| 352 | |||
| 353 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 354 | |||
| 355 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 356 | p.scan_id = '456-789' |
||
| 357 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 358 | r = p.prepare_credentials_for_openvas() |
||
| 359 | |||
| 360 | self.assertTrue(r) |
||
| 361 | assert_called_once(p.kbdb.add_scan_preferences) |
||
| 362 | |||
| 363 | @patch('ospd_openvas.db.KbDB') |
||
| 364 | def test_set_credentials(self, mock_kb): |
||
| 365 | w = DummyDaemon() |
||
| 366 | |||
| 367 | # bad cred type shh instead of ssh |
||
| 368 | creds = { |
||
| 369 | 'shh': { |
||
| 370 | 'type': 'ssh', |
||
| 371 | 'port': '22', |
||
| 372 | 'username': 'username', |
||
| 373 | 'password': 'pass', |
||
| 374 | }, |
||
| 375 | } |
||
| 376 | |||
| 377 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 378 | |||
| 379 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 380 | p.scan_id = '456-789' |
||
| 381 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 382 | r = p.prepare_credentials_for_openvas() |
||
| 383 | |||
| 384 | self.assertFalse(r) |
||
| 385 | |||
| 386 | @patch('ospd_openvas.db.KbDB') |
||
| 387 | def test_set_credentials_empty(self, mock_kb): |
||
| 388 | w = DummyDaemon() |
||
| 389 | |||
| 390 | creds = {} |
||
| 391 | |||
| 392 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 393 | |||
| 394 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 395 | p.scan_id = '456-789' |
||
| 396 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 397 | r = p.prepare_credentials_for_openvas() |
||
| 398 | |||
| 399 | self.assertTrue(r) |
||
| 400 | |||
| 401 | @patch('ospd_openvas.db.KbDB') |
||
| 402 | def test_set_host_options(self, mock_kb): |
||
| 403 | w = DummyDaemon() |
||
| 404 | |||
| 405 | exc = '192.168.0.1' |
||
| 406 | |||
| 407 | w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
||
| 408 | |||
| 409 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 410 | p.scan_id = '456-789' |
||
| 411 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 412 | p.prepare_host_options_for_openvas() |
||
| 413 | |||
| 414 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 415 | p.scan_id, |
||
| 416 | ['exclude_hosts|||192.168.0.1'], |
||
| 417 | ) |
||
| 418 | |||
| 419 | @patch('ospd_openvas.db.KbDB') |
||
| 420 | def test_set_host_options_none(self, mock_kb): |
||
| 421 | w = DummyDaemon() |
||
| 422 | |||
| 423 | exc = '' |
||
| 424 | |||
| 425 | w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
||
| 426 | |||
| 427 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 428 | p.scan_id = '456-789' |
||
| 429 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 430 | p.prepare_host_options_for_openvas() |
||
| 431 | |||
| 432 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 433 | |||
| 434 | @patch('ospd_openvas.db.KbDB') |
||
| 435 | def test_set_scan_params(self, mock_kb): |
||
| 436 | w = DummyDaemon() |
||
| 437 | |||
| 438 | OSPD_PARAMS_MOCK = { |
||
| 439 | 'drop_privileges': { |
||
| 440 | 'type': 'boolean', |
||
| 441 | 'name': 'drop_privileges', |
||
| 442 | 'default': 0, |
||
| 443 | 'mandatory': 1, |
||
| 444 | 'description': '', |
||
| 445 | }, |
||
| 446 | } |
||
| 447 | |||
| 448 | opt = {'drop_privileges': 1} |
||
| 449 | |||
| 450 | w.scan_collection.get_options = MagicMock(return_value=opt) |
||
| 451 | |||
| 452 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 453 | p.scan_id = '456-789' |
||
| 454 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 455 | p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK) |
||
| 456 | |||
| 457 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 458 | p.scan_id, ['drop_privileges|||yes'] |
||
| 459 | ) |
||
| 460 | |||
| 461 | @patch('ospd_openvas.db.KbDB') |
||
| 462 | def test_set_reverse_lookup_opt(self, mock_kb): |
||
| 463 | w = DummyDaemon() |
||
| 464 | |||
| 465 | t_opt = {'reverse_lookup_only': 1} |
||
| 466 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 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 | p.prepare_reverse_lookup_opt_for_openvas() |
||
| 472 | |||
| 473 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 474 | p.scan_id, |
||
| 475 | [ |
||
| 476 | 'reverse_lookup_only|||yes', |
||
| 477 | 'reverse_lookup_unify|||no', |
||
| 478 | ], |
||
| 479 | ) |
||
| 480 | |||
| 481 | @patch('ospd_openvas.db.KbDB') |
||
| 482 | def test_set_boreas_alive_test_with_settings(self, mock_kb): |
||
| 483 | # No Boreas config setting (BOREAS_SETTING_NAME) set |
||
| 484 | w = DummyDaemon() |
||
| 485 | ov_setting = {'not_the_correct_setting': 1} |
||
| 486 | t_opt = {} |
||
| 487 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 488 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 489 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 490 | p.scan_id = '456-789' |
||
| 491 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 492 | p.prepare_boreas_alive_test() |
||
| 493 | |||
| 494 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 495 | |||
| 496 | # Boreas config setting set but invalid alive_test. |
||
| 497 | w = DummyDaemon() |
||
| 498 | t_opt = {'alive_test': "error"} |
||
| 499 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 500 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 501 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 502 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 503 | p.scan_id = '456-789' |
||
| 504 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 505 | p.prepare_boreas_alive_test() |
||
| 506 | |||
| 507 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 508 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 509 | |||
| 510 | # ALIVE_TEST_TCP_SYN_SERVICE as alive test. |
||
| 511 | w = DummyDaemon() |
||
| 512 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE} |
||
| 513 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 514 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 515 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 516 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 517 | p.scan_id = '456-789' |
||
| 518 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 519 | p.prepare_boreas_alive_test() |
||
| 520 | |||
| 521 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] |
||
| 522 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 523 | |||
| 524 | # ICMP was chosen as alive test. |
||
| 525 | w = DummyDaemon() |
||
| 526 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP} |
||
| 527 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 528 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 529 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 530 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 531 | p.scan_id = '456-789' |
||
| 532 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 533 | p.prepare_boreas_alive_test() |
||
| 534 | |||
| 535 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 536 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 537 | |||
| 538 | # "Scan Config Default" as alive_test. |
||
| 539 | w = DummyDaemon() |
||
| 540 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT} |
||
| 541 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 542 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 543 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 544 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 545 | p.scan_id = '456-789' |
||
| 546 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 547 | p.prepare_boreas_alive_test() |
||
| 548 | |||
| 549 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 550 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 551 | |||
| 552 | # TCP-SYN alive test and dedicated port list for alive scan provided. |
||
| 553 | w = DummyDaemon() |
||
| 554 | t_opt = { |
||
| 555 | 'alive_test_ports': "80,137", |
||
| 556 | 'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE, |
||
| 557 | } |
||
| 558 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 559 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 560 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 561 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 562 | p.scan_id = '456-789' |
||
| 563 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 564 | p.prepare_boreas_alive_test() |
||
| 565 | |||
| 566 | calls = [ |
||
| 567 | call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']), |
||
| 568 | call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']), |
||
| 569 | ] |
||
| 570 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 571 | |||
| 572 | @patch('ospd_openvas.db.KbDB') |
||
| 573 | def test_set_boreas_alive_test_not_as_enum(self, mock_kb): |
||
| 574 | # No Boreas config setting (BOREAS_SETTING_NAME) set |
||
| 575 | w = DummyDaemon() |
||
| 576 | ov_setting = {'not_the_correct_setting': 1} |
||
| 577 | t_opt = {} |
||
| 578 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 579 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 580 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 581 | p.scan_id = '456-789' |
||
| 582 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 583 | p.prepare_boreas_alive_test() |
||
| 584 | |||
| 585 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 586 | |||
| 587 | # Boreas config setting set but invalid alive_test. |
||
| 588 | w = DummyDaemon() |
||
| 589 | t_opt = {'alive_test_methods': "1", 'arp': '-1'} |
||
| 590 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 591 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 592 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 593 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 594 | p.scan_id = '456-789' |
||
| 595 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 596 | p.prepare_boreas_alive_test() |
||
| 597 | |||
| 598 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 599 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 600 | |||
| 601 | # ICMP was chosen as alive test. |
||
| 602 | w = DummyDaemon() |
||
| 603 | t_opt = {'alive_test_methods': "1", 'icmp': '1'} |
||
| 604 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 605 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 606 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 607 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 608 | p.scan_id = '456-789' |
||
| 609 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 610 | p.prepare_boreas_alive_test() |
||
| 611 | |||
| 612 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 613 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 614 | |||
| 615 | # tcp_syn as alive test. |
||
| 616 | w = DummyDaemon() |
||
| 617 | t_opt = {'alive_test_methods': "1", 'tcp_syn': '1'} |
||
| 618 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 619 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 620 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 621 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 622 | p.scan_id = '456-789' |
||
| 623 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 624 | p.prepare_boreas_alive_test() |
||
| 625 | |||
| 626 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] |
||
| 627 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 628 | |||
| 629 | # tcp_ack as alive test. |
||
| 630 | w = DummyDaemon() |
||
| 631 | t_opt = {'alive_test_methods': "1", 'tcp_ack': '1'} |
||
| 632 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 633 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 634 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 635 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 636 | p.scan_id = '456-789' |
||
| 637 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 638 | p.prepare_boreas_alive_test() |
||
| 639 | |||
| 640 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||1'])] |
||
| 641 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 642 | |||
| 643 | # arp as alive test. |
||
| 644 | w = DummyDaemon() |
||
| 645 | t_opt = {'alive_test_methods': "1", 'arp': '1'} |
||
| 646 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 647 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 648 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 649 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 650 | p.scan_id = '456-789' |
||
| 651 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 652 | p.prepare_boreas_alive_test() |
||
| 653 | |||
| 654 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||4'])] |
||
| 655 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 656 | |||
| 657 | # arp as alive test. |
||
| 658 | w = DummyDaemon() |
||
| 659 | t_opt = {'alive_test_methods': "1", 'consider_alive': '1'} |
||
| 660 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 661 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 662 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 663 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 664 | p.scan_id = '456-789' |
||
| 665 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 666 | p.prepare_boreas_alive_test() |
||
| 667 | |||
| 668 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||8'])] |
||
| 669 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 670 | |||
| 671 | # all alive test methods |
||
| 672 | w = DummyDaemon() |
||
| 673 | t_opt = { |
||
| 674 | 'alive_test_methods': "1", |
||
| 675 | 'icmp': '1', |
||
| 676 | 'tcp_ack': '1', |
||
| 677 | 'tcp_syn': '1', |
||
| 678 | 'arp': '1', |
||
| 679 | 'consider_alive': '1', |
||
| 680 | } |
||
| 681 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 682 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 683 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 684 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 685 | p.scan_id = '456-789' |
||
| 686 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 687 | p.prepare_boreas_alive_test() |
||
| 688 | |||
| 689 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||31'])] |
||
| 690 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 691 | |||
| 692 | # TCP-SYN alive test and dedicated port list for alive scan provided. |
||
| 693 | w = DummyDaemon() |
||
| 694 | t_opt = { |
||
| 695 | 'alive_test_ports': "80,137", |
||
| 696 | 'alive_test_methods': "1", |
||
| 697 | 'tcp_syn': '1', |
||
| 698 | } |
||
| 699 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 700 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 701 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 702 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 703 | p.scan_id = '456-789' |
||
| 704 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 705 | p.prepare_boreas_alive_test() |
||
| 706 | |||
| 707 | calls = [ |
||
| 708 | call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']), |
||
| 709 | call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']), |
||
| 710 | ] |
||
| 711 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 712 | |||
| 713 | @patch('ospd_openvas.db.KbDB') |
||
| 714 | def test_set_boreas_alive_test_enum_has_precedence(self, mock_kb): |
||
| 715 | w = DummyDaemon() |
||
| 716 | t_opt = { |
||
| 717 | 'alive_test_methods': "1", |
||
| 718 | 'consider_alive': '1', |
||
| 719 | 'alive_test': AliveTest.ALIVE_TEST_ICMP, |
||
| 720 | } |
||
| 721 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 722 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 723 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 724 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 725 | p.scan_id = '456-789' |
||
| 726 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 727 | p.prepare_boreas_alive_test() |
||
| 728 | |||
| 729 | # has icmp and not consider_alive |
||
| 730 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 731 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 732 | |||
| 733 | @patch('ospd_openvas.db.KbDB') |
||
| 734 | def test_set_boreas_alive_test_without_settings(self, mock_kb): |
||
| 735 | w = DummyDaemon() |
||
| 736 | t_opt = {'alive_test': 16} |
||
| 737 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 738 | ov_setting = {} |
||
| 739 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 740 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 741 | p.scan_id = '456-789' |
||
| 742 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 743 | p.prepare_boreas_alive_test() |
||
| 744 | |||
| 745 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 746 | |||
| 747 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 748 | def test_set_alive_no_setting(self, mock_kb): |
||
| 749 | w = DummyDaemon() |
||
| 750 | |||
| 751 | t_opt = {} |
||
| 752 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 753 | |||
| 754 | ov_setting = {} |
||
| 755 | |||
| 756 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 757 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 758 | p.scan_id = '456-789' |
||
| 759 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 760 | p.prepare_alive_test_option_for_openvas() |
||
| 761 | |||
| 762 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 763 | |||
| 764 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 765 | def test_set_alive_no_invalid_alive_test(self, mock_kb): |
||
| 766 | w = DummyDaemon() |
||
| 767 | |||
| 768 | t_opt = {'alive_test': -1} |
||
| 769 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 770 | |||
| 771 | ov_setting = {'some_setting': 1} |
||
| 772 | |||
| 773 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 774 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 775 | p.scan_id = '456-789' |
||
| 776 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 777 | p.prepare_alive_test_option_for_openvas() |
||
| 778 | |||
| 779 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 780 | |||
| 781 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 782 | def test_set_alive_no_invalid_alive_test_no_enum(self, mock_kb): |
||
| 783 | w = DummyDaemon() |
||
| 784 | |||
| 785 | t_opt = {'alive_test_methods': '1', 'icmp': '-1'} |
||
| 786 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 787 | |||
| 788 | ov_setting = {'some_setting': 1} |
||
| 789 | |||
| 790 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 791 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 792 | p.scan_id = '456-789' |
||
| 793 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 794 | p.prepare_alive_test_option_for_openvas() |
||
| 795 | |||
| 796 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 797 | |||
| 798 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 799 | def test_set_alive_pinghost(self, mock_kb): |
||
| 800 | w = DummyDaemon() |
||
| 801 | |||
| 802 | alive_test_out = [ |
||
| 803 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
||
| 804 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
||
| 805 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
||
| 806 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
||
| 807 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
||
| 808 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
||
| 809 | ] |
||
| 810 | |||
| 811 | t_opt = {'alive_test': 2} |
||
| 812 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 813 | |||
| 814 | ov_setting = {'some_setting': 1} |
||
| 815 | |||
| 816 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 817 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 818 | |||
| 819 | p.scan_id = '456-789' |
||
| 820 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 821 | p.prepare_alive_test_option_for_openvas() |
||
| 822 | |||
| 823 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 824 | p.scan_id, |
||
| 825 | alive_test_out, |
||
| 826 | ) |
||
| 827 | |||
| 828 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 829 | def test_prepare_alive_test_not_supplied_as_enum(self, mock_kb): |
||
| 830 | w = DummyDaemon() |
||
| 831 | |||
| 832 | alive_test_out = [ |
||
| 833 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
||
| 834 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
||
| 835 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
||
| 836 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
||
| 837 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
||
| 838 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
||
| 839 | ] |
||
| 840 | |||
| 841 | t_opt = {'alive_test_methods': '1', 'icmp': '1'} |
||
| 842 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 843 | |||
| 844 | ov_setting = {'some_setting': 1} |
||
| 845 | |||
| 846 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 847 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 848 | |||
| 849 | p.scan_id = '456-789' |
||
| 850 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 851 | p.prepare_alive_test_option_for_openvas() |
||
| 852 | |||
| 853 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 854 | p.scan_id, |
||
| 855 | alive_test_out, |
||
| 856 | ) |
||
| 857 | |||
| 858 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 859 | def test_prepare_alive_test_no_enum_no_alive_test(self, mock_kb): |
||
| 860 | w = DummyDaemon() |
||
| 861 | |||
| 862 | t_opt = {'alive_test_methods': '1', 'icmp': '0'} |
||
| 863 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 864 | |||
| 865 | ov_setting = {'some_setting': 1} |
||
| 866 | |||
| 867 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 868 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 869 | |||
| 870 | p.scan_id = '456-789' |
||
| 871 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 872 | p.prepare_alive_test_option_for_openvas() |
||
| 873 | |||
| 874 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 875 | |||
| 876 | def test_alive_test_methods_to_bit_field(self): |
||
| 877 | |||
| 878 | self.assertEqual( |
||
| 879 | AliveTest.ALIVE_TEST_TCP_ACK_SERVICE, |
||
| 880 | alive_test_methods_to_bit_field( |
||
| 881 | icmp=False, |
||
| 882 | tcp_ack=True, |
||
| 883 | tcp_syn=False, |
||
| 884 | arp=False, |
||
| 885 | consider_alive=False, |
||
| 886 | ), |
||
| 887 | ) |
||
| 888 | |||
| 889 | self.assertEqual( |
||
| 890 | AliveTest.ALIVE_TEST_ICMP, |
||
| 891 | alive_test_methods_to_bit_field( |
||
| 892 | icmp=True, |
||
| 893 | tcp_ack=False, |
||
| 894 | tcp_syn=False, |
||
| 895 | arp=False, |
||
| 896 | consider_alive=False, |
||
| 897 | ), |
||
| 898 | ) |
||
| 899 | |||
| 900 | self.assertEqual( |
||
| 901 | AliveTest.ALIVE_TEST_ARP, |
||
| 902 | alive_test_methods_to_bit_field( |
||
| 903 | icmp=False, |
||
| 904 | tcp_ack=False, |
||
| 905 | tcp_syn=False, |
||
| 906 | arp=True, |
||
| 907 | consider_alive=False, |
||
| 908 | ), |
||
| 909 | ) |
||
| 910 | |||
| 911 | self.assertEqual( |
||
| 912 | AliveTest.ALIVE_TEST_CONSIDER_ALIVE, |
||
| 913 | alive_test_methods_to_bit_field( |
||
| 914 | icmp=False, |
||
| 915 | tcp_ack=False, |
||
| 916 | tcp_syn=False, |
||
| 917 | arp=False, |
||
| 918 | consider_alive=True, |
||
| 919 | ), |
||
| 920 | ) |
||
| 921 | |||
| 922 | self.assertEqual( |
||
| 923 | AliveTest.ALIVE_TEST_TCP_SYN_SERVICE, |
||
| 924 | alive_test_methods_to_bit_field( |
||
| 925 | icmp=False, |
||
| 926 | tcp_ack=False, |
||
| 927 | tcp_syn=True, |
||
| 928 | arp=False, |
||
| 929 | consider_alive=False, |
||
| 930 | ), |
||
| 931 | ) |
||
| 932 | |||
| 933 | all_alive_test_methods = ( |
||
| 934 | AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT |
||
| 935 | | AliveTest.ALIVE_TEST_TCP_ACK_SERVICE |
||
| 936 | | AliveTest.ALIVE_TEST_ICMP |
||
| 937 | | AliveTest.ALIVE_TEST_ARP |
||
| 938 | | AliveTest.ALIVE_TEST_CONSIDER_ALIVE |
||
| 939 | | AliveTest.ALIVE_TEST_TCP_SYN_SERVICE |
||
| 940 | ) |
||
| 941 | self.assertEqual( |
||
| 942 | all_alive_test_methods, |
||
| 943 | alive_test_methods_to_bit_field( |
||
| 944 | icmp=True, |
||
| 945 | tcp_ack=True, |
||
| 946 | tcp_syn=True, |
||
| 947 | arp=True, |
||
| 948 | consider_alive=True, |
||
| 949 | ), |
||
| 951 |