fix: routing tables unit tests fix for linux

This commit is contained in:
evilsocket 2025-07-12 15:53:35 +02:00
commit 61891e86a3
2 changed files with 30 additions and 2 deletions

View file

@ -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()

View file

@ -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()