From e3455c7779d33e0a463b08bc00b005cef3a178c2 Mon Sep 17 00:00:00 2001 From: nibrag Date: Tue, 17 May 2016 14:35:24 +0300 Subject: [PATCH] Added test cases for SocksConnector --- tests/test_connector.py | 25 ++++++++++++++++++++++ tests/test_functional.py | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/test_connector.py b/tests/test_connector.py index e58c6fd..b0afd33 100644 --- a/tests/test_connector.py +++ b/tests/test_connector.py @@ -16,6 +16,13 @@ class TestSocksConnector(unittest.TestCase): def tearDown(self): self.loop.close() + def test_properties(self): + addr = aiosocks.Socks4Addr('localhost') + auth = aiosocks.Socks4Auth('login') + conn = SocksConnector(addr, auth, loop=self.loop) + self.assertIs(conn.proxy, addr) + self.assertIs(conn.proxy_auth, auth) + @mock.patch('aiosocks.connector.create_connection') def test_connect_proxy_ip(self, cr_conn_mock): tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol') @@ -58,6 +65,24 @@ class TestSocksConnector(unittest.TestCase): conn.close() + @mock.patch('aiosocks.connector.create_connection') + def test_connect_remote_resolve(self, cr_conn_mock): + tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol') + cr_conn_mock.side_effect = \ + fake_coroutine((tr, proto)).side_effect + + req = ClientRequest('GET', 'http://python.org', loop=self.loop) + connector = SocksConnector(aiosocks.Socks5Addr('127.0.0.1'), + None, loop=self.loop, remote_resolve=True) + + connector._resolve_host = fake_coroutine([mock.MagicMock()]) + + conn = self.loop.run_until_complete(connector.connect(req)) + + self.assertEqual(connector._resolve_host.call_count, 0) + + conn.close() + @mock.patch('aiosocks.connector.create_connection') def test_connect_locale_resolve(self, cr_conn_mock): tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol') diff --git a/tests/test_functional.py b/tests/test_functional.py index f91f0e2..277f1ae 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -320,3 +320,49 @@ class TestSocksConnector(unittest.TestCase): self.assertEqual(content, 'Test message') resp.close() + + def test_fingerprint_success(self): + with fake_socks4_srv(self.loop) as proxy_port: + addr = aiosocks.Socks4Addr('127.0.0.1', proxy_port) + fp = (b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9' + b'\x1a\xe3\xc5\x7f\x89\xe7l\xf9') + + conn = SocksConnector(proxy=addr, proxy_auth=None, loop=self.loop, + remote_resolve=False, verify_ssl=False, + fingerprint=fp) + + with http_srv(self.loop, use_ssl=True) as url: + with aiohttp.ClientSession(connector=conn, + loop=self.loop) as ses: + @asyncio.coroutine + def make_req(): + return (yield from ses.request('get', url=url)) + + resp = self.loop.run_until_complete(make_req()) + + self.assertEqual(resp.status, 200) + + content = self.loop.run_until_complete(resp.text()) + self.assertEqual(content, 'Test message') + + resp.close() + + def test_fingerprint_fail(self): + with fake_socks4_srv(self.loop) as proxy_port: + addr = aiosocks.Socks4Addr('127.0.0.1', proxy_port) + fp = (b's\x93\xfd:\xed\x08\x1do\xa9\xaeq9' + b'\x1a\xe3\xc5\x7f\x89\xe7l\x10') + + conn = SocksConnector(proxy=addr, proxy_auth=None, loop=self.loop, + remote_resolve=False, verify_ssl=False, + fingerprint=fp) + + with http_srv(self.loop, use_ssl=True) as url: + with aiohttp.ClientSession(connector=conn, + loop=self.loop) as ses: + @asyncio.coroutine + def make_req(): + return (yield from ses.request('get', url=url)) + + with self.assertRaises(aiohttp.FingerprintMismatch): + self.loop.run_until_complete(make_req())