Passed
Pull Request — master (#186)
by Juan José
01:35
created

tests.test_ssh_daemon.DummyWrapper.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
# Copyright (C) 2015-2018 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
""" Test module for ospd ssh support.
20
"""
21
22
import unittest
23
24
from ospd import ospd_ssh
25
from ospd.ospd_ssh import OSPDaemonSimpleSSH
26
27
28
class FakeFile(object):
29
    def __init__(self, content):
30
        self.content = content
31
32
    def readlines(self):
33
        return self.content.split('\n')
34
35
36
commands = None  # pylint: disable=invalid-name
37
38
39
class FakeSSHClient(object):
40
    def __init__(self):
41
        global commands
42
        commands = []
43
44
    def set_missing_host_key_policy(self, policy):
45
        pass
46
47
    def connect(self, **kwargs):
48
        pass
49
50
    def exec_command(self, cmd):
51
        commands.append(cmd)
52
        return None, FakeFile(''), None
53
54
    def close(self):
55
        pass
56
57
58
class FakeExceptions(object):
59
    AuthenticationException = None  # pylint: disable=invalid-name
60
61
62
class fakeparamiko(object):  # pylint: disable=invalid-name
63
    @staticmethod
64
    def SSHClient(*args):  # pylint: disable=invalid-name
65
        return FakeSSHClient(*args)
66
67
    @staticmethod
68
    def AutoAddPolicy():  # pylint: disable=invalid-name
69
        pass
70
71
    ssh_exception = FakeExceptions
72
73
74
class DummyWrapper(OSPDaemonSimpleSSH):
75
    def __init__(self, niceness=10):
76
        super().__init__(niceness=niceness)
77
78
79
class SSHDaemonTestCase(unittest.TestCase):
80
    def test_no_paramiko(self):
81
        ospd_ssh.paramiko = None
82
83
        with self.assertRaises(ImportError):
84
            OSPDaemonSimpleSSH()
85
86
    def test_run_command(self):
87
        ospd_ssh.paramiko = fakeparamiko
88
89
        daemon = DummyWrapper(niceness=10)
90
        scanid = daemon.create_scan(
91
            None,
92
            [['host.example.com', '80, 443', '', '', '']],
93
            dict(port=5, ssh_timeout=15, username_password='dummy:pw'),
94
            '',
95
        )
96
97
        res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
98
99
        self.assertIsInstance(res, list)
100
        self.assertEqual(commands, ['nice -n 10 cat /etc/passwd'])
101
102
    def test_run_command_legacy_credential(self):
103
        ospd_ssh.paramiko = fakeparamiko
104
105
        daemon = DummyWrapper(niceness=10)
106
        scanid = daemon.create_scan(
107
            None,
108
            [['host.example.com', '80, 443', '', '', '']],
109
            dict(port=5, ssh_timeout=15, username='dummy', password='pw'),
110
            '',
111
        )
112
113
        res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
114
115
        self.assertIsInstance(res, list)
116
        self.assertEqual(commands, ['nice -n 10 cat /etc/passwd'])
117
118
    def test_run_command_new_credential(self):
119
        ospd_ssh.paramiko = fakeparamiko
120
121
        daemon = DummyWrapper(niceness=10)
122
123
        cred_dict = {
124
            'ssh': {
125
                'type': 'up',
126
                'password': 'mypass',
127
                'port': '22',
128
                'username': 'scanuser',
129
            },
130
            'smb': {'type': 'up', 'password': 'mypass', 'username': 'smbuser'},
131
        }
132
133
        scanid = daemon.create_scan(
134
            None,
135
            [['host.example.com', '80, 443', cred_dict, '', '']],
136
            dict(port=5, ssh_timeout=15),
137
            '',
138
        )
139
        res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
140
141
        self.assertIsInstance(res, list)
142
        self.assertEqual(commands, ['nice -n 10 cat /etc/passwd'])
143
144
    def test_run_command_no_credential(self):
145
        ospd_ssh.paramiko = fakeparamiko
146
147
        daemon = DummyWrapper(niceness=10)
148
        scanid = daemon.create_scan(
149
            None,
150
            [['host.example.com', '80, 443', '', '', '']],
151
            dict(port=5, ssh_timeout=15),
152
            '',
153
        )
154
155
        with self.assertRaises(ValueError):
156
            daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
157