82
94
file(key_file, 'w').write(STUB_SERVER_KEY)
83
95
host_key = paramiko.RSAKey.from_private_key_file(key_file)
84
96
ssh_server.add_server_key(host_key)
97
server = StubServer(self)
86
98
ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer, root=self._root)
87
99
event = threading.Event()
88
100
ssh_server.start_server(event, server)
173
def test_multiple_connections(self):
174
t = self.get_transport()
175
self.assertEquals(self.sftplogs,
176
['sftpserver - authorizing: foo'
177
, 'sftpserver - channel request: session, 1'])
179
# The second request should reuse the first connection
180
# SingleListener only allows for a single connection,
181
# So the next line fails unless the connection is reused
182
t2 = self.get_transport()
183
self.assertEquals(self.sftplogs, [])
151
186
class FakeSFTPTransport (object):
153
188
fake = FakeSFTPTransport()
156
class SFTPNonServerTest (unittest.TestCase):
191
class SFTPNonServerTest(TestCase):
157
192
def test_parse_url(self):
158
193
from bzrlib.transport.sftp import SFTPTransport
159
194
s = SFTPTransport('sftp://simple.example.com/%2fhome/source', clone_from=fake)
160
195
self.assertEquals(s._host, 'simple.example.com')
161
self.assertEquals(s._port, 22)
196
self.assertEquals(s._port, None)
162
197
self.assertEquals(s._path, '/home/source')
163
self.assert_(s._password is None)
198
self.failUnless(s._password is None)
200
self.assertEquals(s.base, 'sftp://simple.example.com/%2Fhome/source')
165
202
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/relative', clone_from=fake)
166
203
self.assertEquals(s._host, 'example.com')
168
205
self.assertEquals(s._username, 'robey')
169
206
self.assertEquals(s._password, 'h@t')
170
207
self.assertEquals(s._path, 'relative')
209
# Base should not keep track of the password
210
self.assertEquals(s.base, 'sftp://robey@example.com:2222/relative')
212
# Double slash should be accepted instead of using %2F
213
s = SFTPTransport('sftp://user@example.com:22//absolute/path', clone_from=fake)
214
self.assertEquals(s._host, 'example.com')
215
self.assertEquals(s._port, 22)
216
self.assertEquals(s._username, 'user')
217
self.assertEquals(s._password, None)
218
self.assertEquals(s._path, '/absolute/path')
220
# Also, don't show the port if it is the default 22
221
self.assertEquals(s.base, 'sftp://user@example.com:22/%2Fabsolute/path')
223
def test_relpath(self):
224
from bzrlib.transport.sftp import SFTPTransport
225
from bzrlib.errors import PathNotChild
227
s = SFTPTransport('sftp://user@host.com//abs/path', clone_from=fake)
228
self.assertEquals(s.relpath('sftp://user@host.com//abs/path/sub'), 'sub')
229
# Can't test this one, because we actually get an AssertionError
230
# TODO: Consider raising an exception rather than an assert
231
#self.assertRaises(PathNotChild, s.relpath, 'http://user@host.com//abs/path/sub')
232
self.assertRaises(PathNotChild, s.relpath, 'sftp://user2@host.com//abs/path/sub')
233
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@otherhost.com//abs/path/sub')
234
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com:33//abs/path/sub')
235
self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com/abs/path/sub')
237
# Make sure it works when we don't supply a username
238
s = SFTPTransport('sftp://host.com//abs/path', clone_from=fake)
239
self.assertEquals(s.relpath('sftp://host.com//abs/path/sub'), 'sub')
241
# Make sure it works when parts of the path will be url encoded
242
# TODO: These may be incorrect, we might need to urllib.urlencode() before
243
# we pass the paths into the SFTPTransport constructor
244
s = SFTPTransport('sftp://host.com/dev/,path', clone_from=fake)
245
self.assertEquals(s.relpath('sftp://host.com/dev/,path/sub'), 'sub')
246
s = SFTPTransport('sftp://host.com/dev/%path', clone_from=fake)
247
self.assertEquals(s.relpath('sftp://host.com/dev/%path/sub'), 'sub')
249
def test_parse_invalid_url(self):
250
from bzrlib.transport.sftp import SFTPTransport, TransportError
252
s = SFTPTransport('sftp://lilypond.org:~janneke/public_html/bzr/gub',
254
self.fail('expected exception not raised')
255
except TransportError, e:
256
self.assertEquals(str(e),
257
'~janneke: invalid port number')
173
260
class SFTPBranchTest(TestCaseWithSFTPServer):
174
261
"""Test some stuff when accessing a bzr Branch over sftp"""
190
277
self.failIf(os.path.lexists('.bzr/branch-lock.write-lock'))
279
def test_no_working_tree(self):
280
from bzrlib.branch import Branch
282
b = Branch.initialize(self._sftp_url)
283
self.assertRaises(errors.NoWorkingTree, b.working_tree)
285
def test_push_support(self):
286
from bzrlib.branch import Branch
289
self.build_tree(['a/', 'a/foo'])
290
b = Branch.initialize('a')
293
t.commit('foo', rev_id='a1')
296
b2 = Branch.initialize(self._sftp_url + 'b')
299
self.assertEquals(b2.revision_history(), ['a1'])
193
302
if not paramiko_loaded:
194
303
# TODO: Skip these