1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2019-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
|
|
|
|
21
|
|
|
from tests.protocols import GmpTestCase |
22
|
|
|
|
23
|
|
|
from gvm.errors import GvmError |
24
|
|
|
|
25
|
|
|
from gvm.protocols.gmp import Gmp |
26
|
|
|
from gvm.protocols.gmpv7 import Gmp as Gmpv7 |
27
|
|
|
from gvm.protocols.gmpv8 import Gmp as Gmpv8 |
28
|
|
|
from gvm.protocols.gmpv9 import Gmp as Gmpv9 |
29
|
|
|
from gvm.protocols.gmpv208 import Gmp as Gmpv208 |
30
|
|
|
from gvm.protocols.gmpv214 import Gmp as Gmpv214 |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class GmpContextManagerTestCase(GmpTestCase): |
34
|
|
|
|
35
|
|
|
gmp_class = Gmp |
36
|
|
|
|
37
|
|
|
def test_select_gmpv7(self): |
38
|
|
|
self.connection.read.return_value( |
39
|
|
|
'<get_version_response status="200" status_text="OK">' |
40
|
|
|
'<version>7.0</version>' |
41
|
|
|
'</get_version_response>' |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
with self.gmp as gmp: |
45
|
|
|
self.assertEqual(gmp.get_protocol_version(), (7,)) |
46
|
|
|
self.assertIsInstance(gmp, Gmpv7) |
47
|
|
|
|
48
|
|
|
def test_select_gmpv8(self): |
49
|
|
|
self.connection.read.return_value( |
50
|
|
|
'<get_version_response status="200" status_text="OK">' |
51
|
|
|
'<version>8.0</version>' |
52
|
|
|
'</get_version_response>' |
53
|
|
|
) |
54
|
|
|
|
55
|
|
|
with self.gmp as gmp: |
56
|
|
|
self.assertEqual(gmp.get_protocol_version(), (8,)) |
57
|
|
|
self.assertIsInstance(gmp, Gmpv8) |
58
|
|
|
|
59
|
|
|
def test_select_gmpv9(self): |
60
|
|
|
self.connection.read.return_value( |
61
|
|
|
'<get_version_response status="200" status_text="OK">' |
62
|
|
|
'<version>9.0</version>' |
63
|
|
|
'</get_version_response>' |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
with self.gmp as gmp: |
67
|
|
|
self.assertEqual(gmp.get_protocol_version(), (9,)) |
68
|
|
|
self.assertIsInstance(gmp, Gmpv9) |
69
|
|
|
|
70
|
|
|
def test_select_gmpv208(self): |
71
|
|
|
self.connection.read.return_value( |
72
|
|
|
'<get_version_response status="200" status_text="OK">' |
73
|
|
|
'<version>20.08</version>' |
74
|
|
|
'</get_version_response>' |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
with self.gmp as gmp: |
78
|
|
|
self.assertEqual(gmp.get_protocol_version(), (20, 8)) |
79
|
|
|
self.assertIsInstance(gmp, Gmpv208) |
80
|
|
|
|
81
|
|
|
def test_select_gmpv214(self): |
82
|
|
|
self.connection.read.return_value( |
83
|
|
|
'<get_version_response status="200" status_text="OK">' |
84
|
|
|
'<version>21.04</version>' |
85
|
|
|
'</get_version_response>' |
86
|
|
|
) |
87
|
|
|
|
88
|
|
|
with self.gmp as gmp: |
89
|
|
|
self.assertEqual(gmp.get_protocol_version(), (21, 4)) |
90
|
|
|
self.assertIsInstance(gmp, Gmpv214) |
91
|
|
|
|
92
|
|
|
def test_unknown_protocol(self): |
93
|
|
|
self.connection.read.return_value( |
94
|
|
|
'<get_version_response status="200" status_text="OK">' |
95
|
|
|
'<version>1.0</version>' |
96
|
|
|
'</get_version_response>' |
97
|
|
|
) |
98
|
|
|
|
99
|
|
|
with self.assertRaises(GvmError): |
100
|
|
|
with self.gmp: |
101
|
|
|
pass |
102
|
|
|
|
103
|
|
|
def test_missing_version_in_response(self): |
104
|
|
|
self.connection.read.return_value( |
105
|
|
|
'<get_version_response status="200" status_text="OK">' |
106
|
|
|
'<foo>bar</foo>' |
107
|
|
|
'</get_version_response>' |
108
|
|
|
) |
109
|
|
|
|
110
|
|
|
with self.assertRaises(GvmError): |
111
|
|
|
with self.gmp: |
112
|
|
|
pass |
113
|
|
|
|
114
|
|
|
def test_invalid_response(self): |
115
|
|
|
self.connection.read.return_value('<get_foo_response/>') |
116
|
|
|
|
117
|
|
|
with self.assertRaises(GvmError): |
118
|
|
|
with self.gmp: |
119
|
|
|
pass |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
if __name__ == '__main__': |
123
|
|
|
unittest.main() |
124
|
|
|
|