Completed
Push — master ( f7cf3e...4f4553 )
by
unknown
16s queued 14s
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
29
30
class GmpContextManagerTestCase(GmpTestCase):
31
32
    gmp_class = Gmp
33
34
    def test_select_gmpv7(self):
35
        self.connection.read.return_value(
36
            '<get_version_response status="200" status_text="OK">'
37
            '<version>7.0</version>'
38
            '</get_version_response>'
39
        )
40
41
        with self.gmp as gmp:
42
            self.assertEqual(gmp.get_protocol_version(), "7")
43
            self.assertIsInstance(gmp, Gmpv7)
44
45
    def test_select_gmpv8(self):
46
        self.connection.read.return_value(
47
            '<get_version_response status="200" status_text="OK">'
48
            '<version>8.0</version>'
49
            '</get_version_response>'
50
        )
51
52
        with self.gmp as gmp:
53
            self.assertEqual(gmp.get_protocol_version(), "8")
54
            self.assertIsInstance(gmp, Gmpv8)
55
56
    def test_unknown_protocol(self):
57
        self.connection.read.return_value(
58
            '<get_version_response status="200" status_text="OK">'
59
            '<version>1.0</version>'
60
            '</get_version_response>'
61
        )
62
63
        with self.assertRaises(GvmError):
64
            with self.gmp:
65
                pass
66
67
    def test_missing_version_in_response(self):
68
        self.connection.read.return_value(
69
            '<get_version_response status="200" status_text="OK">'
70
            '<foo>bar</foo>'
71
            '</get_version_response>'
72
        )
73
74
        with self.assertRaises(GvmError):
75
            with self.gmp:
76
                pass
77
78
    def test_invalid_response(self):
79
        self.connection.read.return_value('<get_foo_response/>')
80
81
        with self.assertRaises(GvmError):
82
            with self.gmp:
83
                pass
84
85
86
if __name__ == '__main__':
87
    unittest.main()
88