1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2021-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from argparse import Namespace
from pathlib import Path
from unittest.mock import patch
from lxml import etree
from . import GmpMockFactory, load_script
CWD = Path(__file__).absolute().parent
class CombineReportsTestCase(unittest.TestCase):
def setUp(self):
self.combine_reports = load_script(
(CWD.parent.parent / "scripts"), "combine-reports"
)
@patch("gvm.protocols.latest.Gmp", new_callable=GmpMockFactory)
def test_combine_reports(self, mock_gmp: GmpMockFactory):
# bah!
mock_gmp.mock_responses(
"get_report",
[
'<get_reports_response status="200" status_text="OK">'
'<report id="00000000-0000-0000-0000-000000000000">'
"<name>2020-11-13T14:47:28Z</name>"
"<creation_time>2020-11-13T14:47:28Z</creation_time>"
"<modification_time>2020-11-13T14:47:28Z</modification_time>"
'<task id="00000000-0000-0000-0001-000000000000">'
"<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>"
"</task>"
'<report id="00000000-0000-0000-0000-000000000000">'
"<scan_run_status>Done</scan_run_status>"
"<timestamp>2020-11-13T14:47:48Z</timestamp>"
"<scan_start>2020-11-13T14:47:28Z</scan_start>"
'<results start="1" max="100">'
'<result id="00000001-0000-0000-0000-000000000000">'
"</result>"
'<result id="00000001-0000-0000-0000-000000000001">'
"</result>"
"</results>"
"<scan_end>2020-11-13T14:47:28Z</scan_end>"
"</report>"
"</report>"
"</get_reports_response>",
'<get_reports_response status="200" status_text="OK">'
'<report id="00000000-0000-0000-0000-000000000001">'
"<name>2020-11-13T14:47:28Z</name>"
"<creation_time>2020-11-13T14:47:28Z</creation_time>"
"<modification_time>2020-11-13T14:47:28Z</modification_time>"
'<task id="00000000-0000-0000-0002-000000000000">'
"<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>"
"</task>"
'<report id="00000000-0000-0000-0000-000000000001">'
"<scan_run_status>Done</scan_run_status>"
"<timestamp>2020-11-13T14:47:48Z</timestamp>"
"<scan_start>2020-11-13T14:47:28Z</scan_start>"
'<results start="1" max="100">'
'<result id="00000001-0000-0000-0000-000000000002">'
"</result>"
'<result id="00000001-0000-0000-0000-000000000003">'
"</result>"
"</results>"
"<scan_end>2020-11-13T14:47:28Z</scan_end>"
"</report>"
"</report>"
"</get_reports_response>",
'<get_reports_response status="200" status_text="OK">'
'<report id="00000000-0000-0000-0000-000000000002">'
"<name>2020-11-13T14:47:28Z</name>"
"<creation_time>2020-11-13T14:47:28Z</creation_time>"
"<modification_time>2020-11-13T14:47:28Z</modification_time>"
'<task id="00000000-0000-0000-0003-000000000000">'
"<name>Offline Scan from 2020-11-13T15:47:28+01:00 8</name>"
"</task>"
'<report id="00000000-0000-0000-0000-000000000002">'
"<scan_run_status>Done</scan_run_status>"
"<timestamp>2020-11-13T14:47:48Z</timestamp>"
"<scan_start>2020-11-13T14:47:28Z</scan_start>"
'<results start="1" max="100">'
'<result id="00000001-0000-0000-0000-000000000004">'
"</result>"
"</results>"
"<scan_end>2020-11-13T14:47:28Z</scan_end>"
"</report>"
"</report>"
"</get_reports_response>",
],
)
args = Namespace(
script=[
"foo",
"00000000-0000-0000-0000-000000000000",
"00000000-0000-0000-0000-000000000001",
"00000000-0000-0000-0000-000000000002",
]
)
combined_report = self.combine_reports.combine_reports(
gmp=mock_gmp.gmp_protocol, args=args
)
results = (
combined_report.find("report").find("results").findall("result")
)
i = 0
for result in results:
self.assertEqual(
result.get("id"), f"00000001-0000-0000-0000-00000000000{str(i)}"
)
i += 1
self.assertEqual(i, 5)
@patch("gvm.protocols.latest.Gmp", new_callable=GmpMockFactory)
def test_send_report(self, mock_gmp: GmpMockFactory):
args = Namespace(
script=[
"foo",
"00000000-0000-0000-0000-000000000000",
"00000000-0000-0000-0000-000000000001",
"00000000-0000-0000-0000-000000000002",
]
)
combined_report = etree.fromstring(
'<report id="20574712-c404-4a04-9c83-03144ae02dca" '
'format_id="d5da9f67-8551-4e51-807b-b6a873d70e34" '
'extension="xml" content_type="text/xml">'
'<report id="20574712-c404-4a04-9c83-03144ae02dca">'
'<results start="1" max="-1">'
'<result id="00000001-0000-0000-0000-000000000000"/>'
'<result id="00000001-0000-0000-0000-000000000001"/>'
'<result id="00000001-0000-0000-0000-000000000002"/>'
'<result id="00000001-0000-0000-0000-000000000003"/>'
'<result id="00000001-0000-0000-0000-000000000004"/>'
"</results></report></report>"
)
report_id = "0e4d8fb2-47fa-494e-a242-d5327d3772f9"
mock_gmp.mock_response(
"import_report",
'<create_report_response status="201" status_text="OK, '
f'resource created" id="{report_id}"/>',
)
mock_gmp.mock_response(
"create_container_task",
'<create_task_response status="201" status_text="OK, '
'resource created" id="6488ef71-e2d5-491f-95bd-ed9f915fa179"/>',
)
created_report_id = self.combine_reports.send_report(
gmp=mock_gmp.gmp_protocol,
args=args,
combined_report=combined_report,
)
self.assertEqual(report_id, created_report_id)
|