-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathfilter_fuzz_test.go
More file actions
1595 lines (1406 loc) · 38.3 KB
/
filter_fuzz_test.go
File metadata and controls
1595 lines (1406 loc) · 38.3 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pongo2
import (
"fmt"
"regexp"
"strings"
"testing"
)
// FuzzFilterEscapejs fuzzes the escapejs filter for JavaScript escaping security.
func FuzzFilterEscapejs(f *testing.F) {
// Basic strings
f.Add("")
f.Add("hello")
f.Add("Hello World")
f.Add("hello world")
// JavaScript-sensitive characters
f.Add("'")
f.Add("\"")
f.Add("\\")
f.Add("/")
f.Add("<")
f.Add(">")
f.Add("&")
f.Add("=")
f.Add("`")
f.Add("$")
// Escape sequences
f.Add("\n")
f.Add("\r")
f.Add("\t")
f.Add("\\n")
f.Add("\\r")
f.Add("\\t")
f.Add("\\\n")
f.Add("\\\\n")
f.Add("\n\r\t")
f.Add("\\n\\r\\t")
f.Add("\\\\\n")
f.Add("line1\nline2")
f.Add("line1\\nline2")
f.Add("col1\tcol2")
f.Add("return\rhere")
// XSS attack vectors
f.Add("<script>alert('xss')</script>")
f.Add(`<script>alert("xss")</script>`)
f.Add("</script><script>alert('xss')</script>")
f.Add("';alert('xss');//")
f.Add(`";alert("xss");//`)
f.Add("javascript:alert('xss')")
f.Add("onerror=alert('xss')")
f.Add("<img src=x onerror=alert('xss')>")
f.Add("<svg/onload=alert('xss')>")
// Template injection attempts
f.Add("${alert('xss')}")
f.Add("{{alert('xss')}}")
f.Add("{{constructor.constructor('alert(1)')()}}")
// Unicode
f.Add("你好世界")
f.Add("こんにちは")
f.Add("Привіт світ")
f.Add("🎉🎊🎁")
f.Add("\u0000")
f.Add("\u001F")
f.Add("\u007F")
f.Add("\u0080")
f.Add("\u00FF")
f.Add("\u0100")
f.Add("\u2028") // Line separator
f.Add("\u2029") // Paragraph separator
f.Add("\uFEFF") // BOM
f.Add(string([]byte{0xED, 0xA0, 0x80})) // High surrogate (invalid UTF-8)
f.Add(string([]byte{0xED, 0xBF, 0xBF})) // Low surrogate (invalid UTF-8)
f.Add("\uFFFD") // Replacement char
// Control characters
f.Add("\x00")
f.Add("\x01")
f.Add("\x1F")
f.Add("\x7F")
f.Add("\x00\x01\x02\x03\x04\x05")
f.Add("hello\x00world")
// Mixed content
f.Add("hello'world")
f.Add(`hello"world`)
f.Add("hello\\world")
f.Add("hello\nworld")
f.Add("hello'\"\\world")
f.Add("It's a \"test\"")
f.Add(`She said "hello"`)
f.Add("path\\to\\file")
f.Add("C:\\Users\\test")
f.Add("\\\\server\\share")
// Edge cases
f.Add("\\")
f.Add("\\\\")
f.Add("\\\\\\")
f.Add("'")
f.Add("''")
f.Add("'''")
f.Add("\"")
f.Add("\"\"")
f.Add("\"\"\"")
f.Add("\\'\"\\/")
// Long strings
f.Add(strings.Repeat("a", 1000))
f.Add(strings.Repeat("'", 100))
f.Add(strings.Repeat("\\", 100))
f.Add(strings.Repeat("\\n", 100))
f.Add(strings.Repeat("\n", 100))
// Invalid UTF-8
f.Add(string([]byte{0x80}))
f.Add(string([]byte{0xFF}))
f.Add(string([]byte{0xC0, 0x80}))
f.Add(string([]byte{0xED, 0xA0, 0x80}))
f.Fuzz(func(t *testing.T, input string) {
result, err := filterEscapejs(AsValue(input), nil)
if err != nil {
t.Fatalf("escapejs returned error: %v", err)
}
output := result.String()
// Verify basic safety: no unescaped dangerous characters
// (except alphanumeric, space, and /)
for i := 0; i < len(output); i++ {
c := output[i]
if c == '\\' && i+1 < len(output) {
// Skip the next char (it's escaped)
i++
continue
}
// These should be escaped in JS strings
if c == '\n' || c == '\r' || c == '\'' || c == '"' {
t.Errorf("escapejs output contains unescaped dangerous char %q at position %d: input=%q, output=%q",
c, i, input, output)
}
}
})
}
// FuzzFilterUrlize fuzzes the urlize filter for URL/email detection.
func FuzzFilterUrlize(f *testing.F) {
// Basic URLs
f.Add("http://example.com")
f.Add("https://example.com")
f.Add("http://www.example.com")
f.Add("https://www.example.com")
f.Add("www.example.com")
f.Add("example.com")
// URLs with paths
f.Add("http://example.com/path")
f.Add("http://example.com/path/to/page")
f.Add("http://example.com/path?query=1")
f.Add("http://example.com/path?a=1&b=2")
f.Add("http://example.com/path#anchor")
f.Add("http://example.com/path?q=1#anchor")
// URLs with ports
f.Add("http://example.com:8080")
f.Add("http://example.com:8080/path")
f.Add("http://localhost:3000")
// URLs with authentication
f.Add("http://[email protected]")
f.Add("http://user:[email protected]")
// Email addresses
f.Add("[email protected]")
f.Add("[email protected]")
f.Add("[email protected]")
f.Add("[email protected]")
f.Add("[email protected]")
// Various TLDs
f.Add("example.org")
f.Add("example.net")
f.Add("example.io")
f.Add("example.dev")
f.Add("example.ai")
f.Add("example.app")
f.Add("example.co")
f.Add("example.me")
f.Add("example.tv")
f.Add("example.info")
f.Add("example.biz")
f.Add("example.edu")
f.Add("example.gov")
// Country code TLDs
f.Add("example.uk")
f.Add("example.de")
f.Add("example.fr")
f.Add("example.jp")
f.Add("example.cn")
f.Add("example.ru")
f.Add("example.br")
f.Add("example.au")
// Mixed content
f.Add("Visit http://example.com today!")
f.Add("Check out www.example.com for more.")
f.Add("Contact [email protected] for help.")
f.Add("See http://a.com and http://b.com")
f.Add("Email me at [email protected] or [email protected]")
f.Add("Visit example.com or example.org")
// Edge cases
f.Add("")
f.Add(" ")
f.Add(" ")
f.Add("no urls here")
f.Add("almost.notld")
f.Add("http://")
f.Add("https://")
f.Add("www.")
f.Add("@")
f.Add("user@")
f.Add("@example.com")
// Unicode domains
f.Add("http://例え.jp")
f.Add("http://münchen.de")
f.Add("http://україна.укр")
// URLs with spaces (should not match)
f.Add("http://example .com")
f.Add("example .com")
// Special characters
f.Add("http://example.com/path with spaces")
f.Add("http://example.com/path%20encoded")
f.Add("http://example.com/<script>")
f.Add("javascript:alert('xss')")
// Long inputs
f.Add(strings.Repeat("http://example.com ", 50))
f.Add(strings.Repeat("[email protected] ", 50))
f.Add("http://" + strings.Repeat("a", 100) + ".com")
// Malformed
f.Add("http://example..com")
f.Add("http://.example.com")
f.Add("http://example.com.")
f.Add("user@@example.com")
f.Add("user@example@com")
f.Add("...")
f.Add("http:///")
f.Add("http://./")
f.Fuzz(func(t *testing.T, input string) {
// Test urlize with default autoescape
result, err := filterUrlize(AsValue(input), AsValue(true))
if err != nil {
// Some inputs may cause errors (very long inputs, etc.)
return
}
_ = result.String()
// Test urlize without autoescape
result2, err := filterUrlize(AsValue(input), AsValue(false))
if err != nil {
return
}
_ = result2.String()
})
}
// FuzzFilterSlice fuzzes the slice filter for Python-style slicing.
func FuzzFilterSlice(f *testing.F) {
// Input strings with various slice parameters
f.Add("hello", ":")
f.Add("hello", "0:")
f.Add("hello", ":5")
f.Add("hello", "0:5")
f.Add("hello", "1:4")
f.Add("hello", "1:")
f.Add("hello", ":4")
f.Add("hello", "0:0")
f.Add("hello", "5:5")
// Negative indices
f.Add("hello world", "-1:")
f.Add("hello world", ":-1")
f.Add("hello world", "-5:")
f.Add("hello world", ":-5")
f.Add("hello world", "-5:-1")
f.Add("hello world", "-1:-5")
f.Add("hello world", "-100:")
f.Add("hello world", ":-100")
f.Add("hello world", "-100:-100")
// Out of bounds
f.Add("hello", "0:100")
f.Add("hello", "100:")
f.Add("hello", ":100")
f.Add("hello", "100:200")
f.Add("hello", "-100:100")
// Edge cases
f.Add("", ":")
f.Add("", "0:")
f.Add("", ":0")
f.Add("", "0:0")
f.Add("a", ":")
f.Add("a", "0:")
f.Add("a", ":1")
f.Add("a", "0:1")
f.Add("a", "-1:")
f.Add("a", ":-1")
// Invalid formats
f.Add("hello", "")
f.Add("hello", "1")
f.Add("hello", "1:2:3")
f.Add("hello", "a:b")
f.Add("hello", "::")
f.Add("hello", ":::")
f.Add("hello", " : ")
f.Add("hello", "1 : 2")
// Unicode strings
f.Add("你好世界", ":")
f.Add("你好世界", "0:2")
f.Add("你好世界", "2:")
f.Add("你好世界", "-2:")
f.Add("你好世界", ":-2")
f.Add("こんにちは", "1:4")
f.Add("🎉🎊🎁", "0:2")
f.Add("🎉🎊🎁", "-1:")
// Long strings
f.Add(strings.Repeat("a", 1000), "0:100")
f.Add(strings.Repeat("a", 1000), "900:")
f.Add(strings.Repeat("a", 1000), "-100:")
f.Add(strings.Repeat("a", 1000), ":-100")
// Whitespace
f.Add("hello", " 0 : 5 ")
f.Add("hello", "\t0\t:\t5\t")
// Special numeric values
f.Add("hello", "0:9999999999")
f.Add("hello", "-9999999999:")
f.Fuzz(func(t *testing.T, input, sliceParam string) {
result, err := filterSlice(AsValue(input), AsValue(sliceParam))
if err != nil {
// Errors are expected for invalid slice formats
return
}
// Result should never be longer than input
if result.Len() > AsValue(input).Len() {
t.Errorf("slice result longer than input: input=%q, param=%q, result=%q",
input, sliceParam, result.String())
}
})
}
// FuzzFilterFloatformat fuzzes the floatformat filter for number formatting.
func FuzzFilterFloatformat(f *testing.F) {
// Basic floats with various decimal places
f.Add("3.14159", "0")
f.Add("3.14159", "1")
f.Add("3.14159", "2")
f.Add("3.14159", "3")
f.Add("3.14159", "4")
f.Add("3.14159", "5")
f.Add("3.14159", "10")
// Negative decimal places (trim mode)
f.Add("3.14159", "-1")
f.Add("3.14159", "-2")
f.Add("3.14159", "-3")
f.Add("3.0", "-1")
f.Add("3.0", "-2")
// Zero and near-zero
f.Add("0", "2")
f.Add("0.0", "2")
f.Add("0.00", "2")
f.Add("-0", "2")
f.Add("-0.0", "2")
f.Add("0.001", "2")
f.Add("0.001", "3")
f.Add("0.001", "4")
// Integers
f.Add("42", "0")
f.Add("42", "2")
f.Add("-42", "2")
f.Add("100", "2")
f.Add("100", "-2")
// Large numbers
f.Add("1234567890", "2")
f.Add("9999999999", "2")
f.Add("12345678901234567890", "2")
// Very small numbers
f.Add("0.000000001", "10")
f.Add("0.000000001", "5")
f.Add("1e-10", "15")
f.Add("1e-10", "5")
// Scientific notation
f.Add("1e10", "2")
f.Add("1.5e10", "2")
f.Add("-1e10", "2")
f.Add("1e-5", "10")
f.Add("1.23e5", "2")
f.Add("1.23E5", "2")
// Special float values
f.Add("inf", "2")
f.Add("-inf", "2")
f.Add("nan", "2")
f.Add("Inf", "2")
f.Add("-Inf", "2")
f.Add("NaN", "2")
// Non-numeric strings
f.Add("hello", "2")
f.Add("", "2")
f.Add(" ", "2")
f.Add("3.14abc", "2")
f.Add("abc3.14", "2")
// Edge case parameters
f.Add("3.14159", "")
f.Add("3.14159", "abc")
f.Add("3.14159", "999")
f.Add("3.14159", "1000")
f.Add("3.14159", "1001")
f.Add("3.14159", "-999")
f.Add("3.14159", "-1000")
// Rounding edge cases
f.Add("1.5", "0")
f.Add("2.5", "0")
f.Add("1.25", "1")
f.Add("1.35", "1")
f.Add("1.45", "1")
f.Add("1.55", "1")
f.Add("0.5", "0")
f.Add("0.05", "1")
f.Add("0.005", "2")
// Precision edge cases
f.Add("0.1", "20")
f.Add("0.2", "20")
f.Add("0.3", "20")
f.Add("0.1", "50")
f.Fuzz(func(t *testing.T, input, decimals string) {
result, err := filterFloatformat(AsValue(input), AsValue(decimals))
if err != nil {
// Errors are expected for very large decimal values
return
}
_ = result.String()
})
}
// FuzzFilterTruncate fuzzes the truncatechars and truncatewords filters.
func FuzzFilterTruncate(f *testing.F) {
// Basic truncation
f.Add("Hello World", "5")
f.Add("Hello World", "10")
f.Add("Hello World", "15")
f.Add("Hello World", "20")
// Edge cases
f.Add("Hello World", "0")
f.Add("Hello World", "1")
f.Add("Hello World", "2")
f.Add("Hello World", "-1")
f.Add("Hello World", "-10")
f.Add("", "5")
f.Add("", "0")
f.Add("a", "1")
f.Add("a", "0")
f.Add("a", "2")
// Long strings
f.Add(strings.Repeat("hello ", 100), "10")
f.Add(strings.Repeat("hello ", 100), "50")
f.Add(strings.Repeat("hello ", 100), "100")
f.Add(strings.Repeat("a", 1000), "100")
f.Add(strings.Repeat("a", 1000), "10")
// Unicode
f.Add("你好世界测试", "3")
f.Add("你好世界测试", "5")
f.Add("こんにちは世界", "4")
f.Add("🎉🎊🎁🎀🎁", "3")
f.Add("Ελληνικά", "4")
f.Add("Привіт світ", "5")
// Mixed content
f.Add("Hello 你好 World", "7")
f.Add("Test 🎉 emoji", "6")
f.Add("Mixed 日本語 content", "10")
// HTML content (for truncatechars_html)
f.Add("<p>Hello World</p>", "5")
f.Add("<p><b>Bold</b> text</p>", "5")
f.Add("<div><p>Nested</p></div>", "5")
f.Add("<a href='test'>Link</a>", "3")
f.Add("<script>alert(1)</script>", "5")
f.Add("<p>你好<b>世界</b></p>", "3")
// Malformed HTML
f.Add("<p>Unclosed", "3")
f.Add("No closing</p>", "5")
f.Add("<>Empty tags</>", "5")
f.Add("<<nested>>", "3")
// Special parameters
f.Add("Hello", "")
f.Add("Hello", "abc")
f.Add("Hello", "1.5")
f.Add("Hello", "99999")
// Words truncation
f.Add("one two three four five", "1")
f.Add("one two three four five", "2")
f.Add("one two three four five", "3")
f.Add("one two three four five", "4")
f.Add("one two three four five", "5")
f.Add("one two three four five", "10")
f.Add("single", "1")
f.Add(" spaced words ", "2")
f.Add("multiple\nlines\nhere", "2")
f.Fuzz(func(t *testing.T, input, length string) {
// Test truncatechars
result1, err1 := filterTruncatechars(AsValue(input), AsValue(length))
if err1 == nil {
out := result1.String()
// Verify output length is reasonable
inputLen := len([]rune(input))
paramLen := AsValue(length).Integer()
if paramLen > 0 && len([]rune(out)) > paramLen && len([]rune(out)) > inputLen {
t.Errorf("truncatechars output too long: input=%q, param=%q, output=%q",
input, length, out)
}
}
// Test truncatewords
result2, err2 := filterTruncatewords(AsValue(input), AsValue(length))
if err2 == nil {
_ = result2.String()
}
// Test truncatechars_html
result3, err3 := filterTruncatecharsHTML(AsValue(input), AsValue(length))
if err3 == nil {
_ = result3.String()
}
// Test truncatewords_html
result4, err4 := filterTruncatewordsHTML(AsValue(input), AsValue(length))
if err4 == nil {
_ = result4.String()
}
})
}
// FuzzFilterAdd fuzzes the add filter for number and string addition.
func FuzzFilterAdd(f *testing.F) {
// Integer addition
f.Add("0", "0")
f.Add("1", "1")
f.Add("-1", "1")
f.Add("1", "-1")
f.Add("-1", "-1")
f.Add("100", "200")
f.Add("2147483647", "1")
f.Add("-2147483648", "-1")
// Float addition
f.Add("1.5", "2.5")
f.Add("0.1", "0.2")
f.Add("-1.5", "1.5")
f.Add("3.14159", "2.71828")
f.Add("1e10", "1")
f.Add("1e-10", "1")
// String concatenation
f.Add("hello", "world")
f.Add("", "test")
f.Add("test", "")
f.Add("", "")
f.Add("a", "b")
f.Add("你好", "世界")
f.Add("🎉", "🎊")
// Mixed types
f.Add("123", "abc")
f.Add("abc", "123")
f.Add("1.5", "abc")
f.Add("abc", "1.5")
// Edge cases
f.Add("nan", "1")
f.Add("inf", "1")
f.Add("-inf", "1")
f.Add(" ", "test")
f.Add("test", " ")
// Large numbers
f.Add("99999999999999999999", "1")
f.Add("1", "99999999999999999999")
f.Fuzz(func(t *testing.T, value, param string) {
result, err := filterAdd(AsValue(value), AsValue(param))
if err != nil {
t.Fatalf("add filter error: %v", err)
}
_ = result.String()
})
}
// FuzzFilterPluralize fuzzes the pluralize filter.
func FuzzFilterPluralize(f *testing.F) {
// Basic pluralization
f.Add("0", "")
f.Add("1", "")
f.Add("2", "")
f.Add("-1", "")
f.Add("100", "")
// With suffix
f.Add("0", "s")
f.Add("1", "s")
f.Add("2", "s")
f.Add("0", "es")
f.Add("1", "es")
f.Add("2", "es")
// With singular,plural format
f.Add("0", "y,ies")
f.Add("1", "y,ies")
f.Add("2", "y,ies")
f.Add("0", "child,children")
f.Add("1", "child,children")
f.Add("2", "child,children")
// Edge cases
f.Add("1.5", "s")
f.Add("0.5", "s")
f.Add("-1.5", "s")
f.Add("abc", "s")
f.Add("", "s")
f.Add("1", "")
f.Add("1", ",")
f.Add("1", ",,")
f.Add("1", ",,,")
f.Add("1", "a,b,c,d")
// Unicode
f.Add("1", "猫,猫们")
f.Add("2", "猫,猫们")
f.Fuzz(func(t *testing.T, count, suffix string) {
result, err := filterPluralize(AsValue(count), AsValue(suffix))
if err != nil {
// Errors expected for invalid suffix formats
return
}
_ = result.String()
})
}
// FuzzFilterYesno fuzzes the yesno filter.
func FuzzFilterYesno(f *testing.F) {
// Boolean values
f.Add("true", "")
f.Add("false", "")
f.Add("True", "")
f.Add("False", "")
// With custom values
f.Add("true", "yes,no")
f.Add("false", "yes,no")
f.Add("true", "yes,no,maybe")
f.Add("false", "yes,no,maybe")
f.Add("", "yes,no,maybe")
// Truthy/falsy values
f.Add("1", "yes,no")
f.Add("0", "yes,no")
f.Add("hello", "yes,no")
f.Add("", "yes,no")
f.Add(" ", "yes,no")
// Edge cases
f.Add("true", ",")
f.Add("true", "a,")
f.Add("true", ",b")
f.Add("true", "a,b,c,d")
f.Add("true", "x")
f.Add("true", "")
// Unicode
f.Add("true", "是,否")
f.Add("false", "是,否")
f.Add("true", "はい,いいえ,たぶん")
f.Fuzz(func(t *testing.T, value, options string) {
result, err := filterYesno(AsValue(value), AsValue(options))
if err != nil {
// Errors expected for invalid option formats
return
}
_ = result.String()
})
}
// FuzzFilterStringformat fuzzes the stringformat filter.
func FuzzFilterStringformat(f *testing.F) {
// Integer formats
f.Add("42", "%d")
f.Add("42", "%5d")
f.Add("42", "%-5d")
f.Add("42", "%05d")
f.Add("-42", "%d")
f.Add("0", "%d")
// Float formats
f.Add("3.14159", "%f")
f.Add("3.14159", "%.2f")
f.Add("3.14159", "%10.2f")
f.Add("3.14159", "%-10.2f")
f.Add("3.14159", "%e")
f.Add("3.14159", "%E")
f.Add("3.14159", "%g")
f.Add("3.14159", "%G")
// String formats
f.Add("hello", "%s")
f.Add("hello", "%10s")
f.Add("hello", "%-10s")
f.Add("hello", "%q")
f.Add("hello", "%.3s")
// Hex/octal/binary
f.Add("255", "%x")
f.Add("255", "%X")
f.Add("255", "%o")
f.Add("255", "%b")
f.Add("255", "%#x")
f.Add("255", "%#X")
f.Add("255", "%#o")
f.Add("255", "%#b")
// Character
f.Add("65", "%c")
f.Add("97", "%c")
// Percent
f.Add("test", "%%")
f.Add("test", "100%%")
// Unicode
f.Add("你好", "%s")
f.Add("你好", "%q")
f.Add("🎉", "%s")
// Edge cases
f.Add("", "%s")
f.Add("test", "")
f.Add("test", "no format")
f.Add("test", "%")
f.Add("test", "%%")
f.Add("test", "%%%")
f.Add("test", "%z")
f.Add("test", "%100s")
f.Add("test", "%.100s")
// Complex formats
f.Add("42", "Value: %d")
f.Add("3.14", "Pi is approximately %f")
f.Add("hello", "The word is: %s")
f.Fuzz(func(t *testing.T, value, format string) {
result, err := filterStringformat(AsValue(value), AsValue(format))
if err != nil {
return
}
_ = result.String()
})
}
// FuzzFilterWordwrap fuzzes the wordwrap filter.
func FuzzFilterWordwrap(f *testing.F) {
// Basic wrapping
f.Add("one two three four five", "2")
f.Add("one two three four five", "3")
f.Add("one two three four five", "5")
f.Add("one two three four five", "10")
// Edge cases
f.Add("", "5")
f.Add("single", "5")
f.Add("one two", "0")
f.Add("one two", "-1")
f.Add("one two", "1")
f.Add("one two", "100")
// Long words
f.Add("supercalifragilisticexpialidocious", "5")
f.Add("a b c d e f g h i j", "1")
// Multiple spaces
f.Add("one two three four", "2")
f.Add(" leading spaces", "2")
f.Add("trailing spaces ", "2")
// Newlines
f.Add("one\ntwo\nthree", "2")
f.Add("one two\nthree four", "2")
// Unicode
f.Add("你好 世界 测试", "2")
f.Add("🎉 🎊 🎁 🎀", "2")
f.Fuzz(func(t *testing.T, input, wrapAt string) {
result, err := filterWordwrap(AsValue(input), AsValue(wrapAt))
if err != nil {
return
}
_ = result.String()
})
}
// FuzzFilterCenter fuzzes the center filter.
func FuzzFilterCenter(f *testing.F) {
f.Add("test", "10")
f.Add("test", "20")
f.Add("test", "4")
f.Add("test", "3")
f.Add("test", "0")
f.Add("test", "-1")
f.Add("test", "1")
f.Add("", "10")
f.Add("a", "10")
f.Add("你好", "10")
f.Add("🎉", "10")
f.Add(strings.Repeat("a", 100), "10")
f.Add("test", "1000")
f.Add("test", "10000")
f.Add("test", "100000")
f.Fuzz(func(t *testing.T, input, width string) {
result, err := filterCenter(AsValue(input), AsValue(width))
if err != nil {
// Errors expected for very large widths
return
}
_ = result.String()
})
}
// FuzzFilterLjustRjust fuzzes the ljust and rjust filters.
func FuzzFilterLjustRjust(f *testing.F) {
f.Add("test", "10")
f.Add("test", "4")
f.Add("test", "3")
f.Add("test", "0")
f.Add("test", "-1")
f.Add("", "10")
f.Add("你好", "10")
f.Add("🎉", "10")
f.Add("test", "100")
f.Add("test", "1000")
f.Add("test", "10000")
f.Fuzz(func(t *testing.T, input, width string) {
// Test ljust
result1, err1 := filterLjust(AsValue(input), AsValue(width))
if err1 == nil {
_ = result1.String()
}
// Test rjust
result2, err2 := filterRjust(AsValue(input), AsValue(width))
if err2 == nil {
_ = result2.String()
}
})
}
// FuzzFilterIriencode fuzzes the iriencode filter.
func FuzzFilterIriencode(f *testing.F) {
// Basic URLs
f.Add("/path/to/page")
f.Add("/search?q=test")
f.Add("/path?a=1&b=2")
f.Add("/path#anchor")
// URLs with spaces
f.Add("/path with spaces")
f.Add("/search?q=hello world")
f.Add("path/file name.txt")
// Special characters
f.Add("/path<script>")
f.Add("/path\"quotes\"")
f.Add("/path'apostrophe'")
f.Add("/path&ersand")
// Unicode
f.Add("/你好世界")
f.Add("/こんにちは")
f.Add("/Привіт")
f.Add("/🎉🎊")
f.Add("/path/日本語/test")
// IRI-safe characters that should be preserved
f.Add("/#%[]=:;$&()+,!?*@'~")
f.Add("/path#fragment")
f.Add("/path?query=value")
f.Add("/path[0]")
f.Add("/path:8080")
// Empty and whitespace
f.Add("")
f.Add(" ")
f.Add(" ")
// Long paths
f.Add(strings.Repeat("/path", 100))
f.Add("/" + strings.Repeat("a", 1000))
f.Fuzz(func(t *testing.T, input string) {
result, err := filterIriencode(AsValue(input), nil)
if err != nil {
t.Fatalf("iriencode error: %v", err)
}
_ = result.String()
})
}
// FuzzFilterGetdigit fuzzes the get_digit filter.
func FuzzFilterGetdigit(f *testing.F) {
// Basic numbers
f.Add("123456789", "1")
f.Add("123456789", "2")
f.Add("123456789", "5")
f.Add("123456789", "9")
// Edge cases
f.Add("123456789", "0")
f.Add("123456789", "-1")
f.Add("123456789", "10")
f.Add("123456789", "100")
f.Add("0", "1")
f.Add("1", "1")
f.Add("", "1")
// Non-numeric strings
f.Add("abc", "1")
f.Add("12abc34", "1")
f.Add("12.34", "1")
f.Add("-123", "1")
// Unicode
f.Add("一二三", "1")
f.Add("١٢٣", "1")
// Long numbers
f.Add(strings.Repeat("1", 100), "50")
f.Add(strings.Repeat("9", 100), "1")
f.Fuzz(func(t *testing.T, input, digit string) {
result, err := filterGetdigit(AsValue(input), AsValue(digit))
if err != nil {
return
}
_ = result.String()
})
}
// FuzzFilterSplit fuzzes the split filter.
func FuzzFilterSplit(f *testing.F) {
// Basic splitting