2020-07-29 01:47:23 +00:00
|
|
|
// Copyright (c) 2020 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-03-25 21:50:21 +00:00
|
|
|
package dns
|
2020-07-29 01:47:23 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-10 15:31:11 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-07-29 01:47:23 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPretty(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
dmap *Map
|
|
|
|
want string
|
|
|
|
}{
|
2020-08-24 21:27:21 +00:00
|
|
|
{"empty", NewMap(nil, nil), ""},
|
2020-07-29 01:47:23 +00:00
|
|
|
{
|
|
|
|
"single",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"hello.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"hello.ipn.dev.\t100.101.102.103\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.domain.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.sub.domain.": netaddr.IPv4(100, 99, 9, 1),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.domain.\t100.101.102.103\ntest2.sub.domain.\t100.99.9.1\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := tt.dmap.Pretty()
|
|
|
|
if tt.want != got {
|
|
|
|
t.Errorf("want %v; got %v", tt.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrettyDiffFrom(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
map1 *Map
|
|
|
|
map2 *Map
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"from_empty",
|
|
|
|
nil,
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"+test1.ipn.dev.\t100.101.102.103\n+test2.ipn.dev.\t100.103.102.101\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"equal",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"changed_ip",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 104, 102, 101),
|
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"-test2.ipn.dev.\t100.103.102.101\n+test2.ipn.dev.\t100.104.102.101\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"new_domain",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test3.ipn.dev.": netaddr.IPv4(100, 105, 106, 107),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"+test3.ipn.dev.\t100.105.106.107\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"gone_domain",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"-test2.ipn.dev.\t100.103.102.101\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"mixed",
|
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 101, 102, 103),
|
|
|
|
"test4.ipn.dev.": netaddr.IPv4(100, 107, 106, 105),
|
|
|
|
"test5.ipn.dev.": netaddr.IPv4(100, 64, 1, 1),
|
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 103, 102, 101),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-07-29 01:47:23 +00:00
|
|
|
NewMap(map[string]netaddr.IP{
|
2020-08-06 18:25:28 +00:00
|
|
|
"test2.ipn.dev.": netaddr.IPv4(100, 104, 102, 101),
|
|
|
|
"test1.ipn.dev.": netaddr.IPv4(100, 100, 101, 102),
|
|
|
|
"test3.ipn.dev.": netaddr.IPv4(100, 64, 1, 1),
|
2020-08-24 21:27:21 +00:00
|
|
|
}, nil),
|
2020-08-06 18:25:28 +00:00
|
|
|
"-test1.ipn.dev.\t100.101.102.103\n+test1.ipn.dev.\t100.100.101.102\n" +
|
|
|
|
"-test2.ipn.dev.\t100.103.102.101\n+test2.ipn.dev.\t100.104.102.101\n" +
|
|
|
|
"+test3.ipn.dev.\t100.64.1.1\n-test4.ipn.dev.\t100.107.106.105\n-test5.ipn.dev.\t100.64.1.1\n",
|
2020-07-29 01:47:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := tt.map2.PrettyDiffFrom(tt.map1)
|
|
|
|
if tt.want != got {
|
|
|
|
t.Errorf("want %v; got %v", tt.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-03-10 15:31:11 +00:00
|
|
|
|
|
|
|
t.Run("truncated", func(t *testing.T) {
|
|
|
|
small := NewMap(nil, nil)
|
|
|
|
m := map[string]netaddr.IP{}
|
|
|
|
for i := 0; i < 5000; i++ {
|
|
|
|
m[fmt.Sprintf("host%d.ipn.dev.", i)] = netaddr.IPv4(100, 64, 1, 1)
|
|
|
|
}
|
|
|
|
veryBig := NewMap(m, nil)
|
|
|
|
diff := veryBig.PrettyDiffFrom(small)
|
|
|
|
if len(diff) > 3<<10 {
|
|
|
|
t.Errorf("pretty diff too large: %d bytes", len(diff))
|
|
|
|
}
|
|
|
|
if !strings.Contains(diff, "truncated") {
|
|
|
|
t.Errorf("big diff not truncated")
|
|
|
|
}
|
|
|
|
})
|
2020-07-29 01:47:23 +00:00
|
|
|
}
|