126
129
"""Test the SFTP transport with homedir based relative paths."""
132
# Only SFTPHomeDirServer is tested here
129
133
self._get_remote_is_absolute = False
130
134
super(SFTPTransportTestRelativeRoot, self).setUp()
132
136
def test__remote_path_relative_root(self):
133
137
# relative paths are preserved
134
138
t = self.get_transport('')
135
# the remote path should be ''
136
self.assertEqual('', t._path)
139
self.assertEqual('/~/', t._path)
140
# the remote path should be relative to home dir
141
# (i.e. not begining with a '/')
137
142
self.assertEqual('a', t._remote_path('a'))
140
class FakeSFTPTransport (object):
142
fake = FakeSFTPTransport()
145
145
class SFTPNonServerTest(TestCase):
147
147
TestCase.setUp(self)
148
148
if not paramiko_loaded:
149
149
raise TestSkipped('you must have paramiko to run this test')
151
def test_parse_url(self):
152
from bzrlib.transport.sftp import SFTPTransport
153
s = SFTPTransport('sftp://simple.example.com/home/source', clone_from=fake)
154
self.assertEquals(s._host, 'simple.example.com')
155
self.assertEquals(s._port, None)
156
self.assertEquals(s._path, '/home/source')
157
self.failUnless(s._password is None)
159
self.assertEquals(s.base, 'sftp://simple.example.com/home/source/')
161
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/~/relative', clone_from=fake)
151
def test_parse_url_with_home_dir(self):
152
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/~/relative')
162
153
self.assertEquals(s._host, 'example.com')
163
154
self.assertEquals(s._port, 2222)
164
self.assertEquals(s._username, 'robey')
155
self.assertEquals(s._user, 'robey')
165
156
self.assertEquals(s._password, 'h@t')
166
self.assertEquals(s._path, 'relative')
168
# Base should not keep track of the password
169
self.assertEquals(s.base, 'sftp://robey@example.com:2222/~/relative/')
157
self.assertEquals(s._path, '/~/relative/')
171
159
def test_relpath(self):
172
from bzrlib.transport.sftp import SFTPTransport
173
from bzrlib.errors import PathNotChild
175
s = SFTPTransport('sftp://user@host.com/abs/path', clone_from=fake)
176
self.assertEquals(s.relpath('sftp://user@host.com/abs/path/sub'), 'sub')
177
# Can't test this one, because we actually get an AssertionError
178
# TODO: Consider raising an exception rather than an assert
179
#self.assertRaises(PathNotChild, s.relpath, 'http://user@host.com/abs/path/sub')
180
self.assertRaises(PathNotChild, s.relpath, 'sftp://user2@host.com/abs/path/sub')
181
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@otherhost.com/abs/path/sub')
182
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com:33/abs/path/sub')
183
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com/~/rel/path/sub')
185
# Make sure it works when we don't supply a username
186
s = SFTPTransport('sftp://host.com/abs/path', clone_from=fake)
187
self.assertEquals(s.relpath('sftp://host.com/abs/path/sub'), 'sub')
189
# Make sure it works when parts of the path will be url encoded
190
# TODO: These may be incorrect, we might need to urllib.urlencode() before
191
# we pass the paths into the SFTPTransport constructor
192
s = SFTPTransport('sftp://host.com/dev/,path', clone_from=fake)
193
self.assertEquals(s.relpath('sftp://host.com/dev/,path/sub'), 'sub')
194
s = SFTPTransport('sftp://host.com/dev/%path', clone_from=fake)
195
self.assertEquals(s.relpath('sftp://host.com/dev/%path/sub'), 'sub')
197
def test_parse_invalid_url(self):
198
from bzrlib.transport.sftp import SFTPTransport, TransportError
200
s = SFTPTransport('sftp://lilypond.org:~janneke/public_html/bzr/gub',
202
self.fail('expected exception not raised')
203
except TransportError, e:
204
self.assertEquals(str(e),
206
'invalid port number ~janneke in url:\n'
207
'sftp://lilypond.org:~janneke/public_html/bzr/gub ')
160
s = SFTPTransport('sftp://user@host.com/abs/path')
161
self.assertRaises(errors.PathNotChild, s.relpath,
162
'sftp://user@host.com/~/rel/path/sub')
209
164
def test_get_paramiko_vendor(self):
210
165
"""Test that if no 'ssh' is available we get builtin paramiko"""