1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
# -*- coding: utf-8 -*-
import unittest
from unittest.mock import MagicMock, patch, call
from ncclient.transport.ssh import SSHSession
from ncclient.transport import AuthenticationError, SessionCloseError, NetconfBase
import paramiko
from ncclient.devices.junos import JunosDeviceHandler
try:
import selectors
except ImportError:
import selectors2 as selectors
reply_data = """<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos" attrib1 = "test">
<software-information>
<host-name>R1</host-name>
<product-model>firefly-perimeter</product-model>
<product-name>firefly-perimeter</product-name>
<package-information>
<name>junos</name>
<comment>JUNOS Software Release [12.1X46-D10.2]</comment>
</package-information>
</software-information>
<cli>
<banner></banner>
</cli>
</rpc-reply>"""
reply_ok = """<rpc-reply>
<ok/>
<rpc-reply/>"""
# A buffer of data with two complete messages and an incomplete message
rpc_reply = reply_data + "\n]]>]]>\n" + reply_ok + "\n]]>]]>\n" + reply_ok
reply_ok_chunk = "\n#%d\n%s\n##\n" % (len(reply_ok), reply_ok)
# einarnn: this test message had to be reduced in size as the improved
# 1.1 parsing finds a whole fragment in it, so needed to have less
# data in it than the terminating '>'
reply_ok_partial_chunk = "\n#%d\n%s" % (len(reply_ok), reply_ok[:-1])
# A buffer of data with two complete messages and an incomplete message
rpc_reply11 = "\n#%d\n%s\n#%d\n%s\n##\n%s%s" % (
30, reply_data[:30], len(reply_data[30:]), reply_data[30:],
reply_ok_chunk, reply_ok_partial_chunk)
rpc_reply_part_1 = """<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos" attrib1 = "test">
<software-information>
<host-name>R1</host-name>
<product-model>firefly-perimeter</product-model>
<product-name>firefly-perimeter</product-name>
<package-information>
<name>junos</name>
<comment>JUNOS Software Release [12.1X46-D10.2]</comment>
</package-information>
</software-information>
<cli>
<banner></banner>
</cli>
</rpc-reply>
]]>]]"""
rpc_reply_part_2 = """>
<rpc-reply>
<ok/>
<rpc-reply/>"""
class TestSSH(unittest.TestCase):
def _test_parsemethod(self, mock_dispatch, parsemethod, reply, ok_chunk,
expected_messages):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._buffer.write(bytes(reply, "utf-8"))
remainder = bytes(ok_chunk, "utf-8")
parsemethod(obj)
for i in range(0, len(expected_messages)):
call = mock_dispatch.call_args_list[i][0][0]
self.assertEqual(call, expected_messages[i])
self.assertEqual(obj._buffer.getvalue(), remainder)
@patch('ncclient.transport.ssh.Session._dispatch_message')
def test_parse(self, mock_dispatch):
self._test_parsemethod(mock_dispatch, SSHSession._parse, rpc_reply,
"\n" + reply_ok, [reply_data])
@patch('ncclient.transport.ssh.Session._dispatch_message')
def test_parse11(self, mock_dispatch):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._buffer.write(bytes(rpc_reply11, "utf-8"))
remainder = bytes(reply_ok_partial_chunk, "utf-8")
obj.parser._parse11()
expected_messages = [reply_data, reply_ok]
for i in range(0, len(expected_messages)):
call = mock_dispatch.call_args_list[i][0][0]
self.assertEqual(call, expected_messages[i])
self.assertEqual(obj._buffer.getvalue(), remainder)
@patch('ncclient.transport.ssh.Session._dispatch_message')
def test_parse_incomplete_delimiter(self, mock_dispatch):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
b = bytes(rpc_reply_part_1, "utf-8")
obj._buffer.write(b)
obj._parse()
self.assertFalse(mock_dispatch.called)
b = bytes(rpc_reply_part_2, "utf-8")
obj._buffer.write(b)
obj._parse()
self.assertTrue(mock_dispatch.called)
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.agent.AgentSSH.get_keys')
def test_auth_agent(self, mock_get_key, mock_auth_public_key):
key = paramiko.PKey(msg="hello")
mock_get_key.return_value = [key]
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
obj._auth('user', 'password', [], True, True)
self.assertEqual(
(mock_auth_public_key.call_args_list[0][0][1]).__repr__(),
key.__repr__())
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.agent.AgentSSH.get_keys')
def test_auth_agent_exception(self, mock_get_key, mock_auth_public_key):
key = paramiko.PKey()
mock_get_key.return_value = [key]
mock_auth_public_key.side_effect = paramiko.ssh_exception.AuthenticationException
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
self.assertRaises(AuthenticationError,
obj._auth,'user', None, [], True, False)
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.PKey.from_path')
def test_auth_keyfiles(self, mock_get_key, mock_auth_public_key):
key = paramiko.PKey()
mock_get_key.return_value = key
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
obj._auth('user', 'password', ["key_file_name"], False, True)
self.assertEqual(
(mock_auth_public_key.call_args_list[0][0][1]).__repr__(),
key.__repr__())
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.PKey.from_path')
def test_auth_keyfiles_exception(self, mock_get_key, mock_auth_public_key):
key = paramiko.PKey()
mock_get_key.side_effect = paramiko.ssh_exception.PasswordRequiredException
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
self.assertRaises(AuthenticationError,
obj._auth,'user', None, ["key_file_name"], False, True)
@patch('os.path.isfile')
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.PKey.from_path')
def test_auth_default_keyfiles(self, mock_get_key, mock_auth_public_key,
mock_is_file):
key = paramiko.PKey()
mock_get_key.return_value = key
mock_is_file.return_value = True
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
obj._auth('user', 'password', [], False, True)
self.assertEqual(
(mock_auth_public_key.call_args_list[0][0][1]).__repr__(),
key.__repr__())
@patch('os.path.isfile')
@patch('paramiko.transport.Transport.auth_publickey')
@patch('paramiko.PKey.from_path')
def test_auth_default_keyfiles_exception(self, mock_get_key,
mock_auth_public_key, mock_is_file):
key = paramiko.PKey()
mock_is_file.return_value = True
mock_get_key.side_effect = paramiko.ssh_exception.PasswordRequiredException
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
self.assertRaises(AuthenticationError,
obj._auth,'user', None, [], False, True)
@patch('paramiko.transport.Transport.auth_password')
def test_auth_password(self, mock_auth_password):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
obj._auth('user', 'password', [], False, True)
self.assertEqual(
mock_auth_password.call_args_list[0][0],
('user',
'password'))
@patch('paramiko.transport.Transport.auth_password')
def test_auth_exception(self, mock_auth_password):
mock_auth_password.side_effect = Exception
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
self.assertRaises(AuthenticationError,
obj._auth, 'user', 'password', [], False, True)
def test_auth_no_methods_exception(self):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
self.assertRaises(AuthenticationError,
obj._auth,'user', None, [], False, False)
@patch('paramiko.transport.Transport.close')
def test_close(self, mock_close):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._transport = paramiko.Transport(MagicMock())
obj._transport.active = True
obj._connected = True
obj.close()
mock_close.assert_called_once_with()
self.assertFalse(obj._connected)
@patch('paramiko.hostkeys.HostKeys.load')
def test_load_host_key(self, mock_load):
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj.load_known_hosts("file_name")
mock_load.assert_called_once_with("file_name")
@patch('os.path.expanduser')
@patch('paramiko.hostkeys.HostKeys.load')
def test_load_host_key_2(self, mock_load, mock_os):
mock_os.return_value = "file_name"
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj.load_known_hosts()
mock_load.assert_called_once_with("file_name")
@patch('os.path.expanduser')
@patch('paramiko.hostkeys.HostKeys.load')
def test_load_host_key_IOError(self, mock_load, mock_os):
mock_os.return_value = "file_name"
mock_load.side_effect = IOError
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj.load_known_hosts()
mock_load.assert_called_with("file_name")
@patch('ncclient.transport.ssh.hexlify')
@patch('os.path.expanduser')
@patch('paramiko.HostKeys')
@patch('paramiko.Transport')
@patch('ncclient.transport.ssh.SSHSession._post_connect')
@patch('ncclient.transport.ssh.SSHSession._auth')
def test_ssh_known_hosts(self, mock_auth, mock_pc,
mock_transport, mock_hk,
mock_os, mock_hex):
mock_os.return_value = "file_name"
hk_inst = MagicMock(check=MagicMock(return_value=True))
mock_hk.return_value = hk_inst
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj.connect(host='h', sock=MagicMock())
hk_inst.load.assert_called_once_with('file_name')
mock_os.assert_called_once_with('~/.ssh/known_hosts')
@patch('ncclient.transport.ssh.hexlify')
@patch('ncclient.transport.ssh.open')
@patch('os.path.expanduser')
@patch('paramiko.HostKeys')
@patch('paramiko.Transport')
@patch('paramiko.SSHConfig')
@patch('ncclient.transport.ssh.SSHSession._post_connect')
@patch('ncclient.transport.ssh.SSHSession._auth')
def test_ssh_known_hosts_2(self, mock_auth, mock_pc,
mock_sshc, mock_transport, mock_hk,
mock_os, mock_open, mock_hex):
mock_os.return_value = "file_name"
hk_inst = MagicMock(check=MagicMock(return_value=True))
mock_hk.return_value = hk_inst
config = {'userknownhostsfile': 'known_hosts_file'}
mock_sshc.return_value = MagicMock(lookup=lambda _h: config)
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj.connect(host='h', sock=MagicMock(), ssh_config=True)
hk_inst.load.assert_called_once_with('file_name')
mock_os.mock_calls == [call('~/.ssh/config'), call('known_hosts_file')]
@patch('ncclient.transport.ssh.SSHSession.close')
@patch('paramiko.channel.Channel.recv')
@patch('selectors.DefaultSelector.select')
@patch('ncclient.transport.ssh.Session._dispatch_error')
def test_run_receive_py3(self, mock_error, mock_selector, mock_recv, mock_close):
mock_selector.return_value = True
mock_recv.return_value = 0
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._channel = paramiko.Channel("c100")
obj.run()
self.assertTrue(
isinstance(
mock_error.call_args_list[0][0][0],
SessionCloseError))
def test_run_send_py3_10(self):
self._test_run_send_py3(NetconfBase.BASE_10,
lambda msg: msg.encode() + b"]]>]]>")
def test_run_send_py3_11(self):
def chunker(msg):
encmsg = msg.encode()
chunks = b"\n#%i\n%b\n##\n" % (len(encmsg), encmsg)
return chunks
self._test_run_send_py3(NetconfBase.BASE_11, chunker)
@patch('ncclient.transport.ssh.SSHSession.close')
@patch('paramiko.channel.Channel.send_ready')
@patch('paramiko.channel.Channel.send')
@patch('selectors.DefaultSelector.select')
@patch('ncclient.transport.ssh.Session._dispatch_error')
def _test_run_send_py3(self, base, chunker, mock_error, mock_selector,
mock_send, mock_ready, mock_close):
mock_selector.return_value = False
mock_ready.return_value = True
mock_send.return_value = -1
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._channel = paramiko.Channel("c100")
msg = "naïve garçon"
obj._q.put(msg)
obj._base = base
obj.run()
self.assertEqual(mock_send.call_args_list[0][0][0], chunker(msg))
self.assertTrue(
isinstance(
mock_error.call_args_list[0][0][0],
SessionCloseError))
@unittest.skip("test currently non-functional")
@patch('ncclient.transport.ssh.SSHSession.close')
@patch('paramiko.channel.Channel.send_ready')
@patch('paramiko.channel.Channel.send')
@patch('selectors2.DefaultSelector')
@patch('ncclient.transport.ssh.Session._dispatch_error')
def test_run_send_py2(self, mock_error, mock_selector, mock_send, mock_ready, mock_close):
mock_selector.select.return_value = False
mock_ready.return_value = True
mock_send.return_value = -1
device_handler = JunosDeviceHandler({'name': 'junos'})
obj = SSHSession(device_handler)
obj._channel = paramiko.Channel("c100")
obj._q.put("rpc")
obj.run()
self.assertEqual(mock_send.call_args_list[0][0][0], "rpc]]>]]>")
self.assertTrue(
isinstance(
mock_error.call_args_list[0][0][0],
SessionCloseError))
|