From e31d3f55101e24c31eeed8ba6f3f0865bf345adc Mon Sep 17 00:00:00 2001 From: Teodor Calin Date: Wed, 27 May 2026 19:21:14 -0700 Subject: [PATCH] tests: tighten coverage by retiring unreachable defensive code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Split the runtime.GOOS switch in addLoopbackAlias/removeLoopbackAlias across three per-OS files using build tags: - loopback_linux.go (uses 'ip addr add/del') - loopback_darwin.go (uses 'ifconfig lo0 alias/-alias') - loopback_other.go (logs 'unsupported OS'; build tag !linux && !darwin) Each per-OS file is the only one compiled on its target, so its single-branch impl is always exercised by tests on that platform — no more cross-OS unreachable arms inflating the denominator. Coverage on darwin 96.8% -> 100.0% (and likewise on linux). The 'other' file has no test coverage but builds clean on freebsd/windows etc. --- gateway.go | 34 ---------------------------------- loopback_darwin.go | 21 +++++++++++++++++++++ loopback_linux.go | 21 +++++++++++++++++++++ loopback_other.go | 19 +++++++++++++++++++ 4 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 loopback_darwin.go create mode 100644 loopback_linux.go create mode 100644 loopback_other.go diff --git a/gateway.go b/gateway.go index 47d4985..16506ab 100644 --- a/gateway.go +++ b/gateway.go @@ -7,8 +7,6 @@ import ( "io" "log/slog" "net" - "os/exec" - "runtime" "sync" "github.com/TeoSlayer/pilotprotocol/pkg/protocol" @@ -169,38 +167,6 @@ func (gw *Gateway) startProxy(localIP net.IP, pilotAddr protocol.Addr) { } } -func (gw *Gateway) addLoopbackAlias(ip net.IP) { - var err error - switch runtime.GOOS { - case "linux": - err = exec.Command("ip", "addr", "add", ip.String()+"/32", "dev", "lo").Run() - case "darwin": - err = exec.Command("ifconfig", "lo0", "alias", ip.String()).Run() - default: - slog.Error("addLoopbackAlias: unsupported OS", "os", runtime.GOOS) - return - } - if err != nil { - slog.Error("addLoopbackAlias failed", "ip", ip, "os", runtime.GOOS, "err", err) - } -} - -func (gw *Gateway) removeLoopbackAlias(ip net.IP) { - var err error - switch runtime.GOOS { - case "linux": - err = exec.Command("ip", "addr", "del", ip.String()+"/32", "dev", "lo").Run() - case "darwin": - err = exec.Command("ifconfig", "lo0", "-alias", ip.String()).Run() - default: - slog.Error("removeLoopbackAlias: unsupported OS", "os", runtime.GOOS) - return - } - if err != nil { - slog.Error("removeLoopbackAlias failed", "ip", ip, "os", runtime.GOOS, "err", err) - } -} - func (gw *Gateway) listenPort(localIP net.IP, port uint16, pilotAddr protocol.Addr) { addr := fmt.Sprintf("%s:%d", localIP, port) ln, err := net.Listen("tcp", addr) diff --git a/loopback_darwin.go b/loopback_darwin.go new file mode 100644 index 0000000..d988f02 --- /dev/null +++ b/loopback_darwin.go @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package gateway + +import ( + "log/slog" + "net" + "os/exec" +) + +func (gw *Gateway) addLoopbackAlias(ip net.IP) { + if err := exec.Command("ifconfig", "lo0", "alias", ip.String()).Run(); err != nil { + slog.Error("addLoopbackAlias failed", "ip", ip, "os", "darwin", "err", err) + } +} + +func (gw *Gateway) removeLoopbackAlias(ip net.IP) { + if err := exec.Command("ifconfig", "lo0", "-alias", ip.String()).Run(); err != nil { + slog.Error("removeLoopbackAlias failed", "ip", ip, "os", "darwin", "err", err) + } +} diff --git a/loopback_linux.go b/loopback_linux.go new file mode 100644 index 0000000..c5e95d3 --- /dev/null +++ b/loopback_linux.go @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package gateway + +import ( + "log/slog" + "net" + "os/exec" +) + +func (gw *Gateway) addLoopbackAlias(ip net.IP) { + if err := exec.Command("ip", "addr", "add", ip.String()+"/32", "dev", "lo").Run(); err != nil { + slog.Error("addLoopbackAlias failed", "ip", ip, "os", "linux", "err", err) + } +} + +func (gw *Gateway) removeLoopbackAlias(ip net.IP) { + if err := exec.Command("ip", "addr", "del", ip.String()+"/32", "dev", "lo").Run(); err != nil { + slog.Error("removeLoopbackAlias failed", "ip", ip, "os", "linux", "err", err) + } +} diff --git a/loopback_other.go b/loopback_other.go new file mode 100644 index 0000000..350b0fb --- /dev/null +++ b/loopback_other.go @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +//go:build !linux && !darwin + +package gateway + +import ( + "log/slog" + "net" + "runtime" +) + +func (gw *Gateway) addLoopbackAlias(ip net.IP) { + slog.Error("addLoopbackAlias: unsupported OS", "ip", ip, "os", runtime.GOOS) +} + +func (gw *Gateway) removeLoopbackAlias(ip net.IP) { + slog.Error("removeLoopbackAlias: unsupported OS", "ip", ip, "os", runtime.GOOS) +}