Passed
Pull Request — master (#61)
by
unknown
02:13
created

gvm_unittest.GVMConnectionTest.testXmlLongCmd()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Description:
3
# Testcases for gvm_connection classes
4
#
5
# Authors:
6
# Raphael Grewe <[email protected]>
7
#
8
# Copyright:
9
# Copyright (C) 2017 Greenbone Networks GmbH
10
#
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation, either version 3 of the License, or
14
# (at your option) any later version.
15
#
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
#
21
# You should have received a copy of the GNU General Public License
22
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
import unittest
25
from gmp.gvm_connection import (SSHConnection,
26
                                TLSConnection,
27
                                UnixSocketConnection,
28
                                GMPError)
29
import warnings
30
import lxml
31
32
33
class GVMConnectionTest(unittest.TestCase):
34
35
    answer_ok = '<get_version_response status="200" status_text="OK"><version>\
36
8.0</version></get_version_response>'
37
    username = 'admin'
38
    password = 'admin'
39
    hostname = 'localhost'
40
    sockpath = '/usr/local/var/run/gvmd.sock'
41
42
    def setUp(self):
43
        warnings.simplefilter("ignore", ResourceWarning)
44
45
    def testSSHConnection(self):
46
        gmp = SSHConnection(hostname=self.hostname, port=22,
47
                            timeout=5, ssh_user='gmp',
48
                            ssh_password='', shell_mode=False)
49
50
        version = gmp.get_version()
51
        gmp.close()
52
        self.assertEqual(version, self.answer_ok)
53
54
    def testSSHConnectionWithWrongHostname(self):
55
56
        try:
57
            SSHConnection(hostname='42.42.42.42', port=22,
58
                          timeout=5, ssh_user='gmp',
59
                          ssh_password='', shell_mode=False)
60
        except Exception as e:
61
            self.assertEqual(str(e), 'timed out')
62
63
    def testSSHConnectionWithWrongPort(self):
64
65
        try:
66
            SSHConnection(hostname='localhost', port=23,
67
                          timeout=5, ssh_user='gmp',
68
                          ssh_password='', shell_mode=False)
69
        except Exception as e:
70
            self.assertEqual(str(e), '[Errno None] Unable to connect to port \
71
23 on 127.0.0.1 or ::1')
72
73
    def testSSHConnectionWithWrongSSHUsername(self):
74
75
        try:
76
            SSHConnection(hostname='localhost', port=22,
77
                          timeout=5, ssh_user='gmppp',
78
                          ssh_password='', shell_mode=False)
79
        except Exception as e:
80
            self.assertEqual(str(e), 'Authentication failed.')
81
82
    def testSSHConnectionWithWrongSSHPassword(self):
83
84
        try:
85
            SSHConnection(hostname='localhost', port=22,
86
                          timeout=5, ssh_user='gmp',
87
                          ssh_password='test', shell_mode=False)
88
        except Exception as e:
89
            self.assertEqual(str(e), 'Authentication failed.')
90
91
    def testUnixSocketConnection(self):
92
        gmp = UnixSocketConnection(sockpath=self.sockpath, shell_mode=False)
93
94
        version = gmp.get_version()
95
        gmp.close()
96
        self.assertEqual(version, self.answer_ok)
97
98
    def testUnixSocketConnectionWrongPath(self):
99
        try:
100
            UnixSocketConnection(sockpath='/foo/bar', shell_mode=False)
101
        except Exception as e:
102
            self.assertEqual(str(e), '[Errno 2] No such file or directory')
103
104
    def testTLSConnection(self):
105
        gmp = TLSConnection(hostname=self.hostname, port=9390,
106
                            shell_mode=False)
107
        version = gmp.get_version()
108
        gmp.close()
109
        self.assertEqual(version, self.answer_ok)
110
111
    def testTLSConnectionWithWrongHostname(self):
112
113
        try:
114
            TLSConnection(hostname='42.42.42.42', port=9390,
115
                          timeout=5, shell_mode=False)
116
        except Exception as e:
117
            self.assertEqual(str(e), 'timed out')
118
119
    def testTLSConnectionWithWrongPort(self):
120
121
        try:
122
            TLSConnection(hostname='localhost', port=9999,
123
                          shell_mode=False)
124
        except Exception as e:
125
            self.assertEqual(str(e), '[Errno 111] Connection refused')
126
127
    def testCheckInvalidCommandStatus(self):
128
        gmp = SSHConnection(hostname=self.hostname, port=22,
129
                            timeout=5, ssh_user='gmp',
130
                            ssh_password='', shell_mode=False)
131
132
        xml_invalid = '<get_version_response status="400" status_text="ERROR">\
133
<version>8.0</version></get_version_response>'
134
135
        try:
136
            gmp.checkCommandStatus(xml_invalid)
137
        except GMPError as e:
138
            self.assertEqual(str(e), 'ERROR')
139
140
        try:
141
            gmp.checkCommandStatus(0)
142
        except GMPError as e:
143
            self.assertEqual(str(e), 'XML Command is empty')
144
145
        try:
146
            gmp.checkCommandStatus(None)
147
        except GMPError as e:
148
            self.assertEqual(str(e), 'XML Command is empty')
149
150
        try:
151
            gmp.checkCommandStatus('')
152
        except Exception as e:
153
            self.assertIsInstance(e, lxml.etree.XMLSyntaxError)
154
        gmp.close()
155
156
    def testXmlLongCmd(self):
157
        long_cmd = int(20000 / 9) * '<element>'
158
        cmd = self.cmdSplitter(long_cmd, 4095)
159
        self.assertEqual(len(cmd), len(cmd) + 5)
160
161
        long_cmd = int(20000 / 9) * '<element>'
162
        cmd = self.cmdSplitter(long_cmd, 2047)
163
        self.assertEqual(len(cmd), len(cmd) + 10)
164
165
166
if __name__ == '__main__':
167
    unittest.main()
168