-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathxchg_test.go
125 lines (107 loc) · 3.35 KB
/
xchg_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
// Copyright © by Jeff Foley 2021-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"strings"
"testing"
"time"
"github.com/caffix/stringset"
"github.com/miekg/dns"
)
func TestXchgAddRemove(t *testing.T) {
name := "caffix.net"
xchg := newXchgMgr(DefaultTimeout)
msg := QueryMsg(name, dns.TypeA)
req := &request{Msg: msg}
if err := xchg.add(req); err != nil {
t.Errorf("Failed to add the request")
}
if err := xchg.add(req); err == nil {
t.Errorf("Failed to detect the same request added twice")
}
ret := xchg.remove(msg.Id, msg.Question[0].Name)
if ret == nil || ret.Msg == nil || name != strings.ToLower(RemoveLastDot(ret.Msg.Question[0].Name)) {
t.Errorf("Did not find and remove the message from the data structure")
}
ret = xchg.remove(msg.Id, msg.Question[0].Name)
if ret != nil {
t.Errorf("Did not return nil when attempting to remove an element for the second time")
}
if err := xchg.add(req); err != nil {
t.Errorf("Failed to add the request after being removed")
}
}
func TestXchgUpdateTimestamp(t *testing.T) {
name := "caffix.net"
xchg := newXchgMgr(DefaultTimeout)
msg := QueryMsg(name, dns.TypeA)
req := &request{Msg: msg}
if !req.Timestamp.IsZero() {
t.Errorf("Expected the new request to have a zero value timestamp")
}
if err := xchg.add(req); err != nil {
t.Errorf("Failed to add the request")
}
xchg.updateTimestamp(msg.Id, name)
// For complete coverage
xchg.updateTimestamp(msg.Id, "Bad Name")
req = xchg.remove(msg.Id, msg.Question[0].Name)
if req == nil || req.Timestamp.IsZero() {
t.Errorf("Expected the updated request to not have a zero value timestamp")
}
}
func TestXchgRemoveExpired(t *testing.T) {
xchg := newXchgMgr(time.Second)
names := []string{"caffix.net", "www.caffix.net", "blog.caffix.net"}
for _, name := range names {
msg := QueryMsg(name, dns.TypeA)
if err := xchg.add(&request{
Msg: msg,
Timestamp: time.Now(),
}); err != nil {
t.Errorf("Failed to add the request")
}
}
// Add one request that should not be removed with the others
name := "vpn.caffix.net"
msg := QueryMsg(name, dns.TypeA)
if err := xchg.add(&request{
Msg: msg,
Timestamp: time.Now().Add(3 * time.Second),
}); err != nil {
t.Errorf("Failed to add the request")
}
if len(xchg.removeExpired()) > 0 {
t.Errorf("The removeExpired method returned requests too early")
}
time.Sleep(1500 * time.Millisecond)
set := stringset.New(names...)
defer set.Close()
for _, req := range xchg.removeExpired() {
name := strings.ToLower(RemoveLastDot(req.Msg.Question[0].Name))
set.Remove(name)
}
if set.Len() > 0 {
t.Errorf("Not all expected requests were returned by removeExpired")
}
}
func TestXchgRemoveAll(t *testing.T) {
xchg := newXchgMgr(time.Second)
names := []string{"caffix.net", "www.caffix.net", "blog.caffix.net"}
for _, name := range names {
msg := QueryMsg(name, dns.TypeA)
if err := xchg.add(&request{Msg: msg}); err != nil {
t.Errorf("Failed to add the request")
}
}
set := stringset.New(names...)
defer set.Close()
for _, req := range xchg.removeAll() {
name := strings.ToLower(RemoveLastDot(req.Msg.Question[0].Name))
set.Remove(name)
}
if set.Len() > 0 {
t.Errorf("Not all expected requests were returned by removeAll")
}
}