1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2020 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see<http://www.gnu.org/licenses/> |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
import unittest |
21
|
|
|
from unittest.mock import patch, MagicMock |
22
|
|
|
from pathlib import Path |
23
|
|
|
from lxml import etree |
24
|
|
|
from . import GmpMockFactory, load_script |
25
|
|
|
|
26
|
|
|
CWD = Path(__file__).absolute().parent |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class SendTasksTestCase(unittest.TestCase): |
30
|
|
|
def setUp(self): |
31
|
|
|
self.send_tasks = load_script( |
32
|
|
|
(CWD.parent.parent / 'scripts'), 'send-tasks' |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
@patch('builtins.input', lambda *args: 'y') |
36
|
|
|
@patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory) |
37
|
|
|
def test_sent_task(self, mock_gmp: GmpMockFactory): |
38
|
|
|
task_xml_path = CWD / 'example_task.xml' |
39
|
|
|
task_xml_str = task_xml_path.read_text() |
40
|
|
|
|
41
|
|
|
self.send_tasks.numerical_option = MagicMock(return_value=1) |
42
|
|
|
|
43
|
|
|
mock_gmp.mock_response( |
44
|
|
|
'get_configs', |
45
|
|
|
'<get_configs_response status="200" status_text="OK">' |
46
|
|
|
'<config id="d21f6c81-2b88-4ac1-b7b4-a2a9f2ad4663">' |
47
|
|
|
'<name>Base</name>' |
48
|
|
|
'</config>' |
49
|
|
|
'<config id="3fe6b460-e6ca-4af7-b712-1d7e9ea96eb0">' |
50
|
|
|
'<name>BSI TR-03116: Part 4 (Date: 10. Januar 2020)</name>' |
51
|
|
|
'</config>' |
52
|
|
|
'<config id="8715c877-47a0-438d-98a3-27c7a6ab2196">' |
53
|
|
|
'<name>Discovery</name>' |
54
|
|
|
'</config>' |
55
|
|
|
'</get_configs_response>', |
56
|
|
|
) |
57
|
|
|
|
58
|
|
|
mock_gmp.mock_response( |
59
|
|
|
'get_scanners', |
60
|
|
|
'<get_scanners_response status="200" status_text="OK">' |
61
|
|
|
'<scanner id="c1c85af7-0cca-4690-8ccc-c79feb5588cf">' |
62
|
|
|
'<name>as</name>' |
63
|
|
|
'</scanner>' |
64
|
|
|
'<scanner id="6acd0832-df90-11e4-b9d5-28d24461215b">' |
65
|
|
|
'<name>CVE</name>' |
66
|
|
|
'</scanner>' |
67
|
|
|
'<scanner id="08b69003-5fc2-4037-a479-93b440211c73">' |
68
|
|
|
'<name>OpenVAS Default</name>' |
69
|
|
|
'</scanner>' |
70
|
|
|
'</get_scanners_response>', |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
mock_gmp.mock_response( |
74
|
|
|
'get_targets', |
75
|
|
|
'<get_targets_response status="200" status_text="OK">' |
76
|
|
|
'<target id="60f95d0e-029e-4931-a13a-b1d11260517d">' |
77
|
|
|
'<name>own</name>' |
78
|
|
|
'</target>' |
79
|
|
|
'<target id="ead9576c-5a4d-4081-b98d-ccd77d5d16f8">' |
80
|
|
|
'<name>Target for xn</name>' |
81
|
|
|
'</target>' |
82
|
|
|
'<target id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc">' |
83
|
|
|
'<name>Unnamed</name>' |
84
|
|
|
'</target>' |
85
|
|
|
'<target id="a1f478c1-27d0-4d8c-959f-150625186421">' |
86
|
|
|
'<name>work</name>' |
87
|
|
|
'</target>' |
88
|
|
|
'<target id="5ca97fe1-694d-4e4a-bd4c-55529719d17e">' |
89
|
|
|
'<name>work2</name>' |
90
|
|
|
'</target>' |
91
|
|
|
'</get_targets_response>', |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
mock_gmp.mock_response( |
95
|
|
|
'create_task', |
96
|
|
|
'<create_task_response status="201" status_text="OK,' |
97
|
|
|
'resource created" id="c8ef0597-e2c1-4e23-869f-072fa2914bf2"/>', |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
task = etree.XML(task_xml_str) |
101
|
|
|
|
102
|
|
|
self.send_tasks.parse_send_xml_tree(mock_gmp.gmp_protocol, task) |
103
|
|
|
|