2021-06-28 22:16:37 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-11-30 23:53:34 +00:00
|
|
|
package tsdial
|
2021-06-28 22:16:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/netmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDNSMapFromNetworkMap(t *testing.T) {
|
|
|
|
pfx := netaddr.MustParseIPPrefix
|
|
|
|
ip := netaddr.MustParseIP
|
|
|
|
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",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100.102.103.104/32"),
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
},
|
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",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100.102.103.104/32"),
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
Name: "a.tailnet",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100.0.0.201/32"),
|
|
|
|
pfx("100::201/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b.tailnet",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100::202/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100::123/128"),
|
|
|
|
},
|
|
|
|
Peers: []*tailcfg.Node{
|
|
|
|
{
|
|
|
|
Name: "a.tailnet",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100.0.0.201/32"),
|
|
|
|
pfx("100::201/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b.tailnet",
|
|
|
|
Addresses: []netaddr.IPPrefix{
|
|
|
|
pfx("100::202/128"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|