File: conftest.py

package info (click to toggle)
python-socks 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: python: 5,191; sh: 8; makefile: 3
file content (268 lines) | stat: -rw-r--r-- 7,059 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import ssl
from contextlib import contextmanager
from unittest import mock

import pytest
import trustme

from python_socks.async_.asyncio._resolver import Resolver as AsyncioResolver
from python_socks.sync._resolver import SyncResolver
from tests.config import (
    PROXY_HOST_IPV4,
    PROXY_HOST_IPV6,
    PROXY_HOST_NAME_IPV4,
    PROXY_HOST_NAME_IPV6,
    SOCKS5_PROXY_PORT,
    LOGIN,
    PASSWORD,
    SKIP_IPV6_TESTS,
    HTTP_PROXY_PORT,
    SOCKS4_PORT_NO_AUTH,
    SOCKS4_PROXY_PORT,
    SOCKS5_PROXY_PORT_NO_AUTH,
    TEST_PORT_IPV4,
    TEST_PORT_IPV6,
    TEST_HOST_IPV4,
    TEST_HOST_IPV6,
    TEST_HOST_NAME_IPV4,
    TEST_HOST_NAME_IPV6,
    TEST_PORT_IPV4_HTTPS,
    HTTPS_PROXY_PORT,
)
from tests.http_server import HttpServer, HttpServerConfig
from tests.mocks import sync_resolve_factory, async_resolve_factory
from tests.proxy_server import ProxyConfig, ProxyServer
from tests.utils import wait_until_connectable


@contextmanager
def nullcontext():
    yield None


@pytest.fixture(scope='session')
def target_ssl_ca() -> trustme.CA:
    return trustme.CA()


@pytest.fixture(scope='session')
def target_ssl_cert(target_ssl_ca) -> trustme.LeafCert:
    return target_ssl_ca.issue_cert(
        'localhost',
        TEST_HOST_IPV4,
        TEST_HOST_IPV6,
        TEST_HOST_NAME_IPV4,
        TEST_HOST_NAME_IPV6,
    )


@pytest.fixture(scope='session')
def target_ssl_certfile(target_ssl_cert):
    with target_ssl_cert.cert_chain_pems[0].tempfile() as cert_path:
        yield cert_path


@pytest.fixture(scope='session')
def target_ssl_keyfile(target_ssl_cert):
    with target_ssl_cert.private_key_pem.tempfile() as private_key_path:
        yield private_key_path


@pytest.fixture(scope='session')
def target_ssl_context(target_ssl_ca) -> ssl.SSLContext:
    ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ssl_ctx.verify_mode = ssl.CERT_REQUIRED
    ssl_ctx.check_hostname = True
    target_ssl_ca.configure_trust(ssl_ctx)
    return ssl_ctx


@pytest.fixture(scope='session')
def proxy_ssl_ca() -> trustme.CA:
    return trustme.CA()


@pytest.fixture(scope='session')
def proxy_ssl_cert(proxy_ssl_ca) -> trustme.LeafCert:
    return proxy_ssl_ca.issue_cert(
        'localhost',
        PROXY_HOST_IPV4,
        PROXY_HOST_IPV6,
        PROXY_HOST_NAME_IPV4,
        PROXY_HOST_NAME_IPV6,
    )


@pytest.fixture(scope='session')
def proxy_ssl_context(proxy_ssl_ca) -> ssl.SSLContext:
    ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ssl_ctx.verify_mode = ssl.CERT_REQUIRED
    ssl_ctx.check_hostname = True
    proxy_ssl_ca.configure_trust(ssl_ctx)
    return ssl_ctx


@pytest.fixture(scope='session')
def proxy_ssl_certfile(proxy_ssl_cert):
    with proxy_ssl_cert.cert_chain_pems[0].tempfile() as cert_path:
        yield cert_path


@pytest.fixture(scope='session')
def proxy_ssl_keyfile(proxy_ssl_cert):
    with proxy_ssl_cert.private_key_pem.tempfile() as private_key_path:
        yield private_key_path


