From 61891e86a30de4a8ba84b41beccaceab6ce58762 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Sat, 12 Jul 2025 15:53:35 +0200 Subject: [PATCH] fix: routing tables unit tests fix for linux --- routing/tables.go | 5 +++++ routing/tables_test.go | 27 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/routing/tables.go b/routing/tables.go index fcb9f043..1023ff3b 100644 --- a/routing/tables.go +++ b/routing/tables.go @@ -21,7 +21,12 @@ func Update() ([]Route, error) { func Gateway(ip RouteType, device string) (string, error) { Update() + return gatewayFromTable(ip, device) +} +// gatewayFromTable finds the gateway from the current table without updating it +// This allows testing with controlled table data +func gatewayFromTable(ip RouteType, device string) (string, error) { lock.RLock() defer lock.RUnlock() diff --git a/routing/tables_test.go b/routing/tables_test.go index a0796784..761f1356 100644 --- a/routing/tables_test.go +++ b/routing/tables_test.go @@ -159,7 +159,7 @@ func TestGatewayEmptyTable(t *testing.T) { // Test with empty table resetTable() - gateway, err := Gateway(IPv4, "eth0") + gateway, err := gatewayFromTable(IPv4, "eth0") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -185,7 +185,7 @@ func TestGatewayNoDefaultRoute(t *testing.T) { } lock.Unlock() - gateway, err := Gateway(IPv4, "eth0") + gateway, err := gatewayFromTable(IPv4, "eth0") if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -204,6 +204,29 @@ func TestGatewayWindowsCase(t *testing.T) { t.Logf("Gateway result for eth0: %s", gateway) } +func TestGatewayFromTableWithDefaults(t *testing.T) { + // Test gatewayFromTable with controlled data containing defaults + resetTable() + addTestRoutes() + + gateway, err := gatewayFromTable(IPv4, "eth0") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if gateway != "192.168.1.1" { + t.Errorf("Expected gateway 192.168.1.1, got %s", gateway) + } + + // Test with device-specific lookup + gateway, err = gatewayFromTable(IPv4, "wlan0") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if gateway != "10.0.0.1" { + t.Errorf("Expected gateway 10.0.0.1, got %s", gateway) + } +} + func TestTableConcurrency(t *testing.T) { // Test concurrent access to Table() resetTable()