From 2eabd929b59ccd16fda914801b627ebce4044485 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 6 Nov 2018 09:37:31 -0500 Subject: [PATCH 1/2] Fix add vt_selection element. Do not add this element if it is empty, since it is not mandatory for OSP, but it must not be empty if it is added. --- gvm/protocols/ospv1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gvm/protocols/ospv1.py b/gvm/protocols/ospv1.py index ff8a61362..dd1e36ef6 100644 --- a/gvm/protocols/ospv1.py +++ b/gvm/protocols/ospv1.py @@ -235,8 +235,8 @@ def start_scan(self, scan_id=None, parallel='1', target=None, raise RequiredArgument('start_scan requires a target. Please pass ' 'targets parameter.') - _xmlvtselection = cmd.add_element('vt_selection') if vt_selection: + _xmlvtselection = cmd.add_element('vt_selection') _xmlvtselection = create_vt_selection_element( _xmlvtselection, vt_selection) From 79dee07efac04e567d1017cd8e5669c02294f59a Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 6 Nov 2018 09:38:17 -0500 Subject: [PATCH 2/2] Add test for osp start_scan() --- tests/protocols/osp/test_osp_start_scan.py | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/protocols/osp/test_osp_start_scan.py diff --git a/tests/protocols/osp/test_osp_start_scan.py b/tests/protocols/osp/test_osp_start_scan.py new file mode 100644 index 000000000..5d7573fd8 --- /dev/null +++ b/tests/protocols/osp/test_osp_start_scan.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2018 Greenbone Networks GmbH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import unittest + +from gvm.errors import RequiredArgument +from gvm.protocols.ospv1 import Osp + +from .. import MockConnection + +class OSPStartScanTestCase(unittest.TestCase): + def setUp(self): + self.connection = MockConnection() + self.osp = Osp(self.connection) + + def test_start_scan(self): + scanner_params = dict() + scanner_params['key1'] = 'value1' + + targets = list() + _target1 = dict() + _target1['hosts'] = 'localhost' + _target1['ports'] = '22,80' + targets.append(_target1) + + _target2 = dict() + _target2['hosts'] = '192.168.10.1' + _target2['ports'] = '443' + _credential1 = dict() + _credential1 = {'smb': {'username': 'username', + 'password': 'pass', + 'port': 'port', + 'type': 'type'}} + _target2['credentials'] = _credential1 + targets.append(_target2) + + vts = dict() + vts['vt1'] = {'value_id': 'value'} + vts['vt_groups'] = ['family=A', 'family=B'] + + self.osp.start_scan( + scan_id='123-456', + parallel='10', + scanner_params=scanner_params, + targets=targets, + vt_selection=vts + ) + + self.connection.send.has_been_called_with( + '' + '' + 'localhost' + '22,80' + '192.168.10.1' + '443' + '' + '' + 'username' + 'pass' + '' + '' + '' + 'value' + '' + '' + ) + + def test_start_scan_without_target(self): + with self.assertRaises(RequiredArgument): + self.osp.start_scan() + + def test_start_scan_legacy(self): + self.osp.start_scan( + scan_id='123-456', + parallel='10', + target="localhost", + ports="22" + ) + self.connection.send.has_been_called_with( + '' + '' + ) + + +if __name__ == '__main__': + unittest.main()