2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-12-08 13:19:55 +01:00
|
|
|
"net/netip"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3/stdnet"
|
2024-01-03 16:06:20 +01:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2023-09-05 21:07:32 +02:00
|
|
|
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2022-09-05 09:06:35 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
// send 5 routes, one for server and 4 for clients, one normal and 2 HA and one small
|
|
|
|
// if linux host, should have one for server in map
|
|
|
|
// we should have 2 client manager
|
|
|
|
// 2 ranges in our routing table
|
|
|
|
|
|
|
|
const localPeerKey = "local"
|
|
|
|
const remotePeerKey1 = "remote1"
|
|
|
|
const remotePeerKey2 = "remote1"
|
|
|
|
|
|
|
|
func TestManagerUpdateRoutes(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2024-04-05 20:38:49 +02:00
|
|
|
name string
|
|
|
|
inputInitRoutes []*route.Route
|
|
|
|
inputRoutes []*route.Route
|
|
|
|
inputSerial uint64
|
|
|
|
removeSrvRouter bool
|
|
|
|
serverRoutesExpected int
|
|
|
|
clientNetworkWatchersExpected int
|
2022-09-05 09:06:35 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Should create 2 client networks",
|
|
|
|
inputInitRoutes: []*route.Route{},
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Create 2 Server Routes",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("100.64.252.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.9/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
serverRoutesExpected: 2,
|
|
|
|
clientNetworkWatchersExpected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Create 1 Route For Client And Server",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("100.64.30.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.9.9/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
serverRoutesExpected: 1,
|
|
|
|
clientNetworkWatchersExpected: 1,
|
|
|
|
},
|
2023-09-05 21:07:32 +02:00
|
|
|
{
|
|
|
|
name: "Should Create 1 Route For Client and Skip Server Route On Empty Server Router",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("100.64.30.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.9.9/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
removeSrvRouter: true,
|
|
|
|
serverRoutesExpected: 0,
|
|
|
|
clientNetworkWatchersExpected: 1,
|
|
|
|
},
|
2022-09-05 09:06:35 +02:00
|
|
|
{
|
|
|
|
name: "Should Create 1 HA Route and 1 Standalone",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.20.0/24"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey2,
|
|
|
|
Network: netip.MustParsePrefix("8.8.20.0/24"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "c",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.9.9/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No Small Client Route Should Be Added",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("0.0.0.0/0"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
2024-04-05 20:38:49 +02:00
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 0,
|
2022-09-05 09:06:35 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Remove 1 Client Route",
|
|
|
|
inputInitRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Update Route to HA",
|
|
|
|
inputInitRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey2,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Remove Client Routes",
|
|
|
|
inputInitRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputRoutes: []*route.Route{},
|
|
|
|
inputSerial: 1,
|
|
|
|
clientNetworkWatchersExpected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Remove All Routes",
|
|
|
|
inputInitRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "a",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "b",
|
|
|
|
NetID: "routeB",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.8.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputRoutes: []*route.Route{},
|
|
|
|
inputSerial: 1,
|
|
|
|
serverRoutesExpected: 0,
|
|
|
|
clientNetworkWatchersExpected: 0,
|
|
|
|
},
|
2022-12-08 13:19:55 +01:00
|
|
|
{
|
|
|
|
name: "HA server should not register routes from the same HA group",
|
|
|
|
inputRoutes: []*route.Route{
|
|
|
|
{
|
|
|
|
ID: "l1",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "l2",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: localPeerKey,
|
|
|
|
Network: netip.MustParsePrefix("8.8.9.8/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "r1",
|
|
|
|
NetID: "routeA",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("100.64.251.250/30"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "r2",
|
|
|
|
NetID: "routeC",
|
|
|
|
Peer: remotePeerKey1,
|
|
|
|
Network: netip.MustParsePrefix("8.8.9.9/32"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
Metric: 9999,
|
|
|
|
Masquerade: false,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inputSerial: 1,
|
|
|
|
serverRoutesExpected: 2,
|
|
|
|
clientNetworkWatchersExpected: 1,
|
|
|
|
},
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
2024-01-03 16:06:20 +01:00
|
|
|
peerPrivateKey, _ := wgtypes.GeneratePrivateKey()
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun43%d", n), "100.65.65.2/24", 33100, peerPrivateKey.String(), iface.DefaultMTU, newNet, nil)
|
2022-09-05 09:06:35 +02:00
|
|
|
require.NoError(t, err, "should create testing WGIface interface")
|
|
|
|
defer wgInterface.Close()
|
|
|
|
|
|
|
|
err = wgInterface.Create()
|
|
|
|
require.NoError(t, err, "should create testing wireguard interface")
|
|
|
|
|
2023-03-16 17:22:36 +01:00
|
|
|
statusRecorder := peer.NewRecorder("https://mgm")
|
2022-09-05 09:06:35 +02:00
|
|
|
ctx := context.TODO()
|
2023-05-31 18:25:24 +02:00
|
|
|
routeManager := NewManager(ctx, localPeerKey, wgInterface, statusRecorder, nil)
|
2022-09-05 09:06:35 +02:00
|
|
|
defer routeManager.Stop()
|
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
if testCase.removeSrvRouter {
|
|
|
|
routeManager.serverRouter = nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
if len(testCase.inputInitRoutes) > 0 {
|
|
|
|
err = routeManager.UpdateRoutes(testCase.inputSerial, testCase.inputRoutes)
|
|
|
|
require.NoError(t, err, "should update routes with init routes")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = routeManager.UpdateRoutes(testCase.inputSerial+uint64(len(testCase.inputInitRoutes)), testCase.inputRoutes)
|
|
|
|
require.NoError(t, err, "should update routes")
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
require.Len(t, routeManager.clientNetworks, testCase.clientNetworkWatchersExpected, "client networks size should match")
|
2022-09-05 09:06:35 +02:00
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
if runtime.GOOS == "linux" && routeManager.serverRouter != nil {
|
2023-08-12 11:42:36 +02:00
|
|
|
sr := routeManager.serverRouter.(*defaultServerRouter)
|
|
|
|
require.Len(t, sr.routes, testCase.serverRoutesExpected, "server networks size should match")
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|