Skip to content

Commit 08a5ef6

Browse files
authored
Added new libssh2 error codes as python exceptions. Updated error han… (#231)
* Added new libssh2 error codes as python exceptions. * Updated error handling for new error codes and exceptions * Updated tests * Updated changelog, docstrings
1 parent 55fafa8 commit 08a5ef6

File tree

9 files changed

+865
-327
lines changed

9 files changed

+865
-327
lines changed

Changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Changes
1616
* Added `ssh2.channel.Channel.signal` function for sending signals over SSH to an open channel - #221
1717
* Added `ssh2.session.Session.direct_streamlocal_ex` for creating `Channel` objects tunneling a local UNIX socket
1818
via the remote host to a third party.
19+
* Added new `libssh2` error codes under `ssh2.error_codes`, equivalent Python exceptions under `ssh2.exceptions`
20+
and updated error code handling for all functions.
1921

2022

2123
Packaging

ssh2/error_codes.c

Lines changed: 147 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh2/error_codes.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,11 @@ cdef extern from "libssh2.h" nogil:
7777
_LIBSSH2_ERROR_KNOWN_HOSTS "LIBSSH2_ERROR_KNOWN_HOSTS"
7878
_LIBSSH2_ERROR_CHANNEL_WINDOW_FULL "LIBSSH2_ERROR_CHANNEL_WINDOW_FULL"
7979
_LIBSSH2_ERROR_KEYFILE_AUTH_FAILED "LIBSSH2_ERROR_KEYFILE_AUTH_FAILED"
80+
_LIBSSH2_ERROR_RANDGEN "LIBSSH2_ERROR_RANDGEN"
81+
_LIBSSH2_ERROR_MISSING_USERAUTH_BANNER "LIBSSH2_ERROR_MISSING_USERAUTH_BANNER"
82+
_LIBSSH2_ERROR_ALGO_UNSUPPORTED "LIBSSH2_ERROR_ALGO_UNSUPPORTED"
83+
_LIBSSH2_ERROR_MAC_FAILURE "LIBSSH2_ERROR_MAC_FAILURE"
84+
_LIBSSH2_ERROR_HASH_INIT "LIBSSH2_ERROR_HASH_INIT"
85+
_LIBSSH2_ERROR_HASH_CALC "LIBSSH2_ERROR_HASH_CALC"
8086

8187
_LIBSSH2CHANNEL_EAGAIN "LIBSSH2_ERROR_EAGAIN"

ssh2/error_codes.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ LIBSSH2_ERROR_CHANNEL_WINDOW_FULL = \
8181
error_codes._LIBSSH2_ERROR_CHANNEL_WINDOW_FULL
8282
LIBSSH2_ERROR_KEYFILE_AUTH_FAILED = \
8383
error_codes._LIBSSH2_ERROR_KEYFILE_AUTH_FAILED
84+
LIBSSH2_ERROR_RANDGEN = error_codes._LIBSSH2_ERROR_RANDGEN
85+
LIBSSH2_ERROR_MISSING_USERAUTH_BANNER = error_codes._LIBSSH2_ERROR_MISSING_USERAUTH_BANNER
86+
LIBSSH2_ERROR_ALGO_UNSUPPORTED = error_codes._LIBSSH2_ERROR_ALGO_UNSUPPORTED
87+
LIBSSH2_ERROR_MAC_FAILURE = error_codes._LIBSSH2_ERROR_MAC_FAILURE
88+
LIBSSH2_ERROR_HASH_INIT = error_codes._LIBSSH2_ERROR_HASH_INIT
89+
LIBSSH2_ERROR_HASH_CALC = error_codes._LIBSSH2_ERROR_HASH_CALC

ssh2/exceptions.c

Lines changed: 375 additions & 159 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh2/exceptions.pyx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,29 @@ class KeyfileAuthFailedError(SSH2Error):
303303
"""Raised on key file authentication error"""
304304

305305

306+
class RandGenError(SSH2Error):
307+
"""Raised on randon number generator error"""
308+
309+
310+
class MissingUserAuthBannerError(SSH2Error):
311+
"""Raised on missing user authentication banner error"""
312+
313+
314+
class AlgoUnsupportedError(SSH2Error):
315+
"""Raised on unsupported algorithm error"""
316+
317+
318+
class MacFailureError(SSH2Error):
319+
"""Raised on MAC failure error"""
320+
321+
322+
class HashInitError(SSH2Error):
323+
"""Raised on hash initialisation error"""
324+
325+
326+
class HashCalcError(SSH2Error):
327+
"""Raised on hash calculation error"""
328+
329+
306330
class UnknownError(SSH2Error):
307331
"""Raised on non-specific or unknown errors"""

ssh2/utils.c

Lines changed: 282 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh2/utils.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ cpdef int handle_error_codes(int errcode) except -1:
222222
raise exceptions.ChannelWindowFullError
223223
elif errcode == error_codes._LIBSSH2_ERROR_KEYFILE_AUTH_FAILED:
224224
raise exceptions.KeyfileAuthFailedError
225+
elif errcode == error_codes._LIBSSH2_ERROR_RANDGEN:
226+
raise exceptions.RandGenError
227+
elif errcode == error_codes._LIBSSH2_ERROR_MISSING_USERAUTH_BANNER:
228+
raise exceptions.MissingUserAuthBannerError
229+
elif errcode == error_codes._LIBSSH2_ERROR_ALGO_UNSUPPORTED:
230+
raise exceptions.AlgoUnsupportedError
231+
elif errcode == error_codes._LIBSSH2_ERROR_MAC_FAILURE:
232+
raise exceptions.MacFailureError
233+
elif errcode == error_codes._LIBSSH2_ERROR_HASH_INIT:
234+
raise exceptions.HashInitError
235+
elif errcode == error_codes._LIBSSH2_ERROR_HASH_CALC:
236+
raise exceptions.HashCalcError
225237
else:
226238
# Switch default
227239
if errcode < 0:

tests/test_exceptions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
LIBSSH2_ERROR_COMPRESS, LIBSSH2_ERROR_OUT_OF_BOUNDARY, \
2222
LIBSSH2_ERROR_AGENT_PROTOCOL, LIBSSH2_ERROR_SOCKET_RECV, \
2323
LIBSSH2_ERROR_SOCKET_SEND, LIBSSH2_ERROR_ENCRYPT, \
24-
LIBSSH2_ERROR_BAD_SOCKET, LIBSSH2_ERROR_KNOWN_HOSTS
24+
LIBSSH2_ERROR_BAD_SOCKET, LIBSSH2_ERROR_KNOWN_HOSTS, \
25+
LIBSSH2_ERROR_RANDGEN, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER, LIBSSH2_ERROR_ALGO_UNSUPPORTED, \
26+
LIBSSH2_ERROR_MAC_FAILURE, LIBSSH2_ERROR_HASH_INIT, LIBSSH2_ERROR_HASH_CALC
2527
from ssh2.exceptions import SSH2Error, AgentError, AuthenticationError, \
2628
AgentConnectionError, AgentAuthenticationError, AgentListIdentitiesError, \
2729
AgentGetIdentityError, AgentProtocolError, SessionError, \
@@ -38,7 +40,8 @@
3840
InvalidPollTypeError, PublicKeyProtocolError, BufferTooSmallError, \
3941
BadUseError, CompressError, OutOfBoundaryError, SocketRecvError, \
4042
SocketSendError, EncryptError, BadSocketError, SFTPError, SFTPProtocolError, \
41-
KnownHostError, UnknownError
43+
KnownHostError, UnknownError, RandGenError, MissingUserAuthBannerError, \
44+
AlgoUnsupportedError, MacFailureError, HashInitError, HashCalcError
4245
from ssh2.utils import handle_error_codes
4346

4447

@@ -65,6 +68,12 @@ def test_general_errors(self):
6568
self.assertRaises(MethodNoneError, handle_error_codes, LIBSSH2_ERROR_METHOD_NONE)
6669
self.assertRaises(AuthenticationError, handle_error_codes, LIBSSH2_ERROR_AUTHENTICATION_FAILED)
6770
self.assertRaises(PublickeyUnverifiedError, handle_error_codes, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED)
71+
self.assertRaises(RandGenError, handle_error_codes, LIBSSH2_ERROR_RANDGEN)
72+
self.assertRaises(MissingUserAuthBannerError, handle_error_codes, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER)
73+
self.assertRaises(AlgoUnsupportedError, handle_error_codes, LIBSSH2_ERROR_ALGO_UNSUPPORTED)
74+
self.assertRaises(MacFailureError, handle_error_codes, LIBSSH2_ERROR_MAC_FAILURE)
75+
self.assertRaises(HashInitError, handle_error_codes, LIBSSH2_ERROR_HASH_INIT)
76+
self.assertRaises(HashCalcError, handle_error_codes, LIBSSH2_ERROR_HASH_CALC)
6877

6978
def test_channel_errors(self):
7079
self.assertRaises(ChannelOutOfOrderError, handle_error_codes, LIBSSH2_ERROR_CHANNEL_OUTOFORDER)

0 commit comments

Comments
 (0)