887
889
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
888
890
-----END RSA PRIVATE KEY-----
892
class SingleListener(threading.Thread):
894
class SocketListener(threading.Thread):
894
896
def __init__(self, callback):
895
897
threading.Thread.__init__(self)
899
901
self._socket.bind(('localhost', 0))
900
902
self._socket.listen(1)
901
903
self.port = self._socket.getsockname()[1]
902
self.stop_event = threading.Event()
905
s, _ = self._socket.accept()
906
# now close the listen socket
909
self._callback(s, self.stop_event)
911
pass #Ignore socket errors
913
# probably a failed test
914
warning('Exception from within unit test server thread: %r' % x)
904
self._stop_event = threading.Event()
917
self.stop_event.set()
907
# called from outside this thread
908
self._stop_event.set()
918
909
# use a timeout here, because if the test fails, the server thread may
919
910
# never notice the stop_event.
916
readable, writable_unused, exception_unused = \
917
select.select([self._socket], [], [], 0.1)
918
if self._stop_event.isSet():
920
if len(readable) == 0:
923
s, addr_unused = self._socket.accept()
924
# because the loopback socket is inline, and transports are
925
# never explicitly closed, best to launch a new thread.
926
threading.Thread(target=self._callback, args=(s,)).start()
927
except socket.error, x:
928
sys.excepthook(*sys.exc_info())
929
warning('Socket error during accept() within unit test server'
932
# probably a failed test; unit test thread will log the
934
sys.excepthook(*sys.exc_info())
935
warning('Exception from within unit test server thread: %r' %
923
939
class SFTPServer(Server):
941
957
"""StubServer uses this to log when a new server is created."""
942
958
self.logs.append(message)
944
def _run_server(self, s, stop_event):
960
def _run_server(self, s):
945
961
ssh_server = paramiko.Transport(s)
946
962
key_file = os.path.join(self._homedir, 'test_rsa.key')
947
file(key_file, 'w').write(STUB_SERVER_KEY)
963
f = open(key_file, 'w')
964
f.write(STUB_SERVER_KEY)
948
966
host_key = paramiko.RSAKey.from_private_key_file(key_file)
949
967
ssh_server.add_server_key(host_key)
950
968
server = StubServer(self)
965
982
self._server_homedir = self._homedir
967
984
# FIXME WINDOWS: _root should be _server_homedir[0]:/
968
self._listener = SingleListener(self._run_server)
985
self._listener = SocketListener(self._run_server)
969
986
self._listener.setDaemon(True)
970
987
self._listener.start()
998
1015
super(SFTPServerWithoutSSH, self).__init__()
999
1016
self._vendor = 'loopback'
1001
def _run_server(self, sock, stop_event):
1018
def _run_server(self, sock):
1002
1019
class FakeChannel(object):
1003
1020
def get_transport(self):