2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-06-28 22:16:37 +00:00
|
|
|
|
2021-11-30 23:53:34 +00:00
|
|
|
package tsdial
|
2021-06-28 22:16:37 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2021-06-28 22:16:37 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/netmap"
|
|
|
|
)
|
|
|
|
|
2023-08-18 14:57:44 +00:00
|
|
|
func nodeViews(v []*tailcfg.Node) []tailcfg.NodeView {
|
|
|
|
nv := make([]tailcfg.NodeView, len(v))
|
|
|
|
for i, n := range v {
|
|
|
|
nv[i] = n.View()
|
|
|
|
}
|
|
|
|
return nv
|
|
|
|
}
|
|
|
|
|
2021-06-28 22:16:37 +00:00
|
|
|
func TestDNSMapFromNetworkMap(t *testing.T) {
|
2022-07-26 03:55:44 +00:00
|
|
|
pfx := netip.MustParsePrefix
|
|
|
|
ip := netip.MustParseAddr
|
2021-06-28 22:16:37 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
nm *netmap.NetworkMap
|
2021-12-02 19:10:35 +00:00
|
|
|
want dnsMap
|
2021-06-28 22:16:37 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "self",
|
|
|
|
nm: &netmap.NetworkMap{
|
|
|
|
Name: "foo.tailnet",
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
pfx("100.102.103.104/32"),
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2021-06-28 22:16:37 +00:00
|
|
|
},
|
2021-12-02 19:10:35 +00:00
|
|
|
want: dnsMap{
|
2021-06-28 22:16:37 +00:00
|
|
|
"foo": ip("100.102.103.104"),
|
|
|
|
"foo.tailnet": ip("100.102.103.104"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_and_peers",
|
|
|
|
nm: &netmap.NetworkMap{
|
|
|
|
Name: "foo.tailnet",
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
pfx("100.102.103.104/32"),
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2023-08-18 14:57:44 +00:00
|
|
|
Peers: []tailcfg.NodeView{
|
|
|
|
(&tailcfg.Node{
|
2021-06-28 22:16:37 +00:00
|
|
|
Name: "a.tailnet",
|
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
|
|
|
Addresses: []netip.Prefix{
|
2021-06-28 22:16:37 +00:00
|
|
|
pfx("100.0.0.201/32"),
|
|
|
|
pfx("100::201/128"),
|
|
|
|
},
|
2023-08-18 14:57:44 +00:00
|
|
|
}).View(),
|
|
|
|
(&tailcfg.Node{
|
2021-06-28 22:16:37 +00:00
|
|
|
Name: "b.tailnet",
|
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
|
|
|
Addresses: []netip.Prefix{
|
2021-06-28 22:16:37 +00:00
|
|
|
pfx("100::202/128"),
|
|
|
|
},
|
2023-08-18 14:57:44 +00:00
|
|
|
}).View(),
|
2021-06-28 22:16:37 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-02 19:10:35 +00:00
|
|
|
want: dnsMap{
|
2021-06-28 22:16:37 +00:00
|
|
|
"foo": ip("100.102.103.104"),
|
|
|
|
"foo.tailnet": ip("100.102.103.104"),
|
|
|
|
"a": ip("100.0.0.201"),
|
|
|
|
"a.tailnet": ip("100.0.0.201"),
|
|
|
|
"b": ip("100::202"),
|
|
|
|
"b.tailnet": ip("100::202"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "self_has_v6_only",
|
|
|
|
nm: &netmap.NetworkMap{
|
|
|
|
Name: "foo.tailnet",
|
2023-09-18 06:31:34 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
|
|
|
Addresses: []netip.Prefix{
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
}).View(),
|
2023-08-18 14:57:44 +00:00
|
|
|
Peers: nodeViews([]*tailcfg.Node{
|
2021-06-28 22:16:37 +00:00
|
|
|
{
|
|
|
|
Name: "a.tailnet",
|
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
|
|
|
Addresses: []netip.Prefix{
|
2021-06-28 22:16:37 +00:00
|
|
|
pfx("100.0.0.201/32"),
|
|
|
|
pfx("100::201/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b.tailnet",
|
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
|
|
|
Addresses: []netip.Prefix{
|
2021-06-28 22:16:37 +00:00
|
|
|
pfx("100::202/128"),
|
|
|
|
},
|
|
|
|
},
|
2023-08-18 14:57:44 +00:00
|
|
|
}),
|
2021-06-28 22:16:37 +00:00
|
|
|
},
|
2021-12-02 19:10:35 +00:00
|
|
|
want: dnsMap{
|
2021-06-28 22:16:37 +00:00
|
|
|
"foo": ip("100::123"),
|
|
|
|
"foo.tailnet": ip("100::123"),
|
|
|
|
"a": ip("100::201"),
|
|
|
|
"a.tailnet": ip("100::201"),
|
|
|
|
"b": ip("100::202"),
|
|
|
|
"b.tailnet": ip("100::202"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-12-02 19:10:35 +00:00
|
|
|
got := dnsMapFromNetworkMap(tt.nm)
|
2021-06-28 22:16:37 +00:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("mismatch:\n got %v\nwant %v\n", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|