From 1b857ecd6944d78d0e4498e75ece8ffa7d1b1663 Mon Sep 17 00:00:00 2001 From: JoshuaMoelans <60878493+JoshuaMoelans@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:47:14 +0100 Subject: [PATCH] add tests --- examples/example.c | 3 ++ tests/proxy.py | 10 ++++-- tests/test_integration_crashpad.py | 45 +++++++++++++++++++++++++++ tests/test_integration_http.py | 49 ++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) diff --git a/examples/example.c b/examples/example.c index da5546767..6ac07fde6 100644 --- a/examples/example.c +++ b/examples/example.c @@ -278,6 +278,9 @@ main(int argc, char **argv) if (has_arg(argc, argv, "http-proxy-ipv6")) { sentry_options_set_proxy(options, "http://[::1]:8080"); } + if (has_arg(argc, argv, "proxy-empty")) { + sentry_options_set_proxy(options, ""); + } if (has_arg(argc, argv, "socks5-proxy")) { sentry_options_set_proxy(options, "socks5://127.0.0.1:1080"); diff --git a/tests/proxy.py b/tests/proxy.py index 2ad82b772..265027abe 100644 --- a/tests/proxy.py +++ b/tests/proxy.py @@ -53,21 +53,25 @@ def start_mitmdump(proxy_type, proxy_auth: str = None): def proxy_test_finally( - expected_logsize, + expected_httpserver_logsize, httpserver, proxy_process, proxy_log_assert=assert_no_proxy_request, + expected_proxy_logsize=None, ): + if expected_proxy_logsize is None: + expected_proxy_logsize = expected_httpserver_logsize + if proxy_process: # Give mitmdump some time to get a response from the mock server time.sleep(0.5) proxy_process.terminate() proxy_process.wait() stdout, stderr = proxy_process.communicate() - if expected_logsize == 0: + if expected_proxy_logsize == 0: # don't expect any incoming requests to make it through the proxy proxy_log_assert(stdout) else: # request passed through successfully assert "POST" in stdout and "200 OK" in stdout - assert len(httpserver.log) == expected_logsize + assert len(httpserver.log) == expected_httpserver_logsize diff --git a/tests/test_integration_crashpad.py b/tests/test_integration_crashpad.py index 5e8258b07..1169a435c 100644 --- a/tests/test_integration_crashpad.py +++ b/tests/test_integration_crashpad.py @@ -103,6 +103,51 @@ def test_crashpad_crash_proxy_env_port_incorrect(cmake, httpserver): cleanup_proxy_env_vars() +def test_crashpad_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 + setup_proxy_env_vars(port=8080) # we start the proxy but expect it to remain unused + try: + env, proxy_process, tmp_path = _setup_crashpad_proxy_test( + cmake, httpserver, "http-proxy" + ) + + with httpserver.wait(timeout=10) as waiting: + child = run( + tmp_path, "sentry_example", ["log", "crash", "proxy-empty"], env=env + ) + assert child.returncode # well, it's a crash after all + assert waiting.result + + finally: + proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0) + cleanup_proxy_env_vars() + + +def test_crashpad_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 + # we start the proxy but expect it to remain unused (dsn is http, so shouldn't use https proxy) + os.environ["https_proxy"] = f"http://localhost:8080" + try: + env, proxy_process, tmp_path = _setup_crashpad_proxy_test( + cmake, httpserver, "http-proxy" + ) + + with httpserver.wait(timeout=10) as waiting: + child = run(tmp_path, "sentry_example", ["log", "crash"], env=env) + assert child.returncode # well, it's a crash after all + assert waiting.result + + finally: + proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0) + del os.environ["https_proxy"] + + @pytest.mark.parametrize( "run_args", [ diff --git a/tests/test_integration_http.py b/tests/test_integration_http.py index 11f7e8e33..bc5f453fd 100644 --- a/tests/test_integration_http.py +++ b/tests/test_integration_http.py @@ -758,6 +758,55 @@ def test_proxy_ipv6(cmake, httpserver): 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 + setup_proxy_env_vars(port=8080) # we start the proxy but expect it to remain unused + try: + env, proxy_process, tmp_path = _setup_http_proxy_test( + cmake, httpserver, "http-proxy" + ) + + run( + tmp_path, + "sentry_example", + ["log", "capture-event", "proxy-empty"], + check=True, + env=env, + ) + + finally: + proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0) + cleanup_proxy_env_vars() + + +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 + # we start the proxy but expect it to remain unused (dsn is http, so shouldn't use https proxy) + os.environ["https_proxy"] = f"http://localhost:8080" + try: + env, proxy_process, tmp_path = _setup_http_proxy_test( + cmake, httpserver, "http-proxy" + ) + + run( + tmp_path, + "sentry_example", + ["log", "capture-event"], + check=True, + env=env, + ) + + finally: + proxy_test_finally(1, httpserver, proxy_process, expected_proxy_logsize=0) + del os.environ["https_proxy"] + + @pytest.mark.parametrize( "run_args", [