-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_connector.py
200 lines (151 loc) · 7.11 KB
/
test_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import ssl
import aiosocks
import aiohttp
import pytest
from yarl import URL
from unittest import mock
from aiohttp.test_utils import make_mocked_coro
from aiohttp import BasicAuth
from aiosocks.connector import ProxyConnector, ProxyClientRequest
from aiosocks.helpers import Socks4Auth, Socks5Auth
async def test_connect_proxy_ip(loop):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro((tr, proto))):
loop.getaddrinfo = make_mocked_coro(
[[0, 0, 0, 0, ['127.0.0.1', 1080]]])
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://proxy.org'))
connector = ProxyConnector(loop=loop)
conn = await connector.connect(req)
assert loop.getaddrinfo.called
assert conn.protocol is proto
conn.close()
async def test_connect_proxy_domain():
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro((tr, proto))):
loop_mock = mock.Mock()
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop_mock,
proxy=URL('socks5://proxy.example'))
connector = ProxyConnector(loop=loop_mock)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
conn = await connector.connect(req)
assert connector._resolve_host.call_count == 1
assert conn.protocol is proto
conn.close()
async def test_connect_remote_resolve(loop):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro((tr, proto))):
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://127.0.0.1'))
connector = ProxyConnector(loop=loop, remote_resolve=True)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
conn = await connector.connect(req)
assert connector._resolve_host.call_count == 1
assert conn.protocol is proto
conn.close()
async def test_connect_locale_resolve(loop):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro((tr, proto))):
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://proxy.example'))
connector = ProxyConnector(loop=loop, remote_resolve=False)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])
conn = await connector.connect(req)
assert connector._resolve_host.call_count == 2
assert conn.protocol is proto
conn.close()
@pytest.mark.parametrize('remote_resolve', [True, False])
async def test_resolve_host_fail(loop, remote_resolve):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro((tr, proto))):
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://proxy.example'))
connector = ProxyConnector(loop=loop, remote_resolve=remote_resolve)
connector._resolve_host = make_mocked_coro(raise_exception=OSError())
with pytest.raises(aiohttp.ClientConnectorError):
await connector.connect(req)
@pytest.mark.parametrize('exc', [
(ssl.CertificateError, aiohttp.ClientConnectorCertificateError),
(ssl.SSLError, aiohttp.ClientConnectorSSLError),
(aiosocks.SocksConnectionError, aiohttp.ClientProxyConnectionError)])
async def test_proxy_connect_fail(loop, exc):
loop_mock = mock.Mock()
loop_mock.getaddrinfo = make_mocked_coro(
[[0, 0, 0, 0, ['127.0.0.1', 1080]]])
cc_coro = make_mocked_coro(
raise_exception=exc[0]())
with mock.patch('aiosocks.connector.create_connection', cc_coro):
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://127.0.0.1'))
connector = ProxyConnector(loop=loop_mock)
with pytest.raises(exc[1]):
await connector.connect(req)
async def test_proxy_negotiate_fail(loop):
loop_mock = mock.Mock()
loop_mock.getaddrinfo = make_mocked_coro(
[[0, 0, 0, 0, ['127.0.0.1', 1080]]])
with mock.patch('aiosocks.connector.create_connection',
make_mocked_coro(raise_exception=aiosocks.SocksError())):
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://127.0.0.1'))
connector = ProxyConnector(loop=loop_mock)
with pytest.raises(aiosocks.SocksError):
await connector.connect(req)
async def test_proxy_connect_http(loop):
tr, proto = mock.Mock(name='transport'), mock.Mock(name='protocol')
loop_mock = mock.Mock()
loop_mock.getaddrinfo = make_mocked_coro([
[0, 0, 0, 0, ['127.0.0.1', 1080]]])
loop_mock.create_connection = make_mocked_coro((tr, proto))
req = ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('http://127.0.0.1'))
connector = ProxyConnector(loop=loop_mock)
await connector.connect(req)
@pytest.mark.parametrize('proxy', [
(URL('socks4://proxy.org'), Socks4Auth('login')),
(URL('socks5://proxy.org'), Socks5Auth('login', 'password')),
(URL('http://proxy.org'), BasicAuth('login')), (None, BasicAuth('login')),
(URL('socks4://proxy.org'), None), (None, None)])
def test_proxy_client_request_valid(proxy, loop):
proxy, proxy_auth = proxy
p = ProxyClientRequest('GET', URL('http://python.org'),
proxy=proxy, proxy_auth=proxy_auth, loop=loop)
assert p.proxy is proxy
assert p.proxy_auth is proxy_auth
def test_proxy_client_request_invalid(loop):
with pytest.raises(ValueError) as cm:
ProxyClientRequest(
'GET', URL('http://python.org'),
proxy=URL('socks6://proxy.org'), proxy_auth=None, loop=loop)
assert 'Only http, socks4 and socks5 proxies are supported' in str(cm)
with pytest.raises(ValueError) as cm:
ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('http://proxy.org'), proxy_auth=Socks4Auth('l'))
assert 'proxy_auth must be None or BasicAuth() ' \
'tuple for http proxy' in str(cm)
with pytest.raises(ValueError) as cm:
ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks4://proxy.org'), proxy_auth=BasicAuth('l'))
assert 'proxy_auth must be None or Socks4Auth() ' \
'tuple for socks4 proxy' in str(cm)
with pytest.raises(ValueError) as cm:
ProxyClientRequest(
'GET', URL('http://python.org'), loop=loop,
proxy=URL('socks5://proxy.org'), proxy_auth=Socks4Auth('l'))
assert 'proxy_auth must be None or Socks5Auth() ' \
'tuple for socks5 proxy' in str(cm)