-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathctl_test.go
More file actions
633 lines (605 loc) · 22 KB
/
ctl_test.go
File metadata and controls
633 lines (605 loc) · 22 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package actions
import (
"bytes"
"strings"
"testing"
"github.com/corazawaf/coraza/v3/debuglog"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/internal/memoize"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
)
func TestCtl(t *testing.T) {
tests := map[string]struct {
input string
prepareTX func(tx *corazawaf.Transaction)
checkTX func(t *testing.T, tx *corazawaf.Transaction, logEntry string)
}{
"ruleRemoveTargetById": {
input: "ruleRemoveTargetById=123",
},
"ruleRemoveTargetById range": {
// Rule 1 is in WAF; range 1-5 should match it without error
input: "ruleRemoveTargetById=1-5;ARGS:test",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToNotContain := "Invalid range"; strings.Contains(logEntry, wantToNotContain) {
t.Errorf("unexpected error in log: %q", logEntry)
}
},
},
"ruleRemoveTargetById regex key": {
// Rule 1 is in WAF; the regex /^test.*/ should remove matching ARGS targets
input: "ruleRemoveTargetById=1;ARGS:/^test.*/",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if strings.Contains(logEntry, "Invalid") || strings.Contains(logEntry, "invalid") {
t.Errorf("unexpected error in log: %q", logEntry)
}
},
},
"ruleRemoveTargetByTag": {
input: "ruleRemoveTargetByTag=tag1",
},
"ruleRemoveTargetByMsg": {
input: "ruleRemoveTargetByMsg=somethingWentWrong",
},
"auditEngine incorrect": {
input: "auditEngine=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "Invalid status", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"auditEngine successfully": {
input: "auditEngine=Off",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if tx.AuditEngine != types.AuditEngineOff {
t.Error("Failed to disable audit log")
}
},
},
"auditLogParts": {
input: "auditLogParts=ABZ",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := types.AuditLogPartRequestHeaders, tx.AuditLogParts[1]; want != have {
t.Errorf("Failed to set audit log parts, want %s, have %s", string(want), string(have))
}
},
},
"forceRequestBodyVariable incorrect": {
input: "forceRequestBodyVariable=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "X", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"forceRequestBodyVariable successfully": {
input: "forceRequestBodyVariable=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := true, tx.ForceRequestBodyVariable; want != have {
t.Errorf("Failed to set forceRequestBodyVariable, want %t, have %t", want, have)
}
},
},
"requestBodyAccess incorrect": {
input: "requestBodyAccess=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Unknown toggle", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"requestBodyAccess too late": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
},
input: "requestBodyAccess=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change request body access after request headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"requestBodyAccess successfully": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
},
input: "requestBodyAccess=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := true, tx.RequestBodyAccess; want != have {
t.Errorf("Failed to set requestBodyAccess, want %t, have %t", want, have)
}
},
},
"requestBodyProcessor too late": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
},
input: "requestBodyProcessor=JSON",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change request body processor after request headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"requestBodyProcessor successfully": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
},
input: "requestBodyProcessor=XML",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := "XML", tx.Variables().RequestBodyProcessor().Get(); want != have {
t.Errorf("Failed to set requestBodyProcessor, want %s, have %s", want, have)
}
},
},
"requestBodyLimit incorrect": {
input: "requestBodyLimit=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Invalid limit", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"requestBodyLimit too late": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
},
input: "requestBodyLimit=12345",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change request body limit after request headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"requestBodyLimit successfully": {
input: "requestBodyLimit=12345",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := int64(12345), tx.RequestBodyLimit; want != have {
t.Errorf("Failed to set requestBodyLimit, want %d, have %d", want, have)
}
},
},
"ruleEngine incorrect": {
input: "ruleEngine=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "Invalid status", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"ruleEngine successfully": {
input: "ruleEngine=Off",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := types.RuleEngineOff, tx.RuleEngine; want != have {
t.Errorf("Failed to set ruleEngine, want %s, have %s", want, have)
}
},
},
"ruleRemoveById": {
input: "ruleRemoveById=123",
},
"ruleRemoveById range": {
input: "ruleRemoveById=1-3",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if len(tx.GetRuleRemoveByIDRanges()) != 1 {
t.Errorf("expected 1 range entry, got %d", len(tx.GetRuleRemoveByIDRanges()))
return
}
rng := tx.GetRuleRemoveByIDRanges()[0]
if rng[0] != 1 || rng[1] != 3 {
t.Errorf("unexpected range [%d, %d], want [1, 3]", rng[0], rng[1])
}
},
},
"ruleRemoveById incorrect": {
input: "ruleRemoveById=W",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Invalid rule ID", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"ruleRemoveById range incorrect": {
input: "ruleRemoveById=a-2",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Invalid range", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"ruleRemoveByMsg": {
input: "ruleRemoveByMsg=somethingWentWrong",
},
"ruleRemoveByTag": {
input: "ruleRemoveByTag=tag1",
},
"requestBodyProcessor": {
input: "requestBodyProcessor=XML",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := tx.Variables().RequestBodyProcessor().Get(), "XML"; want != have {
t.Errorf("failed to set requestBodyProcessor, want %s, have %s", want, have)
}
},
},
"responseBodyAccess incorrect": {
input: "responseBodyAccess=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Unknown toggle", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"responseBodyAccess too late": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
tx.ProcessResponseHeaders(200, "HTTP/1.1")
_, _ = tx.ProcessResponseBody()
},
input: "responseBodyAccess=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change response body access after response headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"responseBodyAccess successfully": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
},
input: "responseBodyAccess=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := true, tx.ResponseBodyAccess; want != have {
t.Errorf("Failed to set responseBodyAccess, want %t, have %t", want, have)
}
},
},
"responseBodyLimit successfully": {
input: "responseBodyLimit=12345",
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
tx.ProcessRequestHeaders()
},
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if tx.ResponseBodyLimit != 12345 {
t.Errorf("Failed to set response body limit, want %d, have %d", 12345, tx.ResponseBodyLimit)
}
},
},
"responseBodyLimit too late": {
input: "responseBodyLimit=12345",
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
tx.ProcessResponseHeaders(200, "HTTP/1.1")
_, _ = tx.ProcessResponseBody()
},
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change response body access after response headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"responseBodyLimit incorrect": {
input: "responseBodyLimit=a",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Invalid limit", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"responseBodyProcessor successfully": {
input: "responseBodyProcessor=XML",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := tx.Variables().ResponseBodyProcessor().Get(), "XML"; want != have {
t.Errorf("failed to set requestBodyProcessor, want %s, have %s", want, have)
}
},
},
"responseBodyProcessor too late": {
prepareTX: func(tx *corazawaf.Transaction) {
tx.ProcessRequestHeaders()
_, _ = tx.ProcessRequestBody()
tx.ProcessResponseHeaders(200, "HTTP/1.1")
_, _ = tx.ProcessResponseBody()
},
input: "responseBodyProcessor=XML",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[WARN] Cannot change response body access after response headers phase", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"forceResponseBodyVariable incorrect": {
input: "forceResponseBodyVariable=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "X", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"forceResponseBodyVariable successfully": {
input: "forceResponseBodyVariable=On",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if want, have := true, tx.ForceResponseBodyVariable; want != have {
t.Errorf("Failed to set forceResponseBodyVariable, want %t, have %t", want, have)
}
},
},
"debugLogLevel incorrect": {
input: "debugLogLevel=X",
checkTX: func(t *testing.T, tx *corazawaf.Transaction, logEntry string) {
if wantToContain, have := "[ERROR] Invalid log level", logEntry; !strings.Contains(have, wantToContain) {
t.Errorf("Failed to log entry, want to contain %q, have %q", wantToContain, have)
}
},
},
"debugLogLevel successfully": {
input: "debugLogLevel=1",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
logsBuf := &bytes.Buffer{}
defer logsBuf.Reset()
logger := debuglog.Default().
WithLevel(debuglog.LevelWarn).
WithOutput(logsBuf)
waf := corazawaf.NewWAF()
waf.Logger = logger
r := corazawaf.NewRule()
r.ID_ = 1
r.LogID_ = "1"
err := waf.Rules.Add(r)
if err != nil {
t.Fatalf("failed to add rule: %s", err.Error())
}
a := ctl()
if err := a.Init(r, test.input); err != nil {
t.Fatalf("failed to init ctl: %s", err.Error())
}
tx := waf.NewTransaction()
if test.prepareTX != nil {
test.prepareTX(tx)
}
a.Evaluate(r, tx)
if test.checkTX == nil {
// TODO(jcchavezs): for some tests we can't do any assertion
// without going too deep into the implementation details.
// t.SkipNow() can't be used because tinygo doesn't support it.
// https://github.com/tinygo-org/tinygo/blob/release/src/testing/testing.go#L246
return
} else {
test.checkTX(t, tx, logsBuf.String())
}
})
}
}
func TestParseCtl(t *testing.T) {
t.Run("invalid ctl", func(t *testing.T) {
ctl, _, _, _, _, err := parseCtl("invalid", nil)
if err == nil {
t.Errorf("expected error, got nil")
}
if ctl != ctlUnknown {
t.Errorf("expected ctlUnknown, got %d", ctl)
}
})
t.Run("malformed ctl", func(t *testing.T) {
ctl, _, _, _, _, err := parseCtl("unknown=", nil)
if err == nil {
t.Errorf("expected error, got nil")
}
if ctl != ctlUnknown {
t.Errorf("expected ctlUnknown, got %d", ctl)
}
})
t.Run("invalid regex in colKey", func(t *testing.T) {
_, _, _, _, _, err := parseCtl("ruleRemoveTargetById=1;ARGS:/[invalid/", nil)
if err == nil {
t.Errorf("expected error for invalid regex, got nil")
}
})
t.Run("empty regex pattern in colKey", func(t *testing.T) {
_, _, _, _, _, err := parseCtl("ruleRemoveTargetById=1;ARGS://", nil)
if err == nil {
t.Errorf("expected error for empty regex pattern, got nil")
}
})
t.Run("escaped slash not treated as regex", func(t *testing.T) {
_, _, _, key, rx, err := parseCtl(`ruleRemoveTargetById=1;ARGS:/user\/`, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if rx != nil {
t.Errorf("expected nil regex for escaped-slash key, got: %s", rx.String())
}
if key != `/user\/` {
t.Errorf("unexpected key, want %q, have %q", `/user\/`, key)
}
})
t.Run("memoizer with valid regex", func(t *testing.T) {
m := memoize.NewMemoizer(99)
_, _, _, _, keyRx, err := parseCtl("ruleRemoveTargetById=1;ARGS:/^test.*/", m)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if keyRx == nil {
t.Error("expected non-nil compiled regex, got nil")
}
})
t.Run("memoizer with invalid regex", func(t *testing.T) {
m := memoize.NewMemoizer(100)
_, _, _, _, _, err := parseCtl("ruleRemoveTargetById=1;ARGS:/[invalid/", m)
if err == nil {
t.Error("expected error for invalid regex with memoizer, got nil")
}
})
tCases := []struct {
input string
expectAction ctlFunctionType
expectValue string
expectCollection variables.RuleVariable
expectKey string
expectKeyRx string
}{
{"auditEngine=On", ctlAuditEngine, "On", variables.Unknown, "", ""},
{"auditLogParts=A", ctlAuditLogParts, "A", variables.Unknown, "", ""},
{"requestBodyAccess=On", ctlRequestBodyAccess, "On", variables.Unknown, "", ""},
{"requestBodyLimit=100", ctlRequestBodyLimit, "100", variables.Unknown, "", ""},
{"requestBodyProcessor=JSON", ctlRequestBodyProcessor, "JSON", variables.Unknown, "", ""},
{"forceRequestBodyVariable=On", ctlForceRequestBodyVariable, "On", variables.Unknown, "", ""},
{"responseBodyAccess=On", ctlResponseBodyAccess, "On", variables.Unknown, "", ""},
{"responseBodyLimit=100", ctlResponseBodyLimit, "100", variables.Unknown, "", ""},
{"responseBodyProcessor=JSON", ctlResponseBodyProcessor, "JSON", variables.Unknown, "", ""},
{"forceResponseBodyVariable=On", ctlForceResponseBodyVariable, "On", variables.Unknown, "", ""},
{"ruleEngine=On", ctlRuleEngine, "On", variables.Unknown, "", ""},
{"ruleRemoveById=1", ctlRuleRemoveByID, "1", variables.Unknown, "", ""},
{"ruleRemoveById=1-9", ctlRuleRemoveByID, "1-9", variables.Unknown, "", ""},
{"ruleRemoveByMsg=MY_MSG", ctlRuleRemoveByMsg, "MY_MSG", variables.Unknown, "", ""},
{"ruleRemoveByTag=MY_TAG", ctlRuleRemoveByTag, "MY_TAG", variables.Unknown, "", ""},
{"ruleRemoveTargetByMsg=MY_MSG;ARGS:user", ctlRuleRemoveTargetByMsg, "MY_MSG", variables.Args, "user", ""},
{"ruleRemoveTargetById=2;REQUEST_FILENAME:", ctlRuleRemoveTargetByID, "2", variables.RequestFilename, "", ""},
{"ruleRemoveTargetById=2;ARGS:/^json\\.\\d+\\.description$/", ctlRuleRemoveTargetByID, "2", variables.Args, "", `^json\.\d+\.description$`},
}
for _, tCase := range tCases {
testName, _, _ := strings.Cut(tCase.input, "=")
t.Run(testName, func(t *testing.T) {
action, value, collection, colKey, colKeyRx, err := parseCtl(tCase.input, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if action != tCase.expectAction {
t.Errorf("unexpected action, want: %d, have: %d", tCase.expectAction, action)
}
if value != tCase.expectValue {
t.Errorf("unexpected value, want: %s, have: %s", tCase.expectValue, value)
}
if collection != tCase.expectCollection {
t.Errorf("unexpected collection, want: %s, have: %s", tCase.expectCollection.Name(), collection.Name())
}
if colKey != tCase.expectKey {
t.Errorf("unexpected key, want: %s, have: %s", tCase.expectKey, colKey)
}
if tCase.expectKeyRx == "" {
if colKeyRx != nil {
t.Errorf("unexpected non-nil regex, have: %s", colKeyRx.String())
}
} else {
if colKeyRx == nil {
t.Errorf("expected non-nil regex matching %q, got nil", tCase.expectKeyRx)
} else if colKeyRx.String() != tCase.expectKeyRx {
t.Errorf("unexpected regex, want: %s, have: %s", tCase.expectKeyRx, colKeyRx.String())
}
}
})
}
}
func TestCtlParseIDOrRange(t *testing.T) {
tCases := []struct {
input string
expectStart int
expectEnd int
expectErr bool
}{
{"1-2", 1, 2, false},
{"4-5", 4, 5, false},
{"4-15", 4, 15, false},
{"5", 5, 5, false},
{"", 0, 0, true},
{"test", 0, 0, true},
{"test-2", 0, 0, true},
{"2-test", 0, 0, true},
{"-", 0, 0, true},
{"4-5-15", 0, 0, true},
}
for _, tCase := range tCases {
t.Run(tCase.input, func(t *testing.T) {
start, end, err := parseIDOrRange(tCase.input)
if tCase.expectErr && err == nil {
t.Error("expected error for input")
}
if !tCase.expectErr && err != nil {
t.Errorf("unexpected error for input: %s", err.Error())
}
if !tCase.expectErr {
if start != tCase.expectStart {
t.Errorf("unexpected start, want %d, have %d", tCase.expectStart, start)
}
if end != tCase.expectEnd {
t.Errorf("unexpected end, want %d, have %d", tCase.expectEnd, end)
}
}
})
}
}
func TestCtlParseRange(t *testing.T) {
tCases := []struct {
input string
expectStart int
expectEnd int
expectErr bool
}{
{"1-2", 1, 2, false},
{"4-15", 4, 15, false},
{"5-5", 5, 5, false},
{"test-2", 0, 0, true},
{"2-test", 0, 0, true},
{"5-4", 0, 0, true}, // start > end
{"-", 0, 0, true},
{"nodash", 0, 0, true}, // no range separator
}
for _, tCase := range tCases {
t.Run(tCase.input, func(t *testing.T) {
start, end, err := parseRange(tCase.input)
if tCase.expectErr && err == nil {
t.Error("expected error for input")
}
if !tCase.expectErr && err != nil {
t.Errorf("unexpected error for input: %s", err.Error())
}
if !tCase.expectErr {
if start != tCase.expectStart {
t.Errorf("unexpected start, want %d, have %d", tCase.expectStart, start)
}
if end != tCase.expectEnd {
t.Errorf("unexpected end, want %d, have %d", tCase.expectEnd, end)
}
}
})
}
}
func TestParseOnOff(t *testing.T) {
tCases := []struct {
val string
expectedVal bool
expectedOK bool
}{
{"on", true, true},
{"ON", true, true},
{"On", true, true},
{"off", false, true},
{"OFF", false, true},
{"Off", false, true},
{"Whatever", false, false},
}
for _, tCase := range tCases {
t.Run(tCase.val, func(t *testing.T) {
val, ok := parseOnOff(tCase.val)
if want, have := tCase.expectedOK, ok; want != have {
t.Errorf("unexpected OK, want: %t, have: %t", want, have)
}
if want, have := tCase.expectedVal, val; want != have {
t.Errorf("unexpected value, want: %t, have: %t", want, have)
}
})
}
}