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.
 
 

109 lines
4.0 KiB

  1. import unittest
  2. import asyncio
  3. import aiosocks
  4. import aiohttp
  5. from unittest import mock
  6. from aiohttp.client_reqrep import ClientRequest
  7. from aiosocks.connector import SocksConnector
  8. from .helpers import fake_coroutine
  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. @mock.patch('aiosocks.connector.create_connection')
  16. def test_connect_proxy_ip(self, cr_conn_mock):
  17. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  18. cr_conn_mock.side_effect = \
  19. fake_coroutine((tr, proto)).side_effect
  20. loop_mock = mock.Mock()
  21. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  22. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  23. None, loop=loop_mock)
  24. loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
  25. conn = self.loop.run_until_complete(connector.connect(req))
  26. self.assertTrue(loop_mock.getaddrinfo.is_called)
  27. self.assertIs(conn._transport, tr)
  28. conn.close()
  29. @mock.patch('aiosocks.connector.create_connection')
  30. def test_connect_proxy_domain(self, cr_conn_mock):
  31. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  32. cr_conn_mock.side_effect = \
  33. fake_coroutine((tr, proto)).side_effect
  34. loop_mock = mock.Mock()
  35. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  36. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  37. None, loop=loop_mock)
  38. connector._resolve_host = fake_coroutine([mock.MagicMock()])
  39. conn = self.loop.run_until_complete(connector.connect(req))
  40. self.assertTrue(connector._resolve_host.is_called)
  41. self.assertEqual(connector._resolve_host.call_count, 1)
  42. self.assertIs(conn._transport, tr)
  43. conn.close()
  44. @mock.patch('aiosocks.connector.create_connection')
  45. def test_connect_locale_resolve(self, cr_conn_mock):
  46. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  47. cr_conn_mock.side_effect = \
  48. fake_coroutine((tr, proto)).side_effect
  49. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  50. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  51. None, loop=self.loop, remote_resolve=False)
  52. connector._resolve_host = fake_coroutine([mock.MagicMock()])
  53. conn = self.loop.run_until_complete(connector.connect(req))
  54. self.assertTrue(connector._resolve_host.is_called)
  55. self.assertEqual(connector._resolve_host.call_count, 2)
  56. conn.close()
  57. @mock.patch('aiosocks.connector.create_connection')
  58. def test_proxy_connect_fail(self, cr_conn_mock):
  59. loop_mock = mock.Mock()
  60. cr_conn_mock.side_effect = \
  61. fake_coroutine(aiosocks.SocksConnectionError()).side_effect
  62. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  63. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  64. None, loop=loop_mock)
  65. loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
  66. with self.assertRaises(aiohttp.ProxyConnectionError):
  67. self.loop.run_until_complete(connector.connect(req))
  68. @mock.patch('aiosocks.connector.create_connection')
  69. def test_proxy_negotiate_fail(self, cr_conn_mock):
  70. loop_mock = mock.Mock()
  71. cr_conn_mock.side_effect = \
  72. fake_coroutine(aiosocks.SocksError()).side_effect
  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 = fake_coroutine([mock.MagicMock()])
  77. with self.assertRaises(aiosocks.SocksError):
  78. self.loop.run_until_complete(connector.connect(req))