forked from semihalev/sdns
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns_cache_test.go
50 lines (37 loc) · 994 Bytes
/
ns_cache_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
package authcache
import (
"testing"
"time"
"github.com/miekg/dns"
"github.com/semihalev/sdns/cache"
"github.com/stretchr/testify/assert"
)
func Test_NSCache(t *testing.T) {
nscache := NewNSCache()
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn("example.com."), dns.TypeA)
key := cache.Hash(m.Question[0])
a := NewAuthServer("0.0.0.0:53", IPv4)
_ = a.String()
servers := &AuthServers{List: []*AuthServer{a}}
_, err := nscache.Get(key)
assert.Error(t, err)
assert.Equal(t, err.Error(), "cache not found")
nscache.Set(key, nil, servers, time.Hour)
_, err = nscache.Get(key)
assert.NoError(t, err)
nscache.now = func() time.Time {
return time.Now().Add(30 * time.Minute)
}
_, err = nscache.Get(key)
assert.NoError(t, err)
nscache.now = func() time.Time {
return time.Now().Add(2 * time.Hour)
}
_, err = nscache.Get(key)
assert.Error(t, err)
assert.Equal(t, err.Error(), "cache expired")
_, err = nscache.Get(key)
assert.Error(t, err)
nscache.Remove(key)
}