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.
 
 

135 lines
4.7 KiB

  1. import unittest
  2. import aiosocks
  3. import asyncio
  4. from unittest import mock
  5. from .helpers import fake_coroutine
  6. try:
  7. from asyncio import ensure_future
  8. except ImportError:
  9. ensure_future = asyncio.async
  10. class TestCreateConnection(unittest.TestCase):
  11. def setUp(self):
  12. self.loop = asyncio.new_event_loop()
  13. asyncio.set_event_loop(None)
  14. def tearDown(self):
  15. self.loop.close()
  16. def test_init(self):
  17. addr = aiosocks.Socks5Addr('localhost')
  18. auth = aiosocks.Socks5Auth('usr', 'pwd')
  19. dst = ('python.org', 80)
  20. # proxy argument
  21. with self.assertRaises(AssertionError) as ct:
  22. conn = aiosocks.create_connection(None, None, auth, dst)
  23. self.loop.run_until_complete(conn)
  24. self.assertEqual(str(ct.exception),
  25. 'proxy must be Socks4Addr() or Socks5Addr() tuple')
  26. with self.assertRaises(AssertionError) as ct:
  27. conn = aiosocks.create_connection(None, auth, auth, dst)
  28. self.loop.run_until_complete(conn)
  29. self.assertEqual(str(ct.exception),
  30. 'proxy must be Socks4Addr() or Socks5Addr() tuple')
  31. # proxy_auth
  32. with self.assertRaises(AssertionError) as ct:
  33. conn = aiosocks.create_connection(None, addr, addr, dst)
  34. self.loop.run_until_complete(conn)
  35. self.assertIn('proxy_auth must be None or Socks4Auth()',
  36. str(ct.exception))
  37. # dst
  38. with self.assertRaises(AssertionError) as ct:
  39. conn = aiosocks.create_connection(None, addr, auth, None)
  40. self.loop.run_until_complete(conn)
  41. self.assertIn('invalid dst format, tuple("dst_host", dst_port))',
  42. str(ct.exception))
  43. # addr and auth compatibility
  44. with self.assertRaises(ValueError) as ct:
  45. conn = aiosocks.create_connection(
  46. None, addr, aiosocks.Socks4Auth(''), dst
  47. )
  48. self.loop.run_until_complete(conn)
  49. self.assertIn('proxy is Socks5Addr but proxy_auth is not Socks5Auth',
  50. str(ct.exception))
  51. with self.assertRaises(ValueError) as ct:
  52. conn = aiosocks.create_connection(
  53. None, aiosocks.Socks4Addr(''), auth, dst
  54. )
  55. self.loop.run_until_complete(conn)
  56. self.assertIn('proxy is Socks4Addr but proxy_auth is not Socks4Auth',
  57. str(ct.exception))
  58. # test ssl, server_hostname
  59. with self.assertRaises(ValueError) as ct:
  60. conn = aiosocks.create_connection(
  61. None, addr, auth, dst, server_hostname='python.org'
  62. )
  63. self.loop.run_until_complete(conn)
  64. self.assertIn('server_hostname is only meaningful with ssl',
  65. str(ct.exception))
  66. def test_connection_fail(self):
  67. addr = aiosocks.Socks5Addr('localhost')
  68. auth = aiosocks.Socks5Auth('usr', 'pwd')
  69. dst = ('python.org', 80)
  70. loop_mock = mock.Mock()
  71. loop_mock.create_connection = fake_coroutine(OSError())
  72. with self.assertRaises(aiosocks.SocksConnectionError):
  73. conn = aiosocks.create_connection(
  74. None, addr, auth, dst, loop=loop_mock
  75. )
  76. self.loop.run_until_complete(conn)
  77. @mock.patch('aiosocks.asyncio.Future')
  78. def test_negotiate_fail(self, future_mock):
  79. addr = aiosocks.Socks5Addr('localhost')
  80. auth = aiosocks.Socks5Auth('usr', 'pwd')
  81. dst = ('python.org', 80)
  82. loop_mock = mock.Mock()
  83. loop_mock.create_connection = fake_coroutine(
  84. (mock.Mock(), mock.Mock())
  85. )
  86. fut = fake_coroutine(aiosocks.SocksError())
  87. future_mock.side_effect = fut.side_effect
  88. with self.assertRaises(aiosocks.SocksError):
  89. conn = aiosocks.create_connection(
  90. None, addr, auth, dst, loop=loop_mock
  91. )
  92. self.loop.run_until_complete(conn)
  93. @mock.patch('aiosocks.asyncio.Future')
  94. def test_open_connection(self, future_mock):
  95. addr = aiosocks.Socks5Addr('localhost')
  96. auth = aiosocks.Socks5Auth('usr', 'pwd')
  97. dst = ('python.org', 80)
  98. transp, proto = mock.Mock(), mock.Mock()
  99. reader, writer = mock.Mock(), mock.Mock()
  100. proto.app_protocol.reader, proto.app_protocol.writer = reader, writer
  101. loop_mock = mock.Mock()
  102. loop_mock.create_connection = fake_coroutine((transp, proto))
  103. fut = fake_coroutine(True)
  104. future_mock.side_effect = fut.side_effect
  105. conn = aiosocks.open_connection(addr, auth, dst, loop=loop_mock)
  106. r, w = self.loop.run_until_complete(conn)
  107. self.assertIs(reader, r)
  108. self.assertIs(writer, w)