90
def test_multiple_connections(self):
91
t = self.get_transport()
92
self.assertTrue('sftpserver - new connection' in self.get_server().logs)
93
self.get_server().logs = []
94
# The second request should reuse the first connection
95
# SingleListener only allows for a single connection,
96
# So the next line fails unless the connection is reused
97
t2 = self.get_transport()
98
self.assertEquals(self.get_server().logs, [])
101
107
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
102
108
"""Test the SFTP transport with homedir based relative paths."""
104
110
def test__remote_path(self):
111
if sys.platform == 'darwin':
112
# This test is about sftp absolute path handling. There is already
113
# (in this test) a TODO about windows needing an absolute path
114
# without drive letter. To me, using self.test_dir is a trick to
115
# get an absolute path for comparison purposes. That fails for OSX
116
# because the sftp server doesn't resolve the links (and it doesn't
117
# have to). --vila 20070924
118
self.knownFailure('Mac OSX symlinks /tmp to /private/tmp,'
119
' testing against self.test_dir'
120
' is not appropriate')
105
121
t = self.get_transport()
122
# This test require unix-like absolute path
123
test_dir = self.test_dir
124
if sys.platform == 'win32':
125
# using hack suggested by John Meinel.
126
# TODO: write another mock server for this test
127
# and use absolute path without drive letter
128
test_dir = '/' + test_dir
106
129
# try what is currently used:
107
130
# remote path = self._abspath(relpath)
108
self.assertEqual(self.test_dir + '/relative', t._remote_path('relative'))
131
self.assertIsSameRealPath(test_dir + '/relative',
132
t._remote_path('relative'))
109
133
# we dont os.path.join because windows gives us the wrong path
110
root_segments = self.test_dir.split('/')
134
root_segments = test_dir.split('/')
111
135
root_parent = '/'.join(root_segments[:-1])
112
136
# .. should be honoured
113
self.assertEqual(root_parent + '/sibling', t._remote_path('../sibling'))
137
self.assertIsSameRealPath(root_parent + '/sibling',
138
t._remote_path('../sibling'))
114
139
# / should be illegal ?
115
140
### FIXME decide and then test for all transports. RBC20051208
118
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
143
class SFTPTransportTestRelativeRoot(TestCaseWithSFTPServer):
119
144
"""Test the SFTP transport with homedir based relative paths."""
147
# Only SFTPHomeDirServer is tested here
122
148
self._get_remote_is_absolute = False
123
super(SFTPTransportTestRelative, self).setUp()
149
super(SFTPTransportTestRelativeRoot, self).setUp()
125
151
def test__remote_path_relative_root(self):
126
152
# relative paths are preserved
127
153
t = self.get_transport('')
154
self.assertEqual('/~/', t._path)
155
# the remote path should be relative to home dir
156
# (i.e. not begining with a '/')
128
157
self.assertEqual('a', t._remote_path('a'))
131
class FakeSFTPTransport (object):
133
fake = FakeSFTPTransport()
136
160
class SFTPNonServerTest(TestCase):
138
162
TestCase.setUp(self)
139
163
if not paramiko_loaded:
140
164
raise TestSkipped('you must have paramiko to run this test')
142
def test_parse_url(self):
143
from bzrlib.transport.sftp import SFTPTransport
144
s = SFTPTransport('sftp://simple.example.com/home/source', clone_from=fake)
145
self.assertEquals(s._host, 'simple.example.com')
146
self.assertEquals(s._port, None)
147
self.assertEquals(s._path, '/home/source')
148
self.failUnless(s._password is None)
150
self.assertEquals(s.base, 'sftp://simple.example.com/home/source/')
152
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/~/relative', clone_from=fake)
166
def test_parse_url_with_home_dir(self):
167
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/~/relative')
153
168
self.assertEquals(s._host, 'example.com')
154
169
self.assertEquals(s._port, 2222)
155
self.assertEquals(s._username, 'robey')
170
self.assertEquals(s._user, 'robey')
156
171
self.assertEquals(s._password, 'h@t')
157
self.assertEquals(s._path, 'relative')
159
# Base should not keep track of the password
160
self.assertEquals(s.base, 'sftp://robey@example.com:2222/~/relative/')
172
self.assertEquals(s._path, '/~/relative/')
162
174
def test_relpath(self):
163
from bzrlib.transport.sftp import SFTPTransport
164
from bzrlib.errors import PathNotChild
166
s = SFTPTransport('sftp://user@host.com/abs/path', clone_from=fake)
167
self.assertEquals(s.relpath('sftp://user@host.com/abs/path/sub'), 'sub')
168
# Can't test this one, because we actually get an AssertionError
169
# TODO: Consider raising an exception rather than an assert
170
#self.assertRaises(PathNotChild, s.relpath, 'http://user@host.com/abs/path/sub')
171
self.assertRaises(PathNotChild, s.relpath, 'sftp://user2@host.com/abs/path/sub')
172
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@otherhost.com/abs/path/sub')
173
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com:33/abs/path/sub')
174
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com/~/rel/path/sub')
176
# Make sure it works when we don't supply a username
177
s = SFTPTransport('sftp://host.com/abs/path', clone_from=fake)
178
self.assertEquals(s.relpath('sftp://host.com/abs/path/sub'), 'sub')
180
# Make sure it works when parts of the path will be url encoded
181
# TODO: These may be incorrect, we might need to urllib.urlencode() before
182
# we pass the paths into the SFTPTransport constructor
183
s = SFTPTransport('sftp://host.com/dev/,path', clone_from=fake)
184
self.assertEquals(s.relpath('sftp://host.com/dev/,path/sub'), 'sub')
185
s = SFTPTransport('sftp://host.com/dev/%path', clone_from=fake)
186
self.assertEquals(s.relpath('sftp://host.com/dev/%path/sub'), 'sub')
188
def test_parse_invalid_url(self):
189
from bzrlib.transport.sftp import SFTPTransport, TransportError
191
s = SFTPTransport('sftp://lilypond.org:~janneke/public_html/bzr/gub',
193
self.fail('expected exception not raised')
194
except TransportError, e:
195
self.assertEquals(str(e),
196
'Transport error: ~janneke: invalid port number ')
175
s = SFTPTransport('sftp://user@host.com/abs/path')
176
self.assertRaises(errors.PathNotChild, s.relpath,
177
'sftp://user@host.com/~/rel/path/sub')
179
def test_get_paramiko_vendor(self):
180
"""Test that if no 'ssh' is available we get builtin paramiko"""
181
from bzrlib.transport import ssh
182
# set '.' as the only location in the path, forcing no 'ssh' to exist
183
orig_vendor = ssh._ssh_vendor_manager._cached_ssh_vendor
184
orig_path = set_or_unset_env('PATH', '.')
186
# No vendor defined yet, query for one
187
ssh._ssh_vendor_manager.clear_cache()
188
vendor = ssh._get_ssh_vendor()
189
self.assertIsInstance(vendor, ssh.ParamikoVendor)
191
set_or_unset_env('PATH', orig_path)
192
ssh._ssh_vendor_manager._cached_ssh_vendor = orig_vendor
194
def test_abspath_root_sibling_server(self):
195
from bzrlib.transport.sftp import SFTPSiblingAbsoluteServer
196
server = SFTPSiblingAbsoluteServer()
199
transport = get_transport(server.get_url())
200
self.assertFalse(transport.abspath('/').endswith('/~/'))
201
self.assertTrue(transport.abspath('/').endswith('/'))
199
207
class SFTPBranchTest(TestCaseWithSFTPServer):