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
|
|
|
import unittest |
20
|
|
|
from unittest.mock import patch |
21
|
|
|
import importlib |
22
|
|
|
from pathlib import Path |
23
|
|
|
from argparse import Namespace |
24
|
|
|
from lxml import etree |
25
|
|
|
from . import GmpMockFactory |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
CWD = Path(__file__).absolute().parent |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class SendTargetTestCase(unittest.TestCase): |
32
|
|
|
def setUp(self): |
33
|
|
|
self.send_targets = importlib.import_module( |
34
|
|
|
'scripts.send-targets', 'gvmtools' |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
@patch('gvm.protocols.latest.Gmp', new_callable=GmpMockFactory) |
38
|
|
|
def test_sent_target(self, mock_gmp: GmpMockFactory): |
39
|
|
|
target_xml_path = CWD / 'example_target.xml' |
40
|
|
|
target_xml_str = target_xml_path.read_text() |
41
|
|
|
|
42
|
|
|
mock_gmp.mock_response( |
43
|
|
|
'get_credentials', |
44
|
|
|
""" |
45
|
|
|
<get_credentials_response status="200" status_text="OK"> |
46
|
|
|
<credential id="6da5b7de-92ad-4dd2-8610-d5711b9c5937"> |
47
|
|
|
</credential> |
48
|
|
|
<credential id="7802648d-1a31-4f69-bb30-00766a1ae1e6"> |
49
|
|
|
</credential> |
50
|
|
|
<credential id="70a63257-4923-4bf4-a9bb-dd8b710b2d80"> |
51
|
|
|
</credential> |
52
|
|
|
<credential id="2bac0c76-795e-4742-b17a-808a0ec8e409"> |
53
|
|
|
</credential> |
54
|
|
|
</get_credentials_response> |
55
|
|
|
""", |
56
|
|
|
) |
57
|
|
|
mock_gmp.mock_response( |
58
|
|
|
'create_target', |
59
|
|
|
'<create_target_response status="201" status_text="OK,' |
60
|
|
|
'resource created" id="6c9f73f5-f14c-42bf-ab44-edb8d2493dbc"/>', |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
target = etree.XML(target_xml_str) |
64
|
|
|
|
65
|
|
|
self.send_targets.parse_send_xml_tree(mock_gmp.gmp_protocol, target) |
66
|
|
|
|
67
|
|
|
def test_args(self): |
68
|
|
|
args = Namespace(script=['foo']) |
69
|
|
|
with self.assertRaises(SystemExit): |
70
|
|
|
self.send_targets.check_args(args) |
71
|
|
|
|
72
|
|
|
args2 = Namespace(script=['foo', 'bar', 'baz']) |
73
|
|
|
|
74
|
|
|
with self.assertRaises(SystemExit): |
75
|
|
|
self.send_targets.check_args(args2) |
76
|
|
|
|
77
|
|
|
@patch('builtins.input', lambda *args: 'y') |
78
|
|
|
def test_yes(self): |
79
|
|
|
yes = self.send_targets.yes_or_no('foo?') |
80
|
|
|
self.assertTrue(yes) |
81
|
|
|
|
82
|
|
|
@patch('builtins.input', lambda *args: 'n') |
83
|
|
|
def test_no(self): |
84
|
|
|
no = self.send_targets.yes_or_no('bar?') |
85
|
|
|
self.assertFalse(no) |
86
|
|
|
|
87
|
|
|
def test_error_and_exit(self): |
88
|
|
|
with self.assertRaises(SystemExit): |
89
|
|
|
self.send_targets.error_and_exit('foo') |
90
|
|
|
|
91
|
|
|
def test_create_xml_tree(self): |
92
|
|
|
target_xml_path = CWD / 'example_target.xml' |
93
|
|
|
|
94
|
|
|
tree = self.send_targets.create_xml_tree(str(target_xml_path)) |
95
|
|
|
self.assertIsInstance( |
96
|
|
|
tree, etree._Element # pylint: disable=protected-access |
97
|
|
|
) |
98
|
|
|
self.assertEqual(tree.tag, 'get_targets_response') |
99
|
|
|
|
100
|
|
|
def test_create_xml_tree_invalid_file(self): |
101
|
|
|
target_xml_path = CWD / 'invalid_file.xml' |
102
|
|
|
|
103
|
|
|
with self.assertRaises(SystemExit): |
104
|
|
|
with self.assertRaises(OSError): |
105
|
|
|
self.send_targets.create_xml_tree(str(target_xml_path)) |
106
|
|
|
|
107
|
|
|
def test_create_xml_tree_invalid_xml(self): |
108
|
|
|
target_xml_path = CWD / 'invalid_xml.xml' |
109
|
|
|
|
110
|
|
|
with self.assertRaises(SystemExit): |
111
|
|
|
with self.assertRaises(etree.Error): |
112
|
|
|
self.send_targets.create_xml_tree(str(target_xml_path)) |
113
|
|
|
|