-
Notifications
You must be signed in to change notification settings - Fork 0
/
zone_test.go
189 lines (147 loc) · 5.09 KB
/
zone_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
package resolver
import (
"context"
"errors"
"github.com/stretchr/testify/mock"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
// Test cases for zone
func TestZone_Exchange_NilPool(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
// Prepare a DNS message
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn("example.com."), dns.TypeA)
ctx := context.TODO()
// Execute
response := z.Exchange(ctx, msg)
// Assertions: Should return an error since the pool is nil
assert.ErrorIs(t, response.Err, ErrNoPoolConfiguredForZone)
}
func TestZone_Exchange_WithPool(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
z.pool = mockPool
// Prepare a DNS message
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn("example.com."), dns.TypeA)
ctx := context.TODO()
expectedResponse := &Response{Msg: msg, Duration: 10 * time.Millisecond}
ctxMatcher := mock.MatchedBy(func(c context.Context) bool {
return true // Always returns true because we are just checking that it implements context.Context
})
// Mock the exchange function to accept any context and return the expected response
mockPool.On("exchange", ctxMatcher, msg).Return(expectedResponse)
// Execute
response := z.Exchange(ctx, msg)
// Assertions: Should return the result from the pool
assert.NoError(t, response.Err)
assert.Equal(t, expectedResponse, response)
ctxMatcher = mock.MatchedBy(func(c context.Context) bool {
// We text the expected ctx was passed in the `AssertCalled` below.
return c.Value(ctxZoneName).(string) == "example.com."
})
mockPool.AssertCalled(t, "exchange", ctxMatcher, msg)
}
func TestZone_Clone(t *testing.T) {
// Setup
originalZone := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
originalZone.pool = mockPool
// Execute: Clone the zone with a new name
clonedZone := originalZone.clone("newzone.com.")
// Assertions: The new zone should have a new name but share the same pool
assert.Equal(t, "newzone.com.", clonedZone.name)
assert.Equal(t, originalZone.pool, clonedZone.pool)
assert.Empty(t, clonedZone.dnskeys)
assert.Empty(t, clonedZone.dnskeyExpiry)
}
func TestZone_DNSKeys_CachedAndValid(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockRR := []dns.RR{&dns.DNSKEY{Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeDNSKEY, Class: dns.ClassINET, Ttl: 300}}}
z.dnskeys = mockRR
z.dnskeyExpiry = time.Now().Add(time.Hour) // Keys are still valid
// Execute
ctx := context.TODO()
keys, err := z.dnsKeys(ctx)
// Assertions: Should return the cached keys and no error
assert.NoError(t, err)
assert.Equal(t, mockRR, keys)
}
func TestZone_DNSKeys_Expired(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
z.pool = mockPool
// Prepare an expired DNS key
z.dnskeyExpiry = time.Now().Add(-time.Hour) // Keys are expired
expectedResponse := &Response{
Msg: &dns.Msg{
Answer: []dns.RR{&dns.DNSKEY{Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeDNSKEY, Class: dns.ClassINET, Ttl: 300}}},
},
}
// Mock the exchange function
mockPool.On("exchange", mock.Anything, mock.AnythingOfType("*dns.Msg")).Return(expectedResponse)
// Execute
ctx := context.TODO()
keys, err := z.dnsKeys(ctx)
// Assertions: Should return the new keys and no error
assert.NoError(t, err)
assert.Equal(t, expectedResponse.Msg.Answer, keys)
mockPool.AssertCalled(t, "exchange", mock.Anything, mock.AnythingOfType("*dns.Msg"))
}
func TestZone_DNSKeys_NilResponse(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
z.pool = mockPool
//Empty
expectedResponse := &Response{}
// Mock the exchange function
mockPool.On("exchange", mock.Anything, mock.AnythingOfType("*dns.Msg")).Return(expectedResponse)
keys, err := z.dnsKeys(context.TODO())
assert.Nil(t, keys)
assert.ErrorIs(t, err, ErrFailedToGetDNSKEYs)
}
func TestZone_DNSKeys_ErrorResponse(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
z.pool = mockPool
ErrTest := errors.New("test error")
expectedResponse := &Response{
Msg: &dns.Msg{
Answer: []dns.RR{&dns.DNSKEY{Hdr: dns.RR_Header{Name: "example.com.", Rrtype: dns.TypeDNSKEY, Class: dns.ClassINET, Ttl: 300}}},
},
Err: ErrTest,
}
// Mock the exchange function
mockPool.On("exchange", mock.Anything, mock.AnythingOfType("*dns.Msg")).Return(expectedResponse)
keys, err := z.dnsKeys(context.TODO())
assert.Nil(t, keys)
assert.ErrorIs(t, err, ErrFailedToGetDNSKEYs)
assert.ErrorIs(t, err, ErrTest)
}
func TestZone_DNSKeys_EmptyAnswer(t *testing.T) {
// Setup
z := &zone{name: "example.com."}
mockPool := new(MockExpiringExchanger)
z.pool = mockPool
expectedResponse := &Response{
Msg: &dns.Msg{
Answer: []dns.RR{},
},
}
// Mock the exchange function
mockPool.On("exchange", mock.Anything, mock.AnythingOfType("*dns.Msg")).Return(expectedResponse)
keys, err := z.dnsKeys(context.TODO())
assert.Nil(t, keys)
assert.NoError(t, err)
// We expect expiry to be in the future.
assert.Greater(t, z.dnskeyExpiry, time.Now())
}