def test_init(self): addr = aiosocks.Socks5Addr('localhost') auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) # proxy argument with self.assertRaises(AssertionError) as ct: conn = aiosocks.create_connection(None, None, auth, dst) self.loop.run_until_complete(conn) self.assertEqual(str(ct.exception), 'proxy must be Socks4Addr() or Socks5Addr() tuple') with self.assertRaises(AssertionError) as ct: conn = aiosocks.create_connection(None, auth, auth, dst) self.loop.run_until_complete(conn) self.assertEqual(str(ct.exception), 'proxy must be Socks4Addr() or Socks5Addr() tuple') # proxy_auth with self.assertRaises(AssertionError) as ct: conn = aiosocks.create_connection(None, addr, addr, dst) self.loop.run_until_complete(conn) self.assertIn('proxy_auth must be None or Socks4Auth()', str(ct.exception)) # dst with self.assertRaises(AssertionError) as ct: conn = aiosocks.create_connection(None, addr, auth, None) self.loop.run_until_complete(conn) self.assertIn('invalid dst format, tuple("dst_host", dst_port))', str(ct.exception)) # addr and auth compatibility with self.assertRaises(ValueError) as ct: conn = aiosocks.create_connection(None, addr, aiosocks.Socks4Auth(''), dst) self.loop.run_until_complete(conn) self.assertIn('proxy is Socks5Addr but proxy_auth is not Socks5Auth', str(ct.exception)) with self.assertRaises(ValueError) as ct: conn = aiosocks.create_connection(None, aiosocks.Socks4Addr(''), auth, dst) self.loop.run_until_complete(conn) self.assertIn('proxy is Socks4Addr but proxy_auth is not Socks4Auth', str(ct.exception)) # test ssl, server_hostname with self.assertRaises(ValueError) as ct: conn = aiosocks.create_connection(None, addr, auth, dst, server_hostname='python.org') self.loop.run_until_complete(conn) self.assertIn('server_hostname is only meaningful with ssl', str(ct.exception))
def test_auth_failed(self): with fake_socks_srv(self.loop, b'\x05\x02\x01\x01') as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection( None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('authentication failed', str(ct.exception))
def test_socks_srv_error(self): with fake_socks_srv(self.loop, b'\x00\x5b\x04W\x01\x01\x01\x01') as port: addr = aiosocks.Socks4Addr('127.0.0.1', port) auth = aiosocks.Socks4Auth('usr') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection( None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('0x5b', str(ct.exception))
def test_cmd_not_granted(self): with fake_socks_srv(self.loop, b'\x05\x02\x01\x00\x05\x01\x00') as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection( None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('General SOCKS server failure', str(ct.exception))
def test_auth_failed(self): with fake_socks_srv(self.loop, b'\x05\x02\x01\x01') as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection(None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('authentication failed', str(ct.exception))
def test_connection_fail(self): addr = aiosocks.Socks5Addr('localhost') auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) loop_mock = mock.Mock() loop_mock.create_connection = fake_coroutine(OSError()) with self.assertRaises(aiosocks.SocksConnectionError): conn = aiosocks.create_connection(None, addr, auth, dst, loop=loop_mock) self.loop.run_until_complete(conn)
def test_socks_srv_error(self): with fake_socks_srv(self.loop, b'\x00\x5b\x04W\x01\x01\x01\x01') as port: addr = aiosocks.Socks4Addr('127.0.0.1', port) auth = aiosocks.Socks4Auth('usr') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection(None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('0x5b', str(ct.exception))
def test_cmd_not_granted(self): with fake_socks_srv(self.loop, b'\x05\x02\x01\x00\x05\x01\x00') as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) with self.assertRaises(aiosocks.SocksError) as ct: coro = aiosocks.create_connection(None, addr, auth, dst, loop=self.loop) self.loop.run_until_complete(coro) self.assertIn('General SOCKS server failure', str(ct.exception))
def test_atype_domain(self): with fake_socks_srv( self.loop, b'\x05\x02\x01\x00\x05\x00\x00\x03\x0apython.org\x04W' ) as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) coro = aiosocks.create_connection( None, addr, auth, dst, loop=self.loop) transport, protocol = self.loop.run_until_complete(coro) self.assertEqual(protocol.proxy_sockname, (b'python.org', 1111)) transport.close()
def test_connect_success(self): with fake_socks_srv(self.loop, b'\x00\x5a\x04W\x01\x01\x01\x01test') as port: addr = aiosocks.Socks4Addr('127.0.0.1', port) auth = aiosocks.Socks4Auth('usr') dst = ('python.org', 80) coro = aiosocks.create_connection( None, addr, auth, dst, loop=self.loop) transport, protocol = self.loop.run_until_complete(coro) self.assertEqual(protocol.proxy_sockname, ('1.1.1.1', 1111)) data = self.loop.run_until_complete( protocol._stream_reader.read(4)) self.assertEqual(data, b'test') transport.close()
def test_negotiate_fail(self, future_mock): addr = aiosocks.Socks5Addr('localhost') auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) loop_mock = mock.Mock() loop_mock.create_connection = fake_coroutine( (mock.Mock(), mock.Mock())) fut = fake_coroutine(aiosocks.SocksError()) future_mock.side_effect = fut.side_effect with self.assertRaises(aiosocks.SocksError): conn = aiosocks.create_connection(None, addr, auth, dst, loop=loop_mock) self.loop.run_until_complete(conn)
def test_atype_domain(self): with fake_socks_srv( self.loop, b'\x05\x02\x01\x00\x05\x00\x00\x03\x0apython.org\x04W' ) as port: addr = aiosocks.Socks5Addr('127.0.0.1', port) auth = aiosocks.Socks5Auth('usr', 'pwd') dst = ('python.org', 80) coro = aiosocks.create_connection(None, addr, auth, dst, loop=self.loop) transport, protocol = self.loop.run_until_complete(coro) self.assertEqual(protocol.proxy_sockname, (b'python.org', 1111)) transport.close()
def test_connect_success(self): with fake_socks_srv(self.loop, b'\x00\x5a\x04W\x01\x01\x01\x01test') as port: addr = aiosocks.Socks4Addr('127.0.0.1', port) auth = aiosocks.Socks4Auth('usr') dst = ('python.org', 80) coro = aiosocks.create_connection(None, addr, auth, dst, loop=self.loop) transport, protocol = self.loop.run_until_complete(coro) self.assertEqual(protocol.proxy_sockname, ('1.1.1.1', 1111)) data = self.loop.run_until_complete( protocol._stream_reader.read(4)) self.assertEqual(data, b'test') transport.close()