An attempt at adding UDP support to aiosocks. Untested due to lack of server support.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
3.3 KiB

  1. import pytest
  2. import aiosocks
  3. from aiohttp.test_utils import make_mocked_coro
  4. from unittest import mock
  5. async def test_create_connection_init():
  6. addr = aiosocks.Socks5Addr('localhost')
  7. auth = aiosocks.Socks5Auth('usr', 'pwd')
  8. dst = ('python.org', 80)
  9. # proxy argument
  10. with pytest.raises(AssertionError) as ct:
  11. await aiosocks.create_connection(None, None, auth, dst)
  12. assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct)
  13. with pytest.raises(AssertionError) as ct:
  14. await aiosocks.create_connection(None, auth, auth, dst)
  15. assert 'proxy must be Socks4Addr() or Socks5Addr() tuple' in str(ct)
  16. # proxy_auth
  17. with pytest.raises(AssertionError) as ct:
  18. await aiosocks.create_connection(None, addr, addr, dst)
  19. assert 'proxy_auth must be None or Socks4Auth()' in str(ct)
  20. # dst
  21. with pytest.raises(AssertionError) as ct:
  22. await aiosocks.create_connection(None, addr, auth, None)
  23. assert 'invalid dst format, tuple("dst_host", dst_port))' in str(ct)
  24. # addr and auth compatibility
  25. with pytest.raises(ValueError) as ct:
  26. await aiosocks.create_connection(
  27. None, addr, aiosocks.Socks4Auth(''), dst)
  28. assert 'proxy is Socks5Addr but proxy_auth is not Socks5Auth' in str(ct)
  29. with pytest.raises(ValueError) as ct:
  30. await aiosocks.create_connection(
  31. None, aiosocks.Socks4Addr(''), auth, dst)
  32. assert 'proxy is Socks4Addr but proxy_auth is not Socks4Auth' in str(ct)
  33. # test ssl, server_hostname
  34. with pytest.raises(ValueError) as ct:
  35. await aiosocks.create_connection(
  36. None, addr, auth, dst, server_hostname='python.org')
  37. assert 'server_hostname is only meaningful with ssl' in str(ct)
  38. async def test_connection_fail():
  39. addr = aiosocks.Socks5Addr('localhost')
  40. auth = aiosocks.Socks5Auth('usr', 'pwd')
  41. dst = ('python.org', 80)
  42. loop_mock = mock.Mock()
  43. loop_mock.create_connection = make_mocked_coro(raise_exception=OSError())
  44. with pytest.raises(aiosocks.SocksConnectionError):
  45. await aiosocks.create_connection(
  46. None, addr, auth, dst, loop=loop_mock)
  47. async def test_negotiate_fail():
  48. addr = aiosocks.Socks5Addr('localhost')
  49. auth = aiosocks.Socks5Auth('usr', 'pwd')
  50. dst = ('python.org', 80)
  51. loop_mock = mock.Mock()
  52. loop_mock.create_connection = make_mocked_coro((mock.Mock(), mock.Mock()))
  53. with mock.patch('aiosocks.asyncio.Future') as future_mock:
  54. future_mock.side_effect = make_mocked_coro(
  55. raise_exception=aiosocks.SocksError())
  56. with pytest.raises(aiosocks.SocksError):
  57. await aiosocks.create_connection(
  58. None, addr, auth, dst, loop=loop_mock)
  59. async def test_open_connection():
  60. addr = aiosocks.Socks5Addr('localhost')
  61. auth = aiosocks.Socks5Auth('usr', 'pwd')
  62. dst = ('python.org', 80)
  63. transp, proto = mock.Mock(), mock.Mock()
  64. reader, writer = mock.Mock(), mock.Mock()
  65. proto.app_protocol.reader, proto.app_protocol.writer = reader, writer
  66. loop_mock = mock.Mock()
  67. loop_mock.create_connection = make_mocked_coro((transp, proto))
  68. with mock.patch('aiosocks.asyncio.Future') as future_mock:
  69. future_mock.side_effect = make_mocked_coro(True)
  70. r, w = await aiosocks.open_connection(addr, auth, dst, loop=loop_mock)
  71. assert reader is r
  72. assert writer is w