1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2021 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 |
22
|
|
|
from pathlib import Path |
23
|
|
|
from argparse import Namespace |
24
|
|
|
from lxml import etree |
25
|
|
|
from . import GmpMockFactory, load_script |
26
|
|
|
|
27
|
|
|
CWD = Path(__file__).absolute().parent |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class CombineReportsTestCase(unittest.TestCase): |
31
|
|
|
def setUp(self): |
32
|
|
|
self.combine_reports = load_script( |
33
|
|
|
(CWD.parent.parent / 'scripts'), 'combine-reports' |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
@patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory) |
37
|
|
|
def test_combine_reports(self, mock_gmp: GmpMockFactory): |
38
|
|
|
# bah! |
39
|
|
|
mock_gmp.mock_responses( |
40
|
|
|
'get_report', |
41
|
|
|
[ |
42
|
|
|
'<get_reports_response status="200" status_text="OK">' |
43
|
|
|
'<report id="00000000-0000-0000-0000-000000000000">' |
44
|
|
|
'<name>2020-11-13T14:47:28Z</name>' |
45
|
|
|
'<creation_time>2020-11-13T14:47:28Z</creation_time>' |
46
|
|
|
'<modification_time>2020-11-13T14:47:28Z</modification_time>' |
47
|
|
|
'<task id="00000000-0000-0000-0001-000000000000">' |
48
|
|
|
'<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>' |
49
|
|
|
'</task>' |
50
|
|
|
'<report id="00000000-0000-0000-0000-000000000000">' |
51
|
|
|
'<scan_run_status>Done</scan_run_status>' |
52
|
|
|
'<timestamp>2020-11-13T14:47:48Z</timestamp>' |
53
|
|
|
'<scan_start>2020-11-13T14:47:28Z</scan_start>' |
54
|
|
|
'<results start="1" max="100">' |
55
|
|
|
'<result id="00000001-0000-0000-0000-000000000000">' |
56
|
|
|
'</result>' |
57
|
|
|
'<result id="00000001-0000-0000-0000-000000000001">' |
58
|
|
|
'</result>' |
59
|
|
|
'</results>' |
60
|
|
|
'<scan_end>2020-11-13T14:47:28Z</scan_end>' |
61
|
|
|
'</report>' |
62
|
|
|
'</report>' |
63
|
|
|
'</get_reports_response>', |
64
|
|
|
'<get_reports_response status="200" status_text="OK">' |
65
|
|
|
'<report id="00000000-0000-0000-0000-000000000001">' |
66
|
|
|
'<name>2020-11-13T14:47:28Z</name>' |
67
|
|
|
'<creation_time>2020-11-13T14:47:28Z</creation_time>' |
68
|
|
|
'<modification_time>2020-11-13T14:47:28Z</modification_time>' |
69
|
|
|
'<task id="00000000-0000-0000-0002-000000000000">' |
70
|
|
|
'<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>' |
71
|
|
|
'</task>' |
72
|
|
|
'<report id="00000000-0000-0000-0000-000000000001">' |
73
|
|
|
'<scan_run_status>Done</scan_run_status>' |
74
|
|
|
'<timestamp>2020-11-13T14:47:48Z</timestamp>' |
75
|
|
|
'<scan_start>2020-11-13T14:47:28Z</scan_start>' |
76
|
|
|
'<results start="1" max="100">' |
77
|
|
|
'<result id="00000001-0000-0000-0000-000000000002">' |
78
|
|
|
'</result>' |
79
|
|
|
'<result id="00000001-0000-0000-0000-000000000003">' |
80
|
|
|
'</result>' |
81
|
|
|
'</results>' |
82
|
|
|
'<scan_end>2020-11-13T14:47:28Z</scan_end>' |
83
|
|
|
'</report>' |
84
|
|
|
'</report>' |
85
|
|
|
'</get_reports_response>', |
86
|
|
|
'<get_reports_response status="200" status_text="OK">' |
87
|
|
|
'<report id="00000000-0000-0000-0000-000000000002">' |
88
|
|
|
'<name>2020-11-13T14:47:28Z</name>' |
89
|
|
|
'<creation_time>2020-11-13T14:47:28Z</creation_time>' |
90
|
|
|
'<modification_time>2020-11-13T14:47:28Z</modification_time>' |
91
|
|
|
'<task id="00000000-0000-0000-0003-000000000000">' |
92
|
|
|
'<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>' |
93
|
|
|
'</task>' |
94
|
|
|
'<report id="00000000-0000-0000-0000-000000000002">' |
95
|
|
|
'<scan_run_status>Done</scan_run_status>' |
96
|
|
|
'<timestamp>2020-11-13T14:47:48Z</timestamp>' |
97
|
|
|
'<scan_start>2020-11-13T14:47:28Z</scan_start>' |
98
|
|
|
'<results start="1" max="100">' |
99
|
|
|
'<result id="00000001-0000-0000-0000-000000000004">' |
100
|
|
|
'</result>' |
101
|
|
|
'</results>' |
102
|
|
|
'<scan_end>2020-11-13T14:47:28Z</scan_end>' |
103
|
|
|
'</report>' |
104
|
|
|
'</report>' |
105
|
|
|
'</get_reports_response>', |
106
|
|
|
], |
107
|
|
|
) |
108
|
|
|
args = Namespace( |
109
|
|
|
script=[ |
110
|
|
|
'foo', |
111
|
|
|
'00000000-0000-0000-0000-000000000000', |
112
|
|
|
'00000000-0000-0000-0000-000000000001', |
113
|
|
|
'00000000-0000-0000-0000-000000000002', |
114
|
|
|
] |
115
|
|
|
) |
116
|
|
|
|
117
|
|
|
combined_report = self.combine_reports.combine_reports( |
118
|
|
|
gmp=mock_gmp.gmp_protocol, args=args |
119
|
|
|
) |
120
|
|
|
|
121
|
|
|
results = ( |
122
|
|
|
combined_report.find('report').find('results').findall('result') |
123
|
|
|
) |
124
|
|
|
i = 0 |
125
|
|
|
for result in results: |
126
|
|
|
self.assertEqual( |
127
|
|
|
result.get('id'), f'00000001-0000-0000-0000-00000000000{str(i)}' |
128
|
|
|
) |
129
|
|
|
i += 1 |
130
|
|
|
|
131
|
|
|
self.assertEqual(i, 5) |
132
|
|
|
|
133
|
|
|
@patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory) |
134
|
|
|
def test_send_report(self, mock_gmp: GmpMockFactory): |
135
|
|
|
|
136
|
|
|
args = Namespace( |
137
|
|
|
script=[ |
138
|
|
|
'foo', |
139
|
|
|
'00000000-0000-0000-0000-000000000000', |
140
|
|
|
'00000000-0000-0000-0000-000000000001', |
141
|
|
|
'00000000-0000-0000-0000-000000000002', |
142
|
|
|
] |
143
|
|
|
) |
144
|
|
|
|
145
|
|
|
combined_report = etree.fromstring( |
146
|
|
|
'<report id="20574712-c404-4a04-9c83-03144ae02dca" ' |
147
|
|
|
'format_id="d5da9f67-8551-4e51-807b-b6a873d70e34" ' |
148
|
|
|
'extension="xml" content_type="text/xml">' |
149
|
|
|
'<report id="20574712-c404-4a04-9c83-03144ae02dca">' |
150
|
|
|
'<results start="1" max="-1">' |
151
|
|
|
'<result id="00000001-0000-0000-0000-000000000000"/>' |
152
|
|
|
'<result id="00000001-0000-0000-0000-000000000001"/>' |
153
|
|
|
'<result id="00000001-0000-0000-0000-000000000002"/>' |
154
|
|
|
'<result id="00000001-0000-0000-0000-000000000003"/>' |
155
|
|
|
'<result id="00000001-0000-0000-0000-000000000004"/>' |
156
|
|
|
'</results></report></report>' |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
report_id = '0e4d8fb2-47fa-494e-a242-d5327d3772f9' |
160
|
|
|
|
161
|
|
|
mock_gmp.mock_response( |
162
|
|
|
'import_report', |
163
|
|
|
'<create_report_response status="201" status_text="OK, ' |
164
|
|
|
f'resource created" id="{report_id}"/>', |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
mock_gmp.mock_response( |
168
|
|
|
'create_container_task', |
169
|
|
|
'<create_task_response status="201" status_text="OK, ' |
170
|
|
|
'resource created" id="6488ef71-e2d5-491f-95bd-ed9f915fa179"/>', |
171
|
|
|
) |
172
|
|
|
|
173
|
|
|
created_report_id = self.combine_reports.send_report( |
174
|
|
|
gmp=mock_gmp.gmp_protocol, |
175
|
|
|
args=args, |
176
|
|
|
combined_report=combined_report, |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
self.assertEqual(report_id, created_report_id) |
180
|
|
|
|