def test_tunnel_sock_failure(self): remote_host = '127.0.0.59' server = OpenSSHServer(listen_ip=remote_host, port=self.port) server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() channel = tunnel._open_channel_retries(self.proxy_host, self.port, 2150) self.assertFalse(channel.eof()) listen_socket, listen_port = tunnel._init_tunnel_sock() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('127.0.0.1', listen_port)) client_socket.send(b'blah\n') client_socket.close() gl1 = spawn(tunnel._read_channel, client_socket, channel) gl2 = spawn(tunnel._read_forward_sock, client_socket, channel) sleep(1) gl1.kill() gl2.kill() tunnel._sockets.append(None) tunnel.cleanup() finally: server.stop()
def test_tunnel_init(self): proxy_host = '127.0.0.49' server = OpenSSHServer(listen_ip=proxy_host, port=self.port) server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() consume_let = spawn(tunnel._consume_q) in_q.append((self.host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) tunnel.cleanup() for _sock in tunnel._sockets: self.assertTrue(_sock.closed) finally: server.stop()
def test_tunnel_channel_eof(self): remote_host = '127.0.0.59' server = OpenSSHServer(listen_ip=remote_host, port=self.port) server.start_server() in_q, out_q = deque(), deque() tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() channel = tunnel._open_channel_retries(self.proxy_host, self.port, 2150) self.assertFalse(channel.eof()) channel.close() listen_socket, listen_port = tunnel._init_tunnel_sock() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('127.0.0.1', listen_port)) try: client = spawn(self._connect_client, client_socket) tunnel._read_channel(client_socket, channel) tunnel._read_forward_sock(client_socket, channel) self.assertTrue(channel.eof()) client.kill() finally: client_socket.close() tunnel.cleanup() server.stop()
def test_tunnel_channel_failure(self): remote_host = '127.0.0.8' remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) remote_server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel.daemon = True tunnel.start() in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = SSHClient( '127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) tunnel.cleanup() spawn(proxy_client.execute, 'echo me') proxy_client.disconnect() self.assertTrue(proxy_client.sock.closed) finally: remote_server.stop()
def test_tunnel_sock_failure(self): remote_host = '127.0.0.59' server = OpenSSHServer(listen_ip=remote_host, port=self.port) server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() channel = tunnel._open_channel_retries(self.proxy_host, self.port, 2150) self.assertFalse(channel.eof()) listen_socket, listen_port = tunnel._init_tunnel_sock() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('127.0.0.1', listen_port)) client_socket.send(b'blah\n') client_socket.close() gl1 = spawn(tunnel._read_channel, client_socket, channel) gl2 = spawn(tunnel._read_forward_sock, client_socket, channel) sleep(1) gl1.kill() gl2.kill() tunnel._sockets.append(None) tunnel.cleanup() finally: server.stop()
def test_tunnel_server_failure(self): proxy_host = '127.0.0.9' remote_host = '127.0.0.8' server = OpenSSHServer(listen_ip=proxy_host, port=self.port) remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) for _server in (server, remote_server): _server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() consume_let = spawn(tunnel._consume_q) in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = spawn(SSHClient, '127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) remote_server.stop() tunnel.cleanup() self.assertRaises(ConnectionErrorException, proxy_client.get) finally: for _server in (server, remote_server): _server.stop()
def test_tunnel_channel_failure(self): remote_host = '127.0.0.8' remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) remote_server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel.daemon = True tunnel.start() in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = SSHClient('127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) tunnel.cleanup() spawn(proxy_client.execute, 'echo me') proxy_client.disconnect() self.assertTrue(proxy_client.sock.closed) finally: remote_server.stop()
def test_tunnel_init(self): proxy_host = '127.0.0.49' server = OpenSSHServer(listen_ip=proxy_host, port=self.port) server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() consume_let = spawn(tunnel._consume_q) in_q.append((self.host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) tunnel.cleanup() for _sock in tunnel._sockets: self.assertTrue(_sock.closed) finally: server.stop()
def test_tunnel_server_failure(self): proxy_host = '127.0.0.9' remote_host = '127.0.0.8' server = OpenSSHServer(listen_ip=proxy_host, port=self.port) remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) for _server in (server, remote_server): _server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel._init_tunnel_client() consume_let = spawn(tunnel._consume_q) in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = spawn( SSHClient, '127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) remote_server.stop() tunnel.cleanup() self.assertRaises(ConnectionErrorException, proxy_client.get) finally: for _server in (server, remote_server): _server.stop()