-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsanitize_test.go
More file actions
1033 lines (951 loc) · 37.1 KB
/
sanitize_test.go
File metadata and controls
1033 lines (951 loc) · 37.1 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 sanitize_test
import (
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mrz1836/go-sanitize"
)
// TestAlpha tests the alpha sanitize method
func TestAlpha(t *testing.T) {
tests := []struct {
name string
input string
expected string
typeCase bool
}{
// Basic cases
{"regular string", "Test This String-!123", "TestThisString", false},
{"various symbols", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", false},
{"carriage returns", "\nThis\nThat", "ThisThat", false},
{"quotes and ticks", "“This is a quote with tick`s … ” ☺ ", "Thisisaquotewithticks", false},
{"spaces", "Test This String-!123", "Test This String", true},
{"symbols and spaces", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", true},
{"quotes and spaces", "“This is a quote with tick`s … ” ☺ ", "This is a quote with ticks ", true},
{"carriage returns with spaces", "\nThis\nThat", `ThisThat`, true},
// Edge cases
{"empty string", "", "", false},
{"only special characters", "!@#$%^&*()", "", false},
{"very long string", strings.Repeat("a", 1000), strings.Repeat("a", 1000), false},
{"tabs", "\tThis1\tThat2", `ThisThat`, true},
{"carriage returns with n", "\nThis1\nThat2", `ThisThat`, true},
{"carriage returns with r", "\rThis1\rThat2", `ThisThat`, true},
{"accented characters", "éclair", "éclair", false},
{"greek characters", "Σigma", "Σigma", false},
{"sharp s", "ßeta", "ßeta", false},
{"numbers only", "123456", "", false},
{"spaces only", " ", " ", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Alpha(test.input, test.typeCase)
assert.Equal(t, test.expected, output)
})
}
}
// TestAlphaNumeric tests the alphanumeric sanitize method
func TestAlphaNumeric(t *testing.T) {
tests := []struct {
name string
input string
expected string
typeCase bool
}{
// Basic cases
{"regular string", "Test This String-!123", "TestThisString123", false},
{"symbols", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", false},
{"carriage returns", "\nThis1\nThat2", "This1That2", false},
{"quotes and ticks", "“This is a quote with tick`s … ” ☺ 342", "Thisisaquotewithticks342", false},
{"string with spaces", "Test This String-! 123", "Test This String 123", true},
{"symbols and spaces", `~!@#$%^&*()-_Symbols 123=+[{]};:'"<>,./?`, "Symbols 123", true},
{"ticks and spaces", "“This is a quote with tick`s…”☺ 123", "This is a quote with ticks 123", true},
{"carriage returns with n", "\nThis1\nThat2", `This1That2`, true},
{"carriage returns with r", "\rThis1\rThat2", `This1That2`, true},
{"tabs", "\tThis1\tThat2", `This1That2`, true},
// Edge cases
{"empty string", "", "", false},
{"spaces only", " ", " ", true},
{"accents and numbers", "éclair123", "éclair123", false},
{"mixed unicode", "ßeta Σigma 456", "ßeta Σigma 456", true},
{"numbers only", "987654", "987654", false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.AlphaNumeric(test.input, test.typeCase)
assert.Equal(t, test.expected, output)
})
}
}
// TestBitcoinAddress will test all permutations
func TestBitcoinAddress(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"remove symbol", ":1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove spaces", " 1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs ", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove spaces 2", " 1K6c7 LGpdB 8LwoGNVfG5 1dRV 9UUEijbrWs ", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove symbols 2", "$#:1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove symbols 3", "$#:1K6c_7LGpd^B8Lw_oGN=VfG+51_dRV9-UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
// No uppercase letter O, uppercase letter I, lowercase letter l, and the number 0
{"uppercase letters", "OIl01K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
// Additional edge cases with normal, vanity, and rare addresses
{"vanity address", "1CounterpartyXXXXXXXXXXXXXXXUWLpVr", "1CounterpartyXXXXXXXXXXXXXXXUWLpVr"},
{"burn address", "1111111111111111111114oLvT2", "1111111111111111111114oLvT2"},
{"remove punctuation", "1BoatSLRHtKNngkdXEeobR76b53LETtpyT!!", "1BoatSLRHtKNngkdXEeobR76b53LETtpyT"},
{"remove spaces around", " 17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ ", "17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.BitcoinAddress(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestBitcoinCashAddress will test all permutations of using BitcoinCashAddress()
func TestBitcoinCashAddress(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"remove symbols", "$#:qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz!", "qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz"},
{"remove spaces", " $#:qze7yy2 au5vuznvn8lzj5y0j5t066 vhs75e3m0eptz! ", "qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz"},
{"remove ignored characters", "pqbq3728yw0y47sOqn6l2na30mcw6zm78idzq5ucqzc371", "pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"},
{"basic cashaddr", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"remove punctuation", "bitcoincash:qqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37!!", "tcncashqqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"},
{"remove spaces", " qr95tpm9f6qt8azfzd73ydyccdefhkcdv3ldk00ht0 ", "qr95tpm9f6qt8azfzd73ydyccdefhkcdv3ldk00ht0"},
// Additional test cases
{"empty string", "", ""},
{"only symbols", "!@#$%^&*()", ""},
{"mixed case address", "QPM2QSZNHKS23Z7629MMS6S4CWEF74VCWVY22GDX6A", "QPM2QSZNHKS23Z7629MMS6S4CWEF74VCWVY22GDX6A"},
{"address with newlines", "\nqpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a\n", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"address with tabs", "\tqpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a\t", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"address with unicode", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a世界", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"}, //nolint:gosmopolitan // Unicode characters are not valid in Bitcoin Cash addresses
{"address with dashes", "qpm2qszn-hks23z7629mms6s4cwef74vcwvy22gdx6a", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"address with numbers only", "1234567890", "234567890"},
{"address with prefix and suffix", "!!qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a!!", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"address with internal spaces", "qpm2qszn hks23z7629mms6s4cwef74vcwvy22gdx6a", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"address with mixed valid and invalid", "qpm2qszn!@#hks23z76*29mms6s4cwef74vcwvy22gdx6a", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.BitcoinCashAddress(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestCustom tests the custom sanitize method
func TestCustom(t *testing.T) {
tests := []struct {
name string
input string
expected string
regex string
}{
{"", "ThisWorks123!", "ThisWorks123", `[^a-zA-Z0-9]`},
{"", "ThisWorks1.23!", "1.23", `[^0-9.-]`},
{"", "ThisWorks1.23!", "ThisWorks123", `[^0-9a-zA-Z]`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Custom(test.input, test.regex)
assert.Equal(t, test.expected, output)
})
}
}
// TestCustomCompiled tests the custom compiled sanitize method
func TestCustomCompiled(t *testing.T) {
t.Run("CustomCompiled", func(t *testing.T) {
tests := []struct {
name string
input string
expected string
re *regexp.Regexp
}{
{"alpha numeric", "Works 123!", "Works123", regexp.MustCompile(`[^a-zA-Z0-9]`)},
{"decimal", "ThisWorks1.23!", "1.23", regexp.MustCompile(`[^0-9.-]`)},
{"numbers and letters", "ThisWorks1.23!", "ThisWorks123", regexp.MustCompile(`[^0-9a-zA-Z]`)},
{"CustomCompiled_UnicodePattern", "Héllo 世界!123", "Héllo 世界", regexp.MustCompile(`[^\p{L}\s]`)}, //nolint:gosmopolitan // test includes Unicode characters
{"CustomCompiled_OverlappingMatches", "ababa", "ba", regexp.MustCompile("aba")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := sanitize.CustomCompiled(tt.input, tt.re)
require.NoError(t, err)
require.Equal(t, tt.expected, output)
})
}
})
t.Run("CustomCompiled_NilRegex", func(t *testing.T) {
_, err := sanitize.CustomCompiled("panic", nil)
require.ErrorIs(t, err, sanitize.ErrNilRegexp)
})
t.Run("Custom_InvalidRegexPanics", func(t *testing.T) {
require.Panics(t, func() {
sanitize.Custom("invalid", "(")
})
})
t.Run("Custom_UnicodePattern", func(t *testing.T) {
//nolint:gosmopolitan // test includes Unicode characters
output := sanitize.Custom("Héllo 世界!123", `[^\p{L}\s]`)
//nolint:gosmopolitan // test includes Unicode characters
require.Equal(t, "Héllo 世界", output)
})
t.Run("Custom_OverlappingMatches", func(t *testing.T) {
output := sanitize.Custom("ababa", "aba")
require.Equal(t, "ba", output)
})
}
// TestDecimal tests the decimal sanitize method
func TestDecimal(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", " String: 1.23 ", "1.23"},
{"basic 2", " String: 001.2300 ", "001.2300"},
{"basic 3", " $-1.034234 Price", "-1.034234"},
{"basic 4", " $-1%.034234e Price", "-1.034234"},
{"basic 5", "/n<< $-1.034234 >>/n", "-1.034234"},
// Edge cases
{"empty string", "", ""},
{"letters only", "abc", ""},
{"plus sign", "+100.50", "100.50"},
{"multiple decimals", "1.2.3", "1.23"},
{"embedded minus", "1-2-3", "123"},
{"scientific notation", "1e-3", "1-3"},
{"comma separated", "1,234.56", "1234.56"},
{"leading minus only at start", "abc-123", "-123"},
{"multiple minus signs", "--123", "-123"},
{"minus in middle ignored", "12-34", "1234"},
{"decimal at start", ".123", ".123"},
{"multiple decimals in sequence", "1..2", "1.2"},
{"separated numbers", "1.2 3.4", "1.23.4"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Decimal(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestDomain tests the domain sanitize method
func TestDomain(t *testing.T) {
t.Run("valid cases", func(t *testing.T) {
tests := []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"no domain name",
"",
"",
true,
true,
},
{
"remove leading http",
"http://IAmaDomain.com",
"IAmaDomain.com",
true,
false,
},
{
"remove leading http and lowercase",
"http://IAmaDomain.com",
"iamadomain.com",
false,
false,
},
{
"full url with params",
"https://IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
false,
},
{
"full url with params, remove www",
"https://www.IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
true,
},
{
"full url with params, leave www",
"https://www.IAmaDomain.com/?this=that#plusThis",
"www.iamadomain.com",
false,
false,
},
{
"caps domain, remove www",
"WWW.DOMAIN.COM",
"domain.com",
false,
true,
},
{
"mixed caps domain, remove www",
"WwW.DOMAIN.COM",
"domain.com",
false,
true,
},
{
"mixed caps domain, remove www",
"wwW.DOMAIN.COM",
"DOMAIN.COM",
true,
true,
},
{
"testing new domain",
"mhj.email",
"mhj.email",
true,
true,
},
{
"domain with tabs and spaces",
` domain.com`,
"domain.com",
false,
true,
},
{
"invalid unicode in host",
"https://examplé.com",
"exampl.com",
false,
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := sanitize.Domain(test.input, test.preserveCase, test.removeWww)
require.NoError(t, err)
assert.Equal(t, test.expected, output)
})
}
})
t.Run("invalid cases", func(t *testing.T) {
tests := []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"spaces in domain",
"http://www.I am a domain.com",
"http://www.I am a domain.com",
true,
true,
},
{
"symbol in domain",
"!I_am a domain.com",
"http://!I_am a domain.com",
true,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := sanitize.Domain(test.input, test.preserveCase, test.removeWww)
require.Error(t, err)
assert.Equal(t, test.expected, output)
})
}
})
}
// TestEmail tests the email sanitize method
func TestEmail(t *testing.T) {
tests := []struct {
name string
input string
expected string
preserveCase bool
}{
{"basic_1", "mailto:testME@GmAil.com", "testme@gmail.com", false},
{"basic_2", "test_ME@GmAil.com", "test_me@gmail.com", false},
{"basic_3", "test-ME@GmAil.com", "test-me@gmail.com", false},
{"basic_4", "test.ME@GmAil.com", "test.me@gmail.com", false},
{"basic_5", " test_ME @GmAil.com ", "test_me@gmail.com", false},
{"basic_6", " <<test_ME @GmAil.com!>> ", "test_me@gmail.com", false},
{"basic_7", " test_ME+2@GmAil.com ", "test_me+2@gmail.com", false},
{"basic_8", " test_ME+2@GmAil.com ", "test_ME+2@GmAil.com", true},
// Additional edge cases
{"empty string", "", "", false},
{"spaces only", " ", "", false},
{"symbols only", "!@#$%^&*()", "@", false},
{"invalid email format", "not-an-email", "not-an-email", false},
{"multiple @ symbols", "test@@example.com", "test@@example.com", false},
{"unicode in local part", "tést@exámple.com", "tst@exmple.com", false},
{"unicode in domain", "test@exámple.com", "test@exmple.com", false},
{"email with subdomain", "user@mail.example.com", "user@mail.example.com", false},
{"email with numbers", "user123@123mail.com", "user123@123mail.com", false},
{"email with dash and underscore", "user-name_test@domain-name.com", "user-name_test@domain-name.com", false},
{"email with plus and dot", "user.name+tag@domain.com", "user.name+tag@domain.com", false},
{"email with leading/trailing spaces", " user@domain.com ", "user@domain.com", false},
{"email with mixed case and preserve", "Test@Domain.COM", "Test@Domain.COM", true},
{"email with mixed case and no preserve", "Test@Domain.COM", "test@domain.com", false},
{"email with mailto and preserve", "MailTo:Test@Domain.COM", "Test@Domain.COM", true},
{"email with special chars in local", "user!#$%&'*+/=?^_`{|}~@domain.com", "user+_@domain.com", false},
{"email with special chars in domain", "user@do!main.com", "user@domain.com", false},
{"email with multiple dots", "user..name@domain.com", "user..name@domain.com", false},
{"email with tab and newline", "user@domain.com\t\n", "user@domain.com", false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Email(test.input, test.preserveCase)
assert.Equal(t, test.expected, output)
})
}
}
// TestFirstToUpper tests the first to upper method
func TestFirstToUpper(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"", "thisworks", "Thisworks"},
{"", "Thisworks", "Thisworks"},
{"", "this", "This"},
{"", "t", "T"},
{"", "tt", "Tt"},
{"", "", ""}, // Edge case for empty string
// Additional edge cases:
{"single space: ' '", " ", " "},
{"multiple spaces: ' '", " ", " "},
{"tab character: '\t'", "\t", "\t"},
{"newline character: '\n'", "\n", "\n"},
{"starts with number: '123abc'", "123abc", "123abc"},
{"starts with symbol: '!@#'", "!@#", "!@#"},
{"German sharp S: 'ßeta' (uppercases to 'SS')", "ßeta", "ßeta"},
{"accented character: 'éclair' (should become 'Éclair')", "éclair", "Éclair"},
{"Greek capital letter: 'Σigma' (should remain unchanged)", "Σigma", "Σigma"},
{"Spanish n-tilde: 'ñandú' (should become 'Ñandú')", "ñandú", "Ñandú"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.FirstToUpper(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestFormalName tests the FormalName sanitize method
func TestFormalName(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", "Mark Mc'Cuban-Host", "Mark Mc'Cuban-Host"},
{"basic 2", "Mark Mc'Cuban-Host the SR.", "Mark Mc'Cuban-Host the SR."},
{"basic 3", "Mark Mc'Cuban-Host the Second.", "Mark Mc'Cuban-Host the Second."},
{"basic 4", "Johnny Apple.Seed, Martin", "Johnny Apple.Seed, Martin"},
{"basic 5", "Does #Not Work!", "Does Not Work"},
// Edge cases
{"empty string", "", ""},
{"accented characters", "José María", "José María"},
{"underscores", "Name_With_Underscore", "NameWithUnderscore"},
{"digits", "John Doe 3rd", "John Doe 3rd"},
{"newline", "John\nDoe", "John\nDoe"},
{"leading spaces", " John", " John"},
{"apostrophe and hyphen", "O'Leary-Brown", "O'Leary-Brown"},
{"prefix d'", "d'Artagnan", "d'Artagnan"},
{"curly apostrophe", "D’Angelo", "DAngelo"},
{"multiple spaces", "Van der Meer", "Van der Meer"},
{"accented surname", "Émilie du Châtelet", "Émilie du Châtelet"},
{"foreign letters", "Björk Guðmundsdóttir", "Björk Guðmundsdóttir"},
{"chinese characters", "李明 Wang", "李明 Wang"}, //nolint:gosmopolitan // test includes Unicode characters
{"arabic characters", "أحمد Smith", "أحمد Smith"},
{"cyrillic characters", "Владимир Putin", "Владимир Putin"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.FormalName(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestHTML tests the HTML sanitize method
func TestHTML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"test_1", "<b>This works?</b>", "This works?"},
{"test_2", "<html><b>This works?</b><i></i></br></html>", "This works?"},
{"test_3", "<html><b class='test'>This works?</b><i></i></br></html>", "This works?"},
{"nested tags", "Hello <div>world <span>!</span></div>", "Hello world !"},
{"unclosed tag", "<div>test", "test"},
{"partial closing", "<div>test</div", "test</div"},
{"html comments", "<!-- comment -->text", "text"},
{"script tag remains", "<script>alert('x')</script>", "alert('x')"},
{"comment with script", "<!--comment--><script>alert('x')</script>text", "alert('x')text"},
{"script with comment", "<script><!--evil-->alert('x')</script>", "alert('x')"},
{"comment around script", "<!-- <script>alert('x')</script> -->text", "alert('x') -->text"},
{"unusual attributes", "<div data-x='1' onload='y'>hello</div>", "hello"},
{"nested unclosed", "<div><span>text", "text"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.HTML(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestIPAddress tests the ip address sanitize method
func TestIPAddress(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"basic_1", "192.168.3.6", "192.168.3.6"},
{"basic_2", "255.255.255.255", "255.255.255.255"},
{"basic_3", "304.255.255.255", ""},
{"basic_4", "fail", ""},
{"basic_5", "192-123-122-123", ""},
{"basic_6", "2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f", "2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f"},
{"basic_7", "2602:305:bceb:1bd0:44ef:2:2:2", "2602:305:bceb:1bd0:44ef:2:2:2"},
{"basic_8", "2:2:2:2:2:2:2:2", "2:2:2:2:2:2:2:2"},
{"basic_9", "192.2", ""},
{"basic_10", "192.2!", ""},
{"basic_11", "IP: 192.168.0.1 ", ""},
{"basic_12", " 192.168.0.1 ", "192.168.0.1"},
{"basic_13", " ##!192.168.0.1!## ", "192.168.0.1"},
{"basic_14", ` 192.168.1.1`, "192.168.1.1"},
{"basic_15", `2001:0db8:85a3:0000:0000:8a2e:0370:7334`, "2001:db8:85a3::8a2e:370:7334"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{"basic_16", `2001:0db8::0001:0000`, "2001:db8::1:0"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{"basic_17", `2001:db8:0:0:1:0:0:1`, "2001:db8::1:0:0:1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{"basic_18", `2001:db8:0000:1:1:1:1:1`, "2001:db8:0:1:1:1:1:1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{"basic_19", `0:0:0:0:0:0:0:1`, "::1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{"basic_20", `0:0:0:0:0:0:0:0`, "::"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
// Additional edge cases
{"empty string", "", ""},
{"spaces only", " ", ""},
{"symbols only", "!@#$%^&*()", ""},
{"letters only", "abcdef", ""},
{"ipv4 with trailing dot", "192.168.1.1.", ""},
{"ipv4 with internal spaces", "192. 168. 1. 1", "192.168.1.1"},
{"ipv4 with tabs and newlines", "\t192.168.1.1\n", "192.168.1.1"},
{"ipv6 with uppercase", "2001:DB8:0:0:8:800:200C:417A", "2001:db8::8:800:200c:417a"},
{"ipv6 with brackets", "[2001:db8::1]", "2001:db8::1"},
{"ipv6 with zone index", "fe80::1%lo0", ""},
{"ipv6 with internal spaces", "2001: db8:: 1", "2001:db8::1"},
{"ipv6 with tabs and newlines", "\t2001:db8::1\n", "2001:db8::1"},
{"ipv4-mapped ipv6", "::ffff:192.0.2.128", "192.0.2.128"},
{"ipv6 with embedded ipv4", "::ffff:192.168.1.1", "192.168.1.1"},
{"ipv6 with all zeros", "::", "::"},
{"ipv6 loopback", "::1", "::1"},
{"ipv4 loopback", "127.0.0.1", "127.0.0.1"},
{"ipv4 broadcast", "255.255.255.255", "255.255.255.255"},
{"ipv4 with port", "192.168.1.1:8080", ""},
{"ipv6 with port", "[2001:db8::1]:443", "2001:db8::1:443"},
{"ipv4 with subnet", "192.168.1.1/24", "192.168.1.124"},
{"ipv6 with subnet", "2001:db8::1/64", "2001:db8::164"},
{"ipv4 with prefix text", "IP:192.168.1.1", ""},
{"ipv6 with prefix text", "IPv6:2001:db8::1", ""},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.IPAddress(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestNumeric tests the numeric sanitize method
func TestNumeric(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", " > Test This String-!1234", "1234"},
{"basic 2", " $1.00 Price!", "100"},
// Edge cases
{"empty string", "", ""},
{"letters only", "abcd", ""},
{"negative decimal", "-123.45", "12345"},
{"phone format", "(123) 456-7890", "1234567890"},
{"hex prefix", "0xFF 55", "055"},
{"spaces only", " ", ""},
{"leading plus", "+12345", "12345"},
{"mixed letters digits", "abc123def456", "123456"},
{"long numeric string", strings.Repeat("9", 100), strings.Repeat("9", 100)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Numeric(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestPhoneNumber tests the phone number sanitize method
func TestPhoneNumber(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"US format", "+1 (234) 567-8900", "+12345678900"},
{"International", "+44 20 7946 0958", "+442079460958"},
{"Hyphens", "+1-800-555-0123", "+18005550123"},
{"Periods", "+1.800.555.0123", "+18005550123"},
// Edge cases
{"No plus", "123-456-7890", "1234567890"},
{"Letters and ext", "(555)555-5555 ext. 123", "5555555555123"},
{"Multiple plus", "++123++", "++123++"},
{"empty string", "", ""},
{"spaces only", " ", ""},
{"symbols only", "!@#$%^&*()", ""},
{"invalid characters", "123-abc-4567", "1234567"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.PhoneNumber(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestPathName tests the PathName sanitize method
func TestPathName(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", "My BadPath (10)", "MyBadPath10"},
{"basic 2", "My BadPath (10)[]()!$", "MyBadPath10"},
{"basic 3", "My_Folder-Path-123_TEST", "My_Folder-Path-123_TEST"},
// Edge cases
{"empty string", "", ""},
{"file extension", "myfile.txt", "myfiletxt"},
{"windows path", "C:\\temp\\file.txt", "Ctempfiletxt"},
{"unicode chars", "naïve.txt", "navetxt"},
{"spaces", "dir name/file", "dirnamefile"},
{"valid symbols", "filename-123_ABC", "filename-123_ABC"},
// Additional edge cases
{"leading period", ".hiddenfile", "hiddenfile"},
{"leading dash", "-dashfile", "-dashfile"},
{"consecutive specials", "file--name__", "file--name__"},
{"combining characters", "cafe\u0301.txt", "cafetxt"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.PathName(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestPunctuation tests the punctuation sanitize method
func TestPunctuation(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", "Mark Mc'Cuban-Host", "Mark Mc'Cuban-Host"},
{"basic 2", "Johnny Apple.Seed, Martin", "Johnny Apple.Seed, Martin"},
{"basic 3", "Does #Not Work!", "Does #Not Work!"},
{"basic 4", "Does #Not Work!?", "Does #Not Work!?"},
{"basic 5", "Does #Not Work! & this", "Does #Not Work! & this"},
{"basic 6", `[@"Does" 'this' work?@]this`, `"Does" 'this' work?this`},
{"basic 7", "Does, 123^* Not & Work!?", "Does, 123 Not & Work!?"},
// Edge cases
{"empty string", "", ""},
{"spaces only", " ", " "},
{"tabs and newlines", "line1\nline2\tend", "line1\nline2\tend"},
{"disallowed punctuation", "Hello; world: [test] {case}", "Hello world test case"},
{"unicode punctuation", "¡Hola señor!", "Hola señor!"},
{"accents kept", "Café & crème brûlée?", "Café & crème brûlée?"},
{"underscore and plus", "foo_bar+baz", "foobarbaz"},
{"parentheses", "Need (something); else: yes?", "Need something else yes?"},
{"smart quotes", "She said “Hello”", "She said Hello"},
{"dash variants", "This—is—dash", "Thisisdash"},
{"numbers with punctuation", "Version 2.0.1, (build #1234)", "Version 2.0.1, build #1234"},
{"emoji", "Smile 😊, please!", "Smile , please!"},
{"mixed allowed", `He said: "Go!"`, `He said "Go!"`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Punctuation(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestScientificNotation tests the scientific notation sanitize method
func TestScientificNotation(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Standard cases
{"simple float", " String: 1.23 ", "1.23"},
{"with exponent", " String: 1.23e-3 ", "1.23e-3"},
{"negative exponent", " String: -1.23e-3 ", "-1.23e-3"},
{"leading zeros", " String: 001.2300 ", "001.2300"},
{"prefixed with dollar and word", " $-1.034234 word", "-1.034234"},
{"prefixed with symbols and word", " $-1%.034234e word", "-1.034234e"},
{"wrapped in symbols", "/n<< $-1.034234 >>/n", "-1.034234"},
// Edge cases
{"empty string", "", ""},
{"letters only", "abcde", "e"},
{"uppercase exponent", "1.2E+3", "1.2E+3"},
{"trailing plus", "1.0e+3+", "1.0e+3+"},
{"multiple exponents", "1e2e3", "1e2e3"},
{"comma separated", "1,234.56e7", "1234.56e7"},
{"embedded minus", "1-2.3e4", "1-2.3e4"},
{"arabic digits", "١٢٣.٤٥e٦", "١٢٣.٤٥e٦"},
{"whitespace and newline", "1.2e3\n4.5e6", "1.2e34.5e6"},
{"multiple decimals", "1.2.3e4", "1.2.3e4"},
{"signs only", "+-", "+-"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.ScientificNotation(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestScripts tests the script removal
func TestScripts(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"test_1", "this <script>$('#something').hide()</script>", "this "},
{"test_2", "this <script type='text/javascript'>$('#something').hide()</script>", "this "},
{"test_3", `this <script type="text/javascript" class="something">$('#something').hide();</script>`, "this "},
{"test_4", `this <iframe width="50" class="something"></iframe>`, "this "},
{"test_5", `this <embed width="50" class="something"></embed>`, "this "},
{"test_6", `this <object width="50" class="something"></object>`, "this "},
{"multiple scripts", "pre<script>1</script>mid<script>2</script>post", "prepost"},
{"mismatched tags", "<script>one</iframe>two</script>", ""},
{"uppercase script", "this <SCRIPT>evil()</SCRIPT> works", "this works"},
{"unclosed script", "<script>oops", "<script>oops"},
{"closing only", "oops</script>", "oops</script>"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Scripts(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestSingleLine tests the SingleLine method
func TestSingleLine(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Regular cases
{"basic multiline", "Mark\nMc'Cuban-Host", "Mark Mc'Cuban-Host"},
{"multiline with extra line", "Mark\nMc'Cuban-Host\nsomething else", "Mark Mc'Cuban-Host something else"},
{"leading tab with newlines", "\tMark\nMc'Cuban-Host\nsomething else", " Mark Mc'Cuban-Host something else"},
// Edge cases
{"empty string", "", ""},
{"only whitespace", "\n\r\t\v\f", " "},
{"mixed whitespace", "Line1\r\nLine2\tLine3\vLine4\f", "Line1 Line2 Line3 Line4 "},
{"leading and trailing", "\nStart\t\n", " Start "},
// Additional edge cases
{"very long input", strings.Repeat("line\n", 1000), strings.Repeat("line ", 1000)},
{"unusual control characters", "start\x00middle\x1bend", "start\x00middle\x1bend"},
{"non-ASCII whitespace", "hello\u00A0world\u2028line\u2029break", "hello\u00A0world\u2028line\u2029break"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.SingleLine(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestTime tests the time sanitize method
func TestTime(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic t00:00d", "t00:00d -EST", "00:00"},
{"basic t00:00:00d", "t00:00:00d -EST", "00:00:00"},
{"embedded time", "SOMETHING t00:00:00d -EST DAY", "00:00:00"},
// Edge cases
{"empty string", "", ""},
{"nonsense string", "abc", ""},
{"time with AM/PM", "10:20PM", "10:20"},
{"negative time prefix", "-10:20", "10:20"},
{"subsecond time", "12:34:56.789", "12:34:56789"},
{"whitespace in time", "10\n:20\t:30", "10:20:30"},
{"hyphen separated", "12-34-56", "123456"},
{"only colons", "::", "::"},
{"trailing colon", "12:34:", "12:34:"},
{"unicode digits", "12:34", "1234"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.Time(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestURI tests the URI sanitize method
func TestURI(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"basic 1", "Test?=what! &this=that", "Test?=what&this=that"},
{"basic 2", "Test?=what! &this=/that/!()*^", "Test?=what&this=/that/"},
{"basic 3", "/This/Works/?that=123&this#page10%", "/This/Works/?that=123&this#page10%"},
// Edge cases
{"encoded spaces", "path%20with%20space", "path%20with%20space"},
{"remove colon", "path:to/resource", "pathto/resource"},
{"unicode characters", "/世界/привет", "/世界/привет"}, //nolint:gosmopolitan // Unicode characters are valid in URIs
{"plus sign in query", "/query?name=foo+bar", "/query?name=foobar"},
{"mixed invalid characters", "/path/../to/;evil?x=1^&y=2", "/path//to/evil?x=1&y=2"},
{"trim spaces", " /something ", "/something"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.URI(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestURL tests the URL sanitize method
func TestURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Basic cases
{"remove spaces", "Test?=what! &this=that#works", "Test?=what&this=that#works"},
{"no dollar signs", "/this/test?param$", "/this/test?param"},
{"using at sign", "https://medium.com/@username/some-title-that-is-a-article", "https://medium.com/@username/some-title-that-is-a-article"},
{"removing symbols", "https://domain.com/this/test?param$!@()[]{}'<>", "https://domain.com/this/test?param@"},
{"params and anchors", "https://domain.com/this/test?this=value&another=123%#page", "https://domain.com/this/test?this=value&another=123%#page"},
{"allow commas", "https://domain.com/this/test,this,value", "https://domain.com/this/test,this,value"},
// Edge cases
{"with port", "https://example.com:8080/path", "https://example.com:8080/path"},
{"ipv6 address", "https://[2001:db8::1]/path", "https://2001:db8::1/path"},
{"plus sign in query", "https://example.com?q=foo+bar", "https://example.com?q=foobar"},
{"trim spaces", " https://example.com/test ", "https://example.com/test"},
{"file url path", "file:///C:/Program Files/Test", "file:///C:/ProgramFiles/Test"},
{"with user info", "https://user:pass@example.com/", "https://user:pass@example.com/"},
{"fragment invalid characters", "https://example.com/path#frag!", "https://example.com/path#frag"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.URL(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestXML tests the XML sanitize method
func TestXML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"test_1", `<?xml version="1.0" encoding="UTF-8"?><note>Something</note>`, "Something"},
{"test_2", `<body>This works?</body><title>Something</title>`, "This works?Something"},
{"nested tags", `<root><child>data</child><child2/></root>`, "data"},
{"attributes", `text <tag attr='1'>value</tag> more`, "text value more"},
{"unclosed tag", `<tag>unclosed`, "unclosed"},
{"xml header and comment", `<?xml version='1.0'?><!--comment--><a>1</a>`, "1"},
{"cdata removed", `<a><![CDATA[test]]></a>`, ""},
{"namespaced tag", `<ns:tag>value</ns:tag>`, "value"},
{"namespaced root", `<root xmlns:ns='urn'><ns:tag>val</ns:tag></root>`, "val"},
{"processing instruction", `<?xml-stylesheet type='text/xsl' href='style.xsl'?><data>something</data>`, "something"},
{"malformed nested", `<a><b>value</a></b>`, "value"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := sanitize.XML(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// TestXSS tests the XSS sanitize method
func TestXSS(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Common script injection vectors
{"Remove <script", "<script", ""},
{"Remove script>", "script>", ""},
{"Remove eval(", "eval(", ""},
{"Remove eval(", "eval(", ""},
{"Remove javascript:", "javascript:", ""},
{"Remove javascript:", "javascript:", ""},
{"Remove fromCharCode", "fromCharCode", ""},
{"Remove >", ">", ""},
{"Remove <", "<", ""},
{"Remove <", "<", ""},
{"Remove &rt;", "&rt;", ""},
// Inline event handlers
{"Remove onclick=", "onclick=", ""},
{"Remove onerror=", "onerror=", ""},
{"Remove onload=", "onload=", ""},
{"Remove onmouseover=", "onmouseover=", ""},
{"Remove onfocus=", "onfocus=", ""},
{"Remove onblur=", "onblur=", ""},