-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstub_test.go
120 lines (99 loc) · 2.47 KB
/
stub_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
package resolver
import (
"context"
"errors"
"github.com/miekg/dns"
"sync"
"testing"
"time"
)
func TestStub_Verify(t *testing.T) {
rs, _ := NewStub("0.0.0.0")
rs.exchangeFunc = func(ctx context.Context, req *dns.Msg, client *client) (r *dns.Msg, rtt time.Duration, err error) {
qm := testData[req.Question[0].Qtype]
reply := qm[req.Question[0].Name]
return reply, 0, nil
}
rs.Verify = func(m *dns.Msg) error {
if m.Answer[0].Header().Name == "ad.example.com." {
return errors.New("sig failed")
}
return nil
}
ctx := context.Background()
if _, _, err := rs.LookupIP(ctx, "ip", "ad.example.com"); err == nil {
t.Fatal("want query to fail")
}
if _, _, err := rs.LookupIP(ctx, "ip", "example.com"); err != nil {
t.Fatal(err)
}
rs.Verify = nil
if _, _, err := rs.LookupIP(ctx, "ip", "ad.example.com"); err != nil {
t.Fatal("want no error")
}
}
func TestStub_NewStub(t *testing.T) {
ad, err := NewStub("https://cloudflare.com")
if err != nil {
t.Fatal(err)
}
if ad.client.addr != "https://cloudflare.com" {
t.Fatalf("want %s, got %s", "https://cloudflare.com", ad.client.addr)
}
if ad.client.d.Net != "https" {
t.Fatalf("want https, got %s", ad.client.d.Net)
}
ad, err = NewStub("1.1.1.1")
if err != nil {
t.Fatal(err)
}
if ad.client.addr != "1.1.1.1:53" {
t.Fatalf("want 1.1.1.1, got %s", ad.client.addr)
}
}
func TestStub_Integration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
// go through every supported transport
// to make sure they are functional
// TODO: create a dummy resolver to test this instead
var wg sync.WaitGroup
protos := []string{"", "udp://", "tcp://", "tls://", "https://"}
wg.Add(len(protos))
for _, p := range protos {
go func(proto string) {
defer wg.Done()
rs, err := NewStub(proto + "1.1.1.1")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if _, _, err := rs.LookupIP(ctx, "ip", "dnssec-failed.org"); err == nil {
t.Fatal("dnssec-failed.org returned a valid response")
}
ips, _, err := rs.LookupIP(ctx, "ip", "example.com")
if err != nil {
t.Fatal(err)
}
if len(ips) == 0 {
t.Fatalf("got no ips")
}
rrs, _, err := rs.LookupTLSA(ctx, "443", "tcp", "freebsd.org")
if err != nil {
t.Fatal(err)
}
if len(rrs) == 0 {
t.Fatalf("got no tlsa records from freebsd.org")
}
}(p)
}
wg.Wait()
}
func testRR(rr string) dns.RR {
r, err := dns.NewRR(rr)
if err != nil {
panic(err)
}
return r
}