Skip to content

Commit

Permalink
add non-localhost hostname set/check/usage
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMoelans committed Feb 4, 2025
1 parent 40482c4 commit 391fba1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ jobs:
run: bash scripts/start-android.sh
timeout-minutes: 20

- name: Add sentry.native.test hostname
# The path is usually C:\Windows\System32\drivers\etc\hosts
run: |
Add-Content -Path $env:SystemRoot\System32\drivers\etc\hosts -Value "127.0.0.1 sentry.native.test"
shell: powershell

- name: Add sentry.native.test hostname
run: |
echo "127.0.0.1 sentry.native.test" | sudo tee -a /etc/hosts
cat /etc/hosts
shell: bash

- name: Print hosts file
run: type $env:SystemRoot\System32\drivers\etc\hosts
shell: powershell

- name: Test
shell: bash
run: |
Expand Down
17 changes: 16 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
from tests.assertions import assert_no_proxy_request


def make_dsn(httpserver, auth="uiaeosnrtdy", id=123456):
def make_dsn(httpserver, auth="uiaeosnrtdy", id=123456, proxy_host=False):
url = urllib.parse.urlsplit(httpserver.url_for("/{}".format(id)))
# We explicitly use `127.0.0.1` here, because on Windows, `localhost` will
# first try `::1` (the ipv6 loopback), retry a couple times and give up
# after a timeout of 2 seconds, falling back to the ipv4 loopback instead.
host = url.netloc.replace("localhost", "127.0.0.1")
if proxy_host:
# To avoid bypassing the proxy for requests to localhost, we need to add this mapping
# to the hosts file & make the DSN using this alternate hostname
# see https://learn.microsoft.com/en-us/windows/win32/wininet/enabling-internet-functionality#listing-the-proxy-bypass
host = url.netloc.replace("127.0.0.1", "sentry.native.test")
check_sentry_native_resolves_to_localhost()

return urllib.parse.urlunsplit(
(
url.scheme,
Expand All @@ -37,6 +44,14 @@ def make_dsn(httpserver, auth="uiaeosnrtdy", id=123456):
)


def check_sentry_native_resolves_to_localhost():
try:
resolved_ip = socket.gethostbyname("sentry.native.test")
assert resolved_ip == "127.0.0.1"
except socket.gaierror:
pytest.skip("sentry.native.test does not resolve to localhost")


def is_proxy_running(host, port):
try:
with socket.create_connection((host, port), timeout=1):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_integration_crashpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_crashpad_crash_proxy_env(cmake, httpserver, port_correct):
# make sure we are isolated from previous runs
shutil.rmtree(tmp_path / ".sentry-native", ignore_errors=True)

env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True))
httpserver.expect_oneshot_request("/api/123456/minidump/").respond_with_data(
"OK"
)
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_crashpad_crash_proxy(cmake, httpserver, run_args, proxy_running):
# make sure we are isolated from previous runs
shutil.rmtree(tmp_path / ".sentry-native", ignore_errors=True)

env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))
env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True))
httpserver.expect_oneshot_request("/api/123456/minidump/").respond_with_data(
"OK"
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_integration_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def test_capture_minidump(cmake, httpserver):
"sentry_example",
["log", "attachment", "capture-minidump"],
check=True,
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver)),
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True)),
)

assert len(httpserver.log) == 1
Expand Down Expand Up @@ -638,7 +638,7 @@ def test_proxy_from_env(cmake, httpserver, port_correct):
"sentry_example",
["log", "capture-event"],
check=True,
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver)),
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True)),
)

if port_correct:
Expand Down Expand Up @@ -673,7 +673,7 @@ def test_proxy_auth(cmake, httpserver, auth_correct):
"sentry_example",
["log", "capture-event", "http-proxy-auth"],
check=True,
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver)),
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True)),
)
if auth_correct:
expected_logsize = 1
Expand Down Expand Up @@ -709,7 +709,7 @@ def test_proxy_ipv6(cmake, httpserver):
"sentry_example",
["log", "capture-event", "http-proxy-ipv6"],
check=True,
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver)),
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True)),
)

expected_logsize = 1
Expand Down Expand Up @@ -756,7 +756,7 @@ def test_capture_proxy(cmake, httpserver, run_args, proxy_running):
["log", "capture-event"]
+ [current_run_arg], # only passes if given proxy is running
check=True,
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver)),
env=dict(os.environ, SENTRY_DSN=make_dsn(httpserver, proxy_host=True)),
)
if proxy_running:
expected_logsize = 1
Expand Down

0 comments on commit 391fba1

Please sign in to comment.