Files
rtnetmon/internal/netdetect/netdetect_test.go
T
sneak ef3c373a12 Detect interfaces per platform; add macOS and single-interface support (closes #2)
Linux runs exactly as before when both bridge interfaces exist. When they do
not, the lone default-route interface is monitored, and a single interface now
draws a single UI pane instead of an empty second one.

macOS is newly supported: a running VPN tunnel (utun) is monitored as the
primary pane alongside the physical default-route interface, or the physical
interface alone when no VPN is up.

Detection lives in internal/netdetect: interface/route data types, pure
selection logic keyed on OS name, and route parsers, all unit-tested on Linux
for both platforms. Only the real route query and the per-platform TCP dial
binding (source address on Linux, IP_BOUND_IF on macOS) are build-tagged. Ping
argument construction is a pure, OS-keyed function. NewMonitor now takes a list
of interfaces.

Model: opus-4-8
2026-09-21 06:53:39 +00:00

239 lines
6.5 KiB
Go

package netdetect
import (
"reflect"
"testing"
)
// gu and backhaul are the Linux bridge interfaces rtnetmon was built for.
func linuxFlags() Flags {
return Flags{
IfaceA: "gu0", LabelA: "gu LAN - VPN outbound",
IfaceB: "backhaul0", LabelB: "Cox cable direct",
}
}
func TestSelect(t *testing.T) {
tests := []struct {
name string
goos string
ifaces []Interface
routes []Route
flags Flags
want []Pane
wantErr bool
}{
{
name: "linux both bridge interfaces present",
goos: "linux",
ifaces: []Interface{
{Name: "gu0", Up: true},
{Name: "backhaul0", Up: true},
{Name: "eth0", Up: true},
},
routes: []Route{{Iface: "eth0", Default: true}},
flags: linuxFlags(),
want: []Pane{
{Name: "gu0", Label: "gu LAN - VPN outbound"},
{Name: "backhaul0", Label: "Cox cable direct"},
},
},
{
name: "linux no bridge interfaces, single default route",
goos: "linux",
ifaces: []Interface{{Name: "eth0", Up: true}, {Name: "lo", Up: true}},
routes: []Route{{Iface: "eth0", Default: true}},
flags: linuxFlags(),
want: []Pane{{Name: "eth0", Label: "default route"}},
},
{
name: "linux only one bridge interface present is an error",
goos: "linux",
ifaces: []Interface{{Name: "gu0", Up: true}, {Name: "eth0", Up: true}},
routes: []Route{{Iface: "eth0", Default: true}},
flags: linuxFlags(),
wantErr: true,
},
{
name: "linux no bridge, no default route is an error",
goos: "linux",
ifaces: []Interface{{Name: "eth0", Up: true}},
routes: nil,
flags: linuxFlags(),
wantErr: true,
},
{
name: "linux no bridge, multiple default routes is an error",
goos: "linux",
ifaces: []Interface{{Name: "eth0", Up: true}, {Name: "eth1", Up: true}},
routes: []Route{{Iface: "eth0", Default: true}, {Iface: "eth1", Default: true}},
flags: linuxFlags(),
wantErr: true,
},
{
name: "linux single default route keeps explicit label",
goos: "linux",
ifaces: []Interface{
{Name: "wlan0", Up: true},
},
routes: []Route{{Iface: "wlan0", Default: true}},
flags: Flags{
IfaceA: "gu0", LabelA: "Home WiFi", LabelASet: true,
IfaceB: "backhaul0", LabelB: "Cox cable direct",
},
want: []Pane{{Name: "wlan0", Label: "Home WiFi"}},
},
{
name: "macos vpn tunnel plus physical default route",
goos: "darwin",
ifaces: []Interface{
{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}},
{Name: "utun4", Up: true, IPv4: []string{"10.64.0.2"}},
{Name: "utun0", Up: true, IPv4: nil},
},
routes: []Route{
{Iface: "utun4", Default: true},
{Iface: "en0", Default: true},
},
flags: linuxFlags(),
want: []Pane{
{Name: "utun4", Label: "VPN"},
{Name: "en0", Label: "default route"},
},
},
{
name: "macos vpn detected by routable address without its own route",
goos: "darwin",
ifaces: []Interface{
{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}},
{Name: "utun6", Up: true, IPv4: []string{"10.2.0.2"}},
},
routes: []Route{{Iface: "en0", Default: true}},
flags: linuxFlags(),
want: []Pane{
{Name: "utun6", Label: "VPN"},
{Name: "en0", Label: "default route"},
},
},
{
name: "macos no vpn, single physical default route",
goos: "darwin",
ifaces: []Interface{
{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}},
{Name: "utun0", Up: true, IPv4: nil},
{Name: "utun1", Up: true, IPv4: []string{"169.254.1.1"}},
},
routes: []Route{{Iface: "en0", Default: true}},
flags: linuxFlags(),
want: []Pane{{Name: "en0", Label: "default route"}},
},
{
name: "macos vpn pane honors explicit labels",
goos: "darwin",
ifaces: []Interface{
{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}},
{Name: "utun4", Up: true, IPv4: []string{"10.64.0.2"}},
},
routes: []Route{
{Iface: "utun4", Default: true},
{Iface: "en0", Default: true},
},
flags: Flags{
LabelA: "Mullvad", LabelASet: true,
LabelB: "Fiber", LabelBSet: true,
},
want: []Pane{
{Name: "utun4", Label: "Mullvad"},
{Name: "en0", Label: "Fiber"},
},
},
{
name: "macos no default route is an error",
goos: "darwin",
ifaces: []Interface{{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}}},
routes: nil,
flags: linuxFlags(),
wantErr: true,
},
{
name: "macos two physical default routes is an error",
goos: "darwin",
ifaces: []Interface{
{Name: "en0", Up: true, IPv4: []string{"192.168.1.20"}},
{Name: "en1", Up: true, IPv4: []string{"192.168.2.20"}},
},
routes: []Route{
{Iface: "en0", Default: true},
{Iface: "en1", Default: true},
},
flags: linuxFlags(),
wantErr: true,
},
{
name: "unsupported operating system is an error",
goos: "windows",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Select(tt.goos, tt.ifaces, tt.routes, tt.flags)
if tt.wantErr {
if err == nil {
t.Fatalf("Select() expected error, got panes %v", got)
}
return
}
if err != nil {
t.Fatalf("Select() unexpected error: %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Select() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseIPRoute(t *testing.T) {
out := "default via 192.168.1.1 dev eth0 proto dhcp metric 100\n"
got := parseIPRoute(out)
want := []Route{{Iface: "eth0", Gateway: "192.168.1.1", Default: true}}
if !reflect.DeepEqual(got, want) {
t.Errorf("parseIPRoute() = %v, want %v", got, want)
}
}
func TestParseProcNetRoute(t *testing.T) {
out := "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\n" +
"eth0\t00000000\t0102A8C0\t0003\t0\t0\t100\t00000000\n" +
"eth0\t0002A8C0\t00000000\t0001\t0\t0\t0\t00FFFFFF\n"
got := parseProcNetRoute(out)
want := []Route{{Iface: "eth0", Gateway: "192.168.2.1", Default: true}}
if !reflect.DeepEqual(got, want) {
t.Errorf("parseProcNetRoute() = %v, want %v", got, want)
}
}
func TestParseNetstat(t *testing.T) {
out := "Routing tables\n\nInternet:\n" +
"Destination Gateway Flags Netif Expire\n" +
"default 10.0.0.1 UGScg en0\n" +
"default link#15 UCSg utun4\n" +
"127.0.0.1 127.0.0.1 UH lo0\n"
got := parseNetstat(out)
want := []Route{
{Iface: "en0", Gateway: "10.0.0.1", Default: true},
{Iface: "utun4", Gateway: "link#15", Default: true},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("parseNetstat() = %v, want %v", got, want)
}
}