2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-24 04:23:59 +00:00
|
|
|
|
|
|
|
package router
|
|
|
|
|
2022-06-28 22:32:09 +00:00
|
|
|
import (
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-06-28 22:32:09 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"tailscale.com/types/preftype"
|
|
|
|
)
|
2021-04-24 04:23:59 +00:00
|
|
|
|
2023-12-22 01:40:03 +00:00
|
|
|
//lint:ignore U1000 used in Windows/Linux tests only
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func mustCIDRs(ss ...string) []netip.Prefix {
|
|
|
|
var ret []netip.Prefix
|
2021-04-24 04:23:59 +00:00
|
|
|
for _, s := range ss {
|
2022-07-26 03:55:44 +00:00
|
|
|
ret = append(ret, netip.MustParsePrefix(s))
|
2021-04-24 04:23:59 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2022-06-28 22:32:09 +00:00
|
|
|
|
|
|
|
func TestConfigEqual(t *testing.T) {
|
|
|
|
testedFields := []string{
|
2023-07-18 21:51:32 +00:00
|
|
|
"LocalAddrs", "Routes", "LocalRoutes", "NewMTU",
|
|
|
|
"SubnetRoutes", "SNATSubnetRoutes", "NetfilterMode",
|
2023-12-04 17:08:56 +00:00
|
|
|
"NetfilterKind",
|
2022-06-28 22:32:09 +00:00
|
|
|
}
|
2024-02-09 01:34:22 +00:00
|
|
|
configType := reflect.TypeFor[Config]()
|
2022-06-28 22:32:09 +00:00
|
|
|
configFields := []string{}
|
|
|
|
for i := 0; i < configType.NumField(); i++ {
|
|
|
|
configFields = append(configFields, configType.Field(i).Name)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(configFields, testedFields) {
|
|
|
|
t.Errorf("Config.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
|
|
|
|
configFields, testedFields)
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
nets := func(strs ...string) (ns []netip.Prefix) {
|
2022-06-28 22:32:09 +00:00
|
|
|
for _, s := range strs {
|
2022-07-26 03:55:44 +00:00
|
|
|
n, err := netip.ParsePrefix(s)
|
2022-06-28 22:32:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ns = append(ns, n)
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
a, b *Config
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
&Config{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{},
|
|
|
|
&Config{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{LocalAddrs: nets("100.1.27.82/32")},
|
|
|
|
&Config{LocalAddrs: nets("100.2.19.82/32")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{LocalAddrs: nets("100.1.27.82/32")},
|
|
|
|
&Config{LocalAddrs: nets("100.1.27.82/32")},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{Routes: nets("100.1.27.0/24")},
|
|
|
|
&Config{Routes: nets("100.2.19.0/24")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{Routes: nets("100.2.19.0/24")},
|
|
|
|
&Config{Routes: nets("100.2.19.0/24")},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{LocalRoutes: nets("100.1.27.0/24")},
|
|
|
|
&Config{LocalRoutes: nets("100.2.19.0/24")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{LocalRoutes: nets("100.1.27.0/24")},
|
|
|
|
&Config{LocalRoutes: nets("100.1.27.0/24")},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{SubnetRoutes: nets("100.1.27.0/24")},
|
|
|
|
&Config{SubnetRoutes: nets("100.2.19.0/24")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{SubnetRoutes: nets("100.1.27.0/24")},
|
|
|
|
&Config{SubnetRoutes: nets("100.1.27.0/24")},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{SNATSubnetRoutes: false},
|
|
|
|
&Config{SNATSubnetRoutes: true},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{SNATSubnetRoutes: false},
|
|
|
|
&Config{SNATSubnetRoutes: false},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Config{NetfilterMode: preftype.NetfilterOff},
|
|
|
|
&Config{NetfilterMode: preftype.NetfilterNoDivert},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{NetfilterMode: preftype.NetfilterNoDivert},
|
|
|
|
&Config{NetfilterMode: preftype.NetfilterNoDivert},
|
|
|
|
true,
|
|
|
|
},
|
2023-07-18 21:51:32 +00:00
|
|
|
{
|
|
|
|
&Config{NewMTU: 0},
|
|
|
|
&Config{NewMTU: 0},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Config{NewMTU: 1280},
|
|
|
|
&Config{NewMTU: 0},
|
|
|
|
false,
|
|
|
|
},
|
2022-06-28 22:32:09 +00:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
got := tt.a.Equal(tt.b)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|