From 7aaf42a7b129ba5b9548db0b2a71a095246aeac9 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 15:59:58 -0700 Subject: [PATCH 01/56] Add Py3 compatibility support helpers --- paramiko/py3compat.py | 130 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 paramiko/py3compat.py diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py new file mode 100644 index 0000000..c4a85ac --- /dev/null +++ b/paramiko/py3compat.py @@ -0,0 +1,130 @@ +import sys + +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] + +PY3 = sys.version_info[0] >= 3 + +if PY3: + import collections + import struct + string_types = str + integer_types = int + text_type = str + bytes_type = bytes + long = int + input = input + + def bytestring(s): + return s + + def byte_ord(c): + assert isinstance(c, int) + return c + + def byte_chr(c): + assert isinstance(c, int) + return struct.pack('B', c) + + def byte_mask(c, mask): + assert isinstance(c, int) + return struct.pack('B', c & mask) + + def b(s, encoding='utf8'): + """cast unicode or bytes to bytes""" + if isinstance(s, bytes): + return s + elif isinstance(s, str): + return s.encode(encoding) + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + def u(s, encoding='utf8'): + """cast bytes or unicode to unicode""" + if isinstance(s, bytes): + return s.decode(encoding) + elif isinstance(s, str): + return s + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + import io + StringIO = io.StringIO # NOQA + BytesIO = io.BytesIO # NOQA + + def is_callable(c): + return isinstance(c, collections.Callable) + + def get_next(c): + return c.__next__ + + next = next + + MAXSIZE = sys.maxsize # NOQA +else: + string_types = basestring + integer_types = (int, long) + text_type = unicode + bytes_type = str + long = long + input = raw_input + + def bytestring(s): # NOQA + if isinstance(s, unicode): + return s.encode('utf-8') + return s + + byte_ord = ord # NOQA + byte_chr = chr # NOQA + + def byte_mask(c, mask): + return chr(ord(c) & mask) + + def b(s, encoding='utf8'): # NOQA + """cast unicode or bytes to bytes""" + if isinstance(s, str): + return s + elif isinstance(s, unicode): + return s.encode(encoding) + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + def u(s, encoding='utf8'): # NOQA + """cast bytes or unicode to unicode""" + if isinstance(s, str): + return s.decode(encoding) + elif isinstance(s, unicode): + return s + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + try: + import cStringIO + StringIO = cStringIO.StringIO # NOQA + except ImportError: + import StringIO + StringIO = StringIO.StringIO # NOQA + + BytesIO = StringIO + + def is_callable(c): # NOQA + return callable(c) + + def get_next(c): # NOQA + return c.next + + def next(c): + return c.next() + + # It's possible to have sizeof(long) != sizeof(Py_ssize_t). + class X(object): + def __len__(self): + return 1 << 31 + try: + len(X()) + except OverflowError: + # 32-bit + MAXSIZE = int((1 << 31) - 1) # NOQA + else: + # 64-bit + MAXSIZE = int((1 << 63) - 1) # NOQA + del X From e5822c9fa1b9243639450369b80df2746df714d0 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:02:01 -0700 Subject: [PATCH 02/56] Add Py3.2 and Py3.3 to travis --- .travis.yml | 2 ++ tests/__init__.py | 0 2 files changed, 2 insertions(+) create mode 100644 tests/__init__.py diff --git a/.travis.yml b/.travis.yml index 43a4279..7ebb931 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ python: - "2.5" - "2.6" - "2.7" + - "3.2" + - "3.3" install: # Self-install for setup.py-driven deps - pip install -e . diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 From f73d5f73e512ffe2a7a4eccce262dc4acd5c4307 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:05:47 -0700 Subject: [PATCH 03/56] Fix print statements --- demos/demo.py | 35 ++++++++++++++++++----------------- demos/interactive.py | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index aa4bdaa..cbd7730 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -45,13 +45,13 @@ def agent_auth(transport, username): return for key in agent_keys: - print 'Trying ssh-agent key %s' % hexlify(key.get_fingerprint()), + print('Trying ssh-agent key %s' % hexlify(key.get_fingerprint())) try: transport.auth_publickey(username, key) - print '... success!' + print('... success!') return except paramiko.SSHException: - print '... nope.' + print('... nope.') def manual_auth(username, hostname): @@ -98,7 +98,7 @@ if len(sys.argv) > 1: else: hostname = raw_input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -111,6 +111,7 @@ try: sock.connect((hostname, port)) except Exception, e: print '*** Connect failed: ' + str(e) + print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -119,7 +120,7 @@ try: try: t.start_client() except paramiko.SSHException: - print '*** SSH negotiation failed.' + print('*** SSH negotiation failed.') sys.exit(1) try: @@ -128,20 +129,20 @@ try: try: keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: - print '*** Unable to open host keys file' + print('*** Unable to open host keys file') keys = {} # check server's host key -- this is important. key = t.get_remote_server_key() - if not keys.has_key(hostname): - print '*** WARNING: Unknown host key!' - elif not keys[hostname].has_key(key.get_name()): - print '*** WARNING: Unknown host key!' + if hostname not in keys: + print('*** WARNING: Unknown host key!') + elif key.get_name() not in keys[hostname]: + print('*** WARNING: Unknown host key!') elif keys[hostname][key.get_name()] != key: - print '*** WARNING: Host key has changed!!!' + print('*** WARNING: Host key has changed!!!') sys.exit(1) else: - print '*** Host key OK.' + print('*** Host key OK.') # get username if username == '': @@ -154,21 +155,21 @@ try: if not t.is_authenticated(): manual_auth(username, hostname) if not t.is_authenticated(): - print '*** Authentication failed. :(' + print('*** Authentication failed. :(') t.close() sys.exit(1) chan = t.open_session() chan.get_pty() chan.invoke_shell() - print '*** Here we go!' - print + print('*** Here we go!\n') interactive.interactive_shell(chan) chan.close() t.close() -except Exception, e: - print '*** Caught exception: ' + str(e.__class__) + ': ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: t.close() diff --git a/demos/interactive.py b/demos/interactive.py index f3be74d..b7962b3 100644 --- a/demos/interactive.py +++ b/demos/interactive.py @@ -51,7 +51,7 @@ def posix_shell(chan): try: x = chan.recv(1024) if len(x) == 0: - print '\r\n*** EOF\r\n', + sys.stdout.write('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() From 8edd2cc75e50a664ffb8370e0f7c26b78f9d9755 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:06:09 -0700 Subject: [PATCH 04/56] Add Py3.2 and Py3.3 to tox --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index e2a8dcf..c63c93d 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py25,py26,py27 +envlist = py25,py26,py27,py32,py33 [testenv] commands = pip install --use-mirrors -q -r dev-requirements.txt From cd918d0dc662777f8f4ebc717496e5f4e65f3502 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:07:03 -0700 Subject: [PATCH 05/56] Add Py3 helpers to common.py --- paramiko/common.py | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 3d7ca58..013510e 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -19,12 +19,13 @@ """ Common constants and global variables. """ +from paramiko.py3compat import * MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, MSG_SERVICE_REQUEST, \ MSG_SERVICE_ACCEPT = range(1, 7) MSG_KEXINIT, MSG_NEWKEYS = range(20, 22) MSG_USERAUTH_REQUEST, MSG_USERAUTH_FAILURE, MSG_USERAUTH_SUCCESS, \ - MSG_USERAUTH_BANNER = range(50, 54) + MSG_USERAUTH_BANNER = range(50, 54) MSG_USERAUTH_PK_OK = 60 MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE = range(60, 62) MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE = range(80, 83) @@ -33,6 +34,10 @@ MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \ MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MSG_CHANNEL_REQUEST, \ MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE = range(90, 101) +for key in list(globals().keys()): + if key.startswith('MSG_'): + globals()['c' + key] = byte_chr(globals()[key]) +del key # for debugging: MSG_NAMES = { @@ -69,7 +74,7 @@ MSG_NAMES = { MSG_CHANNEL_REQUEST: 'channel-request', MSG_CHANNEL_SUCCESS: 'channel-success', MSG_CHANNEL_FAILURE: 'channel-failure' - } +} # authentication request return codes: @@ -118,6 +123,44 @@ else: import logging PY22 = False +if PY3: + zero_byte = b'\x00' + one_byte = b'\x01' + four_byte = b'\x04' + max_byte = b'\xff' + newline_byte = b'\n' + cr_byte = b'\r' +else: + zero_byte = '\x00' + one_byte = '\x01' + four_byte = '\x04' + max_byte = '\xff' + newline_byte = '\n' + cr_byte = '\r' + + +def asbytes(s): + if not isinstance(s, bytes_type): + if isinstance(s, string_types): + s = b(s) + else: + try: + s = s.asbytes() + except Exception: + raise Exception('Unknown type') + return s + +xffffffff = long(0xffffffff) +x80000000 = long(0x80000000) +long_zero = long(0) +long_one = long(1) +o666 = 438 +o660 = 432 +o644 = 420 +o600 = 384 +o777 = 511 +o700 = 448 +o70 = 56 DEBUG = logging.DEBUG INFO = logging.INFO From 3301473ae7122a4728131eb5ced90dc565e53783 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:07:30 -0700 Subject: [PATCH 06/56] Uncap the requirements --- dev-requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index f706c46..cdd120a 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,2 +1,2 @@ -tox>=1.4,<1.5 -epydoc>=3.0,<3.1 +tox>=1.4 +epydoc>=3.0 From 3afc76a6b4c4b06193cdb4837ca1df1f3428fb20 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:10:17 -0700 Subject: [PATCH 07/56] Write to locals instead of globals --- paramiko/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 013510e..2392f03 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -34,9 +34,9 @@ MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \ MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MSG_CHANNEL_REQUEST, \ MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE = range(90, 101) -for key in list(globals().keys()): +for key in list(locals().keys()): if key.startswith('MSG_'): - globals()['c' + key] = byte_chr(globals()[key]) + locals()['c' + key] = byte_chr(locals()[key]) del key # for debugging: From 66cfa97cce92b1d60383d178887b18dddb999fc1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:19:30 -0700 Subject: [PATCH 08/56] Fix imports --- demos/demo.py | 5 +++- paramiko/__init__.py | 48 ++++++++++++++++++------------------- paramiko/_winapi.py | 6 ++++- paramiko/agent.py | 2 +- paramiko/ber.py | 3 ++- paramiko/file.py | 2 +- paramiko/hostkeys.py | 34 +++++++++++++++++++++++--- paramiko/message.py | 2 +- paramiko/pipe.py | 1 + paramiko/primes.py | 1 + paramiko/proxy.py | 1 + paramiko/transport.py | 2 +- test.py | 26 ++++++++++---------- tests/loop.py | 1 + tests/stub_sftp.py | 2 ++ tests/test_auth.py | 3 ++- tests/test_buffered_pipe.py | 2 +- tests/test_client.py | 3 ++- tests/test_kex.py | 1 + tests/test_message.py | 1 + tests/test_packetizer.py | 3 ++- tests/test_pkey.py | 3 +-- tests/test_sftp.py | 7 +++--- tests/test_sftp_big.py | 7 +++--- tests/test_transport.py | 4 ++-- tests/test_util.py | 4 ++-- tests/util.py | 1 + 27 files changed, 112 insertions(+), 63 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index cbd7730..3890eda 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -30,7 +30,10 @@ import time import traceback import paramiko -import interactive +try: + import interactive +except ImportError: + from . import interactive def agent_auth(transport, username): diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 32ccfcd..a12ee04 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -62,32 +62,32 @@ __version_info__ = tuple([ int(d) for d in __version__.split(".") ]) __license__ = "GNU Lesser General Public License (LGPL)" -from transport import SecurityOptions, Transport -from client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy -from auth_handler import AuthHandler -from channel import Channel, ChannelFile -from ssh_exception import SSHException, PasswordRequiredException, \ +from paramiko.transport import SecurityOptions, Transport +from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy +from paramiko.auth_handler import AuthHandler +from paramiko.channel import Channel, ChannelFile +from paramiko.ssh_exception import SSHException, PasswordRequiredException, \ BadAuthenticationType, ChannelException, BadHostKeyException, \ AuthenticationException, ProxyCommandFailure -from server import ServerInterface, SubsystemHandler, InteractiveQuery -from rsakey import RSAKey -from dsskey import DSSKey -from ecdsakey import ECDSAKey -from sftp import SFTPError, BaseSFTP -from sftp_client import SFTP, SFTPClient -from sftp_server import SFTPServer -from sftp_attr import SFTPAttributes -from sftp_handle import SFTPHandle -from sftp_si import SFTPServerInterface -from sftp_file import SFTPFile -from message import Message -from packet import Packetizer -from file import BufferedFile -from agent import Agent, AgentKey -from pkey import PKey -from hostkeys import HostKeys -from config import SSHConfig -from proxy import ProxyCommand +from paramiko.server import ServerInterface, SubsystemHandler, InteractiveQuery +from paramiko.rsakey import RSAKey +from paramiko.dsskey import DSSKey +from paramiko.ecdsakey import ECDSAKey +from paramiko.sftp import SFTPError, BaseSFTP +from paramiko.sftp_client import SFTP, SFTPClient +from paramiko.sftp_server import SFTPServer +from paramiko.sftp_attr import SFTPAttributes +from paramiko.sftp_handle import SFTPHandle +from paramiko.sftp_si import SFTPServerInterface +from paramiko.sftp_file import SFTPFile +from paramiko.message import Message +from paramiko.packet import Packetizer +from paramiko.file import BufferedFile +from paramiko.agent import Agent, AgentKey +from paramiko.pkey import PKey +from paramiko.hostkeys import HostKeys +from paramiko.config import SSHConfig +from paramiko.proxy import ProxyCommand # fix module names for epydoc for c in locals().values(): diff --git a/paramiko/_winapi.py b/paramiko/_winapi.py index f141b00..43d9751 100644 --- a/paramiko/_winapi.py +++ b/paramiko/_winapi.py @@ -8,7 +8,11 @@ in jaraco.windows and asking the author to port the fixes back here. import ctypes import ctypes.wintypes -import __builtin__ +from paramiko.py3compat import u +try: + import builtins +except ImportError: + import __builtin__ as builtins ###################### # jaraco.windows.error diff --git a/paramiko/agent.py b/paramiko/agent.py index 23a5a2e..67bb067 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -34,7 +34,7 @@ from paramiko.ssh_exception import SSHException from paramiko.message import Message from paramiko.pkey import PKey from paramiko.channel import Channel -from paramiko.common import io_sleep +from paramiko.common import * from paramiko.util import retry_on_signal SSH2_AGENTC_REQUEST_IDENTITIES, SSH2_AGENT_IDENTITIES_ANSWER, \ diff --git a/paramiko/ber.py b/paramiko/ber.py index 3941581..f3b4b37 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -17,7 +17,8 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -import util +import paramiko.util as util +from paramiko.common import * class BERException (Exception): diff --git a/paramiko/file.py b/paramiko/file.py index 5fd81cf..d177913 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -20,7 +20,7 @@ BufferedFile. """ -from cStringIO import StringIO +from paramiko.common import * class BufferedFile (object): diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 9bcf0d5..c0e58b0 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -23,7 +23,10 @@ L{HostKeys} import base64 import binascii from Crypto.Hash import SHA, HMAC -import UserDict +try: + from collections import MutableMapping +except ImportError: + from UserDict import DictMixin as MutableMapping from paramiko.common import * from paramiko.dsskey import DSSKey @@ -109,7 +112,7 @@ class HostKeyEntry: return '' % (self.hostnames, self.key) -class HostKeys (UserDict.DictMixin): +class HostKeys (MutableMapping): """ Representation of an openssh-style "known hosts" file. Host keys can be read from one or more files, and then individual hosts can be looked up to @@ -215,12 +218,26 @@ class HostKeys (UserDict.DictMixin): @return: keys associated with this host (or C{None}) @rtype: dict(str, L{PKey}) """ - class SubDict (UserDict.DictMixin): + class SubDict (MutableMapping): def __init__(self, hostname, entries, hostkeys): self._hostname = hostname self._entries = entries self._hostkeys = hostkeys + def __iter__(self): + for k in self.keys(): + yield k + + def __len__(self): + return len(self.keys()) + + def __delitem__(self, key): + for e in list(self._entries): + if e.key.get_name() == key: + self._entries.remove(e) + else: + raise KeyError(key) + def __getitem__(self, key): for e in self._entries: if e.key.get_name() == key: @@ -280,6 +297,17 @@ class HostKeys (UserDict.DictMixin): """ self._entries = [] + def __iter__(self): + for k in self.keys(): + yield k + + def __len__(self): + return len(self.keys()) + + def __delitem__(self, key): + k = self[key] + pass + def __getitem__(self, key): ret = self.lookup(key) if ret is None: diff --git a/paramiko/message.py b/paramiko/message.py index c0e8692..d579a16 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -21,9 +21,9 @@ Implementation of an SSH2 "message". """ import struct -import cStringIO from paramiko import util +from paramiko.common import * class Message (object): diff --git a/paramiko/pipe.py b/paramiko/pipe.py index db43d54..e64547b 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -27,6 +27,7 @@ will trigger as readable in select(). import sys import os import socket +from paramiko.py3compat import b def make_pipe (): diff --git a/paramiko/primes.py b/paramiko/primes.py index 9419cd6..bf2b810 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -24,6 +24,7 @@ from Crypto.Util import number from paramiko import util from paramiko.ssh_exception import SSHException +from paramiko.common import * def _generate_prime(bits, rng): diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 218b76e..a10feb0 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -21,6 +21,7 @@ L{ProxyCommand}. """ import os +import sys from shlex import split as shlsplit import signal from subprocess import Popen, PIPE diff --git a/paramiko/transport.py b/paramiko/transport.py index 3155d3f..c6ab127 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -79,7 +79,7 @@ class SecurityOptions (object): C{ValueError} will be raised. If you try to assign something besides a tuple to one of the fields, C{TypeError} will be raised. """ - __slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] + #__slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] def __init__(self, transport): self._transport = transport diff --git a/test.py b/test.py index 6702e53..159794c 100755 --- a/test.py +++ b/test.py @@ -32,19 +32,19 @@ import threading sys.path.append('tests') -from test_message import MessageTest -from test_file import BufferedFileTest -from test_buffered_pipe import BufferedPipeTest -from test_util import UtilTest -from test_hostkeys import HostKeysTest -from test_pkey import KeyTest -from test_kex import KexTest -from test_packetizer import PacketizerTest -from test_auth import AuthTest -from test_transport import TransportTest -from test_sftp import SFTPTest -from test_sftp_big import BigSFTPTest -from test_client import SSHClientTest +from tests.test_message import MessageTest +from tests.test_file import BufferedFileTest +from tests.test_buffered_pipe import BufferedPipeTest +from tests.test_util import UtilTest +from tests.test_hostkeys import HostKeysTest +from tests.test_pkey import KeyTest +from tests.test_kex import KexTest +from tests.test_packetizer import PacketizerTest +from tests.test_auth import AuthTest +from tests.test_transport import TransportTest +from tests.test_sftp import SFTPTest +from tests.test_sftp_big import BigSFTPTest +from tests.test_client import SSHClientTest default_host = 'localhost' default_user = os.environ.get('USER', 'nobody') diff --git a/tests/loop.py b/tests/loop.py index 91c216d..2f3f5df 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -21,6 +21,7 @@ """ import threading, socket +from paramiko.py3compat import * class LoopSocket (object): diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 3021d81..e5f4454 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -21,8 +21,10 @@ A stub SFTP server for loopback SFTP testing. """ import os +import sys from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED +from paramiko.common import * class StubServer (ServerInterface): diff --git a/tests/test_auth.py b/tests/test_auth.py index 61fe63f..1e247d7 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -29,7 +29,8 @@ from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ AuthenticationException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from loop import LoopSocket +from tests.loop import LoopSocket +from tests.util import test_path class NullServer (ServerInterface): diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index 47ece93..04d665c 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -26,7 +26,7 @@ import unittest from paramiko.buffered_pipe import BufferedPipe, PipeTimeout from paramiko import pipe -from util import ParamikoTest +from tests.util import ParamikoTest def delay_thread(pipe): diff --git a/tests/test_client.py b/tests/test_client.py index e535227..7d1e672 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -20,13 +20,14 @@ Some unit tests for SSHClient. """ +import os import socket import threading import time import unittest import weakref from binascii import hexlify - +from tests.util import test_path import paramiko diff --git a/tests/test_kex.py b/tests/test_kex.py index 39d2e17..be8d7f0 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -26,6 +26,7 @@ import paramiko.util from paramiko.kex_group1 import KexGroup1 from paramiko.kex_gex import KexGex from paramiko import Message +from paramiko.common import * class FakeRng (object): diff --git a/tests/test_message.py b/tests/test_message.py index ad622a2..d0e604e 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -22,6 +22,7 @@ Some unit tests for ssh protocol message blocks. import unittest from paramiko.message import Message +from paramiko.common import * class MessageTest (unittest.TestCase): diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 1f5bec0..c39fc45 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -21,10 +21,11 @@ Some unit tests for the ssh2 protocol in Transport. """ import unittest -from loop import LoopSocket +from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util +from paramiko.py3compat import byte_chr class PacketizerTest (unittest.TestCase): diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 8e8c4aa..fe823a7 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -21,10 +21,9 @@ Some unit tests for public/private key objects. """ from binascii import hexlify, unhexlify -import StringIO import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng +from paramiko.common import rng, StringIO, byte_chr # from openssh's ssh-keygen PUB_RSA = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA049W6geFpmsljTwfvI1UmKWWJPNFI74+vNKTk4dmzkQY2yAMs6FhlvhlI8ysU4oj71ZsRYMecHbBbxdN79+JRFVYTKaLqjwGENeTd+yv4q+V2PvZv3fLnzApI3l7EJCqhWwJUHJ1jAkZzqDx0tyOL4uoZpww3nmE0kb3y21tH4c=' diff --git a/tests/test_sftp.py b/tests/test_sftp.py index cc512c1..3c1fcd5 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -31,11 +31,12 @@ import warnings import sys import threading import unittest -import StringIO import paramiko -from stub_sftp import StubServer, StubSFTPServer -from loop import LoopSocket +from paramiko.common import * +from tests.stub_sftp import StubServer, StubSFTPServer +from tests.loop import LoopSocket +from tests.util import test_path from paramiko.sftp_attr import SFTPAttributes ARTICLE = ''' diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 04b15b0..9a4ea31 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -33,9 +33,10 @@ import time import unittest import paramiko -from stub_sftp import StubServer, StubSFTPServer -from loop import LoopSocket -from test_sftp import get_sftp +from paramiko.common import * +from tests.stub_sftp import StubServer, StubSFTPServer +from tests.loop import LoopSocket +from tests.test_sftp import get_sftp FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') diff --git a/tests/test_transport.py b/tests/test_transport.py index e8f7f36..ed8ebb4 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -35,8 +35,8 @@ from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST from paramiko.message import Message -from loop import LoopSocket -from util import ParamikoTest +from tests.loop import LoopSocket +from tests.util import ParamikoTest, test_path LONG_BANNER = """\ diff --git a/tests/test_util.py b/tests/test_util.py index 12677a9..12575f8 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -21,15 +21,15 @@ Some unit tests for utility functions. """ from binascii import hexlify -import cStringIO import errno import os import unittest from Crypto.Hash import SHA import paramiko.util from paramiko.util import lookup_ssh_host_config as host_config +from paramiko.py3compat import StringIO, byte_ord -from util import ParamikoTest +from tests.util import ParamikoTest test_config_file = """\ Host * diff --git a/tests/util.py b/tests/util.py index 2e0be08..1b380b7 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,3 +1,4 @@ +import os import unittest From 644c52266caaa5b975a13e355d1bfd921c9c9ddb Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:22:52 -0700 Subject: [PATCH 09/56] Use test_path to avoid relative path issues --- tests/test_auth.py | 8 ++++---- tests/test_client.py | 16 ++++++++-------- tests/test_sftp.py | 2 +- tests/test_transport.py | 8 ++++---- tests/util.py | 6 ++++++ 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 1e247d7..ea9b2bd 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -36,8 +36,8 @@ from tests.util import test_path class NullServer (ServerInterface): paranoid_did_password = False paranoid_did_public_key = False - paranoid_key = DSSKey.from_private_key_file('tests/test_dss.key') - + paranoid_key = DSSKey.from_private_key_file(test_path('test_dss.key')) + def get_allowed_auths(self, username): if username == 'slowdive': return 'publickey,password' @@ -111,8 +111,8 @@ class AuthTest (unittest.TestCase): self.sockc.close() def start_server(self): - host_key = RSAKey.from_private_key_file('tests/test_rsa.key') self.public_host_key = RSAKey(data=str(host_key)) + host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.ts.add_server_key(host_key) self.event = threading.Event() self.server = NullServer() @@ -163,7 +163,7 @@ class AuthTest (unittest.TestCase): self.tc.connect(hostkey=self.public_host_key) remain = self.tc.auth_password(username='paranoid', password='paranoid') self.assertEquals(['publickey'], remain) - key = DSSKey.from_private_key_file('tests/test_dss.key') + key = DSSKey.from_private_key_file(test_path('test_dss.key')) remain = self.tc.auth_publickey(username='paranoid', key=key) self.assertEquals([], remain) self.verify_finished() diff --git a/tests/test_client.py b/tests/test_client.py index 7d1e672..91aac33 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -76,7 +76,7 @@ class SSHClientTest (unittest.TestCase): def _run(self): self.socks, addr = self.sockl.accept() self.ts = paramiko.Transport(self.socks) - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.ts.add_server_key(host_key) server = NullServer() self.ts.start_server(self.event, server) @@ -86,8 +86,8 @@ class SSHClientTest (unittest.TestCase): """ verify that the SSHClient stuff works too. """ - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = paramiko.RSAKey(data=str(host_key)) + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) @@ -119,12 +119,12 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient works with a DSA key. """ - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = paramiko.RSAKey(data=str(host_key)) + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) - self.tc.connect(self.addr, self.port, username='slowdive', key_filename='tests/test_dss.key') + self.tc.connect(self.addr, self.port, username='slowdive', key_filename=test_path('test_dss.key')) self.event.wait(1.0) self.assert_(self.event.isSet()) @@ -152,12 +152,12 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient accepts and tries multiple key files. """ - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = paramiko.RSAKey(data=str(host_key)) + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) - self.tc.connect(self.addr, self.port, username='slowdive', key_filename=[ 'tests/test_rsa.key', 'tests/test_dss.key' ]) + self.tc.connect(self.addr, self.port, username='slowdive', key_filename=[ test_path('test_rsa.key'), test_path('test_dss.key') ]) self.event.wait(1.0) self.assert_(self.event.isSet()) @@ -169,8 +169,8 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient's AutoAddPolicy works. """ - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = paramiko.RSAKey(data=str(host_key)) + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -190,8 +190,8 @@ class SSHClientTest (unittest.TestCase): verify that when an SSHClient is collected, its transport (and the transport's packetizer) is closed. """ - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = paramiko.RSAKey(data=str(host_key)) + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 3c1fcd5..a421355 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -122,7 +122,7 @@ class SFTPTest (unittest.TestCase): tc = paramiko.Transport(sockc) ts = paramiko.Transport(socks) - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) ts.add_server_key(host_key) event = threading.Event() server = StubServer() diff --git a/tests/test_transport.py b/tests/test_transport.py index ed8ebb4..ccd3f0c 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -55,7 +55,7 @@ Maybe. class NullServer (ServerInterface): paranoid_did_password = False paranoid_did_public_key = False - paranoid_key = DSSKey.from_private_key_file('tests/test_dss.key') + paranoid_key = DSSKey.from_private_key_file(test_path('test_dss.key')) def get_allowed_auths(self, username): if username == 'slowdive': @@ -121,8 +121,8 @@ class TransportTest(ParamikoTest): self.sockc.close() def setup_test_server(self, client_options=None, server_options=None): - host_key = RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = RSAKey(data=str(host_key)) + host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.ts.add_server_key(host_key) if client_options is not None: @@ -171,8 +171,8 @@ class TransportTest(ParamikoTest): loopback sockets. this is hardly "simple" but it's simpler than the later tests. :) """ - host_key = RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = RSAKey(data=str(host_key)) + host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() @@ -196,8 +196,8 @@ class TransportTest(ParamikoTest): """ verify that a long banner doesn't mess up the handshake. """ - host_key = RSAKey.from_private_key_file('tests/test_rsa.key') public_host_key = RSAKey(data=str(host_key)) + host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() diff --git a/tests/util.py b/tests/util.py index 1b380b7..66d2696 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,6 +1,8 @@ import os import unittest +root_path = os.path.dirname(os.path.realpath(__file__)) + class ParamikoTest(unittest.TestCase): # for Python 2.3 and below @@ -9,3 +11,7 @@ class ParamikoTest(unittest.TestCase): if not hasattr(unittest.TestCase, 'assertFalse'): assertFalse = unittest.TestCase.failIf + +def test_path(filename): + return os.path.join(root_path, filename) + From 6bd1e42b43017c8df3edd826fad5aca798538b10 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:23:36 -0700 Subject: [PATCH 10/56] Fix imports --- paramiko/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index a12ee04..86e0bc6 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -96,14 +96,14 @@ for c in locals().values(): c.__module__ = __name__ del c -from common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED, \ - OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_FAILED_CONNECT_FAILED, \ - OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, OPEN_FAILED_RESOURCE_SHORTAGE +from paramiko.common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED, \ + OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_FAILED_CONNECT_FAILED, \ + OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, OPEN_FAILED_RESOURCE_SHORTAGE -from sftp import SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, \ - SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED +from paramiko.sftp import SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, \ + SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED -from common import io_sleep +from paramiko.common import io_sleep __all__ = [ 'Transport', 'SSHClient', From 2ea352b8baf1037bd22fc79146ef646c163a7692 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:46:33 -0700 Subject: [PATCH 11/56] Fix dict iters, sorts, exceptions, bytes renames and tuple args --- paramiko/__init__.py | 2 +- paramiko/auth_handler.py | 3 +- paramiko/channel.py | 4 +- paramiko/client.py | 26 +++++------ paramiko/config.py | 2 +- paramiko/dsskey.py | 4 +- paramiko/file.py | 6 +-- paramiko/hostkeys.py | 4 +- paramiko/kex_gex.py | 4 +- paramiko/packet.py | 9 ++-- paramiko/pkey.py | 4 +- paramiko/primes.py | 7 ++- paramiko/proxy.py | 10 +++-- paramiko/server.py | 3 +- paramiko/sftp_attr.py | 4 +- paramiko/sftp_client.py | 8 ++-- paramiko/sftp_file.py | 6 +-- paramiko/sftp_handle.py | 6 ++- paramiko/sftp_server.py | 10 +++-- paramiko/transport.py | 96 ++++++++++++++++++++++------------------ paramiko/util.py | 5 ++- tests/stub_sftp.py | 42 ++++++++++++------ tests/test_transport.py | 14 +++--- tests/test_util.py | 6 +-- 24 files changed, 160 insertions(+), 125 deletions(-) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 86e0bc6..160ed39 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -90,7 +90,7 @@ from paramiko.config import SSHConfig from paramiko.proxy import ProxyCommand # fix module names for epydoc -for c in locals().values(): +for c in list(locals().values()): if issubclass(type(c), type) or type(c).__name__ == 'classobj': # classobj for exceptions :/ c.__module__ = __name__ diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index acb7c8b..de0b205 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -308,7 +308,8 @@ class AuthHandler (object): keyblob = m.get_string() try: key = self.transport._key_info[keytype](Message(keyblob)) - except SSHException, e: + except SSHException: + e = sys.exc_info()[1] self.transport._log(INFO, 'Auth rejected: public key: %s' % str(e)) key = None except: diff --git a/paramiko/channel.py b/paramiko/channel.py index d1e6333..d351768 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -615,7 +615,7 @@ class Channel (object): """ try: out = self.in_buffer.read(nbytes, self.timeout) - except PipeTimeout, e: + except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) @@ -665,7 +665,7 @@ class Channel (object): """ try: out = self.in_stderr_buffer.read(nbytes, self.timeout) - except PipeTimeout, e: + except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) diff --git a/paramiko/client.py b/paramiko/client.py index c5a2d1a..98bb47f 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -193,8 +193,8 @@ class SSHClient (object): self.load_host_keys(self.known_hosts) f = open(filename, 'w') - for hostname, keys in self._host_keys.iteritems(): - for keytype, key in keys.iteritems(): + for hostname, keys in self._host_keys.items(): + for keytype, key in keys.items(): f.write('%s %s %s\n' % (hostname, keytype, key.get_base64())) f.close() @@ -452,8 +452,8 @@ class SSHClient (object): two_factor = (allowed_types == ['password']) if not two_factor: return - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor: for key_filename in key_filenames: @@ -466,8 +466,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor and allow_agent: if self._agent == None: @@ -482,8 +482,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] if not two_factor: keyfiles = [] @@ -514,17 +514,15 @@ class SSHClient (object): if not two_factor: return break - except SSHException, e: - saved_exception = e - except IOError, e: - saved_exception = e + except (SSHException, IOError): + saved_exception = sys.exc_info()[1] if password is not None: try: self._transport.auth_password(username, password) return - except SSHException, e: - saved_exception = e + except SSHException: + saved_exception = sys.exc_info()[1] elif two_factor: raise SSHException('Two-factor authentication requires a password') diff --git a/paramiko/config.py b/paramiko/config.py index 1705de7..51415be 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -173,7 +173,7 @@ class SSHConfig (object): ret = {} for match in matches: - for key, value in match['config'].iteritems(): + for key, value in match['config'].items(): if key not in ret: # Create a copy of the original value, # else it will reference the original list diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index f6ecb2a..ff74488 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -184,8 +184,8 @@ class DSSKey (PKey): # DSAPrivateKey = { version = 0, p, q, g, y, x } try: keylist = BER(data).decode() - except BERException, x: - raise SSHException('Unable to parse key file: ' + str(x)) + except BERException: + raise SSHException('Unable to parse key file: ' + str(sys.exc_info()[1])) if (type(keylist) is not list) or (len(keylist) < 6) or (keylist[0] != 0): raise SSHException('not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] diff --git a/paramiko/file.py b/paramiko/file.py index d177913..1a8d3ee 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -254,14 +254,14 @@ class BufferedFile (object): @rtype: list """ lines = [] - bytes = 0 + byte_count = 0 while True: line = self.readline() if len(line) == 0: break lines.append(line) - bytes += len(line) - if (sizehint is not None) and (bytes >= sizehint): + byte_count += len(line) + if (sizehint is not None) and (byte_count >= sizehint): break return lines diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index c0e58b0..f548263 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -91,8 +91,8 @@ class HostKeyEntry: log.info("Unable to handle key of type %s" % (keytype,)) return None - except binascii.Error, e: - raise InvalidHostKey(line, e) + except binascii.Error: + raise InvalidHostKey(line, sys.exc_info()[1]) return cls(names, key) from_line = classmethod(from_line) diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index c0455a1..669bd34 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -95,14 +95,14 @@ class KexGex (object): q = (self.p - 1) // 2 qnorm = util.deflate_long(q, 0) qhbyte = ord(qnorm[0]) - bytes = len(qnorm) + byte_count = len(qnorm) qmask = 0xff while not (qhbyte & 0x80): qhbyte <<= 1 qmask >>= 1 while True: - x_bytes = self.transport.rng.read(bytes) x_bytes = chr(ord(x_bytes[0]) & qmask) + x_bytes[1:] + x_bytes = self.transport.rng.read(byte_count) x = util.inflate_long(x_bytes, 1) if (x > 1) and (x < q): break diff --git a/paramiko/packet.py b/paramiko/packet.py index 6ab7363..3a26b6b 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -214,7 +214,8 @@ class Packetizer (object): n -= len(x) except socket.timeout: got_timeout = True - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. @@ -243,7 +244,8 @@ class Packetizer (object): n = self.__socket.send(out) except socket.timeout: retry_write = True - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): retry_write = True elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): @@ -468,7 +470,8 @@ class Packetizer (object): break except socket.timeout: pass - except EnvironmentError, e: + except EnvironmentError: + e = sys.exc_info()[1] if ((type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR)): pass diff --git a/paramiko/pkey.py b/paramiko/pkey.py index b1199df..d61809f 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -304,8 +304,8 @@ class PKey (object): # if we trudged to the end of the file, just try to cope. try: data = base64.decodestring(''.join(lines[start:end])) - except base64.binascii.Error, e: - raise SSHException('base64 decoding error: ' + str(e)) + except base64.binascii.Error: + raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) if 'proc-type' not in headers: # unencryped: done return data diff --git a/paramiko/primes.py b/paramiko/primes.py index bf2b810..1dd87da 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -47,7 +47,7 @@ def _generate_prime(bits, rng): def _roll_random(rng, n): "returns a random # from 0 to N-1" bits = util.bit_length(n-1) - bytes = (bits + 7) // 8 + byte_count = (bits + 7) // 8 hbyte_mask = pow(2, bits % 8) - 1 # so here's the plan: @@ -57,7 +57,7 @@ def _roll_random(rng, n): # fits, so i can't guarantee that this loop will ever finish, but the odds # of it looping forever should be infinitesimal. while True: - x = rng.read(bytes) + x = rng.read(byte_count) if hbyte_mask > 0: x = chr(ord(x[0]) & hbyte_mask) + x[1:] num = util.inflate_long(x, 1) @@ -125,8 +125,7 @@ class ModulusPack (object): f.close() def get_modulus(self, min, prefer, max): - bitsizes = self.pack.keys() - bitsizes.sort() + bitsizes = sorted(self.pack.keys(), key=hash) if len(bitsizes) == 0: raise SSHException('no moduli available') good = -1 diff --git a/paramiko/proxy.py b/paramiko/proxy.py index a10feb0..43c4666 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -60,12 +60,13 @@ class ProxyCommand(object): """ try: self.process.stdin.write(content) - except IOError, e: + except IOError: + e = sys.exc_info()[1] # There was a problem with the child process. It probably # died and we can't proceed. The best option here is to # raise an exception informing the user that the informed # ProxyCommand is not working. - raise BadProxyCommand(' '.join(self.cmd), e.strerror) + raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) return len(content) def recv(self, size): @@ -80,8 +81,9 @@ class ProxyCommand(object): """ try: return os.read(self.process.stdout.fileno(), size) - except IOError, e: - raise BadProxyCommand(' '.join(self.cmd), e.strerror) + except IOError: + e = sys.exc_info()[1] + raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) def close(self): os.kill(self.process.pid, signal.SIGTERM) diff --git a/paramiko/server.py b/paramiko/server.py index fdb4094..4b6e8b1 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -623,7 +623,8 @@ class SubsystemHandler (threading.Thread): try: self.__transport._log(DEBUG, 'Starting handler for subsystem %s' % self.__name) self.start_subsystem(self.__name, self.__transport, self.__channel) - except Exception, e: + except Exception: + e = sys.exc_info()[1] self.__transport._log(ERROR, 'Exception in subsystem handler for "%s": %s' % (self.__name, str(e))) self.__transport._log(ERROR, util.tb_strings()) diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index b459b04..84c8392 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -143,7 +143,7 @@ class SFTPAttributes (object): msg.add_int(long(self.st_mtime)) if self._flags & self.FLAG_EXTENDED: msg.add_int(len(self.attr)) - for key, val in self.attr.iteritems(): + for key, val in self.attr.items(): msg.add_string(key) msg.add_string(val) return @@ -158,7 +158,7 @@ class SFTPAttributes (object): out += 'mode=' + oct(self.st_mode) + ' ' if (self.st_atime is not None) and (self.st_mtime is not None): out += 'atime=%d mtime=%d ' % (self.st_atime, self.st_mtime) - for k, v in self.attr.iteritems(): + for k, v in self.attr.items(): out += '"%s"=%r ' % (str(k), v) out += ']' return out diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index d921574..954cfa0 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -85,7 +85,7 @@ class SFTPClient (BaseSFTP): self.ultra_debug = transport.get_hexdump() try: server_version = self._send_version() - except EOFError, x: + except EOFError: raise SSHException('EOF during negotiation') self._log(INFO, 'Opened sftp connection (server version %d)' % server_version) @@ -178,7 +178,7 @@ class SFTPClient (BaseSFTP): while True: try: t, msg = self._request(CMD_READDIR, handle) - except EOFError, e: + except EOFError: # done with handle break if t != CMD_NAME: @@ -717,8 +717,8 @@ class SFTPClient (BaseSFTP): while True: try: t, data = self._read_packet() - except EOFError, e: - raise SSHException('Server connection dropped: %s' % (str(e),)) + except EOFError: + raise SSHException('Server connection dropped: %s' % str(sys.exc_info()[1])) msg = Message(data) num = msg.get_int() if num not in self._expecting: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 4ec936d..c0bc898 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -94,7 +94,7 @@ class SFTPFile (BufferedFile): k = [i for i in self._prefetch_reads if i[0] <= offset] if len(k) == 0: return False - k.sort(lambda x, y: cmp(x[0], y[0])) + k.sort(key=hash) buf_offset, buf_size = k[-1] if buf_offset + buf_size <= offset: # prefetch request ends before this one begins @@ -464,8 +464,8 @@ class SFTPFile (BufferedFile): # save exception and re-raise it on next file operation try: self.sftp._convert_status(msg) - except Exception, x: - self._saved_exception = x + except Exception: + self._saved_exception = sys.exc_info()[1] return if t != CMD_DATA: raise SFTPError('Expected data') diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 29d3d0d..8b78063 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -100,7 +100,8 @@ class SFTPHandle (object): readfile.seek(offset) self.__tell = offset data = readfile.read(length) - except IOError, e: + except IOError: + e = sys.exc_info()[1] self.__tell = None return SFTPServer.convert_errno(e.errno) self.__tell += len(data) @@ -139,7 +140,8 @@ class SFTPHandle (object): self.__tell = offset writefile.write(data) writefile.flush() - except IOError, e: + except IOError: + e = sys.exc_info()[1] self.__tell = None return SFTPServer.convert_errno(e.errno) if self.__tell is not None: diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 1833c2e..ec34008 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -92,7 +92,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): except EOFError: self._log(DEBUG, 'EOF -- end of session') return - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(DEBUG, 'Exception on channel: ' + str(e)) self._log(DEBUG, util.tb_strings()) return @@ -100,7 +101,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): request_number = msg.get_int() try: self._process(t, request_number, msg) - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(DEBUG, 'Exception in server processing: ' + str(e)) self._log(DEBUG, util.tb_strings()) # send some kind of failure message, at least @@ -113,9 +115,9 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self.server.session_ended() super(SFTPServer, self).finish_subsystem() # close any file handles that were left open (so we can return them to the OS quickly) - for f in self.file_table.itervalues(): + for f in self.file_table.values(): f.close() - for f in self.folder_table.itervalues(): + for f in self.folder_table.values(): f.close() self.file_table = {} self.folder_table = {} diff --git a/paramiko/transport.py b/paramiko/transport.py index c6ab127..c8a693e 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -177,7 +177,7 @@ class ChannelMap (object): def values(self): self._lock.acquire() try: - return self._map.values() + return list(self._map.values()) finally: self._lock.release() @@ -294,8 +294,8 @@ class Transport (threading.Thread): sock = socket.socket(af, socket.SOCK_STREAM) try: retry_on_signal(lambda: sock.connect((hostname, port))) - except socket.error, e: - reason = str(e) + except socket.error: + reason = str(sys.exc_info()[1]) else: break else: @@ -616,7 +616,7 @@ class Transport (threading.Thread): if not self.active: return self.stop_thread() - for chan in self._channels.values(): + for chan in list(self._channels.values()): chan._unlink() self.sock.close() @@ -691,7 +691,7 @@ class Transport (threading.Thread): """ return self.open_channel('auth-agent@openssh.com') - def open_forwarded_tcpip_channel(self, (src_addr, src_port), (dest_addr, dest_port)): + def open_forwarded_tcpip_channel(self, src_addr_port, dest_addr_port): """ Request a new channel back to the client, of type C{"forwarded-tcpip"}. This is used after a client has requested port forwarding, for sending @@ -702,6 +702,8 @@ class Transport (threading.Thread): @param dest_addr: local (server) connected address @param dest_port: local (server) connected port """ + src_addr, src_port = src_addr_port + dest_addr, dest_port = dest_addr_port return self.open_channel('forwarded-tcpip', (dest_addr, dest_port), (src_addr, src_port)) def open_channel(self, kind, dest_addr=None, src_addr=None): @@ -811,7 +813,9 @@ class Transport (threading.Thread): if port == 0: port = response.get_int() if handler is None: - def default_handler(channel, (src_addr, src_port), (dest_addr, dest_port)): + def default_handler(channel, src_addr_port, dest_addr_port): + #src_addr, src_port = src_addr_port + #dest_addr, dest_port = dest_addr_port self._queue_incoming_channel(channel) handler = default_handler self._tcp_handler = handler @@ -845,22 +849,22 @@ class Transport (threading.Thread): """ return SFTPClient.from_transport(self) - def send_ignore(self, bytes=None): + def send_ignore(self, byte_count=None): """ Send a junk packet across the encrypted link. This is sometimes used to add "noise" to a connection to confuse would-be attackers. It can also be used as a keep-alive for long lived connections traversing firewalls. - @param bytes: the number of random bytes to send in the payload of the + @param byte_count: the number of random bytes to send in the payload of the ignored packet -- defaults to a random number from 10 to 41. - @type bytes: int + @type byte_count: int """ m = Message() m.add_byte(chr(MSG_IGNORE)) - if bytes is None: - bytes = (ord(rng.read(1)) % 32) + 10 - m.add_bytes(rng.read(bytes)) + if byte_count is None: + byte_count = (byte_ord(rng.read(1)) % 32) + 10 + m.add_bytes(rng.read(byte_count)) self._send_user_message(m) def renegotiate_keys(self): @@ -1181,7 +1185,8 @@ class Transport (threading.Thread): return [] try: return self.auth_handler.wait_for_response(my_event) - except BadAuthenticationType, x: + except BadAuthenticationType: + x = sys.exc_info()[1] # if password auth isn't allowed, but keyboard-interactive *is*, try to fudge it if not fallback or ('keyboard-interactive' not in x.allowed_types): raise @@ -1197,7 +1202,7 @@ class Transport (threading.Thread): return [] return [ password ] return self.auth_interactive(username, handler) - except SSHException, ignored: + except SSHException: # attempt failed; just raise the original exception raise x return None @@ -1513,7 +1518,7 @@ class Transport (threading.Thread): # only called if a channel has turned on x11 forwarding if handler is None: # by default, use the same mechanism as accept() - def default_handler(channel, (src_addr, src_port)): + def default_handler(channel, src_addr_port): self._queue_incoming_channel(channel) self._x11_handler = default_handler else: @@ -1604,22 +1609,26 @@ class Transport (threading.Thread): msg.add_byte(chr(MSG_UNIMPLEMENTED)) msg.add_int(m.seqno) self._send_message(msg) - except SSHException, e: + except SSHException: + e = sys.exc_info()[1] self._log(ERROR, 'Exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e - except EOFError, e: + except EOFError: + e = sys.exc_info()[1] self._log(DEBUG, 'EOF in transport thread') #self._log(DEBUG, util.tb_strings()) self.saved_exception = e - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] if type(e.args) is tuple: emsg = '%s (%d)' % (e.args[1], e.args[0]) else: emsg = e.args self._log(ERROR, 'Socket exception: ' + emsg) self.saved_exception = e - except Exception, e: + except Exception: + e = sys.exc_info()[1] self._log(ERROR, 'Unknown exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e @@ -1679,7 +1688,8 @@ class Transport (threading.Thread): buf = self.packetizer.readline(timeout) except ProxyCommandFailure: raise - except Exception, x: + except Exception: + x = sys.exc_info()[1] raise SSHException('Error reading SSH protocol banner' + str(x)) if buf[:4] == 'SSH-': break @@ -1690,7 +1700,7 @@ class Transport (threading.Thread): self.remote_version = buf # pull off any attached comment comment = '' - i = string.find(buf, ' ') + i = buf.find(' ') if i >= 0: comment = buf[i+1:] buf = buf[:i] @@ -1774,19 +1784,19 @@ class Transport (threading.Thread): # as a server, we pick the first item in the client's list that we support. # as a client, we pick the first item in our list that the server supports. if self.server_mode: - agreed_kex = filter(self._preferred_kex.__contains__, kex_algo_list) + agreed_kex = list(filter(self._preferred_kex.__contains__, kex_algo_list)) else: - agreed_kex = filter(kex_algo_list.__contains__, self._preferred_kex) + agreed_kex = list(filter(kex_algo_list.__contains__, self._preferred_kex)) if len(agreed_kex) == 0: raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)') self.kex_engine = self._kex_info[agreed_kex[0]](self) if self.server_mode: - available_server_keys = filter(self.server_key_dict.keys().__contains__, - self._preferred_keys) - agreed_keys = filter(available_server_keys.__contains__, server_key_algo_list) + available_server_keys = list(filter(list(self.server_key_dict.keys()).__contains__, + self._preferred_keys)) + agreed_keys = list(filter(available_server_keys.__contains__, server_key_algo_list)) else: - agreed_keys = filter(server_key_algo_list.__contains__, self._preferred_keys) + agreed_keys = list(filter(server_key_algo_list.__contains__, self._preferred_keys)) if len(agreed_keys) == 0: raise SSHException('Incompatible ssh peer (no acceptable host key)') self.host_key_type = agreed_keys[0] @@ -1794,15 +1804,15 @@ class Transport (threading.Thread): raise SSHException('Incompatible ssh peer (can\'t match requested host key type)') if self.server_mode: - agreed_local_ciphers = filter(self._preferred_ciphers.__contains__, - server_encrypt_algo_list) - agreed_remote_ciphers = filter(self._preferred_ciphers.__contains__, - client_encrypt_algo_list) + agreed_local_ciphers = list(filter(self._preferred_ciphers.__contains__, + server_encrypt_algo_list)) + agreed_remote_ciphers = list(filter(self._preferred_ciphers.__contains__, + client_encrypt_algo_list)) else: - agreed_local_ciphers = filter(client_encrypt_algo_list.__contains__, - self._preferred_ciphers) - agreed_remote_ciphers = filter(server_encrypt_algo_list.__contains__, - self._preferred_ciphers) + agreed_local_ciphers = list(filter(client_encrypt_algo_list.__contains__, + self._preferred_ciphers)) + agreed_remote_ciphers = list(filter(server_encrypt_algo_list.__contains__, + self._preferred_ciphers)) if (len(agreed_local_ciphers) == 0) or (len(agreed_remote_ciphers) == 0): raise SSHException('Incompatible ssh server (no acceptable ciphers)') self.local_cipher = agreed_local_ciphers[0] @@ -1810,22 +1820,22 @@ class Transport (threading.Thread): self._log(DEBUG, 'Ciphers agreed: local=%s, remote=%s' % (self.local_cipher, self.remote_cipher)) if self.server_mode: - agreed_remote_macs = filter(self._preferred_macs.__contains__, client_mac_algo_list) - agreed_local_macs = filter(self._preferred_macs.__contains__, server_mac_algo_list) + agreed_remote_macs = list(filter(self._preferred_macs.__contains__, client_mac_algo_list)) + agreed_local_macs = list(filter(self._preferred_macs.__contains__, server_mac_algo_list)) else: - agreed_local_macs = filter(client_mac_algo_list.__contains__, self._preferred_macs) - agreed_remote_macs = filter(server_mac_algo_list.__contains__, self._preferred_macs) + agreed_local_macs = list(filter(client_mac_algo_list.__contains__, self._preferred_macs)) + agreed_remote_macs = list(filter(server_mac_algo_list.__contains__, self._preferred_macs)) if (len(agreed_local_macs) == 0) or (len(agreed_remote_macs) == 0): raise SSHException('Incompatible ssh server (no acceptable macs)') self.local_mac = agreed_local_macs[0] self.remote_mac = agreed_remote_macs[0] if self.server_mode: - agreed_remote_compression = filter(self._preferred_compression.__contains__, client_compress_algo_list) - agreed_local_compression = filter(self._preferred_compression.__contains__, server_compress_algo_list) + agreed_remote_compression = list(filter(self._preferred_compression.__contains__, client_compress_algo_list)) + agreed_local_compression = list(filter(self._preferred_compression.__contains__, server_compress_algo_list)) else: - agreed_local_compression = filter(client_compress_algo_list.__contains__, self._preferred_compression) - agreed_remote_compression = filter(server_compress_algo_list.__contains__, self._preferred_compression) + agreed_local_compression = list(filter(client_compress_algo_list.__contains__, self._preferred_compression)) + agreed_remote_compression = list(filter(server_compress_algo_list.__contains__, self._preferred_compression)) if (len(agreed_local_compression) == 0) or (len(agreed_remote_compression) == 0): raise SSHException('Incompatible ssh server (no acceptable compression) %r %r %r' % (agreed_local_compression, agreed_remote_compression, self._preferred_compression)) self.local_compression = agreed_local_compression[0] diff --git a/paramiko/util.py b/paramiko/util.py index 85ee6b0..299f44a 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -276,13 +276,14 @@ def retry_on_signal(function): while True: try: return function() - except EnvironmentError, e: + except EnvironmentError: + e = sys.exc_info()[1] if e.errno != errno.EINTR: raise class Counter (object): """Stateful counter for CTR mode crypto""" - def __init__(self, nbits, initial_value=1L, overflow=0L): + def __init__(self, nbits, initial_value=long_one, overflow=long_zero): self.blocksize = nbits / 8 self.overflow = overflow # start with value - 1 so we don't have to store intermediate values when counting diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index e5f4454..b8bea9b 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -40,7 +40,8 @@ class StubSFTPHandle (SFTPHandle): def stat(self): try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def chattr(self, attr): @@ -49,7 +50,8 @@ class StubSFTPHandle (SFTPHandle): try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) @@ -71,21 +73,24 @@ class StubSFTPServer (SFTPServerInterface): attr.filename = fname out.append(attr) return out - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def stat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.stat(path)) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def lstat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) def open(self, path, flags, attr): @@ -100,7 +105,8 @@ class StubSFTPServer (SFTPServerInterface): # os.open() defaults to 0777 which is # an odd default mode for files fd = os.open(path, flags, 0666) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) if (flags & os.O_CREAT) and (attr is not None): attr._flags &= ~attr.FLAG_PERMISSIONS @@ -120,7 +126,8 @@ class StubSFTPServer (SFTPServerInterface): fstr = 'rb' try: f = os.fdopen(fd, fstr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path @@ -132,7 +139,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.remove(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -141,7 +149,8 @@ class StubSFTPServer (SFTPServerInterface): newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -151,7 +160,8 @@ class StubSFTPServer (SFTPServerInterface): os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -159,7 +169,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.rmdir(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -167,7 +178,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -187,7 +199,8 @@ class StubSFTPServer (SFTPServerInterface): target_path = '' try: os.symlink(target_path, path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -195,7 +208,8 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: symlink = os.readlink(path) - except OSError, e: + except OSError: + e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) # if it's absolute, remove the root if os.path.isabs(symlink): diff --git a/tests/test_transport.py b/tests/test_transport.py index ccd3f0c..d8cd10d 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -249,7 +249,7 @@ class TransportTest(ParamikoTest): try: chan.exec_command('no') self.assert_(False) - except SSHException, x: + except SSHException: pass chan = self.tc.open_session() @@ -302,7 +302,8 @@ class TransportTest(ParamikoTest): try: chan = self.tc.open_channel('bogus') self.fail('expected exception') - except ChannelException, x: + except ChannelException: + x = sys.exc_info()[1] self.assert_(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self): @@ -444,7 +445,8 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) requested = [] - def handler(c, (addr, port)): + def handler(c, addr_port): + addr, port = addr_port requested.append((addr, port)) self.tc._queue_incoming_channel(c) @@ -479,9 +481,9 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) requested = [] - def handler(c, (origin_addr, origin_port), (server_addr, server_port)): - requested.append((origin_addr, origin_port)) - requested.append((server_addr, server_port)) + def handler(c, origin_addr_port, server_addr_port): + requested.append(origin_addr_port) + requested.append(server_addr_port) self.tc._queue_incoming_channel(c) port = self.tc.request_port_forward('127.0.0.1', 0, handler) diff --git a/tests/test_util.py b/tests/test_util.py index 12575f8..7e656df 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -65,7 +65,7 @@ class UtilTest(ParamikoTest): """ verify that all the classes can be imported from paramiko. """ - symbols = globals().keys() + symbols = list(globals().keys()) self.assertTrue('Transport' in symbols) self.assertTrue('SSHClient' in symbols) self.assertTrue('MissingHostKeyPolicy' in symbols) @@ -148,8 +148,8 @@ class UtilTest(ParamikoTest): try: hostdict = paramiko.util.load_host_keys('hostfile.temp') self.assertEquals(2, len(hostdict)) - self.assertEquals(1, len(hostdict.values()[0])) - self.assertEquals(1, len(hostdict.values()[1])) + self.assertEquals(1, len(list(hostdict.values())[0])) + self.assertEquals(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) finally: From 339d73cc13765bea4dd5b683ca14b02c9baa589f Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:48:50 -0700 Subject: [PATCH 12/56] Fix imports --- paramiko/agent.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paramiko/agent.py b/paramiko/agent.py index 67bb067..f50a7c0 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -215,7 +215,7 @@ class AgentClientProxy(object): # probably a dangling env var: the ssh agent is gone return elif sys.platform == 'win32': - import win_pageant + from . import win_pageant if win_pageant.can_talk_to_agent(): conn = win_pageant.PageantConnection() else: @@ -334,7 +334,7 @@ class Agent(AgentSSH): # probably a dangling env var: the ssh agent is gone return elif sys.platform == 'win32': - import win_pageant + from . import win_pageant if win_pageant.can_talk_to_agent(): conn = win_pageant.PageantConnection() else: From 0e4ce3762a5b25c5d3eb89335495d3bb9054e3e7 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:09:34 -0700 Subject: [PATCH 13/56] Fix message sending Create constants for byte messages, implement asbytes so many methods can take Message and key objects directly and split get_string into get_text and get_binary. Also, change int handling to use mpint with a flag whenever the int is greater than 32 bits. --- paramiko/agent.py | 24 +++++---- paramiko/auth_handler.py | 68 ++++++++++++------------ paramiko/ber.py | 11 ++-- paramiko/channel.py | 48 ++++++++--------- paramiko/dsskey.py | 19 ++++--- paramiko/ecdsakey.py | 29 +++++----- paramiko/hostkeys.py | 2 +- paramiko/kex_gex.py | 22 ++++---- paramiko/kex_group1.py | 16 +++--- paramiko/message.py | 112 ++++++++++++++++++++++++++++----------- paramiko/pkey.py | 22 +++++--- paramiko/rsakey.py | 15 +++--- paramiko/sftp.py | 5 +- paramiko/sftp_client.py | 20 ++++--- paramiko/sftp_file.py | 4 +- paramiko/sftp_server.py | 52 +++++++++--------- paramiko/transport.py | 72 ++++++++++++------------- tests/test_auth.py | 2 +- tests/test_client.py | 10 ++-- tests/test_hostkeys.py | 16 +++--- tests/test_kex.py | 26 ++++----- tests/test_message.py | 60 ++++++++++++--------- tests/test_packetizer.py | 2 +- tests/test_pkey.py | 24 ++++----- tests/test_sftp.py | 3 +- tests/test_sftp_big.py | 2 +- tests/test_transport.py | 8 +-- 27 files changed, 388 insertions(+), 306 deletions(-) diff --git a/paramiko/agent.py b/paramiko/agent.py index f50a7c0..a6a177d 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -37,8 +37,11 @@ from paramiko.channel import Channel from paramiko.common import * from paramiko.util import retry_on_signal -SSH2_AGENTC_REQUEST_IDENTITIES, SSH2_AGENT_IDENTITIES_ANSWER, \ - SSH2_AGENTC_SIGN_REQUEST, SSH2_AGENT_SIGN_RESPONSE = range(11, 15) +cSSH2_AGENTC_REQUEST_IDENTITIES = byte_chr(11) +SSH2_AGENT_IDENTITIES_ANSWER = 12 +cSSH2_AGENTC_SIGN_REQUEST = byte_chr(13) +SSH2_AGENT_SIGN_RESPONSE = 14 + class AgentSSH(object): """ @@ -68,12 +71,12 @@ class AgentSSH(object): def _connect(self, conn): self._conn = conn - ptype, result = self._send_message(chr(SSH2_AGENTC_REQUEST_IDENTITIES)) + ptype, result = self._send_message(cSSH2_AGENTC_REQUEST_IDENTITIES) if ptype != SSH2_AGENT_IDENTITIES_ANSWER: raise SSHException('could not get keys from ssh-agent') keys = [] for i in range(result.get_int()): - keys.append(AgentKey(self, result.get_string())) + keys.append(AgentKey(self, result.get_binary())) result.get_string() self._keys = tuple(keys) @@ -83,7 +86,7 @@ class AgentSSH(object): self._keys = () def _send_message(self, msg): - msg = str(msg) + msg = asbytes(msg) self._conn.send(struct.pack('>I', len(msg)) + msg) l = self._read_all(4) msg = Message(self._read_all(struct.unpack('>I', l)[0])) @@ -360,21 +363,24 @@ class AgentKey(PKey): def __init__(self, agent, blob): self.agent = agent self.blob = blob - self.name = Message(blob).get_string() + self.name = Message(blob).get_text() + + def asbytes(self): + return self.blob def __str__(self): - return self.blob + return self.asbytes() def get_name(self): return self.name def sign_ssh_data(self, rng, data): msg = Message() - msg.add_byte(chr(SSH2_AGENTC_SIGN_REQUEST)) + msg.add_byte(cSSH2_AGENTC_SIGN_REQUEST) msg.add_string(self.blob) msg.add_string(data) msg.add_int(0) ptype, result = self.agent._send_message(msg) if ptype != SSH2_AGENT_SIGN_RESPONSE: raise SSHException('key cannot be used for signing') - return result.get_string() + return result.get_binary() diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index de0b205..2a65355 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -119,13 +119,13 @@ class AuthHandler (object): def _request_auth(self): m = Message() - m.add_byte(chr(MSG_SERVICE_REQUEST)) + m.add_byte(cMSG_SERVICE_REQUEST) m.add_string('ssh-userauth') self.transport._send_message(m) def _disconnect_service_not_available(self): m = Message() - m.add_byte(chr(MSG_DISCONNECT)) + m.add_byte(cMSG_DISCONNECT) m.add_int(DISCONNECT_SERVICE_NOT_AVAILABLE) m.add_string('Service not available') m.add_string('en') @@ -134,7 +134,7 @@ class AuthHandler (object): def _disconnect_no_more_auth(self): m = Message() - m.add_byte(chr(MSG_DISCONNECT)) + m.add_byte(cMSG_DISCONNECT) m.add_int(DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE) m.add_string('No more auth methods available') m.add_string('en') @@ -144,14 +144,14 @@ class AuthHandler (object): def _get_session_blob(self, key, service, username): m = Message() m.add_string(self.transport.session_id) - m.add_byte(chr(MSG_USERAUTH_REQUEST)) + m.add_byte(cMSG_USERAUTH_REQUEST) m.add_string(username) m.add_string(service) m.add_string('publickey') m.add_boolean(1) m.add_string(key.get_name()) - m.add_string(str(key)) - return str(m) + m.add_string(key) + return m.asbytes() def wait_for_response(self, event): while True: @@ -175,11 +175,11 @@ class AuthHandler (object): return [] def _parse_service_request(self, m): - service = m.get_string() + service = m.get_text() if self.transport.server_mode and (service == 'ssh-userauth'): # accepted m = Message() - m.add_byte(chr(MSG_SERVICE_ACCEPT)) + m.add_byte(cMSG_SERVICE_ACCEPT) m.add_string(service) self.transport._send_message(m) return @@ -187,27 +187,25 @@ class AuthHandler (object): self._disconnect_service_not_available() def _parse_service_accept(self, m): - service = m.get_string() + service = m.get_text() if service == 'ssh-userauth': self.transport._log(DEBUG, 'userauth is OK') m = Message() - m.add_byte(chr(MSG_USERAUTH_REQUEST)) + m.add_byte(cMSG_USERAUTH_REQUEST) m.add_string(self.username) m.add_string('ssh-connection') m.add_string(self.auth_method) if self.auth_method == 'password': m.add_boolean(False) - password = self.password - if isinstance(password, unicode): - password = password.encode('UTF-8') + password = bytestring(self.password) m.add_string(password) elif self.auth_method == 'publickey': m.add_boolean(True) m.add_string(self.private_key.get_name()) - m.add_string(str(self.private_key)) + m.add_string(self.private_key) blob = self._get_session_blob(self.private_key, 'ssh-connection', self.username) sig = self.private_key.sign_ssh_data(self.transport.rng, blob) - m.add_string(str(sig)) + m.add_string(sig) elif self.auth_method == 'keyboard-interactive': m.add_string('') m.add_string(self.submethods) @@ -224,11 +222,11 @@ class AuthHandler (object): m = Message() if result == AUTH_SUCCESSFUL: self.transport._log(INFO, 'Auth granted (%s).' % method) - m.add_byte(chr(MSG_USERAUTH_SUCCESS)) + m.add_byte(cMSG_USERAUTH_SUCCESS) self.authenticated = True else: self.transport._log(INFO, 'Auth rejected (%s).' % method) - m.add_byte(chr(MSG_USERAUTH_FAILURE)) + m.add_byte(cMSG_USERAUTH_FAILURE) m.add_string(self.transport.server_object.get_allowed_auths(username)) if result == AUTH_PARTIALLY_SUCCESSFUL: m.add_boolean(1) @@ -244,7 +242,7 @@ class AuthHandler (object): def _interactive_query(self, q): # make interactive query instead of response m = Message() - m.add_byte(chr(MSG_USERAUTH_INFO_REQUEST)) + m.add_byte(cMSG_USERAUTH_INFO_REQUEST) m.add_string(q.name) m.add_string(q.instructions) m.add_string('') @@ -258,7 +256,7 @@ class AuthHandler (object): if not self.transport.server_mode: # er, uh... what? m = Message() - m.add_byte(chr(MSG_USERAUTH_FAILURE)) + m.add_byte(cMSG_USERAUTH_FAILURE) m.add_string('none') m.add_boolean(0) self.transport._send_message(m) @@ -266,9 +264,9 @@ class AuthHandler (object): if self.authenticated: # ignore return - username = m.get_string() - service = m.get_string() - method = m.get_string() + username = m.get_text() + service = m.get_text() + method = m.get_text() self.transport._log(DEBUG, 'Auth request (type=%s) service=%s, username=%s' % (method, service, username)) if service != 'ssh-connection': self._disconnect_service_not_available() @@ -283,7 +281,7 @@ class AuthHandler (object): result = self.transport.server_object.check_auth_none(username) elif method == 'password': changereq = m.get_boolean() - password = m.get_string() + password = m.get_binary() try: password = password.decode('UTF-8') except UnicodeError: @@ -294,7 +292,7 @@ class AuthHandler (object): # always treated as failure, since we don't support changing passwords, but collect # the list of valid auth types from the callback anyway self.transport._log(DEBUG, 'Auth request to change passwords (rejected)') - newpassword = m.get_string() + newpassword = m.get_binary() try: newpassword = newpassword.decode('UTF-8', 'replace') except UnicodeError: @@ -304,8 +302,8 @@ class AuthHandler (object): result = self.transport.server_object.check_auth_password(username, password) elif method == 'publickey': sig_attached = m.get_boolean() - keytype = m.get_string() - keyblob = m.get_string() + keytype = m.get_text() + keyblob = m.get_binary() try: key = self.transport._key_info[keytype](Message(keyblob)) except SSHException: @@ -326,12 +324,12 @@ class AuthHandler (object): # client wants to know if this key is acceptable, before it # signs anything... send special "ok" message m = Message() - m.add_byte(chr(MSG_USERAUTH_PK_OK)) + m.add_byte(cMSG_USERAUTH_PK_OK) m.add_string(keytype) m.add_string(keyblob) self.transport._send_message(m) return - sig = Message(m.get_string()) + sig = Message(m.get_binary()) blob = self._get_session_blob(key, service, username) if not key.verify_ssh_sig(blob, sig): self.transport._log(INFO, 'Auth rejected: invalid signature') @@ -383,17 +381,17 @@ class AuthHandler (object): def _parse_userauth_info_request(self, m): if self.auth_method != 'keyboard-interactive': raise SSHException('Illegal info request from server') - title = m.get_string() - instructions = m.get_string() - m.get_string() # lang + title = m.get_text() + instructions = m.get_text() + m.get_binary() # lang prompts = m.get_int() prompt_list = [] for i in range(prompts): - prompt_list.append((m.get_string(), m.get_boolean())) + prompt_list.append((m.get_text(), m.get_boolean())) response_list = self.interactive_handler(title, instructions, prompt_list) m = Message() - m.add_byte(chr(MSG_USERAUTH_INFO_RESPONSE)) + m.add_byte(cMSG_USERAUTH_INFO_RESPONSE) m.add_int(len(response_list)) for r in response_list: m.add_string(r) @@ -405,14 +403,14 @@ class AuthHandler (object): n = m.get_int() responses = [] for i in range(n): - responses.append(m.get_string()) + responses.append(m.get_text()) result = self.transport.server_object.check_auth_interactive_response(responses) if isinstance(type(result), InteractiveQuery): # make interactive query instead of response self._interactive_query(result) return self._send_auth_result(self.auth_username, 'keyboard-interactive', result) - + _handler_table = { MSG_SERVICE_REQUEST: _parse_service_request, diff --git a/paramiko/ber.py b/paramiko/ber.py index f3b4b37..45372fc 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -30,13 +30,16 @@ class BER(object): Robey's tiny little attempt at a BER decoder. """ - def __init__(self, content=''): - self.content = content + def __init__(self, content=bytes()): + self.content = b(content) self.idx = 0 - def __str__(self): + def asbytes(self): return self.content + def __str__(self): + return self.asbytes() + def __repr__(self): return 'BER(\'' + repr(self.content) + '\')' @@ -126,5 +129,5 @@ class BER(object): b = BER() for item in data: b.encode(item) - return str(b) + return b.asbytes() encode_sequence = staticmethod(encode_sequence) diff --git a/paramiko/channel.py b/paramiko/channel.py index d351768..422986d 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -148,7 +148,7 @@ class Channel (object): if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('pty-req') m.add_boolean(True) @@ -181,7 +181,7 @@ class Channel (object): if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('shell') m.add_boolean(1) @@ -208,7 +208,7 @@ class Channel (object): if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('exec') m.add_boolean(True) @@ -235,7 +235,7 @@ class Channel (object): if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('subsystem') m.add_boolean(True) @@ -264,7 +264,7 @@ class Channel (object): if self.closed or self.eof_received or self.eof_sent or not self.active: raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('window-change') m.add_boolean(False) @@ -319,7 +319,7 @@ class Channel (object): # in many cases, the channel will not still be open here. # that's fine. m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('exit-status') m.add_boolean(False) @@ -375,7 +375,7 @@ class Channel (object): auth_cookie = binascii.hexlify(self.transport.rng.read(16)) m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('x11-req') m.add_boolean(True) @@ -406,7 +406,7 @@ class Channel (object): raise SSHException('Channel is not open') m = Message() - m.add_byte(chr(MSG_CHANNEL_REQUEST)) + m.add_byte(cMSG_CHANNEL_REQUEST) m.add_int(self.remote_chanid) m.add_string('auth-agent-req@openssh.com') m.add_boolean(False) @@ -491,7 +491,7 @@ class Channel (object): self._feed(data) return old - + ### socket API @@ -622,7 +622,7 @@ class Channel (object): # no need to hold the channel lock when sending this if ack > 0: m = Message() - m.add_byte(chr(MSG_CHANNEL_WINDOW_ADJUST)) + m.add_byte(cMSG_CHANNEL_WINDOW_ADJUST) m.add_int(self.remote_chanid) m.add_int(ack) self.transport._send_user_message(m) @@ -672,7 +672,7 @@ class Channel (object): # no need to hold the channel lock when sending this if ack > 0: m = Message() - m.add_byte(chr(MSG_CHANNEL_WINDOW_ADJUST)) + m.add_byte(cMSG_CHANNEL_WINDOW_ADJUST) m.add_int(self.remote_chanid) m.add_int(ack) self.transport._send_user_message(m) @@ -724,7 +724,7 @@ class Channel (object): # eof or similar return 0 m = Message() - m.add_byte(chr(MSG_CHANNEL_DATA)) + m.add_byte(cMSG_CHANNEL_DATA) m.add_int(self.remote_chanid) m.add_string(s[:size]) finally: @@ -761,7 +761,7 @@ class Channel (object): # eof or similar return 0 m = Message() - m.add_byte(chr(MSG_CHANNEL_EXTENDED_DATA)) + m.add_byte(cMSG_CHANNEL_EXTENDED_DATA) m.add_int(self.remote_chanid) m.add_int(1) m.add_string(s[:size]) @@ -973,12 +973,12 @@ class Channel (object): # passed from _feed_extended s = m else: - s = m.get_string() + s = m.get_binary() self.in_buffer.feed(s) def _feed_extended(self, m): code = m.get_int() - s = m.get_string() + s = m.get_text() if code != 1: self._log(ERROR, 'unknown extended_data type %d; discarding' % code) return @@ -999,7 +999,7 @@ class Channel (object): self.lock.release() def _handle_request(self, m): - key = m.get_string() + key = m.get_text() want_reply = m.get_boolean() server = self.transport.server_object ok = False @@ -1035,13 +1035,13 @@ class Channel (object): else: ok = server.check_channel_env_request(self, name, value) elif key == 'exec': - cmd = m.get_string() + cmd = m.get_text() if server is None: ok = False else: ok = server.check_channel_exec_request(self, cmd) elif key == 'subsystem': - name = m.get_string() + name = m.get_text() if server is None: ok = False else: @@ -1058,8 +1058,8 @@ class Channel (object): pixelheight) elif key == 'x11-req': single_connection = m.get_boolean() - auth_proto = m.get_string() - auth_cookie = m.get_string() + auth_proto = m.get_text() + auth_cookie = m.get_text() screen_number = m.get_int() if server is None: ok = False @@ -1077,9 +1077,9 @@ class Channel (object): if want_reply: m = Message() if ok: - m.add_byte(chr(MSG_CHANNEL_SUCCESS)) + m.add_byte(cMSG_CHANNEL_SUCCESS) else: - m.add_byte(chr(MSG_CHANNEL_FAILURE)) + m.add_byte(cMSG_CHANNEL_FAILURE) m.add_int(self.remote_chanid) self.transport._send_user_message(m) @@ -1145,7 +1145,7 @@ class Channel (object): if self.eof_sent: return None m = Message() - m.add_byte(chr(MSG_CHANNEL_EOF)) + m.add_byte(cMSG_CHANNEL_EOF) m.add_int(self.remote_chanid) self.eof_sent = True self._log(DEBUG, 'EOF sent (%s)', self._name) @@ -1157,7 +1157,7 @@ class Channel (object): return None, None m1 = self._send_eof() m2 = Message() - m2.add_byte(chr(MSG_CHANNEL_CLOSE)) + m2.add_byte(cMSG_CHANNEL_CLOSE) m2.add_int(self.remote_chanid) self._set_closed() # can't unlink from the Transport yet -- the remote side may still diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index ff74488..715f9f6 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -56,7 +56,7 @@ class DSSKey (PKey): else: if msg is None: raise SSHException('Key object may not be empty') - if msg.get_string() != 'ssh-dss': + if msg.get_text() != 'ssh-dss': raise SSHException('Invalid key') self.p = msg.get_mpint() self.q = msg.get_mpint() @@ -64,14 +64,17 @@ class DSSKey (PKey): self.y = msg.get_mpint() self.size = util.bit_length(self.p) - def __str__(self): + def asbytes(self): m = Message() m.add_string('ssh-dss') m.add_mpint(self.p) m.add_mpint(self.q) m.add_mpint(self.g) m.add_mpint(self.y) - return str(m) + return m.asbytes() + + def __str__(self): + return self.asbytes() def __hash__(self): h = hash(self.get_name()) @@ -114,14 +117,14 @@ class DSSKey (PKey): return m def verify_ssh_sig(self, data, msg): - if len(str(msg)) == 40: + if len(msg.asbytes()) == 40: # spies.com bug: signature has no header - sig = str(msg) + sig = msg.asbytes() else: - kind = msg.get_string() + kind = msg.get_text() if kind != 'ssh-dss': return 0 - sig = msg.get_string() + sig = msg.get_binary() # pull out (r, s) which are NOT encoded as mpints sigR = util.inflate_long(sig[:20], 1) @@ -140,7 +143,7 @@ class DSSKey (PKey): b.encode(keylist) except BERException: raise SSHException('Unable to create ber encoding of key') - return str(b) + return b.asbytes() def write_private_key_file(self, filename, password=None): self._write_private_key_file('DSA', filename, self._encode_key(), password) diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index ac840ab..c3d2a73 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -56,30 +56,33 @@ class ECDSAKey (PKey): else: if msg is None: raise SSHException('Key object may not be empty') - if msg.get_string() != 'ecdsa-sha2-nistp256': + if msg.get_text() != 'ecdsa-sha2-nistp256': raise SSHException('Invalid key') - curvename = msg.get_string() + curvename = msg.get_text() if curvename != 'nistp256': raise SSHException("Can't handle curve of type %s" % curvename) - pointinfo = msg.get_string() - if pointinfo[0] != "\x04": - raise SSHException('Point compression is being used: %s'% + pointinfo = msg.get_binary() + if pointinfo[0] != four_byte: + raise SSHException('Point compression is being used: %s' % binascii.hexlify(pointinfo)) self.verifying_key = VerifyingKey.from_string(pointinfo[1:], - curve=curves.NIST256p) + curve=curves.NIST256p) self.size = 256 - def __str__(self): + def asbytes(self): key = self.verifying_key m = Message() m.add_string('ecdsa-sha2-nistp256') m.add_string('nistp256') - point_str = "\x04" + key.to_string() + point_str = four_byte + key.to_string() m.add_string(point_str) - return str(m) + return m.asbytes() + + def __str__(self): + return self.asbytes() def __hash__(self): h = hash(self.get_name()) @@ -106,9 +109,9 @@ class ECDSAKey (PKey): return m def verify_ssh_sig(self, data, msg): - if msg.get_string() != 'ecdsa-sha2-nistp256': + if msg.get_text() != 'ecdsa-sha2-nistp256': return False - sig = msg.get_string() + sig = msg.get_binary() # verify the signature by SHA'ing the data and encrypting it # using the public key. @@ -161,7 +164,7 @@ class ECDSAKey (PKey): s, padding = der.remove_sequence(data) if padding: if padding not in self.ALLOWED_PADDINGS: - raise ValueError, "weird padding: %s" % (binascii.hexlify(empty)) + raise ValueError("weird padding: %s" % (binascii.hexlify(empty))) data = data[:-len(padding)] key = SigningKey.from_der(data) self.signing_key = key @@ -172,7 +175,7 @@ class ECDSAKey (PKey): msg = Message() msg.add_mpint(r) msg.add_mpint(s) - return str(msg) + return msg.asbytes() def _sigdecode(self, sig, order): msg = Message(sig) diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index f548263..17ee808 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -289,7 +289,7 @@ class HostKeys (MutableMapping): host_key = k.get(key.get_name(), None) if host_key is None: return False - return str(host_key) == str(key) + return host_key.asbytes() == key.asbytes() def clear(self): """ diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index 669bd34..d2ef15c 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -33,6 +33,8 @@ from paramiko.ssh_exception import SSHException _MSG_KEXDH_GEX_REQUEST_OLD, _MSG_KEXDH_GEX_GROUP, _MSG_KEXDH_GEX_INIT, \ _MSG_KEXDH_GEX_REPLY, _MSG_KEXDH_GEX_REQUEST = range(30, 35) +c_MSG_KEXDH_GEX_REQUEST_OLD, c_MSG_KEXDH_GEX_GROUP, c_MSG_KEXDH_GEX_INIT, \ + c_MSG_KEXDH_GEX_REPLY, c_MSG_KEXDH_GEX_REQUEST = [byte_chr(c) for c in range(30, 35)] class KexGex (object): @@ -62,11 +64,11 @@ class KexGex (object): m = Message() if _test_old_style: # only used for unit tests: we shouldn't ever send this - m.add_byte(chr(_MSG_KEXDH_GEX_REQUEST_OLD)) + m.add_byte(c_MSG_KEXDH_GEX_REQUEST_OLD) m.add_int(self.preferred_bits) self.old_style = True else: - m.add_byte(chr(_MSG_KEXDH_GEX_REQUEST)) + m.add_byte(c_MSG_KEXDH_GEX_REQUEST) m.add_int(self.min_bits) m.add_int(self.preferred_bits) m.add_int(self.max_bits) @@ -135,7 +137,7 @@ class KexGex (object): self.transport._log(DEBUG, 'Picking p (%d <= %d <= %d bits)' % (minbits, preferredbits, maxbits)) self.g, self.p = pack.get_modulus(minbits, preferredbits, maxbits) m = Message() - m.add_byte(chr(_MSG_KEXDH_GEX_GROUP)) + m.add_byte(c_MSG_KEXDH_GEX_GROUP) m.add_mpint(self.p) m.add_mpint(self.g) self.transport._send_message(m) @@ -156,7 +158,7 @@ class KexGex (object): self.transport._log(DEBUG, 'Picking p (~ %d bits)' % (self.preferred_bits,)) self.g, self.p = pack.get_modulus(self.min_bits, self.preferred_bits, self.max_bits) m = Message() - m.add_byte(chr(_MSG_KEXDH_GEX_GROUP)) + m.add_byte(c_MSG_KEXDH_GEX_GROUP) m.add_mpint(self.p) m.add_mpint(self.g) self.transport._send_message(m) @@ -175,7 +177,7 @@ class KexGex (object): # now compute e = g^x mod p self.e = pow(self.g, self.x, self.p) m = Message() - m.add_byte(chr(_MSG_KEXDH_GEX_INIT)) + m.add_byte(c_MSG_KEXDH_GEX_INIT) m.add_mpint(self.e) self.transport._send_message(m) self.transport._expect_packet(_MSG_KEXDH_GEX_REPLY) @@ -187,7 +189,7 @@ class KexGex (object): self._generate_x() self.f = pow(self.g, self.x, self.p) K = pow(self.e, self.x, self.p) - key = str(self.transport.get_server_key()) + key = self.transport.get_server_key().asbytes() # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, @@ -203,16 +205,16 @@ class KexGex (object): hm.add_mpint(self.e) hm.add_mpint(self.f) hm.add_mpint(K) - H = SHA.new(str(hm)).digest() + H = SHA.new(hm.asbytes()).digest() self.transport._set_K_H(K, H) # sign it sig = self.transport.get_server_key().sign_ssh_data(self.transport.rng, H) # send reply m = Message() - m.add_byte(chr(_MSG_KEXDH_GEX_REPLY)) + m.add_byte(c_MSG_KEXDH_GEX_REPLY) m.add_string(key) m.add_mpint(self.f) - m.add_string(str(sig)) + m.add_string(sig) self.transport._send_message(m) self.transport._activate_outbound() @@ -238,6 +240,6 @@ class KexGex (object): hm.add_mpint(self.e) hm.add_mpint(self.f) hm.add_mpint(K) - self.transport._set_K_H(K, SHA.new(str(hm)).digest()) + self.transport._set_K_H(K, SHA.new(hm.asbytes()).digest()) self.transport._verify_key(host_key, sig) self.transport._activate_outbound() diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index 6e89b6d..83fb87d 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -56,7 +56,7 @@ class KexGroup1(object): # compute e = g^x mod p (where g=2), and send it self.e = pow(G, self.x, P) m = Message() - m.add_byte(chr(_MSG_KEXDH_INIT)) + m.add_byte(c_MSG_KEXDH_INIT) m.add_mpint(self.e) self.transport._send_message(m) self.transport._expect_packet(_MSG_KEXDH_REPLY) @@ -67,7 +67,7 @@ class KexGroup1(object): elif not self.transport.server_mode and (ptype == _MSG_KEXDH_REPLY): return self._parse_kexdh_reply(m) raise SSHException('KexGroup1 asked to handle packet type %d' % ptype) - + ### internals... @@ -92,7 +92,7 @@ class KexGroup1(object): self.f = m.get_mpint() if (self.f < 1) or (self.f > P - 1): raise SSHException('Server kex "f" is out of range') - sig = m.get_string() + sig = m.get_binary() K = pow(self.f, self.x, P) # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() @@ -102,7 +102,7 @@ class KexGroup1(object): hm.add_mpint(self.e) hm.add_mpint(self.f) hm.add_mpint(K) - self.transport._set_K_H(K, SHA.new(str(hm)).digest()) + self.transport._set_K_H(K, SHA.new(hm.asbytes()).digest()) self.transport._verify_key(host_key, sig) self.transport._activate_outbound() @@ -112,7 +112,7 @@ class KexGroup1(object): if (self.e < 1) or (self.e > P - 1): raise SSHException('Client kex "e" is out of range') K = pow(self.e, self.x, P) - key = str(self.transport.get_server_key()) + key = self.transport.get_server_key().asbytes() # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, @@ -121,15 +121,15 @@ class KexGroup1(object): hm.add_mpint(self.e) hm.add_mpint(self.f) hm.add_mpint(K) - H = SHA.new(str(hm)).digest() + H = SHA.new(hm.asbytes()).digest() self.transport._set_K_H(K, H) # sign it sig = self.transport.get_server_key().sign_ssh_data(self.transport.rng, H) # send reply m = Message() - m.add_byte(chr(_MSG_KEXDH_REPLY)) + m.add_byte(c_MSG_KEXDH_REPLY) m.add_string(key) m.add_mpint(self.f) - m.add_string(str(sig)) + m.add_string(sig) self.transport._send_message(m) self.transport._activate_outbound() diff --git a/paramiko/message.py b/paramiko/message.py index d579a16..8e81e69 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -37,6 +37,8 @@ class Message (object): paramiko doesn't support yet. """ + big_int = long(0xff000000) + def __init__(self, content=None): """ Create a new SSH2 Message. @@ -46,18 +48,12 @@ class Message (object): @type content: string """ if content != None: - self.packet = cStringIO.StringIO(content) + self.packet = BytesIO(content) else: - self.packet = cStringIO.StringIO() + self.packet = BytesIO() def __str__(self): - """ - Return the byte stream content of this Message, as a string. - - @return: the contents of this Message. - @rtype: string - """ - return self.packet.getvalue() + return self.asbytes() def __repr__(self): """ @@ -67,6 +63,15 @@ class Message (object): """ return 'paramiko.Message(' + repr(self.packet.getvalue()) + ')' + def asbytes(self): + """ + Return the byte stream content of this Message, as bytes. + + @return: the contents of this Message. + @rtype: bytes + """ + return self.packet.getvalue() + def rewind(self): """ Rewind the message to the beginning as if no items had been parsed @@ -112,7 +117,7 @@ class Message (object): b = self.packet.read(n) max_pad_size = 1<<20 # Limit padding to 1 MB if len(b) < n and n < max_pad_size: - return b + '\x00' * (n - len(b)) + return b + zero_byte * (n - len(b)) return b def get_byte(self): @@ -134,12 +139,25 @@ class Message (object): @rtype: bool """ b = self.get_bytes(1) - return b != '\x00' + return b != zero_byte def get_int(self): """ Fetch an int from the stream. + @return: a 32-bit unsigned integer. + @rtype: int + """ + byte = self.get_bytes(1) + if byte == max_byte: + return util.inflate_long(self.get_binary()) + byte += self.get_bytes(3) + return struct.unpack('>I', byte)[0] + + def get_size(self): + """ + Fetch an int from the stream. + @return: a 32-bit unsigned integer. @rtype: int """ @@ -152,7 +170,7 @@ class Message (object): @return: a 64-bit unsigned integer. @rtype: long """ - return struct.unpack('>Q', self.get_bytes(8))[0] + return self.get_int() def get_mpint(self): """ @@ -161,7 +179,7 @@ class Message (object): @return: an arbitrary-length integer. @rtype: long """ - return util.inflate_long(self.get_string()) + return util.inflate_long(self.get_binary()) def get_string(self): """ @@ -172,7 +190,30 @@ class Message (object): @return: a string. @rtype: string """ - return self.get_bytes(self.get_int()) + return self.get_bytes(self.get_size()) + + def get_text(self): + """ + Fetch a string from the stream. This could be a byte string and may + contain unprintable characters. (It's not unheard of for a string to + contain another byte-stream Message.) + + @return: a string. + @rtype: string + """ + return u(self.get_bytes(self.get_size())) + #return self.get_bytes(self.get_size()) + + def get_binary(self): + """ + Fetch a string from the stream. This could be a byte string and may + contain unprintable characters. (It's not unheard of for a string to + contain another byte-stream Message.) + + @return: a string. + @rtype: string + """ + return self.get_bytes(self.get_size()) def get_list(self): """ @@ -182,7 +223,7 @@ class Message (object): @return: a list of strings. @rtype: list of strings """ - return self.get_string().split(',') + return self.get_text().split(',') def add_bytes(self, b): """ @@ -212,12 +253,12 @@ class Message (object): @type b: bool """ if b: - self.add_byte('\x01') + self.packet.write(one_byte) else: - self.add_byte('\x00') + self.packet.write(zero_byte) return self - def add_int(self, n): + def add_size(self, n): """ Add an integer to the stream. @@ -227,6 +268,20 @@ class Message (object): self.packet.write(struct.pack('>I', n)) return self + def add_int(self, n): + """ + Add an integer to the stream. + + @param n: integer to add + @type n: int + """ + if n >= Message.big_int: + self.packet.write(max_byte) + self.add_string(util.deflate_long(n)) + else: + self.packet.write(struct.pack('>I', n)) + return self + def add_int64(self, n): """ Add a 64-bit int to the stream. @@ -234,8 +289,7 @@ class Message (object): @param n: long int to add @type n: long """ - self.packet.write(struct.pack('>Q', n)) - return self + return self.add_int(n) def add_mpint(self, z): """ @@ -255,7 +309,8 @@ class Message (object): @param s: string to add @type s: str """ - self.add_int(len(s)) + s = asbytes(s) + self.add_size(len(s)) self.packet.write(s) return self @@ -272,21 +327,14 @@ class Message (object): return self def _add(self, i): - if type(i) is str: - return self.add_string(i) - elif type(i) is int: - return self.add_int(i) - elif type(i) is long: - if i > 0xffffffffL: - return self.add_mpint(i) - else: - return self.add_int(i) - elif type(i) is bool: + if type(i) is bool: return self.add_boolean(i) + elif isinstance(i, integer_types): + return self.add_int(i) elif type(i) is list: return self.add_list(i) else: - raise Exception('Unknown type') + return self.add_string(i) def add(self, *seq): """ diff --git a/paramiko/pkey.py b/paramiko/pkey.py index d61809f..9c59dad 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -63,7 +63,7 @@ class PKey (object): """ pass - def __str__(self): + def asbytes(self): """ Return a string of an SSH L{Message} made up of the public part(s) of this key. This string is suitable for passing to L{__init__} to @@ -72,7 +72,10 @@ class PKey (object): @return: string representation of an SSH key message. @rtype: str """ - return '' + return bytes() + + def __str__(self): + return self.asbytes() def __cmp__(self, other): """ @@ -90,7 +93,10 @@ class PKey (object): ho = hash(other) if hs != ho: return cmp(hs, ho) - return cmp(str(self), str(other)) + return cmp(self.asbytes(), other.asbytes()) + + def __eq__(self, other): + return hash(self) == hash(other) def get_name(self): """ @@ -131,7 +137,7 @@ class PKey (object): format. @rtype: str """ - return MD5.new(str(self)).digest() + return MD5.new(self.asbytes()).digest() def get_base64(self): """ @@ -142,7 +148,7 @@ class PKey (object): @return: a base64 string containing the public part of the key. @rtype: str """ - return base64.encodestring(str(self)).replace('\n', '') + return base64.encodestring(self.asbytes()).replace('\n', '') def sign_ssh_data(self, rng, data): """ @@ -156,7 +162,7 @@ class PKey (object): @return: an SSH signature message. @rtype: L{Message} """ - return '' + return bytes() def verify_ssh_sig(self, data, msg): """ @@ -303,7 +309,7 @@ class PKey (object): end += 1 # if we trudged to the end of the file, just try to cope. try: - data = base64.decodestring(''.join(lines[start:end])) + data = base64.decodestring(b(''.join(lines[start:end]))) except base64.binascii.Error: raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) if 'proc-type' not in headers: @@ -356,7 +362,7 @@ class PKey (object): f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag) if password is not None: # since we only support one cipher here, use it - cipher_name = self._CIPHER_TABLE.keys()[0] + cipher_name = list(self._CIPHER_TABLE.keys())[0] cipher = self._CIPHER_TABLE[cipher_name]['cipher'] keysize = self._CIPHER_TABLE[cipher_name]['keysize'] blocksize = self._CIPHER_TABLE[cipher_name]['blocksize'] diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index c7500f8..cb7b30b 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -57,18 +57,21 @@ class RSAKey (PKey): else: if msg is None: raise SSHException('Key object may not be empty') - if msg.get_string() != 'ssh-rsa': + if msg.get_text() != 'ssh-rsa': raise SSHException('Invalid key') self.e = msg.get_mpint() self.n = msg.get_mpint() self.size = util.bit_length(self.n) - def __str__(self): + def asbytes(self): m = Message() m.add_string('ssh-rsa') m.add_mpint(self.e) m.add_mpint(self.n) - return str(m) + return m.asbytes() + + def __str__(self): + return self.asbytes() def __hash__(self): h = hash(self.get_name()) @@ -95,9 +98,9 @@ class RSAKey (PKey): return m def verify_ssh_sig(self, data, msg): - if msg.get_string() != 'ssh-rsa': + if msg.get_text() != 'ssh-rsa': return False - sig = util.inflate_long(msg.get_string(), True) + sig = util.inflate_long(msg.get_binary(), True) # verify the signature by SHA'ing the data and encrypting it using the # public key. some wackiness ensues where we "pkcs1imify" the 20-byte # hash into a string as long as the RSA key. @@ -116,7 +119,7 @@ class RSAKey (PKey): b.encode(keylist) except BERException: raise SSHException('Unable to create ber encoding of key') - return str(b) + return b.asbytes() def write_private_key_file(self, filename, password=None): self._write_private_key_file('RSA', filename, self._encode_key(), password) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index a97c300..0d1287a 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -86,7 +86,7 @@ CMD_NAMES = { CMD_ATTRS: 'attrs', CMD_EXTENDED: 'extended', CMD_EXTENDED_REPLY: 'extended_reply' - } +} class SFTPError (Exception): @@ -125,7 +125,7 @@ class BaseSFTP (object): msg = Message() msg.add_int(_VERSION) msg.add(*extension_pairs) - self._send_packet(CMD_VERSION, str(msg)) + self._send_packet(CMD_VERSION, msg) return version def _log(self, level, msg, *args): @@ -167,6 +167,7 @@ class BaseSFTP (object): def _send_packet(self, t, packet): #self._log(DEBUG2, 'write: %s (len=%d)' % (CMD_NAMES.get(t, '0x%02x' % t), len(packet))) out = struct.pack('>I', len(packet) + 1) + chr(t) + packet + packet = asbytes(packet) if self.ultra_debug: self._log(DEBUG, util.format_binary(out, 'OUT: ')) self._write_all(out) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 954cfa0..0e3d6c8 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -173,7 +173,7 @@ class SFTPClient (BaseSFTP): t, msg = self._request(CMD_OPENDIR, path) if t != CMD_HANDLE: raise SFTPError('Expected handle') - handle = msg.get_string() + handle = msg.get_binary() filelist = [] while True: try: @@ -245,7 +245,7 @@ class SFTPClient (BaseSFTP): t, msg = self._request(CMD_OPEN, filename, imode, attrblock) if t != CMD_HANDLE: raise SFTPError('Expected handle') - handle = msg.get_string() + handle = msg.get_binary() self._log(DEBUG, 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle))) return SFTPFile(self, handle, mode, bufsize) @@ -369,8 +369,7 @@ class SFTPClient (BaseSFTP): """ dest = self._adjust_cwd(dest) self._log(DEBUG, 'symlink(%r, %r)' % (source, dest)) - if type(source) is unicode: - source = source.encode('utf-8') + source = bytestring(source) self._request(CMD_SYMLINK, source, dest) def chmod(self, path, mode): @@ -610,7 +609,7 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ file_size = os.stat(localpath).st_size - fl = file(localpath, 'rb') + fl = open(localpath, 'rb') try: return self.putfo(fl, remotepath, os.stat(localpath).st_size, callback, confirm) finally: @@ -636,7 +635,7 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ - fr = self.file(remotepath, 'rb') + fr = self.open(remotepath, 'rb') file_size = self.stat(remotepath).st_size fr.prefetch() try: @@ -671,7 +670,7 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ file_size = self.stat(remotepath).st_size - fl = file(localpath, 'wb') + fl = open(localpath, 'wb') try: size = self.getfo(remotepath, fl, callback) finally: @@ -707,7 +706,7 @@ class SFTPClient (BaseSFTP): raise Exception('unknown type for %r type %r' % (item, type(item))) num = self.request_number self._expecting[num] = fileobj - self._send_packet(t, str(msg)) + self._send_packet(t, msg) self.request_number += 1 finally: self._lock.release() @@ -752,7 +751,7 @@ class SFTPClient (BaseSFTP): Raises EOFError or IOError on error status; otherwise does nothing. """ code = msg.get_int() - text = msg.get_string() + text = msg.get_text() if code == SFTP_OK: return elif code == SFTP_EOF: @@ -770,8 +769,7 @@ class SFTPClient (BaseSFTP): Return an adjusted path if we're emulating a "current working directory" for the server. """ - if type(path) is unicode: - path = path.encode('utf-8') + path = bytestring(path) if self._cwd is None: return path if (len(path) > 0) and (path[0] == '/'): diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index c0bc898..8cc7233 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -348,8 +348,8 @@ class SFTPFile (BufferedFile): """ t, msg = self.sftp._request(CMD_EXTENDED, 'check-file', self.handle, hash_algorithm, long(offset), long(length), block_size) - ext = msg.get_string() - alg = msg.get_string() + ext = msg.get_text() + alg = msg.get_text() data = msg.get_remainder() return data diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index ec34008..96c1f04 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -189,7 +189,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): item._pack(msg) else: raise Exception('unknown type for ' + repr(item) + ' type ' + repr(type(item))) - self._send_packet(t, str(msg)) + self._send_packet(t, msg) def _send_handle_response(self, request_number, handle, folder=False): if not issubclass(type(handle), SFTPHandle): @@ -236,14 +236,14 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_string(attr.filename) msg.add_string(str(attr)) attr._pack(msg) - self._send_packet(CMD_NAME, str(msg)) + self._send_packet(CMD_NAME, msg) def _check_file(self, request_number, msg): # this extension actually comes from v6 protocol, but since it's an # extension, i feel like we can reasonably support it backported. # it's very useful for verifying uploaded files or checking for # rsync-like differences between local and remote files. - handle = msg.get_string() + handle = msg.get_binary() alg_list = msg.get_list() start = msg.get_int64() length = msg.get_int64() @@ -295,7 +295,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_string('check-file') msg.add_string(algname) msg.add_bytes(sum_out) - self._send_packet(CMD_EXTENDED_REPLY, str(msg)) + self._send_packet(CMD_EXTENDED_REPLY, msg) def _convert_pflags(self, pflags): "convert SFTP-style open() flags to python's os.open() flags" @@ -318,12 +318,12 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def _process(self, t, request_number, msg): self._log(DEBUG, 'Request: %s' % CMD_NAMES[t]) if t == CMD_OPEN: - path = msg.get_string() + path = msg.get_text() flags = self._convert_pflags(msg.get_int()) attr = SFTPAttributes._from_msg(msg) self._send_handle_response(request_number, self.server.open(path, flags, attr)) elif t == CMD_CLOSE: - handle = msg.get_string() + handle = msg.get_binary() if handle in self.folder_table: del self.folder_table[handle] self._send_status(request_number, SFTP_OK) @@ -335,7 +335,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): return self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') elif t == CMD_READ: - handle = msg.get_string() + handle = msg.get_binary() offset = msg.get_int64() length = msg.get_int() if handle not in self.file_table: @@ -350,54 +350,54 @@ class SFTPServer (BaseSFTP, SubsystemHandler): else: self._send_status(request_number, data) elif t == CMD_WRITE: - handle = msg.get_string() + handle = msg.get_binary() offset = msg.get_int64() - data = msg.get_string() + data = msg.get_binary() if handle not in self.file_table: self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return self._send_status(request_number, self.file_table[handle].write(offset, data)) elif t == CMD_REMOVE: - path = msg.get_string() + path = msg.get_text() self._send_status(request_number, self.server.remove(path)) elif t == CMD_RENAME: - oldpath = msg.get_string() - newpath = msg.get_string() + oldpath = msg.get_text() + newpath = msg.get_text() self._send_status(request_number, self.server.rename(oldpath, newpath)) elif t == CMD_MKDIR: - path = msg.get_string() + path = msg.get_text() attr = SFTPAttributes._from_msg(msg) self._send_status(request_number, self.server.mkdir(path, attr)) elif t == CMD_RMDIR: - path = msg.get_string() + path = msg.get_text() self._send_status(request_number, self.server.rmdir(path)) elif t == CMD_OPENDIR: - path = msg.get_string() + path = msg.get_text() self._open_folder(request_number, path) return elif t == CMD_READDIR: - handle = msg.get_string() + handle = msg.get_binary() if handle not in self.folder_table: self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return folder = self.folder_table[handle] self._read_folder(request_number, folder) elif t == CMD_STAT: - path = msg.get_string() + path = msg.get_text() resp = self.server.stat(path) if issubclass(type(resp), SFTPAttributes): self._response(request_number, CMD_ATTRS, resp) else: self._send_status(request_number, resp) elif t == CMD_LSTAT: - path = msg.get_string() + path = msg.get_text() resp = self.server.lstat(path) if issubclass(type(resp), SFTPAttributes): self._response(request_number, CMD_ATTRS, resp) else: self._send_status(request_number, resp) elif t == CMD_FSTAT: - handle = msg.get_string() + handle = msg.get_binary() if handle not in self.file_table: self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return @@ -407,18 +407,18 @@ class SFTPServer (BaseSFTP, SubsystemHandler): else: self._send_status(request_number, resp) elif t == CMD_SETSTAT: - path = msg.get_string() + path = msg.get_text() attr = SFTPAttributes._from_msg(msg) self._send_status(request_number, self.server.chattr(path, attr)) elif t == CMD_FSETSTAT: - handle = msg.get_string() + handle = msg.get_binary() attr = SFTPAttributes._from_msg(msg) if handle not in self.file_table: self._response(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return self._send_status(request_number, self.file_table[handle].chattr(attr)) elif t == CMD_READLINK: - path = msg.get_string() + path = msg.get_text() resp = self.server.readlink(path) if type(resp) is str: self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) @@ -426,15 +426,15 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self._send_status(request_number, resp) elif t == CMD_SYMLINK: # the sftp 2 draft is incorrect here! path always follows target_path - target_path = msg.get_string() - path = msg.get_string() + target_path = msg.get_text() + path = msg.get_text() self._send_status(request_number, self.server.symlink(target_path, path)) elif t == CMD_REALPATH: - path = msg.get_string() + path = msg.get_text() rpath = self.server.canonicalize(path) self._response(request_number, CMD_NAME, 1, rpath, '', SFTPAttributes()) elif t == CMD_EXTENDED: - tag = msg.get_string() + tag = msg.get_text() if tag == 'check-file': self._check_file(request_number, msg) else: diff --git a/paramiko/transport.py b/paramiko/transport.py index c8a693e..c9e81fa 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -112,8 +112,8 @@ class SecurityOptions (object): x = tuple(x) if type(x) is not tuple: raise TypeError('expected tuple or list') - possible = getattr(self._transport, orig).keys() - forbidden = filter(lambda n: n not in possible, x) + possible = list(getattr(self._transport, orig).keys()) + forbidden = [n for n in x if n not in possible] if len(forbidden) > 0: raise ValueError('unknown cipher') setattr(self._transport, name, x) @@ -276,7 +276,7 @@ class Transport (threading.Thread): @param sock: a socket or socket-like object to create the session over. @type sock: socket """ - if isinstance(sock, (str, unicode)): + if isinstance(sock, string_types): # convert "host:port" into (host, port) hl = sock.split(':', 1) if len(hl) == 1: @@ -735,7 +735,7 @@ class Transport (threading.Thread): try: chanid = self._next_channel() m = Message() - m.add_byte(chr(MSG_CHANNEL_OPEN)) + m.add_byte(cMSG_CHANNEL_OPEN) m.add_string(kind) m.add_int(chanid) m.add_int(self.window_size) @@ -861,7 +861,7 @@ class Transport (threading.Thread): @type byte_count: int """ m = Message() - m.add_byte(chr(MSG_IGNORE)) + m.add_byte(cMSG_IGNORE) if byte_count is None: byte_count = (byte_ord(rng.read(1)) % 32) + 10 m.add_bytes(rng.read(byte_count)) @@ -927,7 +927,7 @@ class Transport (threading.Thread): if wait: self.completion_event = threading.Event() m = Message() - m.add_byte(chr(MSG_GLOBAL_REQUEST)) + m.add_byte(cMSG_GLOBAL_REQUEST) m.add_string(kind) m.add_boolean(wait) if data is not None: @@ -1013,10 +1013,10 @@ class Transport (threading.Thread): # check host key if we were given one if (hostkey is not None): key = self.get_remote_server_key() - if (key.get_name() != hostkey.get_name()) or (str(key) != str(hostkey)): + if (key.get_name() != hostkey.get_name()) or (key.asbytes() != hostkey.asbytes()): self._log(DEBUG, 'Bad host key from server') - self._log(DEBUG, 'Expected: %s: %s' % (hostkey.get_name(), repr(str(hostkey)))) - self._log(DEBUG, 'Got : %s: %s' % (key.get_name(), repr(str(key)))) + self._log(DEBUG, 'Expected: %s: %s' % (hostkey.get_name(), repr(hostkey.asbytes()))) + self._log(DEBUG, 'Got : %s: %s' % (key.get_name(), repr(key.asbytes()))) raise SSHException('Bad host key from server') self._log(DEBUG, 'Host key verified (%s)' % hostkey.get_name()) @@ -1476,15 +1476,15 @@ class Transport (threading.Thread): m = Message() m.add_mpint(self.K) m.add_bytes(self.H) - m.add_byte(id) + m.add_byte(b(id)) m.add_bytes(self.session_id) - out = sofar = SHA.new(str(m)).digest() + out = sofar = SHA.new(m.asbytes()).digest() while len(out) < nbytes: m = Message() m.add_mpint(self.K) m.add_bytes(self.H) m.add_bytes(sofar) - digest = SHA.new(str(m)).digest() + digest = SHA.new(m.asbytes()).digest() out += digest sofar += digest return out[:nbytes] @@ -1606,7 +1606,7 @@ class Transport (threading.Thread): else: self._log(WARNING, 'Oops, unhandled type %d' % ptype) msg = Message() - msg.add_byte(chr(MSG_UNIMPLEMENTED)) + msg.add_byte(cMSG_UNIMPLEMENTED) msg.add_int(m.seqno) self._send_message(msg) except SSHException: @@ -1633,7 +1633,7 @@ class Transport (threading.Thread): self._log(ERROR, util.tb_strings()) self.saved_exception = e _active_threads.remove(self) - for chan in self._channels.values(): + for chan in list(self._channels.values()): chan._unlink() if self.active: self.active = False @@ -1642,7 +1642,7 @@ class Transport (threading.Thread): self.completion_event.set() if self.auth_handler is not None: self.auth_handler.abort() - for event in self.channel_events.values(): + for event in list(self.channel_events.values()): event.set() try: self.lock.acquire() @@ -1731,13 +1731,13 @@ class Transport (threading.Thread): pkex = list(self.get_security_options().kex) pkex.remove('diffie-hellman-group-exchange-sha1') self.get_security_options().kex = pkex - available_server_keys = filter(self.server_key_dict.keys().__contains__, - self._preferred_keys) + available_server_keys = list(filter(list(self.server_key_dict.keys()).__contains__, + self._preferred_keys)) else: available_server_keys = self._preferred_keys m = Message() - m.add_byte(chr(MSG_KEXINIT)) + m.add_byte(cMSG_KEXINIT) m.add_bytes(rng.read(16)) m.add_list(self._preferred_kex) m.add_list(available_server_keys) @@ -1752,7 +1752,7 @@ class Transport (threading.Thread): m.add_boolean(False) m.add_int(0) # save a copy for later (needed to compute a hash) - self.local_kex_init = str(m) + self.local_kex_init = m.asbytes() self._send_message(m) def _parse_kex_init(self, m): @@ -1850,7 +1850,7 @@ class Transport (threading.Thread): # actually some extra bytes (one NUL byte in openssh's case) added to # the end of the packet but not parsed. turns out we need to throw # away those bytes because they aren't part of the hash. - self.remote_kex_init = chr(MSG_KEXINIT) + m.get_so_far() + self.remote_kex_init = cMSG_KEXINIT + m.get_so_far() def _activate_inbound(self): "switch on newly negotiated encryption parameters for inbound traffic" @@ -1879,7 +1879,7 @@ class Transport (threading.Thread): def _activate_outbound(self): "switch on newly negotiated encryption parameters for outbound traffic" m = Message() - m.add_byte(chr(MSG_NEWKEYS)) + m.add_byte(cMSG_NEWKEYS) self._send_message(m) block_size = self._cipher_info[self.local_cipher]['block-size'] if self.server_mode: @@ -1952,20 +1952,20 @@ class Transport (threading.Thread): self._log(INFO, 'Disconnect (code %d): %s' % (code, desc)) def _parse_global_request(self, m): - kind = m.get_string() + kind = m.get_text() self._log(DEBUG, 'Received global request "%s"' % kind) want_reply = m.get_boolean() if not self.server_mode: self._log(DEBUG, 'Rejecting "%s" global request from server.' % kind) ok = False elif kind == 'tcpip-forward': - address = m.get_string() + address = m.get_text() port = m.get_int() ok = self.server_object.check_port_forward_request(address, port) if ok != False: ok = (ok,) elif kind == 'cancel-tcpip-forward': - address = m.get_string() + address = m.get_text() port = m.get_int() self.server_object.cancel_port_forward_request(address, port) ok = True @@ -1978,10 +1978,10 @@ class Transport (threading.Thread): if want_reply: msg = Message() if ok: - msg.add_byte(chr(MSG_REQUEST_SUCCESS)) + msg.add_byte(cMSG_REQUEST_SUCCESS) msg.add(*extra) else: - msg.add_byte(chr(MSG_REQUEST_FAILURE)) + msg.add_byte(cMSG_REQUEST_FAILURE) self._send_message(msg) def _parse_request_success(self, m): @@ -2019,8 +2019,8 @@ class Transport (threading.Thread): def _parse_channel_open_failure(self, m): chanid = m.get_int() reason = m.get_int() - reason_str = m.get_string() - lang = m.get_string() + reason_str = m.get_text() + lang = m.get_text() reason_text = CONNECTION_FAILED_CODE.get(reason, '(unknown code)') self._log(INFO, 'Secsh channel %d open FAILED: %s: %s' % (chanid, reason_str, reason_text)) self.lock.acquire() @@ -2036,7 +2036,7 @@ class Transport (threading.Thread): return def _parse_channel_open(self, m): - kind = m.get_string() + kind = m.get_text() chanid = m.get_int() initial_window_size = m.get_int() max_packet_size = m.get_int() @@ -2049,7 +2049,7 @@ class Transport (threading.Thread): finally: self.lock.release() elif (kind == 'x11') and (self._x11_handler is not None): - origin_addr = m.get_string() + origin_addr = m.get_text() origin_port = m.get_int() self._log(DEBUG, 'Incoming x11 connection from %s:%d' % (origin_addr, origin_port)) self.lock.acquire() @@ -2058,9 +2058,9 @@ class Transport (threading.Thread): finally: self.lock.release() elif (kind == 'forwarded-tcpip') and (self._tcp_handler is not None): - server_addr = m.get_string() + server_addr = m.get_text() server_port = m.get_int() - origin_addr = m.get_string() + origin_addr = m.get_text() origin_port = m.get_int() self._log(DEBUG, 'Incoming tcp forwarded connection from %s:%d' % (origin_addr, origin_port)) self.lock.acquire() @@ -2080,9 +2080,9 @@ class Transport (threading.Thread): self.lock.release() if kind == 'direct-tcpip': # handle direct-tcpip requests comming from the client - dest_addr = m.get_string() + dest_addr = m.get_text() dest_port = m.get_int() - origin_addr = m.get_string() + origin_addr = m.get_text() origin_port = m.get_int() reason = self.server_object.check_channel_direct_tcpip_request( my_chanid, (origin_addr, origin_port), @@ -2094,7 +2094,7 @@ class Transport (threading.Thread): reject = True if reject: msg = Message() - msg.add_byte(chr(MSG_CHANNEL_OPEN_FAILURE)) + msg.add_byte(cMSG_CHANNEL_OPEN_FAILURE) msg.add_int(chanid) msg.add_int(reason) msg.add_string('') @@ -2113,7 +2113,7 @@ class Transport (threading.Thread): finally: self.lock.release() m = Message() - m.add_byte(chr(MSG_CHANNEL_OPEN_SUCCESS)) + m.add_byte(cMSG_CHANNEL_OPEN_SUCCESS) m.add_int(chanid) m.add_int(my_chanid) m.add_int(self.window_size) diff --git a/tests/test_auth.py b/tests/test_auth.py index ea9b2bd..a7c9e61 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -111,8 +111,8 @@ class AuthTest (unittest.TestCase): self.sockc.close() def start_server(self): - self.public_host_key = RSAKey(data=str(host_key)) host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + self.public_host_key = RSAKey(data=host_key.asbytes()) self.ts.add_server_key(host_key) self.event = threading.Event() self.server = NullServer() diff --git a/tests/test_client.py b/tests/test_client.py index 91aac33..0959ac9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -86,8 +86,8 @@ class SSHClientTest (unittest.TestCase): """ verify that the SSHClient stuff works too. """ - public_host_key = paramiko.RSAKey(data=str(host_key)) host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) @@ -119,8 +119,8 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient works with a DSA key. """ - public_host_key = paramiko.RSAKey(data=str(host_key)) host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) @@ -152,8 +152,8 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient accepts and tries multiple key files. """ - public_host_key = paramiko.RSAKey(data=str(host_key)) host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = paramiko.SSHClient() self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) @@ -169,8 +169,8 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient's AutoAddPolicy works. """ - public_host_key = paramiko.RSAKey(data=str(host_key)) host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -190,8 +190,8 @@ class SSHClientTest (unittest.TestCase): verify that when an SSHClient is collected, its transport (and the transport's packetizer) is closed. """ - public_host_key = paramiko.RSAKey(data=str(host_key)) host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index 44070cb..fd873e2 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -65,8 +65,8 @@ class HostKeysTest (unittest.TestCase): def test_1_load(self): hostdict = paramiko.HostKeys('hostfile.temp') self.assertEquals(2, len(hostdict)) - self.assertEquals(1, len(hostdict.values()[0])) - self.assertEquals(1, len(hostdict.values()[1])) + self.assertEquals(1, len(list(hostdict.values())[0])) + self.assertEquals(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) @@ -75,7 +75,7 @@ class HostKeysTest (unittest.TestCase): hh = '|1|BMsIC6cUIP2zBuXR3t2LRcJYjzM=|hpkJMysjTk/+zzUUzxQEa2ieq6c=' key = paramiko.RSAKey(data=base64.decodestring(keyblob)) hostdict.add(hh, 'ssh-rsa', key) - self.assertEquals(3, len(hostdict)) + self.assertEquals(3, len(list(hostdict))) x = hostdict['foo.example.com'] fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp) @@ -85,8 +85,8 @@ class HostKeysTest (unittest.TestCase): hostdict = paramiko.HostKeys('hostfile.temp') self.assert_('secure.example.com' in hostdict) self.assert_('not.example.com' not in hostdict) - self.assert_(hostdict.has_key('secure.example.com')) - self.assert_(not hostdict.has_key('not.example.com')) + self.assert_('secure.example.com' in hostdict) + self.assert_('not.example.com' not in hostdict) x = hostdict.get('secure.example.com', None) self.assert_(x is not None) fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() @@ -108,9 +108,9 @@ class HostKeysTest (unittest.TestCase): hostdict['fake.example.com']['ssh-rsa'] = key self.assertEquals(3, len(hostdict)) - self.assertEquals(2, len(hostdict.values()[0])) - self.assertEquals(1, len(hostdict.values()[1])) - self.assertEquals(1, len(hostdict.values()[2])) + self.assertEquals(2, len(list(hostdict.values())[0])) + self.assertEquals(1, len(list(hostdict.values())[1])) + self.assertEquals(1, len(list(hostdict.values())[2])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp) fp = hexlify(hostdict['secure.example.com']['ssh-dss'].get_fingerprint()).upper() diff --git a/tests/test_kex.py b/tests/test_kex.py index be8d7f0..21986fc 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -37,6 +37,8 @@ class FakeRng (object): class FakeKey (object): def __str__(self): return 'fake-key' + def asbytes(self): + return b('fake-key') def sign_ssh_data(self, rng, H): return 'fake-sig' @@ -90,7 +92,7 @@ class KexTest (unittest.TestCase): kex = KexGroup1(transport) kex.start_kex() x = '1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_group1._MSG_KEXDH_REPLY,), transport._expect) # fake "reply" @@ -121,7 +123,7 @@ class KexTest (unittest.TestCase): x = '1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEquals(self.K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assert_(transport._activated) def test_3_gex_client(self): @@ -130,7 +132,7 @@ class KexTest (unittest.TestCase): kex = KexGex(transport) kex.start_kex() x = '22000004000000080000002000' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) msg = Message() @@ -139,7 +141,7 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) x = '20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) msg = Message() @@ -160,7 +162,7 @@ class KexTest (unittest.TestCase): kex = KexGex(transport) kex.start_kex(_test_old_style=True) x = '1E00000800' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) msg = Message() @@ -169,7 +171,7 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) x = '20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) msg = Message() @@ -198,19 +200,19 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, msg) x = '1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) msg = Message() msg.add_mpint(12345) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) - K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581L + K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 H = 'CE754197C21BF3452863B4F44D0B3951F12516EF' x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEquals(K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assert_(transport._activated) def test_6_gex_server_with_old_client(self): @@ -225,17 +227,17 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD, msg) x = '1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) msg = Message() msg.add_mpint(12345) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) - K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581L + K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 H = 'B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B' x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEquals(K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(str(transport._message)).upper()) + self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assert_(transport._activated) diff --git a/tests/test_message.py b/tests/test_message.py index d0e604e..2580115 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -27,10 +27,16 @@ from paramiko.common import * class MessageTest (unittest.TestCase): - __a = '\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + ('x' * 1000) - __b = '\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' - __c = '\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' - __d = '\x00\x00\x00\x05\x00\x00\x00\x05\x11\x22\x33\x44\x55\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' + if PY3: + __a = b'\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + (b'x' * 1000) + __b = b'\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' + __c = b'\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' + __d = b'\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' + else: + __a = '\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + ('x' * 1000) + __b = '\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' + __c = '\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' + __d = '\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' def test_1_encode(self): msg = Message() @@ -39,63 +45,65 @@ class MessageTest (unittest.TestCase): msg.add_string('q') msg.add_string('hello') msg.add_string('x' * 1000) - self.assertEquals(str(msg), self.__a) + self.assertEquals(msg.asbytes(), self.__a) msg = Message() msg.add_boolean(True) msg.add_boolean(False) - msg.add_byte('\xf3') - msg.add_bytes('\x00\x3f') + msg.add_byte(byte_chr(0xf3)) + + msg.add_bytes(zero_byte + byte_chr(0x3f)) msg.add_list(['huey', 'dewey', 'louie']) - self.assertEquals(str(msg), self.__b) + self.assertEquals(msg.asbytes(), self.__b) msg = Message() msg.add_int64(5) - msg.add_int64(0xf5e4d3c2b109L) + msg.add_int64(0xf5e4d3c2b109) msg.add_mpint(17) - msg.add_mpint(0xf5e4d3c2b109L) - msg.add_mpint(-0x65e4d3c2b109L) - self.assertEquals(str(msg), self.__c) + msg.add_mpint(0xf5e4d3c2b109) + msg.add_mpint(-0x65e4d3c2b109) + self.assertEquals(msg.asbytes(), self.__c) def test_2_decode(self): msg = Message(self.__a) self.assertEquals(msg.get_int(), 23) self.assertEquals(msg.get_int(), 123789456) - self.assertEquals(msg.get_string(), 'q') - self.assertEquals(msg.get_string(), 'hello') - self.assertEquals(msg.get_string(), 'x' * 1000) + self.assertEquals(msg.get_text(), 'q') + self.assertEquals(msg.get_text(), 'hello') + self.assertEquals(msg.get_text(), 'x' * 1000) msg = Message(self.__b) self.assertEquals(msg.get_boolean(), True) self.assertEquals(msg.get_boolean(), False) - self.assertEquals(msg.get_byte(), '\xf3') - self.assertEquals(msg.get_bytes(2), '\x00\x3f') + self.assertEquals(msg.get_byte(), byte_chr(0xf3)) + self.assertEquals(msg.get_bytes(2), zero_byte + byte_chr(0x3f)) self.assertEquals(msg.get_list(), ['huey', 'dewey', 'louie']) msg = Message(self.__c) self.assertEquals(msg.get_int64(), 5) - self.assertEquals(msg.get_int64(), 0xf5e4d3c2b109L) + self.assertEquals(msg.get_int64(), 0xf5e4d3c2b109) self.assertEquals(msg.get_mpint(), 17) - self.assertEquals(msg.get_mpint(), 0xf5e4d3c2b109L) - self.assertEquals(msg.get_mpint(), -0x65e4d3c2b109L) + self.assertEquals(msg.get_mpint(), 0xf5e4d3c2b109) + self.assertEquals(msg.get_mpint(), -0x65e4d3c2b109) def test_3_add(self): msg = Message() msg.add(5) - msg.add(0x1122334455L) + msg.add(0x1122334455) + msg.add(0xf00000000000000000) msg.add(True) msg.add('cat') msg.add(['a', 'b']) - self.assertEquals(str(msg), self.__d) + self.assertEquals(msg.asbytes(), self.__d) def test_4_misc(self): msg = Message(self.__d) self.assertEquals(msg.get_int(), 5) - self.assertEquals(msg.get_mpint(), 0x1122334455L) - self.assertEquals(msg.get_so_far(), self.__d[:13]) - self.assertEquals(msg.get_remainder(), self.__d[13:]) + self.assertEquals(msg.get_int(), 0x1122334455) + self.assertEquals(msg.get_int(), 0xf00000000000000000) + self.assertEquals(msg.get_so_far(), self.__d[:29]) + self.assertEquals(msg.get_remainder(), self.__d[29:]) msg.rewind() self.assertEquals(msg.get_int(), 5) self.assertEquals(msg.get_so_far(), self.__d[:4]) self.assertEquals(msg.get_remainder(), self.__d[4:]) - diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index c39fc45..b53b741 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -42,7 +42,7 @@ class PacketizerTest (unittest.TestCase): # message has to be at least 16 bytes long, so we'll have at least one # block of data encrypted that contains zero random padding bytes m = Message() - m.add_byte(chr(100)) + m.add_byte(byte_chr(100)) m.add_int(100) m.add_int(1) m.add_int(900) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index fe823a7..589a35d 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -144,7 +144,7 @@ class KeyTest (unittest.TestCase): # verify that the private & public keys compare equal key = RSAKey.from_private_key_file('tests/test_rsa.key') self.assertEquals(key, key) - pub = RSAKey(data=str(key)) + pub = RSAKey(data=key.asbytes()) self.assert_(key.can_sign()) self.assert_(not pub.can_sign()) self.assertEquals(key, pub) @@ -153,7 +153,7 @@ class KeyTest (unittest.TestCase): # verify that the private & public keys compare equal key = DSSKey.from_private_key_file('tests/test_dss.key') self.assertEquals(key, key) - pub = DSSKey(data=str(key)) + pub = DSSKey(data=key.asbytes()) self.assert_(key.can_sign()) self.assert_(not pub.can_sign()) self.assertEquals(key, pub) @@ -164,11 +164,11 @@ class KeyTest (unittest.TestCase): msg = key.sign_ssh_data(rng, 'ice weasels') self.assert_(type(msg) is Message) msg.rewind() - self.assertEquals('ssh-rsa', msg.get_string()) - sig = ''.join([chr(int(x, 16)) for x in SIGNED_RSA.split(':')]) - self.assertEquals(sig, msg.get_string()) + self.assertEquals('ssh-rsa', msg.get_text()) + sig = bytes().join([byte_chr(int(x, 16)) for x in SIGNED_RSA.split(':')]) + self.assertEquals(sig, msg.get_binary()) msg.rewind() - pub = RSAKey(data=str(key)) + pub = RSAKey(data=key.asbytes()) self.assert_(pub.verify_ssh_sig('ice weasels', msg)) def test_9_sign_dss(self): @@ -177,13 +177,13 @@ class KeyTest (unittest.TestCase): msg = key.sign_ssh_data(rng, 'ice weasels') self.assert_(type(msg) is Message) msg.rewind() - self.assertEquals('ssh-dss', msg.get_string()) + self.assertEquals('ssh-dss', msg.get_text()) # can't do the same test as we do for RSA, because DSS signatures # are usually different each time. but we can test verification # anyway so it's ok. - self.assertEquals(40, len(msg.get_string())) + self.assertEquals(40, len(msg.get_binary())) msg.rewind() - pub = DSSKey(data=str(key)) + pub = DSSKey(data=key.asbytes()) self.assert_(pub.verify_ssh_sig('ice weasels', msg)) def test_A_generate_rsa(self): @@ -227,7 +227,7 @@ class KeyTest (unittest.TestCase): # verify that the private & public keys compare equal key = ECDSAKey.from_private_key_file('tests/test_ecdsa.key') self.assertEquals(key, key) - pub = ECDSAKey(data=str(key)) + pub = ECDSAKey(data=key.asbytes()) self.assert_(key.can_sign()) self.assert_(not pub.can_sign()) self.assertEquals(key, pub) @@ -238,12 +238,12 @@ class KeyTest (unittest.TestCase): msg = key.sign_ssh_data(rng, 'ice weasels') self.assert_(type(msg) is Message) msg.rewind() - self.assertEquals('ecdsa-sha2-nistp256', msg.get_string()) + self.assertEquals('ecdsa-sha2-nistp256', msg.get_text()) # ECDSA signatures, like DSS signatures, tend to be different # each time, so we can't compare against a "known correct" # signature. # Even the length of the signature can change. msg.rewind() - pub = ECDSAKey(data=str(key)) + pub = ECDSAKey(data=key.asbytes()) self.assert_(pub.verify_ssh_sig('ice weasels', msg)) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index a421355..a445271 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -645,7 +645,8 @@ class SFTPTest (unittest.TestCase): try: sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de') sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') - except Exception, e: + except Exception: + e = sys.exc_info()[1] self.fail('exception ' + e) sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 9a4ea31..94088f7 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -73,7 +73,7 @@ class BigSFTPTest (unittest.TestCase): # now make sure every file is there, by creating a list of filenmes # and reading them in random order. - numlist = range(numfiles) + numlist = list(range(numfiles)) while len(numlist) > 0: r = numlist[random.randint(0, len(numlist) - 1)] f = sftp.open('%s/file%d.txt' % (FOLDER, r)) diff --git a/tests/test_transport.py b/tests/test_transport.py index d8cd10d..6c62b3e 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -121,8 +121,8 @@ class TransportTest(ParamikoTest): self.sockc.close() def setup_test_server(self, client_options=None, server_options=None): - public_host_key = RSAKey(data=str(host_key)) host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = RSAKey(data=host_key.asbytes()) self.ts.add_server_key(host_key) if client_options is not None: @@ -171,8 +171,8 @@ class TransportTest(ParamikoTest): loopback sockets. this is hardly "simple" but it's simpler than the later tests. :) """ - public_host_key = RSAKey(data=str(host_key)) host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = RSAKey(data=host_key.asbytes()) self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() @@ -196,8 +196,8 @@ class TransportTest(ParamikoTest): """ verify that a long banner doesn't mess up the handshake. """ - public_host_key = RSAKey(data=str(host_key)) host_key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = RSAKey(data=host_key.asbytes()) self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() @@ -708,7 +708,7 @@ class TransportTest(ParamikoTest): # Simulate in-transit MSG_CHANNEL_WINDOW_ADJUST by sending it # before responding to the incoming MSG_KEXINIT. m2 = Message() - m2.add_byte(chr(MSG_CHANNEL_WINDOW_ADJUST)) + m2.add_byte(cMSG_CHANNEL_WINDOW_ADJUST) m2.add_int(chan.remote_chanid) m2.add_int(1) # bytes to add self._send_message(m2) From 2d738fa08b85c5065cc898d5f9e4d36ee753e871 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:09:50 -0700 Subject: [PATCH 14/56] Fix winapi --- paramiko/_winapi.py | 392 ++++++++++++++++++++++---------------------- 1 file changed, 196 insertions(+), 196 deletions(-) diff --git a/paramiko/_winapi.py b/paramiko/_winapi.py index 43d9751..25d1a30 100644 --- a/paramiko/_winapi.py +++ b/paramiko/_winapi.py @@ -18,71 +18,71 @@ except ImportError: # jaraco.windows.error def format_system_message(errno): - """ - Call FormatMessage with a system error number to retrieve - the descriptive error message. - """ - # first some flags used by FormatMessageW - ALLOCATE_BUFFER = 0x100 - ARGUMENT_ARRAY = 0x2000 - FROM_HMODULE = 0x800 - FROM_STRING = 0x400 - FROM_SYSTEM = 0x1000 - IGNORE_INSERTS = 0x200 + """ + Call FormatMessage with a system error number to retrieve + the descriptive error message. + """ + # first some flags used by FormatMessageW + ALLOCATE_BUFFER = 0x100 + ARGUMENT_ARRAY = 0x2000 + FROM_HMODULE = 0x800 + FROM_STRING = 0x400 + FROM_SYSTEM = 0x1000 + IGNORE_INSERTS = 0x200 - # Let FormatMessageW allocate the buffer (we'll free it below) - # Also, let it know we want a system error message. - flags = ALLOCATE_BUFFER | FROM_SYSTEM - source = None - message_id = errno - language_id = 0 - result_buffer = ctypes.wintypes.LPWSTR() - buffer_size = 0 - arguments = None - bytes = ctypes.windll.kernel32.FormatMessageW( - flags, - source, - message_id, - language_id, - ctypes.byref(result_buffer), - buffer_size, - arguments, - ) - # note the following will cause an infinite loop if GetLastError - # repeatedly returns an error that cannot be formatted, although - # this should not happen. - handle_nonzero_success(bytes) - message = result_buffer.value - ctypes.windll.kernel32.LocalFree(result_buffer) - return message + # Let FormatMessageW allocate the buffer (we'll free it below) + # Also, let it know we want a system error message. + flags = ALLOCATE_BUFFER | FROM_SYSTEM + source = None + message_id = errno + language_id = 0 + result_buffer = ctypes.wintypes.LPWSTR() + buffer_size = 0 + arguments = None + format_bytes = ctypes.windll.kernel32.FormatMessageW( + flags, + source, + message_id, + language_id, + ctypes.byref(result_buffer), + buffer_size, + arguments, + ) + # note the following will cause an infinite loop if GetLastError + # repeatedly returns an error that cannot be formatted, although + # this should not happen. + handle_nonzero_success(format_bytes) + message = result_buffer.value + ctypes.windll.kernel32.LocalFree(result_buffer) + return message -class WindowsError(__builtin__.WindowsError): - "more info about errors at http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx" +class WindowsError(builtins.WindowsError): + "more info about errors at http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx" - def __init__(self, value=None): - if value is None: - value = ctypes.windll.kernel32.GetLastError() - strerror = format_system_message(value) - super(WindowsError, self).__init__(value, strerror) + def __init__(self, value=None): + if value is None: + value = ctypes.windll.kernel32.GetLastError() + strerror = format_system_message(value) + super(WindowsError, self).__init__(value, strerror) - @property - def message(self): - return self.strerror + @property + def message(self): + return self.strerror - @property - def code(self): - return self.winerror + @property + def code(self): + return self.winerror - def __str__(self): - return self.message + def __str__(self): + return self.message - def __repr__(self): - return '{self.__class__.__name__}({self.winerror})'.format(**vars()) + def __repr__(self): + return '{self.__class__.__name__}({self.winerror})'.format(**vars()) def handle_nonzero_success(result): - if result == 0: - raise WindowsError() + if result == 0: + raise WindowsError() ##################### @@ -90,12 +90,12 @@ def handle_nonzero_success(result): CreateFileMapping = ctypes.windll.kernel32.CreateFileMappingW CreateFileMapping.argtypes = [ - ctypes.wintypes.HANDLE, - ctypes.c_void_p, - ctypes.wintypes.DWORD, - ctypes.wintypes.DWORD, - ctypes.wintypes.DWORD, - ctypes.wintypes.LPWSTR, + ctypes.wintypes.HANDLE, + ctypes.c_void_p, + ctypes.wintypes.DWORD, + ctypes.wintypes.DWORD, + ctypes.wintypes.DWORD, + ctypes.wintypes.LPWSTR, ] CreateFileMapping.restype = ctypes.wintypes.HANDLE @@ -103,171 +103,171 @@ MapViewOfFile = ctypes.windll.kernel32.MapViewOfFile MapViewOfFile.restype = ctypes.wintypes.HANDLE class MemoryMap(object): - """ - A memory map object which can have security attributes overrideden. - """ - def __init__(self, name, length, security_attributes=None): - self.name = name - self.length = length - self.security_attributes = security_attributes - self.pos = 0 + """ + A memory map object which can have security attributes overrideden. + """ + def __init__(self, name, length, security_attributes=None): + self.name = name + self.length = length + self.security_attributes = security_attributes + self.pos = 0 - def __enter__(self): - p_SA = ( - ctypes.byref(self.security_attributes) - if self.security_attributes else None - ) - INVALID_HANDLE_VALUE = -1 - PAGE_READWRITE = 0x4 - FILE_MAP_WRITE = 0x2 - filemap = ctypes.windll.kernel32.CreateFileMappingW( - INVALID_HANDLE_VALUE, p_SA, PAGE_READWRITE, 0, self.length, - unicode(self.name)) - handle_nonzero_success(filemap) - if filemap == INVALID_HANDLE_VALUE: - raise Exception("Failed to create file mapping") - self.filemap = filemap - self.view = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0) - return self + def __enter__(self): + p_SA = ( + ctypes.byref(self.security_attributes) + if self.security_attributes else None + ) + INVALID_HANDLE_VALUE = -1 + PAGE_READWRITE = 0x4 + FILE_MAP_WRITE = 0x2 + filemap = ctypes.windll.kernel32.CreateFileMappingW( + INVALID_HANDLE_VALUE, p_SA, PAGE_READWRITE, 0, self.length, + u(self.name)) + handle_nonzero_success(filemap) + if filemap == INVALID_HANDLE_VALUE: + raise Exception("Failed to create file mapping") + self.filemap = filemap + self.view = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0) + return self - def seek(self, pos): - self.pos = pos + def seek(self, pos): + self.pos = pos - def write(self, msg): - ctypes.windll.msvcrt.memcpy(self.view + self.pos, msg, len(msg)) - self.pos += len(msg) + def write(self, msg): + ctypes.windll.msvcrt.memcpy(self.view + self.pos, msg, len(msg)) + self.pos += len(msg) - def read(self, n): - """ - Read n bytes from mapped view. - """ - out = ctypes.create_string_buffer(n) - ctypes.windll.msvcrt.memcpy(out, self.view + self.pos, n) - self.pos += n - return out.raw + def read(self, n): + """ + Read n bytes from mapped view. + """ + out = ctypes.create_string_buffer(n) + ctypes.windll.msvcrt.memcpy(out, self.view + self.pos, n) + self.pos += n + return out.raw - def __exit__(self, exc_type, exc_val, tb): - ctypes.windll.kernel32.UnmapViewOfFile(self.view) - ctypes.windll.kernel32.CloseHandle(self.filemap) + def __exit__(self, exc_type, exc_val, tb): + ctypes.windll.kernel32.UnmapViewOfFile(self.view) + ctypes.windll.kernel32.CloseHandle(self.filemap) ######################### # jaraco.windows.security class TokenInformationClass: - TokenUser = 1 + TokenUser = 1 class TOKEN_USER(ctypes.Structure): - num = 1 - _fields_ = [ - ('SID', ctypes.c_void_p), - ('ATTRIBUTES', ctypes.wintypes.DWORD), - ] + num = 1 + _fields_ = [ + ('SID', ctypes.c_void_p), + ('ATTRIBUTES', ctypes.wintypes.DWORD), + ] class SECURITY_DESCRIPTOR(ctypes.Structure): - """ - typedef struct _SECURITY_DESCRIPTOR - { - UCHAR Revision; - UCHAR Sbz1; - SECURITY_DESCRIPTOR_CONTROL Control; - PSID Owner; - PSID Group; - PACL Sacl; - PACL Dacl; - } SECURITY_DESCRIPTOR; - """ - SECURITY_DESCRIPTOR_CONTROL = ctypes.wintypes.USHORT - REVISION = 1 + """ + typedef struct _SECURITY_DESCRIPTOR + { + UCHAR Revision; + UCHAR Sbz1; + SECURITY_DESCRIPTOR_CONTROL Control; + PSID Owner; + PSID Group; + PACL Sacl; + PACL Dacl; + } SECURITY_DESCRIPTOR; + """ + SECURITY_DESCRIPTOR_CONTROL = ctypes.wintypes.USHORT + REVISION = 1 - _fields_ = [ - ('Revision', ctypes.c_ubyte), - ('Sbz1', ctypes.c_ubyte), - ('Control', SECURITY_DESCRIPTOR_CONTROL), - ('Owner', ctypes.c_void_p), - ('Group', ctypes.c_void_p), - ('Sacl', ctypes.c_void_p), - ('Dacl', ctypes.c_void_p), - ] + _fields_ = [ + ('Revision', ctypes.c_ubyte), + ('Sbz1', ctypes.c_ubyte), + ('Control', SECURITY_DESCRIPTOR_CONTROL), + ('Owner', ctypes.c_void_p), + ('Group', ctypes.c_void_p), + ('Sacl', ctypes.c_void_p), + ('Dacl', ctypes.c_void_p), + ] class SECURITY_ATTRIBUTES(ctypes.Structure): - """ - typedef struct _SECURITY_ATTRIBUTES { - DWORD nLength; - LPVOID lpSecurityDescriptor; - BOOL bInheritHandle; - } SECURITY_ATTRIBUTES; - """ - _fields_ = [ - ('nLength', ctypes.wintypes.DWORD), - ('lpSecurityDescriptor', ctypes.c_void_p), - ('bInheritHandle', ctypes.wintypes.BOOL), - ] + """ + typedef struct _SECURITY_ATTRIBUTES { + DWORD nLength; + LPVOID lpSecurityDescriptor; + BOOL bInheritHandle; + } SECURITY_ATTRIBUTES; + """ + _fields_ = [ + ('nLength', ctypes.wintypes.DWORD), + ('lpSecurityDescriptor', ctypes.c_void_p), + ('bInheritHandle', ctypes.wintypes.BOOL), + ] - def __init__(self, *args, **kwargs): - super(SECURITY_ATTRIBUTES, self).__init__(*args, **kwargs) - self.nLength = ctypes.sizeof(SECURITY_ATTRIBUTES) + def __init__(self, *args, **kwargs): + super(SECURITY_ATTRIBUTES, self).__init__(*args, **kwargs) + self.nLength = ctypes.sizeof(SECURITY_ATTRIBUTES) - def _get_descriptor(self): - return self._descriptor - def _set_descriptor(self, descriptor): - self._descriptor = descriptor - self.lpSecurityDescriptor = ctypes.addressof(descriptor) - descriptor = property(_get_descriptor, _set_descriptor) + def _get_descriptor(self): + return self._descriptor + def _set_descriptor(self, descriptor): + self._descriptor = descriptor + self.lpSecurityDescriptor = ctypes.addressof(descriptor) + descriptor = property(_get_descriptor, _set_descriptor) def GetTokenInformation(token, information_class): - """ - Given a token, get the token information for it. - """ - data_size = ctypes.wintypes.DWORD() - ctypes.windll.advapi32.GetTokenInformation(token, information_class.num, - 0, 0, ctypes.byref(data_size)) - data = ctypes.create_string_buffer(data_size.value) - handle_nonzero_success(ctypes.windll.advapi32.GetTokenInformation(token, - information_class.num, - ctypes.byref(data), ctypes.sizeof(data), - ctypes.byref(data_size))) - return ctypes.cast(data, ctypes.POINTER(TOKEN_USER)).contents + """ + Given a token, get the token information for it. + """ + data_size = ctypes.wintypes.DWORD() + ctypes.windll.advapi32.GetTokenInformation(token, information_class.num, + 0, 0, ctypes.byref(data_size)) + data = ctypes.create_string_buffer(data_size.value) + handle_nonzero_success(ctypes.windll.advapi32.GetTokenInformation(token, + information_class.num, + ctypes.byref(data), ctypes.sizeof(data), + ctypes.byref(data_size))) + return ctypes.cast(data, ctypes.POINTER(TOKEN_USER)).contents class TokenAccess: - TOKEN_QUERY = 0x8 + TOKEN_QUERY = 0x8 def OpenProcessToken(proc_handle, access): - result = ctypes.wintypes.HANDLE() - proc_handle = ctypes.wintypes.HANDLE(proc_handle) - handle_nonzero_success(ctypes.windll.advapi32.OpenProcessToken( - proc_handle, access, ctypes.byref(result))) - return result + result = ctypes.wintypes.HANDLE() + proc_handle = ctypes.wintypes.HANDLE(proc_handle) + handle_nonzero_success(ctypes.windll.advapi32.OpenProcessToken( + proc_handle, access, ctypes.byref(result))) + return result def get_current_user(): - """ - Return a TOKEN_USER for the owner of this process. - """ - process = OpenProcessToken( - ctypes.windll.kernel32.GetCurrentProcess(), - TokenAccess.TOKEN_QUERY, - ) - return GetTokenInformation(process, TOKEN_USER) + """ + Return a TOKEN_USER for the owner of this process. + """ + process = OpenProcessToken( + ctypes.windll.kernel32.GetCurrentProcess(), + TokenAccess.TOKEN_QUERY, + ) + return GetTokenInformation(process, TOKEN_USER) def get_security_attributes_for_user(user=None): - """ - Return a SECURITY_ATTRIBUTES structure with the SID set to the - specified user (uses current user if none is specified). - """ - if user is None: - user = get_current_user() + """ + Return a SECURITY_ATTRIBUTES structure with the SID set to the + specified user (uses current user if none is specified). + """ + if user is None: + user = get_current_user() - assert isinstance(user, TOKEN_USER), "user must be TOKEN_USER instance" + assert isinstance(user, TOKEN_USER), "user must be TOKEN_USER instance" - SD = SECURITY_DESCRIPTOR() - SA = SECURITY_ATTRIBUTES() - # by attaching the actual security descriptor, it will be garbage- - # collected with the security attributes - SA.descriptor = SD - SA.bInheritHandle = 1 + SD = SECURITY_DESCRIPTOR() + SA = SECURITY_ATTRIBUTES() + # by attaching the actual security descriptor, it will be garbage- + # collected with the security attributes + SA.descriptor = SD + SA.bInheritHandle = 1 - ctypes.windll.advapi32.InitializeSecurityDescriptor(ctypes.byref(SD), - SECURITY_DESCRIPTOR.REVISION) - ctypes.windll.advapi32.SetSecurityDescriptorOwner(ctypes.byref(SD), - user.SID, 0) - return SA + ctypes.windll.advapi32.InitializeSecurityDescriptor(ctypes.byref(SD), + SECURITY_DESCRIPTOR.REVISION) + ctypes.windll.advapi32.SetSecurityDescriptorOwner(ctypes.byref(SD), + user.SID, 0) + return SA From 0b7d0cf0a23e4f16f8552ae05a66539119e2e920 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:14:52 -0700 Subject: [PATCH 15/56] Convert and detect types properly, use helper constants, use StringIO and range --- paramiko/ber.py | 12 ++++---- paramiko/channel.py | 2 +- paramiko/client.py | 4 +-- paramiko/kex_gex.py | 6 ++-- paramiko/kex_group1.py | 11 ++++---- paramiko/packet.py | 31 +++++++++++---------- paramiko/pipe.py | 4 +-- paramiko/pkey.py | 4 +-- paramiko/primes.py | 4 +-- paramiko/server.py | 2 +- paramiko/sftp.py | 4 +-- paramiko/sftp_attr.py | 8 +++--- paramiko/sftp_client.py | 4 +-- paramiko/sftp_server.py | 4 +-- paramiko/transport.py | 8 +++--- paramiko/util.py | 61 ++++++++++++++++++----------------------- tests/loop.py | 4 +-- tests/stub_sftp.py | 2 +- tests/test_auth.py | 9 ++++-- tests/test_kex.py | 6 ++-- tests/test_pkey.py | 6 ++-- tests/test_sftp.py | 45 ++++++++++++++++++------------ tests/test_sftp_big.py | 32 ++++++++++----------- tests/test_transport.py | 6 ++-- tests/test_util.py | 20 +++++++------- 25 files changed, 154 insertions(+), 145 deletions(-) diff --git a/paramiko/ber.py b/paramiko/ber.py index 45372fc..f4d2acc 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -49,13 +49,13 @@ class BER(object): def decode_next(self): if self.idx >= len(self.content): return None - ident = ord(self.content[self.idx]) + ident = byte_ord(self.content[self.idx]) self.idx += 1 if (ident & 31) == 31: # identifier > 30 ident = 0 while self.idx < len(self.content): - t = ord(self.content[self.idx]) + t = byte_ord(self.content[self.idx]) self.idx += 1 ident = (ident << 7) | (t & 0x7f) if not (t & 0x80): @@ -63,7 +63,7 @@ class BER(object): if self.idx >= len(self.content): return None # now fetch length - size = ord(self.content[self.idx]) + size = byte_ord(self.content[self.idx]) self.idx += 1 if size & 0x80: # more complimicated... @@ -102,12 +102,12 @@ class BER(object): def encode_tlv(self, ident, val): # no need to support ident > 31 here - self.content += chr(ident) + self.content += byte_chr(ident) if len(val) > 0x7f: lenstr = util.deflate_long(len(val)) - self.content += chr(0x80 + len(lenstr)) + lenstr + self.content += byte_chr(0x80 + len(lenstr)) + lenstr else: - self.content += chr(len(val)) + self.content += byte_chr(len(val)) self.content += val def encode(self, x): diff --git a/paramiko/channel.py b/paramiko/channel.py index 422986d..d686c6d 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -969,7 +969,7 @@ class Channel (object): self.transport._send_user_message(m) def _feed(self, m): - if type(m) is str: + if isinstance(m, bytes_type): # passed from _feed_extended s = m else: diff --git a/paramiko/client.py b/paramiko/client.py index 98bb47f..3881f8d 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -335,7 +335,7 @@ class SSHClient (object): if key_filename is None: key_filenames = [] - elif isinstance(key_filename, (str, unicode)): + elif isinstance(key_filename, string_types): key_filenames = [ key_filename ] else: key_filenames = key_filename @@ -383,7 +383,7 @@ class SSHClient (object): return stdin, stdout, stderr def invoke_shell(self, term='vt100', width=80, height=24, width_pixels=0, - height_pixels=0): + height_pixels=0): """ Start an interactive shell session on the SSH server. A new L{Channel} is opened and connected to a pseudo-terminal using the requested diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index d2ef15c..0249453 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -91,20 +91,20 @@ class KexGex (object): ### internals... - + def _generate_x(self): # generate an "x" (1 < x < (p-1)/2). q = (self.p - 1) // 2 qnorm = util.deflate_long(q, 0) - qhbyte = ord(qnorm[0]) + qhbyte = byte_ord(qnorm[0]) byte_count = len(qnorm) qmask = 0xff while not (qhbyte & 0x80): qhbyte <<= 1 qmask >>= 1 while True: - x_bytes = chr(ord(x_bytes[0]) & qmask) + x_bytes[1:] x_bytes = self.transport.rng.read(byte_count) + x_bytes = byte_mask(x_bytes[0], qmask) + x_bytes[1:] x = util.inflate_long(x_bytes, 1) if (x > 1) and (x < q): break diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index 83fb87d..ea452b3 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -30,9 +30,10 @@ from paramiko.ssh_exception import SSHException _MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32) +c_MSG_KEXDH_INIT, c_MSG_KEXDH_REPLY = [byte_chr(c) for c in range(30, 32)] # draft-ietf-secsh-transport-09.txt, page 17 -P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL +P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 @@ -42,9 +43,9 @@ class KexGroup1(object): def __init__(self, transport): self.transport = transport - self.x = 0L - self.e = 0L - self.f = 0L + self.x = long_zero + self.e = long_zero + self.f = long_zero def start_kex(self): self._generate_x() @@ -80,7 +81,7 @@ class KexGroup1(object): # larger than q (but this is a tiny tiny subset of potential x). while 1: x_bytes = self.transport.rng.read(128) - x_bytes = chr(ord(x_bytes[0]) & 0x7f) + x_bytes[1:] + x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] if (x_bytes[:8] != '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF') and \ (x_bytes[:8] != '\x00\x00\x00\x00\x00\x00\x00\x00'): break diff --git a/paramiko/packet.py b/paramiko/packet.py index 3a26b6b..193acf6 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -38,6 +38,7 @@ try: except ImportError: from Crypto.Hash.HMAC import HMAC + def compute_hmac(key, message, digest_class): return HMAC(key, message, digest_class).digest() @@ -66,7 +67,7 @@ class Packetizer (object): self.__dump_packets = False self.__need_rekey = False self.__init_count = 0 - self.__remainder = '' + self.__remainder = bytes() # used for noticing when to re-key: self.__sent_bytes = 0 @@ -90,8 +91,8 @@ class Packetizer (object): self.__mac_key_in = '' self.__compress_engine_out = None self.__compress_engine_in = None - self.__sequence_number_out = 0L - self.__sequence_number_in = 0L + self.__sequence_number_out = long_zero + self.__sequence_number_in = long_zero # lock around outbound writes (packet computation) self.__write_lock = threading.RLock() @@ -196,7 +197,7 @@ class Packetizer (object): @raise EOFError: if the socket was closed before all the bytes could be read """ - out = '' + out = bytes() # handle over-reading from reading the banner line if len(self.__remainder) > 0: out = self.__remainder[:n] @@ -275,22 +276,22 @@ class Packetizer (object): line, so it's okay to attempt large reads. """ buf = self.__remainder - while not '\n' in buf: + while not newline_byte in buf: buf += self._read_timeout(timeout) - n = buf.index('\n') + n = buf.index(newline_byte) self.__remainder = buf[n+1:] buf = buf[:n] - if (len(buf) > 0) and (buf[-1] == '\r'): + if (len(buf) > 0) and (buf[-1] == cr_byte): buf = buf[:-1] - return buf + return u(buf) def send_message(self, data): """ Write a block of data using the current cipher, as an SSH block. """ # encrypt this sucka - data = str(data) - cmd = ord(data[0]) + data = asbytes(data) + cmd = byte_ord(data[0]) if cmd in MSG_NAMES: cmd_name = MSG_NAMES[cmd] else: @@ -312,7 +313,7 @@ class Packetizer (object): if self.__block_engine_out != None: payload = struct.pack('>I', self.__sequence_number_out) + packet out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] - self.__sequence_number_out = (self.__sequence_number_out + 1) & 0xffffffffL + self.__sequence_number_out = (self.__sequence_number_out + 1) & xffffffff self.write_all(out) self.__sent_bytes += len(out) @@ -361,7 +362,7 @@ class Packetizer (object): my_mac = compute_hmac(self.__mac_key_in, mac_payload, self.__mac_engine_in)[:self.__mac_size_in] if my_mac != mac: raise SSHException('Mismatched MAC') - padding = ord(packet[0]) + padding = byte_ord(packet[0]) payload = packet[1:packet_size - padding] if self.__dump_packets: @@ -372,7 +373,7 @@ class Packetizer (object): msg = Message(payload[1:]) msg.seqno = self.__sequence_number_in - self.__sequence_number_in = (self.__sequence_number_in + 1) & 0xffffffffL + self.__sequence_number_in = (self.__sequence_number_in + 1) & xffffffff # check for rekey raw_packet_size = packet_size + self.__mac_size_in + 4 @@ -395,7 +396,7 @@ class Packetizer (object): self.__received_packets_overflow = 0 self._trigger_rekey() - cmd = ord(payload[0]) + cmd = byte_ord(payload[0]) if cmd in MSG_NAMES: cmd_name = MSG_NAMES[cmd] else: @@ -493,7 +494,7 @@ class Packetizer (object): if self.__sdctr_out or self.__block_engine_out is None: # cute trick i caught openssh doing: if we're not encrypting or SDCTR mode (RFC4344), # don't waste random bytes for the padding - packet += (chr(0) * padding) + packet += (zero_byte * padding) else: packet += rng.read(padding) return packet diff --git a/paramiko/pipe.py b/paramiko/pipe.py index e64547b..4c96546 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -64,7 +64,7 @@ class PosixPipe (object): if self._set or self._closed: return self._set = True - os.write(self._wfd, '*') + os.write(self._wfd, b('*')) def set_forever (self): self._forever = True @@ -110,7 +110,7 @@ class WindowsPipe (object): if self._set or self._closed: return self._set = True - self._wsock.send('*') + self._wsock.send(b('*')) def set_forever (self): self._forever = True diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 9c59dad..c88e5c8 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -352,9 +352,9 @@ class PKey (object): @raise IOError: if there was an error writing the file. """ - f = open(filename, 'w', 0600) + f = open(filename, 'w', o600) # grrr... the mode doesn't always take hold - os.chmod(filename, 0600) + os.chmod(filename, o600) self._write_private_key(tag, f, data, password) f.close() diff --git a/paramiko/primes.py b/paramiko/primes.py index 1dd87da..144454a 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -34,7 +34,7 @@ def _generate_prime(bits, rng): # loop catches the case where we increment n into a higher bit-range x = rng.read((bits+7) // 8) if hbyte_mask > 0: - x = chr(ord(x[0]) & hbyte_mask) + x[1:] + x = byte_mask(x[0], hbyte_mask) + x[1:] n = util.inflate_long(x, 1) n |= 1 n |= (1 << (bits - 1)) @@ -59,7 +59,7 @@ def _roll_random(rng, n): while True: x = rng.read(byte_count) if hbyte_mask > 0: - x = chr(ord(x[0]) & hbyte_mask) + x[1:] + x = byte_mask(x[0], hbyte_mask) + x[1:] num = util.inflate_long(x, 1) if num < n: break diff --git a/paramiko/server.py b/paramiko/server.py index 4b6e8b1..f3383ad 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -48,7 +48,7 @@ class InteractiveQuery (object): self.instructions = instructions self.prompts = [] for x in prompts: - if (type(x) is str) or (type(x) is unicode): + if isinstance(x, string_types): self.add_prompt(x) else: self.add_prompt(x[0], x[1]) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 0d1287a..11aa3cb 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -166,8 +166,8 @@ class BaseSFTP (object): def _send_packet(self, t, packet): #self._log(DEBUG2, 'write: %s (len=%d)' % (CMD_NAMES.get(t, '0x%02x' % t), len(packet))) - out = struct.pack('>I', len(packet) + 1) + chr(t) + packet packet = asbytes(packet) + out = struct.pack('>I', len(packet) + 1) + byte_chr(t) + packet if self.ultra_debug: self._log(DEBUG, util.format_binary(out, 'OUT: ')) self._write_all(out) @@ -183,7 +183,7 @@ class BaseSFTP (object): if self.ultra_debug: self._log(DEBUG, util.format_binary(data, 'IN: ')); if size > 0: - t = ord(data[0]) + t = byte_ord(data[0]) #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) return t, data[1:] return 0, '' diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index 84c8392..5a40f4c 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -44,7 +44,7 @@ class SFTPAttributes (object): FLAG_UIDGID = 2 FLAG_PERMISSIONS = 4 FLAG_AMTIME = 8 - FLAG_EXTENDED = 0x80000000L + FLAG_EXTENDED = x80000000 def __init__(self): """ @@ -194,13 +194,13 @@ class SFTPAttributes (object): ks = 's' else: ks = '?' - ks += self._rwx((self.st_mode & 0700) >> 6, self.st_mode & stat.S_ISUID) - ks += self._rwx((self.st_mode & 070) >> 3, self.st_mode & stat.S_ISGID) + ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) + ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date - if (self.st_mtime is None) or (self.st_mtime == 0xffffffffL): + if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 0e3d6c8..7703246 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -285,7 +285,7 @@ class SFTPClient (BaseSFTP): self._log(DEBUG, 'rename(%r, %r)' % (oldpath, newpath)) self._request(CMD_RENAME, oldpath, newpath) - def mkdir(self, path, mode=0777): + def mkdir(self, path, mode=o777): """ Create a folder (directory) named C{path} with numeric mode C{mode}. The default mode is 0777 (octal). On some systems, mode is ignored. @@ -698,7 +698,7 @@ class SFTPClient (BaseSFTP): msg.add_int(item) elif isinstance(item, long): msg.add_int64(item) - elif isinstance(item, str): + elif isinstance(item, string_types): msg.add_string(item) elif isinstance(item, SFTPAttributes): item._pack(msg) diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 96c1f04..63fe5cc 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -183,7 +183,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_int(item) elif type(item) is long: msg.add_int64(item) - elif type(item) is str: + elif isinstance(item, string_types): msg.add_string(item) elif type(item) is SFTPAttributes: item._pack(msg) @@ -420,7 +420,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_READLINK: path = msg.get_text() resp = self.server.readlink(path) - if type(resp) is str: + if isinstance(resp, string_types): self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) else: self._send_status(request_number, resp) diff --git a/paramiko/transport.py b/paramiko/transport.py index c9e81fa..101fe17 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -376,7 +376,7 @@ class Transport (threading.Thread): @rtype: str """ - out = ' 0) and (ord(s[0]) >= 0x80): + if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80): negative = 1 if len(s) % 4: - filler = '\x00' + filler = zero_byte if negative: - filler = '\xff' + filler = max_byte s = filler * (4 - len(s) % 4) + s for i in range(0, len(s), 4): out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] if negative: - out -= (1L << (8 * len(s))) + out -= (long_one << (8 * len(s))) return out +deflate_zero = 0 if PY3 else zero_byte +deflate_ff = 0xff if PY3 else max_byte + def deflate_long(n, add_sign_padding=True): "turns a long-int into a normalized byte string (adapted from Crypto.Util.number)" # after much testing, this algorithm was deemed to be the fastest - s = '' + s = bytes() n = long(n) while (n != 0) and (n != -1): - s = struct.pack('>I', n & 0xffffffffL) + s + s = struct.pack('>I', n & xffffffff) + s n = n >> 32 # strip off leading zeros, FFs for i in enumerate(s): - if (n == 0) and (i[1] != '\000'): + if (n == 0) and (i[1] != deflate_zero): break - if (n == -1) and (i[1] != '\xff'): + if (n == -1) and (i[1] != deflate_ff): break else: # degenerate case, n was either 0 or -1 i = (0,) if n == 0: - s = '\000' + s = zero_byte else: - s = '\xff' + s = max_byte s = s[i[0]:] if add_sign_padding: - if (n == 0) and (ord(s[0]) >= 0x80): - s = '\x00' + s - if (n == -1) and (ord(s[0]) < 0x80): - s = '\xff' + s + if (n == 0) and (byte_ord(s[0]) >= 0x80): + s = zero_byte + s + if (n == -1) and (byte_ord(s[0]) < 0x80): + s = max_byte + s return s -def format_binary_weird(data): - out = '' - for i in enumerate(data): - out += '%02X' % ord(i[1]) - if i[0] % 2: - out += ' ' - if i[0] % 16 == 15: - out += '\n' - return out - def format_binary(data, prefix=''): x = 0 out = [] @@ -113,8 +106,8 @@ def format_binary(data, prefix=''): return [prefix + x for x in out] def format_binary_line(data): - left = ' '.join(['%02X' % ord(c) for c in data]) - right = ''.join([('.%c..' % c)[(ord(c)+63)//95] for c in data]) + left = ' '.join(['%02X' % byte_ord(c) for c in data]) + right = ''.join([('.%c..' % c)[(byte_ord(c)+63)//95] for c in data]) return '%-50s %s' % (left, right) def hexify(s): @@ -126,17 +119,17 @@ def unhexify(s): def safe_string(s): out = '' for c in s: - if (ord(c) >= 32) and (ord(c) <= 127): + if (byte_ord(c) >= 32) and (byte_ord(c) <= 127): out += c else: - out += '%%%02X' % ord(c) + out += '%%%02X' % byte_ord(c) return out # ''.join([['%%%02X' % ord(c), c][(ord(c) >= 32) and (ord(c) <= 127)] for c in s]) def bit_length(n): norm = deflate_long(n, 0) - hbyte = ord(norm[0]) + hbyte = byte_ord(norm[0]) if hbyte == 0: return 1 bitlen = len(norm) * 8 @@ -298,15 +291,15 @@ class Counter (object): """Increament the counter and return the new value""" i = self.blocksize - 1 while i > -1: - c = self.value[i] = chr((ord(self.value[i]) + 1) % 256) - if c != '\x00': + c = self.value[i] = byte_chr((byte_ord(self.value[i]) + 1) % 256) + if c != zero_byte: return self.value.tostring() i -= 1 # counter reset x = deflate_long(self.overflow, add_sign_padding=False) - self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x) + self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x) return self.value.tostring() - def new(cls, nbits, initial_value=1L, overflow=0L): + def new(cls, nbits, initial_value=long_one, overflow=long_zero): return cls(nbits, initial_value=initial_value, overflow=overflow) new = classmethod(new) diff --git a/tests/loop.py b/tests/loop.py index 2f3f5df..cfd7626 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -32,7 +32,7 @@ class LoopSocket (object): """ def __init__(self): - self.__in_buffer = '' + self.__in_buffer = bytes() self.__lock = threading.Lock() self.__cv = threading.Condition(self.__lock) self.__timeout = None @@ -42,7 +42,7 @@ class LoopSocket (object): self.__unlink() try: self.__lock.acquire() - self.__in_buffer = '' + self.__in_buffer = bytes() finally: self.__lock.release() diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index b8bea9b..26ca13b 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -104,7 +104,7 @@ class StubSFTPServer (SFTPServerInterface): else: # os.open() defaults to 0777 which is # an odd default mode for files - fd = os.open(path, flags, 0666) + fd = os.open(path, flags, o666) except OSError: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) diff --git a/tests/test_auth.py b/tests/test_auth.py index a7c9e61..5fd0bf5 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -32,6 +32,11 @@ from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED from tests.loop import LoopSocket from tests.util import test_path +try: + _pwd = u'\u2022' +except Exception: + _pwd = '\u2022' + class NullServer (ServerInterface): paranoid_did_password = False @@ -65,7 +70,7 @@ class NullServer (ServerInterface): if self.paranoid_did_public_key: return AUTH_SUCCESSFUL return AUTH_PARTIALLY_SUCCESSFUL - if (username == 'utf8') and (password == u'\u2022'): + if (username == 'utf8') and (password == _pwd): return AUTH_SUCCESSFUL if (username == 'non-utf8') and (password == '\xff'): return AUTH_SUCCESSFUL @@ -203,7 +208,7 @@ class AuthTest (unittest.TestCase): """ self.start_server() self.tc.connect(hostkey=self.public_host_key) - remain = self.tc.auth_password('utf8', u'\u2022') + remain = self.tc.auth_password('utf8', _pwd) self.assertEquals([], remain) self.verify_finished() diff --git a/tests/test_kex.py b/tests/test_kex.py index 21986fc..c94b777 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -31,7 +31,7 @@ from paramiko.common import * class FakeRng (object): def read(self, n): - return chr(0xcc) * n + return byte_chr(0xcc) * n class FakeKey (object): @@ -44,7 +44,7 @@ class FakeKey (object): class FakeModulusPack (object): - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 def get_modulus(self, min, ask, max): return self.G, self.P @@ -78,7 +78,7 @@ class FakeTransport (object): class KexTest (unittest.TestCase): - K = 14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504L + K = 14730343317708716439807310032871972459448364195094179797249681733965528989482751523943515690110179031004049109375612685505881911274101441415545039654102474376472240501616988799699744135291070488314748284283496055223852115360852283821334858541043710301057312858051901453919067023103730011648890038847384890504 def setUp(self): pass diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 589a35d..c7240db 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -99,7 +99,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_RSA.split()[1], key.get_base64()) self.assertEquals(1024, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(RSA_PRIVATE_OUT, s.getvalue()) s.seek(0) @@ -124,7 +124,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_DSS.split()[1], key.get_base64()) self.assertEquals(1024, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(DSS_PRIVATE_OUT, s.getvalue()) s.seek(0) @@ -207,7 +207,7 @@ class KeyTest (unittest.TestCase): self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) self.assertEquals(256, key.get_bits()) - s = StringIO.StringIO() + s = StringIO() key.write_private_key(s) self.assertEquals(ECDSA_PRIVATE_OUT, s.getvalue()) s.seek(0) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index a445271..c80967d 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -71,7 +71,10 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True - +try: + unicode_folder = u'\u00fcnic\u00f8de' +except SyntaxError: + unicode_folder = '\u00fcnic\u00f8de' def get_sftp(): global sftp @@ -141,7 +144,7 @@ class SFTPTest (unittest.TestCase): def setUp(self): global FOLDER - for i in xrange(1000): + for i in range(1000): FOLDER = FOLDER[:-3] + '%03d' % i try: sftp.mkdir(FOLDER) @@ -236,7 +239,7 @@ class SFTPTest (unittest.TestCase): pass f = sftp.open(FOLDER + '/second.txt', 'r') f.seek(-6, f.SEEK_END) - self.assertEqual(f.read(4), 'tent') + self.assertEqual(u(f.read(4)), 'tent') f.close() finally: try: @@ -301,16 +304,16 @@ class SFTPTest (unittest.TestCase): f.close() stat = sftp.stat(FOLDER + '/special') - sftp.chmod(FOLDER + '/special', (stat.st_mode & ~0777) | 0600) + sftp.chmod(FOLDER + '/special', (stat.st_mode & ~o777) | o600) stat = sftp.stat(FOLDER + '/special') - expected_mode = 0600 + expected_mode = o600 if sys.platform == 'win32': # chmod not really functional on windows - expected_mode = 0666 + expected_mode = o666 if sys.platform == 'cygwin': # even worse. - expected_mode = 0644 - self.assertEqual(stat.st_mode & 0777, expected_mode) + expected_mode = o644 + self.assertEqual(stat.st_mode & o777, expected_mode) self.assertEqual(stat.st_size, 1024) mtime = stat.st_mtime - 3600 @@ -341,17 +344,17 @@ class SFTPTest (unittest.TestCase): f = sftp.open(FOLDER + '/special', 'r+') stat = f.stat() - f.chmod((stat.st_mode & ~0777) | 0600) + f.chmod((stat.st_mode & ~o777) | o600) stat = f.stat() - expected_mode = 0600 + expected_mode = o600 if sys.platform == 'win32': # chmod not really functional on windows - expected_mode = 0666 + expected_mode = o666 if sys.platform == 'cygwin': # even worse. - expected_mode = 0644 - self.assertEqual(stat.st_mode & 0777, expected_mode) + expected_mode = o644 + self.assertEqual(stat.st_mode & o777, expected_mode) self.assertEqual(stat.st_size, 1024) mtime = stat.st_mtime - 3600 @@ -643,7 +646,7 @@ class SFTPTest (unittest.TestCase): f.close() try: - sftp.rename(FOLDER + '/something', FOLDER + u'/\u00fcnic\u00f8de') + sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') except Exception: e = sys.exc_info()[1] @@ -651,16 +654,16 @@ class SFTPTest (unittest.TestCase): sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') def test_L_utf8_chdir(self): - sftp.mkdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.mkdir(FOLDER + '/' + unicode_folder) try: - sftp.chdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.chdir(FOLDER + '/' + unicode_folder) f = sftp.open('something', 'w') f.write('okay') f.close() sftp.unlink('something') finally: sftp.chdir(None) - sftp.rmdir(FOLDER + u'\u00fcnic\u00f8de') + sftp.rmdir(FOLDER + '/' + unicode_folder) def test_M_bad_readv(self): """ @@ -733,10 +736,16 @@ class SFTPTest (unittest.TestCase): Send an empty file and confirm it is sent. """ target = FOLDER + '/empty file.txt' - stream = StringIO.StringIO() + stream = StringIO() try: attrs = sftp.putfo(stream, target) # the returned attributes should not be null self.assertNotEqual(attrs, None) finally: sftp.remove(target) + + +if __name__ == '__main__': + SFTPTest.init_loopback() + from unittest import main + main() diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 94088f7..20bf007 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -46,7 +46,7 @@ class BigSFTPTest (unittest.TestCase): def setUp(self): global FOLDER sftp = get_sftp() - for i in xrange(1000): + for i in range(1000): FOLDER = FOLDER[:-3] + '%03d' % i try: sftp.mkdir(FOLDER) @@ -69,7 +69,7 @@ class BigSFTPTest (unittest.TestCase): f = sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1) f.write('this is file #%d.\n' % i) f.close() - sftp.chmod('%s/file%d.txt' % (FOLDER, i), 0660) + sftp.chmod('%s/file%d.txt' % (FOLDER, i), o660) # now make sure every file is there, by creating a list of filenmes # and reading them in random order. @@ -124,7 +124,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file, with no linefeeds, using pipelining. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) start = time.time() try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') @@ -165,7 +165,7 @@ class BigSFTPTest (unittest.TestCase): def test_4_prefetch_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -181,13 +181,13 @@ class BigSFTPTest (unittest.TestCase): start = time.time() k2blob = kblob + kblob chunk = 793 - for i in xrange(10): + for i in range(10): f = sftp.open('%s/hongry.txt' % FOLDER, 'r') f.prefetch() base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) - offsets = [base_offset + j * chunk for j in xrange(100)] + offsets = [base_offset + j * chunk for j in range(100)] # randomly seek around and read them out - for j in xrange(100): + for j in range(100): offset = offsets[random.randint(0, len(offsets) - 1)] offsets.remove(offset) f.seek(offset) @@ -203,7 +203,7 @@ class BigSFTPTest (unittest.TestCase): def test_5_readv_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -219,21 +219,21 @@ class BigSFTPTest (unittest.TestCase): start = time.time() k2blob = kblob + kblob chunk = 793 - for i in xrange(10): + for i in range(10): f = sftp.open('%s/hongry.txt' % FOLDER, 'r') base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) # make a bunch of offsets and put them in random order - offsets = [base_offset + j * chunk for j in xrange(100)] + offsets = [base_offset + j * chunk for j in range(100)] readv_list = [] - for j in xrange(100): + for j in range(100): o = offsets[random.randint(0, len(offsets) - 1)] offsets.remove(o) readv_list.append((o, chunk)) ret = f.readv(readv_list) - for i in xrange(len(readv_list)): + for i in range(len(readv_list)): offset = readv_list[i][0] n_offset = offset % 1024 - self.assertEqual(ret.next(), k2blob[n_offset:n_offset + chunk]) + self.assertEqual(next(ret), k2blob[n_offset:n_offset + chunk]) f.close() end = time.time() sys.stderr.write('%ds ' % round(end - start)) @@ -279,7 +279,7 @@ class BigSFTPTest (unittest.TestCase): verify that prefetch and readv don't conflict with each other. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -318,7 +318,7 @@ class BigSFTPTest (unittest.TestCase): returned as a single blob. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in xrange(512)]) + kblob = ''.join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -367,7 +367,7 @@ class BigSFTPTest (unittest.TestCase): k32blob = (32 * 1024 * 'x') try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) - for i in xrange(32): + for i in range(32): f.write(k32blob) f.close() diff --git a/tests/test_transport.py b/tests/test_transport.py index 6c62b3e..69fdbbb 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -158,7 +158,7 @@ class TransportTest(ParamikoTest): pass def test_2_compute_key(self): - self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929L + self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929 self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3') self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) @@ -406,7 +406,7 @@ class TransportTest(ParamikoTest): chan.close() # allow a few seconds for the rekeying to complete - for i in xrange(50): + for i in range(50): if self.tc.H != self.tc.session_id: break time.sleep(0.1) @@ -659,7 +659,7 @@ class TransportTest(ParamikoTest): def run(self): try: - for i in xrange(1, 1+self.iterations): + for i in range(1, 1+self.iterations): if self.done_event.isSet(): break self.watchdog_event.set() diff --git a/tests/test_util.py b/tests/test_util.py index 7e656df..858dedb 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -101,7 +101,7 @@ class UtilTest(ParamikoTest): def test_2_parse_config(self): global test_config_file - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals(config._config, [{'host': ['*'], 'config': {}}, {'host': ['*'], 'config': {'identityfile': ['~/.ssh/id_rsa'], 'user': 'robey'}}, @@ -111,7 +111,7 @@ class UtilTest(ParamikoTest): def test_3_host_config(self): global test_config_file - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) for host, values in { @@ -138,7 +138,7 @@ class UtilTest(ParamikoTest): def test_4_generate_key_bytes(self): x = paramiko.util.generate_key_bytes(SHA, 'ABCDEFGH', 'This is my secret passphrase.', 64) - hex = ''.join(['%02x' % ord(c) for c in x]) + hex = ''.join(['%02x' % byte_ord(c) for c in x]) self.assertEquals(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') def test_5_host_keys(self): @@ -172,7 +172,7 @@ Host *.example.com Host * Port 3333 """ - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' self.assertEquals( @@ -216,7 +216,7 @@ Host space-delimited Host equals-delimited ProxyCommand=foo bar=biz baz """ - f = cStringIO.StringIO(conf) + f = StringIO(conf) config = paramiko.util.parse_ssh_config(f) for host in ('space-delimited', 'equals-delimited'): self.assertEquals( @@ -228,7 +228,7 @@ Host equals-delimited """ ProxyCommand should perform interpolation on the value """ - config = paramiko.util.parse_ssh_config(cStringIO.StringIO(""" + config = paramiko.util.parse_ssh_config(StringIO(""" Host specific Port 37 ProxyCommand host %h port %p lol @@ -264,7 +264,7 @@ Host www13.* Host * Port 3333 """ - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' self.assertEquals( @@ -293,7 +293,7 @@ ProxyCommand foo=bar:%h-%p 'foo=bar:proxy-without-equal-divisor-22'} }.items(): - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals( paramiko.util.lookup_ssh_host_config(host, config), @@ -323,7 +323,7 @@ IdentityFile id_dsa22 'identityfile': ['id_dsa0', 'id_dsa1', 'id_dsa22']} }.items(): - f = cStringIO.StringIO(test_config_file) + f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) self.assertEquals( paramiko.util.lookup_ssh_host_config(host, config), @@ -338,5 +338,5 @@ IdentityFile id_dsa22 AddressFamily inet IdentityFile something_%l_using_fqdn """ - config = paramiko.util.parse_ssh_config(cStringIO.StringIO(test_config)) + config = paramiko.util.parse_ssh_config(StringIO(test_config)) assert config.lookup('meh') # will die during lookup() if bug regresses From 7cdbbf4bdc864bfbbab019787857a9a06d14d7e8 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:15:25 -0700 Subject: [PATCH 16/56] Fix input --- demos/demo.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index 3890eda..744b5c3 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -59,13 +59,13 @@ def agent_auth(transport, username): def manual_auth(username, hostname): default_auth = 'p' - auth = raw_input('Auth by (p)assword, (r)sa key, or (d)ss key? [%s] ' % default_auth) + auth = input('Auth by (p)assword, (r)sa key, or (d)ss key? [%s] ' % default_auth) if len(auth) == 0: auth = default_auth if auth == 'r': default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') - path = raw_input('RSA key [%s]: ' % default_path) + path = input('RSA key [%s]: ' % default_path) if len(path) == 0: path = default_path try: @@ -76,7 +76,7 @@ def manual_auth(username, hostname): t.auth_publickey(username, key) elif auth == 'd': default_path = os.path.join(os.environ['HOME'], '.ssh', 'id_dsa') - path = raw_input('DSS key [%s]: ' % default_path) + path = input('DSS key [%s]: ' % default_path) if len(path) == 0: path = default_path try: @@ -99,7 +99,7 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: print('*** Hostname required.') sys.exit(1) @@ -112,8 +112,8 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception, e: - print '*** Connect failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -150,7 +150,7 @@ try: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username From e4e1dc2002d8a0bea1ccb28fa43d5cd5accf5520 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 17:15:40 -0700 Subject: [PATCH 17/56] Fix next --- paramiko/file.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/paramiko/file.py b/paramiko/file.py index 1a8d3ee..aca97c6 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -92,21 +92,38 @@ class BufferedFile (object): self._wbuffer = StringIO() return - def next(self): - """ - Returns the next line from the input, or raises L{StopIteration} when - EOF is hit. Unlike python file objects, it's okay to mix calls to - C{next} and L{readline}. + if PY3: + def __next__(self): + """ + Returns the next line from the input, or raises L{StopIteration} when + EOF is hit. Unlike python file objects, it's okay to mix calls to + C{next} and L{readline}. - @raise StopIteration: when the end of the file is reached. + @raise StopIteration: when the end of the file is reached. - @return: a line read from the file. - @rtype: str - """ - line = self.readline() - if not line: - raise StopIteration - return line + @return: a line read from the file. + @rtype: str + """ + line = self.readline() + if not line: + raise StopIteration + return line + else: + def next(self): + """ + Returns the next line from the input, or raises L{StopIteration} when + EOF is hit. Unlike python file objects, it's okay to mix calls to + C{next} and L{readline}. + + @raise StopIteration: when the end of the file is reached. + + @return: a line read from the file. + @rtype: str + """ + line = self.readline() + if not line: + raise StopIteration + return line def read(self, size=None): """ From fcf56ff9f85653e48db929b4a60460beabad24de Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 10:01:21 -0700 Subject: [PATCH 18/56] Fix bytes/str type in more places --- paramiko/buffered_pipe.py | 3 +- paramiko/ecdsakey.py | 15 ++++++--- paramiko/hostkeys.py | 5 +-- paramiko/packet.py | 4 +-- paramiko/pkey.py | 4 +-- paramiko/rsakey.py | 29 +++++++++++----- paramiko/util.py | 8 ++--- tests/test_buffered_pipe.py | 19 ++++++----- tests/test_hostkeys.py | 19 ++++++----- tests/test_kex.py | 40 +++++++++++----------- tests/test_packetizer.py | 34 +++++++++++++------ tests/test_pkey.py | 67 ++++++++++++++++++++----------------- tests/test_util.py | 6 ++-- 13 files changed, 147 insertions(+), 106 deletions(-) diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index 4ef5cf7..0391dee 100644 --- a/paramiko/buffered_pipe.py +++ b/paramiko/buffered_pipe.py @@ -25,6 +25,7 @@ read operations are blocking and can have a timeout set. import array import threading import time +from paramiko.common import * class PipeTimeout (IOError): @@ -121,7 +122,7 @@ class BufferedPipe (object): @raise PipeTimeout: if a timeout was specified and no data was ready before that timeout """ - out = '' + out = bytes() self._lock.acquire() try: if len(self._buffer) == 0: diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index c3d2a73..d15b2ac 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -63,7 +63,7 @@ class ECDSAKey (PKey): raise SSHException("Can't handle curve of type %s" % curvename) pointinfo = msg.get_binary() - if pointinfo[0] != four_byte: + if pointinfo[0:1] != four_byte: raise SSHException('Point compression is being used: %s' % binascii.hexlify(pointinfo)) self.verifying_key = VerifyingKey.from_string(pointinfo[1:], @@ -157,14 +157,19 @@ class ECDSAKey (PKey): data = self._read_private_key('EC', file_obj, password) self._decode_key(data) - ALLOWED_PADDINGS = ['\x01', '\x02\x02', '\x03\x03\x03', '\x04\x04\x04\x04', - '\x05\x05\x05\x05\x05', '\x06\x06\x06\x06\x06\x06', - '\x07\x07\x07\x07\x07\x07\x07'] + if PY3: + ALLOWED_PADDINGS = [b'\x01', b'\x02\x02', b'\x03\x03\x03', b'\x04\x04\x04\x04', + b'\x05\x05\x05\x05\x05', b'\x06\x06\x06\x06\x06\x06', + b'\x07\x07\x07\x07\x07\x07\x07'] + else: + ALLOWED_PADDINGS = ['\x01', '\x02\x02', '\x03\x03\x03', '\x04\x04\x04\x04', + '\x05\x05\x05\x05\x05', '\x06\x06\x06\x06\x06\x06', + '\x07\x07\x07\x07\x07\x07\x07'] def _decode_key(self, data): s, padding = der.remove_sequence(data) if padding: if padding not in self.ALLOWED_PADDINGS: - raise ValueError("weird padding: %s" % (binascii.hexlify(empty))) + raise ValueError("weird padding: %s" % (binascii.hexlify(data))) data = data[:-len(padding)] key = SigningKey.from_der(data) self.signing_key = key diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 17ee808..015b3ba 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -81,6 +81,7 @@ class HostKeyEntry: # Decide what kind of key we're looking at and create an object # to hold it accordingly. try: + key = b(key) if keytype == 'ssh-rsa': key = RSAKey(data=base64.decodestring(key)) elif keytype == 'ssh-dss': @@ -361,9 +362,9 @@ class HostKeys (MutableMapping): else: if salt.startswith('|1|'): salt = salt.split('|')[2] - salt = base64.decodestring(salt) + salt = base64.decodestring(b(salt)) assert len(salt) == SHA.digest_size - hmac = HMAC.HMAC(salt, hostname, SHA).digest() + hmac = HMAC.HMAC(salt, b(hostname), SHA).digest() hostkey = '|1|%s|%s' % (base64.encodestring(salt), base64.encodestring(hmac)) return hostkey.replace('\n', '') hash_host = staticmethod(hash_host) diff --git a/paramiko/packet.py b/paramiko/packet.py index 193acf6..fa5ceff 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -87,8 +87,8 @@ class Packetizer (object): self.__sdctr_out = False self.__mac_engine_out = None self.__mac_engine_in = None - self.__mac_key_out = '' - self.__mac_key_in = '' + self.__mac_key_out = bytes() + self.__mac_key_in = bytes() self.__compress_engine_out = None self.__compress_engine_in = None self.__sequence_number_out = long_zero diff --git a/paramiko/pkey.py b/paramiko/pkey.py index c88e5c8..5baf450 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -148,7 +148,7 @@ class PKey (object): @return: a base64 string containing the public part of the key. @rtype: str """ - return base64.encodestring(self.asbytes()).replace('\n', '') + return u(base64.encodestring(self.asbytes())).replace('\n', '') def sign_ssh_data(self, rng, data): """ @@ -378,7 +378,7 @@ class PKey (object): f.write('Proc-Type: 4,ENCRYPTED\n') f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper())) f.write('\n') - s = base64.encodestring(data) + s = u(base64.encodestring(data)) # re-wrap to 64-char lines s = ''.join(s.split('\n')) s = '\n'.join([s[i : i+64] for i in range(0, len(s), 64)]) diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index cb7b30b..9094503 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -152,15 +152,26 @@ class RSAKey (PKey): ### internals... - def _pkcs1imify(self, data): - """ - turn a 20-byte SHA1 hash into a blob of data as large as the key's N, - using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. - """ - SHA1_DIGESTINFO = '\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' - size = len(util.deflate_long(self.n, 0)) - filler = '\xff' * (size - len(SHA1_DIGESTINFO) - len(data) - 3) - return '\x00\x01' + filler + '\x00' + SHA1_DIGESTINFO + data + if PY3: + def _pkcs1imify(self, data): + """ + turn a 20-byte SHA1 hash into a blob of data as large as the key's N, + using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. + """ + SHA1_DIGESTINFO = b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' + size = len(util.deflate_long(self.n, 0)) + filler = b'\xff' * (size - len(SHA1_DIGESTINFO) - len(data) - 3) + return b'\x00\x01' + filler + b'\x00' + SHA1_DIGESTINFO + data + else: + def _pkcs1imify(self, data): + """ + turn a 20-byte SHA1 hash into a blob of data as large as the key's N, + using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. + """ + SHA1_DIGESTINFO = b('\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14') + size = len(util.deflate_long(self.n, 0)) + filler = b('\xff') * (size - len(SHA1_DIGESTINFO) - len(data) - 3) + return b('\x00\x01') + filler + b('\x00') + SHA1_DIGESTINFO + b(data) def _from_private_key_file(self, filename, password): data = self._read_private_key_file('RSA', filename, password) diff --git a/paramiko/util.py b/paramiko/util.py index 61fdcc6..7844fc6 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -151,7 +151,7 @@ def generate_key_bytes(hashclass, salt, key, nbytes): hashing function (like C{MD5} or C{SHA}). @type hashclass: L{Crypto.Hash} @param salt: data to salt the hash with. - @type salt: string + @type salt: byte string @param key: human-entered password or passphrase. @type key: string @param nbytes: number of bytes to generate. @@ -159,15 +159,15 @@ def generate_key_bytes(hashclass, salt, key, nbytes): @return: key data @rtype: string """ - keydata = '' - digest = '' + keydata = bytes() + digest = bytes() if len(salt) > 8: salt = salt[:8] while nbytes > 0: hash_obj = hashclass.new() if len(digest) > 0: hash_obj.update(digest) - hash_obj.update(key) + hash_obj.update(b(key)) hash_obj.update(salt) digest = hash_obj.digest() size = min(nbytes, len(digest)) diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index 04d665c..7f48b70 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -25,6 +25,7 @@ import time import unittest from paramiko.buffered_pipe import BufferedPipe, PipeTimeout from paramiko import pipe +from paramiko.py3compat import b from tests.util import ParamikoTest @@ -48,35 +49,35 @@ class BufferedPipeTest(ParamikoTest): p.feed('hello.') self.assert_(p.read_ready()) data = p.read(6) - self.assertEquals('hello.', data) + self.assertEquals(b('hello.'), data) p.feed('plus/minus') - self.assertEquals('plu', p.read(3)) - self.assertEquals('s/m', p.read(3)) - self.assertEquals('inus', p.read(4)) + self.assertEquals(b('plu'), p.read(3)) + self.assertEquals(b('s/m'), p.read(3)) + self.assertEquals(b('inus'), p.read(4)) p.close() self.assert_(not p.read_ready()) - self.assertEquals('', p.read(1)) + self.assertEquals(b(''), p.read(1)) def test_2_delay(self): p = BufferedPipe() self.assert_(not p.read_ready()) threading.Thread(target=delay_thread, args=(p,)).start() - self.assertEquals('a', p.read(1, 0.1)) + self.assertEquals(b('a'), p.read(1, 0.1)) try: p.read(1, 0.1) self.assert_(False) except PipeTimeout: pass - self.assertEquals('b', p.read(1, 1.0)) - self.assertEquals('', p.read(1)) + self.assertEquals(b('b'), p.read(1, 1.0)) + self.assertEquals(b(''), p.read(1)) def test_3_close_while_reading(self): p = BufferedPipe() threading.Thread(target=close_thread, args=(p,)).start() data = p.read(1, 1.0) - self.assertEquals('', data) + self.assertEquals(b(''), data) def test_4_or_pipe(self): p = pipe.make_pipe() diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index fd873e2..66b41b6 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -25,6 +25,7 @@ from binascii import hexlify import os import unittest import paramiko +from paramiko.py3compat import b test_hosts_file = """\ @@ -36,12 +37,12 @@ BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\ 5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M= """ -keyblob = """\ +keyblob = b("""\ AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4k\ NFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgK\ -oc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=""" +oc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=""") -keyblob_dss = """\ +keyblob_dss = b("""\ AAAAB3NzaC1kc3MAAACBAOeBpgNnfRzr/twmAQRu2XwWAp3CFtrVnug6s6fgwj/oLjYbVtjAy6pl/\ h0EKCWx2rf1IetyNsTxWrniA9I6HeDj65X1FyDkg6g8tvCnaNB8Xp/UUhuzHuGsMIipRxBxw9LF60\ 8EqZcj1E3ytktoW5B5OcjrkEoz3xG7C+rpIjYvAAAAFQDwz4UnmsGiSNu5iqjn3uTzwUpshwAAAIE\ @@ -49,7 +50,7 @@ AkxfFeY8P2wZpDjX0MimZl5wkoFQDL25cPzGBuB4OnB8NoUk/yjAHIIpEShw8V+LzouMK5CTJQo5+\ Ngw3qIch/WgRmMHy4kBq1SsXMjQCte1So6HBMvBPIW5SiMTmjCfZZiw4AYHK+B/JaOwaG9yRg2Ejg\ 4Ok10+XFDxlqZo8Y+wAAACARmR7CCPjodxASvRbIyzaVpZoJ/Z6x7dAumV+ysrV1BVYd0lYukmnjO\ 1kKBWApqpH1ve9XDQYN8zgxM4b16L21kpoWQnZtXrY3GZ4/it9kUgyB7+NwacIBlXa8cMDL7Q/69o\ -0d54U0X/NeX5QxuYR6OMJlrkQB7oiW/P/1mwjQgE=""" +0d54U0X/NeX5QxuYR6OMJlrkQB7oiW/P/1mwjQgE=""") class HostKeysTest (unittest.TestCase): @@ -68,7 +69,7 @@ class HostKeysTest (unittest.TestCase): self.assertEquals(1, len(list(hostdict.values())[0])) self.assertEquals(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) + self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) def test_2_add(self): hostdict = paramiko.HostKeys('hostfile.temp') @@ -78,7 +79,7 @@ class HostKeysTest (unittest.TestCase): self.assertEquals(3, len(list(hostdict))) x = hostdict['foo.example.com'] fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp) + self.assertEquals(b('7EC91BB336CB6D810B124B1353C32396'), fp) self.assert_(hostdict.check('foo.example.com', key)) def test_3_dict(self): @@ -90,7 +91,7 @@ class HostKeysTest (unittest.TestCase): x = hostdict.get('secure.example.com', None) self.assert_(x is not None) fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) + self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) i = 0 for key in hostdict: i += 1 @@ -112,6 +113,6 @@ class HostKeysTest (unittest.TestCase): self.assertEquals(1, len(list(hostdict.values())[1])) self.assertEquals(1, len(list(hostdict.values())[2])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp) + self.assertEquals(b('7EC91BB336CB6D810B124B1353C32396'), fp) fp = hexlify(hostdict['secure.example.com']['ssh-dss'].get_fingerprint()).upper() - self.assertEquals('4478F0B9A23CC5182009FF755BC1D26C', fp) + self.assertEquals(b('4478F0B9A23CC5182009FF755BC1D26C'), fp) diff --git a/tests/test_kex.py b/tests/test_kex.py index c94b777..f7cb064 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -40,7 +40,7 @@ class FakeKey (object): def asbytes(self): return b('fake-key') def sign_ssh_data(self, rng, H): - return 'fake-sig' + return b('fake-sig') class FakeModulusPack (object): @@ -91,7 +91,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGroup1(transport) kex.start_kex() - x = '1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' + x = b('1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_group1._MSG_KEXDH_REPLY,), transport._expect) @@ -102,10 +102,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_group1._MSG_KEXDH_REPLY, msg) - H = '03079780F3D3AD0B3C6DB30C8D21685F367A86D2' + H = b('03079780F3D3AD0B3C6DB30C8D21685F367A86D2') self.assertEquals(self.K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(('fake-host-key', 'fake-sig'), transport._verify) + self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) self.assert_(transport._activated) def test_2_group1_server(self): @@ -119,8 +119,8 @@ class KexTest (unittest.TestCase): msg.add_mpint(69) msg.rewind() kex.parse_next(paramiko.kex_group1._MSG_KEXDH_INIT, msg) - H = 'B16BF34DD10945EDE84E9C1EF24A14BFDC843389' - x = '1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' + H = b('B16BF34DD10945EDE84E9C1EF24A14BFDC843389') + x = b('1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') self.assertEquals(self.K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) @@ -131,7 +131,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGex(transport) kex.start_kex() - x = '22000004000000080000002000' + x = b('22000004000000080000002000') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) @@ -140,7 +140,7 @@ class KexTest (unittest.TestCase): msg.add_mpint(FakeModulusPack.G) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) - x = '20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' + x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) @@ -150,10 +150,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) - H = 'A265563F2FA87F1A89BF007EE90D58BE2E4A4BD0' + H = b('A265563F2FA87F1A89BF007EE90D58BE2E4A4BD0') self.assertEquals(self.K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(('fake-host-key', 'fake-sig'), transport._verify) + self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) self.assert_(transport._activated) def test_4_gex_old_client(self): @@ -161,7 +161,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGex(transport) kex.start_kex(_test_old_style=True) - x = '1E00000800' + x = b('1E00000800') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) @@ -170,7 +170,7 @@ class KexTest (unittest.TestCase): msg.add_mpint(FakeModulusPack.G) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) - x = '20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' + x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) @@ -180,10 +180,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) - H = '807F87B269EF7AC5EC7E75676808776A27D5864C' + H = b('807F87B269EF7AC5EC7E75676808776A27D5864C') self.assertEquals(self.K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(('fake-host-key', 'fake-sig'), transport._verify) + self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) self.assert_(transport._activated) def test_5_gex_server(self): @@ -199,7 +199,7 @@ class KexTest (unittest.TestCase): msg.add_int(4096) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, msg) - x = '1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' + x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) @@ -208,8 +208,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 - H = 'CE754197C21BF3452863B4F44D0B3951F12516EF' - x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' + H = b('CE754197C21BF3452863B4F44D0B3951F12516EF') + x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') self.assertEquals(K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) @@ -226,7 +226,7 @@ class KexTest (unittest.TestCase): msg.add_int(2048) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD, msg) - x = '1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' + x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) @@ -235,8 +235,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 - H = 'B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B' - x = '210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' + H = b('B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B') + x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') self.assertEquals(K, transport._K) self.assertEquals(H, hexlify(transport._H).upper()) self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index b53b741..111ecb9 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -25,7 +25,15 @@ from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util -from paramiko.py3compat import byte_chr +from paramiko.common import * + +if PY3: + x55 = b'\x55' + x1f = b'\x1f' +else: + x55 = '\x55' + x1f = '\x1f' + class PacketizerTest (unittest.TestCase): @@ -36,8 +44,8 @@ class PacketizerTest (unittest.TestCase): p = Packetizer(wsock) p.set_log(util.get_logger('paramiko.transport')) p.set_hexdump(True) - cipher = AES.new('\x00' * 16, AES.MODE_CBC, '\x55' * 16) - p.set_outbound_cipher(cipher, 16, SHA, 12, '\x1f' * 20) + cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) + p.set_outbound_cipher(cipher, 16, SHA, 12, x1f * 20) # message has to be at least 16 bytes long, so we'll have at least one # block of data encrypted that contains zero random padding bytes @@ -50,8 +58,11 @@ class PacketizerTest (unittest.TestCase): data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 self.assertEquals(44, len(data)) - self.assertEquals('\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) - + if PY3: + self.assertEquals(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) + else: + self.assertEquals('\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) + def test_2_read (self): rsock = LoopSocket() wsock = LoopSocket() @@ -59,11 +70,14 @@ class PacketizerTest (unittest.TestCase): p = Packetizer(rsock) p.set_log(util.get_logger('paramiko.transport')) p.set_hexdump(True) - cipher = AES.new('\x00' * 16, AES.MODE_CBC, '\x55' * 16) - p.set_inbound_cipher(cipher, 16, SHA, 12, '\x1f' * 20) - - wsock.send('C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ - '\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') + cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) + p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) + if PY3: + wsock.send(b'C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ + b'\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') + else: + wsock.send('C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ + '\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') cmd, m = p.read_message() self.assertEquals(100, cmd) self.assertEquals(100, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index c7240db..b75f9a1 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,8 @@ Some unit tests for public/private key objects. from binascii import hexlify, unhexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr +from paramiko.common import rng, StringIO, byte_chr, b, PY3 +from tests.util import test_path # from openssh's ssh-keygen PUB_RSA = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA049W6geFpmsljTwfvI1UmKWWJPNFI74+vNKTk4dmzkQY2yAMs6FhlvhlI8ysU4oj71ZsRYMecHbBbxdN79+JRFVYTKaLqjwGENeTd+yv4q+V2PvZv3fLnzApI3l7EJCqhWwJUHJ1jAkZzqDx0tyOL4uoZpww3nmE0kb3y21tH4c=' @@ -76,6 +77,12 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g== -----END EC PRIVATE KEY----- """ +if PY3: + x1234 = b'\x01\x02\x03\x04' +else: + x1234 = '\x01\x02\x03\x04' + + class KeyTest (unittest.TestCase): def setUp(self): @@ -86,14 +93,14 @@ class KeyTest (unittest.TestCase): def test_1_generate_key_bytes(self): from Crypto.Hash import MD5 - key = util.generate_key_bytes(MD5, '\x01\x02\x03\x04', 'happy birthday', 30) + key = util.generate_key_bytes(MD5, x1234, 'happy birthday', 30) exp = unhexlify('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64') self.assertEquals(exp, key) def test_2_load_rsa(self): - key = RSAKey.from_private_key_file('tests/test_rsa.key') + key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.assertEquals('ssh-rsa', key.get_name()) - exp_rsa = FINGER_RSA.split()[1].replace(':', '') + exp_rsa = b(FINGER_RSA.split()[1].replace(':', '')) my_rsa = hexlify(key.get_fingerprint()) self.assertEquals(exp_rsa, my_rsa) self.assertEquals(PUB_RSA.split()[1], key.get_base64()) @@ -107,18 +114,18 @@ class KeyTest (unittest.TestCase): self.assertEquals(key, key2) def test_3_load_rsa_password(self): - key = RSAKey.from_private_key_file('tests/test_rsa_password.key', 'television') + key = RSAKey.from_private_key_file(test_path('test_rsa_password.key'), 'television') self.assertEquals('ssh-rsa', key.get_name()) - exp_rsa = FINGER_RSA.split()[1].replace(':', '') + exp_rsa = b(FINGER_RSA.split()[1].replace(':', '')) my_rsa = hexlify(key.get_fingerprint()) self.assertEquals(exp_rsa, my_rsa) self.assertEquals(PUB_RSA.split()[1], key.get_base64()) self.assertEquals(1024, key.get_bits()) def test_4_load_dss(self): - key = DSSKey.from_private_key_file('tests/test_dss.key') + key = DSSKey.from_private_key_file(test_path('test_dss.key')) self.assertEquals('ssh-dss', key.get_name()) - exp_dss = FINGER_DSS.split()[1].replace(':', '') + exp_dss = b(FINGER_DSS.split()[1].replace(':', '')) my_dss = hexlify(key.get_fingerprint()) self.assertEquals(exp_dss, my_dss) self.assertEquals(PUB_DSS.split()[1], key.get_base64()) @@ -132,9 +139,9 @@ class KeyTest (unittest.TestCase): self.assertEquals(key, key2) def test_5_load_dss_password(self): - key = DSSKey.from_private_key_file('tests/test_dss_password.key', 'television') + key = DSSKey.from_private_key_file(test_path('test_dss_password.key'), 'television') self.assertEquals('ssh-dss', key.get_name()) - exp_dss = FINGER_DSS.split()[1].replace(':', '') + exp_dss = b(FINGER_DSS.split()[1].replace(':', '')) my_dss = hexlify(key.get_fingerprint()) self.assertEquals(exp_dss, my_dss) self.assertEquals(PUB_DSS.split()[1], key.get_base64()) @@ -142,7 +149,7 @@ class KeyTest (unittest.TestCase): def test_6_compare_rsa(self): # verify that the private & public keys compare equal - key = RSAKey.from_private_key_file('tests/test_rsa.key') + key = RSAKey.from_private_key_file(test_path('test_rsa.key')) self.assertEquals(key, key) pub = RSAKey(data=key.asbytes()) self.assert_(key.can_sign()) @@ -151,7 +158,7 @@ class KeyTest (unittest.TestCase): def test_7_compare_dss(self): # verify that the private & public keys compare equal - key = DSSKey.from_private_key_file('tests/test_dss.key') + key = DSSKey.from_private_key_file(test_path('test_dss.key')) self.assertEquals(key, key) pub = DSSKey(data=key.asbytes()) self.assert_(key.can_sign()) @@ -160,8 +167,8 @@ class KeyTest (unittest.TestCase): def test_8_sign_rsa(self): # verify that the rsa private key can sign and verify - key = RSAKey.from_private_key_file('tests/test_rsa.key') - msg = key.sign_ssh_data(rng, 'ice weasels') + key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + msg = key.sign_ssh_data(rng, b('ice weasels')) self.assert_(type(msg) is Message) msg.rewind() self.assertEquals('ssh-rsa', msg.get_text()) @@ -169,12 +176,12 @@ class KeyTest (unittest.TestCase): self.assertEquals(sig, msg.get_binary()) msg.rewind() pub = RSAKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig('ice weasels', msg)) + self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) def test_9_sign_dss(self): # verify that the dss private key can sign and verify - key = DSSKey.from_private_key_file('tests/test_dss.key') - msg = key.sign_ssh_data(rng, 'ice weasels') + key = DSSKey.from_private_key_file(test_path('test_dss.key')) + msg = key.sign_ssh_data(rng, b('ice weasels')) self.assert_(type(msg) is Message) msg.rewind() self.assertEquals('ssh-dss', msg.get_text()) @@ -184,24 +191,24 @@ class KeyTest (unittest.TestCase): self.assertEquals(40, len(msg.get_binary())) msg.rewind() pub = DSSKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig('ice weasels', msg)) + self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) def test_A_generate_rsa(self): key = RSAKey.generate(1024) - msg = key.sign_ssh_data(rng, 'jerri blank') + msg = key.sign_ssh_data(rng, b('jerri blank')) msg.rewind() - self.assert_(key.verify_ssh_sig('jerri blank', msg)) + self.assert_(key.verify_ssh_sig(b('jerri blank'), msg)) def test_B_generate_dss(self): key = DSSKey.generate(1024) - msg = key.sign_ssh_data(rng, 'jerri blank') + msg = key.sign_ssh_data(rng, b('jerri blank')) msg.rewind() - self.assert_(key.verify_ssh_sig('jerri blank', msg)) + self.assert_(key.verify_ssh_sig(b('jerri blank'), msg)) def test_10_load_ecdsa(self): - key = ECDSAKey.from_private_key_file('tests/test_ecdsa.key') + key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) self.assertEquals('ecdsa-sha2-nistp256', key.get_name()) - exp_ecdsa = FINGER_ECDSA.split()[1].replace(':', '') + exp_ecdsa = b(FINGER_ECDSA.split()[1].replace(':', '')) my_ecdsa = hexlify(key.get_fingerprint()) self.assertEquals(exp_ecdsa, my_ecdsa) self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) @@ -215,9 +222,9 @@ class KeyTest (unittest.TestCase): self.assertEquals(key, key2) def test_11_load_ecdsa_password(self): - key = ECDSAKey.from_private_key_file('tests/test_ecdsa_password.key', 'television') + key = ECDSAKey.from_private_key_file(test_path('test_ecdsa_password.key'), b('television')) self.assertEquals('ecdsa-sha2-nistp256', key.get_name()) - exp_ecdsa = FINGER_ECDSA.split()[1].replace(':', '') + exp_ecdsa = b(FINGER_ECDSA.split()[1].replace(':', '')) my_ecdsa = hexlify(key.get_fingerprint()) self.assertEquals(exp_ecdsa, my_ecdsa) self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) @@ -225,7 +232,7 @@ class KeyTest (unittest.TestCase): def test_12_compare_ecdsa(self): # verify that the private & public keys compare equal - key = ECDSAKey.from_private_key_file('tests/test_ecdsa.key') + key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) self.assertEquals(key, key) pub = ECDSAKey(data=key.asbytes()) self.assert_(key.can_sign()) @@ -234,8 +241,8 @@ class KeyTest (unittest.TestCase): def test_13_sign_ecdsa(self): # verify that the rsa private key can sign and verify - key = ECDSAKey.from_private_key_file('tests/test_ecdsa.key') - msg = key.sign_ssh_data(rng, 'ice weasels') + key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) + msg = key.sign_ssh_data(rng, b('ice weasels')) self.assert_(type(msg) is Message) msg.rewind() self.assertEquals('ecdsa-sha2-nistp256', msg.get_text()) @@ -246,4 +253,4 @@ class KeyTest (unittest.TestCase): msg.rewind() pub = ECDSAKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig('ice weasels', msg)) + self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) diff --git a/tests/test_util.py b/tests/test_util.py index 858dedb..84d5e88 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -27,7 +27,7 @@ import unittest from Crypto.Hash import SHA import paramiko.util from paramiko.util import lookup_ssh_host_config as host_config -from paramiko.py3compat import StringIO, byte_ord +from paramiko.py3compat import StringIO, byte_ord, b from tests.util import ParamikoTest @@ -137,7 +137,7 @@ class UtilTest(ParamikoTest): ) def test_4_generate_key_bytes(self): - x = paramiko.util.generate_key_bytes(SHA, 'ABCDEFGH', 'This is my secret passphrase.', 64) + x = paramiko.util.generate_key_bytes(SHA, b('ABCDEFGH'), 'This is my secret passphrase.', 64) hex = ''.join(['%02x' % byte_ord(c) for c in x]) self.assertEquals(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') @@ -151,7 +151,7 @@ class UtilTest(ParamikoTest): self.assertEquals(1, len(list(hostdict.values())[0])) self.assertEquals(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals('E6684DB30E109B67B70FF1DC5C7F1363', fp) + self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) finally: os.unlink('hostfile.temp') From 488d85f9816679f73b369ae4bce19c2834d07cfe Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 10:03:38 -0700 Subject: [PATCH 19/56] Setup so we can run test_sftp_big independently --- tests/test_sftp_big.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 20bf007..379bfe1 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -384,3 +384,10 @@ class BigSFTPTest (unittest.TestCase): finally: sftp.remove('%s/hongry.txt' % FOLDER) t.packetizer.REKEY_BYTES = pow(2, 30) + + +if __name__ == '__main__': + from tests.test_sftp import SFTPTest + SFTPTest.init_loopback() + from unittest import main + main() From 85ade33ae378cff796cb9ad97a855ced957a1dde Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 11:36:47 -0700 Subject: [PATCH 20/56] More type fixups --- paramiko/hostkeys.py | 2 +- paramiko/transport.py | 6 +++--- tests/test_transport.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 015b3ba..a7f9b43 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -365,7 +365,7 @@ class HostKeys (MutableMapping): salt = base64.decodestring(b(salt)) assert len(salt) == SHA.digest_size hmac = HMAC.HMAC(salt, b(hostname), SHA).digest() - hostkey = '|1|%s|%s' % (base64.encodestring(salt), base64.encodestring(hmac)) + hostkey = '|1|%s|%s' % (u(base64.encodestring(salt)), u(base64.encodestring(hmac))) return hostkey.replace('\n', '') hash_host = staticmethod(hash_host) diff --git a/paramiko/transport.py b/paramiko/transport.py index 101fe17..c679a98 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -1747,8 +1747,8 @@ class Transport (threading.Thread): m.add_list(self._preferred_macs) m.add_list(self._preferred_compression) m.add_list(self._preferred_compression) - m.add_string('') - m.add_string('') + m.add_string(bytes()) + m.add_string(bytes()) m.add_boolean(False) m.add_int(0) # save a copy for later (needed to compute a hash) @@ -1948,7 +1948,7 @@ class Transport (threading.Thread): def _parse_disconnect(self, m): code = m.get_int() - desc = m.get_string() + desc = m.get_text() self._log(INFO, 'Disconnect (code %d): %s' % (code, desc)) def _parse_global_request(self, m): diff --git a/tests/test_transport.py b/tests/test_transport.py index 69fdbbb..ccd44e4 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -33,7 +33,7 @@ from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey SSHException, BadAuthenticationType, InteractiveQuery, ChannelException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST +from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b from paramiko.message import Message from tests.loop import LoopSocket from tests.util import ParamikoTest, test_path @@ -162,7 +162,7 @@ class TransportTest(ParamikoTest): self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3') self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) - self.assertEquals('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995', + self.assertEquals(b('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995'), hexlify(key).upper()) def test_3_simple(self): From 04097cbb2610ab64f0e73d2ce9773362b4c0fb40 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 11:37:37 -0700 Subject: [PATCH 21/56] Switch feed from text to binary --- paramiko/channel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index d686c6d..182cd68 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -978,7 +978,7 @@ class Channel (object): def _feed_extended(self, m): code = m.get_int() - s = m.get_text() + s = m.get_binary() if code != 1: self._log(ERROR, 'unknown extended_data type %d; discarding' % code) return From 09a4ffb282957595b1eee7adaead5d0503a5f54a Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:24:09 -0700 Subject: [PATCH 22/56] Salt needs to be 16 bytes instead of 8 --- paramiko/pkey.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 5baf450..376041d 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -367,7 +367,7 @@ class PKey (object): keysize = self._CIPHER_TABLE[cipher_name]['keysize'] blocksize = self._CIPHER_TABLE[cipher_name]['blocksize'] mode = self._CIPHER_TABLE[cipher_name]['mode'] - salt = rng.read(8) + salt = rng.read(16) key = util.generate_key_bytes(MD5, salt, password, keysize) if len(data) % blocksize != 0: n = blocksize - len(data) % blocksize From 7a45d3c70f6f308835ab66e3899e247e0efc17e7 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:25:45 -0700 Subject: [PATCH 23/56] More type conversion --- demos/interactive.py | 3 ++- paramiko/channel.py | 2 +- paramiko/common.py | 2 +- paramiko/file.py | 2 +- paramiko/pkey.py | 6 +++--- paramiko/py3compat.py | 8 +++++++- paramiko/transport.py | 2 +- tests/loop.py | 3 ++- tests/test_client.py | 3 ++- tests/test_transport.py | 14 +++++++------- 10 files changed, 27 insertions(+), 18 deletions(-) diff --git a/demos/interactive.py b/demos/interactive.py index b7962b3..7138cd6 100644 --- a/demos/interactive.py +++ b/demos/interactive.py @@ -19,6 +19,7 @@ import socket import sys +from paramiko.py3compat import u # windows does not have termios... try: @@ -49,7 +50,7 @@ def posix_shell(chan): r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: - x = chan.recv(1024) + x = u(chan.recv(1024)) if len(x) == 0: sys.stdout.write('\r\n*** EOF\r\n') break diff --git a/paramiko/channel.py b/paramiko/channel.py index 182cd68..d715bb8 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -1059,7 +1059,7 @@ class Channel (object): elif key == 'x11-req': single_connection = m.get_boolean() auth_proto = m.get_text() - auth_cookie = m.get_text() + auth_cookie = m.get_binary() screen_number = m.get_int() if server is None: ok = False diff --git a/paramiko/common.py b/paramiko/common.py index 2392f03..9161f30 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -129,7 +129,7 @@ if PY3: four_byte = b'\x04' max_byte = b'\xff' newline_byte = b'\n' - cr_byte = b'\r' + cr_byte = 13 else: zero_byte = '\x00' one_byte = '\x01' diff --git a/paramiko/file.py b/paramiko/file.py index aca97c6..625737f 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -235,7 +235,7 @@ class BufferedFile (object): self._rbuffer = '' self._pos += len(line) return line - line += new_data + line += b2s(new_data) self._realpos += len(new_data) # find the newline pos = line.find('\n') diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 376041d..4cead1c 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -321,7 +321,7 @@ class PKey (object): try: encryption_type, saltstr = headers['dek-info'].split(',') except: - raise SSHException('Can\'t parse DEK-info in private key file') + raise SSHException("Can't parse DEK-info in private key file") if encryption_type not in self._CIPHER_TABLE: raise SSHException('Unknown private key cipher "%s"' % encryption_type) # if no password was passed in, raise an exception pointing out that we need one @@ -373,10 +373,10 @@ class PKey (object): n = blocksize - len(data) % blocksize #data += rng.read(n) # that would make more sense ^, but it confuses openssh. - data += '\0' * n + data += zero_byte * n data = cipher.new(key, mode, salt).encrypt(data) f.write('Proc-Type: 4,ENCRYPTED\n') - f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper())) + f.write('DEK-Info: %s,%s\n' % (cipher_name, u(hexlify(salt)).upper())) f.write('\n') s = u(base64.encodestring(data)) # re-wrap to 64-char lines diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index c4a85ac..e4830da 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,6 @@ import sys -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -47,6 +47,9 @@ if PY3: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def b2s(s): + return s.decode() if isinstance(s, bytes) else s + import io StringIO = io.StringIO # NOQA BytesIO = io.BytesIO # NOQA @@ -97,6 +100,9 @@ else: else: raise TypeError("Expected unicode or bytes, got %r" % s) + def b2s(s): + return s + try: import cStringIO StringIO = cStringIO.StringIO # NOQA diff --git a/paramiko/transport.py b/paramiko/transport.py index c679a98..46e0fe8 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -805,7 +805,7 @@ class Transport (threading.Thread): """ if not self.active: raise SSHException('SSH session not active') - address = str(address) + address = address port = int(port) response = self.global_request('tcpip-forward', (address, port), wait=True) if response is None: diff --git a/tests/loop.py b/tests/loop.py index cfd7626..b2c73dd 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -21,7 +21,7 @@ """ import threading, socket -from paramiko.py3compat import * +from paramiko.common import * class LoopSocket (object): @@ -47,6 +47,7 @@ class LoopSocket (object): self.__lock.release() def send(self, data): + data = asbytes(data) if self.__mate is None: # EOF raise EOFError() diff --git a/tests/test_client.py b/tests/test_client.py index 0959ac9..a8d0463 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -29,6 +29,7 @@ import weakref from binascii import hexlify from tests.util import test_path import paramiko +from paramiko.py3compat import b class NullServer (paramiko.ServerInterface): @@ -44,7 +45,7 @@ class NullServer (paramiko.ServerInterface): return paramiko.AUTH_FAILED def check_auth_publickey(self, username, key): - if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == '4478f0b9a23cc5182009ff755bc1d26c'): + if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == b('4478f0b9a23cc5182009ff755bc1d26c')): return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_FAILED diff --git a/tests/test_transport.py b/tests/test_transport.py index ccd44e4..230f4a2 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -362,7 +362,7 @@ class TransportTest(ParamikoTest): self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('hello\n', chan.recv(6)) + self.assertEquals(b('hello\n'), chan.recv(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) @@ -381,7 +381,7 @@ class TransportTest(ParamikoTest): self.assertEquals([chan], r) self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('', chan.recv(16)) + self.assertEquals(bytes(), chan.recv(16)) # make sure the pipe is still open for now... p = chan._pipe @@ -463,7 +463,7 @@ class TransportTest(ParamikoTest): self.assertEquals(6093, requested[0][1]) x11_server.send('hello') - self.assertEquals('hello', x11_client.recv(5)) + self.assertEquals(b('hello'), x11_client.recv(5)) x11_server.close() x11_client.close() @@ -496,7 +496,7 @@ class TransportTest(ParamikoTest): cch = self.tc.accept() sch.send('hello') - self.assertEquals('hello', cch.recv(5)) + self.assertEquals(b('hello'), cch.recv(5)) sch.close() cch.close() ss.close() @@ -528,12 +528,12 @@ class TransportTest(ParamikoTest): cch.connect(self.server._tcpip_dest) ss, _ = greeting_server.accept() - ss.send('Hello!\n') + ss.send(b('Hello!\n')) ss.close() sch.send(cch.recv(8192)) sch.close() - self.assertEquals('Hello!\n', cs.recv(7)) + self.assertEquals(b('Hello!\n'), cs.recv(7)) cs.close() def test_G_stderr_select(self): @@ -564,7 +564,7 @@ class TransportTest(ParamikoTest): self.assertEquals([], w) self.assertEquals([], e) - self.assertEquals('hello\n', chan.recv_stderr(6)) + self.assertEquals(b('hello\n'), chan.recv_stderr(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) From 8bda3ab2bb5c5cae5baa301148545f6621b4a4c1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:25:57 -0700 Subject: [PATCH 24/56] Fix demo_keygen --- demos/demo_keygen.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/demos/demo_keygen.py b/demos/demo_keygen.py index bdd7388..88facd4 100755 --- a/demos/demo_keygen.py +++ b/demos/demo_keygen.py @@ -19,7 +19,6 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. from __future__ import with_statement -import string import sys from binascii import hexlify @@ -28,6 +27,7 @@ from optparse import OptionParser from paramiko import DSSKey from paramiko import RSAKey from paramiko.ssh_exception import SSHException +from paramiko.py3compat import u usage=""" %prog [-v] [-b bits] -t type [-N new_passphrase] [-f output_keyfile]""" @@ -47,16 +47,16 @@ key_dispatch_table = { def progress(arg=None): if not arg: - print '0%\x08\x08\x08', + sys.stdout.write('0%\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'p': - print '25%\x08\x08\x08\x08', + sys.stdout.write('25%\x08\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'h': - print '50%\x08\x08\x08\x08', + sys.stdout.write('50%\x08\x08\x08\x08 ') sys.stdout.flush() elif arg[0] == 'x': - print '75%\x08\x08\x08\x08', + sys.stdout.write('75%\x08\x08\x08\x08 ') sys.stdout.flush() if __name__ == '__main__': @@ -92,8 +92,8 @@ if __name__ == '__main__': parser.print_help() sys.exit(0) - for o in default_values.keys(): - globals()[o] = getattr(options, o, default_values[string.lower(o)]) + for o in list(default_values.keys()): + globals()[o] = getattr(options, o, default_values[o.lower()]) if options.newphrase: phrase = getattr(options, 'newphrase') @@ -106,7 +106,7 @@ if __name__ == '__main__': if ktype == 'dsa' and bits > 1024: raise SSHException("DSA Keys must be 1024 bits") - if not key_dispatch_table.has_key(ktype): + if ktype not in key_dispatch_table: raise SSHException("Unknown %s algorithm to generate keys pair" % ktype) # generating private key @@ -121,7 +121,7 @@ if __name__ == '__main__': f.write(" %s" % comment) if options.verbose: - print "done." + print("done.") - hash = hexlify(pub.get_fingerprint()) - print "Fingerprint: %d %s %s.pub (%s)" % (bits, ":".join([ hash[i:2+i] for i in range(0, len(hash), 2)]), filename, string.upper(ktype)) + hash = u(hexlify(pub.get_fingerprint())) + print("Fingerprint: %d %s %s.pub (%s)" % (bits, ":".join([ hash[i:2+i] for i in range(0, len(hash), 2)]), filename, ktype.upper())) From bc683ac3653e2d66fe18c020374ec5b7c723ed53 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 15:35:35 -0700 Subject: [PATCH 25/56] Fix demo_server --- demos/demo_server.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/demos/demo_server.py b/demos/demo_server.py index 915b0c6..deb2138 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -27,6 +27,7 @@ import threading import traceback import paramiko +from paramiko.py3compat import b, u # setup logging @@ -35,16 +36,16 @@ paramiko.util.log_to_file('demo_server.log') host_key = paramiko.RSAKey(filename='test_rsa.key') #host_key = paramiko.DSSKey(filename='test_dss.key') -print 'Read key: ' + hexlify(host_key.get_fingerprint()) +print('Read key: ' + u(hexlify(host_key.get_fingerprint()))) class Server (paramiko.ServerInterface): # 'data' is the output of base64.encodestring(str(key)) # (using the "user_rsa_key" files) - data = 'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ - 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ - 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ - 'UWT10hcuO4Ks8=' + data = b('AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ + 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ + 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ + 'UWT10hcuO4Ks8=') good_pub_key = paramiko.RSAKey(data=base64.decodestring(data)) def __init__(self): @@ -61,7 +62,7 @@ class Server (paramiko.ServerInterface): return paramiko.AUTH_FAILED def check_auth_publickey(self, username, key): - print 'Auth attempt with key: ' + hexlify(key.get_fingerprint()) + print('Auth attempt with key: ' + u(hexlify(key.get_fingerprint()))) if (username == 'robey') and (key == self.good_pub_key): return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_FAILED @@ -83,47 +84,49 @@ try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 2200)) -except Exception, e: - print '*** Bind failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) try: sock.listen(100) - print 'Listening for connection ...' + print('Listening for connection ...') client, addr = sock.accept() -except Exception, e: - print '*** Listen/accept failed: ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) -print 'Got a connection!' +print('Got a connection!') try: t = paramiko.Transport(client) try: t.load_server_moduli() except: - print '(Failed to load moduli -- gex will be unsupported.)' + print('(Failed to load moduli -- gex will be unsupported.)') raise t.add_server_key(host_key) server = Server() try: t.start_server(server=server) - except paramiko.SSHException, x: - print '*** SSH negotiation failed.' + except paramiko.SSHException: + print('*** SSH negotiation failed.') sys.exit(1) # wait for auth chan = t.accept(20) if chan is None: - print '*** No channel.' + print('*** No channel.') sys.exit(1) - print 'Authenticated!' + print('Authenticated!') server.event.wait(10) if not server.event.isSet(): - print '*** Client never asked for a shell.' + print('*** Client never asked for a shell.') sys.exit(1) chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n') @@ -135,8 +138,9 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception, e: - print '*** Caught exception: ' + str(e.__class__) + ': ' + str(e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: t.close() From 951e8cfd3a4d892cdaed82ce6b62803dcc001209 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 16:05:30 -0700 Subject: [PATCH 26/56] Fix demos --- demos/demo.py | 1 + demos/demo_sftp.py | 26 ++++++++++++++------------ demos/demo_simple.py | 24 ++++++++++++++---------- demos/forward.py | 18 ++++++++++++------ demos/rforward.py | 12 +++++++----- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index 744b5c3..2f245ba 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -28,6 +28,7 @@ import socket import sys import time import traceback +from paramiko.py3compat import input import paramiko try: diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 7c4aaba..2dba172 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -28,6 +28,7 @@ import sys import traceback import paramiko +from paramiko.py3compat import input # setup logging @@ -40,9 +41,9 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -53,7 +54,7 @@ if hostname.find(':') >= 0: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) @@ -69,13 +70,13 @@ except IOError: # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: - print '*** Unable to open host keys file' + print('*** Unable to open host keys file') host_keys = {} -if host_keys.has_key(hostname): +if hostname in host_keys: hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] - print 'Using host key of type %s' % hostkeytype + print('Using host key of type %s' % hostkeytype) # now, connect and use paramiko Transport to negotiate SSH2 across the connection @@ -86,22 +87,22 @@ try: # dirlist on remote host dirlist = sftp.listdir('.') - print "Dirlist:", dirlist + print("Dirlist: %s" % dirlist) # copy this demo onto the server try: sftp.mkdir("demo_sftp_folder") except IOError: - print '(assuming demo_sftp_folder/ already exists)' + print('(assuming demo_sftp_folder/ already exists)') sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') data = open('demo_sftp.py', 'r').read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) - print 'created demo_sftp_folder/ on the server' + print('created demo_sftp_folder/ on the server') # copy the README back here data = sftp.open('demo_sftp_folder/README', 'r').read() open('README_demo_sftp', 'w').write(data) - print 'copied README back here' + print('copied README back here') # BETTER: use the get() and put() methods sftp.put('demo_sftp.py', 'demo_sftp_folder/demo_sftp.py') @@ -109,8 +110,9 @@ try: t.close() -except Exception, e: - print '*** Caught exception: %s: %s' % (e.__class__, e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: t.close() diff --git a/demos/demo_simple.py b/demos/demo_simple.py index 50f344a..fb71145 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -25,9 +25,13 @@ import os import socket import sys import traceback +from paramiko.py3compat import input import paramiko -import interactive +try: + import interactive +except ImportError: + from . import interactive # setup logging @@ -40,9 +44,9 @@ if len(sys.argv) > 1: if hostname.find('@') >= 0: username, hostname = hostname.split('@') else: - hostname = raw_input('Hostname: ') + hostname = input('Hostname: ') if len(hostname) == 0: - print '*** Hostname required.' + print('*** Hostname required.') sys.exit(1) port = 22 if hostname.find(':') >= 0: @@ -53,7 +57,7 @@ if hostname.find(':') >= 0: # get username if username == '': default_username = getpass.getuser() - username = raw_input('Username [%s]: ' % default_username) + username = input('Username [%s]: ' % default_username) if len(username) == 0: username = default_username password = getpass.getpass('Password for %s@%s: ' % (username, hostname)) @@ -64,18 +68,18 @@ try: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy()) - print '*** Connecting...' + print('*** Connecting...') client.connect(hostname, port, username, password) chan = client.invoke_shell() - print repr(client.get_transport()) - print '*** Here we go!' - print + print(repr(client.get_transport())) + print('*** Here we go!\n') interactive.interactive_shell(chan) chan.close() client.close() -except Exception, e: - print '*** Caught exception: %s: %s' % (e.__class__, e) +except Exception: + e = sys.exc_info()[1] + print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: client.close() diff --git a/demos/forward.py b/demos/forward.py index 5048c77..f1a0c04 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -30,7 +30,11 @@ import getpass import os import socket import select -import SocketServer +try: + import SocketServer +except ImportError: + import socketserver as SocketServer + import sys from optparse import OptionParser @@ -54,7 +58,8 @@ class Handler (SocketServer.BaseRequestHandler): chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) - except Exception, e: + except Exception: + e = sys.exc_info()[1] verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -98,7 +103,7 @@ def forward_tunnel(local_port, remote_host, remote_port, transport): def verbose(s): if g_verbose: - print s + print(s) HELP = """\ @@ -165,8 +170,9 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception, e: - print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) + except Exception: + e = sys.exc_info()[1] + print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) verbose('Now forwarding port %d to %s:%d ...' % (options.port, remote[0], remote[1])) @@ -174,7 +180,7 @@ def main(): try: forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) except KeyboardInterrupt: - print 'C-c: Port forwarding stopped.' + print('C-c: Port forwarding stopped.') sys.exit(0) diff --git a/demos/rforward.py b/demos/rforward.py index 4a5d2e4..fcffbcb 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -46,7 +46,8 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception, e: + except Exception: + e = sys.exc_info()[1] verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -82,7 +83,7 @@ def reverse_forward_tunnel(server_port, remote_host, remote_port, transport): def verbose(s): if g_verbose: - print s + print(s) HELP = """\ @@ -150,8 +151,9 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception, e: - print '*** Failed to connect to %s:%d: %r' % (server[0], server[1], e) + except Exception: + e = sys.exc_info()[1] + print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) verbose('Now forwarding remote port %d to %s:%d ...' % (options.port, remote[0], remote[1])) @@ -159,7 +161,7 @@ def main(): try: reverse_forward_tunnel(options.port, remote[0], remote[1], client.get_transport()) except KeyboardInterrupt: - print 'C-c: Port forwarding stopped.' + print('C-c: Port forwarding stopped.') sys.exit(0) From d26bf3e63efea0002fd81c7579f2b37cc6c587c1 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 16:19:11 -0700 Subject: [PATCH 27/56] More type conversion --- paramiko/channel.py | 2 +- paramiko/common.py | 2 +- paramiko/py3compat.py | 10 +++++----- paramiko/sftp.py | 4 ++-- paramiko/sftp_client.py | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index d715bb8..6a8a798 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -969,7 +969,7 @@ class Channel (object): self.transport._send_user_message(m) def _feed(self, m): - if isinstance(m, bytes_type): + if isinstance(m, bytes_types): # passed from _feed_extended s = m else: diff --git a/paramiko/common.py b/paramiko/common.py index 9161f30..5d7ab31 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -140,7 +140,7 @@ else: def asbytes(s): - if not isinstance(s, bytes_type): + if not isinstance(s, bytes_types): if isinstance(s, string_types): s = b(s) else: diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index e4830da..fd6488d 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,6 @@ import sys -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_type', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -8,9 +8,9 @@ if PY3: import collections import struct string_types = str - integer_types = int text_type = str - bytes_type = bytes + bytes_types = bytes + integer_types = int long = int input = input @@ -65,9 +65,9 @@ if PY3: MAXSIZE = sys.maxsize # NOQA else: string_types = basestring - integer_types = (int, long) text_type = unicode - bytes_type = str + bytes_types = str + integer_types = (int, long) long = long input = raw_input diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 11aa3cb..4186460 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -142,7 +142,7 @@ class BaseSFTP (object): return def _read_all(self, n): - out = '' + out = bytes() while n > 0: if isinstance(self.sock, socket.socket): # sometimes sftp is used directly over a socket instead of @@ -176,7 +176,7 @@ class BaseSFTP (object): x = self._read_all(4) # most sftp servers won't accept packets larger than about 32k, so # anything with the high byte set (> 16MB) is just garbage. - if x[0] != '\x00': + if byte_ord(x[0]): raise SFTPError('Garbage packet received') size = struct.unpack('>I', x)[0] data = self._read_all(size) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 7703246..22a9a04 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -42,7 +42,7 @@ def _to_unicode(s): """ try: return s.encode('ascii') - except UnicodeError: + except (UnicodeError, AttributeError): try: return s.decode('utf-8') except UnicodeError: @@ -698,7 +698,7 @@ class SFTPClient (BaseSFTP): msg.add_int(item) elif isinstance(item, long): msg.add_int64(item) - elif isinstance(item, string_types): + elif isinstance(item, (string_types, bytes_types)): msg.add_string(item) elif isinstance(item, SFTPAttributes): item._pack(msg) From 8e1a7ef4d8278421da43eb8101ae68bec29c5b07 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 17:19:58 -0700 Subject: [PATCH 28/56] More type conversion --- paramiko/sftp_client.py | 17 +++++++++-------- paramiko/sftp_file.py | 2 +- paramiko/sftp_server.py | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 22a9a04..11b61f7 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -48,6 +48,7 @@ def _to_unicode(s): except UnicodeError: return s +b_slash = b('/') class SFTPClient (BaseSFTP): """ @@ -185,8 +186,8 @@ class SFTPClient (BaseSFTP): raise SFTPError('Expected name response') count = msg.get_int() for i in range(count): - filename = _to_unicode(msg.get_string()) - longname = _to_unicode(msg.get_string()) + filename = msg.get_text() + longname = msg.get_text() attr = SFTPAttributes._from_msg(msg, filename, longname) if (filename != '.') and (filename != '..'): filelist.append(attr) @@ -494,7 +495,7 @@ class SFTPClient (BaseSFTP): count = msg.get_int() if count != 1: raise SFTPError('Realpath returned %d results' % count) - return _to_unicode(msg.get_string()) + return msg.get_text() def chdir(self, path): """ @@ -517,7 +518,7 @@ class SFTPClient (BaseSFTP): return if not stat.S_ISDIR(self.stat(path).st_mode): raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) - self._cwd = self.normalize(path).encode('utf-8') + self._cwd = b(self.normalize(path)) def getcwd(self): """ @@ -530,7 +531,7 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ - return self._cwd + return u(self._cwd) def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True): """ @@ -769,15 +770,15 @@ class SFTPClient (BaseSFTP): Return an adjusted path if we're emulating a "current working directory" for the server. """ - path = bytestring(path) + path = b(path) if self._cwd is None: return path if (len(path) > 0) and (path[0] == '/'): # absolute path return path - if self._cwd == '/': + if self._cwd == b_slash: return self._cwd + path - return self._cwd + '/' + path + return self._cwd + b_slash + path class SFTP (SFTPClient): diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 8cc7233..754d038 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -165,7 +165,7 @@ class SFTPFile (BufferedFile): def _write(self, data): # may write less than requested if it would exceed max packet size chunk = min(len(data), self.MAX_REQUEST_SIZE) - self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), str(data[:chunk]))) + self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk])) if not self.pipelined or (len(self._reqs) > 100 and self.sftp.sock.recv_ready()): while len(self._reqs): req = self._reqs.popleft() diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 63fe5cc..19f1ab9 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -183,7 +183,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_int(item) elif type(item) is long: msg.add_int64(item) - elif isinstance(item, string_types): + elif isinstance(item, (string_types, bytes_types)): msg.add_string(item) elif type(item) is SFTPAttributes: item._pack(msg) @@ -196,7 +196,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): # must be error code self._send_status(request_number, handle) return - handle._set_name('hx%d' % self.next_handle) + handle._set_name(b('hx%d' % self.next_handle)) self.next_handle += 1 if folder: self.folder_table[handle._get_name()] = handle From 0677ea76cdb1320fe6d1a08cdfa8c145f3818ab0 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 17:20:27 -0700 Subject: [PATCH 29/56] Fixes for test_sftp --- tests/test_sftp.py | 72 ++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index c80967d..64c727a 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -31,6 +31,7 @@ import warnings import sys import threading import unittest +from tempfile import mkstemp import paramiko from paramiko.common import * @@ -234,7 +235,7 @@ class SFTPTest (unittest.TestCase): sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt') try: f = sftp.open(FOLDER + '/first.txt', 'r') - self.assert_(False, 'no exception on reading nonexistent file') + self.assertTrue(False, 'no exception on reading nonexistent file') except IOError: pass f = sftp.open(FOLDER + '/second.txt', 'r') @@ -264,7 +265,7 @@ class SFTPTest (unittest.TestCase): try: f = sftp.open(FOLDER + '/subfolder/test') # shouldn't be able to create that file - self.assert_(False, 'no exception at dummy file creation') + self.assertTrue(False, 'no exception at dummy file creation') except IOError: pass @@ -285,10 +286,10 @@ class SFTPTest (unittest.TestCase): x = sftp.listdir(FOLDER) self.assertEqual(len(x), 3) - self.assert_('duck.txt' in x) - self.assert_('fish.txt' in x) - self.assert_('tertiary.py' in x) - self.assert_('random' not in x) + self.assertTrue('duck.txt' in x) + self.assertTrue('fish.txt' in x) + self.assertTrue('tertiary.py' in x) + self.assertTrue('random' not in x) finally: sftp.remove(FOLDER + '/duck.txt') sftp.remove(FOLDER + '/fish.txt') @@ -454,7 +455,7 @@ class SFTPTest (unittest.TestCase): self.assertEqual(sftp.stat(FOLDER + '/link.txt').st_size, 9) # the sftp server may be hiding extra path members from us, so the # length may be longer than we expect: - self.assert_(sftp.lstat(FOLDER + '/link2.txt').st_size >= len(abs_path)) + self.assertTrue(sftp.lstat(FOLDER + '/link2.txt').st_size >= len(abs_path)) self.assertEqual(sftp.stat(FOLDER + '/link2.txt').st_size, 9) self.assertEqual(sftp.stat(FOLDER + '/original.txt').st_size, 9) finally: @@ -499,10 +500,10 @@ class SFTPTest (unittest.TestCase): error. """ pwd = sftp.normalize('.') - self.assert_(len(pwd) > 0) + self.assertTrue(len(pwd) > 0) f = sftp.normalize('./' + FOLDER) - self.assert_(len(f) > 0) - self.assertEquals(os.path.join(pwd, FOLDER), f) + self.assertTrue(len(f) > 0) + self.assertEqual(os.path.join(pwd, FOLDER), f) def test_F_mkdir(self): """ @@ -511,19 +512,19 @@ class SFTPTest (unittest.TestCase): try: sftp.mkdir(FOLDER + '/subfolder') except: - self.assert_(False, 'exception creating subfolder') + self.assertTrue(False, 'exception creating subfolder') try: sftp.mkdir(FOLDER + '/subfolder') - self.assert_(False, 'no exception overwriting subfolder') + self.assertTrue(False, 'no exception overwriting subfolder') except IOError: pass try: sftp.rmdir(FOLDER + '/subfolder') except: - self.assert_(False, 'exception removing subfolder') + self.assertTrue(False, 'exception removing subfolder') try: sftp.rmdir(FOLDER + '/subfolder') - self.assert_(False, 'no exception removing nonexistent subfolder') + self.assertTrue(False, 'no exception removing nonexistent subfolder') except IOError: pass @@ -538,17 +539,17 @@ class SFTPTest (unittest.TestCase): sftp.mkdir(FOLDER + '/alpha') sftp.chdir(FOLDER + '/alpha') sftp.mkdir('beta') - self.assertEquals(root + FOLDER + '/alpha', sftp.getcwd()) - self.assertEquals(['beta'], sftp.listdir('.')) + self.assertEqual(root + FOLDER + '/alpha', sftp.getcwd()) + self.assertEqual(['beta'], sftp.listdir('.')) sftp.chdir('beta') f = sftp.open('fish', 'w') f.write('hello\n') f.close() sftp.chdir('..') - self.assertEquals(['fish'], sftp.listdir('beta')) + self.assertEqual(['fish'], sftp.listdir('beta')) sftp.chdir('..') - self.assertEquals(['fish'], sftp.listdir('alpha/beta')) + self.assertEqual(['fish'], sftp.listdir('alpha/beta')) finally: sftp.chdir(root) try: @@ -570,8 +571,9 @@ class SFTPTest (unittest.TestCase): """ warnings.filterwarnings('ignore', 'tempnam.*') - localname = os.tempnam() - text = 'All I wanted was a plastic bunny rabbit.\n' + fd, localname = mkstemp() + os.close(fd) + text = b('All I wanted was a plastic bunny rabbit.\n') f = open(localname, 'wb') f.write(text) f.close() @@ -581,19 +583,20 @@ class SFTPTest (unittest.TestCase): sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) f = sftp.open(FOLDER + '/bunny.txt', 'r') - self.assertEquals(text, f.read(128)) + self.assertEqual(text, f.read(128)) f.close() - self.assertEquals((41, 41), saved_progress[-1]) + self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) - localname = os.tempnam() + fd, localname = mkstemp() + os.close(fd) saved_progress = [] sftp.get(FOLDER + '/bunny.txt', localname, progress_callback) f = open(localname, 'rb') - self.assertEquals(text, f.read(128)) + self.assertEqual(text, f.read(128)) f.close() - self.assertEquals((41, 41), saved_progress[-1]) + self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) sftp.unlink(FOLDER + '/bunny.txt') @@ -611,11 +614,11 @@ class SFTPTest (unittest.TestCase): try: f = sftp.open(FOLDER + '/kitty.txt', 'r') sum = f.check('sha1') - self.assertEquals('91059CFC6615941378D413CB5ADAF4C5EB293402', hexlify(sum).upper()) + self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', hexlify(sum).upper()) sum = f.check('md5', 0, 512) - self.assertEquals('93DE4788FCA28D471516963A1FE3856A', hexlify(sum).upper()) + self.assertEqual('93DE4788FCA28D471516963A1FE3856A', hexlify(sum).upper()) sum = f.check('md5', 0, 0, 510) - self.assertEquals('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', + self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', hexlify(sum).upper()) f.close() finally: @@ -650,7 +653,7 @@ class SFTPTest (unittest.TestCase): sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') except Exception: e = sys.exc_info()[1] - self.fail('exception ' + e) + self.fail('exception ' + str(e)) sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') def test_L_utf8_chdir(self): @@ -689,8 +692,9 @@ class SFTPTest (unittest.TestCase): """ warnings.filterwarnings('ignore', 'tempnam.*') - localname = os.tempnam() - text = 'All I wanted was a plastic bunny rabbit.\n' + fd, localname = mkstemp() + os.close(fd) + text = b('All I wanted was a plastic bunny rabbit.\n') f = open(localname, 'wb') f.write(text) f.close() @@ -699,12 +703,12 @@ class SFTPTest (unittest.TestCase): saved_progress.append((x, y)) res = sftp.put(localname, FOLDER + '/bunny.txt', progress_callback, False) - self.assertEquals(SFTPAttributes().attr, res.attr) + self.assertEqual(SFTPAttributes().attr, res.attr) f = sftp.open(FOLDER + '/bunny.txt', 'r') - self.assertEquals(text, f.read(128)) + self.assertEqual(text, f.read(128)) f.close() - self.assertEquals((41, 41), saved_progress[-1]) + self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) sftp.unlink(FOLDER + '/bunny.txt') From d5ce2b43d602282fe2aa826481759c48d18c9b38 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 31 Oct 2013 18:52:55 -0700 Subject: [PATCH 30/56] More type fixes --- tests/test_sftp_big.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index 379bfe1..b7ffe0b 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -124,7 +124,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file, with no linefeeds, using pipelining. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in range(512)]) + kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) start = time.time() try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') @@ -165,7 +165,7 @@ class BigSFTPTest (unittest.TestCase): def test_4_prefetch_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in range(512)]) + kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -203,7 +203,7 @@ class BigSFTPTest (unittest.TestCase): def test_5_readv_seek(self): sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in range(512)]) + kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -279,7 +279,7 @@ class BigSFTPTest (unittest.TestCase): verify that prefetch and readv don't conflict with each other. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in range(512)]) + kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -318,7 +318,7 @@ class BigSFTPTest (unittest.TestCase): returned as a single blob. """ sftp = get_sftp() - kblob = ''.join([struct.pack('>H', n) for n in range(512)]) + kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -372,7 +372,7 @@ class BigSFTPTest (unittest.TestCase): f.close() self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) - self.assertNotEquals(t.H, t.session_id) + self.assertNotEqual(t.H, t.session_id) # try to read it too. f = sftp.open('%s/hongry.txt' % FOLDER, 'r', 128 * 1024) From 8a7267beeb3435fa16c06d22c201650e1e593d89 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 00:37:11 -0700 Subject: [PATCH 31/56] Eliminate all uses of b'' syntax to allow for Python 2.5 support --- paramiko/common.py | 16 ++++++---------- paramiko/ecdsakey.py | 11 +++-------- paramiko/py3compat.py | 4 +++- paramiko/rsakey.py | 30 ++++++++++-------------------- tests/test_message.py | 15 +++++---------- tests/test_packetizer.py | 21 +++++---------------- tests/test_pkey.py | 7 ++----- tests/test_transport.py | 2 +- 8 files changed, 35 insertions(+), 71 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 5d7ab31..37d5ee8 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -123,19 +123,15 @@ else: import logging PY22 = False +zero_byte = byte_chr(0) +one_byte = byte_chr(1) +four_byte = byte_chr(4) +max_byte = byte_chr(0xff) +newline_byte = byte_chr(10) + if PY3: - zero_byte = b'\x00' - one_byte = b'\x01' - four_byte = b'\x04' - max_byte = b'\xff' - newline_byte = b'\n' cr_byte = 13 else: - zero_byte = '\x00' - one_byte = '\x01' - four_byte = '\x04' - max_byte = '\xff' - newline_byte = '\n' cr_byte = '\r' diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index d15b2ac..8585e6f 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -21,6 +21,7 @@ L{ECDSAKey} """ import binascii +from binascii import unhexlify from ecdsa import SigningKey, VerifyingKey, der, curves from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings from Crypto.Hash import SHA256, MD5 @@ -157,14 +158,8 @@ class ECDSAKey (PKey): data = self._read_private_key('EC', file_obj, password) self._decode_key(data) - if PY3: - ALLOWED_PADDINGS = [b'\x01', b'\x02\x02', b'\x03\x03\x03', b'\x04\x04\x04\x04', - b'\x05\x05\x05\x05\x05', b'\x06\x06\x06\x06\x06\x06', - b'\x07\x07\x07\x07\x07\x07\x07'] - else: - ALLOWED_PADDINGS = ['\x01', '\x02\x02', '\x03\x03\x03', '\x04\x04\x04\x04', - '\x05\x05\x05\x05\x05', '\x06\x06\x06\x06\x06\x06', - '\x07\x07\x07\x07\x07\x07\x07'] + ALLOWED_PADDINGS = [one_byte, byte_chr(2) * 2, byte_chr(3) * 3, byte_chr(4) * 4, + byte_chr(5) * 5, byte_chr(6) * 6, byte_chr(7) * 7] def _decode_key(self, data): s, padding = der.remove_sequence(data) if padding: diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index fd6488d..8a01ba0 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,6 @@ import sys -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -9,6 +9,7 @@ if PY3: import struct string_types = str text_type = str + bytes = bytes bytes_types = bytes integer_types = int long = int @@ -67,6 +68,7 @@ else: string_types = basestring text_type = unicode bytes_types = str + bytes = str integer_types = (int, long) long = long input = raw_input diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index 9094503..4bc94e0 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -20,6 +20,7 @@ L{RSAKey} """ +from binascii import unhexlify from Crypto.PublicKey import RSA from Crypto.Hash import SHA, MD5 from Crypto.Cipher import DES3 @@ -152,26 +153,15 @@ class RSAKey (PKey): ### internals... - if PY3: - def _pkcs1imify(self, data): - """ - turn a 20-byte SHA1 hash into a blob of data as large as the key's N, - using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. - """ - SHA1_DIGESTINFO = b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' - size = len(util.deflate_long(self.n, 0)) - filler = b'\xff' * (size - len(SHA1_DIGESTINFO) - len(data) - 3) - return b'\x00\x01' + filler + b'\x00' + SHA1_DIGESTINFO + data - else: - def _pkcs1imify(self, data): - """ - turn a 20-byte SHA1 hash into a blob of data as large as the key's N, - using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. - """ - SHA1_DIGESTINFO = b('\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14') - size = len(util.deflate_long(self.n, 0)) - filler = b('\xff') * (size - len(SHA1_DIGESTINFO) - len(data) - 3) - return b('\x00\x01') + filler + b('\x00') + SHA1_DIGESTINFO + b(data) + def _pkcs1imify(self, data): + """ + turn a 20-byte SHA1 hash into a blob of data as large as the key's N, + using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. + """ + SHA1_DIGESTINFO = unhexlify('3021300906052b0e03021a05000414') + size = len(util.deflate_long(self.n, 0)) + filler = max_byte * (size - len(SHA1_DIGESTINFO) - len(data) - 3) + return zero_byte + one_byte + filler + zero_byte + SHA1_DIGESTINFO + data def _from_private_key_file(self, filename, password): data = self._read_private_key_file('RSA', filename, password) diff --git a/tests/test_message.py b/tests/test_message.py index 2580115..dde694f 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -21,22 +21,17 @@ Some unit tests for ssh protocol message blocks. """ import unittest +from binascii import unhexlify from paramiko.message import Message from paramiko.common import * class MessageTest (unittest.TestCase): - if PY3: - __a = b'\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + (b'x' * 1000) - __b = b'\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' - __c = b'\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' - __d = b'\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' - else: - __a = '\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01q\x00\x00\x00\x05hello\x00\x00\x03\xe8' + ('x' * 1000) - __b = '\x01\x00\xf3\x00\x3f\x00\x00\x00\x10huey,dewey,louie' - __c = '\x00\x00\x00\x05\xff\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' - __d = '\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03cat\x00\x00\x00\x03a,b' + __a = unhexlify('000000170760e09000000001710000000568656c6c6f000003e8') + (b('x') * 1000) + __b = unhexlify('0100f3003f00000010687565792c64657765792c6c6f756965') + __c = unhexlify('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7') + __d = unhexlify('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62') def test_1_encode(self): msg = Message() diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 111ecb9..b0e8fd4 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -21,18 +21,15 @@ Some unit tests for the ssh2 protocol in Transport. """ import unittest +from binascii import unhexlify from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC from paramiko import Message, Packetizer, util from paramiko.common import * -if PY3: - x55 = b'\x55' - x1f = b'\x1f' -else: - x55 = '\x55' - x1f = '\x1f' +x55 = byte_chr(0x55) +x1f = byte_chr(0x1f) class PacketizerTest (unittest.TestCase): @@ -58,10 +55,7 @@ class PacketizerTest (unittest.TestCase): data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 self.assertEquals(44, len(data)) - if PY3: - self.assertEquals(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) - else: - self.assertEquals('\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) + self.assertEquals(unhexlify('439197bd5b50ac2587c2c46bc7e938c0'), data[:16]) def test_2_read (self): rsock = LoopSocket() @@ -72,12 +66,7 @@ class PacketizerTest (unittest.TestCase): p.set_hexdump(True) cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) - if PY3: - wsock.send(b'C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ - b'\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') - else: - wsock.send('C\x91\x97\xbd[P\xac%\x87\xc2\xc4k\xc7\xe98\xc0' + \ - '\x90\xd2\x16V\rqsa8|L=\xfb\x97}\xe2n\x03\xb1\xa0\xc2\x1c\xd6AAL\xb4Y') + wsock.send(unhexlify('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459')) cmd, m = p.read_message() self.assertEquals(100, cmd) self.assertEquals(100, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index b75f9a1..0000160 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,7 @@ Some unit tests for public/private key objects. from binascii import hexlify, unhexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr, b, PY3 +from paramiko.common import rng, StringIO, byte_chr, b, PY3, bytes from tests.util import test_path # from openssh's ssh-keygen @@ -77,10 +77,7 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g== -----END EC PRIVATE KEY----- """ -if PY3: - x1234 = b'\x01\x02\x03\x04' -else: - x1234 = '\x01\x02\x03\x04' +x1234 = unhexlify('01020304') class KeyTest (unittest.TestCase): diff --git a/tests/test_transport.py b/tests/test_transport.py index 230f4a2..f60c294 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -33,7 +33,7 @@ from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey SSHException, BadAuthenticationType, InteractiveQuery, ChannelException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b +from paramiko.common import MSG_KEXINIT, MSG_CHANNEL_WINDOW_ADJUST, b, bytes from paramiko.message import Message from tests.loop import LoopSocket from tests.util import ParamikoTest, test_path From fee18142a58dce099dd7a9bcb93d124a539cc55c Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 00:51:00 -0700 Subject: [PATCH 32/56] Fixes for Python 2.5 and Python 3.2 support --- paramiko/pkey.py | 2 +- paramiko/rsakey.py | 2 +- tests/test_auth.py | 6 ++---- tests/test_message.py | 8 ++++---- tests/test_packetizer.py | 4 ++-- tests/test_pkey.py | 4 ++-- tests/test_sftp.py | 5 +---- tests/test_transport.py | 2 +- 8 files changed, 14 insertions(+), 19 deletions(-) diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 4cead1c..53361d0 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -330,7 +330,7 @@ class PKey (object): cipher = self._CIPHER_TABLE[encryption_type]['cipher'] keysize = self._CIPHER_TABLE[encryption_type]['keysize'] mode = self._CIPHER_TABLE[encryption_type]['mode'] - salt = unhexlify(saltstr) + salt = unhexlify(b(saltstr)) key = util.generate_key_bytes(MD5, salt, password, keysize) return cipher.new(key, mode, salt).decrypt(data) diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index 4bc94e0..b4222a3 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -158,7 +158,7 @@ class RSAKey (PKey): turn a 20-byte SHA1 hash into a blob of data as large as the key's N, using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. """ - SHA1_DIGESTINFO = unhexlify('3021300906052b0e03021a05000414') + SHA1_DIGESTINFO = unhexlify(b('3021300906052b0e03021a05000414')) size = len(util.deflate_long(self.n, 0)) filler = max_byte * (size - len(SHA1_DIGESTINFO) - len(data) - 3) return zero_byte + one_byte + filler + zero_byte + SHA1_DIGESTINFO + data diff --git a/tests/test_auth.py b/tests/test_auth.py index 5fd0bf5..586289b 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -29,13 +29,11 @@ from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ AuthenticationException from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED +from paramiko.py3compat import u from tests.loop import LoopSocket from tests.util import test_path -try: - _pwd = u'\u2022' -except Exception: - _pwd = '\u2022' +_pwd = u('\u2022') class NullServer (ServerInterface): diff --git a/tests/test_message.py b/tests/test_message.py index dde694f..0aed88c 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -28,10 +28,10 @@ from paramiko.common import * class MessageTest (unittest.TestCase): - __a = unhexlify('000000170760e09000000001710000000568656c6c6f000003e8') + (b('x') * 1000) - __b = unhexlify('0100f3003f00000010687565792c64657765792c6c6f756965') - __c = unhexlify('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7') - __d = unhexlify('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62') + __a = unhexlify(b('000000170760e09000000001710000000568656c6c6f000003e8')) + (b('x') * 1000) + __b = unhexlify(b('0100f3003f00000010687565792c64657765792c6c6f756965')) + __c = unhexlify(b('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')) + __d = unhexlify(b('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')) def test_1_encode(self): msg = Message() diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index b0e8fd4..a4ada72 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -55,7 +55,7 @@ class PacketizerTest (unittest.TestCase): data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 self.assertEquals(44, len(data)) - self.assertEquals(unhexlify('439197bd5b50ac2587c2c46bc7e938c0'), data[:16]) + self.assertEquals(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c0')), data[:16]) def test_2_read (self): rsock = LoopSocket() @@ -66,7 +66,7 @@ class PacketizerTest (unittest.TestCase): p.set_hexdump(True) cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) - wsock.send(unhexlify('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459')) + wsock.send(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459'))) cmd, m = p.read_message() self.assertEquals(100, cmd) self.assertEquals(100, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 0000160..8ab21a3 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -77,7 +77,7 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g== -----END EC PRIVATE KEY----- """ -x1234 = unhexlify('01020304') +x1234 = unhexlify(b('01020304')) class KeyTest (unittest.TestCase): @@ -91,7 +91,7 @@ class KeyTest (unittest.TestCase): def test_1_generate_key_bytes(self): from Crypto.Hash import MD5 key = util.generate_key_bytes(MD5, x1234, 'happy birthday', 30) - exp = unhexlify('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64') + exp = unhexlify(b('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64')) self.assertEquals(exp, key) def test_2_load_rsa(self): diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 64c727a..2b434f8 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,10 +72,7 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True -try: - unicode_folder = u'\u00fcnic\u00f8de' -except SyntaxError: - unicode_folder = '\u00fcnic\u00f8de' +unicode_folder = u('\u00fcnic\u00f8de') def get_sftp(): global sftp diff --git a/tests/test_transport.py b/tests/test_transport.py index f60c294..22a02a8 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -159,7 +159,7 @@ class TransportTest(ParamikoTest): def test_2_compute_key(self): self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929 - self.tc.H = unhexlify('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3') + self.tc.H = unhexlify(b('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3')) self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) self.assertEquals(b('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995'), From 201a61d66dd3c788abb67741de975a56f7df2061 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 01:02:50 -0700 Subject: [PATCH 33/56] Have to use u'' format in test_sftp so this test won't run on Py3.2 unless we find a solution --- tests/test_sftp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 2b434f8..460e04c 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,7 +72,7 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True -unicode_folder = u('\u00fcnic\u00f8de') +unicode_folder = u'\u00fcnic\u00f8de' def get_sftp(): global sftp From 06b866cf406c035ecaffd7a8abd31d6e07b8811a Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 01:06:17 -0700 Subject: [PATCH 34/56] Don't import test_sftp or test_sftp_big unless we are going to do the tests --- test.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test.py b/test.py index 159794c..1162da0 100755 --- a/test.py +++ b/test.py @@ -42,8 +42,6 @@ from tests.test_kex import KexTest from tests.test_packetizer import PacketizerTest from tests.test_auth import AuthTest from tests.test_transport import TransportTest -from tests.test_sftp import SFTPTest -from tests.test_sftp_big import BigSFTPTest from tests.test_client import SSHClientTest default_host = 'localhost' @@ -109,13 +107,16 @@ def main(): paramiko.util.log_to_file('test.log') if options.use_sftp: + from tests.test_sftp import SFTPTest if options.use_loopback_sftp: SFTPTest.init_loopback() else: SFTPTest.init(options.hostname, options.username, options.keyfile, options.password) if not options.use_big_file: SFTPTest.set_big_file_test(False) - + if options.use_big_file: + from tests.test_sftp_big import BigSFTPTest + suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(MessageTest)) suite.addTest(unittest.makeSuite(BufferedFileTest)) From 9662a7f779636f0328263a81cdeb76af25802970 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 09:49:52 -0700 Subject: [PATCH 35/56] Changes inspired by the nischu7 branch --- paramiko/auth_handler.py | 4 ++-- paramiko/ber.py | 4 ++-- paramiko/channel.py | 4 ++-- paramiko/common.py | 2 -- paramiko/dsskey.py | 4 ++-- paramiko/ecdsakey.py | 2 +- paramiko/kex_group1.py | 12 +++++++----- paramiko/packet.py | 5 +++-- paramiko/primes.py | 2 +- paramiko/rsakey.py | 5 +++-- paramiko/sftp.py | 2 +- paramiko/sftp_file.py | 2 +- paramiko/sftp_server.py | 6 +++--- paramiko/transport.py | 11 +++++------ paramiko/util.py | 29 ++++++++++++++++------------- paramiko/win_pageant.py | 3 ++- tests/loop.py | 2 +- tests/test_client.py | 12 +++++++++--- tests/test_sftp.py | 6 +++--- tests/test_sftp_big.py | 8 ++++---- 20 files changed, 68 insertions(+), 57 deletions(-) diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 2a65355..83f27a1 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -245,7 +245,7 @@ class AuthHandler (object): m.add_byte(cMSG_USERAUTH_INFO_REQUEST) m.add_string(q.name) m.add_string(q.instructions) - m.add_string('') + m.add_string(bytes()) m.add_int(len(q.prompts)) for p in q.prompts: m.add_string(p[0]) @@ -375,7 +375,7 @@ class AuthHandler (object): def _parse_userauth_banner(self, m): banner = m.get_string() lang = m.get_string() - self.transport._log(INFO, 'Auth banner: ' + banner) + self.transport._log(INFO, 'Auth banner: %s' % banner) # who cares. def _parse_userauth_info_request(self, m): diff --git a/paramiko/ber.py b/paramiko/ber.py index f4d2acc..c4f3521 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -113,9 +113,9 @@ class BER(object): def encode(self, x): if type(x) is bool: if x: - self.encode_tlv(1, '\xff') + self.encode_tlv(1, max_byte) else: - self.encode_tlv(1, '\x00') + self.encode_tlv(1, zero_byte) elif (type(x) is int) or (type(x) is long): self.encode_tlv(2, util.deflate_long(x)) elif type(x) is str: diff --git a/paramiko/channel.py b/paramiko/channel.py index 6a8a798..9980fce 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -157,7 +157,7 @@ class Channel (object): m.add_int(height) m.add_int(width_pixels) m.add_int(height_pixels) - m.add_string('') + m.add_string(bytes()) self._event_pending() self.transport._send_user_message(m) self._wait_for_event() @@ -477,7 +477,7 @@ class Channel (object): @since: 1.1 """ - data = '' + data = bytes() self.lock.acquire() try: old = self.combine_stderr diff --git a/paramiko/common.py b/paramiko/common.py index 37d5ee8..476ebf5 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -148,8 +148,6 @@ def asbytes(s): xffffffff = long(0xffffffff) x80000000 = long(0x80000000) -long_zero = long(0) -long_one = long(1) o666 = 438 o660 = 432 o644 = 420 diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 715f9f6..4c97b26 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -110,9 +110,9 @@ class DSSKey (PKey): rstr = util.deflate_long(r, 0) sstr = util.deflate_long(s, 0) if len(rstr) < 20: - rstr = '\x00' * (20 - len(rstr)) + rstr + rstr = zero_byte * (20 - len(rstr)) + rstr if len(sstr) < 20: - sstr = '\x00' * (20 - len(sstr)) + sstr + sstr = zero_byte * (20 - len(sstr)) + sstr m.add_string(rstr + sstr) return m diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index 8585e6f..5f9dff2 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -164,7 +164,7 @@ class ECDSAKey (PKey): s, padding = der.remove_sequence(data) if padding: if padding not in self.ALLOWED_PADDINGS: - raise ValueError("weird padding: %s" % (binascii.hexlify(data))) + raise ValueError("weird padding: %s" % u(binascii.hexlify(data))) data = data[:-len(padding)] key = SigningKey.from_der(data) self.signing_key = key diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index ea452b3..05693a1 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -36,6 +36,8 @@ c_MSG_KEXDH_INIT, c_MSG_KEXDH_REPLY = [byte_chr(c) for c in range(30, 32)] P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF G = 2 +b7fffffffffffffff = byte_chr(0x7f) + max_byte * 7 +b0000000000000000 = zero_byte * 8 class KexGroup1(object): @@ -43,9 +45,9 @@ class KexGroup1(object): def __init__(self, transport): self.transport = transport - self.x = long_zero - self.e = long_zero - self.f = long_zero + self.x = long(0) + self.e = long(0) + self.f = long(0) def start_kex(self): self._generate_x() @@ -82,8 +84,8 @@ class KexGroup1(object): while 1: x_bytes = self.transport.rng.read(128) x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] - if (x_bytes[:8] != '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF') and \ - (x_bytes[:8] != '\x00\x00\x00\x00\x00\x00\x00\x00'): + if (x_bytes[:8] != b7fffffffffffffff) and \ + (x_bytes[:8] != b0000000000000000): break self.x = util.inflate_long(x_bytes) diff --git a/paramiko/packet.py b/paramiko/packet.py index fa5ceff..37028bb 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -91,8 +91,8 @@ class Packetizer (object): self.__mac_key_in = bytes() self.__compress_engine_out = None self.__compress_engine_in = None - self.__sequence_number_out = long_zero - self.__sequence_number_in = long_zero + self.__sequence_number_out = 0 + self.__sequence_number_in = 0 # lock around outbound writes (packet computation) self.__write_lock = threading.RLock() @@ -153,6 +153,7 @@ class Packetizer (object): def close(self): self.__closed = True + self.__socket.close() def set_hexdump(self, hexdump): self.__dump_packets = hexdump diff --git a/paramiko/primes.py b/paramiko/primes.py index 144454a..4db6d52 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -125,7 +125,7 @@ class ModulusPack (object): f.close() def get_modulus(self, min, prefer, max): - bitsizes = sorted(self.pack.keys(), key=hash) + bitsizes = sorted(self.pack.keys()) if len(bitsizes) == 0: raise SSHException('no moduli available') good = -1 diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index b4222a3..0a27119 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -32,6 +32,8 @@ from paramiko.ber import BER, BERException from paramiko.pkey import PKey from paramiko.ssh_exception import SSHException +SHA1_DIGESTINFO = unhexlify(b('3021300906052b0e03021a05000414')) + class RSAKey (PKey): """ @@ -92,7 +94,7 @@ class RSAKey (PKey): def sign_ssh_data(self, rpool, data): digest = SHA.new(data).digest() rsa = RSA.construct((long(self.n), long(self.e), long(self.d))) - sig = util.deflate_long(rsa.sign(self._pkcs1imify(digest), '')[0], 0) + sig = util.deflate_long(rsa.sign(self._pkcs1imify(digest), bytes())[0], 0) m = Message() m.add_string('ssh-rsa') m.add_string(sig) @@ -158,7 +160,6 @@ class RSAKey (PKey): turn a 20-byte SHA1 hash into a blob of data as large as the key's N, using PKCS1's \"emsa-pkcs1-v1_5\" encoding. totally bizarre. """ - SHA1_DIGESTINFO = unhexlify(b('3021300906052b0e03021a05000414')) size = len(util.deflate_long(self.n, 0)) filler = max_byte * (size - len(SHA1_DIGESTINFO) - len(data) - 3) return zero_byte + one_byte + filler + zero_byte + SHA1_DIGESTINFO + data diff --git a/paramiko/sftp.py b/paramiko/sftp.py index 4186460..3e05de9 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -186,4 +186,4 @@ class BaseSFTP (object): t = byte_ord(data[0]) #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) return t, data[1:] - return 0, '' + return 0, bytes() diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 754d038..480c371 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -94,7 +94,7 @@ class SFTPFile (BufferedFile): k = [i for i in self._prefetch_reads if i[0] <= offset] if len(k) == 0: return False - k.sort(key=hash) + k.sort(key=lambda x: x[0]) buf_offset, buf_size = k[-1] if buf_offset + buf_size <= offset: # prefetch request ends before this one begins diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 19f1ab9..f591a21 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -272,7 +272,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self._send_status(request_number, SFTP_FAILURE, 'Block size too small') return - sum_out = '' + sum_out = bytes() offset = start while offset < start + length: blocklen = min(block_size, start + length - offset) @@ -342,7 +342,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return data = self.file_table[handle].read(offset, length) - if type(data) is str: + if isinstance(data, (bytes_types, string_types)): if len(data) == 0: self._send_status(request_number, SFTP_EOF) else: @@ -420,7 +420,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_READLINK: path = msg.get_text() resp = self.server.readlink(path) - if isinstance(resp, string_types): + if isinstance(resp, (bytes_types, string_types)): self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) else: self._send_status(request_number, resp) diff --git a/paramiko/transport.py b/paramiko/transport.py index 46e0fe8..2008ecb 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -80,6 +80,7 @@ class SecurityOptions (object): tuple to one of the fields, C{TypeError} will be raised. """ #__slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] + __slots__ = '_transport' def __init__(self, transport): self._transport = transport @@ -402,6 +403,7 @@ class Transport (threading.Thread): @since: 1.5.3 """ + self.sock.close() self.close() def get_security_options(self): @@ -691,7 +693,7 @@ class Transport (threading.Thread): """ return self.open_channel('auth-agent@openssh.com') - def open_forwarded_tcpip_channel(self, src_addr_port, dest_addr_port): + def open_forwarded_tcpip_channel(self, src_addr, dest_addr): """ Request a new channel back to the client, of type C{"forwarded-tcpip"}. This is used after a client has requested port forwarding, for sending @@ -702,9 +704,7 @@ class Transport (threading.Thread): @param dest_addr: local (server) connected address @param dest_port: local (server) connected port """ - src_addr, src_port = src_addr_port - dest_addr, dest_port = dest_addr_port - return self.open_channel('forwarded-tcpip', (dest_addr, dest_port), (src_addr, src_port)) + return self.open_channel('forwarded-tcpip', dest_addr, src_addr) def open_channel(self, kind, dest_addr=None, src_addr=None): """ @@ -805,7 +805,6 @@ class Transport (threading.Thread): """ if not self.active: raise SSHException('SSH session not active') - address = address port = int(port) response = self.global_request('tcpip-forward', (address, port), wait=True) if response is None: @@ -1642,7 +1641,7 @@ class Transport (threading.Thread): self.completion_event.set() if self.auth_handler is not None: self.auth_handler.abort() - for event in list(self.channel_events.values()): + for event in self.channel_events.values(): event.set() try: self.lock.acquire() diff --git a/paramiko/util.py b/paramiko/util.py index 7844fc6..b2ac3f5 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -48,7 +48,7 @@ if sys.version_info < (2,3): def inflate_long(s, always_positive=False): "turns a normalized byte string into a long-int (adapted from Crypto.Util.number)" - out = long_zero + out = long(0) negative = 0 if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80): negative = 1 @@ -60,7 +60,7 @@ def inflate_long(s, always_positive=False): for i in range(0, len(s), 4): out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] if negative: - out -= (long_one << (8 * len(s))) + out -= (long(1) << (8 * len(s))) return out deflate_zero = 0 if PY3 else zero_byte @@ -128,15 +128,18 @@ def safe_string(s): # ''.join([['%%%02X' % ord(c), c][(ord(c) >= 32) and (ord(c) <= 127)] for c in s]) def bit_length(n): - norm = deflate_long(n, 0) - hbyte = byte_ord(norm[0]) - if hbyte == 0: - return 1 - bitlen = len(norm) * 8 - while not (hbyte & 0x80): - hbyte <<= 1 - bitlen -= 1 - return bitlen + try: + return n.bitlength() + except AttributeError: + norm = deflate_long(n, 0) + hbyte = byte_ord(norm[0]) + if hbyte == 0: + return 1 + bitlen = len(norm) * 8 + while not (hbyte & 0x80): + hbyte <<= 1 + bitlen -= 1 + return bitlen def tb_strings(): return ''.join(traceback.format_exception(*sys.exc_info())).split('\n') @@ -276,7 +279,7 @@ def retry_on_signal(function): class Counter (object): """Stateful counter for CTR mode crypto""" - def __init__(self, nbits, initial_value=long_one, overflow=long_zero): + def __init__(self, nbits, initial_value=long(1), overflow=long(0)): self.blocksize = nbits / 8 self.overflow = overflow # start with value - 1 so we don't have to store intermediate values when counting @@ -300,6 +303,6 @@ class Counter (object): self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x) return self.value.tostring() - def new(cls, nbits, initial_value=long_one, overflow=long_zero): + def new(cls, nbits, initial_value=long(1), overflow=long(0)): return cls(nbits, initial_value=initial_value, overflow=overflow) new = classmethod(new) diff --git a/paramiko/win_pageant.py b/paramiko/win_pageant.py index de1cd64..c0d0f4a 100644 --- a/paramiko/win_pageant.py +++ b/paramiko/win_pageant.py @@ -28,6 +28,7 @@ import threading import array import platform import ctypes.wintypes +from paramiko.util import * from . import _winapi @@ -82,7 +83,7 @@ def _query_pageant(msg): with pymap: pymap.write(msg) # Create an array buffer containing the mapped filename - char_buffer = array.array("c", map_name + '\0') + char_buffer = array.array("c", b(map_name) + zero_byte) char_buffer_address, char_buffer_size = char_buffer.buffer_info() # Create a string to use for the SendMessage function call cds = COPYDATASTRUCT(_AGENT_COPYDATA_ID, char_buffer_size, diff --git a/tests/loop.py b/tests/loop.py index b2c73dd..6e933f8 100644 --- a/tests/loop.py +++ b/tests/loop.py @@ -59,7 +59,7 @@ class LoopSocket (object): try: if self.__mate is None: # EOF - return '' + return bytes() if len(self.__in_buffer) == 0: self.__cv.wait(self.__timeout) if len(self.__in_buffer) == 0: diff --git a/tests/test_client.py b/tests/test_client.py index a8d0463..e6d1069 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -207,8 +207,14 @@ class SSHClientTest (unittest.TestCase): self.assert_(p() is not None) del self.tc # hrm, sometimes p isn't cleared right away. why is that? - st = time.time() - while (time.time() - st < 5.0) and (p() is not None): - time.sleep(0.1) + #st = time.time() + #while (time.time() - st < 5.0) and (p() is not None): + # time.sleep(0.1) + + # instead of dumbly waiting for the GC to collect, force a collection + # to see whether the SSHClient object is deallocated correctly + import gc + gc.collect() + self.assert_(p() is None) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 460e04c..20f68d8 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -611,12 +611,12 @@ class SFTPTest (unittest.TestCase): try: f = sftp.open(FOLDER + '/kitty.txt', 'r') sum = f.check('sha1') - self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', hexlify(sum).upper()) + self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', u(hexlify(sum)).upper()) sum = f.check('md5', 0, 512) - self.assertEqual('93DE4788FCA28D471516963A1FE3856A', hexlify(sum).upper()) + self.assertEqual('93DE4788FCA28D471516963A1FE3856A', u(hexlify(sum)).upper()) sum = f.check('md5', 0, 0, 510) self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', - hexlify(sum).upper()) + u(hexlify(sum)).upper()) f.close() finally: sftp.unlink(FOLDER + '/kitty.txt') diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index b7ffe0b..c1d34d7 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -92,7 +92,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file with no buffering. """ sftp = get_sftp() - kblob = (1024 * 'x') + kblob = (1024 * b('x')) start = time.time() try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') @@ -246,7 +246,7 @@ class BigSFTPTest (unittest.TestCase): without using it, to verify that paramiko doesn't get confused. """ sftp = get_sftp() - kblob = (1024 * 'x') + kblob = (1024 * b('x')) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -347,7 +347,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file, with no linefeeds, and a big buffer. """ sftp = get_sftp() - mblob = (1024 * 1024 * 'x') + mblob = (1024 * 1024 * b('x')) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) f.write(mblob) @@ -364,7 +364,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() t = sftp.sock.get_transport() t.packetizer.REKEY_BYTES = 512 * 1024 - k32blob = (32 * 1024 * 'x') + k32blob = (32 * 1024 * b('x')) try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) for i in range(32): From 7decda3297089b2b2e73bb9cd7e577f9b2cb2789 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Fri, 1 Nov 2013 12:32:57 -0700 Subject: [PATCH 36/56] Fix thread stop for Py3 --- test.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test.py b/test.py index 1162da0..c8ac6bc 100755 --- a/test.py +++ b/test.py @@ -29,6 +29,7 @@ import unittest from optparse import OptionParser import paramiko import threading +from paramiko.py3compat import PY3 sys.path.append('tests') @@ -148,7 +149,10 @@ def main(): # TODO: make that not a problem, jeez for thread in threading.enumerate(): if thread is not threading.currentThread(): - thread._Thread__stop() + if PY3: + thread._stop() + else: + thread._Thread__stop() # Exit correctly if not result.wasSuccessful(): sys.exit(1) From 45e65b6e1eb47944a26e4349d41998844c155df5 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 14:56:43 -0700 Subject: [PATCH 37/56] Make sftp.open handle binary and text, more type conversion --- paramiko/client.py | 4 ++-- paramiko/common.py | 10 ++++++--- paramiko/file.py | 49 +++++++++++++++++++++-------------------- paramiko/packet.py | 6 ++--- paramiko/sftp_attr.py | 2 ++ paramiko/sftp_client.py | 4 ++-- paramiko/sftp_file.py | 2 +- paramiko/sftp_server.py | 10 ++++++--- tests/test_client.py | 7 +++--- tests/test_file.py | 11 ++++----- tests/test_sftp.py | 16 ++++++++------ tests/test_sftp_big.py | 28 +++++++++++------------ 12 files changed, 82 insertions(+), 67 deletions(-) diff --git a/paramiko/client.py b/paramiko/client.py index 3881f8d..8800b0f 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -378,8 +378,8 @@ class SSHClient (object): chan.settimeout(timeout) chan.exec_command(command) stdin = chan.makefile('wb', bufsize) - stdout = chan.makefile('rb', bufsize) - stderr = chan.makefile_stderr('rb', bufsize) + stdout = chan.makefile('r', bufsize) + stderr = chan.makefile_stderr('r', bufsize) return stdin, stdout, stderr def invoke_shell(self, term='vt100', width=80, height=24, width_pixels=0, diff --git a/paramiko/common.py b/paramiko/common.py index 476ebf5..223aac1 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -127,12 +127,16 @@ zero_byte = byte_chr(0) one_byte = byte_chr(1) four_byte = byte_chr(4) max_byte = byte_chr(0xff) -newline_byte = byte_chr(10) +cr_byte = byte_chr(13) +linefeed_byte = byte_chr(10) +crlf = cr_byte + linefeed_byte if PY3: - cr_byte = 13 + cr_byte_value = 13 + linefeed_byte_value = 10 else: - cr_byte = '\r' + cr_byte_value = cr_byte + linefeed_byte_value = linefeed_byte def asbytes(s): diff --git a/paramiko/file.py b/paramiko/file.py index 625737f..a0d94ef 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -47,8 +47,8 @@ class BufferedFile (object): self.newlines = None self._flags = 0 self._bufsize = self._DEFAULT_BUFSIZE - self._wbuffer = StringIO() - self._rbuffer = '' + self._wbuffer = BytesIO() + self._rbuffer = bytes() self._at_trailing_cr = False self._closed = False # pos - position within the file, according to the user @@ -89,7 +89,7 @@ class BufferedFile (object): buffering is not turned on. """ self._write_all(self._wbuffer.getvalue()) - self._wbuffer = StringIO() + self._wbuffer = BytesIO() return if PY3: @@ -144,7 +144,7 @@ class BufferedFile (object): if (size is None) or (size < 0): # go for broke result = self._rbuffer - self._rbuffer = '' + self._rbuffer = bytes() self._pos += len(result) while True: try: @@ -156,12 +156,12 @@ class BufferedFile (object): result += new_data self._realpos += len(new_data) self._pos += len(new_data) - return result + return result if self._flags & self.FLAG_BINARY else u(result) if size <= len(self._rbuffer): result = self._rbuffer[:size] self._rbuffer = self._rbuffer[size:] self._pos += len(result) - return result + return result if self._flags & self.FLAG_BINARY else u(result) while len(self._rbuffer) < size: read_size = size - len(self._rbuffer) if self._flags & self.FLAG_BUFFERED: @@ -177,7 +177,7 @@ class BufferedFile (object): result = self._rbuffer[:size] self._rbuffer = self._rbuffer[size:] self._pos += len(result) - return result + return result if self._flags & self.FLAG_BINARY else u(result) def readline(self, size=None): """ @@ -207,11 +207,11 @@ class BufferedFile (object): if self._at_trailing_cr and (self._flags & self.FLAG_UNIVERSAL_NEWLINE) and (len(line) > 0): # edge case: the newline may be '\r\n' and we may have read # only the first '\r' last time. - if line[0] == '\n': + if line[0] == linefeed_byte_value: line = line[1:] - self._record_newline('\r\n') + self._record_newline(crlf) else: - self._record_newline('\r') + self._record_newline(cr_byte) self._at_trailing_cr = False # check size before looking for a linefeed, in case we already have # enough. @@ -221,42 +221,42 @@ class BufferedFile (object): self._rbuffer = line[size:] line = line[:size] self._pos += len(line) - return line + return line if self._flags & self.FLAG_BINARY else u(line) n = size - len(line) else: n = self._bufsize - if ('\n' in line) or ((self._flags & self.FLAG_UNIVERSAL_NEWLINE) and ('\r' in line)): + if (linefeed_byte in line) or ((self._flags & self.FLAG_UNIVERSAL_NEWLINE) and (cr_byte in line)): break try: new_data = self._read(n) except EOFError: new_data = None if (new_data is None) or (len(new_data) == 0): - self._rbuffer = '' + self._rbuffer = bytes() self._pos += len(line) - return line - line += b2s(new_data) + return line if self._flags & self.FLAG_BINARY else u(line) + line += new_data self._realpos += len(new_data) # find the newline - pos = line.find('\n') + pos = line.find(linefeed_byte) if self._flags & self.FLAG_UNIVERSAL_NEWLINE: - rpos = line.find('\r') + rpos = line.find(cr_byte) if (rpos >= 0) and ((rpos < pos) or (pos < 0)): pos = rpos xpos = pos + 1 - if (line[pos] == '\r') and (xpos < len(line)) and (line[xpos] == '\n'): + if (line[pos] == cr_byte_value) and (xpos < len(line)) and (line[xpos] == linefeed_byte_value): xpos += 1 self._rbuffer = line[xpos:] lf = line[pos:xpos] - line = line[:pos] + '\n' - if (len(self._rbuffer) == 0) and (lf == '\r'): + line = line[:pos] + linefeed_byte + if (len(self._rbuffer) == 0) and (lf == cr_byte): # we could read the line up to a '\r' and there could still be a # '\n' following that we read next time. note that and eat it. self._at_trailing_cr = True else: self._record_newline(lf) self._pos += len(line) - return line + return line if self._flags & self.FLAG_BINARY else u(line) def readlines(self, sizehint=None): """ @@ -323,6 +323,7 @@ class BufferedFile (object): @param data: data to write. @type data: str """ + data = b(data) if self._closed: raise IOError('File is closed') if not (self._flags & self.FLAG_WRITE): @@ -333,12 +334,12 @@ class BufferedFile (object): self._wbuffer.write(data) if self._flags & self.FLAG_LINE_BUFFERED: # only scan the new data for linefeed, to avoid wasting time. - last_newline_pos = data.rfind('\n') + last_newline_pos = data.rfind(linefeed_byte) if last_newline_pos >= 0: wbuf = self._wbuffer.getvalue() last_newline_pos += len(wbuf) - len(data) self._write_all(wbuf[:last_newline_pos + 1]) - self._wbuffer = StringIO() + self._wbuffer = BytesIO() self._wbuffer.write(wbuf[last_newline_pos + 1:]) return # even if we're line buffering, if the buffer has grown past the @@ -471,7 +472,7 @@ class BufferedFile (object): return if self.newlines is None: self.newlines = newline - elif (type(self.newlines) is str) and (self.newlines != newline): + elif self.newlines != newline and isinstance(self.newlines, bytes_types): self.newlines = (self.newlines, newline) elif newline not in self.newlines: self.newlines += (newline,) diff --git a/paramiko/packet.py b/paramiko/packet.py index 37028bb..2652188 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -277,12 +277,12 @@ class Packetizer (object): line, so it's okay to attempt large reads. """ buf = self.__remainder - while not newline_byte in buf: + while not linefeed_byte in buf: buf += self._read_timeout(timeout) - n = buf.index(newline_byte) + n = buf.index(linefeed_byte) self.__remainder = buf[n+1:] buf = buf[:n] - if (len(buf) > 0) and (buf[-1] == cr_byte): + if (len(buf) > 0) and (buf[-1] == cr_byte_value): buf = buf[:-1] return u(buf) diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index 5a40f4c..0ed030d 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -221,3 +221,5 @@ class SFTPAttributes (object): return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, self.st_size, datestr, filename) + def asbytes(self): + return b(str(self)) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 11b61f7..31e6697 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -497,7 +497,7 @@ class SFTPClient (BaseSFTP): raise SFTPError('Realpath returned %d results' % count) return msg.get_text() - def chdir(self, path): + def chdir(self, path=None): """ Change the "current directory" of this SFTP session. Since SFTP doesn't really have the concept of a current working directory, this @@ -773,7 +773,7 @@ class SFTPClient (BaseSFTP): path = b(path) if self._cwd is None: return path - if (len(path) > 0) and (path[0] == '/'): + if len(path) and path[0:1] == b_slash: # absolute path return path if self._cwd == b_slash: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 480c371..3b2bcbf 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -218,7 +218,7 @@ class SFTPFile (BufferedFile): self._realpos = self._pos else: self._realpos = self._pos = self._get_size() + offset - self._rbuffer = '' + self._rbuffer = bytes() def stat(self): """ diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index f591a21..2a1a443 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -168,7 +168,11 @@ class SFTPServer (BaseSFTP, SubsystemHandler): if attr._flags & attr.FLAG_AMTIME: os.utime(filename, (attr.st_atime, attr.st_mtime)) if attr._flags & attr.FLAG_SIZE: - open(filename, 'w+').truncate(attr.st_size) + f = open(filename, 'w+') + try: + f.truncate(attr.st_size) + finally: + f.close() set_file_attr = staticmethod(set_file_attr) @@ -234,7 +238,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg.add_int(len(flist)) for attr in flist: msg.add_string(attr.filename) - msg.add_string(str(attr)) + msg.add_string(attr) attr._pack(msg) self._send_packet(CMD_NAME, msg) @@ -282,7 +286,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): hash_obj = alg.new() while count < blocklen: data = f.read(offset, chunklen) - if not type(data) is str: + if not isinstance(data, bytes_types): self._send_status(request_number, data, 'Unable to hash file') return hash_obj.update(data) diff --git a/tests/test_client.py b/tests/test_client.py index e6d1069..b77b90d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -204,8 +204,10 @@ class SSHClientTest (unittest.TestCase): self.assert_(self.ts.is_active()) p = weakref.ref(self.tc._transport.packetizer) - self.assert_(p() is not None) + self.assertTrue(p() is not None) + self.tc.close() del self.tc + # hrm, sometimes p isn't cleared right away. why is that? #st = time.time() #while (time.time() - st < 5.0) and (p() is not None): @@ -216,5 +218,4 @@ class SSHClientTest (unittest.TestCase): import gc gc.collect() - self.assert_(p() is None) - + self.assertTrue(p() is None) diff --git a/tests/test_file.py b/tests/test_file.py index 6cb3507..0430040 100755 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -22,6 +22,7 @@ Some unit tests for the BufferedFile abstraction. import unittest from paramiko.file import BufferedFile +from paramiko.common import * class LoopbackFile (BufferedFile): @@ -31,7 +32,7 @@ class LoopbackFile (BufferedFile): def __init__(self, mode='r', bufsize=-1): BufferedFile.__init__(self) self._set_mode(mode, bufsize) - self.buffer = '' + self.buffer = bytes() def _read(self, size): if len(self.buffer) == 0: @@ -83,9 +84,9 @@ class BufferedFileTest (unittest.TestCase): self.assert_(False, 'no exception on readline of closed file') except IOError: pass - self.assert_('\n' in f.newlines) - self.assert_('\r\n' in f.newlines) - self.assert_('\r' not in f.newlines) + self.assert_(linefeed_byte in f.newlines) + self.assert_(crlf in f.newlines) + self.assert_(cr_byte not in f.newlines) def test_3_lf(self): """ @@ -97,7 +98,7 @@ class BufferedFileTest (unittest.TestCase): f.write('\nSecond.\r\n') self.assertEqual(f.readline(), 'Second.\n') f.close() - self.assertEqual(f.newlines, '\r\n') + self.assertEqual(f.newlines, crlf) def test_4_write(self): """ diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 20f68d8..c17defa 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,7 +72,8 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True -unicode_folder = u'\u00fcnic\u00f8de' +unicode_folder = eval(compile(r"'\u00fcnic\u00f8de'" if PY3 else r"u'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) +utf8_folder = eval(compile(r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY3 else r"'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) def get_sftp(): global sftp @@ -151,6 +152,7 @@ class SFTPTest (unittest.TestCase): pass def tearDown(self): + #sftp.chdir() sftp.rmdir(FOLDER) def test_1_file(self): @@ -579,7 +581,7 @@ class SFTPTest (unittest.TestCase): saved_progress.append((x, y)) sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) - f = sftp.open(FOLDER + '/bunny.txt', 'r') + f = sftp.open(FOLDER + '/bunny.txt', 'rb') self.assertEqual(text, f.read(128)) f.close() self.assertEqual((41, 41), saved_progress[-1]) @@ -647,11 +649,11 @@ class SFTPTest (unittest.TestCase): try: sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) - sftp.open(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65', 'r') + sftp.open(b(FOLDER) + utf8_folder, 'r') except Exception: e = sys.exc_info()[1] self.fail('exception ' + str(e)) - sftp.unlink(FOLDER + '/\xc3\xbcnic\xc3\xb8\x64\x65') + sftp.unlink(b(FOLDER) + utf8_folder) def test_L_utf8_chdir(self): sftp.mkdir(FOLDER + '/' + unicode_folder) @@ -662,7 +664,7 @@ class SFTPTest (unittest.TestCase): f.close() sftp.unlink('something') finally: - sftp.chdir(None) + sftp.chdir() sftp.rmdir(FOLDER + '/' + unicode_folder) def test_M_bad_readv(self): @@ -691,8 +693,8 @@ class SFTPTest (unittest.TestCase): fd, localname = mkstemp() os.close(fd) - text = b('All I wanted was a plastic bunny rabbit.\n') - f = open(localname, 'wb') + text = 'All I wanted was a plastic bunny rabbit.\n' + f = open(localname, 'w') f.write(text) f.close() saved_progress = [] diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index c1d34d7..a53a6c3 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -92,7 +92,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file with no buffering. """ sftp = get_sftp() - kblob = (1024 * b('x')) + kblob = (1024 * 'x') start = time.time() try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') @@ -127,7 +127,7 @@ class BigSFTPTest (unittest.TestCase): kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) start = time.time() try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') + f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') f.set_pipelined(True) for n in range(1024): f.write(kblob) @@ -141,7 +141,7 @@ class BigSFTPTest (unittest.TestCase): sys.stderr.write('%ds ' % round(end - start)) start = time.time() - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') f.prefetch() # read on odd boundaries to make sure the bytes aren't getting scrambled @@ -167,7 +167,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') + f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') f.set_pipelined(True) for n in range(1024): f.write(kblob) @@ -182,7 +182,7 @@ class BigSFTPTest (unittest.TestCase): k2blob = kblob + kblob chunk = 793 for i in range(10): - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') f.prefetch() base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) offsets = [base_offset + j * chunk for j in range(100)] @@ -205,7 +205,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') + f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') f.set_pipelined(True) for n in range(1024): f.write(kblob) @@ -220,7 +220,7 @@ class BigSFTPTest (unittest.TestCase): k2blob = kblob + kblob chunk = 793 for i in range(10): - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) # make a bunch of offsets and put them in random order offsets = [base_offset + j * chunk for j in range(100)] @@ -246,7 +246,7 @@ class BigSFTPTest (unittest.TestCase): without using it, to verify that paramiko doesn't get confused. """ sftp = get_sftp() - kblob = (1024 * b('x')) + kblob = (1024 * 'x') try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w') f.set_pipelined(True) @@ -281,7 +281,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') + f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') f.set_pipelined(True) for n in range(1024): f.write(kblob) @@ -292,7 +292,7 @@ class BigSFTPTest (unittest.TestCase): self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') f.prefetch() data = f.read(1024) self.assertEqual(data, kblob) @@ -320,7 +320,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') + f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') f.set_pipelined(True) for n in range(1024): f.write(kblob) @@ -331,7 +331,7 @@ class BigSFTPTest (unittest.TestCase): self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') data = list(f.readv([(23 * 1024, 128 * 1024)])) self.assertEqual(1, len(data)) data = data[0] @@ -347,7 +347,7 @@ class BigSFTPTest (unittest.TestCase): write a 1MB file, with no linefeeds, and a big buffer. """ sftp = get_sftp() - mblob = (1024 * 1024 * b('x')) + mblob = (1024 * 1024 * 'x') try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) f.write(mblob) @@ -364,7 +364,7 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() t = sftp.sock.get_transport() t.packetizer.REKEY_BYTES = 512 * 1024 - k32blob = (32 * 1024 * b('x')) + k32blob = (32 * 1024 * 'x') try: f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) for i in range(32): From 7444a999931cddc1e61bb35270468aa45da2687e Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 20:18:18 -0700 Subject: [PATCH 38/56] Fix some deprecation and resource warnings --- demos/demo_server.py | 4 +- demos/demo_sftp.py | 8 +- paramiko/hostkeys.py | 50 +++++----- paramiko/pkey.py | 22 +++-- paramiko/primes.py | 20 ++-- paramiko/py3compat.py | 12 ++- tests/test_auth.py | 34 +++---- tests/test_buffered_pipe.py | 28 +++--- tests/test_client.py | 60 ++++++------ tests/test_file.py | 12 +-- tests/test_hostkeys.py | 48 +++++----- tests/test_kex.py | 82 ++++++++--------- tests/test_message.py | 54 +++++------ tests/test_packetizer.py | 12 +-- tests/test_pkey.py | 112 +++++++++++------------ tests/test_sftp.py | 4 +- tests/test_sftp_big.py | 1 + tests/test_transport.py | 176 ++++++++++++++++++------------------ tests/test_util.py | 34 +++---- 19 files changed, 398 insertions(+), 375 deletions(-) diff --git a/demos/demo_server.py b/demos/demo_server.py index deb2138..5a41a71 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -27,7 +27,7 @@ import threading import traceback import paramiko -from paramiko.py3compat import b, u +from paramiko.py3compat import b, u, decodebytes # setup logging @@ -46,7 +46,7 @@ class Server (paramiko.ServerInterface): 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ 'UWT10hcuO4Ks8=') - good_pub_key = paramiko.RSAKey(data=base64.decodestring(data)) + good_pub_key = paramiko.RSAKey(data=decodebytes(data)) def __init__(self): self.event = threading.Event() diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 2dba172..d7f2808 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -20,6 +20,8 @@ # based on code provided by raymond mosteller (thanks!) +from __future__ import with_statement + import base64 import getpass import os @@ -95,13 +97,15 @@ try: except IOError: print('(assuming demo_sftp_folder/ already exists)') sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') - data = open('demo_sftp.py', 'r').read() + with open('demo_sftp.py', 'r') as f: + data = f.read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) print('created demo_sftp_folder/ on the server') # copy the README back here data = sftp.open('demo_sftp_folder/README', 'r').read() - open('README_demo_sftp', 'w').write(data) + with open('README_demo_sftp', 'w') as f: + f.write(data) print('copied README back here') # BETTER: use the get() and put() methods diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index a7f9b43..9da883e 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -83,11 +83,11 @@ class HostKeyEntry: try: key = b(key) if keytype == 'ssh-rsa': - key = RSAKey(data=base64.decodestring(key)) + key = RSAKey(data=decodebytes(key)) elif keytype == 'ssh-dss': - key = DSSKey(data=base64.decodestring(key)) + key = DSSKey(data=decodebytes(key)) elif keytype == 'ecdsa-sha2-nistp256': - key = ECDSAKey(data=base64.decodestring(key)) + key = ECDSAKey(data=decodebytes(key)) else: log.info("Unable to handle key of type %s" % (keytype,)) return None @@ -173,19 +173,21 @@ class HostKeys (MutableMapping): @raise IOError: if there was an error reading the file """ f = open(filename, 'r') - for lineno, line in enumerate(f): - line = line.strip() - if (len(line) == 0) or (line[0] == '#'): - continue - e = HostKeyEntry.from_line(line, lineno) - if e is not None: - _hostnames = e.hostnames - for h in _hostnames: - if self.check(h, e.key): - e.hostnames.remove(h) - if len(e.hostnames): - self._entries.append(e) - f.close() + try: + for lineno, line in enumerate(f): + line = line.strip() + if (len(line) == 0) or (line[0] == '#'): + continue + e = HostKeyEntry.from_line(line, lineno) + if e is not None: + _hostnames = e.hostnames + for h in _hostnames: + if self.check(h, e.key): + e.hostnames.remove(h) + if len(e.hostnames): + self._entries.append(e) + finally: + f.close() def save(self, filename): """ @@ -202,11 +204,13 @@ class HostKeys (MutableMapping): @since: 1.6.1 """ f = open(filename, 'w') - for e in self._entries: - line = e.to_line() - if line: - f.write(line) - f.close() + try: + for e in self._entries: + line = e.to_line() + if line: + f.write(line) + finally: + f.close() def lookup(self, hostname): """ @@ -362,10 +366,10 @@ class HostKeys (MutableMapping): else: if salt.startswith('|1|'): salt = salt.split('|')[2] - salt = base64.decodestring(b(salt)) + salt = decodebytes(b(salt)) assert len(salt) == SHA.digest_size hmac = HMAC.HMAC(salt, b(hostname), SHA).digest() - hostkey = '|1|%s|%s' % (u(base64.encodestring(salt)), u(base64.encodestring(hmac))) + hostkey = '|1|%s|%s' % (u(encodebytes(salt)), u(encodebytes(hmac))) return hostkey.replace('\n', '') hash_host = staticmethod(hash_host) diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 53361d0..3d786ae 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -148,7 +148,7 @@ class PKey (object): @return: a base64 string containing the public part of the key. @rtype: str """ - return u(base64.encodestring(self.asbytes())).replace('\n', '') + return u(encodebytes(self.asbytes())).replace('\n', '') def sign_ssh_data(self, rng, data): """ @@ -283,8 +283,10 @@ class PKey (object): @raise SSHException: if the key file is invalid. """ f = open(filename, 'r') - data = self._read_private_key(tag, f, password) - f.close() + try: + data = self._read_private_key(tag, f, password) + finally: + f.close() return data def _read_private_key(self, tag, f, password=None): @@ -309,7 +311,7 @@ class PKey (object): end += 1 # if we trudged to the end of the file, just try to cope. try: - data = base64.decodestring(b(''.join(lines[start:end]))) + data = decodebytes(b(''.join(lines[start:end]))) except base64.binascii.Error: raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) if 'proc-type' not in headers: @@ -353,10 +355,12 @@ class PKey (object): @raise IOError: if there was an error writing the file. """ f = open(filename, 'w', o600) - # grrr... the mode doesn't always take hold - os.chmod(filename, o600) - self._write_private_key(tag, f, data, password) - f.close() + try: + # grrr... the mode doesn't always take hold + os.chmod(filename, o600) + self._write_private_key(tag, f, data, password) + finally: + f.close() def _write_private_key(self, tag, f, data, password=None): f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag) @@ -378,7 +382,7 @@ class PKey (object): f.write('Proc-Type: 4,ENCRYPTED\n') f.write('DEK-Info: %s,%s\n' % (cipher_name, u(hexlify(salt)).upper())) f.write('\n') - s = u(base64.encodestring(data)) + s = u(encodebytes(data)) # re-wrap to 64-char lines s = ''.join(s.split('\n')) s = '\n'.join([s[i : i+64] for i in range(0, len(s), 64)]) diff --git a/paramiko/primes.py b/paramiko/primes.py index 4db6d52..13ec52d 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -114,15 +114,17 @@ class ModulusPack (object): """ self.pack = {} f = open(filename, 'r') - for line in f: - line = line.strip() - if (len(line) == 0) or (line[0] == '#'): - continue - try: - self._parse_modulus(line) - except: - continue - f.close() + try: + for line in f: + line = line.strip() + if (len(line) == 0) or (line[0] == '#'): + continue + try: + self._parse_modulus(line) + except: + continue + finally: + f.close() def get_modulus(self, min, prefer, max): bitsizes = sorted(self.pack.keys()) diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index 8a01ba0..0aad361 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,6 +1,9 @@ import sys +import base64 -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] +__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', + 'decodebytes', 'encodebytes', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', + 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] PY3 = sys.version_info[0] >= 3 @@ -12,8 +15,11 @@ if PY3: bytes = bytes bytes_types = bytes integer_types = int - long = int + class long(int): + pass input = input + decodebytes = base64.decodebytes + encodebytes = base64.encodebytes def bytestring(s): return s @@ -72,6 +78,8 @@ else: integer_types = (int, long) long = long input = raw_input + decodebytes = base64.decodestring + encodebytes = base64.encodestring def bytestring(s): # NOQA if isinstance(s, unicode): diff --git a/tests/test_auth.py b/tests/test_auth.py index 586289b..d26b180 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -119,13 +119,13 @@ class AuthTest (unittest.TestCase): self.ts.add_server_key(host_key) self.event = threading.Event() self.server = NullServer() - self.assert_(not self.event.isSet()) + self.assertTrue(not self.event.isSet()) self.ts.start_server(self.event, self.server) def verify_finished(self): self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) def test_1_bad_auth_type(self): """ @@ -136,11 +136,11 @@ class AuthTest (unittest.TestCase): try: self.tc.connect(hostkey=self.public_host_key, username='unknown', password='error') - self.assert_(False) + self.assertTrue(False) except: etype, evalue, etb = sys.exc_info() - self.assertEquals(BadAuthenticationType, etype) - self.assertEquals(['publickey'], evalue.allowed_types) + self.assertEqual(BadAuthenticationType, etype) + self.assertEqual(['publickey'], evalue.allowed_types) def test_2_bad_password(self): """ @@ -151,10 +151,10 @@ class AuthTest (unittest.TestCase): self.tc.connect(hostkey=self.public_host_key) try: self.tc.auth_password(username='slowdive', password='error') - self.assert_(False) + self.assertTrue(False) except: etype, evalue, etb = sys.exc_info() - self.assert_(issubclass(etype, AuthenticationException)) + self.assertTrue(issubclass(etype, AuthenticationException)) self.tc.auth_password(username='slowdive', password='pygmalion') self.verify_finished() @@ -165,10 +165,10 @@ class AuthTest (unittest.TestCase): self.start_server() self.tc.connect(hostkey=self.public_host_key) remain = self.tc.auth_password(username='paranoid', password='paranoid') - self.assertEquals(['publickey'], remain) + self.assertEqual(['publickey'], remain) key = DSSKey.from_private_key_file(test_path('test_dss.key')) remain = self.tc.auth_publickey(username='paranoid', key=key) - self.assertEquals([], remain) + self.assertEqual([], remain) self.verify_finished() def test_4_interactive_auth(self): @@ -184,9 +184,9 @@ class AuthTest (unittest.TestCase): self.got_prompts = prompts return ['cat'] remain = self.tc.auth_interactive('commie', handler) - self.assertEquals(self.got_title, 'password') - self.assertEquals(self.got_prompts, [('Password', False)]) - self.assertEquals([], remain) + self.assertEqual(self.got_title, 'password') + self.assertEqual(self.got_prompts, [('Password', False)]) + self.assertEqual([], remain) self.verify_finished() def test_5_interactive_auth_fallback(self): @@ -197,7 +197,7 @@ class AuthTest (unittest.TestCase): self.start_server() self.tc.connect(hostkey=self.public_host_key) remain = self.tc.auth_password('commie', 'cat') - self.assertEquals([], remain) + self.assertEqual([], remain) self.verify_finished() def test_6_auth_utf8(self): @@ -207,7 +207,7 @@ class AuthTest (unittest.TestCase): self.start_server() self.tc.connect(hostkey=self.public_host_key) remain = self.tc.auth_password('utf8', _pwd) - self.assertEquals([], remain) + self.assertEqual([], remain) self.verify_finished() def test_7_auth_non_utf8(self): @@ -218,7 +218,7 @@ class AuthTest (unittest.TestCase): self.start_server() self.tc.connect(hostkey=self.public_host_key) remain = self.tc.auth_password('non-utf8', '\xff') - self.assertEquals([], remain) + self.assertEqual([], remain) self.verify_finished() def test_8_auth_gets_disconnected(self): @@ -232,4 +232,4 @@ class AuthTest (unittest.TestCase): remain = self.tc.auth_password('bad-server', 'hello') except: etype, evalue, etb = sys.exc_info() - self.assert_(issubclass(etype, AuthenticationException)) + self.assertTrue(issubclass(etype, AuthenticationException)) diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index 7f48b70..5a088d8 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -45,39 +45,39 @@ def close_thread(pipe): class BufferedPipeTest(ParamikoTest): def test_1_buffered_pipe(self): p = BufferedPipe() - self.assert_(not p.read_ready()) + self.assertTrue(not p.read_ready()) p.feed('hello.') - self.assert_(p.read_ready()) + self.assertTrue(p.read_ready()) data = p.read(6) - self.assertEquals(b('hello.'), data) + self.assertEqual(b('hello.'), data) p.feed('plus/minus') - self.assertEquals(b('plu'), p.read(3)) - self.assertEquals(b('s/m'), p.read(3)) - self.assertEquals(b('inus'), p.read(4)) + self.assertEqual(b('plu'), p.read(3)) + self.assertEqual(b('s/m'), p.read(3)) + self.assertEqual(b('inus'), p.read(4)) p.close() - self.assert_(not p.read_ready()) - self.assertEquals(b(''), p.read(1)) + self.assertTrue(not p.read_ready()) + self.assertEqual(b(''), p.read(1)) def test_2_delay(self): p = BufferedPipe() - self.assert_(not p.read_ready()) + self.assertTrue(not p.read_ready()) threading.Thread(target=delay_thread, args=(p,)).start() - self.assertEquals(b('a'), p.read(1, 0.1)) + self.assertEqual(b('a'), p.read(1, 0.1)) try: p.read(1, 0.1) - self.assert_(False) + self.assertTrue(False) except PipeTimeout: pass - self.assertEquals(b('b'), p.read(1, 1.0)) - self.assertEquals(b(''), p.read(1)) + self.assertEqual(b('b'), p.read(1, 1.0)) + self.assertEqual(b(''), p.read(1)) def test_3_close_while_reading(self): p = BufferedPipe() threading.Thread(target=close_thread, args=(p,)).start() data = p.read(1, 1.0) - self.assertEquals(b(''), data) + self.assertEqual(b(''), data) def test_4_or_pipe(self): p = pipe.make_pipe() diff --git a/tests/test_client.py b/tests/test_client.py index b77b90d..e96a426 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -95,10 +95,10 @@ class SSHClientTest (unittest.TestCase): self.tc.connect(self.addr, self.port, username='slowdive', password='pygmalion') self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) - self.assertEquals('slowdive', self.ts.get_username()) - self.assertEquals(True, self.ts.is_authenticated()) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) + self.assertEqual('slowdive', self.ts.get_username()) + self.assertEqual(True, self.ts.is_authenticated()) stdin, stdout, stderr = self.tc.exec_command('yes') schan = self.ts.accept(1.0) @@ -107,10 +107,10 @@ class SSHClientTest (unittest.TestCase): schan.send_stderr('This is on stderr.\n') schan.close() - self.assertEquals('Hello there.\n', stdout.readline()) - self.assertEquals('', stdout.readline()) - self.assertEquals('This is on stderr.\n', stderr.readline()) - self.assertEquals('', stderr.readline()) + self.assertEqual('Hello there.\n', stdout.readline()) + self.assertEqual('', stdout.readline()) + self.assertEqual('This is on stderr.\n', stderr.readline()) + self.assertEqual('', stderr.readline()) stdin.close() stdout.close() @@ -128,10 +128,10 @@ class SSHClientTest (unittest.TestCase): self.tc.connect(self.addr, self.port, username='slowdive', key_filename=test_path('test_dss.key')) self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) - self.assertEquals('slowdive', self.ts.get_username()) - self.assertEquals(True, self.ts.is_authenticated()) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) + self.assertEqual('slowdive', self.ts.get_username()) + self.assertEqual(True, self.ts.is_authenticated()) stdin, stdout, stderr = self.tc.exec_command('yes') schan = self.ts.accept(1.0) @@ -140,10 +140,10 @@ class SSHClientTest (unittest.TestCase): schan.send_stderr('This is on stderr.\n') schan.close() - self.assertEquals('Hello there.\n', stdout.readline()) - self.assertEquals('', stdout.readline()) - self.assertEquals('This is on stderr.\n', stderr.readline()) - self.assertEquals('', stderr.readline()) + self.assertEqual('Hello there.\n', stdout.readline()) + self.assertEqual('', stdout.readline()) + self.assertEqual('This is on stderr.\n', stderr.readline()) + self.assertEqual('', stderr.readline()) stdin.close() stdout.close() @@ -161,10 +161,10 @@ class SSHClientTest (unittest.TestCase): self.tc.connect(self.addr, self.port, username='slowdive', key_filename=[ test_path('test_rsa.key'), test_path('test_dss.key') ]) self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) - self.assertEquals('slowdive', self.ts.get_username()) - self.assertEquals(True, self.ts.is_authenticated()) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) + self.assertEqual('slowdive', self.ts.get_username()) + self.assertEqual(True, self.ts.is_authenticated()) def test_4_auto_add_policy(self): """ @@ -175,16 +175,16 @@ class SSHClientTest (unittest.TestCase): self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.assertEquals(0, len(self.tc.get_host_keys())) + self.assertEqual(0, len(self.tc.get_host_keys())) self.tc.connect(self.addr, self.port, username='slowdive', password='pygmalion') self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) - self.assertEquals('slowdive', self.ts.get_username()) - self.assertEquals(True, self.ts.is_authenticated()) - self.assertEquals(1, len(self.tc.get_host_keys())) - self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) + self.assertEqual('slowdive', self.ts.get_username()) + self.assertEqual(True, self.ts.is_authenticated()) + self.assertEqual(1, len(self.tc.get_host_keys())) + self.assertEqual(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) def test_5_cleanup(self): """ @@ -196,12 +196,12 @@ class SSHClientTest (unittest.TestCase): self.tc = paramiko.SSHClient() self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.assertEquals(0, len(self.tc.get_host_keys())) + self.assertEqual(0, len(self.tc.get_host_keys())) self.tc.connect(self.addr, self.port, username='slowdive', password='pygmalion') self.event.wait(1.0) - self.assert_(self.event.isSet()) - self.assert_(self.ts.is_active()) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) p = weakref.ref(self.tc._transport.packetizer) self.assertTrue(p() is not None) diff --git a/tests/test_file.py b/tests/test_file.py index 0430040..33a4913 100755 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -54,7 +54,7 @@ class BufferedFileTest (unittest.TestCase): f = LoopbackFile('r') try: f.write('hi') - self.assert_(False, 'no exception on write to read-only file') + self.assertTrue(False, 'no exception on write to read-only file') except: pass f.close() @@ -62,7 +62,7 @@ class BufferedFileTest (unittest.TestCase): f = LoopbackFile('w') try: f.read(1) - self.assert_(False, 'no exception to read from write-only file') + self.assertTrue(False, 'no exception to read from write-only file') except: pass f.close() @@ -81,12 +81,12 @@ class BufferedFileTest (unittest.TestCase): f.close() try: f.readline() - self.assert_(False, 'no exception on readline of closed file') + self.assertTrue(False, 'no exception on readline of closed file') except IOError: pass - self.assert_(linefeed_byte in f.newlines) - self.assert_(crlf in f.newlines) - self.assert_(cr_byte not in f.newlines) + self.assertTrue(linefeed_byte in f.newlines) + self.assertTrue(crlf in f.newlines) + self.assertTrue(cr_byte not in f.newlines) def test_3_lf(self): """ diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index 66b41b6..a7621d8 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -25,7 +25,7 @@ from binascii import hexlify import os import unittest import paramiko -from paramiko.py3compat import b +from paramiko.py3compat import b, decodebytes test_hosts_file = """\ @@ -65,42 +65,42 @@ class HostKeysTest (unittest.TestCase): def test_1_load(self): hostdict = paramiko.HostKeys('hostfile.temp') - self.assertEquals(2, len(hostdict)) - self.assertEquals(1, len(list(hostdict.values())[0])) - self.assertEquals(1, len(list(hostdict.values())[1])) + self.assertEqual(2, len(hostdict)) + self.assertEqual(1, len(list(hostdict.values())[0])) + self.assertEqual(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) def test_2_add(self): hostdict = paramiko.HostKeys('hostfile.temp') hh = '|1|BMsIC6cUIP2zBuXR3t2LRcJYjzM=|hpkJMysjTk/+zzUUzxQEa2ieq6c=' - key = paramiko.RSAKey(data=base64.decodestring(keyblob)) + key = paramiko.RSAKey(data=decodebytes(keyblob)) hostdict.add(hh, 'ssh-rsa', key) - self.assertEquals(3, len(list(hostdict))) + self.assertEqual(3, len(list(hostdict))) x = hostdict['foo.example.com'] fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals(b('7EC91BB336CB6D810B124B1353C32396'), fp) - self.assert_(hostdict.check('foo.example.com', key)) + self.assertEqual(b('7EC91BB336CB6D810B124B1353C32396'), fp) + self.assertTrue(hostdict.check('foo.example.com', key)) def test_3_dict(self): hostdict = paramiko.HostKeys('hostfile.temp') - self.assert_('secure.example.com' in hostdict) - self.assert_('not.example.com' not in hostdict) - self.assert_('secure.example.com' in hostdict) - self.assert_('not.example.com' not in hostdict) + self.assertTrue('secure.example.com' in hostdict) + self.assertTrue('not.example.com' not in hostdict) + self.assertTrue('secure.example.com' in hostdict) + self.assertTrue('not.example.com' not in hostdict) x = hostdict.get('secure.example.com', None) - self.assert_(x is not None) + self.assertTrue(x is not None) fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) i = 0 for key in hostdict: i += 1 - self.assertEquals(2, i) + self.assertEqual(2, i) def test_4_dict_set(self): hostdict = paramiko.HostKeys('hostfile.temp') - key = paramiko.RSAKey(data=base64.decodestring(keyblob)) - key_dss = paramiko.DSSKey(data=base64.decodestring(keyblob_dss)) + key = paramiko.RSAKey(data=decodebytes(keyblob)) + key_dss = paramiko.DSSKey(data=decodebytes(keyblob_dss)) hostdict['secure.example.com'] = { 'ssh-rsa': key, 'ssh-dss': key_dss @@ -108,11 +108,11 @@ class HostKeysTest (unittest.TestCase): hostdict['fake.example.com'] = {} hostdict['fake.example.com']['ssh-rsa'] = key - self.assertEquals(3, len(hostdict)) - self.assertEquals(2, len(list(hostdict.values())[0])) - self.assertEquals(1, len(list(hostdict.values())[1])) - self.assertEquals(1, len(list(hostdict.values())[2])) + self.assertEqual(3, len(hostdict)) + self.assertEqual(2, len(list(hostdict.values())[0])) + self.assertEqual(1, len(list(hostdict.values())[1])) + self.assertEqual(1, len(list(hostdict.values())[2])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals(b('7EC91BB336CB6D810B124B1353C32396'), fp) + self.assertEqual(b('7EC91BB336CB6D810B124B1353C32396'), fp) fp = hexlify(hostdict['secure.example.com']['ssh-dss'].get_fingerprint()).upper() - self.assertEquals(b('4478F0B9A23CC5182009FF755BC1D26C'), fp) + self.assertEqual(b('4478F0B9A23CC5182009FF755BC1D26C'), fp) diff --git a/tests/test_kex.py b/tests/test_kex.py index f7cb064..dbe377a 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -92,8 +92,8 @@ class KexTest (unittest.TestCase): kex = KexGroup1(transport) kex.start_kex() x = b('1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_group1._MSG_KEXDH_REPLY,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_group1._MSG_KEXDH_REPLY,), transport._expect) # fake "reply" msg = Message() @@ -103,17 +103,17 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_group1._MSG_KEXDH_REPLY, msg) H = b('03079780F3D3AD0B3C6DB30C8D21685F367A86D2') - self.assertEquals(self.K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) - self.assert_(transport._activated) + self.assertEqual(self.K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertTrue(transport._activated) def test_2_group1_server(self): transport = FakeTransport() transport.server_mode = True kex = KexGroup1(transport) kex.start_kex() - self.assertEquals((paramiko.kex_group1._MSG_KEXDH_INIT,), transport._expect) + self.assertEqual((paramiko.kex_group1._MSG_KEXDH_INIT,), transport._expect) msg = Message() msg.add_mpint(69) @@ -121,10 +121,10 @@ class KexTest (unittest.TestCase): kex.parse_next(paramiko.kex_group1._MSG_KEXDH_INIT, msg) H = b('B16BF34DD10945EDE84E9C1EF24A14BFDC843389') x = b('1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') - self.assertEquals(self.K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assert_(transport._activated) + self.assertEqual(self.K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertTrue(transport._activated) def test_3_gex_client(self): transport = FakeTransport() @@ -132,8 +132,8 @@ class KexTest (unittest.TestCase): kex = KexGex(transport) kex.start_kex() x = b('22000004000000080000002000') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) msg = Message() msg.add_mpint(FakeModulusPack.P) @@ -141,8 +141,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) msg = Message() msg.add_string('fake-host-key') @@ -151,10 +151,10 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) H = b('A265563F2FA87F1A89BF007EE90D58BE2E4A4BD0') - self.assertEquals(self.K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) - self.assert_(transport._activated) + self.assertEqual(self.K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertTrue(transport._activated) def test_4_gex_old_client(self): transport = FakeTransport() @@ -162,8 +162,8 @@ class KexTest (unittest.TestCase): kex = KexGex(transport) kex.start_kex(_test_old_style=True) x = b('1E00000800') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) msg = Message() msg.add_mpint(FakeModulusPack.P) @@ -171,8 +171,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) msg = Message() msg.add_string('fake-host-key') @@ -181,17 +181,17 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) H = b('807F87B269EF7AC5EC7E75676808776A27D5864C') - self.assertEquals(self.K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals((b('fake-host-key'), b('fake-sig')), transport._verify) - self.assert_(transport._activated) + self.assertEqual(self.K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertTrue(transport._activated) def test_5_gex_server(self): transport = FakeTransport() transport.server_mode = True kex = KexGex(transport) kex.start_kex() - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD), transport._expect) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD), transport._expect) msg = Message() msg.add_int(1024) @@ -200,8 +200,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, msg) x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) msg = Message() msg.add_mpint(12345) @@ -210,25 +210,25 @@ class KexTest (unittest.TestCase): K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 H = b('CE754197C21BF3452863B4F44D0B3951F12516EF') x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') - self.assertEquals(K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assert_(transport._activated) + self.assertEqual(K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertTrue(transport._activated) def test_6_gex_server_with_old_client(self): transport = FakeTransport() transport.server_mode = True kex = KexGex(transport) kex.start_kex() - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD), transport._expect) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD), transport._expect) msg = Message() msg.add_int(2048) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD, msg) x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assertEquals((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) msg = Message() msg.add_mpint(12345) @@ -237,7 +237,7 @@ class KexTest (unittest.TestCase): K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 H = b('B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B') x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') - self.assertEquals(K, transport._K) - self.assertEquals(H, hexlify(transport._H).upper()) - self.assertEquals(x, hexlify(transport._message.asbytes()).upper()) - self.assert_(transport._activated) + self.assertEqual(K, transport._K) + self.assertEqual(H, hexlify(transport._H).upper()) + self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) + self.assertTrue(transport._activated) diff --git a/tests/test_message.py b/tests/test_message.py index 0aed88c..f983b4d 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -40,7 +40,7 @@ class MessageTest (unittest.TestCase): msg.add_string('q') msg.add_string('hello') msg.add_string('x' * 1000) - self.assertEquals(msg.asbytes(), self.__a) + self.assertEqual(msg.asbytes(), self.__a) msg = Message() msg.add_boolean(True) @@ -49,7 +49,7 @@ class MessageTest (unittest.TestCase): msg.add_bytes(zero_byte + byte_chr(0x3f)) msg.add_list(['huey', 'dewey', 'louie']) - self.assertEquals(msg.asbytes(), self.__b) + self.assertEqual(msg.asbytes(), self.__b) msg = Message() msg.add_int64(5) @@ -57,29 +57,29 @@ class MessageTest (unittest.TestCase): msg.add_mpint(17) msg.add_mpint(0xf5e4d3c2b109) msg.add_mpint(-0x65e4d3c2b109) - self.assertEquals(msg.asbytes(), self.__c) + self.assertEqual(msg.asbytes(), self.__c) def test_2_decode(self): msg = Message(self.__a) - self.assertEquals(msg.get_int(), 23) - self.assertEquals(msg.get_int(), 123789456) - self.assertEquals(msg.get_text(), 'q') - self.assertEquals(msg.get_text(), 'hello') - self.assertEquals(msg.get_text(), 'x' * 1000) + self.assertEqual(msg.get_int(), 23) + self.assertEqual(msg.get_int(), 123789456) + self.assertEqual(msg.get_text(), 'q') + self.assertEqual(msg.get_text(), 'hello') + self.assertEqual(msg.get_text(), 'x' * 1000) msg = Message(self.__b) - self.assertEquals(msg.get_boolean(), True) - self.assertEquals(msg.get_boolean(), False) - self.assertEquals(msg.get_byte(), byte_chr(0xf3)) - self.assertEquals(msg.get_bytes(2), zero_byte + byte_chr(0x3f)) - self.assertEquals(msg.get_list(), ['huey', 'dewey', 'louie']) + self.assertEqual(msg.get_boolean(), True) + self.assertEqual(msg.get_boolean(), False) + self.assertEqual(msg.get_byte(), byte_chr(0xf3)) + self.assertEqual(msg.get_bytes(2), zero_byte + byte_chr(0x3f)) + self.assertEqual(msg.get_list(), ['huey', 'dewey', 'louie']) msg = Message(self.__c) - self.assertEquals(msg.get_int64(), 5) - self.assertEquals(msg.get_int64(), 0xf5e4d3c2b109) - self.assertEquals(msg.get_mpint(), 17) - self.assertEquals(msg.get_mpint(), 0xf5e4d3c2b109) - self.assertEquals(msg.get_mpint(), -0x65e4d3c2b109) + self.assertEqual(msg.get_int64(), 5) + self.assertEqual(msg.get_int64(), 0xf5e4d3c2b109) + self.assertEqual(msg.get_mpint(), 17) + self.assertEqual(msg.get_mpint(), 0xf5e4d3c2b109) + self.assertEqual(msg.get_mpint(), -0x65e4d3c2b109) def test_3_add(self): msg = Message() @@ -89,16 +89,16 @@ class MessageTest (unittest.TestCase): msg.add(True) msg.add('cat') msg.add(['a', 'b']) - self.assertEquals(msg.asbytes(), self.__d) + self.assertEqual(msg.asbytes(), self.__d) def test_4_misc(self): msg = Message(self.__d) - self.assertEquals(msg.get_int(), 5) - self.assertEquals(msg.get_int(), 0x1122334455) - self.assertEquals(msg.get_int(), 0xf00000000000000000) - self.assertEquals(msg.get_so_far(), self.__d[:29]) - self.assertEquals(msg.get_remainder(), self.__d[29:]) + self.assertEqual(msg.get_int(), 5) + self.assertEqual(msg.get_int(), 0x1122334455) + self.assertEqual(msg.get_int(), 0xf00000000000000000) + self.assertEqual(msg.get_so_far(), self.__d[:29]) + self.assertEqual(msg.get_remainder(), self.__d[29:]) msg.rewind() - self.assertEquals(msg.get_int(), 5) - self.assertEquals(msg.get_so_far(), self.__d[:4]) - self.assertEquals(msg.get_remainder(), self.__d[4:]) + self.assertEqual(msg.get_int(), 5) + self.assertEqual(msg.get_so_far(), self.__d[:4]) + self.assertEqual(msg.get_remainder(), self.__d[4:]) diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index a4ada72..43dea38 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -54,8 +54,8 @@ class PacketizerTest (unittest.TestCase): p.send_message(m) data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 - self.assertEquals(44, len(data)) - self.assertEquals(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c0')), data[:16]) + self.assertEqual(44, len(data)) + self.assertEqual(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c0')), data[:16]) def test_2_read (self): rsock = LoopSocket() @@ -68,7 +68,7 @@ class PacketizerTest (unittest.TestCase): p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) wsock.send(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459'))) cmd, m = p.read_message() - self.assertEquals(100, cmd) - self.assertEquals(100, m.get_int()) - self.assertEquals(1, m.get_int()) - self.assertEquals(900, m.get_int()) + self.assertEqual(100, cmd) + self.assertEqual(100, m.get_int()) + self.assertEqual(1, m.get_int()) + self.assertEqual(900, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 8ab21a3..f854946 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -92,157 +92,157 @@ class KeyTest (unittest.TestCase): from Crypto.Hash import MD5 key = util.generate_key_bytes(MD5, x1234, 'happy birthday', 30) exp = unhexlify(b('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64')) - self.assertEquals(exp, key) + self.assertEqual(exp, key) def test_2_load_rsa(self): key = RSAKey.from_private_key_file(test_path('test_rsa.key')) - self.assertEquals('ssh-rsa', key.get_name()) + self.assertEqual('ssh-rsa', key.get_name()) exp_rsa = b(FINGER_RSA.split()[1].replace(':', '')) my_rsa = hexlify(key.get_fingerprint()) - self.assertEquals(exp_rsa, my_rsa) - self.assertEquals(PUB_RSA.split()[1], key.get_base64()) - self.assertEquals(1024, key.get_bits()) + self.assertEqual(exp_rsa, my_rsa) + self.assertEqual(PUB_RSA.split()[1], key.get_base64()) + self.assertEqual(1024, key.get_bits()) s = StringIO() key.write_private_key(s) - self.assertEquals(RSA_PRIVATE_OUT, s.getvalue()) + self.assertEqual(RSA_PRIVATE_OUT, s.getvalue()) s.seek(0) key2 = RSAKey.from_private_key(s) - self.assertEquals(key, key2) + self.assertEqual(key, key2) def test_3_load_rsa_password(self): key = RSAKey.from_private_key_file(test_path('test_rsa_password.key'), 'television') - self.assertEquals('ssh-rsa', key.get_name()) + self.assertEqual('ssh-rsa', key.get_name()) exp_rsa = b(FINGER_RSA.split()[1].replace(':', '')) my_rsa = hexlify(key.get_fingerprint()) - self.assertEquals(exp_rsa, my_rsa) - self.assertEquals(PUB_RSA.split()[1], key.get_base64()) - self.assertEquals(1024, key.get_bits()) + self.assertEqual(exp_rsa, my_rsa) + self.assertEqual(PUB_RSA.split()[1], key.get_base64()) + self.assertEqual(1024, key.get_bits()) def test_4_load_dss(self): key = DSSKey.from_private_key_file(test_path('test_dss.key')) - self.assertEquals('ssh-dss', key.get_name()) + self.assertEqual('ssh-dss', key.get_name()) exp_dss = b(FINGER_DSS.split()[1].replace(':', '')) my_dss = hexlify(key.get_fingerprint()) - self.assertEquals(exp_dss, my_dss) - self.assertEquals(PUB_DSS.split()[1], key.get_base64()) - self.assertEquals(1024, key.get_bits()) + self.assertEqual(exp_dss, my_dss) + self.assertEqual(PUB_DSS.split()[1], key.get_base64()) + self.assertEqual(1024, key.get_bits()) s = StringIO() key.write_private_key(s) - self.assertEquals(DSS_PRIVATE_OUT, s.getvalue()) + self.assertEqual(DSS_PRIVATE_OUT, s.getvalue()) s.seek(0) key2 = DSSKey.from_private_key(s) - self.assertEquals(key, key2) + self.assertEqual(key, key2) def test_5_load_dss_password(self): key = DSSKey.from_private_key_file(test_path('test_dss_password.key'), 'television') - self.assertEquals('ssh-dss', key.get_name()) + self.assertEqual('ssh-dss', key.get_name()) exp_dss = b(FINGER_DSS.split()[1].replace(':', '')) my_dss = hexlify(key.get_fingerprint()) - self.assertEquals(exp_dss, my_dss) - self.assertEquals(PUB_DSS.split()[1], key.get_base64()) - self.assertEquals(1024, key.get_bits()) + self.assertEqual(exp_dss, my_dss) + self.assertEqual(PUB_DSS.split()[1], key.get_base64()) + self.assertEqual(1024, key.get_bits()) def test_6_compare_rsa(self): # verify that the private & public keys compare equal key = RSAKey.from_private_key_file(test_path('test_rsa.key')) - self.assertEquals(key, key) + self.assertEqual(key, key) pub = RSAKey(data=key.asbytes()) - self.assert_(key.can_sign()) - self.assert_(not pub.can_sign()) - self.assertEquals(key, pub) + self.assertTrue(key.can_sign()) + self.assertTrue(not pub.can_sign()) + self.assertEqual(key, pub) def test_7_compare_dss(self): # verify that the private & public keys compare equal key = DSSKey.from_private_key_file(test_path('test_dss.key')) - self.assertEquals(key, key) + self.assertEqual(key, key) pub = DSSKey(data=key.asbytes()) - self.assert_(key.can_sign()) - self.assert_(not pub.can_sign()) - self.assertEquals(key, pub) + self.assertTrue(key.can_sign()) + self.assertTrue(not pub.can_sign()) + self.assertEqual(key, pub) def test_8_sign_rsa(self): # verify that the rsa private key can sign and verify key = RSAKey.from_private_key_file(test_path('test_rsa.key')) msg = key.sign_ssh_data(rng, b('ice weasels')) - self.assert_(type(msg) is Message) + self.assertTrue(type(msg) is Message) msg.rewind() - self.assertEquals('ssh-rsa', msg.get_text()) + self.assertEqual('ssh-rsa', msg.get_text()) sig = bytes().join([byte_chr(int(x, 16)) for x in SIGNED_RSA.split(':')]) - self.assertEquals(sig, msg.get_binary()) + self.assertEqual(sig, msg.get_binary()) msg.rewind() pub = RSAKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) def test_9_sign_dss(self): # verify that the dss private key can sign and verify key = DSSKey.from_private_key_file(test_path('test_dss.key')) msg = key.sign_ssh_data(rng, b('ice weasels')) - self.assert_(type(msg) is Message) + self.assertTrue(type(msg) is Message) msg.rewind() - self.assertEquals('ssh-dss', msg.get_text()) + self.assertEqual('ssh-dss', msg.get_text()) # can't do the same test as we do for RSA, because DSS signatures # are usually different each time. but we can test verification # anyway so it's ok. - self.assertEquals(40, len(msg.get_binary())) + self.assertEqual(40, len(msg.get_binary())) msg.rewind() pub = DSSKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) def test_A_generate_rsa(self): key = RSAKey.generate(1024) msg = key.sign_ssh_data(rng, b('jerri blank')) msg.rewind() - self.assert_(key.verify_ssh_sig(b('jerri blank'), msg)) + self.assertTrue(key.verify_ssh_sig(b('jerri blank'), msg)) def test_B_generate_dss(self): key = DSSKey.generate(1024) msg = key.sign_ssh_data(rng, b('jerri blank')) msg.rewind() - self.assert_(key.verify_ssh_sig(b('jerri blank'), msg)) + self.assertTrue(key.verify_ssh_sig(b('jerri blank'), msg)) def test_10_load_ecdsa(self): key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) - self.assertEquals('ecdsa-sha2-nistp256', key.get_name()) + self.assertEqual('ecdsa-sha2-nistp256', key.get_name()) exp_ecdsa = b(FINGER_ECDSA.split()[1].replace(':', '')) my_ecdsa = hexlify(key.get_fingerprint()) - self.assertEquals(exp_ecdsa, my_ecdsa) - self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) - self.assertEquals(256, key.get_bits()) + self.assertEqual(exp_ecdsa, my_ecdsa) + self.assertEqual(PUB_ECDSA.split()[1], key.get_base64()) + self.assertEqual(256, key.get_bits()) s = StringIO() key.write_private_key(s) - self.assertEquals(ECDSA_PRIVATE_OUT, s.getvalue()) + self.assertEqual(ECDSA_PRIVATE_OUT, s.getvalue()) s.seek(0) key2 = ECDSAKey.from_private_key(s) - self.assertEquals(key, key2) + self.assertEqual(key, key2) def test_11_load_ecdsa_password(self): key = ECDSAKey.from_private_key_file(test_path('test_ecdsa_password.key'), b('television')) - self.assertEquals('ecdsa-sha2-nistp256', key.get_name()) + self.assertEqual('ecdsa-sha2-nistp256', key.get_name()) exp_ecdsa = b(FINGER_ECDSA.split()[1].replace(':', '')) my_ecdsa = hexlify(key.get_fingerprint()) - self.assertEquals(exp_ecdsa, my_ecdsa) - self.assertEquals(PUB_ECDSA.split()[1], key.get_base64()) - self.assertEquals(256, key.get_bits()) + self.assertEqual(exp_ecdsa, my_ecdsa) + self.assertEqual(PUB_ECDSA.split()[1], key.get_base64()) + self.assertEqual(256, key.get_bits()) def test_12_compare_ecdsa(self): # verify that the private & public keys compare equal key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) - self.assertEquals(key, key) + self.assertEqual(key, key) pub = ECDSAKey(data=key.asbytes()) - self.assert_(key.can_sign()) - self.assert_(not pub.can_sign()) - self.assertEquals(key, pub) + self.assertTrue(key.can_sign()) + self.assertTrue(not pub.can_sign()) + self.assertEqual(key, pub) def test_13_sign_ecdsa(self): # verify that the rsa private key can sign and verify key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) msg = key.sign_ssh_data(rng, b('ice weasels')) - self.assert_(type(msg) is Message) + self.assertTrue(type(msg) is Message) msg.rewind() - self.assertEquals('ecdsa-sha2-nistp256', msg.get_text()) + self.assertEqual('ecdsa-sha2-nistp256', msg.get_text()) # ECDSA signatures, like DSS signatures, tend to be different # each time, so we can't compare against a "known correct" # signature. @@ -250,4 +250,4 @@ class KeyTest (unittest.TestCase): msg.rewind() pub = ECDSAKey(data=key.asbytes()) - self.assert_(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index c17defa..b84b3fd 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -162,8 +162,8 @@ class SFTPTest (unittest.TestCase): f = sftp.open(FOLDER + '/test', 'w') try: self.assertEqual(f.stat().st_size, 0) - f.close() finally: + f.close() sftp.remove(FOLDER + '/test') def test_2_close(self): @@ -219,8 +219,8 @@ class SFTPTest (unittest.TestCase): self.assertEqual(f.stat().st_size, 37) f.seek(-26, f.SEEK_CUR) self.assertEqual(f.readline(), 'second line\n') - f.close() finally: + f.close() sftp.remove(FOLDER + '/append.txt') def test_5_rename(self): diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index a53a6c3..dc9cba9 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -262,6 +262,7 @@ class BigSFTPTest (unittest.TestCase): for i in range(10): f = sftp.open('%s/hongry.txt' % FOLDER, 'r') f.prefetch() + f.close() f = sftp.open('%s/hongry.txt' % FOLDER, 'r') f.prefetch() for n in range(1024): diff --git a/tests/test_transport.py b/tests/test_transport.py index 22a02a8..397b00c 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -132,28 +132,28 @@ class TransportTest(ParamikoTest): event = threading.Event() self.server = NullServer() - self.assert_(not event.isSet()) + self.assertTrue(not event.isSet()) self.ts.start_server(event, self.server) self.tc.connect(hostkey=public_host_key, username='slowdive', password='pygmalion') event.wait(1.0) - self.assert_(event.isSet()) - self.assert_(self.ts.is_active()) + self.assertTrue(event.isSet()) + self.assertTrue(self.ts.is_active()) def test_1_security_options(self): o = self.tc.get_security_options() - self.assertEquals(type(o), SecurityOptions) - self.assert_(('aes256-cbc', 'blowfish-cbc') != o.ciphers) + self.assertEqual(type(o), SecurityOptions) + self.assertTrue(('aes256-cbc', 'blowfish-cbc') != o.ciphers) o.ciphers = ('aes256-cbc', 'blowfish-cbc') - self.assertEquals(('aes256-cbc', 'blowfish-cbc'), o.ciphers) + self.assertEqual(('aes256-cbc', 'blowfish-cbc'), o.ciphers) try: o.ciphers = ('aes256-cbc', 'made-up-cipher') - self.assert_(False) + self.assertTrue(False) except ValueError: pass try: o.ciphers = 23 - self.assert_(False) + self.assertTrue(False) except TypeError: pass @@ -162,7 +162,7 @@ class TransportTest(ParamikoTest): self.tc.H = unhexlify(b('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3')) self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) - self.assertEquals(b('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995'), + self.assertEqual(b('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995'), hexlify(key).upper()) def test_3_simple(self): @@ -176,21 +176,21 @@ class TransportTest(ParamikoTest): self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() - self.assert_(not event.isSet()) - self.assertEquals(None, self.tc.get_username()) - self.assertEquals(None, self.ts.get_username()) - self.assertEquals(False, self.tc.is_authenticated()) - self.assertEquals(False, self.ts.is_authenticated()) + self.assertTrue(not event.isSet()) + self.assertEqual(None, self.tc.get_username()) + self.assertEqual(None, self.ts.get_username()) + self.assertEqual(False, self.tc.is_authenticated()) + self.assertEqual(False, self.ts.is_authenticated()) self.ts.start_server(event, server) self.tc.connect(hostkey=public_host_key, username='slowdive', password='pygmalion') event.wait(1.0) - self.assert_(event.isSet()) - self.assert_(self.ts.is_active()) - self.assertEquals('slowdive', self.tc.get_username()) - self.assertEquals('slowdive', self.ts.get_username()) - self.assertEquals(True, self.tc.is_authenticated()) - self.assertEquals(True, self.ts.is_authenticated()) + self.assertTrue(event.isSet()) + self.assertTrue(self.ts.is_active()) + self.assertEqual('slowdive', self.tc.get_username()) + self.assertEqual('slowdive', self.ts.get_username()) + self.assertEqual(True, self.tc.is_authenticated()) + self.assertEqual(True, self.ts.is_authenticated()) def test_3a_long_banner(self): """ @@ -201,14 +201,14 @@ class TransportTest(ParamikoTest): self.ts.add_server_key(host_key) event = threading.Event() server = NullServer() - self.assert_(not event.isSet()) + self.assertTrue(not event.isSet()) self.socks.send(LONG_BANNER) self.ts.start_server(event, server) self.tc.connect(hostkey=public_host_key, username='slowdive', password='pygmalion') event.wait(1.0) - self.assert_(event.isSet()) - self.assert_(self.ts.is_active()) + self.assertTrue(event.isSet()) + self.assertTrue(self.ts.is_active()) def test_4_special(self): """ @@ -219,10 +219,10 @@ class TransportTest(ParamikoTest): options.ciphers = ('aes256-cbc',) options.digests = ('hmac-md5-96',) self.setup_test_server(client_options=force_algorithms) - self.assertEquals('aes256-cbc', self.tc.local_cipher) - self.assertEquals('aes256-cbc', self.tc.remote_cipher) - self.assertEquals(12, self.tc.packetizer.get_mac_size_out()) - self.assertEquals(12, self.tc.packetizer.get_mac_size_in()) + self.assertEqual('aes256-cbc', self.tc.local_cipher) + self.assertEqual('aes256-cbc', self.tc.remote_cipher) + self.assertEqual(12, self.tc.packetizer.get_mac_size_out()) + self.assertEqual(12, self.tc.packetizer.get_mac_size_in()) self.tc.send_ignore(1024) self.tc.renegotiate_keys() @@ -233,10 +233,10 @@ class TransportTest(ParamikoTest): verify that the keepalive will be sent. """ self.setup_test_server() - self.assertEquals(None, getattr(self.server, '_global_request', None)) + self.assertEqual(None, getattr(self.server, '_global_request', None)) self.tc.set_keepalive(1) time.sleep(2) - self.assertEquals('keepalive@lag.net', self.server._global_request) + self.assertEqual('keepalive@lag.net', self.server._global_request) def test_6_exec_command(self): """ @@ -248,7 +248,7 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) try: chan.exec_command('no') - self.assert_(False) + self.assertTrue(False) except SSHException: pass @@ -260,11 +260,11 @@ class TransportTest(ParamikoTest): schan.close() f = chan.makefile() - self.assertEquals('Hello there.\n', f.readline()) - self.assertEquals('', f.readline()) + self.assertEqual('Hello there.\n', f.readline()) + self.assertEqual('', f.readline()) f = chan.makefile_stderr() - self.assertEquals('This is on stderr.\n', f.readline()) - self.assertEquals('', f.readline()) + self.assertEqual('This is on stderr.\n', f.readline()) + self.assertEqual('', f.readline()) # now try it with combined stdout/stderr chan = self.tc.open_session() @@ -276,9 +276,9 @@ class TransportTest(ParamikoTest): chan.set_combine_stderr(True) f = chan.makefile() - self.assertEquals('Hello there.\n', f.readline()) - self.assertEquals('This is on stderr.\n', f.readline()) - self.assertEquals('', f.readline()) + self.assertEqual('Hello there.\n', f.readline()) + self.assertEqual('This is on stderr.\n', f.readline()) + self.assertEqual('', f.readline()) def test_7_invoke_shell(self): """ @@ -290,9 +290,9 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) chan.send('communist j. cat\n') f = schan.makefile() - self.assertEquals('communist j. cat\n', f.readline()) + self.assertEqual('communist j. cat\n', f.readline()) chan.close() - self.assertEquals('', f.readline()) + self.assertEqual('', f.readline()) def test_8_channel_exception(self): """ @@ -304,7 +304,7 @@ class TransportTest(ParamikoTest): self.fail('expected exception') except ChannelException: x = sys.exc_info()[1] - self.assert_(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) + self.assertTrue(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self): """ @@ -316,7 +316,7 @@ class TransportTest(ParamikoTest): schan = self.ts.accept(1.0) chan.exec_command('yes') schan.send('Hello there.\n') - self.assert_(not chan.exit_status_ready()) + self.assertTrue(not chan.exit_status_ready()) # trigger an EOF schan.shutdown_read() schan.shutdown_write() @@ -324,15 +324,15 @@ class TransportTest(ParamikoTest): schan.close() f = chan.makefile() - self.assertEquals('Hello there.\n', f.readline()) - self.assertEquals('', f.readline()) + self.assertEqual('Hello there.\n', f.readline()) + self.assertEqual('', f.readline()) count = 0 while not chan.exit_status_ready(): time.sleep(0.1) count += 1 if count > 50: raise Exception("timeout") - self.assertEquals(23, chan.recv_exit_status()) + self.assertEqual(23, chan.recv_exit_status()) chan.close() def test_A_select(self): @@ -346,9 +346,9 @@ class TransportTest(ParamikoTest): # nothing should be ready r, w, e = select.select([chan], [], [], 0.1) - self.assertEquals([], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([], r) + self.assertEqual([], w) + self.assertEqual([], e) schan.send('hello\n') @@ -358,17 +358,17 @@ class TransportTest(ParamikoTest): if chan in r: break time.sleep(0.1) - self.assertEquals([chan], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([chan], r) + self.assertEqual([], w) + self.assertEqual([], e) - self.assertEquals(b('hello\n'), chan.recv(6)) + self.assertEqual(b('hello\n'), chan.recv(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) - self.assertEquals([], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([], r) + self.assertEqual([], w) + self.assertEqual([], e) schan.close() @@ -378,17 +378,17 @@ class TransportTest(ParamikoTest): if chan in r: break time.sleep(0.1) - self.assertEquals([chan], r) - self.assertEquals([], w) - self.assertEquals([], e) - self.assertEquals(bytes(), chan.recv(16)) + self.assertEqual([chan], r) + self.assertEqual([], w) + self.assertEqual([], e) + self.assertEqual(bytes(), chan.recv(16)) # make sure the pipe is still open for now... p = chan._pipe - self.assertEquals(False, p._closed) + self.assertEqual(False, p._closed) chan.close() # ...and now is closed. - self.assertEquals(True, p._closed) + self.assertEqual(True, p._closed) def test_B_renegotiate(self): """ @@ -400,7 +400,7 @@ class TransportTest(ParamikoTest): chan.exec_command('yes') schan = self.ts.accept(1.0) - self.assertEquals(self.tc.H, self.tc.session_id) + self.assertEqual(self.tc.H, self.tc.session_id) for i in range(20): chan.send('x' * 1024) chan.close() @@ -410,7 +410,7 @@ class TransportTest(ParamikoTest): if self.tc.H != self.tc.session_id: break time.sleep(0.1) - self.assertNotEquals(self.tc.H, self.tc.session_id) + self.assertNotEqual(self.tc.H, self.tc.session_id) schan.close() @@ -429,8 +429,8 @@ class TransportTest(ParamikoTest): chan.send('x' * 1024) bytes2 = self.tc.packetizer._Packetizer__sent_bytes # tests show this is actually compressed to *52 bytes*! including packet overhead! nice!! :) - self.assert_(bytes2 - bytes < 1024) - self.assertEquals(52, bytes2 - bytes) + self.assertTrue(bytes2 - bytes < 1024) + self.assertEqual(52, bytes2 - bytes) chan.close() schan.close() @@ -450,20 +450,20 @@ class TransportTest(ParamikoTest): requested.append((addr, port)) self.tc._queue_incoming_channel(c) - self.assertEquals(None, getattr(self.server, '_x11_screen_number', None)) + self.assertEqual(None, getattr(self.server, '_x11_screen_number', None)) cookie = chan.request_x11(0, single_connection=True, handler=handler) - self.assertEquals(0, self.server._x11_screen_number) - self.assertEquals('MIT-MAGIC-COOKIE-1', self.server._x11_auth_protocol) - self.assertEquals(cookie, self.server._x11_auth_cookie) - self.assertEquals(True, self.server._x11_single_connection) + self.assertEqual(0, self.server._x11_screen_number) + self.assertEqual('MIT-MAGIC-COOKIE-1', self.server._x11_auth_protocol) + self.assertEqual(cookie, self.server._x11_auth_cookie) + self.assertEqual(True, self.server._x11_single_connection) x11_server = self.ts.open_x11_channel(('localhost', 6093)) x11_client = self.tc.accept() - self.assertEquals('localhost', requested[0][0]) - self.assertEquals(6093, requested[0][1]) + self.assertEqual('localhost', requested[0][0]) + self.assertEqual(6093, requested[0][1]) x11_server.send('hello') - self.assertEquals(b('hello'), x11_client.recv(5)) + self.assertEqual(b('hello'), x11_client.recv(5)) x11_server.close() x11_client.close() @@ -487,7 +487,7 @@ class TransportTest(ParamikoTest): self.tc._queue_incoming_channel(c) port = self.tc.request_port_forward('127.0.0.1', 0, handler) - self.assertEquals(port, self.server._listen.getsockname()[1]) + self.assertEqual(port, self.server._listen.getsockname()[1]) cs = socket.socket() cs.connect(('127.0.0.1', port)) @@ -496,7 +496,7 @@ class TransportTest(ParamikoTest): cch = self.tc.accept() sch.send('hello') - self.assertEquals(b('hello'), cch.recv(5)) + self.assertEqual(b('hello'), cch.recv(5)) sch.close() cch.close() ss.close() @@ -533,7 +533,7 @@ class TransportTest(ParamikoTest): sch.send(cch.recv(8192)) sch.close() - self.assertEquals(b('Hello!\n'), cs.recv(7)) + self.assertEqual(b('Hello!\n'), cs.recv(7)) cs.close() def test_G_stderr_select(self): @@ -548,9 +548,9 @@ class TransportTest(ParamikoTest): # nothing should be ready r, w, e = select.select([chan], [], [], 0.1) - self.assertEquals([], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([], r) + self.assertEqual([], w) + self.assertEqual([], e) schan.send_stderr('hello\n') @@ -560,17 +560,17 @@ class TransportTest(ParamikoTest): if chan in r: break time.sleep(0.1) - self.assertEquals([chan], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([chan], r) + self.assertEqual([], w) + self.assertEqual([], e) - self.assertEquals(b('hello\n'), chan.recv_stderr(6)) + self.assertEqual(b('hello\n'), chan.recv_stderr(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) - self.assertEquals([], r) - self.assertEquals([], w) - self.assertEquals([], e) + self.assertEqual([], r) + self.assertEqual([], w) + self.assertEqual([], e) schan.close() chan.close() @@ -584,7 +584,7 @@ class TransportTest(ParamikoTest): chan.invoke_shell() schan = self.ts.accept(1.0) - self.assertEquals(chan.send_ready(), True) + self.assertEqual(chan.send_ready(), True) total = 0 K = '*' * 1024 while total < 1024 * 1024: @@ -592,11 +592,11 @@ class TransportTest(ParamikoTest): total += len(K) if not chan.send_ready(): break - self.assert_(total < 1024 * 1024) + self.assertTrue(total < 1024 * 1024) schan.close() chan.close() - self.assertEquals(chan.send_ready(), True) + self.assertEqual(chan.send_ready(), True) def test_I_rekey_deadlock(self): """ diff --git a/tests/test_util.py b/tests/test_util.py index 84d5e88..dba5223 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -103,7 +103,7 @@ class UtilTest(ParamikoTest): global test_config_file f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) - self.assertEquals(config._config, + self.assertEqual(config._config, [{'host': ['*'], 'config': {}}, {'host': ['*'], 'config': {'identityfile': ['~/.ssh/id_rsa'], 'user': 'robey'}}, {'host': ['*.example.com'], 'config': {'user': 'bjork', 'port': '3333'}}, {'host': ['*'], 'config': {'crazy': 'something dumb '}}, @@ -131,7 +131,7 @@ class UtilTest(ParamikoTest): hostname=host, identityfile=[os.path.expanduser("~/.ssh/id_rsa")] ) - self.assertEquals( + self.assertEqual( paramiko.util.lookup_ssh_host_config(host, config), values ) @@ -139,7 +139,7 @@ class UtilTest(ParamikoTest): def test_4_generate_key_bytes(self): x = paramiko.util.generate_key_bytes(SHA, b('ABCDEFGH'), 'This is my secret passphrase.', 64) hex = ''.join(['%02x' % byte_ord(c) for c in x]) - self.assertEquals(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') + self.assertEqual(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') def test_5_host_keys(self): f = open('hostfile.temp', 'w') @@ -147,11 +147,11 @@ class UtilTest(ParamikoTest): f.close() try: hostdict = paramiko.util.load_host_keys('hostfile.temp') - self.assertEquals(2, len(hostdict)) - self.assertEquals(1, len(list(hostdict.values())[0])) - self.assertEquals(1, len(list(hostdict.values())[1])) + self.assertEqual(2, len(hostdict)) + self.assertEqual(1, len(list(hostdict.values())[0])) + self.assertEqual(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEquals(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) finally: os.unlink('hostfile.temp') @@ -159,7 +159,7 @@ class UtilTest(ParamikoTest): from paramiko.common import rng # just verify that we can pull out 32 bytes and not get an exception. x = rng.read(32) - self.assertEquals(len(x), 32) + self.assertEqual(len(x), 32) def test_7_host_config_expose_issue_33(self): test_config_file = """ @@ -175,13 +175,13 @@ Host * f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' - self.assertEquals( + self.assertEqual( paramiko.util.lookup_ssh_host_config(host, config), {'hostname': host, 'port': '22'} ) def test_8_eintr_retry(self): - self.assertEquals('foo', paramiko.util.retry_on_signal(lambda: 'foo')) + self.assertEqual('foo', paramiko.util.retry_on_signal(lambda: 'foo')) # Variables that are set by raises_intr intr_errors_remaining = [3] @@ -192,8 +192,8 @@ Host * intr_errors_remaining[0] -= 1 raise IOError(errno.EINTR, 'file', 'interrupted system call') self.assertTrue(paramiko.util.retry_on_signal(raises_intr) is None) - self.assertEquals(0, intr_errors_remaining[0]) - self.assertEquals(4, call_count[0]) + self.assertEqual(0, intr_errors_remaining[0]) + self.assertEqual(4, call_count[0]) def raises_ioerror_not_eintr(): raise IOError(errno.ENOENT, 'file', 'file not found') @@ -219,7 +219,7 @@ Host equals-delimited f = StringIO(conf) config = paramiko.util.parse_ssh_config(f) for host in ('space-delimited', 'equals-delimited'): - self.assertEquals( + self.assertEqual( host_config(host, config)['proxycommand'], 'foo bar=biz baz' ) @@ -245,7 +245,7 @@ Host * ('specific', "host specific port 37 lol"), ('portonly', "host portonly port 155"), ): - self.assertEquals( + self.assertEqual( host_config(host, config)['proxycommand'], val ) @@ -267,7 +267,7 @@ Host * f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) host = 'www13.example.com' - self.assertEquals( + self.assertEqual( paramiko.util.lookup_ssh_host_config(host, config), {'hostname': host, 'port': '8080'} ) @@ -295,7 +295,7 @@ ProxyCommand foo=bar:%h-%p f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) - self.assertEquals( + self.assertEqual( paramiko.util.lookup_ssh_host_config(host, config), values ) @@ -325,7 +325,7 @@ IdentityFile id_dsa22 f = StringIO(test_config_file) config = paramiko.util.parse_ssh_config(f) - self.assertEquals( + self.assertEqual( paramiko.util.lookup_ssh_host_config(host, config), values ) From dc58b7bcb2bf6c945878a841b1861f0f195a3075 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 20:19:52 -0700 Subject: [PATCH 39/56] Fix message to handle long properly, even on Py3 --- paramiko/message.py | 5 +++-- paramiko/sftp_client.py | 6 +++--- paramiko/sftp_server.py | 6 +++--- tests/test_message.py | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/paramiko/message.py b/paramiko/message.py index 8e81e69..4508621 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -170,7 +170,7 @@ class Message (object): @return: a 64-bit unsigned integer. @rtype: long """ - return self.get_int() + return struct.unpack('>Q', self.get_bytes(8))[0] def get_mpint(self): """ @@ -289,7 +289,8 @@ class Message (object): @param n: long int to add @type n: long """ - return self.add_int(n) + self.packet.write(struct.pack('>Q', n)) + return self def add_mpint(self, z): """ diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 31e6697..5a08db6 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -695,10 +695,10 @@ class SFTPClient (BaseSFTP): msg = Message() msg.add_int(self.request_number) for item in arg: - if isinstance(item, int): - msg.add_int(item) - elif isinstance(item, long): + if isinstance(item, long): msg.add_int64(item) + elif isinstance(item, int): + msg.add_int(item) elif isinstance(item, (string_types, bytes_types)): msg.add_string(item) elif isinstance(item, SFTPAttributes): diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 2a1a443..38adc63 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -183,10 +183,10 @@ class SFTPServer (BaseSFTP, SubsystemHandler): msg = Message() msg.add_int(request_number) for item in arg: - if type(item) is int: - msg.add_int(item) - elif type(item) is long: + if isinstance(item, long): msg.add_int64(item) + elif isinstance(item, int): + msg.add_int(item) elif isinstance(item, (string_types, bytes_types)): msg.add_string(item) elif type(item) is SFTPAttributes: diff --git a/tests/test_message.py b/tests/test_message.py index f983b4d..cb815f9 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -30,7 +30,7 @@ class MessageTest (unittest.TestCase): __a = unhexlify(b('000000170760e09000000001710000000568656c6c6f000003e8')) + (b('x') * 1000) __b = unhexlify(b('0100f3003f00000010687565792c64657765792c6c6f756965')) - __c = unhexlify(b('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')) + __c = unhexlify(b('00000000000000050000f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')) __d = unhexlify(b('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')) def test_1_encode(self): From 7d5fa50ca473f6c4336b80ca365e2d1270b9136f Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 20:20:09 -0700 Subject: [PATCH 40/56] More type conversions --- paramiko/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paramiko/util.py b/paramiko/util.py index b2ac3f5..51fa6d6 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -285,10 +285,10 @@ class Counter (object): # start with value - 1 so we don't have to store intermediate values when counting # could the iv be 0? if initial_value == 0: - self.value = array.array('c', '\xFF' * self.blocksize) + self.value = array.array('c', max_byte * self.blocksize) else: x = deflate_long(initial_value - 1, add_sign_padding=False) - self.value = array.array('c', '\x00' * (self.blocksize - len(x)) + x) + self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x) def __call__(self): """Increament the counter and return the new value""" From 01731fa2c34483ec6d429a23bf9d19126f63f618 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Sat, 2 Nov 2013 20:28:02 -0700 Subject: [PATCH 41/56] Bump version to 1.13.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 10237f2..be404b1 100644 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ if sys.platform == 'darwin': setup(name = "paramiko", - version = "1.12.0", + version = "1.13.0", description = "SSH2 protocol library", author = "Jeff Forcier", author_email = "jeff@bitprophet.org", From 3ce336c88b7bfbfad03fab17bff8cb3c3a77176c Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 07:30:45 -0800 Subject: [PATCH 42/56] Change conditional from PY3 to PY2 to be better prepared for a possible Py4. --- paramiko/common.py | 8 +- paramiko/file.py | 6 +- paramiko/py3compat.py | 168 +++++++++++++++++++++++------------------- paramiko/util.py | 4 +- test.py | 8 +- tests/test_pkey.py | 2 +- tests/test_sftp.py | 4 +- 7 files changed, 107 insertions(+), 93 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 223aac1..e30df73 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -131,12 +131,12 @@ cr_byte = byte_chr(13) linefeed_byte = byte_chr(10) crlf = cr_byte + linefeed_byte -if PY3: - cr_byte_value = 13 - linefeed_byte_value = 10 -else: +if PY2: cr_byte_value = cr_byte linefeed_byte_value = linefeed_byte +else: + cr_byte_value = 13 + linefeed_byte_value = 10 def asbytes(s): diff --git a/paramiko/file.py b/paramiko/file.py index a0d94ef..c9f191a 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -92,8 +92,8 @@ class BufferedFile (object): self._wbuffer = BytesIO() return - if PY3: - def __next__(self): + if PY2: + def next(self): """ Returns the next line from the input, or raises L{StopIteration} when EOF is hit. Unlike python file objects, it's okay to mix calls to @@ -109,7 +109,7 @@ class BufferedFile (object): raise StopIteration return line else: - def next(self): + def __next__(self): """ Returns the next line from the input, or raises L{StopIteration} when EOF is hit. Unlike python file objects, it's okay to mix calls to diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index 0aad361..2228599 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,13 +1,101 @@ import sys import base64 -__all__ = ['PY3', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', +__all__ = ['PY2', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', 'decodebytes', 'encodebytes', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next'] -PY3 = sys.version_info[0] >= 3 +PY2 = sys.version_info[0] < 3 -if PY3: +if PY2: + string_types = basestring + text_type = unicode + bytes_types = str + bytes = str + integer_types = (int, long) + long = long + input = raw_input + decodebytes = base64.decodestring + encodebytes = base64.encodestring + + + def bytestring(s): # NOQA + if isinstance(s, unicode): + return s.encode('utf-8') + return s + + + byte_ord = ord # NOQA + byte_chr = chr # NOQA + + + def byte_mask(c, mask): + return chr(ord(c) & mask) + + + def b(s, encoding='utf8'): # NOQA + """cast unicode or bytes to bytes""" + if isinstance(s, str): + return s + elif isinstance(s, unicode): + return s.encode(encoding) + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + + def u(s, encoding='utf8'): # NOQA + """cast bytes or unicode to unicode""" + if isinstance(s, str): + return s.decode(encoding) + elif isinstance(s, unicode): + return s + else: + raise TypeError("Expected unicode or bytes, got %r" % s) + + + def b2s(s): + return s + + + try: + import cStringIO + + StringIO = cStringIO.StringIO # NOQA + except ImportError: + import StringIO + + StringIO = StringIO.StringIO # NOQA + + BytesIO = StringIO + + + def is_callable(c): # NOQA + return callable(c) + + + def get_next(c): # NOQA + return c.next + + + def next(c): + return c.next() + + # It's possible to have sizeof(long) != sizeof(Py_ssize_t). + class X(object): + def __len__(self): + return 1 << 31 + + + try: + len(X()) + except OverflowError: + # 32-bit + MAXSIZE = int((1 << 31) - 1) # NOQA + else: + # 64-bit + MAXSIZE = int((1 << 63) - 1) # NOQA + del X +else: import collections import struct string_types = str @@ -70,77 +158,3 @@ if PY3: next = next MAXSIZE = sys.maxsize # NOQA -else: - string_types = basestring - text_type = unicode - bytes_types = str - bytes = str - integer_types = (int, long) - long = long - input = raw_input - decodebytes = base64.decodestring - encodebytes = base64.encodestring - - def bytestring(s): # NOQA - if isinstance(s, unicode): - return s.encode('utf-8') - return s - - byte_ord = ord # NOQA - byte_chr = chr # NOQA - - def byte_mask(c, mask): - return chr(ord(c) & mask) - - def b(s, encoding='utf8'): # NOQA - """cast unicode or bytes to bytes""" - if isinstance(s, str): - return s - elif isinstance(s, unicode): - return s.encode(encoding) - else: - raise TypeError("Expected unicode or bytes, got %r" % s) - - def u(s, encoding='utf8'): # NOQA - """cast bytes or unicode to unicode""" - if isinstance(s, str): - return s.decode(encoding) - elif isinstance(s, unicode): - return s - else: - raise TypeError("Expected unicode or bytes, got %r" % s) - - def b2s(s): - return s - - try: - import cStringIO - StringIO = cStringIO.StringIO # NOQA - except ImportError: - import StringIO - StringIO = StringIO.StringIO # NOQA - - BytesIO = StringIO - - def is_callable(c): # NOQA - return callable(c) - - def get_next(c): # NOQA - return c.next - - def next(c): - return c.next() - - # It's possible to have sizeof(long) != sizeof(Py_ssize_t). - class X(object): - def __len__(self): - return 1 << 31 - try: - len(X()) - except OverflowError: - # 32-bit - MAXSIZE = int((1 << 31) - 1) # NOQA - else: - # 64-bit - MAXSIZE = int((1 << 63) - 1) # NOQA - del X diff --git a/paramiko/util.py b/paramiko/util.py index 51fa6d6..71fc467 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -63,8 +63,8 @@ def inflate_long(s, always_positive=False): out -= (long(1) << (8 * len(s))) return out -deflate_zero = 0 if PY3 else zero_byte -deflate_ff = 0xff if PY3 else max_byte +deflate_zero = zero_byte if PY2 else 0 +deflate_ff = max_byte if PY2 else 0xff def deflate_long(n, add_sign_padding=True): "turns a long-int into a normalized byte string (adapted from Crypto.Util.number)" diff --git a/test.py b/test.py index c8ac6bc..bd966d1 100755 --- a/test.py +++ b/test.py @@ -29,7 +29,7 @@ import unittest from optparse import OptionParser import paramiko import threading -from paramiko.py3compat import PY3 +from paramiko.py3compat import PY2 sys.path.append('tests') @@ -149,10 +149,10 @@ def main(): # TODO: make that not a problem, jeez for thread in threading.enumerate(): if thread is not threading.currentThread(): - if PY3: - thread._stop() - else: + if PY2: thread._Thread__stop() + else: + thread._stop() # Exit correctly if not result.wasSuccessful(): sys.exit(1) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index f854946..19c5c69 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -23,7 +23,7 @@ Some unit tests for public/private key objects. from binascii import hexlify, unhexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.common import rng, StringIO, byte_chr, b, PY3, bytes +from paramiko.common import rng, StringIO, byte_chr, b, bytes from tests.util import test_path # from openssh's ssh-keygen diff --git a/tests/test_sftp.py b/tests/test_sftp.py index b84b3fd..4a41258 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,8 +72,8 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True -unicode_folder = eval(compile(r"'\u00fcnic\u00f8de'" if PY3 else r"u'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) -utf8_folder = eval(compile(r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY3 else r"'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) +unicode_folder = eval(compile(r"u'\u00fcnic\u00f8de'" if PY2 else r"'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) +utf8_folder = eval(compile(r"'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY2 else r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) def get_sftp(): global sftp From 25dd096da065b1bc2f35c1a62d8a7055b022818b Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:06:35 -0800 Subject: [PATCH 43/56] Change all exceptions to modern format (not Py2.5 compatible) --- demos/demo.py | 6 ++---- demos/demo_server.py | 9 +++------ demos/demo_sftp.py | 3 +-- demos/demo_simple.py | 3 +-- demos/forward.py | 6 ++---- demos/rforward.py | 6 ++---- paramiko/auth_handler.py | 3 +-- paramiko/client.py | 16 +++++++-------- paramiko/dsskey.py | 4 ++-- paramiko/hostkeys.py | 2 +- paramiko/packet.py | 9 +++------ paramiko/pkey.py | 4 ++-- paramiko/proxy.py | 6 ++---- paramiko/server.py | 3 +-- paramiko/sftp_client.py | 4 ++-- paramiko/sftp_file.py | 4 ++-- paramiko/sftp_handle.py | 6 ++---- paramiko/sftp_server.py | 6 ++---- paramiko/transport.py | 28 +++++++++++---------------- paramiko/util.py | 3 +-- tests/stub_sftp.py | 42 ++++++++++++++-------------------------- tests/test_sftp.py | 3 +-- tests/test_transport.py | 5 ++--- 23 files changed, 68 insertions(+), 113 deletions(-) diff --git a/demos/demo.py b/demos/demo.py index 2f245ba..fff6178 100755 --- a/demos/demo.py +++ b/demos/demo.py @@ -113,8 +113,7 @@ if hostname.find(':') >= 0: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, port)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Connect failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -171,8 +170,7 @@ try: chan.close() t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_server.py b/demos/demo_server.py index 5a41a71..34a9bd5 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -84,8 +84,7 @@ try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', 2200)) -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Bind failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -94,8 +93,7 @@ try: sock.listen(100) print('Listening for connection ...') client, addr = sock.accept() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Listen/accept failed: ' + str(e)) traceback.print_exc() sys.exit(1) @@ -138,8 +136,7 @@ try: chan.send('\r\nI don\'t like you, ' + username + '.\r\n') chan.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) traceback.print_exc() try: diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index d7f2808..1cccffc 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -114,8 +114,7 @@ try: t.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/demo_simple.py b/demos/demo_simple.py index fb71145..ae631e4 100755 --- a/demos/demo_simple.py +++ b/demos/demo_simple.py @@ -77,8 +77,7 @@ try: chan.close() client.close() -except Exception: - e = sys.exc_info()[1] +except Exception as e: print('*** Caught exception: %s: %s' % (e.__class__, e)) traceback.print_exc() try: diff --git a/demos/forward.py b/demos/forward.py index f1a0c04..96e1700 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -58,8 +58,7 @@ class Handler (SocketServer.BaseRequestHandler): chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) @@ -170,8 +169,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) diff --git a/demos/rforward.py b/demos/rforward.py index fcffbcb..ae70670 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -46,8 +46,7 @@ def handler(chan, host, port): sock = socket.socket() try: sock.connect((host, port)) - except Exception: - e = sys.exc_info()[1] + except Exception as e: verbose('Forwarding request to %s:%d failed: %r' % (host, port, e)) return @@ -151,8 +150,7 @@ def main(): try: client.connect(server[0], server[1], username=options.user, key_filename=options.keyfile, look_for_keys=options.look_for_keys, password=password) - except Exception: - e = sys.exc_info()[1] + except Exception as e: print('*** Failed to connect to %s:%d: %r' % (server[0], server[1], e)) sys.exit(1) diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 83f27a1..3e389da 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -306,8 +306,7 @@ class AuthHandler (object): keyblob = m.get_binary() try: key = self.transport._key_info[keytype](Message(keyblob)) - except SSHException: - e = sys.exc_info()[1] + except SSHException as e: self.transport._log(INFO, 'Auth rejected: public key: %s' % str(e)) key = None except: diff --git a/paramiko/client.py b/paramiko/client.py index 8800b0f..3ce0168 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -452,8 +452,8 @@ class SSHClient (object): two_factor = (allowed_types == ['password']) if not two_factor: return - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor: for key_filename in key_filenames: @@ -466,8 +466,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor and allow_agent: if self._agent == None: @@ -482,8 +482,8 @@ class SSHClient (object): if not two_factor: return break - except SSHException: - saved_exception = sys.exc_info()[1] + except SSHException as e: + saved_exception = e if not two_factor: keyfiles = [] @@ -514,8 +514,8 @@ class SSHClient (object): if not two_factor: return break - except (SSHException, IOError): - saved_exception = sys.exc_info()[1] + except (SSHException, IOError) as e: + saved_exception = e if password is not None: try: diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 4c97b26..a02a4dd 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -187,8 +187,8 @@ class DSSKey (PKey): # DSAPrivateKey = { version = 0, p, q, g, y, x } try: keylist = BER(data).decode() - except BERException: - raise SSHException('Unable to parse key file: ' + str(sys.exc_info()[1])) + except BERException as e: + raise SSHException('Unable to parse key file: ' + str(e)) if (type(keylist) is not list) or (len(keylist) < 6) or (keylist[0] != 0): raise SSHException('not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 9da883e..16f7f90 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -92,7 +92,7 @@ class HostKeyEntry: log.info("Unable to handle key of type %s" % (keytype,)) return None - except binascii.Error: + except binascii.Error as e: raise InvalidHostKey(line, sys.exc_info()[1]) return cls(names, key) diff --git a/paramiko/packet.py b/paramiko/packet.py index 2652188..b941c75 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -216,8 +216,7 @@ class Packetizer (object): n -= len(x) except socket.timeout: got_timeout = True - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. @@ -246,8 +245,7 @@ class Packetizer (object): n = self.__socket.send(out) except socket.timeout: retry_write = True - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): retry_write = True elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): @@ -472,8 +470,7 @@ class Packetizer (object): break except socket.timeout: pass - except EnvironmentError: - e = sys.exc_info()[1] + except EnvironmentError as e: if ((type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR)): pass diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 3d786ae..a2f27ed 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -312,8 +312,8 @@ class PKey (object): # if we trudged to the end of the file, just try to cope. try: data = decodebytes(b(''.join(lines[start:end]))) - except base64.binascii.Error: - raise SSHException('base64 decoding error: ' + str(sys.exc_info()[1])) + except base64.binascii.Error as e: + raise SSHException('base64 decoding error: ' + str(e)) if 'proc-type' not in headers: # unencryped: done return data diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 43c4666..72927c0 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -60,8 +60,7 @@ class ProxyCommand(object): """ try: self.process.stdin.write(content) - except IOError: - e = sys.exc_info()[1] + except IOError as e: # There was a problem with the child process. It probably # died and we can't proceed. The best option here is to # raise an exception informing the user that the informed @@ -81,8 +80,7 @@ class ProxyCommand(object): """ try: return os.read(self.process.stdout.fileno(), size) - except IOError: - e = sys.exc_info()[1] + except IOError as e: raise ProxyCommandFailure(' '.join(self.cmd), e.strerror) def close(self): diff --git a/paramiko/server.py b/paramiko/server.py index f3383ad..a922201 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -623,8 +623,7 @@ class SubsystemHandler (threading.Thread): try: self.__transport._log(DEBUG, 'Starting handler for subsystem %s' % self.__name) self.start_subsystem(self.__name, self.__transport, self.__channel) - except Exception: - e = sys.exc_info()[1] + except Exception as e: self.__transport._log(ERROR, 'Exception in subsystem handler for "%s": %s' % (self.__name, str(e))) self.__transport._log(ERROR, util.tb_strings()) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 5a08db6..1082c6c 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -717,8 +717,8 @@ class SFTPClient (BaseSFTP): while True: try: t, data = self._read_packet() - except EOFError: - raise SSHException('Server connection dropped: %s' % str(sys.exc_info()[1])) + except EOFError as e: + raise SSHException('Server connection dropped: %s' % str(e)) msg = Message(data) num = msg.get_int() if num not in self._expecting: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 3b2bcbf..f0f7e38 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -464,8 +464,8 @@ class SFTPFile (BufferedFile): # save exception and re-raise it on next file operation try: self.sftp._convert_status(msg) - except Exception: - self._saved_exception = sys.exc_info()[1] + except Exception as e: + self._saved_exception = e return if t != CMD_DATA: raise SFTPError('Expected data') diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 8b78063..9fc2100 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -100,8 +100,7 @@ class SFTPHandle (object): readfile.seek(offset) self.__tell = offset data = readfile.read(length) - except IOError: - e = sys.exc_info()[1] + except IOError as e: self.__tell = None return SFTPServer.convert_errno(e.errno) self.__tell += len(data) @@ -140,8 +139,7 @@ class SFTPHandle (object): self.__tell = offset writefile.write(data) writefile.flush() - except IOError: - e = sys.exc_info()[1] + except IOError as e: self.__tell = None return SFTPServer.convert_errno(e.errno) if self.__tell is not None: diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 38adc63..7dcef6b 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -92,8 +92,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): except EOFError: self._log(DEBUG, 'EOF -- end of session') return - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(DEBUG, 'Exception on channel: ' + str(e)) self._log(DEBUG, util.tb_strings()) return @@ -101,8 +100,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): request_number = msg.get_int() try: self._process(t, request_number, msg) - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(DEBUG, 'Exception in server processing: ' + str(e)) self._log(DEBUG, util.tb_strings()) # send some kind of failure message, at least diff --git a/paramiko/transport.py b/paramiko/transport.py index 2008ecb..6ab9274 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -295,8 +295,8 @@ class Transport (threading.Thread): sock = socket.socket(af, socket.SOCK_STREAM) try: retry_on_signal(lambda: sock.connect((hostname, port))) - except socket.error: - reason = str(sys.exc_info()[1]) + except socket.error as e: + reason = str(e) else: break else: @@ -1184,10 +1184,9 @@ class Transport (threading.Thread): return [] try: return self.auth_handler.wait_for_response(my_event) - except BadAuthenticationType: - x = sys.exc_info()[1] + except BadAuthenticationType as e: # if password auth isn't allowed, but keyboard-interactive *is*, try to fudge it - if not fallback or ('keyboard-interactive' not in x.allowed_types): + if not fallback or ('keyboard-interactive' not in e.allowed_types): raise try: def handler(title, instructions, fields): @@ -1203,7 +1202,7 @@ class Transport (threading.Thread): return self.auth_interactive(username, handler) except SSHException: # attempt failed; just raise the original exception - raise x + raise e return None def auth_publickey(self, username, key, event=None): @@ -1608,26 +1607,22 @@ class Transport (threading.Thread): msg.add_byte(cMSG_UNIMPLEMENTED) msg.add_int(m.seqno) self._send_message(msg) - except SSHException: - e = sys.exc_info()[1] + except SSHException as e: self._log(ERROR, 'Exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e - except EOFError: - e = sys.exc_info()[1] + except EOFError as e: self._log(DEBUG, 'EOF in transport thread') #self._log(DEBUG, util.tb_strings()) self.saved_exception = e - except socket.error: - e = sys.exc_info()[1] + except socket.error as e: if type(e.args) is tuple: emsg = '%s (%d)' % (e.args[1], e.args[0]) else: emsg = e.args self._log(ERROR, 'Socket exception: ' + emsg) self.saved_exception = e - except Exception: - e = sys.exc_info()[1] + except Exception as e: self._log(ERROR, 'Unknown exception: ' + str(e)) self._log(ERROR, util.tb_strings()) self.saved_exception = e @@ -1687,9 +1682,8 @@ class Transport (threading.Thread): buf = self.packetizer.readline(timeout) except ProxyCommandFailure: raise - except Exception: - x = sys.exc_info()[1] - raise SSHException('Error reading SSH protocol banner' + str(x)) + except Exception as e: + raise SSHException('Error reading SSH protocol banner' + str(e)) if buf[:4] == 'SSH-': break self._log(DEBUG, 'Banner: ' + buf) diff --git a/paramiko/util.py b/paramiko/util.py index 71fc467..e9f6250 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -272,8 +272,7 @@ def retry_on_signal(function): while True: try: return function() - except EnvironmentError: - e = sys.exc_info()[1] + except EnvironmentError as e: if e.errno != errno.EINTR: raise diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 26ca13b..58e4be2 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -40,8 +40,7 @@ class StubSFTPHandle (SFTPHandle): def stat(self): try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def chattr(self, attr): @@ -50,8 +49,7 @@ class StubSFTPHandle (SFTPHandle): try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) @@ -73,24 +71,21 @@ class StubSFTPServer (SFTPServerInterface): attr.filename = fname out.append(attr) return out - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def stat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.stat(path)) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def lstat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) def open(self, path, flags, attr): @@ -105,8 +100,7 @@ class StubSFTPServer (SFTPServerInterface): # os.open() defaults to 0777 which is # an odd default mode for files fd = os.open(path, flags, o666) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) if (flags & os.O_CREAT) and (attr is not None): attr._flags &= ~attr.FLAG_PERMISSIONS @@ -126,8 +120,7 @@ class StubSFTPServer (SFTPServerInterface): fstr = 'rb' try: f = os.fdopen(fd, fstr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path @@ -139,8 +132,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.remove(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -149,8 +141,7 @@ class StubSFTPServer (SFTPServerInterface): newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -160,8 +151,7 @@ class StubSFTPServer (SFTPServerInterface): os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -169,8 +159,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: os.rmdir(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -178,8 +167,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -199,8 +187,7 @@ class StubSFTPServer (SFTPServerInterface): target_path = '' try: os.symlink(target_path, path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK @@ -208,8 +195,7 @@ class StubSFTPServer (SFTPServerInterface): path = self._realpath(path) try: symlink = os.readlink(path) - except OSError: - e = sys.exc_info()[1] + except OSError as e: return SFTPServer.convert_errno(e.errno) # if it's absolute, remove the root if os.path.isabs(symlink): diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 4a41258..1a7ad02 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -650,8 +650,7 @@ class SFTPTest (unittest.TestCase): try: sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) sftp.open(b(FOLDER) + utf8_folder, 'r') - except Exception: - e = sys.exc_info()[1] + except Exception as e: self.fail('exception ' + str(e)) sftp.unlink(b(FOLDER) + utf8_folder) diff --git a/tests/test_transport.py b/tests/test_transport.py index 397b00c..6f6843b 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -302,9 +302,8 @@ class TransportTest(ParamikoTest): try: chan = self.tc.open_channel('bogus') self.fail('expected exception') - except ChannelException: - x = sys.exc_info()[1] - self.assertTrue(x.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) + except ChannelException as e: + self.assertTrue(e.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED) def test_9_exit_status(self): """ From 7471515fffd2315f7330c5e1dd42f353982ded6c Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:07:46 -0800 Subject: [PATCH 44/56] Remove eval that was required for Py25 support --- tests/test_sftp.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 1a7ad02..70fd910 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -72,8 +72,10 @@ FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing000') sftp = None tc = None g_big_file_test = True +# we need to use eval(compile()) here because Py3.2 doesn't support the 'u' marker for unicode +# this test is the only line in the entire program that has to be treated specially to support Py3.2 unicode_folder = eval(compile(r"u'\u00fcnic\u00f8de'" if PY2 else r"'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) -utf8_folder = eval(compile(r"'/\xc3\xbcnic\xc3\xb8\x64\x65'" if PY2 else r"b'/\xc3\xbcnic\xc3\xb8\x64\x65'", 'test_sftp.py', 'eval')) +utf8_folder = b'/\xc3\xbcnic\xc3\xb8\x64\x65' def get_sftp(): global sftp From 2da5f1fb4565d1e6dc72672f640dac44b122d235 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:56:53 -0800 Subject: [PATCH 45/56] Use 'with' for opening most file and SFTPFIle objects --- demos/demo_sftp.py | 3 +- paramiko/auth_handler.py | 4 +- paramiko/client.py | 9 +- paramiko/hostkeys.py | 10 +- paramiko/pkey.py | 10 +- paramiko/primes.py | 5 +- paramiko/sftp_client.py | 28 ++-- paramiko/sftp_server.py | 5 +- tests/test_hostkeys.py | 5 +- tests/test_sftp.py | 266 +++++++++++++++++-------------------- tests/test_sftp_big.py | 276 ++++++++++++++++++--------------------- tests/test_util.py | 5 +- 12 files changed, 272 insertions(+), 354 deletions(-) diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 1cccffc..9476f76 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -96,7 +96,8 @@ try: sftp.mkdir("demo_sftp_folder") except IOError: print('(assuming demo_sftp_folder/ already exists)') - sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n') + with sftp.open('demo_sftp_folder/README', 'w') as f: + f.write('This was created by demo_sftp.py.\n') with open('demo_sftp.py', 'r') as f: data = f.read() sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data) diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 3e389da..5046cac 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -93,7 +93,7 @@ class AuthHandler (object): self._request_auth() finally: self.transport.lock.release() - + def auth_interactive(self, username, handler, event, submethods=''): """ response_list = handler(title, instructions, prompt_list) @@ -108,7 +108,7 @@ class AuthHandler (object): self._request_auth() finally: self.transport.lock.release() - + def abort(self): if self.auth_event is not None: self.auth_event.set() diff --git a/paramiko/client.py b/paramiko/client.py index 3ce0168..7cf897f 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -192,11 +192,10 @@ class SSHClient (object): if self.known_hosts is not None: self.load_host_keys(self.known_hosts) - f = open(filename, 'w') - for hostname, keys in self._host_keys.items(): - for keytype, key in keys.items(): - f.write('%s %s %s\n' % (hostname, keytype, key.get_base64())) - f.close() + with open(filename, 'w') as f: + for hostname, keys in self._host_keys.items(): + for keytype, key in keys.items(): + f.write('%s %s %s\n' % (hostname, keytype, key.get_base64())) def get_host_keys(self): """ diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 16f7f90..cf586a2 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -172,8 +172,7 @@ class HostKeys (MutableMapping): @raise IOError: if there was an error reading the file """ - f = open(filename, 'r') - try: + with open(filename, 'r') as f: for lineno, line in enumerate(f): line = line.strip() if (len(line) == 0) or (line[0] == '#'): @@ -186,8 +185,6 @@ class HostKeys (MutableMapping): e.hostnames.remove(h) if len(e.hostnames): self._entries.append(e) - finally: - f.close() def save(self, filename): """ @@ -203,14 +200,11 @@ class HostKeys (MutableMapping): @since: 1.6.1 """ - f = open(filename, 'w') - try: + with open(filename, 'w') as f: for e in self._entries: line = e.to_line() if line: f.write(line) - finally: - f.close() def lookup(self, hostname): """ diff --git a/paramiko/pkey.py b/paramiko/pkey.py index a2f27ed..0a174ca 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -282,11 +282,8 @@ class PKey (object): encrypted, and C{password} is C{None}. @raise SSHException: if the key file is invalid. """ - f = open(filename, 'r') - try: + with open(filename, 'r') as f: data = self._read_private_key(tag, f, password) - finally: - f.close() return data def _read_private_key(self, tag, f, password=None): @@ -354,13 +351,10 @@ class PKey (object): @raise IOError: if there was an error writing the file. """ - f = open(filename, 'w', o600) - try: + with open(filename, 'w', o600) as f: # grrr... the mode doesn't always take hold os.chmod(filename, o600) self._write_private_key(tag, f, data, password) - finally: - f.close() def _write_private_key(self, tag, f, data, password=None): f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag) diff --git a/paramiko/primes.py b/paramiko/primes.py index 13ec52d..d10a15f 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -113,8 +113,7 @@ class ModulusPack (object): @raise IOError: passed from any file operations that fail. """ self.pack = {} - f = open(filename, 'r') - try: + with open(filename, 'r') as f: for line in f: line = line.strip() if (len(line) == 0) or (line[0] == '#'): @@ -123,8 +122,6 @@ class ModulusPack (object): self._parse_modulus(line) except: continue - finally: - f.close() def get_modulus(self, min, prefer, max): bitsizes = sorted(self.pack.keys()) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 1082c6c..2b3eb18 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -561,10 +561,9 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ - fr = self.file(remotepath, 'wb') - fr.set_pipelined(True) - size = 0 - try: + with self.file(remotepath, 'wb') as fr: + fr.set_pipelined(True) + size = 0 while True: data = fl.read(32768) fr.write(data) @@ -573,8 +572,6 @@ class SFTPClient (BaseSFTP): callback(size, file_size) if len(data) == 0: break - finally: - fr.close() if confirm: s = self.stat(remotepath) if s.st_size != size: @@ -610,11 +607,8 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ file_size = os.stat(localpath).st_size - fl = open(localpath, 'rb') - try: + with open(localpath, 'rb') as fl: return self.putfo(fl, remotepath, os.stat(localpath).st_size, callback, confirm) - finally: - fl.close() def getfo(self, remotepath, fl, callback=None): """ @@ -636,10 +630,9 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ - fr = self.open(remotepath, 'rb') - file_size = self.stat(remotepath).st_size - fr.prefetch() - try: + with self.open(remotepath, 'rb') as fr: + file_size = self.stat(remotepath).st_size + fr.prefetch() size = 0 while True: data = fr.read(32768) @@ -649,8 +642,6 @@ class SFTPClient (BaseSFTP): callback(size, file_size) if len(data) == 0: break - finally: - fr.close() return size def get(self, remotepath, localpath, callback=None): @@ -671,11 +662,8 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ file_size = self.stat(remotepath).st_size - fl = open(localpath, 'wb') - try: + with open(localpath, 'wb') as fl: size = self.getfo(remotepath, fl, callback) - finally: - fl.close() s = os.stat(localpath) if s.st_size != size: raise IOError('size mismatch in get! %d != %d' % (s.st_size, size)) diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 7dcef6b..4539ede 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -166,11 +166,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): if attr._flags & attr.FLAG_AMTIME: os.utime(filename, (attr.st_atime, attr.st_mtime)) if attr._flags & attr.FLAG_SIZE: - f = open(filename, 'w+') - try: + with open(filename, 'w+') as f: f.truncate(attr.st_size) - finally: - f.close() set_file_attr = staticmethod(set_file_attr) diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index a7621d8..5290a4d 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -56,9 +56,8 @@ Ngw3qIch/WgRmMHy4kBq1SsXMjQCte1So6HBMvBPIW5SiMTmjCfZZiw4AYHK+B/JaOwaG9yRg2Ejg\ class HostKeysTest (unittest.TestCase): def setUp(self): - f = open('hostfile.temp', 'w') - f.write(test_hosts_file) - f.close() + with open('hostfile.temp', 'w') as f: + f.write(test_hosts_file) def tearDown(self): os.unlink('hostfile.temp') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 70fd910..e3dc788 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -186,10 +186,9 @@ class SFTPTest (unittest.TestCase): """ verify that a file can be created and written, and the size is correct. """ - f = sftp.open(FOLDER + '/duck.txt', 'w') try: - f.write(ARTICLE) - f.close() + with sftp.open(FOLDER + '/duck.txt', 'w') as f: + f.write(ARTICLE) self.assertEqual(sftp.stat(FOLDER + '/duck.txt').st_size, 1483) finally: sftp.remove(FOLDER + '/duck.txt') @@ -209,40 +208,36 @@ class SFTPTest (unittest.TestCase): """ verify that a file can be opened for append, and tell() still works. """ - f = sftp.open(FOLDER + '/append.txt', 'w') try: - f.write('first line\nsecond line\n') - self.assertEqual(f.tell(), 23) - f.close() + with sftp.open(FOLDER + '/append.txt', 'w') as f: + f.write('first line\nsecond line\n') + self.assertEqual(f.tell(), 23) - f = sftp.open(FOLDER + '/append.txt', 'a+') - f.write('third line!!!\n') - self.assertEqual(f.tell(), 37) - self.assertEqual(f.stat().st_size, 37) - f.seek(-26, f.SEEK_CUR) - self.assertEqual(f.readline(), 'second line\n') + with sftp.open(FOLDER + '/append.txt', 'a+') as f: + f.write('third line!!!\n') + self.assertEqual(f.tell(), 37) + self.assertEqual(f.stat().st_size, 37) + f.seek(-26, f.SEEK_CUR) + self.assertEqual(f.readline(), 'second line\n') finally: - f.close() sftp.remove(FOLDER + '/append.txt') def test_5_rename(self): """ verify that renaming a file works. """ - f = sftp.open(FOLDER + '/first.txt', 'w') try: - f.write('content!\n') - f.close() + with sftp.open(FOLDER + '/first.txt', 'w') as f: + f.write('content!\n') sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt') try: f = sftp.open(FOLDER + '/first.txt', 'r') self.assertTrue(False, 'no exception on reading nonexistent file') except IOError: pass - f = sftp.open(FOLDER + '/second.txt', 'r') - f.seek(-6, f.SEEK_END) - self.assertEqual(u(f.read(4)), 'tent') - f.close() + with sftp.open(FOLDER + '/second.txt', 'r') as f: + f.seek(-6, f.SEEK_END) + self.assertEqual(u(f.read(4)), 'tent') finally: try: sftp.remove(FOLDER + '/first.txt') @@ -300,10 +295,9 @@ class SFTPTest (unittest.TestCase): """ verify that the setstat functions (chown, chmod, utime, truncate) work. """ - f = sftp.open(FOLDER + '/special', 'w') try: - f.write('x' * 1024) - f.close() + with sftp.open(FOLDER + '/special', 'w') as f: + f.write('x' * 1024) stat = sftp.stat(FOLDER + '/special') sftp.chmod(FOLDER + '/special', (stat.st_mode & ~o777) | o600) @@ -339,40 +333,38 @@ class SFTPTest (unittest.TestCase): verify that the fsetstat functions (chown, chmod, utime, truncate) work on open files. """ - f = sftp.open(FOLDER + '/special', 'w') try: - f.write('x' * 1024) - f.close() + with sftp.open(FOLDER + '/special', 'w') as f: + f.write('x' * 1024) - f = sftp.open(FOLDER + '/special', 'r+') - stat = f.stat() - f.chmod((stat.st_mode & ~o777) | o600) - stat = f.stat() + with sftp.open(FOLDER + '/special', 'r+') as f: + stat = f.stat() + f.chmod((stat.st_mode & ~o777) | o600) + stat = f.stat() - expected_mode = o600 - if sys.platform == 'win32': - # chmod not really functional on windows - expected_mode = o666 - if sys.platform == 'cygwin': - # even worse. - expected_mode = o644 - self.assertEqual(stat.st_mode & o777, expected_mode) - self.assertEqual(stat.st_size, 1024) + expected_mode = o600 + if sys.platform == 'win32': + # chmod not really functional on windows + expected_mode = o666 + if sys.platform == 'cygwin': + # even worse. + expected_mode = o644 + self.assertEqual(stat.st_mode & o777, expected_mode) + self.assertEqual(stat.st_size, 1024) - mtime = stat.st_mtime - 3600 - atime = stat.st_atime - 1800 - f.utime((atime, mtime)) - stat = f.stat() - self.assertEqual(stat.st_mtime, mtime) - if sys.platform not in ('win32', 'cygwin'): - self.assertEqual(stat.st_atime, atime) + mtime = stat.st_mtime - 3600 + atime = stat.st_atime - 1800 + f.utime((atime, mtime)) + stat = f.stat() + self.assertEqual(stat.st_mtime, mtime) + if sys.platform not in ('win32', 'cygwin'): + self.assertEqual(stat.st_atime, atime) - # can't really test chown, since we'd have to know a valid uid. + # can't really test chown, since we'd have to know a valid uid. - f.truncate(512) - stat = f.stat() - self.assertEqual(stat.st_size, 512) - f.close() + f.truncate(512) + stat = f.stat() + self.assertEqual(stat.st_size, 512) finally: sftp.remove(FOLDER + '/special') @@ -384,25 +376,23 @@ class SFTPTest (unittest.TestCase): buffering is reset on 'seek'. """ try: - f = sftp.open(FOLDER + '/duck.txt', 'w') - f.write(ARTICLE) - f.close() + with sftp.open(FOLDER + '/duck.txt', 'w') as f: + f.write(ARTICLE) - f = sftp.open(FOLDER + '/duck.txt', 'r+') - line_number = 0 - loc = 0 - pos_list = [] - for line in f: - line_number += 1 - pos_list.append(loc) - loc = f.tell() - f.seek(pos_list[6], f.SEEK_SET) - self.assertEqual(f.readline(), 'Nouzilly, France.\n') - f.seek(pos_list[17], f.SEEK_SET) - self.assertEqual(f.readline()[:4], 'duck') - f.seek(pos_list[10], f.SEEK_SET) - self.assertEqual(f.readline(), 'duck types were equally resistant to exogenous insulin compared with chicken.\n') - f.close() + with sftp.open(FOLDER + '/duck.txt', 'r+') as f: + line_number = 0 + loc = 0 + pos_list = [] + for line in f: + line_number += 1 + pos_list.append(loc) + loc = f.tell() + f.seek(pos_list[6], f.SEEK_SET) + self.assertEqual(f.readline(), 'Nouzilly, France.\n') + f.seek(pos_list[17], f.SEEK_SET) + self.assertEqual(f.readline()[:4], 'duck') + f.seek(pos_list[10], f.SEEK_SET) + self.assertEqual(f.readline(), 'duck types were equally resistant to exogenous insulin compared with chicken.\n') finally: sftp.remove(FOLDER + '/duck.txt') @@ -411,17 +401,15 @@ class SFTPTest (unittest.TestCase): create a text file, seek back and change part of it, and verify that the changes worked. """ - f = sftp.open(FOLDER + '/testing.txt', 'w') try: - f.write('hello kitty.\n') - f.seek(-5, f.SEEK_CUR) - f.write('dd') - f.close() + with sftp.open(FOLDER + '/testing.txt', 'w') as f: + f.write('hello kitty.\n') + f.seek(-5, f.SEEK_CUR) + f.write('dd') self.assertEqual(sftp.stat(FOLDER + '/testing.txt').st_size, 13) - f = sftp.open(FOLDER + '/testing.txt', 'r') - data = f.read(20) - f.close() + with sftp.open(FOLDER + '/testing.txt', 'r') as f: + data = f.read(20) self.assertEqual(data, 'hello kiddy.\n') finally: sftp.remove(FOLDER + '/testing.txt') @@ -434,16 +422,14 @@ class SFTPTest (unittest.TestCase): # skip symlink tests on windows return - f = sftp.open(FOLDER + '/original.txt', 'w') try: - f.write('original\n') - f.close() + with sftp.open(FOLDER + '/original.txt', 'w') as f: + f.write('original\n') sftp.symlink('original.txt', FOLDER + '/link.txt') self.assertEqual(sftp.readlink(FOLDER + '/link.txt'), 'original.txt') - f = sftp.open(FOLDER + '/link.txt', 'r') - self.assertEqual(f.readlines(), ['original\n']) - f.close() + with sftp.open(FOLDER + '/link.txt', 'r') as f: + self.assertEqual(f.readlines(), ['original\n']) cwd = sftp.normalize('.') if cwd[-1] == '/': @@ -477,18 +463,16 @@ class SFTPTest (unittest.TestCase): """ verify that buffered writes are automatically flushed on seek. """ - f = sftp.open(FOLDER + '/happy.txt', 'w', 1) try: - f.write('full line.\n') - f.write('partial') - f.seek(9, f.SEEK_SET) - f.write('?\n') - f.close() + with sftp.open(FOLDER + '/happy.txt', 'w', 1) as f: + f.write('full line.\n') + f.write('partial') + f.seek(9, f.SEEK_SET) + f.write('?\n') - f = sftp.open(FOLDER + '/happy.txt', 'r') - self.assertEqual(f.readline(), 'full line?\n') - self.assertEqual(f.read(7), 'partial') - f.close() + with sftp.open(FOLDER + '/happy.txt', 'r') as f: + self.assertEqual(f.readline(), 'full line?\n') + self.assertEqual(f.read(7), 'partial') finally: try: sftp.remove(FOLDER + '/happy.txt') @@ -544,9 +528,8 @@ class SFTPTest (unittest.TestCase): self.assertEqual(['beta'], sftp.listdir('.')) sftp.chdir('beta') - f = sftp.open('fish', 'w') - f.write('hello\n') - f.close() + with sftp.open('fish', 'w') as f: + f.write('hello\n') sftp.chdir('..') self.assertEqual(['fish'], sftp.listdir('beta')) sftp.chdir('..') @@ -575,17 +558,15 @@ class SFTPTest (unittest.TestCase): fd, localname = mkstemp() os.close(fd) text = b('All I wanted was a plastic bunny rabbit.\n') - f = open(localname, 'wb') - f.write(text) - f.close() + with open(localname, 'wb') as f: + f.write(text) saved_progress = [] def progress_callback(x, y): saved_progress.append((x, y)) sftp.put(localname, FOLDER + '/bunny.txt', progress_callback) - f = sftp.open(FOLDER + '/bunny.txt', 'rb') - self.assertEqual(text, f.read(128)) - f.close() + with sftp.open(FOLDER + '/bunny.txt', 'rb') as f: + self.assertEqual(text, f.read(128)) self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) @@ -594,9 +575,8 @@ class SFTPTest (unittest.TestCase): saved_progress = [] sftp.get(FOLDER + '/bunny.txt', localname, progress_callback) - f = open(localname, 'rb') - self.assertEqual(text, f.read(128)) - f.close() + with open(localname, 'rb') as f: + self.assertEqual(text, f.read(128)) self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) @@ -608,20 +588,18 @@ class SFTPTest (unittest.TestCase): (it's an sftp extension that we support, and may be the only ones who support it.) """ - f = sftp.open(FOLDER + '/kitty.txt', 'w') - f.write('here kitty kitty' * 64) - f.close() + with sftp.open(FOLDER + '/kitty.txt', 'w') as f: + f.write('here kitty kitty' * 64) try: - f = sftp.open(FOLDER + '/kitty.txt', 'r') - sum = f.check('sha1') - self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', u(hexlify(sum)).upper()) - sum = f.check('md5', 0, 512) - self.assertEqual('93DE4788FCA28D471516963A1FE3856A', u(hexlify(sum)).upper()) - sum = f.check('md5', 0, 0, 510) - self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', - u(hexlify(sum)).upper()) - f.close() + with sftp.open(FOLDER + '/kitty.txt', 'r') as f: + sum = f.check('sha1') + self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', u(hexlify(sum)).upper()) + sum = f.check('md5', 0, 512) + self.assertEqual('93DE4788FCA28D471516963A1FE3856A', u(hexlify(sum)).upper()) + sum = f.check('md5', 0, 0, 510) + self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', + u(hexlify(sum)).upper()) finally: sftp.unlink(FOLDER + '/kitty.txt') @@ -645,9 +623,8 @@ class SFTPTest (unittest.TestCase): """ verify that unicode strings are encoded into utf8 correctly. """ - f = sftp.open(FOLDER + '/something', 'w') - f.write('okay') - f.close() + with sftp.open(FOLDER + '/something', 'w') as f: + f.write('okay') try: sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder) @@ -660,9 +637,8 @@ class SFTPTest (unittest.TestCase): sftp.mkdir(FOLDER + '/' + unicode_folder) try: sftp.chdir(FOLDER + '/' + unicode_folder) - f = sftp.open('something', 'w') - f.write('okay') - f.close() + with sftp.open('something', 'w') as f: + f.write('okay') sftp.unlink('something') finally: sftp.chdir() @@ -675,14 +651,12 @@ class SFTPTest (unittest.TestCase): f = sftp.open(FOLDER + '/zero', 'w') f.close() try: - f = sftp.open(FOLDER + '/zero', 'r') - f.readv([(0, 12)]) - f.close() + with sftp.open(FOLDER + '/zero', 'r') as f: + f.readv([(0, 12)]) - f = sftp.open(FOLDER + '/zero', 'r') - f.prefetch() - f.read(100) - f.close() + with sftp.open(FOLDER + '/zero', 'r') as f: + f.prefetch() + f.read(100) finally: sftp.unlink(FOLDER + '/zero') @@ -695,9 +669,8 @@ class SFTPTest (unittest.TestCase): fd, localname = mkstemp() os.close(fd) text = 'All I wanted was a plastic bunny rabbit.\n' - f = open(localname, 'w') - f.write(text) - f.close() + with open(localname, 'w') as f: + f.write(text) saved_progress = [] def progress_callback(x, y): saved_progress.append((x, y)) @@ -705,9 +678,8 @@ class SFTPTest (unittest.TestCase): self.assertEqual(SFTPAttributes().attr, res.attr) - f = sftp.open(FOLDER + '/bunny.txt', 'r') - self.assertEqual(text, f.read(128)) - f.close() + with sftp.open(FOLDER + '/bunny.txt', 'r') as f: + self.assertEqual(text, f.read(128)) self.assertEqual((41, 41), saved_progress[-1]) os.unlink(localname) @@ -719,19 +691,17 @@ class SFTPTest (unittest.TestCase): does not work except through paramiko. :( openssh fails. """ - f = sftp.open(FOLDER + '/append.txt', 'a') try: - f.write('first line\nsecond line\n') - f.seek(11, f.SEEK_SET) - f.write('third line\n') - f.close() + with sftp.open(FOLDER + '/append.txt', 'a') as f: + f.write('first line\nsecond line\n') + f.seek(11, f.SEEK_SET) + f.write('third line\n') - f = sftp.open(FOLDER + '/append.txt', 'r') - self.assertEqual(f.stat().st_size, 34) - self.assertEqual(f.readline(), 'first line\n') - self.assertEqual(f.readline(), 'second line\n') - self.assertEqual(f.readline(), 'third line\n') - f.close() + with sftp.open(FOLDER + '/append.txt', 'r') as f: + self.assertEqual(f.stat().st_size, 34) + self.assertEqual(f.readline(), 'first line\n') + self.assertEqual(f.readline(), 'second line\n') + self.assertEqual(f.readline(), 'third line\n') finally: sftp.remove(FOLDER + '/append.txt') diff --git a/tests/test_sftp_big.py b/tests/test_sftp_big.py index dc9cba9..6870c6b 100644 --- a/tests/test_sftp_big.py +++ b/tests/test_sftp_big.py @@ -66,9 +66,8 @@ class BigSFTPTest (unittest.TestCase): numfiles = 100 try: for i in range(numfiles): - f = sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1) - f.write('this is file #%d.\n' % i) - f.close() + with sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1) as f: + f.write('this is file #%d.\n' % i) sftp.chmod('%s/file%d.txt' % (FOLDER, i), o660) # now make sure every file is there, by creating a list of filenmes @@ -76,9 +75,8 @@ class BigSFTPTest (unittest.TestCase): numlist = list(range(numfiles)) while len(numlist) > 0: r = numlist[random.randint(0, len(numlist) - 1)] - f = sftp.open('%s/file%d.txt' % (FOLDER, r)) - self.assertEqual(f.readline(), 'this is file #%d.\n' % r) - f.close() + with sftp.open('%s/file%d.txt' % (FOLDER, r)) as f: + self.assertEqual(f.readline(), 'this is file #%d.\n' % r) numlist.remove(r) finally: for i in range(numfiles): @@ -95,12 +93,11 @@ class BigSFTPTest (unittest.TestCase): kblob = (1024 * 'x') start = time.time() try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'w') as f: + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) @@ -108,11 +105,10 @@ class BigSFTPTest (unittest.TestCase): sys.stderr.write('%ds ' % round(end - start)) start = time.time() - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') - for n in range(1024): - data = f.read(1024) - self.assertEqual(data, kblob) - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f: + for n in range(1024): + data = f.read(1024) + self.assertEqual(data, kblob) end = time.time() sys.stderr.write('%ds ' % round(end - start)) @@ -127,13 +123,12 @@ class BigSFTPTest (unittest.TestCase): kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) start = time.time() try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) @@ -141,22 +136,21 @@ class BigSFTPTest (unittest.TestCase): sys.stderr.write('%ds ' % round(end - start)) start = time.time() - f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') - f.prefetch() + with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f: + f.prefetch() - # read on odd boundaries to make sure the bytes aren't getting scrambled - n = 0 - k2blob = kblob + kblob - chunk = 629 - size = 1024 * 1024 - while n < size: - if n + chunk > size: - chunk = size - n - data = f.read(chunk) - offset = n % 1024 - self.assertEqual(data, k2blob[offset:offset + chunk]) - n += chunk - f.close() + # read on odd boundaries to make sure the bytes aren't getting scrambled + n = 0 + k2blob = kblob + kblob + chunk = 629 + size = 1024 * 1024 + while n < size: + if n + chunk > size: + chunk = size - n + data = f.read(chunk) + offset = n % 1024 + self.assertEqual(data, k2blob[offset:offset + chunk]) + n += chunk end = time.time() sys.stderr.write('%ds ' % round(end - start)) @@ -167,13 +161,12 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) @@ -182,20 +175,19 @@ class BigSFTPTest (unittest.TestCase): k2blob = kblob + kblob chunk = 793 for i in range(10): - f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') - f.prefetch() - base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) - offsets = [base_offset + j * chunk for j in range(100)] - # randomly seek around and read them out - for j in range(100): - offset = offsets[random.randint(0, len(offsets) - 1)] - offsets.remove(offset) - f.seek(offset) - data = f.read(chunk) - n_offset = offset % 1024 - self.assertEqual(data, k2blob[n_offset:n_offset + chunk]) - offset += chunk - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f: + f.prefetch() + base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) + offsets = [base_offset + j * chunk for j in range(100)] + # randomly seek around and read them out + for j in range(100): + offset = offsets[random.randint(0, len(offsets) - 1)] + offsets.remove(offset) + f.seek(offset) + data = f.read(chunk) + n_offset = offset % 1024 + self.assertEqual(data, k2blob[n_offset:n_offset + chunk]) + offset += chunk end = time.time() sys.stderr.write('%ds ' % round(end - start)) finally: @@ -205,13 +197,12 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) @@ -220,21 +211,20 @@ class BigSFTPTest (unittest.TestCase): k2blob = kblob + kblob chunk = 793 for i in range(10): - f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') - base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) - # make a bunch of offsets and put them in random order - offsets = [base_offset + j * chunk for j in range(100)] - readv_list = [] - for j in range(100): - o = offsets[random.randint(0, len(offsets) - 1)] - offsets.remove(o) - readv_list.append((o, chunk)) - ret = f.readv(readv_list) - for i in range(len(readv_list)): - offset = readv_list[i][0] - n_offset = offset % 1024 - self.assertEqual(next(ret), k2blob[n_offset:n_offset + chunk]) - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f: + base_offset = (512 * 1024) + 17 * random.randint(1000, 2000) + # make a bunch of offsets and put them in random order + offsets = [base_offset + j * chunk for j in range(100)] + readv_list = [] + for j in range(100): + o = offsets[random.randint(0, len(offsets) - 1)] + offsets.remove(o) + readv_list.append((o, chunk)) + ret = f.readv(readv_list) + for i in range(len(readv_list)): + offset = readv_list[i][0] + n_offset = offset % 1024 + self.assertEqual(next(ret), k2blob[n_offset:n_offset + chunk]) end = time.time() sys.stderr.write('%ds ' % round(end - start)) finally: @@ -248,29 +238,26 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = (1024 * 'x') try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'w') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) for i in range(10): - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') + with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f: + f.prefetch() + with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f: f.prefetch() - f.close() - f = sftp.open('%s/hongry.txt' % FOLDER, 'r') - f.prefetch() - for n in range(1024): - data = f.read(1024) - self.assertEqual(data, kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + for n in range(1024): + data = f.read(1024) + self.assertEqual(data, kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') finally: sftp.remove('%s/hongry.txt' % FOLDER) @@ -282,33 +269,31 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) - f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') - f.prefetch() - data = f.read(1024) - self.assertEqual(data, kblob) - - chunk_size = 793 - base_offset = 512 * 1024 - k2blob = kblob + kblob - chunks = [(base_offset + (chunk_size * i), chunk_size) for i in range(20)] - for data in f.readv(chunks): - offset = base_offset % 1024 - self.assertEqual(chunk_size, len(data)) - self.assertEqual(k2blob[offset:offset + chunk_size], data) - base_offset += chunk_size + with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f: + f.prefetch() + data = f.read(1024) + self.assertEqual(data, kblob) + + chunk_size = 793 + base_offset = 512 * 1024 + k2blob = kblob + kblob + chunks = [(base_offset + (chunk_size * i), chunk_size) for i in range(20)] + for data in f.readv(chunks): + offset = base_offset % 1024 + self.assertEqual(chunk_size, len(data)) + self.assertEqual(k2blob[offset:offset + chunk_size], data) + base_offset += chunk_size - f.close() sys.stderr.write(' ') finally: sftp.remove('%s/hongry.txt' % FOLDER) @@ -321,24 +306,22 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() kblob = bytes().join([struct.pack('>H', n) for n in range(512)]) try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'wb') - f.set_pipelined(True) - for n in range(1024): - f.write(kblob) - if n % 128 == 0: - sys.stderr.write('.') - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f: + f.set_pipelined(True) + for n in range(1024): + f.write(kblob) + if n % 128 == 0: + sys.stderr.write('.') sys.stderr.write(' ') self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) - f = sftp.open('%s/hongry.txt' % FOLDER, 'rb') - data = list(f.readv([(23 * 1024, 128 * 1024)])) - self.assertEqual(1, len(data)) - data = data[0] - self.assertEqual(128 * 1024, len(data)) + with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f: + data = list(f.readv([(23 * 1024, 128 * 1024)])) + self.assertEqual(1, len(data)) + data = data[0] + self.assertEqual(128 * 1024, len(data)) - f.close() sys.stderr.write(' ') finally: sftp.remove('%s/hongry.txt' % FOLDER) @@ -350,9 +333,8 @@ class BigSFTPTest (unittest.TestCase): sftp = get_sftp() mblob = (1024 * 1024 * 'x') try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) - f.write(mblob) - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) as f: + f.write(mblob) self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) finally: @@ -367,21 +349,19 @@ class BigSFTPTest (unittest.TestCase): t.packetizer.REKEY_BYTES = 512 * 1024 k32blob = (32 * 1024 * 'x') try: - f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) - for i in range(32): - f.write(k32blob) - f.close() - + with sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) as f: + for i in range(32): + f.write(k32blob) + self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024) self.assertNotEqual(t.H, t.session_id) # try to read it too. - f = sftp.open('%s/hongry.txt' % FOLDER, 'r', 128 * 1024) - f.prefetch() - total = 0 - while total < 1024 * 1024: - total += len(f.read(32 * 1024)) - f.close() + with sftp.open('%s/hongry.txt' % FOLDER, 'r', 128 * 1024) as f: + f.prefetch() + total = 0 + while total < 1024 * 1024: + total += len(f.read(32 * 1024)) finally: sftp.remove('%s/hongry.txt' % FOLDER) t.packetizer.REKEY_BYTES = pow(2, 30) diff --git a/tests/test_util.py b/tests/test_util.py index dba5223..be10719 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -142,9 +142,8 @@ class UtilTest(ParamikoTest): self.assertEqual(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') def test_5_host_keys(self): - f = open('hostfile.temp', 'w') - f.write(test_hosts_file) - f.close() + with open('hostfile.temp', 'w') as f: + f.write(test_hosts_file) try: hostdict = paramiko.util.load_host_keys('hostfile.temp') self.assertEqual(2, len(hostdict)) From 106f9ea444ee9ed8246be128f232217b6c4977b8 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 08:56:53 -0800 Subject: [PATCH 46/56] Use 'with' for opening most file and SFTPFIle objects --- demos/demo_sftp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index 9476f76..c04d6e1 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -104,7 +104,8 @@ try: print('created demo_sftp_folder/ on the server') # copy the README back here - data = sftp.open('demo_sftp_folder/README', 'r').read() + with sftp.open('demo_sftp_folder/README', 'r') as f: + data = f.read() with open('README_demo_sftp', 'w') as f: f.write(data) print('copied README back here') From dcc78768bf2063bd63e9c7260ff74ea1dbccb89c Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 09:29:08 -0800 Subject: [PATCH 47/56] Remove unnecessary vars for open --- tests/test_sftp.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index e3dc788..96315c6 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -231,7 +231,7 @@ class SFTPTest (unittest.TestCase): f.write('content!\n') sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt') try: - f = sftp.open(FOLDER + '/first.txt', 'r') + sftp.open(FOLDER + '/first.txt', 'r') self.assertTrue(False, 'no exception on reading nonexistent file') except IOError: pass @@ -254,12 +254,11 @@ class SFTPTest (unittest.TestCase): remove the folder and verify that we can't create a file in it anymore. """ sftp.mkdir(FOLDER + '/subfolder') - f = sftp.open(FOLDER + '/subfolder/test', 'w') - f.close() + sftp.open(FOLDER + '/subfolder/test', 'w').close() sftp.remove(FOLDER + '/subfolder/test') sftp.rmdir(FOLDER + '/subfolder') try: - f = sftp.open(FOLDER + '/subfolder/test') + sftp.open(FOLDER + '/subfolder/test') # shouldn't be able to create that file self.assertTrue(False, 'no exception at dummy file creation') except IOError: @@ -271,14 +270,9 @@ class SFTPTest (unittest.TestCase): and those files show up in sftp.listdir. """ try: - f = sftp.open(FOLDER + '/duck.txt', 'w') - f.close() - - f = sftp.open(FOLDER + '/fish.txt', 'w') - f.close() - - f = sftp.open(FOLDER + '/tertiary.py', 'w') - f.close() + sftp.open(FOLDER + '/duck.txt', 'w').close() + sftp.open(FOLDER + '/fish.txt', 'w').close() + sftp.open(FOLDER + '/tertiary.py', 'w').close() x = sftp.listdir(FOLDER) self.assertEqual(len(x), 3) @@ -607,12 +601,11 @@ class SFTPTest (unittest.TestCase): """ verify that the 'x' flag works when opening a file. """ - f = sftp.open(FOLDER + '/unusual.txt', 'wx') - f.close() + sftp.open(FOLDER + '/unusual.txt', 'wx').close() try: try: - f = sftp.open(FOLDER + '/unusual.txt', 'wx') + sftp.open(FOLDER + '/unusual.txt', 'wx') self.fail('expected exception') except IOError: pass @@ -648,8 +641,7 @@ class SFTPTest (unittest.TestCase): """ verify that readv at the end of the file doesn't essplode. """ - f = sftp.open(FOLDER + '/zero', 'w') - f.close() + sftp.open(FOLDER + '/zero', 'w').close() try: with sftp.open(FOLDER + '/zero', 'r') as f: f.readv([(0, 12)]) From 981f768a62402a5aaa9f9f0ddfb4e14faa0d4442 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 09:37:33 -0800 Subject: [PATCH 48/56] Remove `from __future__ import with_statement` --- demos/demo_keygen.py | 1 - demos/demo_sftp.py | 2 -- paramiko/win_pageant.py | 2 -- tests/test_sftp.py | 2 -- 4 files changed, 7 deletions(-) diff --git a/demos/demo_keygen.py b/demos/demo_keygen.py index 88facd4..860ee4e 100755 --- a/demos/demo_keygen.py +++ b/demos/demo_keygen.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -from __future__ import with_statement import sys diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py index c04d6e1..a34f2b1 100755 --- a/demos/demo_sftp.py +++ b/demos/demo_sftp.py @@ -20,8 +20,6 @@ # based on code provided by raymond mosteller (thanks!) -from __future__ import with_statement - import base64 import getpass import os diff --git a/paramiko/win_pageant.py b/paramiko/win_pageant.py index c0d0f4a..3c23f2f 100644 --- a/paramiko/win_pageant.py +++ b/paramiko/win_pageant.py @@ -21,8 +21,6 @@ Functions for communicating with Pageant, the basic windows ssh agent program. """ -from __future__ import with_statement - import struct import threading import array diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 96315c6..f78047b 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -23,8 +23,6 @@ a real actual sftp server is contacted, and a new folder is created there to do test file operations in (so no existing files will be harmed). """ -from __future__ import with_statement - from binascii import hexlify import os import warnings From 6d75c75e643c3a109075e86e72b08221c76088cc Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 10:09:08 -0800 Subject: [PATCH 49/56] Remove byte conversions and unhexlify calls that we only needed for Py2.5 support and use the `b` byte string marker instead --- demos/demo_server.py | 8 +++---- paramiko/ecdsakey.py | 1 - paramiko/pipe.py | 4 ++-- paramiko/rsakey.py | 3 +-- paramiko/sftp_client.py | 2 +- tests/test_buffered_pipe.py | 18 ++++++++-------- tests/test_client.py | 2 +- tests/test_hostkeys.py | 18 ++++++++-------- tests/test_kex.py | 42 ++++++++++++++++++------------------- tests/test_message.py | 9 ++++---- tests/test_packetizer.py | 5 ++--- tests/test_pkey.py | 28 ++++++++++++------------- tests/test_sftp.py | 2 +- tests/test_transport.py | 18 ++++++++-------- tests/test_util.py | 4 ++-- 15 files changed, 80 insertions(+), 84 deletions(-) diff --git a/demos/demo_server.py b/demos/demo_server.py index 34a9bd5..bb35258 100644 --- a/demos/demo_server.py +++ b/demos/demo_server.py @@ -42,10 +42,10 @@ print('Read key: ' + u(hexlify(host_key.get_fingerprint()))) class Server (paramiko.ServerInterface): # 'data' is the output of base64.encodestring(str(key)) # (using the "user_rsa_key" files) - data = b('AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \ - 'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \ - 'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \ - 'UWT10hcuO4Ks8=') + data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + b'UWT10hcuO4Ks8=') good_pub_key = paramiko.RSAKey(data=decodebytes(data)) def __init__(self): diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index 5f9dff2..3ecf0a5 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -21,7 +21,6 @@ L{ECDSAKey} """ import binascii -from binascii import unhexlify from ecdsa import SigningKey, VerifyingKey, der, curves from ecdsa.util import number_to_string, sigencode_string, sigencode_strings, sigdecode_strings from Crypto.Hash import SHA256, MD5 diff --git a/paramiko/pipe.py b/paramiko/pipe.py index 4c96546..619b807 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -64,7 +64,7 @@ class PosixPipe (object): if self._set or self._closed: return self._set = True - os.write(self._wfd, b('*')) + os.write(self._wfd, b'*') def set_forever (self): self._forever = True @@ -110,7 +110,7 @@ class WindowsPipe (object): if self._set or self._closed: return self._set = True - self._wsock.send(b('*')) + self._wsock.send(b'*') def set_forever (self): self._forever = True diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index 0a27119..65f1463 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -20,7 +20,6 @@ L{RSAKey} """ -from binascii import unhexlify from Crypto.PublicKey import RSA from Crypto.Hash import SHA, MD5 from Crypto.Cipher import DES3 @@ -32,7 +31,7 @@ from paramiko.ber import BER, BERException from paramiko.pkey import PKey from paramiko.ssh_exception import SSHException -SHA1_DIGESTINFO = unhexlify(b('3021300906052b0e03021a05000414')) +SHA1_DIGESTINFO = b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14' class RSAKey (PKey): diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 2b3eb18..41e763e 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -48,7 +48,7 @@ def _to_unicode(s): except UnicodeError: return s -b_slash = b('/') +b_slash = b'/' class SFTPClient (BaseSFTP): """ diff --git a/tests/test_buffered_pipe.py b/tests/test_buffered_pipe.py index 5a088d8..b9d2bef 100644 --- a/tests/test_buffered_pipe.py +++ b/tests/test_buffered_pipe.py @@ -49,35 +49,35 @@ class BufferedPipeTest(ParamikoTest): p.feed('hello.') self.assertTrue(p.read_ready()) data = p.read(6) - self.assertEqual(b('hello.'), data) + self.assertEqual(b'hello.', data) p.feed('plus/minus') - self.assertEqual(b('plu'), p.read(3)) - self.assertEqual(b('s/m'), p.read(3)) - self.assertEqual(b('inus'), p.read(4)) + self.assertEqual(b'plu', p.read(3)) + self.assertEqual(b's/m', p.read(3)) + self.assertEqual(b'inus', p.read(4)) p.close() self.assertTrue(not p.read_ready()) - self.assertEqual(b(''), p.read(1)) + self.assertEqual(b'', p.read(1)) def test_2_delay(self): p = BufferedPipe() self.assertTrue(not p.read_ready()) threading.Thread(target=delay_thread, args=(p,)).start() - self.assertEqual(b('a'), p.read(1, 0.1)) + self.assertEqual(b'a', p.read(1, 0.1)) try: p.read(1, 0.1) self.assertTrue(False) except PipeTimeout: pass - self.assertEqual(b('b'), p.read(1, 1.0)) - self.assertEqual(b(''), p.read(1)) + self.assertEqual(b'b', p.read(1, 1.0)) + self.assertEqual(b'', p.read(1)) def test_3_close_while_reading(self): p = BufferedPipe() threading.Thread(target=close_thread, args=(p,)).start() data = p.read(1, 1.0) - self.assertEqual(b(''), data) + self.assertEqual(b'', data) def test_4_or_pipe(self): p = pipe.make_pipe() diff --git a/tests/test_client.py b/tests/test_client.py index e96a426..c508b83 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -45,7 +45,7 @@ class NullServer (paramiko.ServerInterface): return paramiko.AUTH_FAILED def check_auth_publickey(self, username, key): - if (key.get_name() == 'ssh-dss') and (hexlify(key.get_fingerprint()) == b('4478f0b9a23cc5182009ff755bc1d26c')): + if (key.get_name() == 'ssh-dss') and key.get_fingerprint() == b'\x44\x78\xf0\xb9\xa2\x3c\xc5\x18\x20\x09\xff\x75\x5b\xc1\xd2\x6c': return paramiko.AUTH_SUCCESSFUL return paramiko.AUTH_FAILED diff --git a/tests/test_hostkeys.py b/tests/test_hostkeys.py index 5290a4d..9a7e368 100644 --- a/tests/test_hostkeys.py +++ b/tests/test_hostkeys.py @@ -37,12 +37,12 @@ BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\ 5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M= """ -keyblob = b("""\ +keyblob = b"""\ AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4k\ NFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgK\ -oc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=""") +oc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=""" -keyblob_dss = b("""\ +keyblob_dss = b"""\ AAAAB3NzaC1kc3MAAACBAOeBpgNnfRzr/twmAQRu2XwWAp3CFtrVnug6s6fgwj/oLjYbVtjAy6pl/\ h0EKCWx2rf1IetyNsTxWrniA9I6HeDj65X1FyDkg6g8tvCnaNB8Xp/UUhuzHuGsMIipRxBxw9LF60\ 8EqZcj1E3ytktoW5B5OcjrkEoz3xG7C+rpIjYvAAAAFQDwz4UnmsGiSNu5iqjn3uTzwUpshwAAAIE\ @@ -50,7 +50,7 @@ AkxfFeY8P2wZpDjX0MimZl5wkoFQDL25cPzGBuB4OnB8NoUk/yjAHIIpEShw8V+LzouMK5CTJQo5+\ Ngw3qIch/WgRmMHy4kBq1SsXMjQCte1So6HBMvBPIW5SiMTmjCfZZiw4AYHK+B/JaOwaG9yRg2Ejg\ 4Ok10+XFDxlqZo8Y+wAAACARmR7CCPjodxASvRbIyzaVpZoJ/Z6x7dAumV+ysrV1BVYd0lYukmnjO\ 1kKBWApqpH1ve9XDQYN8zgxM4b16L21kpoWQnZtXrY3GZ4/it9kUgyB7+NwacIBlXa8cMDL7Q/69o\ -0d54U0X/NeX5QxuYR6OMJlrkQB7oiW/P/1mwjQgE=""") +0d54U0X/NeX5QxuYR6OMJlrkQB7oiW/P/1mwjQgE=""" class HostKeysTest (unittest.TestCase): @@ -68,7 +68,7 @@ class HostKeysTest (unittest.TestCase): self.assertEqual(1, len(list(hostdict.values())[0])) self.assertEqual(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b'E6684DB30E109B67B70FF1DC5C7F1363', fp) def test_2_add(self): hostdict = paramiko.HostKeys('hostfile.temp') @@ -78,7 +78,7 @@ class HostKeysTest (unittest.TestCase): self.assertEqual(3, len(list(hostdict))) x = hostdict['foo.example.com'] fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEqual(b('7EC91BB336CB6D810B124B1353C32396'), fp) + self.assertEqual(b'7EC91BB336CB6D810B124B1353C32396', fp) self.assertTrue(hostdict.check('foo.example.com', key)) def test_3_dict(self): @@ -90,7 +90,7 @@ class HostKeysTest (unittest.TestCase): x = hostdict.get('secure.example.com', None) self.assertTrue(x is not None) fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper() - self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b'E6684DB30E109B67B70FF1DC5C7F1363', fp) i = 0 for key in hostdict: i += 1 @@ -112,6 +112,6 @@ class HostKeysTest (unittest.TestCase): self.assertEqual(1, len(list(hostdict.values())[1])) self.assertEqual(1, len(list(hostdict.values())[2])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEqual(b('7EC91BB336CB6D810B124B1353C32396'), fp) + self.assertEqual(b'7EC91BB336CB6D810B124B1353C32396', fp) fp = hexlify(hostdict['secure.example.com']['ssh-dss'].get_fingerprint()).upper() - self.assertEqual(b('4478F0B9A23CC5182009FF755BC1D26C'), fp) + self.assertEqual(b'4478F0B9A23CC5182009FF755BC1D26C', fp) diff --git a/tests/test_kex.py b/tests/test_kex.py index dbe377a..e69c051 100644 --- a/tests/test_kex.py +++ b/tests/test_kex.py @@ -38,9 +38,9 @@ class FakeKey (object): def __str__(self): return 'fake-key' def asbytes(self): - return b('fake-key') + return b'fake-key' def sign_ssh_data(self, rng, H): - return b('fake-sig') + return b'fake-sig' class FakeModulusPack (object): @@ -91,7 +91,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGroup1(transport) kex.start_kex() - x = b('1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') + x = b'1E000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_group1._MSG_KEXDH_REPLY,), transport._expect) @@ -102,10 +102,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_group1._MSG_KEXDH_REPLY, msg) - H = b('03079780F3D3AD0B3C6DB30C8D21685F367A86D2') + H = b'03079780F3D3AD0B3C6DB30C8D21685F367A86D2' self.assertEqual(self.K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) - self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertEqual((b'fake-host-key', b'fake-sig'), transport._verify) self.assertTrue(transport._activated) def test_2_group1_server(self): @@ -119,8 +119,8 @@ class KexTest (unittest.TestCase): msg.add_mpint(69) msg.rewind() kex.parse_next(paramiko.kex_group1._MSG_KEXDH_INIT, msg) - H = b('B16BF34DD10945EDE84E9C1EF24A14BFDC843389') - x = b('1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') + H = b'B16BF34DD10945EDE84E9C1EF24A14BFDC843389' + x = b'1F0000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEqual(self.K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) @@ -131,7 +131,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGex(transport) kex.start_kex() - x = b('22000004000000080000002000') + x = b'22000004000000080000002000' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) @@ -140,7 +140,7 @@ class KexTest (unittest.TestCase): msg.add_mpint(FakeModulusPack.G) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) - x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') + x = b'20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) @@ -150,10 +150,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) - H = b('A265563F2FA87F1A89BF007EE90D58BE2E4A4BD0') + H = b'A265563F2FA87F1A89BF007EE90D58BE2E4A4BD0' self.assertEqual(self.K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) - self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertEqual((b'fake-host-key', b'fake-sig'), transport._verify) self.assertTrue(transport._activated) def test_4_gex_old_client(self): @@ -161,7 +161,7 @@ class KexTest (unittest.TestCase): transport.server_mode = False kex = KexGex(transport) kex.start_kex(_test_old_style=True) - x = b('1E00000800') + x = b'1E00000800' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_GROUP,), transport._expect) @@ -170,7 +170,7 @@ class KexTest (unittest.TestCase): msg.add_mpint(FakeModulusPack.G) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_GROUP, msg) - x = b('20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4') + x = b'20000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D4' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_REPLY,), transport._expect) @@ -180,10 +180,10 @@ class KexTest (unittest.TestCase): msg.add_string('fake-sig') msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REPLY, msg) - H = b('807F87B269EF7AC5EC7E75676808776A27D5864C') + H = b'807F87B269EF7AC5EC7E75676808776A27D5864C' self.assertEqual(self.K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) - self.assertEqual((b('fake-host-key'), b('fake-sig')), transport._verify) + self.assertEqual((b'fake-host-key', b'fake-sig'), transport._verify) self.assertTrue(transport._activated) def test_5_gex_server(self): @@ -199,7 +199,7 @@ class KexTest (unittest.TestCase): msg.add_int(4096) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST, msg) - x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') + x = b'1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) @@ -208,8 +208,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 - H = b('CE754197C21BF3452863B4F44D0B3951F12516EF') - x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') + H = b'CE754197C21BF3452863B4F44D0B3951F12516EF' + x = b'210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEqual(K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) @@ -226,7 +226,7 @@ class KexTest (unittest.TestCase): msg.add_int(2048) msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_REQUEST_OLD, msg) - x = b('1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102') + x = b'1F0000008100FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF0000000102' self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) self.assertEqual((paramiko.kex_gex._MSG_KEXDH_GEX_INIT,), transport._expect) @@ -235,8 +235,8 @@ class KexTest (unittest.TestCase): msg.rewind() kex.parse_next(paramiko.kex_gex._MSG_KEXDH_GEX_INIT, msg) K = 67592995013596137876033460028393339951879041140378510871612128162185209509220726296697886624612526735888348020498716482757677848959420073720160491114319163078862905400020959196386947926388406687288901564192071077389283980347784184487280885335302632305026248574716290537036069329724382811853044654824945750581 - H = b('B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B') - x = b('210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967') + H = b'B41A06B2E59043CEFC1AE16EC31F1E2D12EC455B' + x = b'210000000866616B652D6B6579000000807E2DDB1743F3487D6545F04F1C8476092FB912B013626AB5BCEB764257D88BBA64243B9F348DF7B41B8C814A995E00299913503456983FFB9178D3CD79EB6D55522418A8ABF65375872E55938AB99A84A0B5FC8A1ECC66A7C3766E7E0F80B7CE2C9225FC2DD683F4764244B72963BBB383F529DCF0C5D17740B8A2ADBE9208D40000000866616B652D736967' self.assertEqual(K, transport._K) self.assertEqual(H, hexlify(transport._H).upper()) self.assertEqual(x, hexlify(transport._message.asbytes()).upper()) diff --git a/tests/test_message.py b/tests/test_message.py index cb815f9..4da52cf 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -21,17 +21,16 @@ Some unit tests for ssh protocol message blocks. """ import unittest -from binascii import unhexlify from paramiko.message import Message from paramiko.common import * class MessageTest (unittest.TestCase): - __a = unhexlify(b('000000170760e09000000001710000000568656c6c6f000003e8')) + (b('x') * 1000) - __b = unhexlify(b('0100f3003f00000010687565792c64657765792c6c6f756965')) - __c = unhexlify(b('00000000000000050000f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')) - __d = unhexlify(b('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')) + __a = b'\x00\x00\x00\x17\x07\x60\xe0\x90\x00\x00\x00\x01\x71\x00\x00\x00\x05\x68\x65\x6c\x6c\x6f\x00\x00\x03\xe8' + b'x' * 1000 + __b = b'\x01\x00\xf3\x00\x3f\x00\x00\x00\x10\x68\x75\x65\x79\x2c\x64\x65\x77\x65\x79\x2c\x6c\x6f\x75\x69\x65' + __c = b'\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x01\x11\x00\x00\x00\x07\x00\xf5\xe4\xd3\xc2\xb1\x09\x00\x00\x00\x06\x9a\x1b\x2c\x3d\x4e\xf7' + __d = b'\x00\x00\x00\x05\xff\x00\x00\x00\x05\x11\x22\x33\x44\x55\xff\x00\x00\x00\x0a\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03\x63\x61\x74\x00\x00\x00\x03\x61\x2c\x62' def test_1_encode(self): msg = Message() diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py index 43dea38..5c36fed 100644 --- a/tests/test_packetizer.py +++ b/tests/test_packetizer.py @@ -21,7 +21,6 @@ Some unit tests for the ssh2 protocol in Transport. """ import unittest -from binascii import unhexlify from tests.loop import LoopSocket from Crypto.Cipher import AES from Crypto.Hash import SHA, HMAC @@ -55,7 +54,7 @@ class PacketizerTest (unittest.TestCase): data = rsock.recv(100) # 32 + 12 bytes of MAC = 44 self.assertEqual(44, len(data)) - self.assertEqual(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c0')), data[:16]) + self.assertEqual(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0', data[:16]) def test_2_read (self): rsock = LoopSocket() @@ -66,7 +65,7 @@ class PacketizerTest (unittest.TestCase): p.set_hexdump(True) cipher = AES.new(zero_byte * 16, AES.MODE_CBC, x55 * 16) p.set_inbound_cipher(cipher, 16, SHA, 12, x1f * 20) - wsock.send(unhexlify(b('439197bd5b50ac2587c2c46bc7e938c090d216560d717361387c4c3dfb977de26e03b1a0c21cd641414cb459'))) + wsock.send(b'\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0\x90\xd2\x16\x56\x0d\x71\x73\x61\x38\x7c\x4c\x3d\xfb\x97\x7d\xe2\x6e\x03\xb1\xa0\xc2\x1c\xd6\x41\x41\x4c\xb4\x59') cmd, m = p.read_message() self.assertEqual(100, cmd) self.assertEqual(100, m.get_int()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 19c5c69..2e565a5 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -20,7 +20,7 @@ Some unit tests for public/private key objects. """ -from binascii import hexlify, unhexlify +from binascii import hexlify import unittest from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util from paramiko.common import rng, StringIO, byte_chr, b, bytes @@ -77,7 +77,7 @@ ADRvOqQ5R98Sxst765CAqXmRtz8vwoD96g== -----END EC PRIVATE KEY----- """ -x1234 = unhexlify(b('01020304')) +x1234 = b'\x01\x02\x03\x04' class KeyTest (unittest.TestCase): @@ -91,7 +91,7 @@ class KeyTest (unittest.TestCase): def test_1_generate_key_bytes(self): from Crypto.Hash import MD5 key = util.generate_key_bytes(MD5, x1234, 'happy birthday', 30) - exp = unhexlify(b('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64')) + exp = b'\x61\xE1\xF2\x72\xF4\xC1\xC4\x56\x15\x86\xBD\x32\x24\x98\xC0\xE9\x24\x67\x27\x80\xF4\x7B\xB3\x7D\xDA\x7D\x54\x01\x9E\x64' self.assertEqual(exp, key) def test_2_load_rsa(self): @@ -165,7 +165,7 @@ class KeyTest (unittest.TestCase): def test_8_sign_rsa(self): # verify that the rsa private key can sign and verify key = RSAKey.from_private_key_file(test_path('test_rsa.key')) - msg = key.sign_ssh_data(rng, b('ice weasels')) + msg = key.sign_ssh_data(rng, b'ice weasels') self.assertTrue(type(msg) is Message) msg.rewind() self.assertEqual('ssh-rsa', msg.get_text()) @@ -173,12 +173,12 @@ class KeyTest (unittest.TestCase): self.assertEqual(sig, msg.get_binary()) msg.rewind() pub = RSAKey(data=key.asbytes()) - self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b'ice weasels', msg)) def test_9_sign_dss(self): # verify that the dss private key can sign and verify key = DSSKey.from_private_key_file(test_path('test_dss.key')) - msg = key.sign_ssh_data(rng, b('ice weasels')) + msg = key.sign_ssh_data(rng, b'ice weasels') self.assertTrue(type(msg) is Message) msg.rewind() self.assertEqual('ssh-dss', msg.get_text()) @@ -188,19 +188,19 @@ class KeyTest (unittest.TestCase): self.assertEqual(40, len(msg.get_binary())) msg.rewind() pub = DSSKey(data=key.asbytes()) - self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b'ice weasels', msg)) def test_A_generate_rsa(self): key = RSAKey.generate(1024) - msg = key.sign_ssh_data(rng, b('jerri blank')) + msg = key.sign_ssh_data(rng, b'jerri blank') msg.rewind() - self.assertTrue(key.verify_ssh_sig(b('jerri blank'), msg)) + self.assertTrue(key.verify_ssh_sig(b'jerri blank', msg)) def test_B_generate_dss(self): key = DSSKey.generate(1024) - msg = key.sign_ssh_data(rng, b('jerri blank')) + msg = key.sign_ssh_data(rng, b'jerri blank') msg.rewind() - self.assertTrue(key.verify_ssh_sig(b('jerri blank'), msg)) + self.assertTrue(key.verify_ssh_sig(b'jerri blank', msg)) def test_10_load_ecdsa(self): key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) @@ -219,7 +219,7 @@ class KeyTest (unittest.TestCase): self.assertEqual(key, key2) def test_11_load_ecdsa_password(self): - key = ECDSAKey.from_private_key_file(test_path('test_ecdsa_password.key'), b('television')) + key = ECDSAKey.from_private_key_file(test_path('test_ecdsa_password.key'), b'television') self.assertEqual('ecdsa-sha2-nistp256', key.get_name()) exp_ecdsa = b(FINGER_ECDSA.split()[1].replace(':', '')) my_ecdsa = hexlify(key.get_fingerprint()) @@ -239,7 +239,7 @@ class KeyTest (unittest.TestCase): def test_13_sign_ecdsa(self): # verify that the rsa private key can sign and verify key = ECDSAKey.from_private_key_file(test_path('test_ecdsa.key')) - msg = key.sign_ssh_data(rng, b('ice weasels')) + msg = key.sign_ssh_data(rng, b'ice weasels') self.assertTrue(type(msg) is Message) msg.rewind() self.assertEqual('ecdsa-sha2-nistp256', msg.get_text()) @@ -250,4 +250,4 @@ class KeyTest (unittest.TestCase): msg.rewind() pub = ECDSAKey(data=key.asbytes()) - self.assertTrue(pub.verify_ssh_sig(b('ice weasels'), msg)) + self.assertTrue(pub.verify_ssh_sig(b'ice weasels', msg)) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index f78047b..271c08b 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -549,7 +549,7 @@ class SFTPTest (unittest.TestCase): fd, localname = mkstemp() os.close(fd) - text = b('All I wanted was a plastic bunny rabbit.\n') + text = b'All I wanted was a plastic bunny rabbit.\n' with open(localname, 'wb') as f: f.write(text) saved_progress = [] diff --git a/tests/test_transport.py b/tests/test_transport.py index 6f6843b..876759c 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -20,7 +20,7 @@ Some unit tests for the ssh2 protocol in Transport. """ -from binascii import hexlify, unhexlify +from binascii import hexlify import select import socket import sys @@ -159,10 +159,10 @@ class TransportTest(ParamikoTest): def test_2_compute_key(self): self.tc.K = 123281095979686581523377256114209720774539068973101330872763622971399429481072519713536292772709507296759612401802191955568143056534122385270077606457721553469730659233569339356140085284052436697480759510519672848743794433460113118986816826624865291116513647975790797391795651716378444844877749505443714557929 - self.tc.H = unhexlify(b('0C8307CDE6856FF30BA93684EB0F04C2520E9ED3')) + self.tc.H = b'\x0C\x83\x07\xCD\xE6\x85\x6F\xF3\x0B\xA9\x36\x84\xEB\x0F\x04\xC2\x52\x0E\x9E\xD3' self.tc.session_id = self.tc.H key = self.tc._compute_key('C', 32) - self.assertEqual(b('207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995'), + self.assertEqual(b'207E66594CA87C44ECCBA3B3CD39FDDB378E6FDB0F97C54B2AA0CFBF900CD995', hexlify(key).upper()) def test_3_simple(self): @@ -361,7 +361,7 @@ class TransportTest(ParamikoTest): self.assertEqual([], w) self.assertEqual([], e) - self.assertEqual(b('hello\n'), chan.recv(6)) + self.assertEqual(b'hello\n', chan.recv(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) @@ -462,7 +462,7 @@ class TransportTest(ParamikoTest): self.assertEqual(6093, requested[0][1]) x11_server.send('hello') - self.assertEqual(b('hello'), x11_client.recv(5)) + self.assertEqual(b'hello', x11_client.recv(5)) x11_server.close() x11_client.close() @@ -495,7 +495,7 @@ class TransportTest(ParamikoTest): cch = self.tc.accept() sch.send('hello') - self.assertEqual(b('hello'), cch.recv(5)) + self.assertEqual(b'hello', cch.recv(5)) sch.close() cch.close() ss.close() @@ -527,12 +527,12 @@ class TransportTest(ParamikoTest): cch.connect(self.server._tcpip_dest) ss, _ = greeting_server.accept() - ss.send(b('Hello!\n')) + ss.send(b'Hello!\n') ss.close() sch.send(cch.recv(8192)) sch.close() - self.assertEqual(b('Hello!\n'), cs.recv(7)) + self.assertEqual(b'Hello!\n', cs.recv(7)) cs.close() def test_G_stderr_select(self): @@ -563,7 +563,7 @@ class TransportTest(ParamikoTest): self.assertEqual([], w) self.assertEqual([], e) - self.assertEqual(b('hello\n'), chan.recv_stderr(6)) + self.assertEqual(b'hello\n', chan.recv_stderr(6)) # and, should be dead again now r, w, e = select.select([chan], [], [], 0.1) diff --git a/tests/test_util.py b/tests/test_util.py index be10719..4f85c39 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -137,7 +137,7 @@ class UtilTest(ParamikoTest): ) def test_4_generate_key_bytes(self): - x = paramiko.util.generate_key_bytes(SHA, b('ABCDEFGH'), 'This is my secret passphrase.', 64) + x = paramiko.util.generate_key_bytes(SHA, b'ABCDEFGH', 'This is my secret passphrase.', 64) hex = ''.join(['%02x' % byte_ord(c) for c in x]) self.assertEqual(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b') @@ -150,7 +150,7 @@ class UtilTest(ParamikoTest): self.assertEqual(1, len(list(hostdict.values())[0])) self.assertEqual(1, len(list(hostdict.values())[1])) fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper() - self.assertEqual(b('E6684DB30E109B67B70FF1DC5C7F1363'), fp) + self.assertEqual(b'E6684DB30E109B67B70FF1DC5C7F1363', fp) finally: os.unlink('hostfile.temp') From 103d056a779d891ede6653f4b205a4b1dfad22e9 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 12:08:13 -0800 Subject: [PATCH 50/56] Remove Python 2.5 from travis and fix minimum required version in init and README --- .travis.yml | 1 - README | 4 ++-- paramiko/__init__.py | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7ebb931..5de0a5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python python: - - "2.5" - "2.6" - "2.7" - "3.2" diff --git a/README b/README index 1899819..ae50a80 100644 --- a/README +++ b/README @@ -15,7 +15,7 @@ What ---- "paramiko" is a combination of the esperanto words for "paranoid" and -"friend". it's a module for python 2.5+ that implements the SSH2 protocol +"friend". it's a module for python 2.6+ that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines. unlike SSL (aka TLS), SSH2 protocol does not require hierarchical certificates signed by a powerful central authority. you may know SSH2 as @@ -34,7 +34,7 @@ that should have come with this archive. Requirements ------------ - - python 2.5 or better + - python 2.6 or better - pycrypto 2.1 or better If you have setuptools, you can build and install paramiko and all its diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 160ed39..48e2ee2 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -18,7 +18,7 @@ """ I{Paramiko} (a combination of the esperanto words for "paranoid" and "friend") -is a module for python 2.5 or greater that implements the SSH2 protocol for +is a module for python 2.6 or greater that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines. Unlike SSL (aka TLS), the SSH2 protocol does not require hierarchical certificates signed by a powerful central authority. You may know SSH2 as the protocol that @@ -52,8 +52,8 @@ Mailing list: U{paramiko@librelist.com} import sys -if sys.version_info < (2, 5): - raise RuntimeError('You need python 2.5+ for this module.') +if sys.version_info < (2, 6): + raise RuntimeError('You need python 2.6+ for this module.') __author__ = "Jeff Forcier " From aa301506f4494bb2eb157bddbbb84bf3a513fc3b Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Tue, 19 Nov 2013 12:08:33 -0800 Subject: [PATCH 51/56] Bump version to 1.13.0 in init --- paramiko/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 48e2ee2..8a81406 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -57,7 +57,7 @@ if sys.version_info < (2, 6): __author__ = "Jeff Forcier " -__version__ = "1.12.0" +__version__ = "1.13.0" __version_info__ = tuple([ int(d) for d in __version__.split(".") ]) __license__ = "GNU Lesser General Public License (LGPL)" From a15d5ba25d19564f21e53dba5294448d6550cfce Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 4 Dec 2013 00:43:03 -0500 Subject: [PATCH 52/56] Fix deprecation warning in array conversion --- paramiko/buffered_pipe.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index 0391dee..02408a2 100644 --- a/paramiko/buffered_pipe.py +++ b/paramiko/buffered_pipe.py @@ -49,6 +49,20 @@ class BufferedPipe (object): self._buffer = array.array('B') self._closed = False + if PY2: + def _buffer_frombytes(self, data): + self._buffer.fromstring(data) + + def _buffer_tobytes(self, limit=None): + return self._buffer[:limit].tostring() + else: + def _buffer_frombytes(self, data): + self._buffer.frombytes(data) + + def _buffer_tobytes(self, limit=None): + return self._buffer[:limit].tobytes() + + def set_event(self, event): """ Set an event on this buffer. When data is ready to be read (or the @@ -76,7 +90,7 @@ class BufferedPipe (object): try: if self._event is not None: self._event.set() - self._buffer.fromstring(data) + self._buffer_frombytes(b(data)) self._cv.notifyAll() finally: self._lock.release() @@ -143,12 +157,12 @@ class BufferedPipe (object): # something's in the buffer and we have the lock! if len(self._buffer) <= nbytes: - out = self._buffer.tostring() + out = self._buffer_tobytes() del self._buffer[:] if (self._event is not None) and not self._closed: self._event.clear() else: - out = self._buffer[:nbytes].tostring() + out = self._buffer_tobytes(nbytes) del self._buffer[:nbytes] finally: self._lock.release() @@ -164,7 +178,7 @@ class BufferedPipe (object): """ self._lock.acquire() try: - out = self._buffer.tostring() + out = self._buffer_tobytes() del self._buffer[:] if (self._event is not None) and not self._closed: self._event.clear() From 676a30c2983f2c335ec86571e3ef7726ee735c52 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 5 Dec 2013 11:05:47 -0500 Subject: [PATCH 53/56] Fix import of win_pageant --- paramiko/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/agent.py b/paramiko/agent.py index a6a177d..8f2a486 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -218,7 +218,7 @@ class AgentClientProxy(object): # probably a dangling env var: the ssh agent is gone return elif sys.platform == 'win32': - from . import win_pageant + import paramiko.win_pageant as win_pageant if win_pageant.can_talk_to_agent(): conn = win_pageant.PageantConnection() else: From ab8d874064d0d9ad298b0630d2ed281fde7e9b94 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 16 Jan 2014 19:09:13 -0800 Subject: [PATCH 54/56] Fix getcwd when _cwd is None --- paramiko/sftp_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 41e763e..0f55c6a 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -531,7 +531,7 @@ class SFTPClient (BaseSFTP): @since: 1.4 """ - return u(self._cwd) + return self._cwd and u(self._cwd) def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True): """ From aa8ea3c4d42d508a722fdf4ea635ff5e609cca1b Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 16 Jan 2014 19:36:04 -0800 Subject: [PATCH 55/56] Add getcwd test --- tests/test_sftp.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 271c08b..f8fab1c 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -26,7 +26,6 @@ do test file operations in (so no existing files will be harmed). from binascii import hexlify import os import warnings -import sys import threading import unittest from tempfile import mkstemp @@ -75,6 +74,7 @@ g_big_file_test = True unicode_folder = eval(compile(r"u'\u00fcnic\u00f8de'" if PY2 else r"'\u00fcnic\u00f8de'", 'test_sftp.py', 'eval')) utf8_folder = b'/\xc3\xbcnic\xc3\xb8\x64\x65' + def get_sftp(): global sftp return sftp @@ -591,7 +591,7 @@ class SFTPTest (unittest.TestCase): self.assertEqual('93DE4788FCA28D471516963A1FE3856A', u(hexlify(sum)).upper()) sum = f.check('md5', 0, 0, 510) self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', - u(hexlify(sum)).upper()) + u(hexlify(sum)).upper()) finally: sftp.unlink(FOLDER + '/kitty.txt') @@ -675,6 +675,25 @@ class SFTPTest (unittest.TestCase): os.unlink(localname) sftp.unlink(FOLDER + '/bunny.txt') + def test_O_getcwd(self): + """ + verify that chdir/getcwd work. + """ + self.assertEqual(None, sftp.getcwd()) + root = sftp.normalize('.') + if root[-1] != '/': + root += '/' + try: + sftp.mkdir(FOLDER + '/alpha') + sftp.chdir(FOLDER + '/alpha') + self.assertEqual('/' + FOLDER + '/alpha', sftp.getcwd()) + finally: + sftp.chdir(root) + try: + sftp.rmdir(FOLDER + '/alpha') + except: + pass + def XXX_test_M_seek_append(self): """ verify that seek does't affect writes during append. From ae078f51d622931954e47e78029a889c4e721a05 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Thu, 16 Jan 2014 20:15:16 -0800 Subject: [PATCH 56/56] Fix new test for Py3 and start server in tests instead of in setUp so we can skip starting server for test 5 --- tests/test_client.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index b472484..9715097 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -21,16 +21,14 @@ Some unit tests for SSHClient. """ import socket +from tempfile import mkstemp import threading -import time import unittest import weakref import warnings import os -from binascii import hexlify from tests.util import test_path import paramiko -from paramiko.py3compat import b class NullServer (paramiko.ServerInterface): @@ -67,8 +65,6 @@ class SSHClientTest (unittest.TestCase): self.sockl.listen(1) self.addr, self.port = self.sockl.getsockname() self.event = threading.Event() - thread = threading.Thread(target=self._run) - thread.start() def tearDown(self): for attr in "tc ts socks sockl".split(): @@ -83,11 +79,11 @@ class SSHClientTest (unittest.TestCase): server = NullServer() self.ts.start_server(self.event, server) - def test_1_client(self): """ verify that the SSHClient stuff works too. """ + threading.Thread(target=self._run).start() host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) @@ -121,6 +117,7 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient works with a DSA key. """ + threading.Thread(target=self._run).start() host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) @@ -154,6 +151,7 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient accepts and tries multiple key files. """ + threading.Thread(target=self._run).start() host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) @@ -171,6 +169,7 @@ class SSHClientTest (unittest.TestCase): """ verify that SSHClient's AutoAddPolicy works. """ + threading.Thread(target=self._run).start() host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) @@ -193,9 +192,10 @@ class SSHClientTest (unittest.TestCase): """ warnings.filterwarnings('ignore', 'tempnam.*') - host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') - public_host_key = paramiko.RSAKey(data=str(host_key)) - localname = os.tempnam() + host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) + public_host_key = paramiko.RSAKey(data=host_key.asbytes()) + fd, localname = mkstemp() + os.close(fd) client = paramiko.SSHClient() self.assertEquals(0, len(client.get_host_keys())) @@ -218,6 +218,7 @@ class SSHClientTest (unittest.TestCase): verify that when an SSHClient is collected, its transport (and the transport's packetizer) is closed. """ + threading.Thread(target=self._run).start() host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) public_host_key = paramiko.RSAKey(data=host_key.asbytes())