@pytest.fixture(scope='session', autouse=True)
def patch_resolvers():
    p1 = mock.patch.object(
        SyncResolver,
        attribute='resolve',
        new=sync_resolve_factory(SyncResolver),
    )

    p2 = mock.patch.object(
        AsyncioResolver,
        attribute='resolve',
        new=async_resolve_factory(AsyncioResolver),
    )

    try:
        # noinspection PyProtectedMember
        from python_socks.async_.trio._resolver import Resolver as TrioResolver
    except ImportError:
        p3 = nullcontext()
    else:
        p3 = mock.patch.object(
            TrioResolver,
            attribute='resolve',
            new=async_resolve_factory(TrioResolver),
        )

    try:
        # noinspection PyProtectedMember
        from python_socks.async_.curio._resolver import Resolver as CurioResolver
    except ImportError:
        p4 = nullcontext()
    else:
        p4 = mock.patch.object(
            CurioResolver,
            attribute='resolve',
            new=async_resolve_factory(CurioResolver),
        )

    try:
        from python_socks.async_.anyio._resolver import Resolver as AnyioResolver
    except ImportError:
        p5 = nullcontext()
    else:
        p5 = mock.patch.object(
            AnyioResolver,
            attribute='resolve',
            new=async_resolve_factory(AnyioResolver),
        )

    with p1, p2, p3, p4, p5:
        yield None


@pytest.fixture(scope='session', autouse=True)
def proxy_server(proxy_ssl_certfile, proxy_ssl_keyfile):
    config = [
        ProxyConfig(
            proxy_type='http',
            host=PROXY_HOST_IPV4,
            port=HTTP_PROXY_PORT,
            username=LOGIN,
            password=PASSWORD,
        ),
        ProxyConfig(
            proxy_type='socks4',
            host=PROXY_HOST_IPV4,
            port=SOCKS4_PROXY_PORT,
            username=LOGIN,
            password=None,
        ),
        ProxyConfig(
            proxy_type='socks4',
            host=PROXY_HOST_IPV4,
            port=SOCKS4_PORT_NO_AUTH,
            username=None,
            password=None,
        ),
        ProxyConfig(
            proxy_type='socks5',
            host=PROXY_HOST_IPV4,
            port=SOCKS5_PROXY_PORT,
            username=LOGIN,
            password=PASSWORD,
        ),
        ProxyConfig(
            proxy_type='socks5',
            host=PROXY_HOST_IPV4,
            port=SOCKS5_PROXY_PORT_NO_AUTH,
            username=None,
            password=None,
        ),
        ProxyConfig(
            proxy_type='http',
            # host=PROXY_HOST_NAME_IPV4,
            host=PROXY_HOST_IPV4,
            port=HTTPS_PROXY_PORT,
            username=LOGIN,
            password=PASSWORD,
            ssl_certfile=proxy_ssl_certfile,
            ssl_keyfile=proxy_ssl_keyfile,
        ),
    ]

    if not SKIP_IPV6_TESTS:
        config.append(
            ProxyConfig(
                proxy_type='socks5',
                host=PROXY_HOST_IPV6,
                port=SOCKS5_PROXY_PORT,
                username=LOGIN,
                password=PASSWORD,
            ),
        )

    server = ProxyServer(config=config)
    server.start()
    for cfg in config:
        wait_until_connectable(host=cfg.host, port=cfg.port, timeout=10)

    yield None

    server.terminate()


@pytest.fixture(scope='session', autouse=True)
def web_server(target_ssl_certfile, target_ssl_keyfile):
    config = [
        HttpServerConfig(
            host=TEST_HOST_IPV4,
            port=TEST_PORT_IPV4,
        ),
        HttpServerConfig(
            host=TEST_HOST_IPV4,
            port=TEST_PORT_IPV4_HTTPS,
            # certfile=TEST_HOST_CERT_FILE,
            # keyfile=TEST_HOST_KEY_FILE,
            certfile=target_ssl_certfile,
            keyfile=target_ssl_keyfile,
        ),
    ]

    if not SKIP_IPV6_TESTS:
        config.append(HttpServerConfig(host=TEST_HOST_IPV6, port=TEST_PORT_IPV6))

    server = HttpServer(config=config)
    server.start()
    for cfg in config:
        server.wait_until_connectable(host=cfg.host, port=cfg.port)

    yield None

    server.terminate()