forked from presotto/go-mdns-sd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rrcache_test.go
132 lines (119 loc) · 3.32 KB
/
rrcache_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mdns
import (
"testing"
"time"
dns "github.com/vanadium/go-mdns-sd/go_dns"
)
var (
short []dns.RR = []dns.RR{
&dns.RR_TXT{dns.RR_Header{"x.local.", dns.TypeTXT, dns.ClassINET, 2, 0}, []string{"the rain in spain"}},
&dns.RR_PTR{dns.RR_Header{"x.local.", dns.TypePTR, dns.ClassINET, 2, 0}, "y.local."},
}
long []dns.RR = []dns.RR{
&dns.RR_TXT{dns.RR_Header{"x.local.", dns.TypeTXT, dns.ClassINET, 10000, 0}, []string{"falls mainly on the plain"}},
&dns.RR_PTR{dns.RR_Header{"x.local.", dns.TypePTR, dns.ClassINET, 10000, 0}, "z.local."},
}
override []dns.RR = []dns.RR{
&dns.RR_TXT{dns.RR_Header{"x.local.", dns.TypeTXT, dns.ClassINET | 0x8000, 10000, 0}, []string{"except on tuesday"}},
&dns.RR_PTR{dns.RR_Header{"x.local.", dns.TypePTR, dns.ClassINET | 0x8000, 10000, 0}, "q.local."},
}
goodbye []dns.RR = []dns.RR{
&dns.RR_TXT{dns.RR_Header{"x.local.", dns.TypeTXT, dns.ClassINET, 0, 0}, []string{"except on tuesday"}},
&dns.RR_PTR{dns.RR_Header{"x.local.", dns.TypePTR, dns.ClassINET, 0, 0}, "q.local."},
}
)
// Lookup RRs that match.
func lookup(cache *rrCache, dn string, rrtype uint16) []dns.RR {
rc := make(chan dns.RR, 10)
go func() {
cache.Lookup(dn, rrtype, rc)
close(rc)
}()
rrs := make([]dns.RR, 0)
for rr := <-rc; rr != nil; rr = <-rc {
rrs = append(rrs, rr)
}
return rrs
}
// Compare two lists of RRs.
func compare(a, b []dns.RR) bool {
if len(a) != len(b) {
return false
}
for _, rrb := range b {
found := false
L:
for _, rra := range a {
if rra.Header().Rrtype != rrb.Header().Rrtype {
continue
}
if rra.Header().Ttl != rrb.Header().Ttl {
continue
}
switch rra := rra.(type) {
case *dns.RR_TXT:
rrb := rrb.(*dns.RR_TXT)
if rra.Txt[0] == rrb.Txt[0] {
found = true
break L
}
case *dns.RR_PTR:
rrb := rrb.(*dns.RR_PTR)
if rra.Ptr == rrb.Ptr {
found = true
break L
}
}
}
if !found {
return false
}
}
return true
}
func TestRRCache(t *testing.T) {
cache := newRRCache(*logLevelFlag)
// Cache a number of RRs with short TTLs.
for _, rr := range short {
cache.Add(rr)
}
// Cache a number of RRs with long TTLs.
for _, rr := range long {
cache.Add(rr)
}
// Make sure all the RRs are still there.
x := lookup(cache, "x.local.", dns.TypeALL)
if !compare(x, append(short, long...)) {
t.Errorf("%v != %v", x, append(short, long...))
}
// Lookup only RR_TXT entries
x = lookup(cache, "x.local.", dns.TypeTXT)
if !compare(x, []dns.RR{short[0], long[0]}) {
t.Errorf("%v != %v", x, []dns.RR{short[0], long[0]})
}
// Wait past the short TTL and make sure only the long ones are still there.
time.Sleep(5 * time.Second)
x = lookup(cache, "x.local.", dns.TypeALL)
if !compare(x, long) {
t.Errorf("%v != %v", x, long)
}
// Make sure cache flush works. The new entries should override rather than append.
for _, rr := range override {
cache.Add(rr)
}
x = lookup(cache, "x.local.", dns.TypeALL)
if !compare(x, override) {
t.Errorf("%v != %v", x, override)
}
// Make sure goodbye works. The entries should be deleted after one second.
for _, rr := range goodbye {
cache.Add(rr)
}
time.Sleep(2 * time.Second)
x = lookup(cache, "x.local.", dns.TypeALL)
if len(x) != 0 {
t.Errorf("%v != []", x)
}
}