-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
101 lines (77 loc) · 1.42 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package newdns
import (
"bytes"
"encoding/json"
"sort"
"strings"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func run(s *Server, addr string, fn func()) {
defer s.Close()
go func() {
err := s.Run(addr)
if err != nil {
panic(err)
}
}()
time.Sleep(100 * time.Millisecond)
fn()
}
func serve(handler dns.Handler, addr string, fn func()) {
closer := make(chan struct{})
defer close(closer)
go func() {
err := Run(addr, handler, Accept(nil), closer)
if err != nil {
panic(err)
}
}()
time.Sleep(100 * time.Millisecond)
fn()
}
func equalJSON(t *testing.T, a, b interface{}) {
buf := new(bytes.Buffer)
e := json.NewEncoder(buf)
e.SetIndent("", " ")
_ = e.Encode(a)
aa := buf.String()
buf.Reset()
_ = e.Encode(b)
bb := buf.String()
assert.JSONEq(t, aa, bb)
}
func order(rrs []dns.RR) []dns.RR {
cpy := make([]dns.RR, len(rrs))
copy(cpy, rrs)
sort.Slice(cpy, func(i, j int) bool {
var ai string
switch rr := cpy[i].(type) {
case *dns.NS:
ai = rr.Ns
rr.Hdr.Rdlength = 0
}
var aj string
switch rr := cpy[j].(type) {
case *dns.NS:
aj = rr.Ns
rr.Hdr.Rdlength = 0
}
return ai < aj
})
return cpy
}
func isIOError(err error) bool {
if err == nil {
return false
}
if strings.Contains(err.Error(), "i/o timeout") {
return true
}
if strings.Contains(err.Error(), "connection reset by peer") {
return true
}
return false
}