1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018 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
|
|
|
import unittest |
20
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpImportReportTestCase: |
25
|
|
|
|
26
|
|
|
TASK_ID = '00000000-0000-0000-0000-000000000001' |
27
|
|
|
REPORT_XML_STRING = ( |
28
|
|
|
'<report id="67a62fb7-b238-4f0e-bc48-59bde8939cdc">' |
29
|
|
|
'<results max="1" start="1">' |
30
|
|
|
'<result id="f180b40f-49dd-4856-81ed-8c1195afce80">' |
31
|
|
|
'<severity>0.0</severity>' |
32
|
|
|
'<nvt oid="1.3.6.1.4.1.25623.1.0.10330"/>' |
33
|
|
|
'<host>132.67.253.114</host>' |
34
|
|
|
'</result></results></report>' |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
def test_import_report_with_task_id(self): |
38
|
|
|
self.gmp.import_report(self.REPORT_XML_STRING, task_id=self.TASK_ID) |
39
|
|
|
|
40
|
|
|
self.connection.send.has_been_called_with( |
41
|
|
|
'<create_report>' |
42
|
|
|
'<task id="{task}"/>' |
43
|
|
|
'{report}' |
44
|
|
|
'</create_report>'.format( |
45
|
|
|
task=self.TASK_ID, report=self.REPORT_XML_STRING |
46
|
|
|
) |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
def test_import_report_missing_report(self): |
50
|
|
|
with self.assertRaises(RequiredArgument): |
51
|
|
|
self.gmp.import_report( |
52
|
|
|
None, task_id=self.TASK_ID, |
53
|
|
|
) |
54
|
|
|
|
55
|
|
|
with self.assertRaises(RequiredArgument): |
56
|
|
|
self.gmp.import_report( |
57
|
|
|
'', task_id=self.TASK_ID, |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
def test_import_report_missing_task(self): |
61
|
|
|
with self.assertRaises(RequiredArgument): |
62
|
|
|
self.gmp.import_report(self.REPORT_XML_STRING) |
63
|
|
|
|
64
|
|
|
def test_import_report_invalid_xml(self): |
65
|
|
|
with self.assertRaises(InvalidArgument): |
66
|
|
|
self.gmp.import_report( |
67
|
|
|
'Foo', # not root tag |
68
|
|
|
task_id=self.TASK_ID, |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
with self.assertRaises(InvalidArgument): |
72
|
|
|
self.gmp.import_report( |
73
|
|
|
'<Foo>', # missing closing tag |
74
|
|
|
task_id=self.TASK_ID, |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
def test_import_report_with_in_assets(self): |
78
|
|
|
self.gmp.import_report( |
79
|
|
|
self.REPORT_XML_STRING, task_id=self.TASK_ID, in_assets=False |
80
|
|
|
) |
81
|
|
|
|
82
|
|
|
self.connection.send.has_been_called_with( |
83
|
|
|
'<create_report>' |
84
|
|
|
'<task id="{task}"/>' |
85
|
|
|
'<in_assets>0</in_assets>' |
86
|
|
|
'{report}' |
87
|
|
|
'</create_report>'.format( |
88
|
|
|
task=self.TASK_ID, report=self.REPORT_XML_STRING |
89
|
|
|
) |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
self.gmp.import_report( |
93
|
|
|
self.REPORT_XML_STRING, task_id=self.TASK_ID, in_assets=True |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
self.connection.send.has_been_called_with( |
97
|
|
|
'<create_report>' |
98
|
|
|
'<task id="{task}"/>' |
99
|
|
|
'<in_assets>1</in_assets>' |
100
|
|
|
'{report}' |
101
|
|
|
'</create_report>'.format( |
102
|
|
|
task=self.TASK_ID, report=self.REPORT_XML_STRING |
103
|
|
|
) |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
if __name__ == '__main__': |
108
|
|
|
unittest.main() |
109
|
|
|
|
110
|
|
|
|