-
Notifications
You must be signed in to change notification settings - Fork 193
/
zones_closest_test.go
99 lines (80 loc) · 2.3 KB
/
zones_closest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package zones
import (
"net"
"reflect"
"sort"
"testing"
"github.com/miekg/dns"
)
func TestClosest(t *testing.T) {
muxm := loadZones(t)
t.Log("test closests")
tests := []struct {
Label string
ClientIP string
ExpectedA []string
QType uint16
MaxHosts int
}{
{"closest", "212.237.144.84", []string{"194.106.223.155"}, dns.TypeA, 1},
{"closest", "208.113.157.108", []string{"207.171.7.49", "207.171.7.59"}, dns.TypeA, 2},
{"closest", "2620:0:872::1", []string{"2607:f238:3::1:45"}, dns.TypeAAAA, 1},
// {"closest", "208.113.157.108", []string{"207.171.7.59"}, 1},
}
for _, x := range tests {
ip := net.ParseIP(x.ClientIP)
if ip == nil {
t.Fatalf("Invalid ClientIP: %s", x.ClientIP)
}
tz := muxm.zonelist["test.example.com"]
targets, netmask, location := tz.Options.Targeting.GetTargets(ip, true)
t.Logf("targets: %q, netmask: %d, location: %+v", targets, netmask, location)
// This is a weird API, but it's what serve() uses now. Fixing it
// isn't super straight forward. Moving some of the exceptions from serve()
// into configuration and making the "find the best answer" code have
// a better API should be possible though. Some day.
labelMatches := tz.FindLabels(
x.Label,
targets,
[]uint16{dns.TypeMF, dns.TypeCNAME, x.QType},
)
if len(labelMatches) == 0 {
t.Fatalf("no labelmatches")
}
for _, match := range labelMatches {
label := match.Label
labelQtype := match.Type
records := tz.Picker(label, labelQtype, x.MaxHosts, location)
if records == nil {
t.Fatalf("didn't get closest records")
}
if len(x.ExpectedA) == 0 {
if len(records) > 0 {
t.Logf("Expected 0 records but got %d", len(records))
t.Fail()
}
}
if len(x.ExpectedA) != len(records) {
t.Logf("Expected %d records, got %d", len(x.ExpectedA), len(records))
t.Fail()
}
ips := []string{}
for _, r := range records {
switch rr := r.RR.(type) {
case *dns.A:
ips = append(ips, rr.A.String())
case *dns.AAAA:
ips = append(ips, rr.AAAA.String())
default:
t.Fatalf("unexpected RR type: %s", rr.Header().String())
}
}
sort.Strings(ips)
sort.Strings(x.ExpectedA)
if !reflect.DeepEqual(ips, x.ExpectedA) {
t.Logf("Got '%+v', expected '%+v'", ips, x.ExpectedA)
t.Fail()
}
}
}
}