Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add test for osp start_scan() #23

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gvm/protocols/ospv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
102 changes: 102 additions & 0 deletions tests/protocols/osp/test_osp_start_scan.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an int

Suggested change
parallel='10',
parallel=10,

scanner_params=scanner_params,
targets=targets,
vt_selection=vts
)

self.connection.send.has_been_called_with(
'<start_scan scan_id="123-456" parallel="10">'
'<scanner_params key1="value1"/>'
'<targets><target><hosts>localhost</hosts>'
'<ports>22,80</ports></target>'
'<target><hosts>192.168.10.1</hosts>'
'<ports>443</ports>'
'<credentials>'
'<credential port="port" service="smb" type="type">'
'<username>username</username>'
'<password>pass</password>'
'</credential></credentials>'
'</target></targets><vt_selection>'
'<vt_single id="vt1">'
'<vt_value id="value_id">value</vt_value></vt_single>'
'<vt_group filter="family=A"/><vt_group filter="family=B"/>'
'</vt_selection></start_scan>'
)

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
parallel='10',
parallel=10,

target="localhost",
ports="22"
)
self.connection.send.has_been_called_with(
'<start_scan scan_id="123-456" parallel="10" '
'target="localhost" ports="22">'
'<scanner_params/></start_scan>'
)


if __name__ == '__main__':
unittest.main()