-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_test.go
More file actions
432 lines (367 loc) · 11.4 KB
/
Copy pathclient_test.go
File metadata and controls
432 lines (367 loc) · 11.4 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package allinkl
import (
"context"
"fmt"
"log"
"os"
"sync"
"testing"
"github.com/joho/godotenv"
"github.com/libdns/libdns"
)
func TestGetAllRecords(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
// Call the GetAllRecords method
records, err := p.GetAllRecords(ctx, zone)
if err != nil {
t.Logf("GetAllRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// Remove this condition once the method is properly implemented
if records != nil {
t.Errorf("Expected records to be nil when error occurs, got: %v", records)
}
return
}
t.Logf("Type: %T", records)
log.Println("Records fetched:")
for _, record := range records {
rr := record.RR()
t.Logf("%s (.%s): %s, %s\n", rr.Name, zone, rr.Data, rr.Type)
}
}
func TestAppendRecord(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
record := libdns.RR{
Type: "A",
Name: "test",
Data: "123.123.123.123",
TTL: 3600, // 1 hour
}
// Call the AppendRecords method
newRecord, err := p.AppendRecord(ctx, zone, record)
if err != nil {
t.Logf("AppendRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// if newRecord != nil {
// t.Errorf("Expected records to be nil when error occurs, got: %v", newRecord)
// }
return
}
t.Logf("Type: %T", newRecord)
records, err := p.GetAllRecords(ctx, zone)
if err != nil {
t.Logf("GetAllRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// Remove this condition once the method is properly implemented
if records != nil {
t.Errorf("Expected records to be nil when error occurs, got: %v", records)
}
return
}
log.Println("Records fetched:")
for _, record := range records {
rr := record.RR()
t.Logf("%s (.%s): %s, %s\n", rr.Name, zone, rr.Data, rr.Type)
}
}
func TestSetRecord(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
record := libdns.RR{
Type: "A",
Name: "test",
Data: "124.124.124.124",
TTL: 3600, // 1 hour
}
// Call the SetRecords method
setRecord, err := p.SetRecord(ctx, zone, record)
if err != nil {
t.Logf("SetRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// if records != nil {
// t.Errorf("Expected records to be nil when error occurs, got: %v", records)
// }
return
}
t.Logf("Type: %T", setRecord)
records, err := p.GetAllRecords(ctx, zone)
if err != nil {
t.Logf("GetAllRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// Remove this condition once the method is properly implemented
if records != nil {
t.Errorf("Expected records to be nil when error occurs, got: %v", records)
}
return
}
log.Println("Records fetched:")
for _, record := range records {
rr := record.RR()
t.Logf("%s (.%s): %s, %s\n", rr.Name, zone, rr.Data, rr.Type)
}
}
func TestDeleteRecord(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
record := libdns.RR{
Type: "A",
Name: "test",
Data: "124.124.124.124",
TTL: 3600, // 1 hour
}
// Call the AppendRecords method
deletedRecord, err := p.DeleteRecord(ctx, zone, record)
if err != nil {
t.Logf("AppendRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// if records != nil {
// t.Errorf("Expected records to be nil when error occurs, got: %v", records)
// }
return
}
t.Logf("Type: %T", deletedRecord)
records, err := p.GetAllRecords(ctx, zone)
if err != nil {
t.Logf("GetAllRecords returned error (expected for now): %v", err)
// Since the method is not fully implemented, we expect an error
// Remove this condition once the method is properly implemented
if records != nil {
t.Errorf("Expected records to be nil when error occurs, got: %v", records)
}
return
}
log.Println("Records fetched:")
for _, record := range records {
rr := record.RR()
t.Logf("%s (.%s): %s, %s\n", rr.Name, zone, rr.Data, rr.Type)
}
}
// TestDeleteRecord_UniqueMatchWithDuplicateNames reproduces the scenario
// from libdns/all-inkl#2: two records sharing the same name but different
// values (as happens when solving DNS-01 challenges for a certificate and
// its wildcard counterpart at the same time — both create
// "_acme-challenge.<zone>" TXT records with different tokens). Under the
// old name-only matching, DeleteRecord could remove either one; it must now
// remove only the record whose name, type, AND value all match.
func TestDeleteRecord_UniqueMatchWithDuplicateNames(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
recordKeep := libdns.RR{Type: "TXT", Name: "_acme-challenge-test", Data: "token-keep-me", TTL: 600}
recordDrop := libdns.RR{Type: "TXT", Name: "_acme-challenge-test", Data: "token-delete-me", TTL: 600}
if _, err := p.AppendRecord(ctx, zone, recordKeep); err != nil {
t.Fatalf("failed to create recordKeep: %v", err)
}
t.Cleanup(func() {
_, _ = p.DeleteRecord(ctx, zone, recordKeep)
})
if _, err := p.AppendRecord(ctx, zone, recordDrop); err != nil {
t.Fatalf("failed to create recordDrop: %v", err)
}
// Only recordDrop should be removed.
if _, err := p.DeleteRecord(ctx, zone, recordDrop); err != nil {
t.Fatalf("DeleteRecord(recordDrop) failed: %v", err)
}
records, err := p.GetAllRecords(ctx, zone)
if err != nil {
t.Fatalf("GetAllRecords failed: %v", err)
}
var foundKeep, foundDrop bool
for _, record := range records {
rr := record.RR()
if rr.Type != "TXT" || rr.Name != "_acme-challenge-test" {
continue
}
switch rr.Data {
case recordKeep.Data:
foundKeep = true
case recordDrop.Data:
foundDrop = true
}
}
if !foundKeep {
t.Errorf("recordKeep is missing — DeleteRecord matched the wrong record when two records shared a name")
}
if foundDrop {
t.Errorf("recordDrop is still present — DeleteRecord failed to remove the targeted record")
}
}
// TestConcurrentAppendRecords exercises kasCallMu by firing several
// AppendRecord calls at once, similar to Caddy solving multiple DNS-01
// challenges in parallel (see caddy-dns/all-inkl#2). Before serializing
// calls with a mutex, concurrent requests could race past the flood-delay
// check and get rejected by KAS's flood protection. If this test becomes
// flaky with rate-limit-shaped errors, that's a regression in the
// serialization, not the test.
func TestConcurrentAppendRecords(t *testing.T) {
_ = godotenv.Load()
username := os.Getenv("KAS_USERNAME")
password := os.Getenv("KAS_PASSWORD")
zone := os.Getenv("ZONE")
if username == "" || password == "" {
t.Skip("KAS_USERNAME and KAS_PASSWORD environment variables must be set")
}
p := &Provider{
KasUsername: username,
KasPassword: password,
}
ctx := context.Background()
const n = 5
records := make([]libdns.RR, n)
for i := range records {
records[i] = libdns.RR{
Type: "TXT",
Name: fmt.Sprintf("_acme-challenge-concurrent-%d", i),
Data: fmt.Sprintf("token-%d", i),
TTL: 600,
}
}
t.Cleanup(func() {
for _, r := range records {
_, _ = p.DeleteRecord(ctx, zone, r)
}
})
var wg sync.WaitGroup
errs := make([]error, n)
for i, r := range records {
wg.Add(1)
go func(i int, r libdns.RR) {
defer wg.Done()
_, err := p.AppendRecord(ctx, zone, r)
errs[i] = err
}(i, r)
}
wg.Wait()
for i, err := range errs {
if err != nil {
t.Errorf("concurrent AppendRecord %d failed: %v (check that kasCallMu is serializing calls to the KAS API)", i, err)
}
}
}
// TestFindFaultString is a pure unit test (no credentials or network
// required) covering the bug behind caddy-dns/all-inkl#2: SOAP faults
// returned with a namespace prefix (e.g. "soap:Fault") were previously
// missed entirely because the old check only matched a bare "Fault" key,
// producing an unhelpful generic "invalid response format" error instead of
// the real KAS error message.
func TestFindFaultString(t *testing.T) {
tests := []struct {
name string
input interface{}
wantFound bool
wantString string
}{
{
name: "bare Fault element",
input: map[string]interface{}{
"Fault": map[string]interface{}{
"faultcode": "soap:Client",
"faultstring": "invalid credentials",
},
},
wantFound: true,
wantString: "invalid credentials",
},
{
name: "namespaced fault nested under Envelope/Body",
input: map[string]interface{}{
"soap:Envelope": map[string]interface{}{
"soap:Body": map[string]interface{}{
"soap:Fault": map[string]interface{}{
"faultstring": map[string]interface{}{"#text": "too many requests"},
},
},
},
},
wantFound: true,
wantString: "too many requests",
},
{
name: "successful response has no fault",
input: map[string]interface{}{
"KasApiResponse": map[string]interface{}{
"return": map[string]interface{}{
"item": []interface{}{},
},
},
},
wantFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, found := findFaultString(tt.input)
if found != tt.wantFound {
t.Fatalf("findFaultString() found = %v, want %v", found, tt.wantFound)
}
if found && got != tt.wantString {
t.Errorf("findFaultString() = %q, want %q", got, tt.wantString)
}
})
}
}
// TestExtractText is a pure unit test covering the small helper used by
// findFaultString to pull text out of either a plain string or an mxj
// "{#text: ...}" wrapper map.
func TestExtractText(t *testing.T) {
if s, ok := extractText("plain string"); !ok || s != "plain string" {
t.Errorf(`extractText("plain string") = (%q, %v), want ("plain string", true)`, s, ok)
}
if s, ok := extractText(map[string]interface{}{"#text": "wrapped string"}); !ok || s != "wrapped string" {
t.Errorf(`extractText(wrapped) = (%q, %v), want ("wrapped string", true)`, s, ok)
}
if _, ok := extractText(42); ok {
t.Errorf("extractText(42) expected ok=false for an unsupported type")
}
}