2021-01-05 19:02:52 +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-02-04 21:12:42 +00:00
|
|
|
package ipnlocal
|
2021-01-05 19:02:52 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-04 21:12:42 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-01-05 19:02:52 +00:00
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/tailcfg"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2021-01-05 19:02:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkMapCompare(t *testing.T) {
|
|
|
|
prefix1, err := netaddr.ParseIPPrefix("192.168.0.0/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
node1 := &tailcfg.Node{Addresses: []netaddr.IPPrefix{prefix1}}
|
|
|
|
|
|
|
|
prefix2, err := netaddr.ParseIPPrefix("10.0.0.0/8")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
node2 := &tailcfg.Node{Addresses: []netaddr.IPPrefix{prefix2}}
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-08 17:32:55 +00:00
|
|
|
name string
|
2021-02-05 23:44:46 +00:00
|
|
|
a, b *netmap.NetworkMap
|
2021-01-05 19:02:52 +00:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"both nil",
|
2021-01-05 19:02:52 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"b nil",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{},
|
2021-01-05 19:02:52 +00:00
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"a nil",
|
2021-01-05 19:02:52 +00:00
|
|
|
nil,
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{},
|
2021-01-05 19:02:52 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"both default",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{},
|
|
|
|
&netmap.NetworkMap{},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"names identical",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Name: "map1"},
|
|
|
|
&netmap.NetworkMap{Name: "map1"},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"names differ",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Name: "map1"},
|
|
|
|
&netmap.NetworkMap{Name: "map2"},
|
2021-01-05 19:02:52 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Peers identical",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{}},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Peer list length",
|
2021-01-05 19:02:52 +00:00
|
|
|
// length of Peers list differs
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{{}}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{}},
|
2021-01-05 19:02:52 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Node names identical",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{Name: "A"}}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{Name: "A"}}},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Node names differ",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{Name: "A"}}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{Name: "B"}}},
|
2021-01-05 19:02:52 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Node lists identical",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{node1, node1}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{node1, node1}},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Node lists differ",
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{node1, node1}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{node1, node2}},
|
2021-01-05 19:02:52 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:32:55 +00:00
|
|
|
"Node Users differ",
|
2021-01-05 19:02:52 +00:00
|
|
|
// User field is not checked.
|
2021-02-05 23:44:46 +00:00
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{User: 0}}},
|
|
|
|
&netmap.NetworkMap{Peers: []*tailcfg.Node{&tailcfg.Node{User: 1}}},
|
2021-01-05 19:02:52 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
2021-01-08 17:32:55 +00:00
|
|
|
for _, tt := range tests {
|
2021-01-05 19:02:52 +00:00
|
|
|
got := dnsMapsEqual(tt.a, tt.b)
|
|
|
|
if got != tt.want {
|
2021-01-08 17:32:55 +00:00
|
|
|
t.Errorf("%s: Equal = %v; want %v", tt.name, got, tt.want)
|
2021-01-05 19:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|