817
825
self._translate_io_exception(e, abspath, ': unable to open',
818
826
failure_exc=FileExists)
829
# ------------- server test implementation --------------
833
from bzrlib.tests.stub_sftp import StubServer, StubSFTPServer
835
STUB_SERVER_KEY = """
836
-----BEGIN RSA PRIVATE KEY-----
837
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
838
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
839
d8ufMCkjeXsQkKqFbAlQcnWMCRnOoPHS3I4vi6hmnDDeeYTSRvfLbW0fhwIBIwKB
840
gBIiOqZYaoqbeD9OS9z2K9KR2atlTxGxOJPXiP4ESqP3NVScWNwyZ3NXHpyrJLa0
841
EbVtzsQhLn6rF+TzXnOlcipFvjsem3iYzCpuChfGQ6SovTcOjHV9z+hnpXvQ/fon
842
soVRZY65wKnF7IAoUwTmJS9opqgrN6kRgCd3DASAMd1bAkEA96SBVWFt/fJBNJ9H
843
tYnBKZGw0VeHOYmVYbvMSstssn8un+pQpUm9vlG/bp7Oxd/m+b9KWEh2xPfv6zqU
844
avNwHwJBANqzGZa/EpzF4J8pGti7oIAPUIDGMtfIcmqNXVMckrmzQ2vTfqtkEZsA
845
4rE1IERRyiJQx6EJsz21wJmGV9WJQ5kCQQDwkS0uXqVdFzgHO6S++tjmjYcxwr3g
846
H0CoFYSgbddOT6miqRskOQF3DZVkJT3kyuBgU2zKygz52ukQZMqxCb1fAkASvuTv
847
qfpH87Qq5kQhNKdbbwbmd2NxlNabazPijWuphGTdW0VfJdWfklyS2Kr+iqrs/5wV
848
HhathJt636Eg7oIjAkA8ht3MQ+XSl9yIJIS8gVpbPxSw5OMfw0PjVE7tBdQruiSc
849
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
850
-----END RSA PRIVATE KEY-----
854
class SingleListener(threading.Thread):
856
def __init__(self, callback):
857
threading.Thread.__init__(self)
858
self._callback = callback
859
self._socket = socket.socket()
860
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
861
self._socket.bind(('localhost', 0))
862
self._socket.listen(1)
863
self.port = self._socket.getsockname()[1]
864
self.stop_event = threading.Event()
867
s, _ = self._socket.accept()
868
# now close the listen socket
870
self._callback(s, self.stop_event)
873
self.stop_event.set()
874
# We should consider waiting for the other thread
875
# to stop, because otherwise we get spurious
876
# bzr: ERROR: Socket exception: Connection reset by peer (54)
877
# because the test suite finishes before the thread has a chance
878
# to close. (Especially when only running a few tests)
881
class SFTPServer(Server):
882
"""Common code for SFTP server facilities."""
884
def _get_sftp_url(self, path):
885
"""Calculate a sftp url to this server for path."""
886
return 'sftp://foo:bar@localhost:%d/%s' % (self._listener.port, path)
889
self._original_vendor = None
891
self._server_homedir = None
892
self._listener = None
897
def log(self, message):
898
"""What to do here? do we need this? Its for the StubServer.."""
899
self.logs.append(message)
901
def _run_server(self, s, stop_event):
902
ssh_server = paramiko.Transport(s)
903
key_file = os.path.join(self._homedir, 'test_rsa.key')
904
file(key_file, 'w').write(STUB_SERVER_KEY)
905
host_key = paramiko.RSAKey.from_private_key_file(key_file)
906
ssh_server.add_server_key(host_key)
907
server = StubServer(self)
908
ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer,
909
StubSFTPServer, root=self._root,
910
home=self._server_homedir)
911
event = threading.Event()
912
ssh_server.start_server(event, server)
914
stop_event.wait(30.0)
917
"""See bzrlib.transport.Server.setUp."""
918
# XXX: 20051124 jamesh
919
# The tests currently pop up a password prompt when an external ssh
920
# is used. This forces the use of the paramiko implementation.
922
self._original_vendor = _ssh_vendor
924
self._homedir = os.getcwdu()
925
if self._server_homedir is None:
926
self._server_homedir = self._homedir
928
# FIXME WINDOWS: _root should be _server_homedir[0]:/
929
self._listener = SingleListener(self._run_server)
930
self._listener.setDaemon(True)
931
self._listener.start()
934
"""See bzrlib.transport.Server.tearDown."""
936
self._listener.stop()
937
_ssh_vendor = self._original_vendor
940
class SFTPAbsoluteServer(SFTPServer):
941
"""A test server for sftp transports, using absolute urls."""
944
"""See bzrlib.transport.Server.get_url."""
945
return self._get_sftp_url("%%2f%s" %
946
urlescape(self._homedir[1:]))
949
class SFTPHomeDirServer(SFTPServer):
950
"""A test server for sftp transports, using homedir relative urls."""
953
"""See bzrlib.transport.Server.get_url."""
954
return self._get_sftp_url("")
957
class SFTPSiblingAbsoluteServer(SFTPAbsoluteServer):
958
"""A test servere for sftp transports, using absolute urls to non-home."""
961
self._server_homedir = '/dev/noone/runs/tests/here'
962
super(SFTPSiblingAbsoluteServer, self).setUp()
965
def get_test_permutations():
966
"""Return the permutations to be used in testing."""
967
return [(SFTPTransport, SFTPAbsoluteServer),
968
(SFTPTransport, SFTPHomeDirServer),
969
(SFTPTransport, SFTPSiblingAbsoluteServer),