| 1 |
import pickle |
| 2 |
import unittest |
| 3 |
|
| 4 |
from paramiko import RSAKey |
| 5 |
from paramiko.ssh_exception import ( |
| 6 |
NoValidConnectionsError, |
| 7 |
BadAuthenticationType, |
| 8 |
PartialAuthentication, |
| 9 |
ChannelException, |
| 10 |
BadHostKeyException, |
| 11 |
ProxyCommandFailure, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class NoValidConnectionsErrorTest(unittest.TestCase): |
| 16 |
def test_pickling(self): |
| 17 |
# Regression test for https://github.com/paramiko/paramiko/issues/617 |
| 18 |
exc = NoValidConnectionsError({("127.0.0.1", "22"): Exception()}) |
| 19 |
new_exc = pickle.loads(pickle.dumps(exc)) |
| 20 |
self.assertEqual(type(exc), type(new_exc)) |
| 21 |
self.assertEqual(str(exc), str(new_exc)) |
| 22 |
self.assertEqual(exc.args, new_exc.args) |
| 23 |
|
| 24 |
def test_error_message_for_single_host(self): |
| 25 |
exc = NoValidConnectionsError({("127.0.0.1", "22"): Exception()}) |
| 26 |
assert "Unable to connect to port 22 on 127.0.0.1" in str(exc) |
| 27 |
|
| 28 |
def test_error_message_for_two_hosts(self): |
| 29 |
exc = NoValidConnectionsError( |
| 30 |
{("127.0.0.1", "22"): Exception(), ("::1", "22"): Exception()} |
| 31 |
) |
| 32 |
assert "Unable to connect to port 22 on 127.0.0.1 or ::1" in str(exc) |
| 33 |
|
| 34 |
def test_error_message_for_multiple_hosts(self): |
| 35 |
exc = NoValidConnectionsError( |
| 36 |
{ |
| 37 |
("127.0.0.1", "22"): Exception(), |
| 38 |
("::1", "22"): Exception(), |
| 39 |
("10.0.0.42", "22"): Exception(), |
| 40 |
} |
| 41 |
) |
| 42 |
exp = "Unable to connect to port 22 on 10.0.0.42, 127.0.0.1 or ::1" |
| 43 |
assert exp in str(exc) |
| 44 |
|
| 45 |
|
| 46 |
class ExceptionStringDisplayTest(unittest.TestCase): |
| 47 |
def test_BadAuthenticationType(self): |
| 48 |
exc = BadAuthenticationType( |
| 49 |
"Bad authentication type", ["ok", "also-ok"] |
| 50 |
) |
| 51 |
expected = "Bad authentication type; allowed types: ['ok', 'also-ok']" |
| 52 |
assert str(exc) == expected |
| 53 |
|
| 54 |
def test_PartialAuthentication(self): |
| 55 |
exc = PartialAuthentication(["ok", "also-ok"]) |
| 56 |
expected = "Partial authentication; allowed types: ['ok', 'also-ok']" |
| 57 |
assert str(exc) == expected |
| 58 |
|
| 59 |
def test_BadHostKeyException(self): |
| 60 |
got_key = RSAKey.generate(2048) |
| 61 |
wanted_key = RSAKey.generate(2048) |
| 62 |
exc = BadHostKeyException("myhost", got_key, wanted_key) |
| 63 |
expected = "Host key for server 'myhost' does not match: got '{}', expected '{}'" # noqa |
| 64 |
assert str(exc) == expected.format( |
| 65 |
got_key.get_base64(), wanted_key.get_base64() |
| 66 |
) |
| 67 |
|
| 68 |
def test_ProxyCommandFailure(self): |
| 69 |
exc = ProxyCommandFailure("man squid", 7) |
| 70 |
expected = 'ProxyCommand("man squid") returned nonzero exit status: 7' |
| 71 |
assert str(exc) == expected |
| 72 |
|
| 73 |
def test_ChannelException(self): |
| 74 |
exc = ChannelException(17, "whatever") |
| 75 |
assert str(exc) == "ChannelException(17, 'whatever')" |