-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
78 lines (70 loc) · 1.46 KB
/
utils_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
package main
// General utilites
import (
"fmt"
"github.com/miekg/dns"
"net"
"time"
)
func UpstreamTestingDialer(upstream Upstream) func(addr string) (conn *dns.Conn, err error) {
expectedAddress := upstream.GetAddress()
return func(addr string) (conn *dns.Conn, err error) {
if addr != expectedAddress {
err = fmt.Errorf("got unexpected address [%s] when dialing upstream [%v], expecting address [%s] to be dialed", addr, upstream, expectedAddress)
}
server, client := net.Pipe()
server.Close()
return &dns.Conn{Conn: client}, err
}
}
func WaitForCondition(x int, f func() bool) (result bool) {
for i := 0; i < x; i++ {
if result = f(); result {
break
}
time.Sleep(time.Duration(100) * time.Millisecond)
}
return
}
func buildRequest() (request *dns.Msg) {
return &dns.Msg{
Question: []dns.Question{
dns.Question{
Name: "example.com",
Qtype: 1,
Qclass: 1,
},
},
}
}
func buildQuery() (q Query) {
writer := &MockResponseWriter{}
qdt := &MockQueryDurationTimer{}
q = Query{
W: writer,
Msg: &dns.Msg{},
Reply: &dns.Msg{},
Timer: qdt,
}
return q
}
func buildAnswer() (m *dns.Msg) {
a, err := dns.NewRR("example.com. 123 IN A 10.0.0.0")
if err != nil {
panic(fmt.Sprintf("couldn't create answer for query: %s", err))
}
return &dns.Msg{
Answer: []dns.RR{
a,
},
}
}
func buildConnEntry() (c ConnEntry) {
s, cl := net.Pipe()
s.Close()
return &connEntry{
Conn: &dns.Conn{
Conn: cl,
},
}
}