mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-14 17:28:56 +02:00
[client] Replace string to netip.Prefix (#3362)
Replace string to netip.Prefix --------- Co-authored-by: Hakan Sariman <hknsrmn46@gmail.com>
This commit is contained in:
@ -26,6 +26,7 @@ import (
|
||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||
|
||||
"github.com/netbirdio/management-integrations/integrations"
|
||||
|
||||
"github.com/netbirdio/netbird/client/iface"
|
||||
"github.com/netbirdio/netbird/client/iface/bind"
|
||||
"github.com/netbirdio/netbird/client/iface/configurer"
|
||||
@ -77,7 +78,7 @@ type MockWGIface struct {
|
||||
ToInterfaceFunc func() *net.Interface
|
||||
UpFunc func() (*bind.UniversalUDPMuxDefault, error)
|
||||
UpdateAddrFunc func(newAddr string) error
|
||||
UpdatePeerFunc func(peerKey string, allowedIps string, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error
|
||||
UpdatePeerFunc func(peerKey string, allowedIps []netip.Prefix, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error
|
||||
RemovePeerFunc func(peerKey string) error
|
||||
AddAllowedIPFunc func(peerKey string, allowedIP string) error
|
||||
RemoveAllowedIPFunc func(peerKey string, allowedIP string) error
|
||||
@ -128,7 +129,7 @@ func (m *MockWGIface) UpdateAddr(newAddr string) error {
|
||||
return m.UpdateAddrFunc(newAddr)
|
||||
}
|
||||
|
||||
func (m *MockWGIface) UpdatePeer(peerKey string, allowedIps string, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error {
|
||||
func (m *MockWGIface) UpdatePeer(peerKey string, allowedIps []netip.Prefix, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error {
|
||||
return m.UpdatePeerFunc(peerKey, allowedIps, keepAlive, endpoint, preSharedKey)
|
||||
}
|
||||
|
||||
@ -534,7 +535,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
|
||||
t.Errorf("expecting Engine.peerConns to contain peer %s", p)
|
||||
}
|
||||
expectedAllowedIPs := strings.Join(p.AllowedIps, ",")
|
||||
if conn.WgConfig().AllowedIps != expectedAllowedIPs {
|
||||
if !compareNetIPLists(conn.WgConfig().AllowedIps, p.AllowedIps) {
|
||||
t.Errorf("expecting peer %s to have AllowedIPs= %s, got %s", p.GetWgPubKey(),
|
||||
expectedAllowedIPs, conn.WgConfig().AllowedIps)
|
||||
}
|
||||
@ -1237,6 +1238,91 @@ func Test_CheckFilesEqual(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareNetIPLists(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
list1 []netip.Prefix
|
||||
list2 []string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "both empty",
|
||||
list1: []netip.Prefix{},
|
||||
list2: []string{},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "single match ipv4",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.0.0/24")},
|
||||
list2: []string{"192.168.0.0/24"},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "multiple match ipv4, different order",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24"), netip.MustParsePrefix("10.0.0.0/8")},
|
||||
list2: []string{"10.0.0.0/8", "192.168.1.0/24"},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "ipv4 mismatch due to extra element in list2",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")},
|
||||
list2: []string{"192.168.1.0/24", "10.0.0.0/8"},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "ipv4 mismatch due to duplicate count",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24"), netip.MustParsePrefix("192.168.1.0/24")},
|
||||
list2: []string{"192.168.1.0/24"},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "invalid prefix in list2",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")},
|
||||
list2: []string{"invalid-prefix"},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "ipv4 mismatch because different prefixes",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")},
|
||||
list2: []string{"10.0.0.0/8"},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "single match ipv6",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("2001:db8::/32")},
|
||||
list2: []string{"2001:db8::/32"},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "multiple match ipv6, different order",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("2001:db8::/32"), netip.MustParsePrefix("fe80::/10")},
|
||||
list2: []string{"fe80::/10", "2001:db8::/32"},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "mixed ipv4 and ipv6 match",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24"), netip.MustParsePrefix("2001:db8::/32")},
|
||||
list2: []string{"2001:db8::/32", "192.168.1.0/24"},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "ipv6 mismatch with invalid prefix",
|
||||
list1: []netip.Prefix{netip.MustParsePrefix("2001:db8::/32")},
|
||||
list2: []string{"invalid-ipv6"},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := compareNetIPLists(tt.list1, tt.list2)
|
||||
if result != tt.expected {
|
||||
t.Errorf("compareNetIPLists(%v, %v) = %v; want %v", tt.list1, tt.list2, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func createEngine(ctx context.Context, cancel context.CancelFunc, setupKey string, i int, mgmtAddr string, signalAddr string) (*Engine, error) {
|
||||
key, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user