summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobey Pointer <robey@lag.net>2005-05-10 17:36:38 +0000
committerRobey Pointer <robey@lag.net>2005-05-10 17:36:38 +0000
commit366f216e3f4792da0f619e6aeff84a91230c2527 (patch)
treef25a96245cc7850c15482db7a407788c44446d39
parente8efe095a68a764b1b9ebaa73510fea16707fd5a (diff)
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-8]
add unit tests for the packetizer, and fix a little locking bug where i think more of the packetizer write function should be inside a lock
-rw-r--r--README4
-rw-r--r--paramiko/__init__.py4
-rw-r--r--paramiko/packet.py10
-rwxr-xr-xtest.py2
-rw-r--r--tests/test_packetizer.py70
5 files changed, 82 insertions, 8 deletions
diff --git a/README b/README
index 01b89396..50123140 100644
--- a/README
+++ b/README
@@ -226,10 +226,10 @@ v0.9 FEAROW
*** MISSING LINKS
* ctr forms of ciphers are missing (blowfish-ctr, aes128-ctr, aes256-ctr)
-* server mode needs better documentation
-* why are big files so slow to transfer? profiling needed...
* would be nice to have an ftp-like interface to sftp (put, get, chdir...)
+* why are big files so slow to transfer? profiling needed...
* speed up file transfers!
* what is psyco?
+* make a simple example demonstrating use of SocketServer
diff --git a/paramiko/__init__.py b/paramiko/__init__.py
index 89411f10..3d831e3b 100644
--- a/paramiko/__init__.py
+++ b/paramiko/__init__.py
@@ -66,7 +66,8 @@ __version__ = "1.3 (marowak)"
__license__ = "GNU Lesser General Public License (LGPL)"
-import transport, auth_transport, channel, rsakey, dsskey, message, ssh_exception, file
+import transport, auth_transport, channel, rsakey, dsskey, message
+import ssh_exception, file, packet
import sftp, sftp_client, sftp_attr, sftp_file, sftp_handle, sftp_server, sftp_si
randpool = transport.randpool
@@ -89,6 +90,7 @@ ServerInterface = server.ServerInterface
SubsystemHandler = server.SubsystemHandler
SecurityOptions = transport.SecurityOptions
BufferedFile = file.BufferedFile
+Packetizer = packet.Packetizer
from common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED, \
OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_FAILED_CONNECT_FAILED, \
diff --git a/paramiko/packet.py b/paramiko/packet.py
index e75ea776..bf4cdb44 100644
--- a/paramiko/packet.py
+++ b/paramiko/packet.py
@@ -226,14 +226,14 @@ class Packetizer (object):
packet = self._build_packet(data)
if self.__dump_packets:
self._log(DEBUG, util.format_binary(packet, 'OUT: '))
- if self.__block_engine_out != None:
- out = self.__block_engine_out.encrypt(packet)
- else:
- out = packet
- # + mac
self.__write_lock.acquire()
try:
if self.__block_engine_out != None:
+ out = self.__block_engine_out.encrypt(packet)
+ else:
+ out = packet
+ # + mac
+ if self.__block_engine_out != None:
payload = struct.pack('>I', self.__sequence_number_out) + packet
out += HMAC.HMAC(self.__mac_key_out, payload, self.__mac_engine_out).digest()[:self.__mac_size_out]
self.__sequence_number_out = (self.__sequence_number_out + 1) & 0xffffffffL
diff --git a/test.py b/test.py
index a97354ee..97188a4e 100755
--- a/test.py
+++ b/test.py
@@ -32,6 +32,7 @@ from test_message import MessageTest
from test_file import BufferedFileTest
from test_pkey import KeyTest
from test_kex import KexTest
+from test_packetizer import PacketizerTest
from test_transport import TransportTest
from test_sftp import SFTPTest
@@ -87,6 +88,7 @@ suite.addTest(unittest.makeSuite(BufferedFileTest))
if options.use_pkey:
suite.addTest(unittest.makeSuite(KeyTest))
suite.addTest(unittest.makeSuite(KexTest))
+suite.addTest(unittest.makeSuite(PacketizerTest))
if options.use_transport:
suite.addTest(unittest.makeSuite(TransportTest))
if options.use_sftp:
diff --git a/tests/test_packetizer.py b/tests/test_packetizer.py
new file mode 100644
index 00000000..8c992bd8
--- /dev/null
+++ b/tests/test_packetizer.py
@@ -0,0 +1,70 @@
+# Copyright (C) 2003-2005 Robey Pointer <robey@lag.net>
+#
+# This file is part of paramiko.
+#
+# Paramiko is free software; you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation; either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# 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.
+
+"""
+Some unit tests for the ssh2 protocol in Transport.
+"""
+
+import unittest
+from loop import LoopSocket
+from Crypto.Cipher import AES
+from Crypto.Hash import SHA, HMAC
+from paramiko import Message, Packetizer, util
+
+class PacketizerTest (unittest.TestCase):
+
+ def test_1_write (self):
+ rsock = LoopSocket()
+ wsock = LoopSocket()
+ rsock.link(wsock)
+ 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)
+
+ # 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_int(100)
+ m.add_int(1)
+ m.add_int(900)
+ p.send_message(m)
+ 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])
+
+ def test_2_read (self):
+ rsock = LoopSocket()
+ wsock = LoopSocket()
+ rsock.link(wsock)
+ 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')
+ 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())