2005-02-28 03:06:08 -05:00
|
|
|
# Copyright (C) 2003-2005 Robey Pointer <robey@lag.net>
|
2004-03-08 12:54:19 -05:00
|
|
|
#
|
|
|
|
# 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
|
2004-09-11 16:37:59 -04:00
|
|
|
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
|
2004-03-08 12:54:19 -05:00
|
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
|
|
|
|
"""
|
|
|
|
some unit tests to make sure sftp works.
|
|
|
|
|
|
|
|
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).
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys, os
|
2004-05-29 14:58:11 -04:00
|
|
|
import random
|
2004-12-19 14:56:48 -05:00
|
|
|
import logging, threading
|
2004-03-08 12:54:19 -05:00
|
|
|
|
2004-04-06 04:16:02 -04:00
|
|
|
import paramiko, unittest
|
2004-12-19 14:56:48 -05:00
|
|
|
from stub_sftp import StubServer, StubSFTPServer
|
|
|
|
from loop import LoopSocket
|
2004-03-08 12:54:19 -05:00
|
|
|
|
|
|
|
ARTICLE = '''
|
|
|
|
Insulin sensitivity and liver insulin receptor structure in ducks from two
|
|
|
|
genera
|
|
|
|
|
|
|
|
T. Constans, B. Chevalier, M. Derouet and J. Simon
|
|
|
|
Station de Recherches Avicoles, Institut National de la Recherche Agronomique,
|
|
|
|
Nouzilly, France.
|
|
|
|
|
|
|
|
Insulin sensitivity and liver insulin receptor structure were studied in
|
|
|
|
5-wk-old ducks from two genera (Muscovy and Pekin). In the fasting state, both
|
|
|
|
duck types were equally resistant to exogenous insulin compared with chicken.
|
|
|
|
Despite the low potency of duck insulin, the number of insulin receptors was
|
|
|
|
lower in Muscovy duck and similar in Pekin duck and chicken liver membranes.
|
|
|
|
After 125I-insulin cross-linking, the size of the alpha-subunit of the
|
|
|
|
receptors from the three species was 135,000. Wheat germ agglutinin-purified
|
|
|
|
receptors from the three species were contaminated by an active and unusual
|
|
|
|
adenosinetriphosphatase (ATPase) contaminant (highest activity in Muscovy
|
|
|
|
duck). Sequential purification of solubilized receptor from both duck types on
|
|
|
|
lentil and then wheat germ agglutinin lectins led to a fraction of receptors
|
|
|
|
very poor in ATPase activity that exhibited a beta-subunit size (95,000) and
|
|
|
|
tyrosine kinase activity similar to those of ATPase-free chicken insulin
|
|
|
|
receptors. Therefore the ducks from the two genera exhibit an alpha-beta-
|
|
|
|
structure for liver insulin receptors and a clear difference in the number of
|
|
|
|
liver insulin receptors. Their sensitivity to insulin is, however, similarly
|
|
|
|
decreased compared with chicken.
|
|
|
|
'''
|
|
|
|
|
2004-09-25 18:03:48 -04:00
|
|
|
FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing')
|
2004-03-08 12:54:19 -05:00
|
|
|
|
2004-09-25 18:03:48 -04:00
|
|
|
sftp = None
|
2005-04-17 20:30:52 -04:00
|
|
|
tc = None
|
2004-12-19 14:56:48 -05:00
|
|
|
g_big_file_test = True
|
2004-03-08 12:54:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SFTPTest (unittest.TestCase):
|
|
|
|
|
2004-09-25 18:03:48 -04:00
|
|
|
def init(hostname, username, keyfile, passwd):
|
|
|
|
global sftp
|
|
|
|
|
|
|
|
t = paramiko.Transport(hostname)
|
|
|
|
try:
|
|
|
|
key = paramiko.RSAKey.from_private_key_file(keyfile, passwd)
|
|
|
|
except paramiko.PasswordRequiredException:
|
|
|
|
sys.stderr.write('\n\nparamiko.RSAKey.from_private_key_file REQUIRES PASSWORD.\n')
|
|
|
|
sys.stderr.write('You have two options:\n')
|
|
|
|
sys.stderr.write('* Use the "-K" option to point to a different (non-password-protected)\n')
|
|
|
|
sys.stderr.write(' private key file.\n')
|
|
|
|
sys.stderr.write('* Use the "-P" option to provide the password needed to unlock this private\n')
|
|
|
|
sys.stderr.write(' key.\n')
|
|
|
|
sys.stderr.write('\n')
|
|
|
|
sys.exit(1)
|
|
|
|
try:
|
|
|
|
t.connect(username=username, pkey=key)
|
|
|
|
except paramiko.SSHException:
|
|
|
|
t.close()
|
|
|
|
sys.stderr.write('\n\nparamiko.Transport.connect FAILED.\n')
|
|
|
|
sys.stderr.write('There are several possible reasons why it might fail so quickly:\n\n')
|
|
|
|
sys.stderr.write('* The host to connect to (%s) is not a valid SSH server.\n' % hostname)
|
|
|
|
sys.stderr.write(' (Use the "-H" option to change the host.)\n')
|
|
|
|
sys.stderr.write('* The username to auth as (%s) is invalid.\n' % username)
|
|
|
|
sys.stderr.write(' (Use the "-U" option to change the username.)\n')
|
|
|
|
sys.stderr.write('* The private key given (%s) is not accepted by the server.\n' % keyfile)
|
|
|
|
sys.stderr.write(' (Use the "-K" option to provide a different key file.)\n')
|
|
|
|
sys.stderr.write('\n')
|
|
|
|
sys.exit(1)
|
|
|
|
sftp = paramiko.SFTP.from_transport(t)
|
|
|
|
init = staticmethod(init)
|
|
|
|
|
2004-12-19 14:56:48 -05:00
|
|
|
def init_loopback():
|
2005-04-17 20:30:52 -04:00
|
|
|
global sftp, tc
|
2004-12-19 14:56:48 -05:00
|
|
|
|
|
|
|
socks = LoopSocket()
|
|
|
|
sockc = LoopSocket()
|
|
|
|
sockc.link(socks)
|
|
|
|
tc = paramiko.Transport(sockc)
|
|
|
|
ts = paramiko.Transport(socks)
|
|
|
|
|
|
|
|
host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key')
|
|
|
|
ts.add_server_key(host_key)
|
|
|
|
event = threading.Event()
|
|
|
|
server = StubServer()
|
|
|
|
ts.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer)
|
|
|
|
ts.start_server(event, server)
|
|
|
|
tc.connect(username='slowdive', password='pygmalion')
|
|
|
|
event.wait(1.0)
|
|
|
|
# self.assert_(self.ts.is_active())
|
|
|
|
|
|
|
|
sftp = paramiko.SFTP.from_transport(tc)
|
|
|
|
init_loopback = staticmethod(init_loopback)
|
|
|
|
|
|
|
|
def set_big_file_test(onoff):
|
|
|
|
global g_big_file_test
|
|
|
|
g_big_file_test = onoff
|
|
|
|
set_big_file_test = staticmethod(set_big_file_test)
|
|
|
|
|
2004-03-08 12:54:19 -05:00
|
|
|
def setUp(self):
|
|
|
|
sftp.mkdir(FOLDER)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sftp.rmdir(FOLDER)
|
|
|
|
|
2005-02-06 18:30:40 -05:00
|
|
|
def test_1_file(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
2005-02-06 18:30:40 -05:00
|
|
|
verify that we can create a file.
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
f = sftp.open(FOLDER + '/test', 'w')
|
|
|
|
try:
|
|
|
|
self.assertEqual(f.stat().st_size, 0)
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/test')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_2_close(self):
|
2005-04-17 20:30:52 -04:00
|
|
|
"""
|
|
|
|
verify that closing the sftp session doesn't do anything bad, and that
|
|
|
|
a new one can be opened.
|
|
|
|
"""
|
|
|
|
global sftp
|
|
|
|
sftp.close()
|
|
|
|
try:
|
|
|
|
sftp.open(FOLDER + '/test2', 'w')
|
|
|
|
self.fail('expected exception')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
sftp = paramiko.SFTP.from_transport(tc)
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_3_write(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
self.assertEqual(sftp.stat(FOLDER + '/duck.txt').st_size, 1483)
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/duck.txt')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_4_append(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
|
|
|
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')
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/append.txt')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_5_rename(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
verify that renaming a file works.
|
|
|
|
"""
|
|
|
|
f = sftp.open(FOLDER + '/first.txt', 'w')
|
|
|
|
try:
|
|
|
|
f.write('content!\n');
|
|
|
|
f.close()
|
|
|
|
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')
|
2005-02-06 18:30:40 -05:00
|
|
|
except IOError:
|
2004-03-08 12:54:19 -05:00
|
|
|
pass
|
|
|
|
f = sftp.open(FOLDER + '/second.txt', 'r')
|
|
|
|
f.seek(-6, f.SEEK_END)
|
|
|
|
self.assertEqual(f.read(4), 'tent')
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/first.txt')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/second.txt')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_6_folder(self):
|
2005-02-06 18:30:40 -05:00
|
|
|
"""
|
|
|
|
create a temporary folder, verify that we can create a file in it, then
|
|
|
|
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.remove(FOLDER + '/subfolder/test')
|
|
|
|
sftp.rmdir(FOLDER + '/subfolder')
|
|
|
|
try:
|
|
|
|
f = sftp.open(FOLDER + '/subfolder/test')
|
|
|
|
# shouldn't be able to create that file
|
|
|
|
self.assert_(False, 'no exception at dummy file creation')
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_7_listdir(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
verify that a folder can be created, a bunch of files can be placed in it,
|
|
|
|
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()
|
|
|
|
|
|
|
|
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)
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/duck.txt')
|
|
|
|
sftp.remove(FOLDER + '/fish.txt')
|
|
|
|
sftp.remove(FOLDER + '/tertiary.py')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_8_setstat(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
verify that the setstat functions (chown, chmod, utime) work.
|
|
|
|
"""
|
|
|
|
f = sftp.open(FOLDER + '/special', 'w')
|
|
|
|
try:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
stat = sftp.stat(FOLDER + '/special')
|
|
|
|
sftp.chmod(FOLDER + '/special', (stat.st_mode & ~0777) | 0600)
|
|
|
|
self.assertEqual(sftp.stat(FOLDER + '/special').st_mode & 0777, 0600)
|
|
|
|
|
|
|
|
mtime = stat.st_mtime - 3600
|
|
|
|
atime = stat.st_atime - 1800
|
|
|
|
sftp.utime(FOLDER + '/special', (atime, mtime))
|
|
|
|
nstat = sftp.stat(FOLDER + '/special')
|
|
|
|
self.assertEqual(nstat.st_mtime, mtime)
|
|
|
|
self.assertEqual(nstat.st_atime, atime)
|
|
|
|
|
|
|
|
# can't really test chown, since we'd have to know a valid uid.
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/special')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_9_readline_seek(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
create a text file and write a bunch of text into it. then count the lines
|
|
|
|
in the file, and seek around to retreive particular lines. this should
|
|
|
|
verify that read buffering and 'tell' work well together, and that read
|
|
|
|
buffering is reset on 'seek'.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
f = sftp.open(FOLDER + '/duck.txt', 'w')
|
|
|
|
f.write(ARTICLE)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
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()
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/duck.txt')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_A_write_seek(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
|
|
|
self.assertEqual(sftp.stat(FOLDER + '/testing.txt').st_size, 13)
|
|
|
|
f = sftp.open(FOLDER + '/testing.txt', 'r')
|
|
|
|
data = f.read(20)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(data, 'hello kiddy.\n')
|
|
|
|
finally:
|
|
|
|
sftp.remove(FOLDER + '/testing.txt')
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_B_symlink(self):
|
2004-03-08 12:54:19 -05:00
|
|
|
"""
|
|
|
|
create a symlink and then check that lstat doesn't follow it.
|
|
|
|
"""
|
|
|
|
f = sftp.open(FOLDER + '/original.txt', 'w')
|
|
|
|
try:
|
|
|
|
f.write('original\n')
|
|
|
|
f.close()
|
|
|
|
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()
|
2004-12-19 14:56:48 -05:00
|
|
|
|
|
|
|
cwd = sftp.normalize('.')
|
|
|
|
if cwd[-1] == '/':
|
|
|
|
cwd = cwd[:-1]
|
|
|
|
abs_path = cwd + '/' + FOLDER + '/original.txt'
|
|
|
|
sftp.symlink(abs_path, FOLDER + '/link2.txt')
|
|
|
|
self.assertEqual(abs_path, sftp.readlink(FOLDER + '/link2.txt'))
|
|
|
|
|
2004-03-08 12:54:19 -05:00
|
|
|
self.assertEqual(sftp.lstat(FOLDER + '/link.txt').st_size, 12)
|
2004-12-10 03:30:44 -05:00
|
|
|
self.assertEqual(sftp.stat(FOLDER + '/link.txt').st_size, 9)
|
2004-12-19 14:56:48 -05:00
|
|
|
# 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.assertEqual(sftp.stat(FOLDER + '/link2.txt').st_size, 9)
|
2004-03-08 12:54:19 -05:00
|
|
|
self.assertEqual(sftp.stat(FOLDER + '/original.txt').st_size, 9)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/link.txt')
|
|
|
|
except:
|
|
|
|
pass
|
2004-12-19 14:56:48 -05:00
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/link2.txt')
|
|
|
|
except:
|
|
|
|
pass
|
2004-03-08 12:54:19 -05:00
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/original.txt')
|
|
|
|
except:
|
|
|
|
pass
|
2004-05-29 14:58:11 -04:00
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_C_flush_seek(self):
|
2004-06-10 13:35:30 -04:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
|
|
|
f = sftp.open(FOLDER + '/happy.txt', 'r')
|
|
|
|
self.assertEqual(f.readline(), 'full line?\n')
|
|
|
|
self.assertEqual(f.read(7), 'partial')
|
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
sftp.remove(FOLDER + '/happy.txt')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_D_lots_of_files(self):
|
2004-05-29 14:58:11 -04:00
|
|
|
"""
|
|
|
|
create a bunch of files over the same session.
|
|
|
|
"""
|
2004-12-19 14:56:48 -05:00
|
|
|
global g_big_file_test
|
|
|
|
if not g_big_file_test:
|
|
|
|
return
|
2004-05-29 14:58:11 -04:00
|
|
|
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()
|
|
|
|
sftp.chmod('%s/file%d.txt' % (FOLDER, i), 0660)
|
|
|
|
|
|
|
|
# now make sure every file is there, by creating a list of filenmes
|
|
|
|
# and reading them in random order.
|
|
|
|
numlist = 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()
|
|
|
|
numlist.remove(r)
|
|
|
|
finally:
|
|
|
|
for i in range(numfiles):
|
|
|
|
try:
|
|
|
|
sftp.remove('%s/file%d.txt' % (FOLDER, i))
|
|
|
|
except:
|
|
|
|
pass
|
2004-06-10 13:35:30 -04:00
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_E_big_file(self):
|
2004-06-10 13:35:30 -04:00
|
|
|
"""
|
|
|
|
write a 1MB file, with no linefeeds, using line buffering.
|
|
|
|
FIXME: this is slow! what causes the slowness?
|
|
|
|
"""
|
2004-12-19 14:56:48 -05:00
|
|
|
global g_big_file_test
|
|
|
|
if not g_big_file_test:
|
|
|
|
return
|
2004-06-10 13:35:30 -04:00
|
|
|
kblob = (1024 * 'x')
|
|
|
|
try:
|
|
|
|
f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 1)
|
|
|
|
for n in range(1024):
|
|
|
|
f.write(kblob)
|
|
|
|
if n % 128 == 0:
|
|
|
|
sys.stderr.write('.')
|
|
|
|
f.close()
|
|
|
|
sys.stderr.write(' ')
|
|
|
|
|
|
|
|
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
|
|
|
finally:
|
|
|
|
sftp.remove('%s/hongry.txt' % FOLDER)
|
2004-09-11 16:37:59 -04:00
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_F_big_file_big_buffer(self):
|
2005-02-28 02:49:56 -05:00
|
|
|
"""
|
2005-02-28 03:06:08 -05:00
|
|
|
write a 1MB file, with no linefeeds, and a big buffer.
|
2005-02-28 02:49:56 -05:00
|
|
|
"""
|
|
|
|
global g_big_file_test
|
|
|
|
if not g_big_file_test:
|
|
|
|
return
|
|
|
|
kblob = (1024 * 1024 * 'x')
|
|
|
|
try:
|
2005-02-28 03:06:08 -05:00
|
|
|
f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024)
|
2005-02-28 02:49:56 -05:00
|
|
|
f.write(kblob)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
|
|
|
finally:
|
|
|
|
sftp.remove('%s/hongry.txt' % FOLDER)
|
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_G_realpath(self):
|
2004-09-11 16:37:59 -04:00
|
|
|
"""
|
|
|
|
test that realpath is returning something non-empty and not an
|
|
|
|
error.
|
|
|
|
"""
|
|
|
|
pwd = sftp.normalize('.')
|
|
|
|
self.assert_(len(pwd) > 0)
|
|
|
|
f = sftp.normalize('./' + FOLDER)
|
|
|
|
self.assert_(len(f) > 0)
|
2004-12-19 14:56:48 -05:00
|
|
|
self.assertEquals(os.path.join(pwd, FOLDER), f)
|
2004-11-06 21:08:11 -05:00
|
|
|
|
[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-5]
split out Packetizer, fix banner detection bug, new unit test
split out a chunk of BaseTransport into a Packetizer class, which handles
the in/out packet data, ciphers, etc. it didn't make the code any smaller
(transport.py is still close to 1500 lines, which is awful) but it did split
out a coherent chunk of functionality into a discrete unit.
in the process, fixed a bug that alain spineux pointed out: the banner
check was too forgiving and would block forever waiting for an SSH banner.
now it waits 5 seconds for the first line, and 2 seconds for each subsequent
line, before giving up.
added a unit test to test keepalive, since i wasn't sure that was still
working after pulling out Packetizer.
2005-05-01 04:04:59 -04:00
|
|
|
def test_H_mkdir(self):
|
2004-11-06 21:08:11 -05:00
|
|
|
"""
|
|
|
|
verify that mkdir/rmdir work.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
sftp.mkdir(FOLDER + '/subfolder')
|
|
|
|
except:
|
|
|
|
self.assert_(False, 'exception creating subfolder')
|
|
|
|
try:
|
|
|
|
sftp.mkdir(FOLDER + '/subfolder')
|
|
|
|
self.assert_(False, 'no exception overwriting subfolder')
|
2005-02-06 18:30:40 -05:00
|
|
|
except IOError:
|
2004-11-06 21:08:11 -05:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
sftp.rmdir(FOLDER + '/subfolder')
|
|
|
|
except:
|
|
|
|
self.assert_(False, 'exception removing subfolder')
|
|
|
|
try:
|
|
|
|
sftp.rmdir(FOLDER + '/subfolder')
|
|
|
|
self.assert_(False, 'no exception removing nonexistent subfolder')
|
2005-02-06 18:30:40 -05:00
|
|
|
except IOError:
|
2004-11-06 21:08:11 -05:00
|
|
|
pass
|