-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest_integration_proxy.py
More file actions
266 lines (219 loc) · 7.79 KB
/
test_integration_proxy.py
File metadata and controls
266 lines (219 loc) · 7.79 KB
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
import os
import shutil
import sys
import pytest
from . import (
make_dsn,
run,
)
from .assertions import (
assert_failed_proxy_auth_request,
)
from .conditions import has_http
from .proxy import (
closed_port,
start_mitmdump,
proxy_test_finally,
)
pytestmark = pytest.mark.skipif(not has_http, reason="tests need http")
def _setup_http_proxy_test(
cmake, httpserver, proxy, proxy_auth=None, listen_host="127.0.0.1"
):
if proxy:
proxy_process, port = start_mitmdump(proxy, proxy_auth, listen_host=listen_host)
else:
proxy_process, port = None, None
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"})
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True))
if port is not None:
env["SENTRY_TEST_PROXY_PORT"] = str(port)
httpserver.expect_request("/api/123456/envelope/").respond_with_data("OK")
return env, proxy_process, tmp_path, port
def test_proxy_from_env(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy"
)
env["http_proxy"] = f"http://127.0.0.1:{port}"
env["https_proxy"] = f"http://127.0.0.1:{port}"
run(
tmp_path,
"sentry_example",
["log", "capture-event"],
env=env,
)
finally:
proxy_test_finally(1, httpserver, proxy_process)
def test_proxy_from_env_port_incorrect(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy"
)
# Set env vars with a wrong port (offset by 1 from the actual proxy port)
env["http_proxy"] = f"http://127.0.0.1:{port + 1}"
env["https_proxy"] = f"http://127.0.0.1:{port + 1}"
run(
tmp_path,
"sentry_example",
["log", "capture-event"],
env=env,
)
finally:
proxy_test_finally(0, httpserver, proxy_process)
def test_proxy_auth(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy", proxy_auth="user:password"
)
run(
tmp_path,
"sentry_example",
["log", "capture-event", "http-proxy-auth"],
env=dict(
os.environ,
SENTRY_DSN=make_dsn(httpserver, proxy_host=True),
SENTRY_TEST_PROXY_PORT=str(port),
),
)
finally:
proxy_test_finally(
1,
httpserver,
proxy_process,
assert_failed_proxy_auth_request,
)
def test_proxy_auth_incorrect(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy", proxy_auth="wrong:wrong"
)
run(
tmp_path,
"sentry_example",
["log", "capture-event", "http-proxy-auth"],
env=dict(
os.environ,
SENTRY_DSN=make_dsn(httpserver, proxy_host=True),
SENTRY_TEST_PROXY_PORT=str(port),
),
)
finally:
proxy_test_finally(
0,
httpserver,
proxy_process,
assert_failed_proxy_auth_request,
)
def test_proxy_ipv6(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy", listen_host="::1"
)
run(
tmp_path,
"sentry_example",
["log", "capture-event", "http-proxy-ipv6"],
env=env,
)
finally:
proxy_test_finally(1, httpserver, proxy_process)
def test_proxy_set_empty(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy"
)
# we start the proxy but expect it to remain unused
env["http_proxy"] = f"http://127.0.0.1:{port}"
env["https_proxy"] = f"http://127.0.0.1:{port}"
run(
tmp_path,
"sentry_example",
["log", "capture-event", "proxy-empty"],
env=env,
)
finally:
proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0)
def test_proxy_https_not_http(cmake, httpserver):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
try:
# we start the proxy but expect it to remain unused (dsn is http, so shouldn't use https proxy)
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, "http-proxy"
)
env["https_proxy"] = f"http://127.0.0.1:{port}"
run(
tmp_path,
"sentry_example",
["log", "capture-event"],
env=env,
)
finally:
proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0)
@pytest.mark.parametrize(
"run_args",
[
pytest.param(["http-proxy"]), # HTTP proxy test runs on all platforms
pytest.param(
["socks5-proxy"],
marks=pytest.mark.skipif(
sys.platform not in ["darwin", "linux"],
reason="SOCKS5 proxy tests are only supported on macOS and Linux",
),
),
],
)
@pytest.mark.parametrize("proxy_running", [True, False])
def test_capture_proxy(cmake, httpserver, run_args, proxy_running):
if not shutil.which("mitmdump"):
pytest.skip("mitmdump is not installed")
proxy_process = None # store the proxy process to terminate it later
expected_logsize = 0
try:
proxy_to_start = run_args[0] if proxy_running else None
env, proxy_process, tmp_path, port = _setup_http_proxy_test(
cmake, httpserver, proxy_to_start
)
def do_run(effective_port):
port_env = {"SENTRY_TEST_PROXY_PORT": str(effective_port)}
run(
tmp_path,
"sentry_example",
["log", "capture-event"]
+ run_args, # only passes if given proxy is running
env=dict(
os.environ,
SENTRY_DSN=make_dsn(httpserver, proxy_host=True),
**port_env,
),
)
if proxy_running:
do_run(port)
expected_logsize = 1
else:
# Hold a port open without listening — connections are refused and
# no other process can claim it while the test is running.
with closed_port() as refused_port:
do_run(refused_port)
expected_logsize = 0
finally:
proxy_test_finally(expected_logsize, httpserver, proxy_process)