-
-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathsolvers_test.go
More file actions
328 lines (303 loc) · 8.07 KB
/
Copy pathsolvers_test.go
File metadata and controls
328 lines (303 loc) · 8.07 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
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
// Copyright 2015 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package certmagic
import (
"context"
"net"
"strconv"
"testing"
"time"
"github.com/caddyserver/certmagic/internal/filedescriptor"
"github.com/libdns/libdns"
"github.com/mholt/acmez/v3/acme"
)
type cleanupContextKey struct{}
type cleanupContextSolver struct {
t *testing.T
wantValue string
cleanedUp bool
}
func (*cleanupContextSolver) Present(context.Context, acme.Challenge) error { return nil }
func (s *cleanupContextSolver) CleanUp(ctx context.Context, _ acme.Challenge) error {
s.cleanedUp = true
if err := ctx.Err(); err != nil {
s.t.Fatalf("cleanup context is canceled: %v", err)
}
if got := ctx.Value(cleanupContextKey{}); got != s.wantValue {
s.t.Fatalf("cleanup context value = %v, want %q", got, s.wantValue)
}
return nil
}
func TestSolverWrapperCleanUpUsesUncanceledContext(t *testing.T) {
const contextValue = "cleanup-value"
solver := &cleanupContextSolver{t: t, wantValue: contextValue}
wrapper := solverWrapper{Solver: solver}
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), cleanupContextKey{}, contextValue))
cancel()
if err := wrapper.CleanUp(ctx, acme.Challenge{}); err != nil {
t.Fatalf("cleanup failed: %v", err)
}
if !solver.cleanedUp {
t.Fatal("expected wrapped solver cleanup to run")
}
}
func Test_challengeKey(t *testing.T) {
type args struct {
chal acme.Challenge
}
tests := []struct {
name string
args args
want string
}{
{
name: "ok/dns-dns",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeDNS01,
Identifier: acme.Identifier{
Type: "dns",
Value: "*.example.com",
},
},
},
want: "*.example.com",
},
{
name: "ok/http-dns",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeHTTP01,
Identifier: acme.Identifier{
Type: "dns",
Value: "*.example.com",
},
},
},
want: "*.example.com",
},
{
name: "ok/tls-dns",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeTLSALPN01,
Identifier: acme.Identifier{
Type: "dns",
Value: "*.example.com",
},
},
},
want: "*.example.com",
},
{
name: "ok/http-ipv4",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeHTTP01,
Identifier: acme.Identifier{
Type: "ip",
Value: "127.0.0.1",
},
},
},
want: "127.0.0.1",
},
{
name: "ok/http-ipv6",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeHTTP01,
Identifier: acme.Identifier{
Type: "ip",
Value: "2001:db8::1",
},
},
},
want: "2001:db8::1",
},
{
name: "ok/tls-ipv4",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeTLSALPN01,
Identifier: acme.Identifier{
Type: "ip",
Value: "127.0.0.1",
},
},
},
want: "1.0.0.127.in-addr.arpa",
},
{
name: "ok/tls-ipv6",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeTLSALPN01,
Identifier: acme.Identifier{
Type: "ip",
Value: "2001:db8::1",
},
},
},
want: "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa",
},
{
name: "fail/tls-ipv4",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeTLSALPN01,
Identifier: acme.Identifier{
Type: "ip",
Value: "127.0.0.1000",
},
},
},
want: "127.0.0.1000", // reversing this fails; default to identifier value
},
{
name: "fail/tls-ipv6",
args: args{
chal: acme.Challenge{
Type: acme.ChallengeTypeTLSALPN01,
Identifier: acme.Identifier{
Type: "ip",
Value: "2001:db8::10000",
},
},
},
want: "2001:db8::10000", // reversing this fails; default to identifier value
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := challengeKey(tt.args.chal); got != tt.want {
t.Errorf("challengeKey() = %v, want %v", got, tt.want)
}
})
}
}
func TestDNSManagerCleanUpRecordPreservesProviderData(t *testing.T) {
provider := &providerDataDeleteProvider{t: t}
manager := DNSManager{
DNSProvider: provider,
PropagationTimeout: time.Second,
}
err := manager.cleanUpRecord(context.Background(), zoneRecord{
zone: "example.com.",
record: libdns.TXT{
Name: "_acme-challenge",
Text: "token",
TTL: time.Minute,
ProviderData: map[string]string{
"id": "123",
},
},
})
if err != nil {
t.Fatalf("cleanup failed: %v", err)
}
if !provider.deleted {
t.Fatal("expected DeleteRecords to be called")
}
}
type providerDataDeleteProvider struct {
t *testing.T
deleted bool
}
func (p *providerDataDeleteProvider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return records, nil
}
func (p *providerDataDeleteProvider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.deleted = true
if zone != "example.com." {
p.t.Fatalf("expected zone example.com., got %q", zone)
}
if len(records) != 1 {
p.t.Fatalf("expected 1 record, got %d", len(records))
}
txt, ok := records[0].(libdns.TXT)
if !ok {
p.t.Fatalf("expected libdns.TXT with provider data, got %T", records[0])
}
pd, ok := txt.ProviderData.(map[string]string)
if !ok {
p.t.Fatalf("expected ProviderData map, got %T", txt.ProviderData)
}
if pd["id"] != "123" {
p.t.Fatalf("expected provider ID 123, got %q", pd["id"])
}
return records, nil
}
func TestGetACMEChallenge_IPv6Brackets(t *testing.T) {
// Store a challenge under a bare IPv6 identifier (as CertMagic does internally).
bare := "::1"
activeChallengesMu.Lock()
activeChallenges[bare] = Challenge{}
activeChallengesMu.Unlock()
defer func() {
activeChallengesMu.Lock()
delete(activeChallenges, bare)
activeChallengesMu.Unlock()
}()
// Lookup with bracketed IPv6 (as received from Go's HTTP server via r.Host).
if _, ok := GetACMEChallenge("[::1]"); !ok {
t.Error("GetACMEChallenge(\"[::1]\") should find challenge stored under \"::1\"")
}
// Lookup with bare IPv6 should still work.
if _, ok := GetACMEChallenge("::1"); !ok {
t.Error("GetACMEChallenge(\"::1\") should find challenge stored under \"::1\"")
}
}
func TestTryListen(t *testing.T) {
// Make sure that a regular TCP address still works.
regularLn, err := robustTryListen("127.0.0.1:8080")
if err != nil {
t.Fatalf("robustTryListen with regular address: %v", err)
}
regularLn.Close()
// The rest of the tests only make sense on Unix-like systems
if !filedescriptor.IsUnix {
t.Skip("file descriptor tests only work on Unix-like systems")
}
// Create a new file descriptor containing a TCP listening socket
fdLn, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
t.Fatalf("net.Listen: %v", err)
}
defer fdLn.Close()
// Get the pseudo-address of the file descriptor.
fd, err := fdLn.(*net.TCPListener).File()
if err != nil {
t.Fatalf("TCPListener.File: %v", err)
}
correctName := "fd/" + strconv.FormatUint(uint64(fd.Fd()), 10)
// Make sure that we can listen on the file descriptor.
correctLn, err := robustTryListen(correctName)
if err != nil {
t.Fatalf("robustTryListen: %v", err)
}
correctLn.Close()
// Make sure that it still works when we add a port number.
correctLn, err = robustTryListen(correctName + ":80")
if err != nil {
t.Fatalf("robustTryListen with port: %v", err)
}
correctLn.Close()
// Make up a fake file descriptor that shouldn't exist.
fakeName := "fd/123456"
_, err = robustTryListen(fakeName)
if err == nil {
t.Fatalf("robustTryListen(%q) should have failed", fakeName)
}
}