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.
 
 

126 lines
4.5 KiB

  1. import unittest
  2. import asyncio
  3. import aiosocks
  4. import aiohttp
  5. from unittest import mock
  6. from asyncio import coroutine
  7. from aiohttp.client_reqrep import ClientRequest
  8. from aiosocks.connector import SocksConnector
  9. class TestSocksConnector(unittest.TestCase):
  10. def setUp(self):
  11. self.loop = asyncio.new_event_loop()
  12. asyncio.set_event_loop(None)
  13. def tearDown(self):
  14. self.loop.close()
  15. def _fake_coroutine(self, return_value):
  16. def coro(*args, **kwargs):
  17. if isinstance(return_value, Exception):
  18. raise return_value
  19. return return_value
  20. return mock.Mock(side_effect=coroutine(coro))
  21. def test_connect_proxy_ip(self):
  22. loop_mock = mock.Mock()
  23. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  24. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  25. None, loop=loop_mock)
  26. loop_mock.getaddrinfo = self._fake_coroutine([mock.MagicMock()])
  27. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  28. proto.negotiate_done = self._fake_coroutine(True)
  29. loop_mock.create_connection = self._fake_coroutine((tr, proto))
  30. conn = self.loop.run_until_complete(connector.connect(req))
  31. self.assertTrue(loop_mock.getaddrinfo.is_called)
  32. self.assertIs(conn._transport, tr)
  33. self.assertTrue(
  34. isinstance(conn._protocol, aiohttp.parsers.StreamProtocol)
  35. )
  36. conn.close()
  37. def test_connect_proxy_domain(self):
  38. loop_mock = mock.Mock()
  39. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  40. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  41. None, loop=loop_mock)
  42. connector._resolve_host = self._fake_coroutine([mock.MagicMock()])
  43. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  44. proto.negotiate_done = self._fake_coroutine(True)
  45. loop_mock.create_connection = self._fake_coroutine((tr, proto))
  46. conn = self.loop.run_until_complete(connector.connect(req))
  47. self.assertTrue(connector._resolve_host.is_called)
  48. self.assertEqual(connector._resolve_host.call_count, 1)
  49. self.assertIs(conn._transport, tr)
  50. self.assertTrue(
  51. isinstance(conn._protocol, aiohttp.parsers.StreamProtocol)
  52. )
  53. conn.close()
  54. def test_connect_locale_resolve(self):
  55. loop_mock = mock.Mock()
  56. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  57. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  58. None, loop=loop_mock, remote_resolve=False)
  59. connector._resolve_host = self._fake_coroutine([mock.MagicMock()])
  60. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  61. proto.negotiate_done = self._fake_coroutine(True)
  62. loop_mock.create_connection = self._fake_coroutine((tr, proto))
  63. conn = self.loop.run_until_complete(connector.connect(req))
  64. self.assertTrue(connector._resolve_host.is_called)
  65. self.assertEqual(connector._resolve_host.call_count, 2)
  66. self.assertIs(conn._transport, tr)
  67. self.assertTrue(
  68. isinstance(conn._protocol, aiohttp.parsers.StreamProtocol)
  69. )
  70. conn.close()
  71. def test_proxy_connect_fail(self):
  72. loop_mock = mock.Mock()
  73. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  74. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  75. None, loop=loop_mock)
  76. loop_mock.getaddrinfo = self._fake_coroutine([mock.MagicMock()])
  77. loop_mock.create_connection = self._fake_coroutine(OSError())
  78. with self.assertRaises(aiohttp.ProxyConnectionError):
  79. self.loop.run_until_complete(connector.connect(req))
  80. def test_proxy_negotiate_fail(self):
  81. loop_mock = mock.Mock()
  82. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  83. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  84. None, loop=loop_mock)
  85. loop_mock.getaddrinfo = self._fake_coroutine([mock.MagicMock()])
  86. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  87. proto.negotiate_done = self._fake_coroutine(aiosocks.SocksError())
  88. loop_mock.create_connection = self._fake_coroutine((tr, proto))
  89. with self.assertRaises(aiosocks.SocksError):
  90. self.loop.run_until_complete(connector.connect(req))