-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration_test.go
168 lines (148 loc) · 4.24 KB
/
integration_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: GPL-3.0-or-later
package dnscore_test
import (
"context"
"crypto/tls"
"net/http"
"testing"
"time"
"github.com/miekg/dns"
"github.com/rbmk-project/dnscore"
"github.com/rbmk-project/dnscore/dnscoretest"
"github.com/stretchr/testify/assert"
)
func checkResult(t *testing.T, resp *dns.Msg, err error) {
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 1, len(resp.Answer))
assert.Equal(t, "example.com.", resp.Answer[0].Header().Name)
assert.Equal(t, dns.TypeA, resp.Answer[0].Header().Rrtype)
assert.Equal(
t, dnscoretest.ExampleComAddrA.String(),
resp.Answer[0].(*dns.A).A.String(),
)
}
func TestTransport_RoundTrip_UDP(t *testing.T) {
// create and start a testing server
server := &dnscoretest.Server{}
handler := dnscoretest.NewExampleComHandler()
<-server.StartUDP(handler)
defer server.Close()
// create transport, server addr, and query
txp := &dnscore.Transport{}
serverAddr := &dnscore.ServerAddr{
Protocol: dnscore.ProtocolUDP,
Address: server.Addr,
}
options := []dnscore.QueryOption{
dnscore.QueryOptionEDNS0(
dnscore.EDNS0SuggestedMaxResponseSizeUDP,
0,
),
}
query, err := dnscore.NewQuery("example.com", dns.TypeA, options...)
if err != nil {
t.Fatal(err)
}
// issue the query and get the response
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := txp.Query(ctx, serverAddr, query)
// verify the results
checkResult(t, resp, err)
}
func TestTransport_RoundTrip_TCP(t *testing.T) {
// create and start a testing server
server := &dnscoretest.Server{}
handler := dnscoretest.NewExampleComHandler()
<-server.StartTCP(handler)
defer server.Close()
// create transport, server addr, and query
txp := &dnscore.Transport{}
serverAddr := &dnscore.ServerAddr{
Protocol: dnscore.ProtocolTCP,
Address: server.Addr,
}
options := []dnscore.QueryOption{
dnscore.QueryOptionEDNS0(
dnscore.EDNS0SuggestedMaxResponseSizeOtherwise,
0,
),
}
query, err := dnscore.NewQuery("example.com", dns.TypeA, options...)
if err != nil {
t.Fatal(err)
}
// issue the query and get the response
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := txp.Query(ctx, serverAddr, query)
// verify the results
checkResult(t, resp, err)
}
func TestTransport_RoundTrip_TLS(t *testing.T) {
// create and start a testing server
server := &dnscoretest.Server{}
handler := dnscoretest.NewExampleComHandler()
<-server.StartTLS(handler)
defer server.Close()
// create transport, server addr, and query
txp := &dnscore.Transport{RootCAs: server.RootCAs}
serverAddr := &dnscore.ServerAddr{
Protocol: dnscore.ProtocolDoT,
Address: server.Addr,
}
options := []dnscore.QueryOption{
dnscore.QueryOptionEDNS0(
dnscore.EDNS0SuggestedMaxResponseSizeOtherwise,
dnscore.EDNS0FlagDO|dnscore.EDNS0FlagBlockLengthPadding,
),
}
query, err := dnscore.NewQuery("example.com", dns.TypeA, options...)
if err != nil {
t.Fatal(err)
}
// issue the query and get the response
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := txp.Query(ctx, serverAddr, query)
// verify the results
checkResult(t, resp, err)
}
func TestTransport_RoundTrip_HTTPS(t *testing.T) {
// create and start a testing server
server := &dnscoretest.Server{}
handler := dnscoretest.NewExampleComHandler()
<-server.StartHTTPS(handler)
defer server.Close()
// create transport, server addr, and query
txp := &dnscore.Transport{
HTTPClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: server.RootCAs,
},
},
},
}
serverAddr := &dnscore.ServerAddr{
Protocol: dnscore.ProtocolDoH,
Address: server.URL,
}
options := []dnscore.QueryOption{
dnscore.QueryOptionEDNS0(
dnscore.EDNS0SuggestedMaxResponseSizeOtherwise,
dnscore.EDNS0FlagDO|dnscore.EDNS0FlagBlockLengthPadding,
),
}
query, err := dnscore.NewQuery("example.com", dns.TypeA, options...)
if err != nil {
t.Fatal(err)
}
// issue the query and get the response
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := txp.Query(ctx, serverAddr, query)
// verify the results
checkResult(t, resp, err)
}