forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns_test.go
More file actions
244 lines (200 loc) · 7.24 KB
/
dns_test.go
File metadata and controls
244 lines (200 loc) · 7.24 KB
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
package va
import (
"context"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/jmhodges/clock"
"github.com/letsencrypt/boulder/bdns"
"github.com/letsencrypt/boulder/core"
"github.com/letsencrypt/boulder/identifier"
"github.com/letsencrypt/boulder/metrics"
"github.com/letsencrypt/boulder/probs"
"github.com/letsencrypt/boulder/test"
"github.com/prometheus/client_golang/prometheus"
)
func dnsChallenge() core.Challenge {
return createChallenge(core.ChallengeTypeDNS01)
}
func TestDNSValidationEmpty(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
// This test calls PerformValidation directly, because that is where the
// metrics checked below are incremented.
req := createValidationRequest("empty-txts.com", core.ChallengeTypeDNS01)
res, _ := va.PerformValidation(context.Background(), req)
test.AssertEquals(t, res.Problems.ProblemType, "unauthorized")
test.AssertEquals(t, res.Problems.Detail, "No TXT record found at _acme-challenge.empty-txts.com")
test.AssertMetricWithLabelsEquals(t, va.metrics.validationTime, prometheus.Labels{
"type": "dns-01",
"result": "invalid",
"problem_type": "unauthorized",
}, 1)
}
func TestDNSValidationWrong(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateDNS01(context.Background(), dnsi("wrong-dns01.com"), dnsChallenge())
if prob == nil {
t.Fatalf("Successful DNS validation with wrong TXT record")
}
test.AssertEquals(t, prob.Error(), "unauthorized :: Incorrect TXT record \"a\" found at _acme-challenge.wrong-dns01.com")
}
func TestDNSValidationWrongMany(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateDNS01(context.Background(), dnsi("wrong-many-dns01.com"), dnsChallenge())
if prob == nil {
t.Fatalf("Successful DNS validation with wrong TXT record")
}
test.AssertEquals(t, prob.Error(), "unauthorized :: Incorrect TXT record \"a\" (and 4 more) found at _acme-challenge.wrong-many-dns01.com")
}
func TestDNSValidationWrongLong(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateDNS01(context.Background(), dnsi("long-dns01.com"), dnsChallenge())
if prob == nil {
t.Fatalf("Successful DNS validation with wrong TXT record")
}
test.AssertEquals(t, prob.Error(), "unauthorized :: Incorrect TXT record \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...\" found at _acme-challenge.long-dns01.com")
}
func TestDNSValidationFailure(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateDNS01(ctx, dnsi("localhost"), dnsChallenge())
test.AssertEquals(t, prob.Type, probs.UnauthorizedProblem)
}
func TestDNSValidationInvalid(t *testing.T) {
var notDNS = identifier.ACMEIdentifier{
Type: identifier.IdentifierType("iris"),
Value: "790DB180-A274-47A4-855F-31C428CB1072",
}
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateDNS01(ctx, notDNS, dnsChallenge())
test.AssertEquals(t, prob.Type, probs.MalformedProblem)
}
func TestDNSValidationNotSane(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
chall := dnsChallenge()
chall.Token = ""
_, prob := va.validateChallenge(ctx, dnsi("localhost"), chall)
if prob.Type != probs.MalformedProblem {
t.Errorf("Got wrong error type: expected %s, got %s",
prob.Type, probs.MalformedProblem)
}
if !strings.Contains(prob.Error(), "Challenge failed consistency check:") {
t.Errorf("Got wrong error: %s", prob.Error())
}
chall.Token = "yfCBb-bRTLz8Wd1C0lTUQK3qlKj3-t2tYGwx5Hj7r_"
_, prob = va.validateChallenge(ctx, dnsi("localhost"), chall)
if prob.Type != probs.MalformedProblem {
t.Errorf("Got wrong error type: expected %s, got %s",
prob.Type, probs.MalformedProblem)
}
if !strings.Contains(prob.Error(), "Challenge failed consistency check:") {
t.Errorf("Got wrong error: %s", prob.Error())
}
chall.ProvidedKeyAuthorization = "a"
_, prob = va.validateChallenge(ctx, dnsi("localhost"), chall)
if prob.Type != probs.MalformedProblem {
t.Errorf("Got wrong error type: expected %s, got %s",
prob.Type, probs.MalformedProblem)
}
if !strings.Contains(prob.Error(), "Challenge failed consistency check:") {
t.Errorf("Got wrong error: %s", prob.Error())
}
}
func TestDNSValidationServFail(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateChallenge(ctx, dnsi("servfail.com"), dnsChallenge())
test.AssertEquals(t, prob.Type, probs.DNSProblem)
}
func TestDNSValidationNoServer(t *testing.T) {
va, log := setup(nil, 0, "", nil)
staticProvider, err := bdns.NewStaticProvider([]string{})
test.AssertNotError(t, err, "Couldn't make new static provider")
va.dnsClient = bdns.NewTest(
time.Second*5,
staticProvider,
metrics.NoopRegisterer,
clock.New(),
1,
log)
_, prob := va.validateChallenge(ctx, dnsi("localhost"), dnsChallenge())
test.AssertEquals(t, prob.Type, probs.DNSProblem)
}
func TestDNSValidationOK(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateChallenge(ctx, dnsi("good-dns01.com"), dnsChallenge())
test.Assert(t, prob == nil, "Should be valid.")
}
func TestDNSValidationNoAuthorityOK(t *testing.T) {
va, _ := setup(nil, 0, "", nil)
_, prob := va.validateChallenge(ctx, dnsi("no-authority-dns01.com"), dnsChallenge())
test.Assert(t, prob == nil, "Should be valid.")
}
func TestAvailableAddresses(t *testing.T) {
v6a := net.ParseIP("::1")
v6b := net.ParseIP("2001:db8::2:1") // 2001:DB8 is reserved for docs (RFC 3849)
v4a := net.ParseIP("127.0.0.1")
v4b := net.ParseIP("192.0.2.1") // 192.0.2.0/24 is reserved for docs (RFC 5737)
testcases := []struct {
input []net.IP
v4 []net.IP
v6 []net.IP
}{
// An empty validation record
{
[]net.IP{},
[]net.IP{},
[]net.IP{},
},
// A validation record with one IPv4 address
{
[]net.IP{v4a},
[]net.IP{v4a},
[]net.IP{},
},
// A dual homed record with an IPv4 and IPv6 address
{
[]net.IP{v4a, v6a},
[]net.IP{v4a},
[]net.IP{v6a},
},
// The same as above but with the v4/v6 order flipped
{
[]net.IP{v6a, v4a},
[]net.IP{v4a},
[]net.IP{v6a},
},
// A validation record with just IPv6 addresses
{
[]net.IP{v6a, v6b},
[]net.IP{},
[]net.IP{v6a, v6b},
},
// A validation record with interleaved IPv4/IPv6 records
{
[]net.IP{v6a, v4a, v6b, v4b},
[]net.IP{v4a, v4b},
[]net.IP{v6a, v6b},
},
}
for _, tc := range testcases {
// Split the input record into v4/v6 addresses
v4result, v6result := availableAddresses(tc.input)
// Test that we got the right number of v4 results
test.Assert(t, len(tc.v4) == len(v4result),
fmt.Sprintf("Wrong # of IPv4 results: expected %d, got %d", len(tc.v4), len(v4result)))
// Check that all of the v4 results match expected values
for i, v4addr := range tc.v4 {
test.Assert(t, v4addr.String() == v4result[i].String(),
fmt.Sprintf("Wrong v4 result index %d: expected %q got %q", i, v4addr.String(), v4result[i].String()))
}
// Test that we got the right number of v6 results
test.Assert(t, len(tc.v6) == len(v6result),
fmt.Sprintf("Wrong # of IPv6 results: expected %d, got %d", len(tc.v6), len(v6result)))
// Check that all of the v6 results match expected values
for i, v6addr := range tc.v6 {
test.Assert(t, v6addr.String() == v6result[i].String(),
fmt.Sprintf("Wrong v6 result index %d: expected %q got %q", i, v6addr.String(), v6result[i].String()))
}
}
}