From 0a3d0ac069c9eb53fc12adb4b617719a58f29b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 11:32:31 +0100 Subject: [PATCH 001/107] Add additional validation for create_schedule arguments Also update docstring about the expected ranges. --- gvm/protocols/gmpv7.py | 74 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 3ebc2435f..edd3dfe9d 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -24,6 +24,7 @@ https://docs.greenbone.net/API/GMP/gmp-7.0.html """ import logging +import numbers from lxml import etree @@ -1265,22 +1266,23 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, name (str): Name of the schedule comment (str, optional): Comment for the schedule first_time_minute (int, optional): First time minute the schedule - will run + will run. Must be an integer >= 0. first_time_hour (int, optional): First time hour the schedule - will run + will run. Must be an integer >= 0. first_time_day_of_month (int, optional): First time day of month the - schedule will run + schedule will run. Must be an integer > 0 <= 31. first_time_month (int, optional): First time month the schedule - will run + will run. Must be an integer >= 1 <= 12. first_time_year (int, optional): First time year the schedule - will run + will run. Must be an integer >= 1970. duration (int, optional): How long the Manager will run the scheduled task for until it gets paused if not finished yet. + Must be an integer > 0. duration_unit (str, optional): Unit of the duration. One of second, minute, hour, day, week, month, year, decade. Required if duration is set. period (int, optional): How often the Manager will repeat the - scheduled task + scheduled task. Must be an integer > 0. period_unit (str, optional): Unit of the period. One of second, minute, hour, day, week, month, year, decade. Required if period is set. @@ -1301,22 +1303,56 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, if first_time_minute or first_time_hour or first_time_day_of_month or \ first_time_month or first_time_year: - if not first_time_minute: + if first_time_minute is None: raise RequiredArgument( 'Setting first_time requires first_time_minute argument') - if not first_time_hour: + elif not isinstance(first_time_minute, numbers.Integral) or \ + first_time_minute < 0: + raise InvalidArgument( + 'first_time_minute argument of create_schedule needs to be ' + 'an integer greater or equal 0' + ) + + if first_time_hour is None: raise RequiredArgument( 'Setting first_time requires first_time_hour argument') - if not first_time_day_of_month: + elif not isinstance(first_time_hour, numbers.Integral) or \ + first_time_hour < 0: + raise InvalidArgument( + 'first_time_hour argument of create_schedule needs to be ' + 'an integer greater or equal 0' + ) + + if first_time_day_of_month is None: raise RequiredArgument( 'Setting first_time requires first_time_day_of_month ' 'argument') - if not first_time_month: + elif not isinstance(first_time_day_of_month, numbers.Integral) or \ + first_time_day_of_month < 1 or first_time_day_of_month > 31: + raise InvalidArgument( + 'first_time_day_of_month argument of create_schedule needs ' + 'to be an integer between 1 and 31' + ) + + if first_time_month is None: raise RequiredArgument( 'Setting first_time requires first_time_month argument') - if not first_time_year: + elif not isinstance(first_time_month, numbers.Integral) or \ + first_time_month < 1 or first_time_month > 12: + raise InvalidArgument( + 'first_time_month argument of create_schedule needs ' + 'to be an integer between 1 and 12' + ) + + if first_time_year is None: raise RequiredArgument( 'Setting first_time requires first_time_year argument') + elif not isinstance(first_time_year, numbers.Integral) or \ + first_time_year < 1970: + raise InvalidArgument( + 'first_time_year argument of create_schedule needs ' + 'to be an integer greater or equal 1970' + ) _xmlftime = cmd.add_element('first_time') _xmlftime.add_element('minute', str(first_time_minute)) @@ -1325,21 +1361,26 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, _xmlftime.add_element('month', str(first_time_month)) _xmlftime.add_element('year', str(first_time_year)) - if duration: + if duration is not None: if not duration_unit: raise RequiredArgument( 'Setting duration requires duration_unit argument') if not duration_unit in TIME_UNITS: raise InvalidArgument( - 'duration_unit must be one of {units} but {actual} has ' + 'duration_unit must be one of {units}. But {actual} has ' 'been passed'.format( units=', '.join(TIME_UNITS), actual=duration_unit)) + if not isinstance(duration, numbers.Integral) or duration < 1: + raise InvalidArgument( + 'duration argument must be an integer greater then 0', + ) + _xmlduration = cmd.add_element('duration', str(duration)) _xmlduration.add_element('unit', duration_unit) - if period: + if period is not None: if not period_unit: raise RequiredArgument( 'Setting period requires period_unit argument') @@ -1350,6 +1391,11 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, 'been passed'.format( units=', '.join(TIME_UNITS), actual=period_unit)) + if not isinstance(period, numbers.Integral) or period < 1: + raise InvalidArgument( + 'period argument must be an integer greater then 0', + ) + _xmlperiod = cmd.add_element('period', str(period)) _xmlperiod.add_element('unit', period_unit) From 904ff4f464c7df974dee5bb3ab688ddc71450cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 11:33:11 +0100 Subject: [PATCH 002/107] Add tests for create_schedule Gmp method --- tests/protocols/gmpv7/test_create_schedule.py | 409 ++++++++++++++++++ 1 file changed, 409 insertions(+) create mode 100644 tests/protocols/gmpv7/test_create_schedule.py diff --git a/tests/protocols/gmpv7/test_create_schedule.py b/tests/protocols/gmpv7/test_create_schedule.py new file mode 100644 index 000000000..516958d2b --- /dev/null +++ b/tests/protocols/gmpv7/test_create_schedule.py @@ -0,0 +1,409 @@ +# -*- 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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpCreateScheduleTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_create_schedule(self): + self.gmp.create_schedule( + name='foo', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + ) + + def test_create_schedule_missing_name(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name=None, + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='', + ) + + def test_create_schedule_with_comment(self): + self.gmp.create_schedule( + name='foo', + comment='bar' + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'bar' + '' + ) + + def test_create_schedule_with_first_time_missing_minute(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + first_time_hour=10, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_invalid_minute(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute='', + first_time_hour=1, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=-1, + first_time_hour=1, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_missing_hour(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=10, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_invalid_hour(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=10, + first_time_hour='', + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=10, + first_time_hour=-1, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_missing_day_of_month(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_invalid_day_of_month(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month='', + first_time_month=1, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=0, + first_time_month=1, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=-1, + first_time_month=1, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=32, + first_time_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_missing_month(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_invalid_month(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month='', + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=0, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=-1, + first_time_year=2020, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=13, + first_time_year=2020, + ) + + def test_create_schedule_with_first_time_missing_year(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=12, + ) + + def test_create_schedule_with_first_time_invalid_year(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=1, + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=0, + first_time_year=2020, + ) + + + def test_create_schedule_with_first_time(self): + self.gmp.create_schedule( + name='foo', + first_time_minute=0, + first_time_hour=0, + first_time_day_of_month=1, + first_time_month=1, + first_time_year=2020, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '0' + '0' + '1' + '1' + '2020' + '' + '' + ) + + + def test_create_schedule_invalid_duration(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + duration='bar', + duration_unit='day', + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + duration=0, + duration_unit='day', + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + duration=-1, + duration_unit='day', + ) + + def test_create_schedule_with_duration_missing_unit(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + duration=1, + ) + + def test_create_schedule_with_duration_invalid_unit(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + duration=1, + duration_unit='foo' + ) + + def test_create_schedule_with_duration(self): + self.gmp.create_schedule( + name='foo', + duration=1, + duration_unit='day', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '1' + 'day' + '' + '' + ) + + def test_create_schedule_with_period_missing_unit(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_schedule( + name='foo', + period=1, + ) + + def test_create_schedule_with_period_invalid_unit(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + period=1, + period_unit='foo' + ) + + def test_create_schedule_invalid_period(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + period='foo', + period_unit='day' + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + period=0, + period_unit='day' + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_schedule( + name='foo', + period=-1, + period_unit='day' + ) + + + def test_create_schedule_with_period(self): + self.gmp.create_schedule( + name='foo', + period=1, + period_unit='day', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '1' + 'day' + '' + '' + ) + + def test_create_schedule_with_timezone(self): + self.gmp.create_schedule( + name='foo', + timezone='foo', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'foo' + '' + ) + + +if __name__ == '__main__': + unittest.main() From c9341f5e14bfcc24c4c953fc821072a42f339902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 12:03:57 +0100 Subject: [PATCH 003/107] Update formatting in CHANGELOG --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70a8dcc11..fdea3a273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ * Fixed generating XML for `get_nvts` command * Fixed generating XML for `get_settings` command * Fixed generating XML for `get_credentials` command -* Renamed create_asset method to create_host and dropped asset_type parameter. It is - only possible to create host assets. +* Renamed create_asset method to create_host and dropped asset_type parameter. + It is only possible to create host assets. # python-gvm 1.0.0.beta2 (04.12.2018) From 89204a5def366ac9b0b7dabfb174fe2ad04242ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 12:04:19 +0100 Subject: [PATCH 004/107] Add changelog entry for create_schedule arguments validation --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdea3a273..919eb8ad6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fixed generating XML for `get_credentials` command * Renamed create_asset method to create_host and dropped asset_type parameter. It is only possible to create host assets. +* Updated and improved validation of `create_schedule` arguments # python-gvm 1.0.0.beta2 (04.12.2018) From 4ddd6d286b9b2f94092b203e6633555868719772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 12:04:41 +0100 Subject: [PATCH 005/107] Check if required arguments of create_tag are set --- gvm/protocols/gmpv7.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index edd3dfe9d..b569c784b 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1437,6 +1437,22 @@ def create_tag(self, name, resource_id, resource_type, *, value=None, Returns: The response. See :py:meth:`send_command` for details. """ + if not name: + raise RequiredArgument( + 'create_tag requires name argument' + ) + + if not resource_id: + raise RequiredArgument( + 'create_tag requires resource_id argument' + ) + + if not resource_type: + raise RequiredArgument( + 'create_tag requires resource_type argument' + ) + + cmd = XmlCommand('create_tag') cmd.add_element('name', name) _xmlresource = cmd.add_element('resource', From 00622a9e87a0ac45b71755cd9d05f7eea027ffef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 12:05:13 +0100 Subject: [PATCH 006/107] Add test for create_tag --- tests/protocols/gmpv7/test_create_tag.py | 166 +++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/protocols/gmpv7/test_create_tag.py diff --git a/tests/protocols/gmpv7/test_create_tag.py b/tests/protocols/gmpv7/test_create_tag.py new file mode 100644 index 000000000..63a1bb4c5 --- /dev/null +++ b/tests/protocols/gmpv7/test_create_tag.py @@ -0,0 +1,166 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + +class GmpCreateTagTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_create_tag_missing_name(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name=None, + resource_id='foo', + resource_type='task', + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name='', + resource_id='foo', + resource_type='task', + ) + + def test_create_tag_missing_resource_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name='foo', + resource_id=None, + resource_type='task', + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name='foo', + resource_id='', + resource_type='task', + ) + + def test_create_tag_missing_resource_type(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type=None, + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='', + ) + + def test_create_tag(self): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='task', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + 'task' + '' + '' + ) + + def test_create_tag_with_comment(self): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='task', + comment='bar', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + 'task' + '' + 'bar' + '' + ) + + def test_create_tag_with_value(self): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='task', + value='bar', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + 'task' + '' + 'bar' + '' + ) + + def test_create_tag_with_active(self): + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='task', + active=True, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + 'task' + '' + '1' + '' + ) + + self.gmp.create_tag( + name='foo', + resource_id='foo', + resource_type='task', + active=False, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + 'task' + '' + '0' + '' + ) + + +if __name__ == '__main__': + unittest.main() From 8380cba0b596278fe391186229406e09116d2803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 12:23:23 +0100 Subject: [PATCH 007/107] Update and simplify create task tests --- tests/protocols/gmpv7/test_create_task.py | 88 ++++++++++------------- 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/tests/protocols/gmpv7/test_create_task.py b/tests/protocols/gmpv7/test_create_task.py index 278105ea9..8555033eb 100644 --- a/tests/protocols/gmpv7/test_create_task.py +++ b/tests/protocols/gmpv7/test_create_task.py @@ -25,75 +25,65 @@ class GMPCreateTaskCommandTestCase(unittest.TestCase): - TASK_NAME = "important task" - CONFIG_ID = "cd0641e7-40b8-4e2c-811e-6b39d6d4b904" - TARGET_ID = '267a3405-e84a-47da-97b2-5fa0d2e8995e' - SCANNER_ID = 'b64c81b2-b9de-11e3-a2e9-406186ea4fc5' - ALERT_IDS = ['3ab38c6a-30ac-407a-98db-ad6e74c98b9a',] - COMMENT = 'this task has been created for test purposes' - def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) - def test_without_alert_correct_cmd(self): + def test_create_task(self): self.gmp.create_task( - self.TASK_NAME, self.CONFIG_ID, self.TARGET_ID, self.SCANNER_ID, - comment=self.COMMENT) + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + ) self.connection.send.has_been_called_with( '' - '{0}' - '' - '{1}' - ''.format(self.TASK_NAME, self.COMMENT, - self.CONFIG_ID, self.TARGET_ID, - self.SCANNER_ID) + 'foo' + '' + '' + '' + '' ) - def test_single_alert(self): + def test_create_task_single_alert(self): self.gmp.create_task( - self.TASK_NAME, self.CONFIG_ID, self.TARGET_ID, self.SCANNER_ID, - alert_ids=self.ALERT_IDS) + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + alert_ids=['a1'], + ) self.connection.send.has_been_called_with( '' - '{task}' - '' - '' - '' - '' - ''.format( - task=self.TASK_NAME, config=self.CONFIG_ID, - target=self.TARGET_ID, scanner=self.SCANNER_ID, - alert=self.ALERT_IDS[0]) + 'foo' + '' + '' + '' + '' + '' ) - def test_multiple_alerts(self): - alert_id2 = 'fb3d6f82-d706-4f99-9e53-d7d85257e25f' - alert_id3 = 'a33864a9-d3fd-44b3-8717-972bfb01dfcf' - alert_ids = self.ALERT_IDS[:] - alert_ids.extend([alert_id2, alert_id3]) - + def test_create_task_multiple_alerts(self): self.gmp.create_task( - self.TASK_NAME, self.CONFIG_ID, self.TARGET_ID, self.SCANNER_ID, - alert_ids=alert_ids) + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + alert_ids=['a1', 'a2', 'a3'], + ) self.connection.send.has_been_called_with( '' - '{task}' - '' - '' - '' - '' - '' - '' - ''.format( - task=self.TASK_NAME, config=self.CONFIG_ID, - target=self.TARGET_ID, scanner=self.SCANNER_ID, - alert1=alert_ids[0], - alert2=alert_ids[1], - alert3=alert_ids[2]) + 'foo' + '' + '' + '' + '' + '' + '' + '' ) From 8eadada77aed3749c73dc02a844e4ebe13d0dabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:05:19 +0100 Subject: [PATCH 008/107] Improve docstring and comment for create_task observer argument --- gvm/protocols/gmpv7.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index b569c784b..eae6e83f8 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1626,8 +1626,8 @@ def create_task(self, name, config_id, target_id, scanner_id, *, be run. schedule_periods (int, optional): A limit to the number of times the task will be scheduled, or 0 for no limit - observers (list, optional): List of user names which should be - allowed to observe this task + observers (list, optional): List of user names or user ids which + should be allowed to observe this task Returns: The response. See :py:meth:`send_command` for details. @@ -1683,7 +1683,10 @@ def create_task(self, name, config_id, target_id, scanner_id, *, cmd.add_element('schedule_periods', str(schedule_periods)) if observers: - cmd.add_element('observers', ' '.join(observers)) + # gvmd splits by command and space + # gvmd tries to lookup each value as user name and afterwards as + # user id. So both user name and user id are possible + cmd.add_element('observers', ','.join(observers)) return self._send_xml_command(cmd) From 57a3b5bcaf5212dd509ffabb27ab7b3c6465ff19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:05:56 +0100 Subject: [PATCH 009/107] Add comment about create_task hosts_ordering argument Maybe in future the values have to be checked... --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index eae6e83f8..1fcde1a1d 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1660,6 +1660,9 @@ def create_task(self, name, config_id, target_id, scanner_id, *, cmd.add_element('alterable', '0') if hosts_ordering: + # not sure about the possible values for hosts_orderning + # it seems gvmd doesn't check the param + # gsa allows to select 'sequential', 'random' or 'reverse' cmd.add_element('hosts_ordering', hosts_ordering) if alert_ids: From 30e4681e5c8b142375623a21881b5b07d77889ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:06:33 +0100 Subject: [PATCH 010/107] Check create_task schedule_period value Ensure schedule_period to be an integer greater or equal 0 --- gvm/protocols/gmpv7.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 1fcde1a1d..fc493546a 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1680,9 +1680,15 @@ def create_task(self, name, config_id, target_id, scanner_id, *, cmd.add_element('alert', attrs={'id': str(alert)}) if schedule_id: - cmd.add_element('schedule', schedule_id) - - if schedule_periods: + cmd.add_element('schedule', attrs={'id': schedule_id}) + + if schedule_periods is not None: + if not isinstance(schedule_periods, numbers.Integral) or \ + schedule_periods < 0: + raise InvalidArgument( + 'schedule_periods must be an integer greater or equal ' + 'then 0' + ) cmd.add_element('schedule_periods', str(schedule_periods)) if observers: From b4841c6b93675641852326a19165fda095431ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:07:27 +0100 Subject: [PATCH 011/107] Update tests for create_task --- tests/protocols/gmpv7/test_create_task.py | 262 ++++++++++++++++++++++ 1 file changed, 262 insertions(+) diff --git a/tests/protocols/gmpv7/test_create_task.py b/tests/protocols/gmpv7/test_create_task.py index 8555033eb..2029ffe86 100644 --- a/tests/protocols/gmpv7/test_create_task.py +++ b/tests/protocols/gmpv7/test_create_task.py @@ -18,6 +18,7 @@ import unittest +from gvm.errors import RequiredArgument, InvalidArgument from gvm.protocols.gmpv7 import Gmp from .. import MockConnection @@ -46,7 +47,112 @@ def test_create_task(self): '' ) + def test_create_task_missing_name(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name=None, + config_id='c1', + target_id='t1', + scanner_id='s1', + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='', + config_id='c1', + target_id='t1', + scanner_id='s1', + ) + + def test_create_task_missing_config_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id=None, + target_id='t1', + scanner_id='s1', + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id='', + target_id='t1', + scanner_id='s1', + ) + + def test_create_task_missing_target_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id=None, + scanner_id='s1', + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='', + scanner_id='s1', + ) + + def test_create_task_missing_scanner_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id=None, + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='', + ) + + def test_create_task_with_comment(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + comment='bar', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + 'bar' + '' + ) + def test_create_task_single_alert(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + alert_ids='a1', # will be removed in future + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '' + '' + ) + self.gmp.create_task( name='foo', config_id='c1', @@ -86,6 +192,162 @@ def test_create_task_multiple_alerts(self): '' ) + def test_create_task_with_alterable(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + alterable=True, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '1' + '' + ) + + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + alterable=False, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '0' + '' + ) + + def test_create_task_with_hosts_ordering(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + hosts_ordering='foo', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + 'foo' + '' + ) + + def test_create_task_with_schedule(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + schedule_id='s1', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '' + '' + ) + + def test_create_task_with_schedule_and_schedule_periods(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + schedule_id='s1', + schedule_periods=0, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '' + '0' + '' + ) + + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + schedule_id='s1', + schedule_periods=5, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + '' + '5' + '' + ) + + def test_create_task_with_schedule_and_invalid_schedule_periods(self): + with self.assertRaises(InvalidArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + schedule_id='s1', + schedule_periods='foo', + ) + + with self.assertRaises(InvalidArgument): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + schedule_id='s1', + schedule_periods=-1, + ) + + def test_create_task_with_observers(self): + self.gmp.create_task( + name='foo', + config_id='c1', + target_id='t1', + scanner_id='s1', + observers=['u1', 'u2'], + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + 'u1,u2' + '' + ) + if __name__ == '__main__': unittest.main() From b119e5b7f079c231a9518f56fc06c457f5ecee98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:37:57 +0100 Subject: [PATCH 012/107] Don't add extra spaces for create_user hosts and ifaces --- gvm/protocols/gmpv7.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index fc493546a..88be40d66 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1744,11 +1744,11 @@ def create_user(self, name, *, password=None, hosts=None, hosts_allow=False, cmd.add_element('password', password) if hosts: - cmd.add_element('hosts', ', '.join(hosts), + cmd.add_element('hosts', ','.join(hosts), attrs={'allow': '1' if hosts_allow else '0'}) if ifaces: - cmd.add_element('ifaces', ', '.join(ifaces), + cmd.add_element('ifaces', ','.join(ifaces), attrs={'allow': '1' if ifaces_allow else '0'}) if role_ids: From 998ed3664e95e06f8ffba693c7173e0df0b6cd7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:38:39 +0100 Subject: [PATCH 013/107] Add tests for create_user gmp command --- tests/protocols/gmpv7/test_create_user.py | 161 ++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 tests/protocols/gmpv7/test_create_user.py diff --git a/tests/protocols/gmpv7/test_create_user.py b/tests/protocols/gmpv7/test_create_user.py new file mode 100644 index 000000000..9a7015386 --- /dev/null +++ b/tests/protocols/gmpv7/test_create_user.py @@ -0,0 +1,161 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + +class GmpCreateUserTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_create_user_missing_name(self): + with self.assertRaises(RequiredArgument): + self.gmp.create_user( + name=None, + ) + + with self.assertRaises(RequiredArgument): + self.gmp.create_user( + name='', + ) + + def test_create_user(self): + self.gmp.create_user( + name='foo', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + ) + + def test_create_user_with_password(self): + self.gmp.create_user( + name='foo', + password='bar', + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'bar' + '' + ) + + def test_create_user_with_hosts(self): + self.gmp.create_user( + name='foo', + hosts=['h1', 'h2'], + hosts_allow=True, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + self.gmp.create_user( + name='foo', + hosts=['h1', 'h2'], + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + self.gmp.create_user( + name='foo', + hosts=['h1', 'h2'], + hosts_allow=False, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + def test_create_user_with_ifaces(self): + self.gmp.create_user( + name='foo', + ifaces=['h1', 'h2'], + ifaces_allow=True, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + self.gmp.create_user( + name='foo', + ifaces=['h1', 'h2'], + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + self.gmp.create_user( + name='foo', + ifaces=['h1', 'h2'], + ifaces_allow=False, + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + 'h1,h2' + '' + ) + + def test_create_user_with_role_ids(self): + self.gmp.create_user( + name='foo', + role_ids=['r1', 'r2'], + ) + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + '' + '' + ) + + +if __name__ == '__main__': + unittest.main() From 2d4e9bbdd3000edb14c9fb7263e7f171e7542951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:41:09 +0100 Subject: [PATCH 014/107] Add tests for delete_alert gmp command --- tests/protocols/gmpv7/test_delete_alert.py | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/protocols/gmpv7/test_delete_alert.py diff --git a/tests/protocols/gmpv7/test_delete_alert.py b/tests/protocols/gmpv7/test_delete_alert.py new file mode 100644 index 000000000..a1975b5d2 --- /dev/null +++ b/tests/protocols/gmpv7/test_delete_alert.py @@ -0,0 +1,53 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + +class GmpDeleteAlertTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_delete(self): + self.gmp.delete_alert('a1') + + self.connection.send.has_been_called_with( + '') + + def test_delete_ultimate(self): + self.gmp.delete_alert('a1', ultimate=True) + + self.connection.send.has_been_called_with( + '') + + def test_missing_alert_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.delete_alert(None) + + with self.assertRaises(RequiredArgument): + self.gmp.delete_alert('') + + +if __name__ == '__main__': + unittest.main() From 8c50615dd335cc6d289ba899e65f35c7cf4463ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:53:39 +0100 Subject: [PATCH 015/107] Raise RequiredArgument exception for missing agent_id in get_agent --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 88be40d66..0c4abc888 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2231,6 +2231,11 @@ def get_agent(self, agent_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not agent_id: + raise RequiredArgument( + 'get_agent requires an agent_id argument' + ) + cmd = XmlCommand('get_agents') cmd.set_attribute('agent_id', agent_id) From 586d9b96a14203986e4999347a4a2a6ea045bb9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:54:03 +0100 Subject: [PATCH 016/107] Add tests for get_agent --- tests/protocols/gmpv7/test_get_agent.py | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_agent.py diff --git a/tests/protocols/gmpv7/test_get_agent.py b/tests/protocols/gmpv7/test_get_agent.py new file mode 100644 index 000000000..71b6ebc54 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_agent.py @@ -0,0 +1,49 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAgentTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_agent_missing_agent_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_agent(agent_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_agent(agent_id='') + + def test_get_agent(self): + self.gmp.get_agent(agent_id='agent_id') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 128fdac68469490fc3e97a0c39cf8bf4dcb683d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 18 Dec 2018 16:54:15 +0100 Subject: [PATCH 017/107] Add tests for get_agents --- tests/protocols/gmpv7/test_get_agents.py | 91 ++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_agents.py diff --git a/tests/protocols/gmpv7/test_get_agents.py b/tests/protocols/gmpv7/test_get_agents.py new file mode 100644 index 000000000..c86440c1d --- /dev/null +++ b/tests/protocols/gmpv7/test_get_agents.py @@ -0,0 +1,91 @@ +# -*- 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 InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAgentsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_agents(self): + self.gmp.get_agents() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_agents_with_trash(self): + self.gmp.get_agents(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_agents(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_agents_with_details(self): + self.gmp.get_agents(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_agents(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_agents_with_format(self): + self.gmp.get_agents(format='installer') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_agents(format='howto_install') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_agents(format='howto_use') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_agents_invalid_format(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_agents(format='foo') + + +if __name__ == '__main__': + unittest.main() From ed60dae23067a73c7efc5336cc376b93579179d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:39:32 +0100 Subject: [PATCH 018/107] Raise exception if alert_id is missing for get_alert --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 0c4abc888..cd0a13475 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2284,6 +2284,9 @@ def get_alert(self, alert_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not alert_id: + raise RequiredArgument('get_alert requires an alert_id argument') + cmd = XmlCommand('get_alerts') cmd.set_attribute('alert_id', alert_id) return self._send_xml_command(cmd) From 88ce7b883e87b3d16bda49a58442ca77cf586c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:40:33 +0100 Subject: [PATCH 019/107] Add tests for get_alert command --- tests/protocols/gmpv7/test_get_alert.py | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_alert.py diff --git a/tests/protocols/gmpv7/test_get_alert.py b/tests/protocols/gmpv7/test_get_alert.py new file mode 100644 index 000000000..3a11e898c --- /dev/null +++ b/tests/protocols/gmpv7/test_get_alert.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAlertTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_alert(self): + self.gmp.get_alert('a1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_alert(alert_id='a1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_alert_invalid_alert_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_alert(alert_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_alert(alert_id='') + + +if __name__ == '__main__': + unittest.main() From 2d3d4f3e19de46930841445e0a40660c7bf01785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:40:45 +0100 Subject: [PATCH 020/107] Add tests for get_alerts command --- tests/protocols/gmpv7/test_get_alerts.py | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_alerts.py diff --git a/tests/protocols/gmpv7/test_get_alerts.py b/tests/protocols/gmpv7/test_get_alerts.py new file mode 100644 index 000000000..6a8b59a50 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_alerts.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAlertsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_alerts(self): + self.gmp.get_alerts() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_alerts_with_trash(self): + self.gmp.get_alerts(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_alerts(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_alerts_with_filter(self): + self.gmp.get_alerts(filter="foo=bar") + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_alerts_with_filter_id(self): + self.gmp.get_alerts(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_alerts_with_tasks(self): + self.gmp.get_alerts(tasks=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_alerts(tasks=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 887ec30899875f305404079a5c79137e6f1abf5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:47:51 +0100 Subject: [PATCH 021/107] Add asset_type before filter arguments to get_assets xml --- gvm/protocols/gmpv7.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index cd0a13475..db1d11341 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2308,10 +2308,10 @@ def get_assets(self, asset_type, *, filter=None, filter_id=None): cmd = XmlCommand('get_assets') - _add_filter(cmd, filter, filter_id) - cmd.set_attribute('type', asset_type) + _add_filter(cmd, filter, filter_id) + return self._send_xml_command(cmd) def get_asset(self, asset_id, asset_type): From aba198aca5f6dffc30debb55200a40a80663361d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:48:28 +0100 Subject: [PATCH 022/107] Add tests for get_assets command --- tests/protocols/gmpv7/test_get_assets.py | 77 ++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_assets.py diff --git a/tests/protocols/gmpv7/test_get_assets.py b/tests/protocols/gmpv7/test_get_assets.py new file mode 100644 index 000000000..0b2c5b418 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_assets.py @@ -0,0 +1,77 @@ +# -*- 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 InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAssetsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_assets(self): + self.gmp.get_assets('os') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_assets(asset_type='os') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_assets(asset_type='host') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_assets_invalid_asset_type(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_assets(asset_type=None) + + with self.assertRaises(InvalidArgument): + self.gmp.get_assets(asset_type='') + with self.assertRaises(InvalidArgument): + self.gmp.get_assets(asset_type='foo') + + def test_get_assets_with_filter(self): + self.gmp.get_assets(asset_type='os', filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_assets_with_filter_id(self): + self.gmp.get_assets(asset_type='os', filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 2caf94f3072398b34defd29ff0cd814a2649e858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:57:11 +0100 Subject: [PATCH 023/107] Extract possible asset types into a tuple constant --- gvm/protocols/gmpv7.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index db1d11341..14ef04d2b 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -138,6 +138,11 @@ 'Email', ) +ASSET_TYPES = ( + 'host', + 'os', +) + def _check_command_status(xml): """Check gmp response @@ -2303,7 +2308,7 @@ def get_assets(self, asset_type, *, filter=None, filter_id=None): Returns: The response. See :py:meth:`send_command` for details. """ - if not asset_type in ('os', 'host'): + if not asset_type in ASSET_TYPES: raise InvalidArgument('asset_type must be either os or host') cmd = XmlCommand('get_assets') @@ -2324,7 +2329,7 @@ def get_asset(self, asset_id, asset_type): Returns: The response. See :py:meth:`send_command` for details. """ - if not asset_type in ('os', 'host'): + if not asset_type in ASSET_TYPES: raise InvalidArgument('asset_type must be either os or host') cmd = XmlCommand('get_assets') From c3acf2e4d67e2d7f5ac3d2d7f635e0e0a40a172b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:57:28 +0100 Subject: [PATCH 024/107] Raise exception if asset_id is missing for get_asset --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 14ef04d2b..76e249174 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2332,6 +2332,9 @@ def get_asset(self, asset_id, asset_type): if not asset_type in ASSET_TYPES: raise InvalidArgument('asset_type must be either os or host') + if not asset_id: + raise RequiredArgument('get_asset requires an asset_type argument') + cmd = XmlCommand('get_assets') cmd.set_attribute('asset_id', asset_id) cmd.set_attribute('type', asset_type) From 1023ac73f1ea2b52a40cdaf49fb2a89fffef35ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 11:57:47 +0100 Subject: [PATCH 025/107] Add tests for get_asset method --- tests/protocols/gmpv7/test_get_asset.py | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_asset.py diff --git a/tests/protocols/gmpv7/test_get_asset.py b/tests/protocols/gmpv7/test_get_asset.py new file mode 100644 index 000000000..d836f425c --- /dev/null +++ b/tests/protocols/gmpv7/test_get_asset.py @@ -0,0 +1,77 @@ +# -*- 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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetAssetTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_asset_host(self): + self.gmp.get_asset('a1', 'host') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_asset(asset_id='a1', asset_type='host') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_asset_os(self): + self.gmp.get_asset('a1', 'os') + + self.connection.send.has_been_called_with( + '' + ) + self.gmp.get_asset(asset_id='a1', asset_type='os') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_asset_missing_asset_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_asset(asset_id=None, asset_type='host') + + with self.assertRaises(RequiredArgument): + self.gmp.get_asset(asset_id='', asset_type='os') + + def test_get_asset_invalid_asset_type(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_asset(asset_id='a1', asset_type='foo') + + with self.assertRaises(InvalidArgument): + self.gmp.get_asset(asset_id='a1', asset_type=None) + + with self.assertRaises(InvalidArgument): + self.gmp.get_asset(asset_id='a1', asset_type='') + + +if __name__ == '__main__': + unittest.main() From 92548275d3b7f20374eeb090c8227dfda7f27c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:19:04 +0100 Subject: [PATCH 026/107] Remove the format parameter of get_credentials method It shouldn't be necessary to export several credentials in aspecific format in one request. A user should request a format for only one credential at once. --- CHANGELOG.md | 1 + gvm/protocols/gmpv7.py | 10 +--------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 919eb8ad6..443008a97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Renamed create_asset method to create_host and dropped asset_type parameter. It is only possible to create host assets. * Updated and improved validation of `create_schedule` arguments +* Removed the format parameter from `get_credentials` method # python-gvm 1.0.0.beta2 (04.12.2018) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 76e249174..e086b93cf 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2342,7 +2342,7 @@ def get_asset(self, asset_id, asset_type): return self._send_xml_command(cmd) def get_credentials(self, *, filter=None, filter_id=None, scanners=None, - trash=None, targets=None, format=None): + trash=None, targets=None): """Request a list of credentials Arguments: @@ -2355,7 +2355,6 @@ def get_credentials(self, *, filter=None, filter_id=None, scanners=None, instead targets (boolean, optional): Whether to include a list of targets using the credentials - format (str, optional): One of "key", "rpm", "deb" or "exe" Returns: The response. See :py:meth:`send_command` for details. @@ -2373,13 +2372,6 @@ def get_credentials(self, *, filter=None, filter_id=None, scanners=None, if not targets is None: cmd.set_attribute('targets', _to_bool(targets)) - if format: - if not format in ('key', 'rpm', 'deb', 'exe'): - raise InvalidArgument( - 'format argument needs to one of key, rpm, deb or exe') - - cmd.set_attribute('format', format) - return self._send_xml_command(cmd) def get_credential(self, credential_id, *, credential_format=None): From 5b454a3e2c0659487833d729c3fee79b0ac4b66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:25:56 +0100 Subject: [PATCH 027/107] Add additional tests for get_credentials --- tests/protocols/gmpv7/test_get_credentials.py | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/protocols/gmpv7/test_get_credentials.py b/tests/protocols/gmpv7/test_get_credentials.py index 6d86a9ca6..09030e1ba 100644 --- a/tests/protocols/gmpv7/test_get_credentials.py +++ b/tests/protocols/gmpv7/test_get_credentials.py @@ -28,12 +28,66 @@ def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) - def test_get_credentials_simple(self): + def test_get_credential(self): self.gmp.get_credentials() self.connection.send.has_been_called_with( '') + def test_get_credentials_with_filter(self): + self.gmp.get_credentials(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_credentials_with_filter_id(self): + self.gmp.get_credentials(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_credentials_with_scanners(self): + self.gmp.get_credentials(scanners=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_credentials(scanners=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_credentials_with_trash(self): + self.gmp.get_credentials(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_credentials(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_credentials_with_targets(self): + self.gmp.get_credentials(targets=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_credentials(targets=False) + + self.connection.send.has_been_called_with( + '' + ) + + if __name__ == '__main__': unittest.main() From 8eb0ad501e124d7b55ee9da96585fd944c6cd1b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:28:51 +0100 Subject: [PATCH 028/107] Update get_credential tests Rename test methods and add some corner cases. --- tests/protocols/gmpv7/test_get_credential.py | 33 +++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/protocols/gmpv7/test_get_credential.py b/tests/protocols/gmpv7/test_get_credential.py index 8823f45a5..5c16ff9c9 100644 --- a/tests/protocols/gmpv7/test_get_credential.py +++ b/tests/protocols/gmpv7/test_get_credential.py @@ -29,27 +29,50 @@ def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) - def test_get_credential_simple(self): + def test_get_credential(self): self.gmp.get_credential('id') self.connection.send.has_been_called_with( '') - def test_fail_without_credential_id(self): + def test_get_credentials_missing_credential_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_credential(None) - def test_fail_with_empty_credential_id(self): + def test_get_credentials_invalid_credential_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_credential(credential_id=None) + with self.assertRaises(RequiredArgument): self.gmp.get_credential('') - def test_get_credential_with_valid_format(self): + def test_get_credential_with_credential_format(self): self.gmp.get_credential('id', credential_format='key') self.connection.send.has_been_called_with( '') - def test_get_credential_with_invalid_format(self): + self.gmp.get_credential('id', credential_format='rpm') + + self.connection.send.has_been_called_with( + '') + + self.gmp.get_credential('id', credential_format='deb') + + self.connection.send.has_been_called_with( + '') + + self.gmp.get_credential('id', credential_format='exe') + + self.connection.send.has_been_called_with( + '') + + self.gmp.get_credential('id', credential_format='pem') + + self.connection.send.has_been_called_with( + '') + + def test_get_credential_with_invalid_credential_format(self): with self.assertRaises(InvalidArgument): self.gmp.get_credential('id', credential_format='foo') From 4e288d5d3e9831abce15e873c01e42b20d074de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:41:45 +0100 Subject: [PATCH 029/107] Require feed_type as argument for get_feed Don't crash if feed_type is None. --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index e086b93cf..700f7d0c4 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2478,6 +2478,9 @@ def get_feed(self, feed_type): Returns: The response. See :py:meth:`send_command` for details. """ + if not feed_type: + raise RequiredArgument('get_feed requires a feed_type argument') + feed_type = feed_type.upper() if not feed_type in ('NVT', 'CERT', 'SCAP'): From 4bbbdb143c0d636171033870f4885c9645e8f1dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:42:20 +0100 Subject: [PATCH 030/107] Add tests for get_feed command --- tests/protocols/gmpv7/test_get_feed.py | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_feed.py diff --git a/tests/protocols/gmpv7/test_get_feed.py b/tests/protocols/gmpv7/test_get_feed.py new file mode 100644 index 000000000..8b2353eed --- /dev/null +++ b/tests/protocols/gmpv7/test_get_feed.py @@ -0,0 +1,70 @@ +# -*- 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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + +class GmpGetFeedTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_feed(self): + self.gmp.get_feed('nvt') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_feed(feed_type='nvt') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_feed('cert') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_feed('scap') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_feed_missing_feed_type(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_feed(feed_type=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_feed('') + + def test_get_feed_invalid_feed_type(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_feed(feed_type='foo') + + +if __name__ == '__main__': + unittest.main() From ef811839bc92fd8cba72bc8b1fca4596562398c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:42:32 +0100 Subject: [PATCH 031/107] Add test for get_feeds command --- tests/protocols/gmpv7/test_get_feeds.py | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_feeds.py diff --git a/tests/protocols/gmpv7/test_get_feeds.py b/tests/protocols/gmpv7/test_get_feeds.py new file mode 100644 index 000000000..9ec072e8f --- /dev/null +++ b/tests/protocols/gmpv7/test_get_feeds.py @@ -0,0 +1,40 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + +class GmpGetFeedsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_feeds(self): + self.gmp.get_feeds() + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From bf12003d25a135db676689d495c5a63e27763e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 12:46:48 +0100 Subject: [PATCH 032/107] Add tests for get_filters --- tests/protocols/gmpv7/test_get_filters.py | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_filters.py diff --git a/tests/protocols/gmpv7/test_get_filters.py b/tests/protocols/gmpv7/test_get_filters.py new file mode 100644 index 000000000..bb325f330 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_filters.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetFiltersTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_filters(self): + self.gmp.get_filters() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_filters_with_filter(self): + self.gmp.get_filters(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_filters_with_filter_id(self): + self.gmp.get_filters(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_filters_with_trash(self): + self.gmp.get_filters(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_filters(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_filters_with_alerts(self): + self.gmp.get_filters(alerts=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_filters(alerts=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 55708c21704793696dc189ff8f0a75b512c8fb20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 13:01:52 +0100 Subject: [PATCH 033/107] Require filter_id argument for get_filter --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 700f7d0c4..20e5923e8 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2529,6 +2529,9 @@ def get_filter(self, filter_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not filter_id: + raise RequiredArgument('get_filter requires a filter_id argument') + cmd = XmlCommand('get_filters') cmd.set_attribute('filter_id', filter_id) return self._send_xml_command(cmd) From 94d009741337495fa04bd35d3bfc71a263ca9830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 13:02:05 +0100 Subject: [PATCH 034/107] Add tests for get_filter --- tests/protocols/gmpv7/test_get_filter.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_filter.py diff --git a/tests/protocols/gmpv7/test_get_filter.py b/tests/protocols/gmpv7/test_get_filter.py new file mode 100644 index 000000000..a3b544cd3 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_filter.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetFilterTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_filter(self): + self.gmp.get_filter('f1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_filter(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_filter_missing_filter_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_filter(filter_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_filter('') + + +if __name__ == '__main__': + unittest.main() From 824813c549014e24a17dcc1a1d184bf8ff427aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 14:08:03 +0100 Subject: [PATCH 035/107] Require group_id for get_group method --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 20e5923e8..d2e58c171 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2567,6 +2567,9 @@ def get_group(self, group_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not group_id: + raise RequiredArgument('get_group requires a group_id argument') + cmd = XmlCommand('get_groups') cmd.set_attribute('group_id', group_id) return self._send_xml_command(cmd) From 7409f6f176667ce97df9f1e3473cfd7fb336211e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 14:08:20 +0100 Subject: [PATCH 036/107] Add tests for get_group --- tests/protocols/gmpv7/test_get_group.py | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_group.py diff --git a/tests/protocols/gmpv7/test_get_group.py b/tests/protocols/gmpv7/test_get_group.py new file mode 100644 index 000000000..d1f791366 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_group.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetGroupTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_group(self): + self.gmp.get_group('f1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_group(group_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_group_missing_group_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_group(group_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_group('') + + +if __name__ == '__main__': + unittest.main() From 68441d309699e9e4c8afda0b9d814a0f6a5ffe35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 14:08:32 +0100 Subject: [PATCH 037/107] Add tests for get_groups --- tests/protocols/gmpv7/test_get_groups.py | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_groups.py diff --git a/tests/protocols/gmpv7/test_get_groups.py b/tests/protocols/gmpv7/test_get_groups.py new file mode 100644 index 000000000..51f60897a --- /dev/null +++ b/tests/protocols/gmpv7/test_get_groups.py @@ -0,0 +1,68 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetGroupsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_groups(self): + self.gmp.get_groups() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_groups_with_filter(self): + self.gmp.get_groups(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_groups_with_filter_id(self): + self.gmp.get_groups(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_groups_with_trash(self): + self.gmp.get_groups(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_groups(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 7a2b4a817b12448bacb206fc1e16424f7d5fff26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 14:08:49 +0100 Subject: [PATCH 038/107] Extract list of info types as a tuple constant --- gvm/protocols/gmpv7.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index d2e58c171..8e8e41089 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -143,6 +143,16 @@ 'os', ) +INFO_TYPES = ( + 'CERT_BUND_ADV', + 'CPE', + 'CVE', + 'DFN_CERT_ADV', + 'OVALDEF', + 'NVT', + 'ALLINFO', +) + def _check_command_status(xml): """Check gmp response @@ -2594,9 +2604,7 @@ def get_info_list(self, info_type, *, filter=None, filter_id=None, """ info_type = info_type.upper() - if not info_type in ( - 'CERT_BUND_ADV', 'CPE', 'CVE', 'DFN_CERT_ADV', 'OVALDEF', 'NVT', - 'ALLINFO'): + if not info_type in INFO_TYPES: raise InvalidArgument( 'get_info_list info_type argument must be one of CERT_BUND_ADV' ', CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO') @@ -2626,9 +2634,7 @@ def get_info(self, info_id, info_type): Returns: The response. See :py:meth:`send_command` for details. """ - if not info_type in ( - 'CERT_BUND_ADV', 'CPE', 'CVE', 'DFN_CERT_ADV', 'OVALDEF', 'NVT', - 'ALLINFO'): + if not info_type in INFO_TYPES: raise InvalidArgument( 'get_info_list info_type argument must be one of CERT_BUND_ADV' ', CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO') From 5874f5d4b034722c50ebcfb0203b19354abaf195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:18:32 +0100 Subject: [PATCH 039/107] Require info_type argument for get_info_list Don't crash if info_type is None. --- gvm/protocols/gmpv7.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 8e8e41089..35d721cc1 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2602,6 +2602,10 @@ def get_info_list(self, info_type, *, filter=None, filter_id=None, Returns: The response. See :py:meth:`send_command` for details. """ + if not info_type: + raise RequiredArgument( + 'get_info_list requires an info_type argument') + info_type = info_type.upper() if not info_type in INFO_TYPES: From 390df131cc8426c80cb23b0ef1f7d1e54327102a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:18:56 +0100 Subject: [PATCH 040/107] Add type before filter to get_info xml command for get_info_list --- gvm/protocols/gmpv7.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 35d721cc1..58e99fe99 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2615,10 +2615,10 @@ def get_info_list(self, info_type, *, filter=None, filter_id=None, cmd = XmlCommand('get_info') - _add_filter(cmd, filter, filter_id) - cmd.set_attribute('type', info_type) + _add_filter(cmd, filter, filter_id) + if name: cmd.set_attribute('name', name) From cca1acff9ad15e610118a8863c68fa0c1429a76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:19:20 +0100 Subject: [PATCH 041/107] Require info_type and info_id for get_info --- gvm/protocols/gmpv7.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 58e99fe99..cd74c9d3f 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2638,11 +2638,21 @@ def get_info(self, info_id, info_type): Returns: The response. See :py:meth:`send_command` for details. """ + if not info_type: + raise RequiredArgument( + 'get_info requires an info_type argument') + + info_type = info_type.upper() + if not info_type in INFO_TYPES: raise InvalidArgument( - 'get_info_list info_type argument must be one of CERT_BUND_ADV' + 'get_info info_type argument must be one of CERT_BUND_ADV' ', CPE, CVE, DFN_CERT_ADV, OVALDEF, NVT or ALLINFO') + if not info_id: + raise RequiredArgument( + 'get_info requires an info_id argument') + cmd = XmlCommand('get_info') cmd.set_attribute('info_id', info_id) From 5387197598cba681b6f5dfeb70534abf695dba0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:20:04 +0100 Subject: [PATCH 042/107] Add tests for get_info --- tests/protocols/gmpv7/test_get_info.py | 102 +++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_info.py diff --git a/tests/protocols/gmpv7/test_get_info.py b/tests/protocols/gmpv7/test_get_info.py new file mode 100644 index 000000000..2bc907237 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_info.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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetInfoTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_info(self): + self.gmp.get_info(info_type='cert_bund_adv', info_id='i1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'cpe') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'cve') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'dfn_cert_adv') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'ovaldef') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'nvt') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info('i1', 'allinfo') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_info_missing_info_type(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_info(info_id='i1', info_type=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_info(info_id='i1', info_type='') + + with self.assertRaises(RequiredArgument): + self.gmp.get_info('i1', '') + + def test_get_info_invalid_info_type(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_info(info_id='i1', info_type='foo') + + def test_get_info_missing_info_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_info(info_id='', info_type='cpe') + + with self.assertRaises(RequiredArgument): + self.gmp.get_info('', info_type='cpe') + + with self.assertRaises(RequiredArgument): + self.gmp.get_info(info_id=None, info_type='cpe') + + +if __name__ == '__main__': + unittest.main() From 33675f80fbb317f86100ee7d52fde214d2828e02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:20:13 +0100 Subject: [PATCH 043/107] Add tests for get_info_list --- tests/protocols/gmpv7/test_get_info_list.py | 132 ++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_info_list.py diff --git a/tests/protocols/gmpv7/test_get_info_list.py b/tests/protocols/gmpv7/test_get_info_list.py new file mode 100644 index 000000000..839aee008 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_info_list.py @@ -0,0 +1,132 @@ +# -*- 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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetInfoListTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_info_list(self): + self.gmp.get_info_list('cert_bund_adv') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('cpe') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list(info_type='cpe') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('cve') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('dfn_cert_adv') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('ovaldef') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('nvt') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list('allinfo') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_info_list_missing_info_type(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_info_list(info_type=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_info_list(info_type='') + + with self.assertRaises(RequiredArgument): + self.gmp.get_info_list('') + + def test_get_info_list_invalid_info_type(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_info_list(info_type='foo') + + def test_get_info_list_with_filter(self): + self.gmp.get_info_list('cpe', filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_info_list_with_filter_id(self): + self.gmp.get_info_list(info_type='cpe', filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_info_list_with_name(self): + self.gmp.get_info_list(info_type='cpe', name='foo') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_info_list_with_details(self): + self.gmp.get_info_list(info_type='cpe', details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_info_list(info_type='cpe', details=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 90a21c6843c3f455860ddd760fd2897e1da2aa1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:41:11 +0100 Subject: [PATCH 044/107] Remove filter_id and nvt_oid parameters Both parameters are inconsistent again. We should use filters for limiting notes like filter='task_id=foo'. --- CHANGELOG.md | 1 + gvm/protocols/gmpv7.py | 12 ++---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 443008a97..2e2882c08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ It is only possible to create host assets. * Updated and improved validation of `create_schedule` arguments * Removed the format parameter from `get_credentials` method +* Removed the task_id and nvt_oid parameter from `get_notes` method # python-gvm 1.0.0.beta2 (04.12.2018) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index cd74c9d3f..788e45a5a 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2662,16 +2662,14 @@ def get_info(self, info_id, info_type): cmd.set_attribute('details', '1') return self._send_xml_command(cmd) - def get_notes(self, *, filter=None, filter_id=None, nvt_oid=None, - task_id=None, details=None, result=None): + def get_notes(self, *, filter=None, filter_id=None, details=None, + result=None): """Request a list of notes Arguments: filter (str, optional): Filter term to use for the query filter_id (str, optional): UUID of an existing filter to use for the query - nvt_oid (str, optional): OID of a nvt - task_id (str, optional): UUID of a task details (boolean, optional): result (boolean, optional): @@ -2682,12 +2680,6 @@ def get_notes(self, *, filter=None, filter_id=None, nvt_oid=None, _add_filter(cmd, filter, filter_id) - if nvt_oid: - cmd.set_attribute('nvt_oid', nvt_oid) - - if task_id: - cmd.set_attribute('task_id', task_id) - if not details is None: cmd.set_attribute('details', _to_bool(details)) From 7de82d7bb584ecbe303f413c02b10da278db24da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:43:32 +0100 Subject: [PATCH 045/107] Update docstrings for details and result arguments for get_notes --- gvm/protocols/gmpv7.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 788e45a5a..ec1f6a752 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2670,8 +2670,10 @@ def get_notes(self, *, filter=None, filter_id=None, details=None, filter (str, optional): Filter term to use for the query filter_id (str, optional): UUID of an existing filter to use for the query - details (boolean, optional): - result (boolean, optional): + details (boolean, optional): Add info about connected results and + tasks + result (boolean, optional): Return the details of possible connected + results. Returns: The response. See :py:meth:`send_command` for details. From b6357032e024a8d31588d9e448d7605b7203c44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:44:05 +0100 Subject: [PATCH 046/107] Require note_id argument for get_note --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index ec1f6a752..441bb550b 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2699,6 +2699,11 @@ def get_note(self, note_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not note_id: + raise RequiredArgument( + 'get_note requires a note_id argument' + ) + cmd = XmlCommand('get_notes') cmd.set_attribute('note_id', note_id) From abf1e415be119850ac464906f726ea91efebcb43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:47:48 +0100 Subject: [PATCH 047/107] Add tests for get_note --- tests/protocols/gmpv7/test_get_note.py | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_note.py diff --git a/tests/protocols/gmpv7/test_get_note.py b/tests/protocols/gmpv7/test_get_note.py new file mode 100644 index 000000000..28e49987a --- /dev/null +++ b/tests/protocols/gmpv7/test_get_note.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetNoteTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_note(self): + self.gmp.get_note('n1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_note(note_id='n1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_note_missing_note_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_note(note_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_note('') + + +if __name__ == '__main__': + unittest.main() From d3a58eb9c37f46f2dbd70dadec5ff6e3d89239ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:48:02 +0100 Subject: [PATCH 048/107] Add tests for get_notes --- tests/protocols/gmpv7/test_get_notes.py | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_notes.py diff --git a/tests/protocols/gmpv7/test_get_notes.py b/tests/protocols/gmpv7/test_get_notes.py new file mode 100644 index 000000000..00aeca927 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_notes.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetNotesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_notes(self): + self.gmp.get_notes() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_notes_with_filter(self): + self.gmp.get_notes(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_notes_with_filter_id(self): + self.gmp.get_notes(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_notes_with_details(self): + self.gmp.get_notes(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_notes(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_notes_with_result(self): + self.gmp.get_notes(result=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_notes(result=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 7a585c8f3a694e931fe1165fd961f7ed899f9549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:53:18 +0100 Subject: [PATCH 049/107] Require nvt_oid for get_nvt method --- gvm/protocols/gmpv7.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 441bb550b..36f677c7f 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2774,6 +2774,9 @@ def get_nvt(self, nvt_oid): Returns: The response. See :py:meth:`send_command` for details. """ + if not nvt_oid: + raise RequiredArgument('get_nvt requires nvt_oid argument') + cmd = XmlCommand('get_nvts') cmd.set_attribute('nvt_oid', nvt_oid) From 4b6fc3a0ea8ba0bebfe44a5657ac80ec22fc7d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:53:36 +0100 Subject: [PATCH 050/107] Add test for missing nvt_oid when calling get_nvt --- tests/protocols/gmpv7/test_get_nvt.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/protocols/gmpv7/test_get_nvt.py b/tests/protocols/gmpv7/test_get_nvt.py index a311efb2b..88ad4cca1 100644 --- a/tests/protocols/gmpv7/test_get_nvt.py +++ b/tests/protocols/gmpv7/test_get_nvt.py @@ -18,6 +18,7 @@ import unittest +from gvm.errors import RequiredArgument from gvm.protocols.gmpv7 import Gmp from .. import MockConnection @@ -36,6 +37,16 @@ def test_get_nvt_with_nvt_oid(self): '' ) + def test_get_nvt_missing_nvt_oid(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt(nvt_oid=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt(nvt_oid='') + + with self.assertRaises(RequiredArgument): + self.gmp.get_nvt('') + if __name__ == '__main__': unittest.main() From 2be57b27358b937e6da91ce2d8ab915dcee5fee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:53:53 +0100 Subject: [PATCH 051/107] Add test for passing false for boolean args of get_nvts --- tests/protocols/gmpv7/test_get_nvts.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/protocols/gmpv7/test_get_nvts.py b/tests/protocols/gmpv7/test_get_nvts.py index 74b00b533..578d4c9b0 100644 --- a/tests/protocols/gmpv7/test_get_nvts.py +++ b/tests/protocols/gmpv7/test_get_nvts.py @@ -42,6 +42,12 @@ def test_get_nvts_with_details(self): '' ) + self.gmp.get_nvts(details=False) + + self.connection.send.has_been_called_with( + '' + ) + def test_get_nvts_with_preferences(self): self.gmp.get_nvts(preferences=True) @@ -49,6 +55,12 @@ def test_get_nvts_with_preferences(self): '' ) + self.gmp.get_nvts(preferences=False) + + self.connection.send.has_been_called_with( + '' + ) + def test_get_nvts_with_preference_count(self): self.gmp.get_nvts(preference_count=True) @@ -63,6 +75,12 @@ def test_get_nvts_with_timeout(self): '' ) + self.gmp.get_nvts(timeout=False) + + self.connection.send.has_been_called_with( + '' + ) + def test_get_nvts_with_config_id(self): self.gmp.get_nvts(config_id='config_id') From dcc80f8375ca31948defea5d0b0d37386549619d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:57:02 +0100 Subject: [PATCH 052/107] Add tests for get_nvt_families --- .../protocols/gmpv7/test_get_nvt_families.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_nvt_families.py diff --git a/tests/protocols/gmpv7/test_get_nvt_families.py b/tests/protocols/gmpv7/test_get_nvt_families.py new file mode 100644 index 000000000..4d59034a3 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_nvt_families.py @@ -0,0 +1,47 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetNvtFamiliesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_nvt_families(self): + self.gmp.get_nvt_families() + + self.connection.send.has_been_called_with( + '') + + def test_get_nvt_families_with_sort_order(self): + self.gmp.get_nvt_families(sort_order='foo') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 973ef899d7b39adcea6317cd20a952a266cef40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 15:58:42 +0100 Subject: [PATCH 053/107] Remove nvt_oid and task_id from get_overrides Same arguments as with get_notes. Filter arguments should be passed as filter instead. --- CHANGELOG.md | 3 ++- gvm/protocols/gmpv7.py | 12 ++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e2882c08..4dab70748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ It is only possible to create host assets. * Updated and improved validation of `create_schedule` arguments * Removed the format parameter from `get_credentials` method -* Removed the task_id and nvt_oid parameter from `get_notes` method +* Removed the task_id and nvt_oid parameters from `get_notes` and + `get_overrides` methods # python-gvm 1.0.0.beta2 (04.12.2018) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 36f677c7f..20b3c952a 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2800,16 +2800,14 @@ def get_nvt_families(self, *, sort_order=None): return self._send_xml_command(cmd) - def get_overrides(self, *, filter=None, filter_id=None, nvt_oid=None, - task_id=None, details=None, result=None): + def get_overrides(self, *, filter=None, filter_id=None, details=None, + result=None): """Request a list of overrides Arguments: filter (str, optional): Filter term to use for the query filter_id (str, optional): UUID of an existing filter to use for the query - nvt_oid (str, optional): OID of a nvt - task_id (str, optional): UUID of a task details (boolean, optional): result (boolean, optional): @@ -2820,12 +2818,6 @@ def get_overrides(self, *, filter=None, filter_id=None, nvt_oid=None, _add_filter(cmd, filter, filter_id) - if nvt_oid: - cmd.set_attribute('nvt_oid', nvt_oid) - - if task_id: - cmd.set_attribute('task_id', task_id) - if not details is None: cmd.set_attribute('details', _to_bool(details)) From b8fe399ad0511578ad4fe1aff6e2d95663d498fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:01:17 +0100 Subject: [PATCH 054/107] Require override_id for get_override method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 20b3c952a..8efae609a 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2835,6 +2835,11 @@ def get_override(self, override_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not override_id: + raise RequiredArgument( + 'get_override requires a override_id argument' + ) + cmd = XmlCommand('get_overrides') cmd.set_attribute('override_id', override_id) From a40c737b0b82e9dd2b22f326f617f5cec9c87978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:01:29 +0100 Subject: [PATCH 055/107] Add tests for get_override --- tests/protocols/gmpv7/test_get_override.py | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_override.py diff --git a/tests/protocols/gmpv7/test_get_override.py b/tests/protocols/gmpv7/test_get_override.py new file mode 100644 index 000000000..79c280b1e --- /dev/null +++ b/tests/protocols/gmpv7/test_get_override.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetOverrideTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_override(self): + self.gmp.get_override('o1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_override(override_id='o1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_override_missing_override_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_override(override_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_override('') + + +if __name__ == '__main__': + unittest.main() From 3e4b5d2549942b01966bdd83db8d7f9b1d24d577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:02:52 +0100 Subject: [PATCH 056/107] Add tests for get_overrides --- tests/protocols/gmpv7/test_get_overrides.py | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_overrides.py diff --git a/tests/protocols/gmpv7/test_get_overrides.py b/tests/protocols/gmpv7/test_get_overrides.py new file mode 100644 index 000000000..2ee564c25 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_overrides.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetOverridesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_overrides(self): + self.gmp.get_overrides() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_overrides_with_filter(self): + self.gmp.get_overrides(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_overrides_with_filter_id(self): + self.gmp.get_overrides(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_overrides_with_details(self): + self.gmp.get_overrides(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_overrides(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_overrides_with_result(self): + self.gmp.get_overrides(result=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_overrides(result=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From ab66341be8cdfdd27b5d3d0c2244bad14701b09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:04:36 +0100 Subject: [PATCH 057/107] Add tests for get_permissions --- tests/protocols/gmpv7/test_get_permissions.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_permissions.py diff --git a/tests/protocols/gmpv7/test_get_permissions.py b/tests/protocols/gmpv7/test_get_permissions.py new file mode 100644 index 000000000..675b4cd38 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_permissions.py @@ -0,0 +1,68 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPermissionsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_permissions(self): + self.gmp.get_permissions() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_permissions_with_filter(self): + self.gmp.get_permissions(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_permissions_with_filter_id(self): + self.gmp.get_permissions(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_permissions_with_trash(self): + self.gmp.get_permissions(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_permissions(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From b98e4acc70ae61482dde6864693c788cd9b4c604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:06:22 +0100 Subject: [PATCH 058/107] Require permission_id for get_permission --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 8efae609a..831e75d20 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2878,6 +2878,11 @@ def get_permission(self, permission_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not permission_id: + raise RequiredArgument( + 'get_permission requires a permission_id argument' + ) + cmd = XmlCommand('get_permissions') cmd.set_attribute('permission_id', permission_id) return self._send_xml_command(cmd) From dea3dbf2743ed2370278f29f4791c89a3ee11723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:06:35 +0100 Subject: [PATCH 059/107] Add tests for get_permission --- tests/protocols/gmpv7/test_get_permission.py | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_permission.py diff --git a/tests/protocols/gmpv7/test_get_permission.py b/tests/protocols/gmpv7/test_get_permission.py new file mode 100644 index 000000000..b1fa952a9 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_permission.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPermissionTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_permission(self): + self.gmp.get_permission('p1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_permission(permission_id='p1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_permission_missing_permission_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_permission(permission_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_permission('') + + +if __name__ == '__main__': + unittest.main() From 8dd82d9fe435d405dc4106c5aad87cda09ae0027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:10:26 +0100 Subject: [PATCH 060/107] Require port_list_id for get_port_list method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 831e75d20..3eca844fa 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2929,6 +2929,11 @@ def get_port_list(self, port_list_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not port_list_id: + raise RequiredArgument( + 'get_port_list requires a port_list_id argument' + ) + cmd = XmlCommand('get_port_lists') cmd.set_attribute('port_list_id', port_list_id) From 8014002ac1ddf884e720435f32cbc24a05607281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:10:43 +0100 Subject: [PATCH 061/107] Add tests for get_port_list --- tests/protocols/gmpv7/test_get_port_list.py | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_port_list.py diff --git a/tests/protocols/gmpv7/test_get_port_list.py b/tests/protocols/gmpv7/test_get_port_list.py new file mode 100644 index 000000000..2a02c692e --- /dev/null +++ b/tests/protocols/gmpv7/test_get_port_list.py @@ -0,0 +1,52 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPortListTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_port_list(self): + self.gmp.get_port_list(port_list_id='port_list_id') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_list_missing_port_list_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_port_list(port_list_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_port_list(port_list_id='') + + with self.assertRaises(RequiredArgument): + self.gmp.get_port_list('') + + +if __name__ == '__main__': + unittest.main() From d295196d9b8c985b9b291063e382838e1a4c5f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:13:49 +0100 Subject: [PATCH 062/107] Add tests for get_port_lists --- tests/protocols/gmpv7/test_get_port_lists.py | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_port_lists.py diff --git a/tests/protocols/gmpv7/test_get_port_lists.py b/tests/protocols/gmpv7/test_get_port_lists.py new file mode 100644 index 000000000..f38078d3d --- /dev/null +++ b/tests/protocols/gmpv7/test_get_port_lists.py @@ -0,0 +1,94 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPortListsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_port_lists(self): + self.gmp.get_port_lists() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_lists_with_filter(self): + self.gmp.get_port_lists(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_lists_with_filter_id(self): + self.gmp.get_port_lists(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_lists_with_trash(self): + self.gmp.get_port_lists(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_port_lists(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_lists_with_details(self): + self.gmp.get_port_lists(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_port_lists(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_port_lists_with_targets(self): + self.gmp.get_port_lists(targets=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_port_lists(targets=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 9baa9bd7baf8dae5ac797d59b7bf8327a7015033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:20:25 +0100 Subject: [PATCH 063/107] Move getting a single preference into its own method --- CHANGELOG.md | 2 ++ gvm/protocols/gmpv7.py | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dab70748..af2d03bd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ * Removed the format parameter from `get_credentials` method * Removed the task_id and nvt_oid parameters from `get_notes` and `get_overrides` methods +* Split getting a single preference by name from `get_preferences` method into + `get_preference` # python-gvm 1.0.0.beta2 (04.12.2018) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 3eca844fa..c765e8b0e 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -2941,7 +2941,7 @@ def get_port_list(self, port_list_id): cmd.set_attribute('details', '1') return self._send_xml_command(cmd) - def get_preferences(self, *, nvt_oid=None, config_id=None, preference=None): + def get_preferences(self, *, nvt_oid=None, config_id=None): """Request a list of preferences When the command includes a config_id attribute, the preference element @@ -2966,8 +2966,26 @@ def get_preferences(self, *, nvt_oid=None, config_id=None, preference=None): if config_id: cmd.set_attribute('config_id', config_id) - if preference: - cmd.set_attribute('preference', preference) + return self._send_xml_command(cmd) + + def get_preference(self, name): + """Request a nvt preference + + + Arguments: + preference (str): name of a particular preference + + Returns: + The response. See :py:meth:`send_command` for details. + """ + if not name: + raise RequiredArgument( + 'get_preference requires a name argument' + ) + + cmd = XmlCommand('get_preferences') + + cmd.set_attribute('preference', name) return self._send_xml_command(cmd) From f5625f2b17c09af373400aa880d8dc127644d714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:27:34 +0100 Subject: [PATCH 064/107] Add tests for get_preference --- tests/protocols/gmpv7/test_get_preference.py | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_preference.py diff --git a/tests/protocols/gmpv7/test_get_preference.py b/tests/protocols/gmpv7/test_get_preference.py new file mode 100644 index 000000000..815239f37 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_preference.py @@ -0,0 +1,52 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPreferenceTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_preference(self): + self.gmp.get_preference(name='foo') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_preference_missing_name(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_preference(name=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_preference(name='') + + with self.assertRaises(RequiredArgument): + self.gmp.get_preference('') + + +if __name__ == '__main__': + unittest.main() From 931c2ebe97d7c8911d30f732c16373ec7b7bffc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:27:45 +0100 Subject: [PATCH 065/107] Add tests for get_preferences --- tests/protocols/gmpv7/test_get_preferences.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_preferences.py diff --git a/tests/protocols/gmpv7/test_get_preferences.py b/tests/protocols/gmpv7/test_get_preferences.py new file mode 100644 index 000000000..ea2620a5c --- /dev/null +++ b/tests/protocols/gmpv7/test_get_preferences.py @@ -0,0 +1,55 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetPreferencesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_preferences(self): + self.gmp.get_preferences() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_preferences_with_nvt_oid(self): + self.gmp.get_preferences(nvt_oid='oid') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_preferences_with_config_id(self): + self.gmp.get_preferences(config_id='c1') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 30ee72ce8d424aec3011535c116b6bbc967fd30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:30:18 +0100 Subject: [PATCH 066/107] Add tests for get_report_formats --- .../gmpv7/test_get_report_formats.py | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_report_formats.py diff --git a/tests/protocols/gmpv7/test_get_report_formats.py b/tests/protocols/gmpv7/test_get_report_formats.py new file mode 100644 index 000000000..e0f5325e0 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_report_formats.py @@ -0,0 +1,107 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetReportFormatsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_report_formats(self): + self.gmp.get_report_formats() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_filter(self): + self.gmp.get_report_formats(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_filter_id(self): + self.gmp.get_report_formats(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_trash(self): + self.gmp.get_report_formats(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_report_formats(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_details(self): + self.gmp.get_report_formats(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_report_formats(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_alerts(self): + self.gmp.get_report_formats(alerts=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_report_formats(alerts=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_formats_with_params(self): + self.gmp.get_report_formats(params=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_report_formats(params=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 89a2fdd4c164d33d336aeee69594745a4c20957d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:35:11 +0100 Subject: [PATCH 067/107] Require report_format_id for get_report_format method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index c765e8b0e..d89e99b3d 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3103,6 +3103,11 @@ def get_report_format(self, report_format_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not report_format_id: + raise RequiredArgument( + 'get_report_format requires report_format_id argument' + ) + cmd = XmlCommand('get_report_formats') cmd.set_attribute('report_format_id', report_format_id) From 56dfe2cb9d4694a654a1b87c410903ede45b556e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:35:26 +0100 Subject: [PATCH 068/107] Add tests for get_report_format --- .../protocols/gmpv7/test_get_report_format.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_report_format.py diff --git a/tests/protocols/gmpv7/test_get_report_format.py b/tests/protocols/gmpv7/test_get_report_format.py new file mode 100644 index 000000000..7498fe7fa --- /dev/null +++ b/tests/protocols/gmpv7/test_get_report_format.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetReportFormatTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_report_format(self): + self.gmp.get_report_format('rf1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_report_format(report_format_id='rf1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_report_format_missing_report_format_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_report_format(report_format_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_report_format('') + + +if __name__ == '__main__': + unittest.main() From 0e9db170484c431af161b25dff008e7e351cb60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:41:16 +0100 Subject: [PATCH 069/107] Add tests for get_results --- tests/protocols/gmpv7/test_get_results.py | 101 ++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_results.py diff --git a/tests/protocols/gmpv7/test_get_results.py b/tests/protocols/gmpv7/test_get_results.py new file mode 100644 index 000000000..4ce75c84a --- /dev/null +++ b/tests/protocols/gmpv7/test_get_results.py @@ -0,0 +1,101 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetResultsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_results(self): + self.gmp.get_results() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_filter(self): + self.gmp.get_results(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_filter_id(self): + self.gmp.get_results(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_note_details(self): + self.gmp.get_results(note_details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_results(note_details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_override_details(self): + self.gmp.get_results(override_details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_results(override_details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_details(self): + self.gmp.get_results(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_results(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_results_with_task_id(self): + self.gmp.get_results(task_id='t1') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From e80f9e11c9f318348c19858d6f6325033612243d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:42:56 +0100 Subject: [PATCH 070/107] Require result_id for get_result --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index d89e99b3d..e01d46e92 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3161,6 +3161,11 @@ def get_result(self, result_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not result_id: + raise RequiredArgument( + 'get_result requires a result_id argument' + ) + cmd = XmlCommand('get_results') cmd.set_attribute('result_id', result_id) From 222d89633f5e027f517920696aa36c37218f4127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:43:12 +0100 Subject: [PATCH 071/107] Add tests for get_result --- tests/protocols/gmpv7/test_get_result.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_result.py diff --git a/tests/protocols/gmpv7/test_get_result.py b/tests/protocols/gmpv7/test_get_result.py new file mode 100644 index 000000000..5f864bf65 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_result.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetResultTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_result(self): + self.gmp.get_result('r1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_result(result_id='r1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_result_missing_result_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_result(result_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_result('') + + +if __name__ == '__main__': + unittest.main() From 7fcec38941b994f79624d39d16d99f32a655cd52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:45:10 +0100 Subject: [PATCH 072/107] Require role_id for get_role method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index e01d46e92..bbd565062 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3203,6 +3203,11 @@ def get_role(self, role_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not role_id: + raise RequiredArgument( + 'get_role requires a role_id argument' + ) + cmd = XmlCommand('get_roles') cmd.set_attribute('role_id', role_id) return self._send_xml_command(cmd) From 92c54827e72a8dd46767c5a2ef929acfb71f1da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:45:23 +0100 Subject: [PATCH 073/107] Add tests for get_role --- tests/protocols/gmpv7/test_get_role.py | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_role.py diff --git a/tests/protocols/gmpv7/test_get_role.py b/tests/protocols/gmpv7/test_get_role.py new file mode 100644 index 000000000..3bfb5ffd0 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_role.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetRoleTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_role(self): + self.gmp.get_role('r1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_role(role_id='r1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_role_missing_role_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_role(role_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_role('') + + +if __name__ == '__main__': + unittest.main() From 1c15b383d7b6e432c934d67daf13894140729a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 19 Dec 2018 16:47:02 +0100 Subject: [PATCH 074/107] Add tests for get_roles --- tests/protocols/gmpv7/test_get_roles.py | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_roles.py diff --git a/tests/protocols/gmpv7/test_get_roles.py b/tests/protocols/gmpv7/test_get_roles.py new file mode 100644 index 000000000..8c3cb9d85 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_roles.py @@ -0,0 +1,68 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetRolesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_roles(self): + self.gmp.get_roles() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_roles_with_filter(self): + self.gmp.get_roles(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_roles_with_filter_id(self): + self.gmp.get_roles(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_roles_with_trash(self): + self.gmp.get_roles(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_roles(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 81b991a140f90f29810566db2bcadafd8d7e29d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 08:41:45 +0100 Subject: [PATCH 075/107] Require scanner_id for get_scanner method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index bbd565062..ddc0915f2 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3249,6 +3249,11 @@ def get_scanner(self, scanner_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not scanner_id: + raise RequiredArgument( + 'get_scanner requires a scanner_id argument' + ) + cmd = XmlCommand('get_scanners') cmd.set_attribute('scanner_id', scanner_id) From 548ff552c6f12891e796c22c49d127115ac03c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 08:42:19 +0100 Subject: [PATCH 076/107] Add tests for get_scanner --- tests/protocols/gmpv7/test_get_scanner.py | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_scanner.py diff --git a/tests/protocols/gmpv7/test_get_scanner.py b/tests/protocols/gmpv7/test_get_scanner.py new file mode 100644 index 000000000..c2c1308dc --- /dev/null +++ b/tests/protocols/gmpv7/test_get_scanner.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetScannerTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_scanner(self): + self.gmp.get_scanner('s1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_scanner(scanner_id='s1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_scanner_missing_scanner_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_scanner(scanner_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_scanner('') + + +if __name__ == '__main__': + unittest.main() From 8c26612bf7fbb34480cb985947580ee99f639135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 08:42:44 +0100 Subject: [PATCH 077/107] Add tests for get_scanners --- tests/protocols/gmpv7/test_get_scanners.py | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_scanners.py diff --git a/tests/protocols/gmpv7/test_get_scanners.py b/tests/protocols/gmpv7/test_get_scanners.py new file mode 100644 index 000000000..1e8624acd --- /dev/null +++ b/tests/protocols/gmpv7/test_get_scanners.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetScannersTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_scanners(self): + self.gmp.get_scanners() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_scanners_with_filter(self): + self.gmp.get_scanners(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_scanners_with_filter_id(self): + self.gmp.get_scanners(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_scanners_with_trash(self): + self.gmp.get_scanners(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_scanners(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_scanners_with_details(self): + self.gmp.get_scanners(details=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_scanners(details=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 2c6ee321589c6e2029fd0377c168ca614992f2cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 10:04:53 +0100 Subject: [PATCH 078/107] Require schedule_id for get_schedule method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index ddc0915f2..b610e09eb 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3298,6 +3298,11 @@ def get_schedule(self, schedule_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not schedule_id: + raise RequiredArgument( + 'get_schedule requires a schedule_id argument' + ) + cmd = XmlCommand('get_schedules') cmd.set_attribute('schedule_id', schedule_id) return self._send_xml_command(cmd) From 981ace13ad16f3b3156950f3d12546e97c5b3922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 10:18:08 +0100 Subject: [PATCH 079/107] Add tests for get_schedule --- tests/protocols/gmpv7/test_get_schedule.py | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_schedule.py diff --git a/tests/protocols/gmpv7/test_get_schedule.py b/tests/protocols/gmpv7/test_get_schedule.py new file mode 100644 index 000000000..278e94fc0 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_schedule.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetScheduleTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_schedule(self): + self.gmp.get_schedule('s1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_schedule(schedule_id='s1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_schedule_missing_schedule_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_schedule(schedule_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_schedule('') + + +if __name__ == '__main__': + unittest.main() From 5979577553bab179e5ad0d02b1040d025da94f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 10:18:20 +0100 Subject: [PATCH 080/107] Add tests for get_schedules --- tests/protocols/gmpv7/test_get_schedules.py | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_schedules.py diff --git a/tests/protocols/gmpv7/test_get_schedules.py b/tests/protocols/gmpv7/test_get_schedules.py new file mode 100644 index 000000000..dccdf3539 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_schedules.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetSchedulesTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_schedules(self): + self.gmp.get_schedules() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_schedules_with_filter(self): + self.gmp.get_schedules(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_schedules_with_filter_id(self): + self.gmp.get_schedules(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_schedules_with_trash(self): + self.gmp.get_schedules(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_schedules(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_schedules_with_tasks(self): + self.gmp.get_schedules(tasks=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_schedules(tasks=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 1a093645f07122a49e9d71aba3fef294e32d0c7a Mon Sep 17 00:00:00 2001 From: Michael Wiegand Date: Thu, 20 Dec 2018 10:24:31 +0100 Subject: [PATCH 081/107] Apply suggestions from code review Fix typos Co-Authored-By: bjoernricks --- gvm/protocols/gmpv7.py | 10 +++++----- tests/protocols/gmpv7/test_get_credentials.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index b610e09eb..3907ed2f3 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -1389,7 +1389,7 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, if not isinstance(duration, numbers.Integral) or duration < 1: raise InvalidArgument( - 'duration argument must be an integer greater then 0', + 'duration argument must be an integer greater than 0', ) _xmlduration = cmd.add_element('duration', str(duration)) @@ -1408,7 +1408,7 @@ def create_schedule(self, name, *, comment=None, first_time_minute=None, if not isinstance(period, numbers.Integral) or period < 1: raise InvalidArgument( - 'period argument must be an integer greater then 0', + 'period argument must be an integer greater than 0', ) _xmlperiod = cmd.add_element('period', str(period)) @@ -1641,7 +1641,7 @@ def create_task(self, name, config_id, target_id, scanner_id, *, be run. schedule_periods (int, optional): A limit to the number of times the task will be scheduled, or 0 for no limit - observers (list, optional): List of user names or user ids which + observers (list, optional): List of names or ids of users which should be allowed to observe this task Returns: @@ -1702,7 +1702,7 @@ def create_task(self, name, config_id, target_id, scanner_id, *, schedule_periods < 0: raise InvalidArgument( 'schedule_periods must be an integer greater or equal ' - 'then 0' + 'than 0' ) cmd.add_element('schedule_periods', str(schedule_periods)) @@ -2837,7 +2837,7 @@ def get_override(self, override_id): """ if not override_id: raise RequiredArgument( - 'get_override requires a override_id argument' + 'get_override requires an override_id argument' ) cmd = XmlCommand('get_overrides') diff --git a/tests/protocols/gmpv7/test_get_credentials.py b/tests/protocols/gmpv7/test_get_credentials.py index 09030e1ba..c013edd08 100644 --- a/tests/protocols/gmpv7/test_get_credentials.py +++ b/tests/protocols/gmpv7/test_get_credentials.py @@ -28,7 +28,7 @@ def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) - def test_get_credential(self): + def test_get_credentials(self): self.gmp.get_credentials() self.connection.send.has_been_called_with( From 948cb752134572e0917a15c400e345d0ca9cc108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 10:26:44 +0100 Subject: [PATCH 082/107] Update get_settings tests Remove unused imports and add test for passing a filter --- tests/protocols/gmpv7/test_get_settings.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/protocols/gmpv7/test_get_settings.py b/tests/protocols/gmpv7/test_get_settings.py index efc8ccffa..7b45369f5 100644 --- a/tests/protocols/gmpv7/test_get_settings.py +++ b/tests/protocols/gmpv7/test_get_settings.py @@ -18,7 +18,6 @@ import unittest -from gvm.errors import InvalidArgument, RequiredArgument from gvm.protocols.gmpv7 import Gmp from .. import MockConnection @@ -29,12 +28,18 @@ def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) - def test_get_settings_simple(self): + def test_get_settings(self): self.gmp.get_settings() self.connection.send.has_been_called_with( '') + def test_get_settings_with_filter(self): + self.gmp.get_settings(filter="foo=bar") + + self.connection.send.has_been_called_with( + '') + if __name__ == '__main__': unittest.main() From 0c8eaea78a81de07ccafac4be95f91b734b662d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:25:09 +0100 Subject: [PATCH 083/107] Ensure duration of get_system_reports is an integer --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 3907ed2f3..b46e44077 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3362,6 +3362,11 @@ def get_system_reports(self, *, name=None, duration=None, start_time=None, cmd.set_attribute('name', name) if not duration is None: + if not isinstance(duration, numbers.Integral): + raise InvalidArgument( + 'duration needs to be an integer number' + ) + cmd.set_attribute('duration', str(duration)) if start_time: From a791a513dd6e681ea875505508b1d1da73610607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:25:33 +0100 Subject: [PATCH 084/107] Add tests for get_system_reports --- .../gmpv7/test_get_system_reports.py | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_system_reports.py diff --git a/tests/protocols/gmpv7/test_get_system_reports.py b/tests/protocols/gmpv7/test_get_system_reports.py new file mode 100644 index 000000000..723fe873f --- /dev/null +++ b/tests/protocols/gmpv7/test_get_system_reports.py @@ -0,0 +1,97 @@ +# -*- 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 InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetSystemReportsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_system_reports(self): + self.gmp.get_system_reports() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_name(self): + self.gmp.get_system_reports(name='foo') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_slave_id(self): + self.gmp.get_system_reports(slave_id='s1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_brief(self): + self.gmp.get_system_reports(brief=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_system_reports(brief=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_duration(self): + self.gmp.get_system_reports(duration=3600) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_invalid_duration(self): + with self.assertRaises(InvalidArgument): + self.gmp.get_system_reports(duration='') + + def test_get_system_reports_with_start_time(self): + self.gmp.get_system_reports(start_time='01-01-2019') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_system_reports_with_end_time(self): + self.gmp.get_system_reports(end_time='01-01-2019') + + self.connection.send.has_been_called_with( + '' + ) + + + + + +if __name__ == '__main__': + unittest.main() From 723ceef7a043f8c02a82101b8a733a2746c01f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:28:06 +0100 Subject: [PATCH 085/107] Require setting_id for get_setting --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index b46e44077..e24d973de 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3332,6 +3332,11 @@ def get_setting(self, setting_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not setting_id: + raise RequiredArgument( + 'get_setting requires a setting_id argument' + ) + cmd = XmlCommand('get_settings') cmd.set_attribute('setting_id', setting_id) return self._send_xml_command(cmd) From e2c0f08f3fc3a56be88900368fa477937c6561a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:28:20 +0100 Subject: [PATCH 086/107] Add test for missing setting_id of get_setting --- tests/protocols/gmpv7/test_get_setting.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/protocols/gmpv7/test_get_setting.py b/tests/protocols/gmpv7/test_get_setting.py index 0dcb42eb9..cdd93cf7d 100644 --- a/tests/protocols/gmpv7/test_get_setting.py +++ b/tests/protocols/gmpv7/test_get_setting.py @@ -35,6 +35,13 @@ def test_get_setting_simple(self): self.connection.send.has_been_called_with( '') + def test_get_setting_missing_setting_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_setting(setting_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_setting('') + if __name__ == '__main__': unittest.main() From fb0f9479ffd642e7a59d3e117bef18dbcc1cb478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:29:26 +0100 Subject: [PATCH 087/107] Require tag_id for get_tag --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index e24d973de..9f3f3bec4 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3425,6 +3425,11 @@ def get_tag(self, tag_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not tag_id: + raise RequiredArgument( + 'get_tag requires a tag_id argument' + ) + cmd = XmlCommand('get_tags') cmd.set_attribute('tag_id', tag_id) return self._send_xml_command(cmd) From 14dfb2dd46fbbe148681ebfaee722558aeec0629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:29:41 +0100 Subject: [PATCH 088/107] Add tests for get_tag --- tests/protocols/gmpv7/test_get_tag.py | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_tag.py diff --git a/tests/protocols/gmpv7/test_get_tag.py b/tests/protocols/gmpv7/test_get_tag.py new file mode 100644 index 000000000..5b68531d4 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_tag.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetTagTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_tag(self): + self.gmp.get_tag('t1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_tag(tag_id='t1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_tag_missing_tag_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_tag(tag_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_tag('') + + +if __name__ == '__main__': + unittest.main() From bec7e2f7efb3dd063a10712089b2e35071f54790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:31:06 +0100 Subject: [PATCH 089/107] Add tests for get_tags --- tests/protocols/gmpv7/test_get_tags.py | 81 ++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_tags.py diff --git a/tests/protocols/gmpv7/test_get_tags.py b/tests/protocols/gmpv7/test_get_tags.py new file mode 100644 index 000000000..1dc6573ad --- /dev/null +++ b/tests/protocols/gmpv7/test_get_tags.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetTagsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_tags(self): + self.gmp.get_tags() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_tags_with_filter(self): + self.gmp.get_tags(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_tags_with_filter_id(self): + self.gmp.get_tags(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_tags_with_trash(self): + self.gmp.get_tags(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_tags(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_tags_with_names_only(self): + self.gmp.get_tags(names_only=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_tags(names_only=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 9442f9f4929b86991a3fc498d5e290f1ee24c61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:33:22 +0100 Subject: [PATCH 090/107] Add tests for get_targets --- tests/protocols/gmpv7/test_get_targets.py | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_targets.py diff --git a/tests/protocols/gmpv7/test_get_targets.py b/tests/protocols/gmpv7/test_get_targets.py new file mode 100644 index 000000000..eb56702a8 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_targets.py @@ -0,0 +1,81 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetTargetsTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_targets(self): + self.gmp.get_targets() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_targets_with_filter(self): + self.gmp.get_targets(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_targets_with_filter_id(self): + self.gmp.get_targets(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_targets_with_trash(self): + self.gmp.get_targets(trash=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_targets(trash=False) + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_targets_with_tasks(self): + self.gmp.get_targets(tasks=True) + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_targets(tasks=False) + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 9e38cdcff19ae54ef3bd3fb3e8a9d0c21a957a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:34:54 +0100 Subject: [PATCH 091/107] Require target_id for get_target --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 9f3f3bec4..7fd7bceee 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3471,6 +3471,11 @@ def get_target(self, target_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not target_id: + raise RequiredArgument( + 'get_target requires a target_id argument' + ) + cmd = XmlCommand('get_targets') cmd.set_attribute('target_id', target_id) return self._send_xml_command(cmd) From f202e07817a0868dd1359a6b7e4d694a47cbc918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:35:06 +0100 Subject: [PATCH 092/107] Add tests for get_target --- tests/protocols/gmpv7/test_get_target.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_target.py diff --git a/tests/protocols/gmpv7/test_get_target.py b/tests/protocols/gmpv7/test_get_target.py new file mode 100644 index 000000000..b64e520e7 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_target.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetTargetTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_target(self): + self.gmp.get_target('t1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_target(target_id='t1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_target_missing_target_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_target(target_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_target('') + + +if __name__ == '__main__': + unittest.main() From 616f159a94bb58ec337609b0c0685dd46a996c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:36:42 +0100 Subject: [PATCH 093/107] Add tests for get_users --- tests/protocols/gmpv7/test_get_users.py | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_users.py diff --git a/tests/protocols/gmpv7/test_get_users.py b/tests/protocols/gmpv7/test_get_users.py new file mode 100644 index 000000000..edd5be434 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_users.py @@ -0,0 +1,55 @@ +# -*- 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.protocols.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetUserTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_users(self): + self.gmp.get_users() + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_users_with_filter(self): + self.gmp.get_users(filter='foo=bar') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_users_with_filter_id(self): + self.gmp.get_users(filter_id='f1') + + self.connection.send.has_been_called_with( + '' + ) + + +if __name__ == '__main__': + unittest.main() From 8e0f95fc2333a8fa3225510b9c530c7c8c8b17c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:38:08 +0100 Subject: [PATCH 094/107] Require user_id argument for get_user method --- gvm/protocols/gmpv7.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 7fd7bceee..19acaf384 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3556,6 +3556,11 @@ def get_user(self, user_id): Returns: The response. See :py:meth:`send_command` for details. """ + if not user_id: + raise RequiredArgument( + 'get_user requires a user_id argument' + ) + cmd = XmlCommand('get_users') cmd.set_attribute('user_id', user_id) return self._send_xml_command(cmd) From d5c7291f42b2af65de8d5b6921165f21829b6af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:38:21 +0100 Subject: [PATCH 095/107] Add tests for get_user --- tests/protocols/gmpv7/test_get_user.py | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/protocols/gmpv7/test_get_user.py diff --git a/tests/protocols/gmpv7/test_get_user.py b/tests/protocols/gmpv7/test_get_user.py new file mode 100644 index 000000000..e9b871000 --- /dev/null +++ b/tests/protocols/gmpv7/test_get_user.py @@ -0,0 +1,55 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + + +class GmpGetTargetTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_get_user(self): + self.gmp.get_user('u1') + + self.connection.send.has_been_called_with( + '' + ) + + self.gmp.get_user(user_id='u1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_get_user_missing_user_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.get_user(user_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.get_user('') + + +if __name__ == '__main__': + unittest.main() From 3e76575f99ca6017fc57da3ab12924f69720c42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 14:41:54 +0100 Subject: [PATCH 096/107] Add tests for missing report_id when calling trigger_alert --- tests/protocols/gmpv7/test_trigger_alert.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/protocols/gmpv7/test_trigger_alert.py b/tests/protocols/gmpv7/test_trigger_alert.py index dbdc1f5fa..d533fed9c 100644 --- a/tests/protocols/gmpv7/test_trigger_alert.py +++ b/tests/protocols/gmpv7/test_trigger_alert.py @@ -36,6 +36,13 @@ def test_trigger_alert_without_alert_id(self): with self.assertRaises(RequiredArgument): self.gmp.trigger_alert(alert_id='', report_id='r1') + def test_trigger_alert_without_report_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.trigger_alert(alert_id='a1', report_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.trigger_alert(alert_id='a1', report_id='') + def test_trigger_alert(self): self.gmp.trigger_alert(alert_id='a1', report_id='r1') From fcea21a3dc8ee5b07625c2cc76872cd91f9c9594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:28:50 +0100 Subject: [PATCH 097/107] Update coding style of create alert tests --- tests/protocols/gmpv7/test_create_alert.py | 47 ++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/tests/protocols/gmpv7/test_create_alert.py b/tests/protocols/gmpv7/test_create_alert.py index d097ab552..07bc671da 100644 --- a/tests/protocols/gmpv7/test_create_alert.py +++ b/tests/protocols/gmpv7/test_create_alert.py @@ -24,6 +24,7 @@ from .. import MockConnection + class GmpCreateAlertTestCase(unittest.TestCase): def setUp(self): @@ -69,7 +70,8 @@ def test_missing_method(self): def test_invalid_condition(self): with self.assertRaises(InvalidArgument): self.gmp.create_alert( - name='foo', condition='bar', event='Task run status changed', method='Email') + name='foo', condition='bar', event='Task run status changed', + method='Email') def test_invalid_event(self): with self.assertRaises(InvalidArgument): @@ -79,21 +81,25 @@ def test_invalid_event(self): def test_invalid_method(self): with self.assertRaises(InvalidArgument): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='ipsum') + name='foo', condition='Always', event='Task run status changed', + method='ipsum') def test_invalid_condition_for_secinfo(self): with self.assertRaises(InvalidArgument): self.gmp.create_alert( - name='foo', condition='Severity at least', event='Updated SecInfo arrived', method='Email') + name='foo', condition='Severity at least', + event='Updated SecInfo arrived', method='Email') def test_invalid_method_for_secinfo(self): with self.assertRaises(InvalidArgument): self.gmp.create_alert( - name='foo', condition='Always', event='Updated SecInfo arrived', method='HTTP Get') + name='foo', condition='Always', event='Updated SecInfo arrived', + method='HTTP Get') def test_create_alert(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email') + name='foo', condition='Always', event='Task run status changed', + method='Email') self.connection.send.has_been_called_with( '' @@ -106,8 +112,8 @@ def test_create_alert(self): def test_create_alert_with_filter_id(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email', - filter_id='f1') + name='foo', condition='Always', event='Task run status changed', + method='Email', filter_id='f1') self.connection.send.has_been_called_with( '' @@ -121,8 +127,8 @@ def test_create_alert_with_filter_id(self): def test_create_alert_with_comment(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email', - comment='hello') + name='foo', condition='Always', event='Task run status changed', + method='Email', comment='hello') self.connection.send.has_been_called_with( '' @@ -136,8 +142,10 @@ def test_create_alert_with_comment(self): def test_create_alert_with_condition_data(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email', - condition_data={'foo': 'bar'}) + name='foo', condition='Always', event='Task run status changed', + method='Email', + condition_data={'foo': 'bar'}, + ) self.connection.send.has_been_called_with( '' @@ -150,22 +158,28 @@ def test_create_alert_with_condition_data(self): def test_create_alert_with_event_data(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email', - event_data={'foo': 'bar'}) + name='foo', condition='Always', event='Task run status changed', + method='Email', + event_data={'foo': 'bar'}, + ) self.connection.send.has_been_called_with( '' 'foo' 'Always' - 'Task run status changedbarfoo' + 'Task run status changed' + 'barfoo' + '' 'Email' '' ) def test_create_alert_with_method_data(self): self.gmp.create_alert( - name='foo', condition='Always', event='Task run status changed', method='Email', - method_data={'foo': 'bar'}) + name='foo', condition='Always', event='Task run status changed', + method='Email', + method_data={'foo': 'bar'}, + ) self.connection.send.has_been_called_with( '' @@ -176,5 +190,6 @@ def test_create_alert_with_method_data(self): '' ) + if __name__ == '__main__': unittest.main() From d0b45f019661f8efb421dff28890f18f3f178ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:31:36 +0100 Subject: [PATCH 098/107] Extract checking the event of create_alert into an own function This will allow to reuse the code for modify_alert. --- gvm/protocols/gmpv7.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 19acaf384..9858b67ad 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -190,6 +190,20 @@ def _add_filter(cmd, filter, filter_id): if filter_id: cmd.set_attribute('filt_id', filter_id) +def _check_event(event, condition, method): + if event in ALERT_EVENTS: + if condition not in ALERT_CONDITIONS: + raise InvalidArgument('Invalid condition for event') + if method not in ALERT_METHODS: + raise InvalidArgument('Invalid method for event') + elif event in ALERT_EVENTS_SECINFO: + if condition not in ALERT_CONDITIONS_SECINFO: + raise InvalidArgument('Invalid condition for event') + if method not in ALERT_METHODS_SECINFO: + raise InvalidArgument('Invalid method for event') + elif event is not None: + raise InvalidArgument('Invalid event "{0}"'.format(event)) + class Gmp(GvmProtocol): """Python interface for Greenbone Management Protocol @@ -373,19 +387,7 @@ def create_alert(self, name, condition, event, method, *, method_data=None, if not method: raise RequiredArgument('create_alert requires method argument') - if event in ALERT_EVENTS: - if condition not in ALERT_CONDITIONS: - raise InvalidArgument('Invalid condition for event') - if method not in ALERT_METHODS: - raise InvalidArgument('Invalid method for event') - elif event in ALERT_EVENTS_SECINFO: - if condition not in ALERT_CONDITIONS_SECINFO: - raise InvalidArgument('Invalid condition for event') - if method not in ALERT_METHODS_SECINFO: - raise InvalidArgument('Invalid method for event') - else: - raise InvalidArgument('Invalid event') - + _check_event(event, condition, method) cmd = XmlCommand('create_alert') cmd.add_element('name', name) From 6a5389927402e87400a321829c8396160805f6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:32:19 +0100 Subject: [PATCH 099/107] Update coding style in modify_agent method --- gvm/protocols/gmpv7.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 9858b67ad..836c17fb4 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3619,8 +3619,10 @@ def modify_agent(self, agent_id, *, name=None, comment=None): cmd = XmlCommand('modify_agent') cmd.set_attribute('agent_id', str(agent_id)) + if name: cmd.add_element('name', name) + if comment: cmd.add_element('comment', comment) From d244273c6252b4389792c1c0b3cfd5c2307c8d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:32:55 +0100 Subject: [PATCH 100/107] Add tests for modify_agent --- tests/protocols/gmpv7/test_modify_agent.py | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/protocols/gmpv7/test_modify_agent.py diff --git a/tests/protocols/gmpv7/test_modify_agent.py b/tests/protocols/gmpv7/test_modify_agent.py new file mode 100644 index 000000000..12cb5a086 --- /dev/null +++ b/tests/protocols/gmpv7/test_modify_agent.py @@ -0,0 +1,69 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + +class GmpModifyAgentTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_modify_agent(self): + self.gmp.modify_agent(agent_id='a1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_modify_agent_without_agent_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.modify_agent(agent_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.modify_agent(agent_id='') + + with self.assertRaises(RequiredArgument): + self.gmp.modify_agent('') + + def test_modify_agent_with_comment(self): + self.gmp.modify_agent(agent_id='a1', comment='lorem') + + self.connection.send.has_been_called_with( + '' + 'lorem' + '' + ) + + def test_modify_agent_with_name(self): + self.gmp.modify_agent(agent_id='a1', name='lorem') + + self.connection.send.has_been_called_with( + '' + 'lorem' + '' + ) + + +if __name__ == '__main__': + unittest.main() From e027d67a3e61963324a45445ebe1324c2e9790ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:35:49 +0100 Subject: [PATCH 101/107] Only add xml elements if values are provided to modify_alert --- gvm/protocols/gmpv7.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 836c17fb4..f37047dd7 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3667,26 +3667,29 @@ def modify_alert(self, alert_id, *, name=None, comment=None, if filter_id: cmd.add_element('filter', attrs={'id': filter_id}) - conditions = cmd.add_element('condition', condition) + if condition: + conditions = cmd.add_element('condition', condition) - if not condition_data is None: - for value, key in condition_data.items(): - _data = conditions.add_element('data', value) - _data.add_element('name', key) + if not condition_data is None: + for value, key in condition_data.items(): + _data = conditions.add_element('data', value) + _data.add_element('name', key) - events = cmd.add_element('event', event) + if event: + events = cmd.add_element('event', event) - if not event_data is None: - for value, key in event_data.items(): - _data = events.add_element('data', value) - _data.add_element('name', key) + if not event_data is None: + for value, key in event_data.items(): + _data = events.add_element('data', value) + _data.add_element('name', key) - methods = cmd.add_element('method', method) + if method: + methods = cmd.add_element('method', method) - if not method_data is None: - for value, key in method_data.items(): - _data = methods.add_element('data', value) - _data.add_element('name', key) + if not method_data is None: + for value, key in method_data.items(): + _data = methods.add_element('data', value) + _data.add_element('name', key) return self._send_xml_command(cmd) From a4a8018fda6820d47be65336bc53febc87538757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:46:06 +0100 Subject: [PATCH 102/107] Fix order of key and value for modify_alert data Fix order of key and value for method, condition and event data of modify_alert --- CHANGELOG.md | 2 ++ gvm/protocols/gmpv7.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af2d03bd1..fc2913624 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ `get_overrides` methods * Split getting a single preference by name from `get_preferences` method into `get_preference` +* Fixed wrong order of key and value for condition_data, event_data and + method_data dict parameters of `modify_alert` method. # python-gvm 1.0.0.beta2 (04.12.2018) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index f37047dd7..a02af2f0f 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3671,7 +3671,7 @@ def modify_alert(self, alert_id, *, name=None, comment=None, conditions = cmd.add_element('condition', condition) if not condition_data is None: - for value, key in condition_data.items(): + for key, value in condition_data.items(): _data = conditions.add_element('data', value) _data.add_element('name', key) @@ -3679,7 +3679,7 @@ def modify_alert(self, alert_id, *, name=None, comment=None, events = cmd.add_element('event', event) if not event_data is None: - for value, key in event_data.items(): + for key, value in event_data.items(): _data = events.add_element('data', value) _data.add_element('name', key) @@ -3687,7 +3687,7 @@ def modify_alert(self, alert_id, *, name=None, comment=None, methods = cmd.add_element('method', method) if not method_data is None: - for value, key in method_data.items(): + for key, value in method_data.items(): _data = methods.add_element('data', value) _data.add_element('name', key) From 3fe81d2530feb54acced32939b329f6fd7e443f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:49:37 +0100 Subject: [PATCH 103/107] Check event argument for modify_alert Add validation for event, condition and method combinations to modify_alert. --- gvm/protocols/gmpv7.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index a02af2f0f..5116f3511 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3655,6 +3655,8 @@ def modify_alert(self, alert_id, *, name=None, comment=None, if not alert_id: raise RequiredArgument('modify_alert requires an alert_id argument') + _check_event(event, condition, method) + cmd = XmlCommand('modify_alert') cmd.set_attribute('alert_id', str(alert_id)) From 529cbb3c9d046142de5d98c7813a027ce2d227f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:50:19 +0100 Subject: [PATCH 104/107] Update docstrings about condition, event and method args --- gvm/protocols/gmpv7.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index 5116f3511..e3b45adfe 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3639,11 +3639,21 @@ def modify_alert(self, alert_id, *, name=None, comment=None, name (str, optional): Name of the Alert. condition (str, optional): The condition that must be satisfied for the alert to occur. + condition (str, optional): The condition that must be satisfied for + the alert to occur; if the event is either 'Updated SecInfo + arrived' or 'New SecInfo arrived', condition must be 'Always'. + Otherwise, condition can also be on of 'Severity at least', + 'Filter count changed' or 'Filter count at least'. condition_data (dict, optional): Data that defines the condition - event (str, optional): The event that must happen for the alert - to occur. + event (str, optional): The event that must happen for the alert to + occur, one of 'Task run status changed', + 'Updated SecInfo arrived' or 'New SecInfo arrived' event_data (dict, optional): Data that defines the event - method (str, optional): The method by which the user is alerted + method (str, optional): The method by which the user is alerted, + one of 'SCP', 'Send', 'SMB', 'SNMP', 'Syslog' or 'Email'; + if the event is neither 'Updated SecInfo arrived' nor + 'New SecInfo arrived', method can also be one of 'Start Task', + 'HTTP Get', 'Sourcefire Connector' or 'verinice Connector'. method_data (dict, optional): Data that defines the method filter_id (str, optional): Filter to apply when executing alert comment (str, optional): Comment for the alert From a2f17f755fc5573807d3d50840723c9be598b218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:50:39 +0100 Subject: [PATCH 105/107] Add tests for modify_alert --- tests/protocols/gmpv7/test_modify_alert.py | 176 +++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 tests/protocols/gmpv7/test_modify_alert.py diff --git a/tests/protocols/gmpv7/test_modify_alert.py b/tests/protocols/gmpv7/test_modify_alert.py new file mode 100644 index 000000000..8632b083a --- /dev/null +++ b/tests/protocols/gmpv7/test_modify_alert.py @@ -0,0 +1,176 @@ +# -*- 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, InvalidArgument +from gvm.protocols.gmpv7 import Gmp + +from .. import MockConnection + +class GmpModifyAlertTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_modify_alert(self): + self.gmp.modify_alert(alert_id='a1') + + self.connection.send.has_been_called_with( + '' + ) + + def test_modify_alert_without_alert_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.modify_alert(alert_id=None) + + with self.assertRaises(RequiredArgument): + self.gmp.modify_alert(alert_id='') + + with self.assertRaises(RequiredArgument): + self.gmp.modify_alert('') + + def test_modify_alert_with_comment(self): + self.gmp.modify_alert(alert_id='a1', comment='lorem') + + self.connection.send.has_been_called_with( + '' + 'lorem' + '' + ) + + def test_modify_alert_with_name(self): + self.gmp.modify_alert(alert_id='a1', name='lorem') + + self.connection.send.has_been_called_with( + '' + 'lorem' + '' + ) + + def test_modify_alert_with_filter_id(self): + self.gmp.modify_alert(alert_id='a1', filter_id='f1') + + self.connection.send.has_been_called_with( + '' + '' + '' + ) + + def test_modify_alert_invalid_condition(self): + with self.assertRaises(InvalidArgument): + self.gmp.modify_alert( + alert_id='a1', + condition='bar', + event='Task run status changed', + method='Email', + ) + + def test_modify_alert_invalid_event(self): + with self.assertRaises(InvalidArgument): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='lorem', + method='Email', + ) + + def test_modify_alert_invalid_method(self): + with self.assertRaises(InvalidArgument): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='Task run status changed', + method='ipsum', + ) + + def test_modify_alert_invalid_condition_for_secinfo(self): + with self.assertRaises(InvalidArgument): + self.gmp.modify_alert( + alert_id='a1', + condition='Severity at least', + event='Updated SecInfo arrived', + method='Email', + ) + + def test_modify_alert_invalid_method_for_secinfo(self): + with self.assertRaises(InvalidArgument): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='Updated SecInfo arrived', + method='HTTP Get', + ) + + def test_modify_alert_with_event_data(self): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='Task run status changed', + method='Email', + event_data={'foo': 'bar'}, + ) + + self.connection.send.has_been_called_with( + '' + 'Always' + 'Task run status changed' + 'barfoo' + '' + 'Email' + '' + ) + + def test_modify_alert_with_condition_data(self): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='Task run status changed', + method='Email', + condition_data={'foo': 'bar'}, + ) + + self.connection.send.has_been_called_with( + '' + 'Alwaysbarfoo' + 'Task run status changed' + 'Email' + '' + ) + + def test_modify_alert_with_method_data(self): + self.gmp.modify_alert( + alert_id='a1', + condition='Always', + event='Task run status changed', + method='Email', + method_data={'foo': 'bar'}, + ) + + self.connection.send.has_been_called_with( + '' + 'Always' + 'Task run status changed' + 'Emailbarfoo' + '' + ) + + +if __name__ == '__main__': + unittest.main() From f730153eb66d1d3f3f2b631e47412414b87ae084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:57:56 +0100 Subject: [PATCH 106/107] Reset comment when calling modify_asset without comment argument --- gvm/protocols/gmpv7.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gvm/protocols/gmpv7.py b/gvm/protocols/gmpv7.py index e3b45adfe..3ca791429 100644 --- a/gvm/protocols/gmpv7.py +++ b/gvm/protocols/gmpv7.py @@ -3705,7 +3705,7 @@ def modify_alert(self, alert_id, *, name=None, comment=None, return self._send_xml_command(cmd) - def modify_asset(self, asset_id, comment): + def modify_asset(self, asset_id, comment=''): """Modifies an existing asset. Arguments: From c1352b0920b881c5ea7141412ccf9791a2611d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 20 Dec 2018 15:58:48 +0100 Subject: [PATCH 107/107] Add tests for modify_asset --- tests/protocols/gmpv7/test_modify_asset.py | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/protocols/gmpv7/test_modify_asset.py diff --git a/tests/protocols/gmpv7/test_modify_asset.py b/tests/protocols/gmpv7/test_modify_asset.py new file mode 100644 index 000000000..053fb9563 --- /dev/null +++ b/tests/protocols/gmpv7/test_modify_asset.py @@ -0,0 +1,86 @@ +# -*- 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.gmpv7 import Gmp + +from .. import MockConnection + +class GmpModifyAssetTestCase(unittest.TestCase): + + def setUp(self): + self.connection = MockConnection() + self.gmp = Gmp(self.connection) + + def test_modify_asset(self): + self.gmp.modify_asset(asset_id='a1') + + self.connection.send.has_been_called_with( + '' + '' + '' + ) + + def test_modify_asset_without_asset_id(self): + with self.assertRaises(RequiredArgument): + self.gmp.modify_asset(asset_id=None, comment='foo') + + with self.assertRaises(RequiredArgument): + self.gmp.modify_asset(asset_id='', comment='foo') + + with self.assertRaises(RequiredArgument): + self.gmp.modify_asset('', comment='foo') + + def test_modify_asset_with_comment(self): + self.gmp.modify_asset('a1', 'foo') + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + ) + + self.gmp.modify_asset('a1', comment='foo') + + self.connection.send.has_been_called_with( + '' + 'foo' + '' + ) + + self.gmp.modify_asset('a1', '') + + self.connection.send.has_been_called_with( + '' + '' + '' + ) + + self.gmp.modify_asset('a1', None) + + self.connection.send.has_been_called_with( + '' + '' + '' + ) + + +if __name__ == '__main__': + unittest.main()