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.
 
 

134 lines
5.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. def test_properties(self):
  16. addr = aiosocks.Socks4Addr('localhost')
  17. auth = aiosocks.Socks4Auth('login')
  18. conn = SocksConnector(addr, auth, loop=self.loop)
  19. self.assertIs(conn.proxy, addr)
  20. self.assertIs(conn.proxy_auth, auth)
  21. @mock.patch('aiosocks.connector.create_connection')
  22. def test_connect_proxy_ip(self, cr_conn_mock):
  23. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  24. cr_conn_mock.side_effect = \
  25. fake_coroutine((tr, proto)).side_effect
  26. loop_mock = mock.Mock()
  27. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  28. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  29. None, loop=loop_mock)
  30. loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
  31. conn = self.loop.run_until_complete(connector.connect(req))
  32. self.assertTrue(loop_mock.getaddrinfo.is_called)
  33. self.assertIs(conn._transport, tr)
  34. conn.close()
  35. @mock.patch('aiosocks.connector.create_connection')
  36. def test_connect_proxy_domain(self, cr_conn_mock):
  37. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  38. cr_conn_mock.side_effect = \
  39. fake_coroutine((tr, proto)).side_effect
  40. loop_mock = mock.Mock()
  41. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  42. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  43. None, loop=loop_mock)
  44. connector._resolve_host = fake_coroutine([mock.MagicMock()])
  45. conn = self.loop.run_until_complete(connector.connect(req))
  46. self.assertTrue(connector._resolve_host.is_called)
  47. self.assertEqual(connector._resolve_host.call_count, 1)
  48. self.assertIs(conn._transport, tr)
  49. conn.close()
  50. @mock.patch('aiosocks.connector.create_connection')
  51. def test_connect_remote_resolve(self, cr_conn_mock):
  52. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  53. cr_conn_mock.side_effect = \
  54. fake_coroutine((tr, proto)).side_effect
  55. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  56. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  57. None, loop=self.loop, remote_resolve=True)
  58. connector._resolve_host = fake_coroutine([mock.MagicMock()])
  59. conn = self.loop.run_until_complete(connector.connect(req))
  60. self.assertEqual(connector._resolve_host.call_count, 0)
  61. conn.close()
  62. @mock.patch('aiosocks.connector.create_connection')
  63. def test_connect_locale_resolve(self, cr_conn_mock):
  64. tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
  65. cr_conn_mock.side_effect = \
  66. fake_coroutine((tr, proto)).side_effect
  67. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  68. connector = SocksConnector(aiosocks.Socks5Addr('proxy.example'),
  69. None, loop=self.loop, remote_resolve=False)
  70. connector._resolve_host = fake_coroutine([mock.MagicMock()])
  71. conn = self.loop.run_until_complete(connector.connect(req))
  72. self.assertTrue(connector._resolve_host.is_called)
  73. self.assertEqual(connector._resolve_host.call_count, 2)
  74. conn.close()
  75. @mock.patch('aiosocks.connector.create_connection')
  76. def test_proxy_connect_fail(self, cr_conn_mock):
  77. loop_mock = mock.Mock()
  78. cr_conn_mock.side_effect = \
  79. fake_coroutine(aiosocks.SocksConnectionError()).side_effect
  80. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  81. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  82. None, loop=loop_mock)
  83. loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
  84. with self.assertRaises(aiohttp.ProxyConnectionError):
  85. self.loop.run_until_complete(connector.connect(req))
  86. @mock.patch('aiosocks.connector.create_connection')
  87. def test_proxy_negotiate_fail(self, cr_conn_mock):
  88. loop_mock = mock.Mock()
  89. cr_conn_mock.side_effect = \
  90. fake_coroutine(aiosocks.SocksError()).side_effect
  91. req = ClientRequest('GET', 'http://python.org', loop=self.loop)
  92. connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'),
  93. None, loop=loop_mock)
  94. loop_mock.getaddrinfo = fake_coroutine([mock.MagicMock()])
  95. with self.assertRaises(aiosocks.SocksError):
  96. self.loop.run_until_complete(connector.connect(req))