Passed
Pull Request — master (#186)
by Jaspar
12:57
created

GmpContextManagerTestCase.test_select_gmpv7()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019 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
30
31
class GmpContextManagerTestCase(GmpTestCase):
32
33
    gmp_class = Gmp
34
35
    def test_select_gmpv7(self):
36
        self.connection.read.return_value(
37
            '<get_version_response status="200" status_text="OK">'
38
            '<version>7.0</version>'
39
            '</get_version_response>'
40
        )
41
42
        with self.gmp as gmp:
43
            self.assertEqual(gmp.get_protocol_version(), (7,))
44
            self.assertIsInstance(gmp, Gmpv7)
45
46
    def test_select_gmpv8(self):
47
        self.connection.read.return_value(
48
            '<get_version_response status="200" status_text="OK">'
49
            '<version>8.0</version>'
50
            '</get_version_response>'
51
        )
52
53
        with self.gmp as gmp:
54
            self.assertEqual(gmp.get_protocol_version(), (8,))
55
            self.assertIsInstance(gmp, Gmpv8)
56
57
    def test_select_gmpv9(self):
58
        self.connection.read.return_value(
59
            '<get_version_response status="200" status_text="OK">'
60
            '<version>9.0</version>'
61
            '</get_version_response>'
62
        )
63
64
        with self.gmp as gmp:
65
            self.assertEqual(gmp.get_protocol_version(), (9,))
66
            self.assertIsInstance(gmp, Gmpv9)
67
68
    def test_unknown_protocol(self):
69
        self.connection.read.return_value(
70
            '<get_version_response status="200" status_text="OK">'
71
            '<version>1.0</version>'
72
            '</get_version_response>'
73
        )
74
75
        with self.assertRaises(GvmError):
76
            with self.gmp:
77
                pass
78
79
    def test_missing_version_in_response(self):
80
        self.connection.read.return_value(
81
            '<get_version_response status="200" status_text="OK">'
82
            '<foo>bar</foo>'
83
            '</get_version_response>'
84
        )
85
86
        with self.assertRaises(GvmError):
87
            with self.gmp:
88
                pass
89
90
    def test_invalid_response(self):
91
        self.connection.read.return_value('<get_foo_response/>')
92
93
        with self.assertRaises(GvmError):
94
            with self.gmp:
95
                pass
96
97
98
if __name__ == '__main__':
99
    unittest.main()
100