2022-08-18 18:22:15 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-03-13 15:14:18 +01:00
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
"github.com/rs/xid"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-19 11:42:25 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
"github.com/netbirdio/netbird/route"
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
const (
|
|
|
|
peer1Key = "BhRPtynAAYRDy08+q4HTMsos8fs4plTP4NOSh7C1ry8="
|
|
|
|
peer2Key = "/yF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5NyI="
|
2023-02-03 10:33:28 +01:00
|
|
|
peer1ID = "peer-1-id"
|
|
|
|
peer2ID = "peer-2-id"
|
2022-12-06 10:11:57 +01:00
|
|
|
routeGroup1 = "routeGroup1"
|
|
|
|
routeGroup2 = "routeGroup2"
|
|
|
|
routeInvalidGroup1 = "routeInvalidGroup1"
|
2023-01-02 15:11:32 +01:00
|
|
|
userID = "testingUser"
|
2022-12-06 10:11:57 +01:00
|
|
|
)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
func TestCreateRoute(t *testing.T) {
|
|
|
|
type input struct {
|
2022-08-22 14:10:24 +02:00
|
|
|
network string
|
|
|
|
netID string
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey string
|
2022-08-18 18:22:15 +02:00
|
|
|
description string
|
|
|
|
masquerade bool
|
|
|
|
metric int
|
|
|
|
enabled bool
|
2022-12-06 10:11:57 +01:00
|
|
|
groups []string
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
inputArgs input
|
|
|
|
shouldCreate bool
|
|
|
|
errFunc require.ErrorAssertionFunc
|
|
|
|
expectedRoute *route.Route
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Happy Path",
|
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
NetID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Prefix Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/34",
|
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Peer Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: "notExistingPeer",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Empty Peer Should Create",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: "",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: false,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
|
|
|
NetID: "happy",
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: "",
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: false,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Large Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/16",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "happy",
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 99999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Small Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
inputArgs: input{
|
2022-08-22 14:10:24 +02:00
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "happy",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 0,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2022-08-22 14:10:24 +02:00
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Large NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
inputArgs: input{
|
|
|
|
network: "192.168.0.0/16",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
netID: "12345678901234567890qwertyuiopqwertyuiop1",
|
|
|
|
description: "super",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Small NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
inputArgs: input{
|
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group List Should Fail",
|
|
|
|
inputArgs: input{
|
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group ID string Should Fail",
|
|
|
|
inputArgs: input{
|
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{""},
|
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Group Should Fail",
|
|
|
|
inputArgs: input{
|
|
|
|
network: "192.168.0.0/16",
|
|
|
|
netID: "NewId",
|
2023-02-03 10:33:28 +01:00
|
|
|
peerKey: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
description: "",
|
|
|
|
masquerade: false,
|
|
|
|
metric: 9999,
|
|
|
|
enabled: true,
|
|
|
|
groups: []string{routeInvalidGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
errFunc: require.Error,
|
|
|
|
shouldCreate: false,
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
outRoute, err := am.CreateRoute(
|
|
|
|
account.Id,
|
2022-08-22 14:10:24 +02:00
|
|
|
testCase.inputArgs.network,
|
2023-02-03 10:33:28 +01:00
|
|
|
testCase.inputArgs.peerKey,
|
2022-08-18 18:22:15 +02:00
|
|
|
testCase.inputArgs.description,
|
2022-08-22 14:10:24 +02:00
|
|
|
testCase.inputArgs.netID,
|
2022-08-18 18:22:15 +02:00
|
|
|
testCase.inputArgs.masquerade,
|
|
|
|
testCase.inputArgs.metric,
|
2022-12-06 10:11:57 +01:00
|
|
|
testCase.inputArgs.groups,
|
2022-08-18 18:22:15 +02:00
|
|
|
testCase.inputArgs.enabled,
|
2023-01-25 16:29:59 +01:00
|
|
|
userID,
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
testCase.errFunc(t, err)
|
|
|
|
|
|
|
|
if !testCase.shouldCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign generated ID
|
|
|
|
testCase.expectedRoute.ID = outRoute.ID
|
|
|
|
|
|
|
|
if !testCase.expectedRoute.IsEqual(outRoute) {
|
|
|
|
t.Errorf("new route didn't match expected route:\nGot %#v\nExpected:%#v\n", outRoute, testCase.expectedRoute)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveRoute(t *testing.T) {
|
2023-02-03 10:33:28 +01:00
|
|
|
validPeer := peer2ID
|
2022-08-18 18:22:15 +02:00
|
|
|
invalidPeer := "nonExisting"
|
|
|
|
validPrefix := netip.MustParsePrefix("192.168.0.0/24")
|
|
|
|
invalidPrefix, _ := netip.ParsePrefix("192.168.0.0/34")
|
|
|
|
validMetric := 1000
|
|
|
|
invalidMetric := 99999
|
2022-08-22 14:10:24 +02:00
|
|
|
validNetID := "12345678901234567890qw"
|
|
|
|
invalidNetID := "12345678901234567890qwertyuiopqwertyuiop1"
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
existingRoute *route.Route
|
|
|
|
newPeer *string
|
|
|
|
newMetric *int
|
|
|
|
newPrefix *netip.Prefix
|
2022-12-06 10:11:57 +01:00
|
|
|
newGroups []string
|
2022-08-18 18:22:15 +02:00
|
|
|
skipCopying bool
|
|
|
|
shouldCreate bool
|
|
|
|
errFunc require.ErrorAssertionFunc
|
|
|
|
expectedRoute *route.Route
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Happy Path",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPeer: &validPeer,
|
|
|
|
newMetric: &validMetric,
|
|
|
|
newPrefix: &validPrefix,
|
2022-12-06 10:11:57 +01:00
|
|
|
newGroups: []string{routeGroup2},
|
2022-08-18 18:22:15 +02:00
|
|
|
errFunc: require.NoError,
|
|
|
|
shouldCreate: true,
|
|
|
|
expectedRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: validPrefix,
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: validPeer,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: validMetric,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup2},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Prefix Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPrefix: &invalidPrefix,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Bad Peer Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newPeer: &invalidPeer,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Invalid Metric Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-22 14:10:24 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-22 14:10:24 +02:00
|
|
|
},
|
|
|
|
newMetric: &invalidMetric,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Invalid NetID Should Fail",
|
2022-08-22 14:10:24 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: invalidNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
newMetric: &invalidMetric,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
2022-12-06 10:11:57 +01:00
|
|
|
name: "Nil Route Should Fail",
|
2022-08-18 18:22:15 +02:00
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
},
|
|
|
|
skipCopying: true,
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2022-12-06 10:11:57 +01:00
|
|
|
{
|
|
|
|
name: "Empty Group List Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty Group ID String Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{""},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid Group Should Fail",
|
|
|
|
existingRoute: &route.Route{
|
|
|
|
ID: "testingRoute",
|
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: validNetID,
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-12-06 10:11:57 +01:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
Groups: []string{routeGroup1},
|
|
|
|
},
|
|
|
|
newGroups: []string{routeInvalidGroup1},
|
|
|
|
errFunc: require.Error,
|
|
|
|
},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Routes[testCase.existingRoute.ID] = testCase.existingRoute
|
|
|
|
|
|
|
|
err = am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("account should be saved")
|
|
|
|
}
|
|
|
|
|
|
|
|
var routeToSave *route.Route
|
|
|
|
|
|
|
|
if !testCase.skipCopying {
|
|
|
|
routeToSave = testCase.existingRoute.Copy()
|
|
|
|
if testCase.newPeer != nil {
|
|
|
|
routeToSave.Peer = *testCase.newPeer
|
|
|
|
}
|
|
|
|
if testCase.newMetric != nil {
|
|
|
|
routeToSave.Metric = *testCase.newMetric
|
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.newPrefix != nil {
|
2022-08-22 14:10:24 +02:00
|
|
|
routeToSave.Network = *testCase.newPrefix
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2022-12-06 10:11:57 +01:00
|
|
|
|
|
|
|
if testCase.newGroups != nil {
|
|
|
|
routeToSave.Groups = testCase.newGroups
|
|
|
|
}
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
err = am.SaveRoute(account.Id, userID, routeToSave)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
testCase.errFunc(t, err)
|
|
|
|
|
|
|
|
if !testCase.shouldCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
account, err = am.Store.GetAccount(account.Id)
|
2022-11-07 12:10:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
savedRoute, saved := account.Routes[testCase.expectedRoute.ID]
|
|
|
|
require.True(t, saved)
|
|
|
|
|
|
|
|
if !testCase.expectedRoute.IsEqual(savedRoute) {
|
|
|
|
t.Errorf("new route didn't match expected route:\nGot %#v\nExpected:%#v\n", savedRoute, testCase.expectedRoute)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRoute(t *testing.T) {
|
|
|
|
testingRoute := &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetworkType: route.IPv4Network,
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: peer1Key,
|
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Routes[testingRoute.ID] = testingRoute
|
|
|
|
|
|
|
|
err = am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to save account")
|
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
err = am.DeleteRoute(account.Id, testingRoute.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("deleting route failed with error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
savedAccount, err := am.Store.GetAccount(account.Id)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to retrieve saved account with error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, found := savedAccount.Routes[testingRoute.ID]
|
|
|
|
if found {
|
|
|
|
t.Error("route shouldn't be found after delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNetworkMap_RouteSync(t *testing.T) {
|
|
|
|
// no routes for peer in different groups
|
|
|
|
// no routes when route is deleted
|
|
|
|
baseRoute := &route.Route{
|
|
|
|
ID: "testingRoute",
|
2022-08-22 14:10:24 +02:00
|
|
|
Network: netip.MustParsePrefix("192.168.0.0/16"),
|
|
|
|
NetID: "superNet",
|
|
|
|
NetworkType: route.IPv4Network,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peer: peer1ID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Description: "super",
|
|
|
|
Masquerade: false,
|
|
|
|
Metric: 9999,
|
|
|
|
Enabled: true,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: []string{routeGroup1},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
am, err := createRouterManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := initTestRouteAccount(t, am)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
newAccountRoutes, err := am.GetNetworkMap(peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newAccountRoutes.Routes, 0, "new accounts should have no routes")
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
createdRoute, err := am.CreateRoute(account.Id, baseRoute.Network.String(), peer1ID,
|
2023-01-25 16:29:59 +01:00
|
|
|
baseRoute.Description, baseRoute.NetID, baseRoute.Masquerade, baseRoute.Metric, baseRoute.Groups, false,
|
|
|
|
userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
noDisabledRoutes, err := am.GetNetworkMap(peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, noDisabledRoutes.Routes, 0, "no routes for disabled routes")
|
|
|
|
|
|
|
|
enabledRoute := createdRoute.Copy()
|
|
|
|
enabledRoute.Enabled = true
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
// network map contains route.Route objects that have Route.Peer field filled with Peer.Key instead of Peer.ID
|
|
|
|
expectedRoute := enabledRoute.Copy()
|
|
|
|
expectedRoute.Peer = peer1Key
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
err = am.SaveRoute(account.Id, userID, enabledRoute)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer1Routes, err := am.GetNetworkMap(peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1Routes.Routes, 1, "we should receive one route for peer1")
|
2023-02-03 10:33:28 +01:00
|
|
|
require.True(t, expectedRoute.IsEqual(peer1Routes.Routes[0]), "received route should be equal")
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer2Routes, err := am.GetNetworkMap(peer2ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.Len(t, peer2Routes.Routes, 0, "no routes for peers not in the distribution group")
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.GroupAddPeer(account.Id, routeGroup1, peer2ID)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer2Routes, err = am.GetNetworkMap(peer2ID)
|
2022-12-06 10:11:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer2Routes.Routes, 1, "we should receive one route")
|
2022-08-18 18:22:15 +02:00
|
|
|
require.True(t, peer1Routes.Routes[0].IsEqual(peer2Routes.Routes[0]), "routes should be the same for peers in the same group")
|
|
|
|
|
|
|
|
newGroup := &Group{
|
|
|
|
ID: xid.New().String(),
|
|
|
|
Name: "peer1 group",
|
2023-02-03 10:33:28 +01:00
|
|
|
Peers: []string{peer1ID},
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
err = am.SaveGroup(account.Id, userID, newGroup)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
rules, err := am.ListPolicies(account.Id, "testingUser")
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defaultRule := rules[0]
|
2023-03-13 15:14:18 +01:00
|
|
|
newPolicy := defaultRule.Copy()
|
|
|
|
newPolicy.ID = xid.New().String()
|
|
|
|
newPolicy.Name = "peer1 only"
|
|
|
|
newPolicy.Rules[0].Sources = []string{newGroup.ID}
|
|
|
|
newPolicy.Rules[0].Destinations = []string{newGroup.ID}
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
err = am.SavePolicy(account.Id, userID, newPolicy)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
err = am.DeletePolicy(account.Id, defaultRule.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer1GroupRoutes, err := am.GetNetworkMap(peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1GroupRoutes.Routes, 1, "we should receive one route for peer1")
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer2GroupRoutes, err := am.GetNetworkMap(peer2ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer2GroupRoutes.Routes, 0, "we should not receive routes for peer2")
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
err = am.DeleteRoute(account.Id, enabledRoute.ID, userID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
peer1DeletedRoute, err := am.GetNetworkMap(peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, peer1DeletedRoute.Routes, 0, "we should receive one route for peer1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRouterManager(t *testing.T) (*DefaultAccountManager, error) {
|
|
|
|
store, err := createRouterStore(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
2023-09-19 18:08:40 +02:00
|
|
|
return BuildManager(store, NewPeersUpdateManager(), nil, "", "", eventStore, false)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createRouterStore(t *testing.T) (Store, error) {
|
|
|
|
dataDir := t.TempDir()
|
2023-05-19 11:42:25 +02:00
|
|
|
store, err := NewFileStore(dataDir, nil)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestRouteAccount(t *testing.T, am *DefaultAccountManager) (*Account, error) {
|
2023-02-03 10:33:28 +01:00
|
|
|
accountID := "testingAcc"
|
|
|
|
domain := "example.com"
|
|
|
|
|
|
|
|
account := newAccountWithId(accountID, userID, domain)
|
|
|
|
err := am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := account.getTakenIPs()
|
|
|
|
peer1IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
peer1 := &Peer{
|
2023-02-03 10:33:28 +01:00
|
|
|
IP: peer1IP,
|
|
|
|
ID: peer1ID,
|
|
|
|
Key: peer1Key,
|
|
|
|
Name: "test-host1@netbird.io",
|
|
|
|
UserID: userID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Meta: PeerSystemMeta{
|
|
|
|
Hostname: "test-host1@netbird.io",
|
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
account.Peers[peer1.ID] = peer1
|
|
|
|
|
|
|
|
ips = account.getTakenIPs()
|
|
|
|
peer2IP, err := AllocatePeerIP(account.Network.Net, ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
peer2 := &Peer{
|
2023-02-03 10:33:28 +01:00
|
|
|
IP: peer2IP,
|
|
|
|
ID: peer2ID,
|
|
|
|
Key: peer2Key,
|
|
|
|
Name: "test-host2@netbird.io",
|
|
|
|
UserID: userID,
|
2022-08-18 18:22:15 +02:00
|
|
|
Meta: PeerSystemMeta{
|
|
|
|
Hostname: "test-host2@netbird.io",
|
|
|
|
GoOS: "linux",
|
|
|
|
Kernel: "Linux",
|
|
|
|
Core: "21.04",
|
|
|
|
Platform: "x86_64",
|
|
|
|
OS: "Ubuntu",
|
|
|
|
WtVersion: "development",
|
|
|
|
UIVersion: "development",
|
|
|
|
},
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
account.Peers[peer2.ID] = peer2
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.Store.SaveAccount(account)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
groupAll, err := account.GetGroupAll()
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.GroupAddPeer(accountID, groupAll.ID, peer1ID)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.GroupAddPeer(accountID, groupAll.ID, peer2ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
newGroup := &Group{
|
|
|
|
ID: routeGroup1,
|
|
|
|
Name: routeGroup1,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peers: []string{peer1.ID},
|
2022-12-06 10:11:57 +01:00
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
err = am.SaveGroup(accountID, userID, newGroup)
|
2022-12-06 10:11:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newGroup = &Group{
|
|
|
|
ID: routeGroup2,
|
|
|
|
Name: routeGroup2,
|
2023-02-03 10:33:28 +01:00
|
|
|
Peers: []string{peer2.ID},
|
2022-12-06 10:11:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = am.SaveGroup(accountID, userID, newGroup)
|
2022-12-06 10:11:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
return am.Store.GetAccount(account.Id)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|