1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from bzrlib.tests import TestCaseInTempDir, TestCase
22
from bzrlib.tests.test_transport import TestTransportMixIn
26
from stub_sftp import StubServer, StubSFTPServer
27
paramiko_loaded = True
29
paramiko_loaded = False
31
# XXX: 20051124 jamesh
32
# The tests currently pop up a password prompt when an external ssh
33
# is used. This forces the use of the paramiko implementation.
34
import bzrlib.transport.sftp
35
bzrlib.transport.sftp._ssh_vendor = 'none'
39
-----BEGIN RSA PRIVATE KEY-----
40
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
41
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
42
d8ufMCkjeXsQkKqFbAlQcnWMCRnOoPHS3I4vi6hmnDDeeYTSRvfLbW0fhwIBIwKB
43
gBIiOqZYaoqbeD9OS9z2K9KR2atlTxGxOJPXiP4ESqP3NVScWNwyZ3NXHpyrJLa0
44
EbVtzsQhLn6rF+TzXnOlcipFvjsem3iYzCpuChfGQ6SovTcOjHV9z+hnpXvQ/fon
45
soVRZY65wKnF7IAoUwTmJS9opqgrN6kRgCd3DASAMd1bAkEA96SBVWFt/fJBNJ9H
46
tYnBKZGw0VeHOYmVYbvMSstssn8un+pQpUm9vlG/bp7Oxd/m+b9KWEh2xPfv6zqU
47
avNwHwJBANqzGZa/EpzF4J8pGti7oIAPUIDGMtfIcmqNXVMckrmzQ2vTfqtkEZsA
48
4rE1IERRyiJQx6EJsz21wJmGV9WJQ5kCQQDwkS0uXqVdFzgHO6S++tjmjYcxwr3g
49
H0CoFYSgbddOT6miqRskOQF3DZVkJT3kyuBgU2zKygz52ukQZMqxCb1fAkASvuTv
50
qfpH87Qq5kQhNKdbbwbmd2NxlNabazPijWuphGTdW0VfJdWfklyS2Kr+iqrs/5wV
51
HhathJt636Eg7oIjAkA8ht3MQ+XSl9yIJIS8gVpbPxSw5OMfw0PjVE7tBdQruiSc
52
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
53
-----END RSA PRIVATE KEY-----
57
class SingleListener (threading.Thread):
58
def __init__(self, callback):
59
threading.Thread.__init__(self)
60
self._callback = callback
61
self._socket = socket.socket()
62
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
63
self._socket.bind(('localhost', 0))
64
self._socket.listen(1)
65
self.port = self._socket.getsockname()[1]
66
self.stop_event = threading.Event()
69
s, _ = self._socket.accept()
70
# now close the listen socket
72
self._callback(s, self.stop_event)
76
# We should consider waiting for the other thread
77
# to stop, because otherwise we get spurious
78
# bzr: ERROR: Socket exception: Connection reset by peer (54)
79
# because the test suite finishes before the thread has a chance
80
# to close. (Especially when only running a few tests)
83
class TestCaseWithSFTPServer (TestCaseInTempDir):
85
Execute a test case with a stub SFTP server, serving files from the local
86
filesystem over the loopback network.
89
def _run_server(self, s, stop_event):
90
ssh_server = paramiko.Transport(s)
91
key_file = os.path.join(self._root, 'test_rsa.key')
92
file(key_file, 'w').write(STUB_SERVER_KEY)
93
host_key = paramiko.RSAKey.from_private_key_file(key_file)
94
ssh_server.add_server_key(host_key)
95
server = StubServer(self)
96
ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer, root=self._root)
97
event = threading.Event()
98
ssh_server.start_server(event, server)
100
stop_event.wait(30.0)
103
TestCaseInTempDir.setUp(self)
104
self._root = self.test_dir
105
self._is_setup = False
107
def delayed_setup(self):
108
# some tests are just stubs that call setUp and then immediately call
109
# tearDwon. so don't create the port listener until get_transport is
110
# called and we know we're in an actual test.
113
self._listener = SingleListener(self._run_server)
114
self._listener.setDaemon(True)
115
self._listener.start()
116
self._sftp_url = 'sftp://foo:bar@localhost:%d/' % (self._listener.port,)
117
self._is_setup = True
121
self._listener.stop()
122
except AttributeError:
124
TestCaseInTempDir.tearDown(self)
127
class SFTPTransportTest (TestCaseWithSFTPServer, TestTransportMixIn):
131
TestCaseWithSFTPServer.setUp(self)
134
def log(self, *args):
135
"""Override the default log to grab sftp server messages"""
136
TestCaseWithSFTPServer.log(self, *args)
137
if args and args[0].startswith('sftpserver'):
138
self.sftplogs.append(args[0])
140
def get_transport(self):
142
from bzrlib.transport.sftp import SFTPTransport
144
return SFTPTransport(url)
146
def test_sftp_locks(self):
147
from bzrlib.errors import LockError
148
t = self.get_transport()
150
l = t.lock_write('bogus')
151
self.failUnlessExists('bogus.write-lock')
153
# Don't wait for the lock, locking an already locked
154
# file should raise an assert
155
self.assertRaises(LockError, t.lock_write, 'bogus')
158
self.failIf(os.path.lexists('bogus.write-lock'))
160
open('something.write-lock', 'wb').write('fake lock\n')
161
self.assertRaises(LockError, t.lock_write, 'something')
162
os.remove('something.write-lock')
164
l = t.lock_write('something')
166
l2 = t.lock_write('bogus')
171
def test_multiple_connections(self):
172
t = self.get_transport()
173
self.assertEquals(self.sftplogs,
174
['sftpserver - authorizing: foo'
175
, 'sftpserver - channel request: session, 1'])
177
# The second request should reuse the first connection
178
# SingleListener only allows for a single connection,
179
# So the next line fails unless the connection is reused
180
t2 = self.get_transport()
181
self.assertEquals(self.sftplogs, [])
184
class FakeSFTPTransport (object):
186
fake = FakeSFTPTransport()
189
class SFTPNonServerTest(TestCase):
190
def test_parse_url(self):
191
from bzrlib.transport.sftp import SFTPTransport
192
s = SFTPTransport('sftp://simple.example.com/%2fhome/source', clone_from=fake)
193
self.assertEquals(s._host, 'simple.example.com')
194
self.assertEquals(s._port, 22)
195
self.assertEquals(s._path, '/home/source')
196
self.failUnless(s._password is None)
198
self.assertEquals(s.base, 'sftp://simple.example.com/%2Fhome/source')
200
s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/relative', clone_from=fake)
201
self.assertEquals(s._host, 'example.com')
202
self.assertEquals(s._port, 2222)
203
self.assertEquals(s._username, 'robey')
204
self.assertEquals(s._password, 'h@t')
205
self.assertEquals(s._path, 'relative')
207
# Base should not keep track of the password
208
self.assertEquals(s.base, 'sftp://robey@example.com:2222/relative')
210
# Double slash should be accepted instead of using %2F
211
s = SFTPTransport('sftp://user@example.com:22//absolute/path', clone_from=fake)
212
self.assertEquals(s._host, 'example.com')
213
self.assertEquals(s._port, 22)
214
self.assertEquals(s._username, 'user')
215
self.assertEquals(s._password, None)
216
self.assertEquals(s._path, '/absolute/path')
218
# Also, don't show the port if it is the default 22
219
self.assertEquals(s.base, 'sftp://user@example.com/%2Fabsolute/path')
221
def test_parse_invalid_url(self):
222
from bzrlib.transport.sftp import SFTPTransport, SFTPTransportError
224
s = SFTPTransport('sftp://lilypond.org:~janneke/public_html/bzr/gub',
226
self.fail('expected exception not raised')
227
except SFTPTransportError, e:
228
self.assertEquals(str(e),
229
'~janneke: invalid port number')
232
class SFTPBranchTest(TestCaseWithSFTPServer):
233
"""Test some stuff when accessing a bzr Branch over sftp"""
235
def test_lock_file(self):
236
"""Make sure that a Branch accessed over sftp tries to lock itself."""
237
from bzrlib.branch import Branch
240
b = Branch.initialize(self._sftp_url)
241
self.failUnlessExists('.bzr/')
242
self.failUnlessExists('.bzr/branch-format')
243
self.failUnlessExists('.bzr/branch-lock')
245
self.failIf(os.path.lexists('.bzr/branch-lock.write-lock'))
247
self.failUnlessExists('.bzr/branch-lock.write-lock')
249
self.failIf(os.path.lexists('.bzr/branch-lock.write-lock'))
252
if not paramiko_loaded:
254
del SFTPTransportTest
255
del SFTPNonServerTest