-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
335 lines (262 loc) · 7.76 KB
/
main_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"context"
"errors"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
var (
testZone string = "myip.opendns.com."
timeout int = 5
)
// MockDNSClient is a mock implementation of the DNSClient interface
type MockDNSClient struct {
mock.Mock
}
func (m *MockDNSClient) ExchangeContext(ctx context.Context, msg *dns.Msg, address string) (*dns.Msg, time.Duration, error) {
args := m.Called(msg, address)
return args.Get(0).(*dns.Msg), args.Get(1).(time.Duration), args.Error(2)
}
// MockHTTPClient is a mock implementation of the HTTPClient interface
type MockHTTPClient struct {
mock.Mock
}
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
args := m.Called(req)
return args.Get(0).(*http.Response), args.Error(1)
}
func TestFetchIPViaDNS(t *testing.T) {
// Prepare the mock DNS client
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
dns.Id = func() uint16 { return 1234 }
// Prepare the DNS message and response
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
msg.RecursionDesired = true
msg.Id = 1234
response := new(dns.Msg)
response.Answer = append(response.Answer, &dns.A{
A: net.IPv4(192, 0, 2, 1),
})
response.Id = 1234
mockClient.On(
"ExchangeContext",
msg,
"resolver1.opendns.com:53",
).Return(response, time.Duration(0), nil)
// Call the function under test
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
// Verify the results
require.NoError(t, err)
assert.Equal(t, "192.0.2.1", ip)
mockClient.AssertExpectations(t)
}
func TestFetchIPViaDNS_EmptyARecord(t *testing.T) {
// Prepare the mock DNS client
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
dns.Id = func() uint16 { return 1234 }
// Prepare the DNS message and response
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
msg.RecursionDesired = true
msg.Id = 1234
response := new(dns.Msg)
response.Answer = []dns.RR{}
response.Id = 1234
mockClient.On(
"ExchangeContext",
msg,
"resolver1.opendns.com:53",
).Return(response, time.Duration(0), nil)
// Call the function under test
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
// Verify the results
require.Error(t, err)
assert.EqualError(t, err, "no A record found in DNS query")
assert.Empty(t, ip)
mockClient.AssertExpectations(t)
}
func TestFetchIPViaDNS_Error(t *testing.T) {
// Prepare the mock DNS client
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
// Make the ids static otherwise the test fails
dns.Id = func() uint16 { return 1234 }
// Prepare the DNS message and an empty response
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
response := new(dns.Msg) // No Answer
mockClient.On(
"ExchangeContext",
msg,
"resolver1.opendns.com:53",
).Return(response, time.Duration(0), nil)
// Call the function under test
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
// Verify the results
require.Error(t, err)
assert.Empty(t, ip)
}
func TestFetchIPViaDNS_Timeout(t *testing.T) {
// Prepare the mock DNS client
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
// Simulate a DNS timeout by returning an error
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
msg.RecursionDesired = true
mockClient.On("ExchangeContext", msg, "resolver1.opendns.com:53").Return(
(*dns.Msg)(nil),
time.Duration(0),
errors.New("timeout"),
)
// Call the function under test
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
// Verify that a timeout error is returned
require.Error(t, err)
assert.Empty(t, ip)
assert.Contains(t, err.Error(), "timeout")
// Assert the method was called with expected arguments
mockClient.AssertExpectations(t)
}
func TestFetchIPViaDNS_NetworkError(t *testing.T) {
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
dns.Id = func() uint16 { return 1234 }
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
msg.RecursionDesired = true
mockClient.On("ExchangeContext", msg, "resolver1.opendns.com:53").Return((*dns.Msg)(nil), time.Duration(0), errors.New("network error"))
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
require.Error(t, err)
assert.Empty(t, ip)
assert.Contains(t, err.Error(), "network error")
}
func TestFetchIPViaDNS_IncorrectAddress(t *testing.T) {
mockClient := new(MockDNSClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
dns.Id = func() uint16 { return 1234 }
msg := new(dns.Msg)
msg.SetQuestion("myip.opendns.com.", dns.TypeA)
msg.RecursionDesired = true
mockClient.On("ExchangeContext", msg, "resolver1.opendns.com:53").Return(
(*dns.Msg)(nil),
time.Duration(0),
errors.New("unreachable"),
)
ip, err := fetchIPViaDNS(ctx, mockClient, testZone)
require.Error(t, err)
assert.Empty(t, ip)
assert.Contains(t, err.Error(), "unreachable")
}
func TestFetchIPViaHTTP(t *testing.T) {
// Prepare the mock HTTP client
mockClient := new(MockHTTPClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
// Prepare the HTTP response
response := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("192.0.2.1")),
}
mockClient.On("Do", mock.Anything).Return(response, nil)
// Call the function under test
ip, err := fetchIPViaHTTP(ctx, mockClient)
// Verify the results
require.NoError(t, err)
assert.Equal(t, "192.0.2.1", ip)
}
func TestFetchIPViaHTTP_Error(t *testing.T) {
// Prepare the mock HTTP client
mockClient := new(MockHTTPClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
mockClient.On("Do", mock.Anything).Return(
(*http.Response)(nil),
errors.New("failed to connect"),
)
// Call the function under test
ip, err := fetchIPViaHTTP(ctx, mockClient)
// Verify the results
require.Error(t, err)
assert.Empty(t, ip)
}
func TestFetchIPViaHTTP_Non200Status(t *testing.T) {
mockClient := new(MockHTTPClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
mockClient.On("Do", mock.Anything).Return(&http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(strings.NewReader(http.StatusText(http.StatusInternalServerError))),
}, nil)
ip, err := fetchIPViaHTTP(ctx, mockClient)
require.Error(t, err)
assert.Empty(t, ip)
assert.Contains(t, err.Error(), "unexpected status code")
}
func TestFetchIPViaHTTP_BodyReadError(t *testing.T) {
mockClient := new(MockHTTPClient)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(timeout)*time.Second,
)
defer cancel()
body := io.NopCloser(&errReader{})
response := &http.Response{
StatusCode: http.StatusOK,
Body: body,
}
mockClient.On("Do", mock.Anything).Return(response, nil)
ip, err := fetchIPViaHTTP(ctx, mockClient)
require.Error(t, err)
assert.Empty(t, ip)
assert.Contains(t, err.Error(), "failed to read response body")
}
// errReader simulates an error when reading from an io.Reader
type errReader struct{}
func (*errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("read error")
}