2024-04-05 20:38:49 +02:00
|
|
|
//go:build !android
|
2024-03-21 16:49:28 +01:00
|
|
|
|
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
2024-04-03 11:11:46 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2024-03-21 16:49:28 +01:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-04-03 11:11:46 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-03-21 16:49:28 +01:00
|
|
|
"testing"
|
|
|
|
|
2024-04-03 11:11:46 +02:00
|
|
|
"github.com/pion/transport/v3/stdnet"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-03-21 16:49:28 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-04-03 11:11:46 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface"
|
2024-03-21 16:49:28 +01:00
|
|
|
)
|
|
|
|
|
2024-04-03 11:11:46 +02:00
|
|
|
func TestAddRemoveRoutes(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
prefix netip.Prefix
|
|
|
|
shouldRouteToWireguard bool
|
|
|
|
shouldBeRemoved bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Should Add And Remove Route 100.66.120.0/24",
|
|
|
|
prefix: netip.MustParsePrefix("100.66.120.0/24"),
|
|
|
|
shouldRouteToWireguard: true,
|
|
|
|
shouldBeRemoved: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Not Add Or Remove Route 127.0.0.1/32",
|
|
|
|
prefix: netip.MustParsePrefix("127.0.0.1/32"),
|
|
|
|
shouldRouteToWireguard: false,
|
|
|
|
shouldBeRemoved: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
peerPrivateKey, _ := wgtypes.GeneratePrivateKey()
|
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, peerPrivateKey.String(), iface.DefaultMTU, newNet, nil)
|
|
|
|
require.NoError(t, err, "should create testing WGIface interface")
|
|
|
|
defer wgInterface.Close()
|
|
|
|
|
|
|
|
err = wgInterface.Create()
|
|
|
|
require.NoError(t, err, "should create testing wireguard interface")
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
err = addToRouteTableIfNoExists(testCase.prefix, wgInterface.Address().IP.String())
|
|
|
|
require.NoError(t, err, "addToRouteTableIfNoExists should not return err")
|
2024-04-03 11:11:46 +02:00
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
prefixGateway, err := getExistingRIBRouteGateway(testCase.prefix)
|
|
|
|
require.NoError(t, err, "getExistingRIBRouteGateway should not return err")
|
2024-04-03 11:11:46 +02:00
|
|
|
if testCase.shouldRouteToWireguard {
|
2024-04-05 20:38:49 +02:00
|
|
|
require.Equal(t, wgInterface.Address().IP.String(), prefixGateway.String(), "route should point to wireguard interface IP")
|
2024-04-03 11:11:46 +02:00
|
|
|
} else {
|
2024-04-05 20:38:49 +02:00
|
|
|
require.NotEqual(t, wgInterface.Address().IP.String(), prefixGateway.String(), "route should point to a different interface")
|
2024-04-03 11:11:46 +02:00
|
|
|
}
|
|
|
|
exists, err := existsInRouteTable(testCase.prefix)
|
|
|
|
require.NoError(t, err, "existsInRouteTable should not return err")
|
|
|
|
if exists && testCase.shouldRouteToWireguard {
|
2024-04-05 20:38:49 +02:00
|
|
|
err = removeFromRouteTableIfNonSystem(testCase.prefix, wgInterface.Address().IP.String())
|
|
|
|
require.NoError(t, err, "removeFromRouteTableIfNonSystem should not return err")
|
2024-04-03 11:11:46 +02:00
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
prefixGateway, err = getExistingRIBRouteGateway(testCase.prefix)
|
|
|
|
require.NoError(t, err, "getExistingRIBRouteGateway should not return err")
|
2024-04-03 11:11:46 +02:00
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
internetGateway, err := getExistingRIBRouteGateway(netip.MustParsePrefix("0.0.0.0/0"))
|
2024-04-03 11:11:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if testCase.shouldBeRemoved {
|
|
|
|
require.Equal(t, internetGateway, prefixGateway, "route should be pointing to default internet gateway")
|
|
|
|
} else {
|
|
|
|
require.NotEqual(t, internetGateway, prefixGateway, "route should be pointing to a different gateway than the internet gateway")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
func TestGetExistingRIBRouteGateway(t *testing.T) {
|
|
|
|
gateway, err := getExistingRIBRouteGateway(netip.MustParsePrefix("0.0.0.0/0"))
|
2024-04-03 11:11:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when fetching the gateway: ", err)
|
|
|
|
}
|
2024-04-05 20:38:49 +02:00
|
|
|
if gateway == nil {
|
2024-04-03 11:11:46 +02:00
|
|
|
t.Fatal("should return a gateway")
|
|
|
|
}
|
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when fetching interface addresses: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testingIP string
|
|
|
|
var testingPrefix netip.Prefix
|
|
|
|
for _, address := range addresses {
|
|
|
|
if address.Network() != "ip+net" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prefix := netip.MustParsePrefix(address.String())
|
|
|
|
if !prefix.Addr().IsLoopback() && prefix.Addr().Is4() {
|
|
|
|
testingIP = prefix.Addr().String()
|
|
|
|
testingPrefix = prefix.Masked()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
localIP, err := getExistingRIBRouteGateway(testingPrefix)
|
2024-04-03 11:11:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error: ", err)
|
|
|
|
}
|
2024-04-05 20:38:49 +02:00
|
|
|
if localIP == nil {
|
2024-04-03 11:11:46 +02:00
|
|
|
t.Fatal("should return a gateway for local network")
|
|
|
|
}
|
|
|
|
if localIP.String() == gateway.String() {
|
|
|
|
t.Fatal("local ip should not match with gateway IP")
|
|
|
|
}
|
|
|
|
if localIP.String() != testingIP {
|
|
|
|
t.Fatalf("local ip should match with testing IP: want %s got %s", testingIP, localIP.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
func TestAddExistAndRemoveRouteNonAndroid(t *testing.T) {
|
|
|
|
defaultGateway, err := getExistingRIBRouteGateway(netip.MustParsePrefix("0.0.0.0/0"))
|
2024-04-03 11:11:46 +02:00
|
|
|
t.Log("defaultGateway: ", defaultGateway)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when fetching the gateway: ", err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
prefix netip.Prefix
|
|
|
|
preExistingPrefix netip.Prefix
|
|
|
|
shouldAddRoute bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Should Add And Remove random Route",
|
|
|
|
prefix: netip.MustParsePrefix("99.99.99.99/32"),
|
|
|
|
shouldAddRoute: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Not Add Route if overlaps with default gateway",
|
|
|
|
prefix: netip.MustParsePrefix(defaultGateway.String() + "/31"),
|
|
|
|
shouldAddRoute: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Add Route if bigger network exists",
|
|
|
|
prefix: netip.MustParsePrefix("100.100.100.0/24"),
|
|
|
|
preExistingPrefix: netip.MustParsePrefix("100.100.0.0/16"),
|
|
|
|
shouldAddRoute: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Add Route if smaller network exists",
|
|
|
|
prefix: netip.MustParsePrefix("100.100.0.0/16"),
|
|
|
|
preExistingPrefix: netip.MustParsePrefix("100.100.100.0/24"),
|
|
|
|
shouldAddRoute: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should Not Add Route if same network exists",
|
|
|
|
prefix: netip.MustParsePrefix("100.100.0.0/16"),
|
|
|
|
preExistingPrefix: netip.MustParsePrefix("100.100.0.0/16"),
|
|
|
|
shouldAddRoute: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, testCase := range testCases {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
log.SetOutput(&buf)
|
|
|
|
defer func() {
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
}()
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
peerPrivateKey, _ := wgtypes.GeneratePrivateKey()
|
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wgInterface, err := iface.NewWGIFace(fmt.Sprintf("utun53%d", n), "100.65.75.2/24", 33100, peerPrivateKey.String(), iface.DefaultMTU, newNet, nil)
|
|
|
|
require.NoError(t, err, "should create testing WGIface interface")
|
|
|
|
defer wgInterface.Close()
|
|
|
|
|
|
|
|
err = wgInterface.Create()
|
|
|
|
require.NoError(t, err, "should create testing wireguard interface")
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
MockAddr := wgInterface.Address().IP.String()
|
|
|
|
|
2024-04-03 11:11:46 +02:00
|
|
|
// Prepare the environment
|
|
|
|
if testCase.preExistingPrefix.IsValid() {
|
2024-04-05 20:38:49 +02:00
|
|
|
err := addToRouteTableIfNoExists(testCase.preExistingPrefix, MockAddr)
|
2024-04-03 11:11:46 +02:00
|
|
|
require.NoError(t, err, "should not return err when adding pre-existing route")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the route
|
2024-04-05 20:38:49 +02:00
|
|
|
err = addToRouteTableIfNoExists(testCase.prefix, MockAddr)
|
2024-04-03 11:11:46 +02:00
|
|
|
require.NoError(t, err, "should not return err when adding route")
|
|
|
|
|
|
|
|
if testCase.shouldAddRoute {
|
|
|
|
// test if route exists after adding
|
|
|
|
ok, err := existsInRouteTable(testCase.prefix)
|
|
|
|
require.NoError(t, err, "should not return err")
|
|
|
|
require.True(t, ok, "route should exist")
|
|
|
|
|
|
|
|
// remove route again if added
|
2024-04-05 20:38:49 +02:00
|
|
|
err = removeFromRouteTableIfNonSystem(testCase.prefix, MockAddr)
|
2024-04-03 11:11:46 +02:00
|
|
|
require.NoError(t, err, "should not return err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// route should either not have been added or should have been removed
|
|
|
|
// In case of already existing route, it should not have been added (but still exist)
|
|
|
|
ok, err := existsInRouteTable(testCase.prefix)
|
|
|
|
t.Log("Buffer string: ", buf.String())
|
|
|
|
require.NoError(t, err, "should not return err")
|
|
|
|
if !strings.Contains(buf.String(), "because it already exists") {
|
|
|
|
require.False(t, ok, "route should not exist")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:38:49 +02:00
|
|
|
func TestExistsInRouteTable(t *testing.T) {
|
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when fetching interface addresses: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addressPrefixes []netip.Prefix
|
|
|
|
for _, address := range addresses {
|
|
|
|
p := netip.MustParsePrefix(address.String())
|
|
|
|
if p.Addr().Is4() {
|
|
|
|
addressPrefixes = append(addressPrefixes, p.Masked())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range addressPrefixes {
|
|
|
|
exists, err := existsInRouteTable(prefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when checking if address exists in route table: ", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
t.Fatalf("address %s should exist in route table", prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 16:49:28 +01:00
|
|
|
func TestIsSubRange(t *testing.T) {
|
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when fetching interface addresses: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var subRangeAddressPrefixes []netip.Prefix
|
|
|
|
var nonSubRangeAddressPrefixes []netip.Prefix
|
|
|
|
for _, address := range addresses {
|
|
|
|
p := netip.MustParsePrefix(address.String())
|
|
|
|
if !p.Addr().IsLoopback() && p.Addr().Is4() && p.Bits() < 32 {
|
|
|
|
p2 := netip.PrefixFrom(p.Masked().Addr(), p.Bits()+1)
|
|
|
|
subRangeAddressPrefixes = append(subRangeAddressPrefixes, p2)
|
|
|
|
nonSubRangeAddressPrefixes = append(nonSubRangeAddressPrefixes, p.Masked())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range subRangeAddressPrefixes {
|
|
|
|
isSubRangePrefix, err := isSubRange(prefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when checking if address is sub-range: ", err)
|
|
|
|
}
|
|
|
|
if !isSubRangePrefix {
|
|
|
|
t.Fatalf("address %s should be sub-range of an existing route in the table", prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range nonSubRangeAddressPrefixes {
|
|
|
|
isSubRangePrefix, err := isSubRange(prefix)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("shouldn't return error when checking if address is sub-range: ", err)
|
|
|
|
}
|
|
|
|
if isSubRangePrefix {
|
|
|
|
t.Fatalf("address %s should not be sub-range of an existing route in the table", prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|