def test_get_targets(self): transform = ObjectTransform() root = get_root_from_file("test_get_targets.xml") permissions = Permission("Everything") target_expected1 = Target( gmp=None, uuid="f8fac1da-de4f-461a-b4cc-7aff71734ec7", owner=Owner("admin"), name="Test target", comment="Test comment", creation_time=datetime.datetime(2020, 3, 3, 10, 5, 26), modification_time=datetime.datetime(2020, 3, 3, 10, 5, 26), writable=True, in_use=True, permissions=permissions, hosts=["192.168.212.44", "192.168.212.46"], exclude_hosts=["192.168.212.45"], reverse_lookup_only=False, reverse_lookup_unify=False, trash=None, all_info_loaded=False, _port_list=None, ) target_expected2 = Target( gmp=None, uuid="7d27b6b7-b9ec-4543-9620-6e39c74a0c57", owner=Owner("admin"), name="Target for Test - 2020-03-03 10:37:49", comment="Automatically generated by wizard", creation_time=datetime.datetime(2020, 3, 3, 10, 37, 49), modification_time=datetime.datetime(2020, 3, 3, 10, 37, 49), writable=True, in_use=True, permissions=permissions, reverse_lookup_only=False, reverse_lookup_unify=False, trash=None, all_info_loaded=False, _port_list=None, ) targets = [target_expected1, target_expected2] response = transform(root) self.assertEqual(response.targets, targets)
def test_task_load_current_report(self): with patch('gvm.protocols.gmpv9.Gmp') as gmp_mock: xml = get_root_from_file("test_get_report.xml") parser = etree.XMLParser(encoding="utf-8", recover=True, huge_tree=True) root = etree.XML(xml, parser) gmp_mock.get_report.return_value = GetReportsResponse(None, root) report_before = Report( uuid="c96b26fb-22df-4a79-9d20-c579a5fa5533", all_info_loaded=False, ) debug = ResultCounter(0, 0) hole = ResultCounter(0, 0) info = ResultCounter(0, 0) log = ResultCounter(0, 0) warning = ResultCounter(0, 0) false_positiv = ResultCounter(0, 0) report_expected = Report( gmp=None, uuid="c96b26fb-22df-4a79-9d20-c579a5fa5533", format_id="", content_type="application/xml", owner=Owner("admin"), name="2020-03-03T10:05:42Z", comment="Test Comment", creation_time=datetime.datetime(2020, 3, 3, 10, 5, 42), modification_time=datetime.datetime(2020, 3, 3, 10, 6, 22), writable=False, in_use=False, gmp_version="9.0", scan_run_status="Done", timestamp=datetime.datetime(2020, 3, 3, 10, 5, 26), scan_start=datetime.datetime(2020, 3, 3, 10, 5, 42), scan_end=datetime.datetime(2020, 3, 3, 10, 6, 22), timezone="Coordinated Universal Time", timezone_abbrev="UTC", result_count=ResultCount(0, 0, 0, debug, hole, info, log, warning, false_positiv), severity=Severity(float(-99.0), float(-99.0)), all_info_loaded=True, _task=ReportTask("1e9844ab-9918-44db-b7d8-9bc32c0b1cee", False), ) task1 = Task(gmp=gmp_mock, _current_report=report_before) report_after = task1.current_report self.assertEqual(report_after, report_expected) task2 = Task() report2 = task2.current_report self.assertIsNone(report2)
def test_get_users(self): transform = ObjectTransform() root = get_root_from_file("test_get_users.xml") groups = [ Group(uuid="62d425f1-41af-4b62-b6a8-ea0201490eab", name="TestGruppe1"), Group(uuid="62d425f1-41af-4b62-b6a8-ea0201490ebb", name="TestGruppe2"), ] user_expected1 = User( uuid="d6dcf91b-e9cf-4700-9d33-34c92ce1c3c8", owner=None, name="admin", comment="comment", creation_time=datetime.datetime(2020, 6, 4, 9, 57, 37), modification_time=datetime.datetime(2020, 6, 4, 9, 57, 37), writable=True, in_use=False, groups=groups, permissions=Permission(name="get_users"), user_tags=None, ) user_expected2 = User( uuid="ec3266fd-1a35-40db-ab5a-794de9060e04", owner=Owner(name="admin"), name="TestUser", comment="Tester", creation_time=datetime.datetime(2020, 6, 4, 14, 35, 20), modification_time=datetime.datetime(2020, 6, 4, 14, 35, 20), writable=True, in_use=False, groups=groups, permissions=Permission(name="Everything"), user_tags=UserTags(count=1, tags=[]), ) users_expected = [user_expected1, user_expected2] response = transform(root) self.assertIsInstance(response, GetUsersResponse) self.assertEqual(response.users, users_expected)
def test_get_port_list(self): transform = ObjectTransform() root = get_root_from_file("test_get_port_list.xml") permissions = [ Permission("Test_Permission1"), Permission("Test_Permission2"), ] port_count = PortCount(3, 2, 1) port_ranges = [] port_ranges.append( PortRange( "1fa9476e-ba37-4d99-a7fa-90d912250c47", 1, 80, "tcp", "TestComment1", )) port_ranges.append( PortRange( "5d203ed9-35ac-48c2-a850-5f9de31af411", 82, 113, "tcp", "TestComment2", )) port_list = PortList( "33d0cd82-57c6-11e1-8ed1-406186ea4fc5", Owner("Test_name"), "All IANA assigned TCP 2012-02-10", "TestComment", datetime.datetime(2020, 3, 2, 10, 48, 37), datetime.datetime(2020, 3, 2, 10, 48, 37), False, True, permissions, port_count, port_ranges, ) response = transform(root, None) self.assertIsInstance(response, GetPortListsResponse) self.assertEqual(response.port_lists, port_list)
def test_task_load_target(self): with patch('gvm.protocols.gmpv9.Gmp') as gmp_mock: xml = get_root_from_file("test_get_target.xml") parser = etree.XMLParser(encoding="utf-8", recover=True, huge_tree=True) root = etree.XML(xml, parser) gmp_mock.get_target.return_value = GetTargetsResponse(None, root) target_before = Target( uuid="f8fac1da-de4f-461a-b4cc-7aff71734ec7", all_info_loaded=False, ) target_expected = Target( uuid="f8fac1da-de4f-461a-b4cc-7aff71734ec7", owner=Owner("admin"), name="Test target", comment="Test comment", creation_time=datetime.datetime(2020, 3, 3, 10, 5, 26), modification_time=datetime.datetime(2020, 3, 3, 10, 5, 26), writable=True, in_use=True, hosts=["192.168.212.44", "192.168.212.46"], reverse_lookup_only=False, reverse_lookup_unify=False, trash=None, all_info_loaded=True, _port_list=None, ) task1 = Task(gmp=gmp_mock, _target=target_before) target_after = task1.target self.assertEqual(target_after, target_expected) task2 = Task() target2 = task2.target self.assertIsNone(target2)
def test_get_reports(self): transform = ObjectTransform() root = get_root_from_file("test_get_reports.xml") debug = ResultCounter(0, 0) hole = ResultCounter(0, 0) info = ResultCounter(0, 0) log = ResultCounter(0, 0) log2 = ResultCounter(5, 5) warning = ResultCounter(0, 0) false_positiv = ResultCounter(0, 0) report_expected1 = Report( gmp=None, uuid="c96b26fb-22df-4a79-9d20-c579a5fa5533", format_id="", content_type="application/xml", owner=Owner("admin"), name="2020-03-03T10:05:42Z", comment="Test Comment", creation_time=datetime.datetime(2020, 3, 3, 10, 5, 42), modification_time=datetime.datetime(2020, 3, 3, 10, 6, 22), writable=False, in_use=False, gmp_version="9.0", scan_run_status="Done", timestamp=datetime.datetime(2020, 3, 3, 10, 5, 26), scan_start=datetime.datetime(2020, 3, 3, 10, 5, 42), scan_end=datetime.datetime(2020, 3, 3, 10, 6, 22), timezone="Coordinated Universal Time", timezone_abbrev="UTC", result_count=ResultCount(0, 0, 0, debug, hole, info, log, warning, false_positiv), severity=Severity(float(-99.0), float(-99.0)), all_info_loaded=False, _task=ReportTask("1e9844ab-9918-44db-b7d8-9bc32c0b1cee", False), ) report_expected2 = Report( gmp=None, uuid="0f7c7734-e33c-45ef-a9e4-f624e39312cf", format_id="Test_ID", content_type="application/xml", owner=Owner("admin"), name="2020-03-03T10:38:05Z", comment=None, creation_time=datetime.datetime(2020, 3, 3, 10, 38, 5), modification_time=datetime.datetime(2020, 3, 3, 10, 39, 47), writable=False, in_use=True, gmp_version="9.0", scan_run_status="Done", timestamp=datetime.datetime(2020, 3, 3, 10, 37, 49), scan_start=datetime.datetime(2020, 3, 3, 10, 38, 5), scan_end=datetime.datetime(2020, 3, 3, 10, 39, 47), timezone="Coordinated Universal Time", timezone_abbrev="UTC", result_count=ResultCount(5, 5, 5, debug, hole, info, log2, warning, false_positiv), severity=Severity(float(0.0), float(0.0)), all_info_loaded=False, _task=ReportTask("a16aec9e-9c53-4fde-b42f-3c20311c0afc", False), ) reports = [report_expected1, report_expected2] response = transform(root) self.assertEqual(response.reports, reports)
def test_get_task(self): transform = ObjectTransform() root = get_root_from_file("test_get_task.xml") response = transform(root, None) permissions = [] permissions.append(Permission("Test1")) permissions.append(Permission("Test2")) users = ["admin", "admin_Clone_4"] groups = Group( None, "44ff149f-8817-4419-9d5c-b6c3a8655ab2", None, "plebeians", None, None, None, None, None, None, None, None, ) task_scan_config_expected = TaskScanConfig( "8715c877-47a0-438d-98a3-27c7a6ab2196", False, False) observers = Observers(users, groups) user_tags = UserTags( 1, Tag("6e6f1411-c2ba-4fe6-99bf-470dc67ccade", None, None, None)) task = Task( None, "c6415957-984c-40ba-9906-db6e3390c82d", Owner("admin"), "Discovery", "Test Comment", datetime.datetime(2020, 3, 5, 15, 35, 21), datetime.datetime(2020, 3, 5, 15, 35, 21), True, False, permissions, user_tags, False, "scan", "Test Ordering", # alert, "Done", -1, ReportCount(1, 1), "Test Trend", Schedule("Test Schedule", "over", False), 0, observers, None, False, None, None, task_scan_config_expected, None, None, ) self.assertIsInstance(response, GetTasksResponse) self.assertEqual(response.tasks, task)
def test_get_configs(self): """ This tests also Preferences, because they are part of the get_configs_response """ transform = ObjectTransform() root = get_root_from_file("test_get_configs.xml") permissions = [Permission("get_configs"), Permission("get_configs")] preference_expected1 = Preference( nvt=Nvt("1.3.6.1.4.1.25623.1.0.100151", "PostgreSQL Detection"), preference_id=1, hr_name="Postgres Username:"******"Postgres Username:"******"entry", value="postgres", alternatives=None, default="postgres", ) preference_expected2 = Preference( nvt=Nvt("1.3.6.1.4.1.25623.1.0.97001", "PCI-DSS Version 2.0"), preference_id=1, hr_name="Berichtformat/Report Format", name="Berichtformat/Report Format", preference_type="radio", value="Text", alternatives=[ "Tabellarisch/Tabular", "Text und/and Tabellarisch/Tabular", ], default="Text", ) preferences = [preference_expected1, preference_expected2] config_expected1 = ScanConfig( uuid="d21f6c81-2b88-4ac1-b7b4-a2a9f2ad4663", owner=None, name="Base", comment="Basic configuration template.", creation_time=datetime.datetime(2020, 3, 2, 10, 48, 37), modification_time=datetime.datetime(2020, 3, 2, 10, 48, 37), writable=False, in_use=False, permissions=permissions, family_count=FamilyCount(2, False), nvt_count=NvtCount(3, False), config_type=0, usage_type="scan", max_nvt_count=None, known_nvt_count=None, scanner=None, user_tags=None, tasks=None, preferences=None, trash=None, all_info_loaded=False, ) config_expected2 = ScanConfig( uuid="8715c877-47a0-438d-98a3-27c7a6ab2196", owner=Owner("TestName"), name="Discovery", comment="Network Discovery scan configuration.", creation_time=datetime.datetime(2020, 3, 2, 10, 48, 37), modification_time=datetime.datetime(2020, 3, 2, 10, 48, 37), writable=False, in_use=True, permissions=permissions, family_count=FamilyCount(16, False), nvt_count=NvtCount(3023, True), config_type=0, usage_type="scan", max_nvt_count=239, known_nvt_count=3, scanner=None, user_tags=None, tasks=None, preferences=preferences, trash=None, all_info_loaded=False, ) scan_configs = [config_expected1, config_expected2] response = transform(root) self.assertEqual(response.scan_configs, scan_configs)