summaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorScott Maxwell <scott@codecobblers.com>2013-11-02 14:56:43 -0700
committerScott Maxwell <scott@codecobblers.com>2013-11-02 14:56:43 -0700
commit45e65b6e1eb47944a26e4349d41998844c155df5 (patch)
tree3bc7cb1f1fcfd1d7230f71c9e083c30485f1f951 /tests
parent7decda3297089b2b2e73bb9cd7e577f9b2cb2789 (diff)
Make sftp.open handle binary and text, more type conversion
Diffstat (limited to 'tests')
-rw-r--r--tests/test_client.py7
-rwxr-xr-xtests/test_file.py11
-rwxr-xr-xtests/test_sftp.py16
-rw-r--r--tests/test_sftp_big.py28
4 files changed, 33 insertions, 29 deletions
diff --git a/tests/test_client.py b/tests/test_client.py
index e6d10699..b77b90d7 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 6cb35070..0430040c 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 20f68d8a..c17defaa 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 c1d34d7c..a53a6c3d 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):