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