-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy_test.go
311 lines (266 loc) · 9.08 KB
/
proxy_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package https
import (
"bytes"
"context"
"errors"
"io"
"net"
"net/http"
"strings"
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
const upstreamURL = "https://example.com/dns-query"
type mockHTTPClientFunc func(*http.Request) (*http.Response, error)
func (f mockHTTPClientFunc) Do(req *http.Request) (*http.Response, error) {
return f(req)
}
func newExpectedDNSMsg() *dns.Msg {
return &dns.Msg{
MsgHdr: dns.MsgHdr{Response: true},
Question: []dns.Question{
{
Name: "example.com.",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
},
},
Answer: []dns.RR{
&dns.A{
Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 30},
A: net.IPv4(1, 1, 1, 1),
},
},
}
}
func packMsg(t *testing.T, msg *dns.Msg) []byte {
t.Helper()
data, err := msg.Pack()
require.NoError(t, err)
return data
}
func TestDNSClient(t *testing.T) {
callCount := 0
expectedMsg := newExpectedDNSMsg()
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
callCount++
acceptHdrs := req.Header["Accept"]
require.NotEmpty(t, acceptHdrs, "Accept header is empty")
require.Equal(t, dnsMessageMimeType, acceptHdrs[0], "invalid accept header")
contentTypeHdrs := req.Header["Content-Type"]
require.NotEmpty(t, contentTypeHdrs, "Content-Type header is empty")
require.Equal(t, dnsMessageMimeType, contentTypeHdrs[0], "invalid Content-Type header")
require.Equal(t, upstreamURL, req.URL.String(), "invalid request URL")
require.Equal(t, "POST", req.Method, "invalid request method")
buf, err := io.ReadAll(req.Body)
require.NoError(t, err)
require.Equal(t, []byte("abc"), buf, "invalid request body")
resp = &http.Response{
Body: io.NopCloser(bytes.NewReader(packMsg(t, expectedMsg))),
StatusCode: http.StatusOK,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
result, err := dnsClient.Query(context.Background(), []byte("abc"))
require.NoError(t, err)
require.Equal(t, 1, callCount, "invalid http client call count")
require.True(t, result.MsgHdr.Response)
require.Equal(t, len(expectedMsg.Answer), len(result.Answer))
require.Equal(t, expectedMsg.Answer[0].String(), result.Answer[0].String())
}
func TestDNSClientNewRequestError(t *testing.T) {
invalidURL := "https://example.com/\t\n"
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
t.Fatal("http client must not be called")
return
})
dnsClient := newDoHDNSClient(httpClient, invalidURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
func TestDNSClientHttpClientError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
err = errors.New("http error")
resp = &http.Response{
Body: io.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
StatusCode: http.StatusOK,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
func TestDNSClientHttpStatusError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: io.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
StatusCode: http.StatusInternalServerError,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
type errReader struct {
delegate io.Reader
err error
}
// return err instead of io.EOF after the last piece of data is read
func (r *errReader) Read(p []byte) (n int, err error) {
n, err = r.delegate.Read(p)
if err == io.EOF {
err = r.err
}
return
}
func TestDNSClientResponseReadError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
reader := &errReader{
delegate: bytes.NewReader(packMsg(t, newExpectedDNSMsg())),
err: errors.New("io error"),
}
resp = &http.Response{
Body: io.NopCloser(reader),
StatusCode: http.StatusOK,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
func TestDNSClientMsgUnpackError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: io.NopCloser(strings.NewReader("def")),
StatusCode: http.StatusOK,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
func TestDNSClientLargeResponseError(t *testing.T) {
// create large buffer with the correct DNS message at the beginning
var buf bytes.Buffer
dnsMsg := packMsg(t, newExpectedDNSMsg())
buf.Write(dnsMsg)
buf.WriteString(strings.Repeat("a", maxDNSMessageSize))
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: io.NopCloser(&buf),
StatusCode: http.StatusOK,
}
return
})
dnsClient := newDoHDNSClient(httpClient, upstreamURL)
_, err := dnsClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
}
type mockDNSClient struct {
callCount int
reqBody []byte
t *testing.T
err error
}
func (c *mockDNSClient) Query(ctx context.Context, dnsreq []byte) (result *dns.Msg, err error) {
c.t.Helper()
c.callCount++
require.NotNil(c.t, ctx)
require.Equal(c.t, c.reqBody, dnsreq, "invalid request body")
return newExpectedDNSMsg(), c.err
}
func TestLoadBalanceDNSClient(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t}
clients := []dnsClient{client1}
lbClient := newLoadBalanceDNSClient(clients)
result, err := lbClient.Query(context.Background(), []byte("abc"))
require.NoError(t, err)
require.Equal(t, 1, client1.callCount)
require.Equal(t, newExpectedDNSMsg(), result)
}
func TestLoadBalanceDNSClientError(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t, err: errors.New("client error")}
clients := []dnsClient{client1}
lbClient := newLoadBalanceDNSClient(clients)
_, err := lbClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
require.Equal(t, 1, client1.callCount)
}
func TestLoadBalanceDNSClientRetry(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t, err: errors.New("client error")}
client2 := &mockDNSClient{reqBody: []byte("abc"), t: t}
clients := []dnsClient{client1, client2}
lbClient := newLoadBalanceDNSClient(clients, withLbPolicy(newSequentialPolicy()))
result, err := lbClient.Query(context.Background(), []byte("abc"))
require.NoError(t, err)
require.Equal(t, 1, client1.callCount)
require.Equal(t, 1, client2.callCount)
require.Equal(t, newExpectedDNSMsg(), result)
}
func TestLoadBalanceDNSClientPolicy(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t}
client2 := &mockDNSClient{reqBody: []byte("abc"), t: t}
clients := []dnsClient{client1, client2}
lbClient := newLoadBalanceDNSClient(clients, withLbPolicy(newRoundRobinPolicy()))
result, err := lbClient.Query(context.Background(), []byte("abc"))
require.NoError(t, err)
require.Equal(t, 1, client1.callCount)
require.Equal(t, 0, client2.callCount)
require.Equal(t, newExpectedDNSMsg(), result)
result, err = lbClient.Query(context.Background(), []byte("abc"))
require.NoError(t, err)
require.Equal(t, 1, client1.callCount)
require.Equal(t, 1, client2.callCount)
require.Equal(t, newExpectedDNSMsg(), result)
}
func TestLoadBalanceDNSClientMaxFails(t *testing.T) {
tests := []struct {
name string
maxFails int
callCountClient1 int
callCountClient2 int
}{
{
name: "OneMaxFail",
maxFails: 1,
callCountClient1: 1,
callCountClient2: 0,
},
{
name: "TenMaxFails",
maxFails: 10,
callCountClient1: 1,
callCountClient2: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t, err: errors.New("client error")}
client2 := &mockDNSClient{reqBody: []byte("abc"), t: t, err: errors.New("client error")}
clients := []dnsClient{client1, client2}
lbClient := newLoadBalanceDNSClient(clients,
withLbPolicy(newSequentialPolicy()),
withLbRequestTimeout(defaultRequestTimeout),
withLbMaxFails(tt.maxFails))
result, err := lbClient.Query(context.Background(), []byte("abc"))
require.Error(t, err)
require.Equal(t, tt.callCountClient1, client1.callCount)
require.Equal(t, tt.callCountClient2, client2.callCount)
require.Equal(t, newExpectedDNSMsg(), result)
})
}
}
func TestDefaultNewLoadBalanceDNSClient(t *testing.T) {
client1 := &mockDNSClient{reqBody: []byte("abc"), t: t}
client2 := &mockDNSClient{reqBody: []byte("abc"), t: t}
clients := []dnsClient{client1, client2}
lbClient := newLoadBalanceDNSClient(clients)
require.Equal(t, 2, lbClient.maxFails)
require.Equal(t, defaultRequestTimeout, lbClient.timeout)
}