Don't hardcode 127.0.0.1 in tests

How about we resolve through the special host name "localhost." instead of assuming that the loopback interface is bound to 127.0.0.1 (which might not be the case, especially in FreeBSD jails)?

See RFC6761 for how "localhost." is treated, especially:

Name resolution APIs and libraries SHOULD recognize localhost
names as special and SHOULD always return the IP loopback address
for address queries and negative responses for all other query
types.  Name resolution APIs SHOULD NOT send queries for
localhost names to their configured caching DNS server(s).
This commit is contained in:
Pietro Cerutti
2026-09-08 10:50:34 +00:00
parent 359d9f9d49
commit ba17c47e06
+11 -1
View File
@@ -23,8 +23,18 @@
(setdyn *lint-warn* :none)
(defn decide-test-host [port]
(if-let [host (os/getenv "JANET_TEST_HOST")]
host
(try
(let [s (net/listen "localhost." port)
[host _] (net/localname s)]
(net/close s)
host)
([] "127.0.0.1"))))
(def test-port (os/getenv "JANET_TEST_PORT" "8761"))
(def test-host (os/getenv "JANET_TEST_HOST" "127.0.0.1"))
(def test-host (decide-test-host test-port))
# Subprocess
# 5e1a8c86f