forked from BakedSoftware/go-validation
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextra_validations_test.go
842 lines (698 loc) · 26.2 KB
/
extra_validations_test.go
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
package validate
import (
"fmt"
"strings"
"testing"
)
const testPhone = "234-234-2345"
const testMxCountryCode = "+52"
const emailInvalidFormatError = "email is not a valid address format"
// TestIsValidSocial testing the social security number (invalid and valid)
func TestIsValidSocial(t *testing.T) {
var ok bool
var err error
testSocialNumber := "000-00-0000" // All zeros
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "434-43-433" // Not enough digits
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "434-43-4334444" // Too many digits
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "666-00-0000" // Starts with 666
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "000-12-1235" // First section zeros
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "888-00-1235" // Middle section zeros
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
testSocialNumber = "888-14-0000" // Last section zeros
if ok, err = IsValidSocial(testSocialNumber); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
// Test all blacklisted socials
for _, ssn := range blacklistedSocials {
if ok, err = IsValidSocial(ssn); ok {
t.Fatal("Social test passed when it should have failed.", testSocialNumber, err)
}
}
testSocialNumber = "434-43-4334" // Valid
if ok, err = IsValidSocial(testSocialNumber); !ok {
t.Fatal("Social test failed when it should have passed.", testSocialNumber, err)
}
testSocialNumber = "323126767" // Valid
if ok, err = IsValidSocial(testSocialNumber); !ok {
t.Fatal("Social test failed when it should have passed.", testSocialNumber, err)
}
testSocialNumber = "212126768" // Valid
if ok, err = IsValidSocial(testSocialNumber); !ok {
t.Fatal("Social test failed when it should have passed.", testSocialNumber, err)
}
}
// TestIsValidSocialErrorResponses test the error responses
func TestIsValidSocialErrorResponses(t *testing.T) {
var err error
testSocialNumber := "" // Empty
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social is empty" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
testSocialNumber = "2123" // Not long enough
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social is not nine digits in length" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
testSocialNumber = "000-00-0000" // All zeros
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social section was found invalid (cannot be 000 or 666)" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
testSocialNumber = "1234-1-2342" // Invalid Pattern
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social does not match the regex pattern" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
testSocialNumber = "000-00-2345" // Invalid section
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social section was found invalid (cannot be 000 or 666)" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
for _, ssn := range blacklistedSocials { // Blacklisted
if _, err = IsValidSocial(ssn); err != nil && err.Error() != "social was found to be blacklisted" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
}
}
// ExampleIsValidSocial_invalid example of an invalid social response
func ExampleIsValidSocial_invalid() {
ok, err := IsValidSocial("666-00-0000") // Invalid
fmt.Println(ok, err)
// Output: false social section was found invalid (cannot be 000 or 666)
}
// ExampleIsValidSocial_invalid example of a valid social response
func ExampleIsValidSocial_valid() {
ok, err := IsValidSocial("212126768") // Valid
fmt.Println(ok, err)
// Output: true <nil>
}
// BenchmarkIsValidSocial benchmarks the IsValidSocial (valid value)
func BenchmarkIsValidSocial(b *testing.B) {
testSocialNumber := "212126768"
for i := 0; i < b.N; i++ {
_, _ = IsValidSocial(testSocialNumber)
}
}
// TestIsValidEmail testing the email validation
func TestIsValidEmail(t *testing.T) {
var success bool
email := "test"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "test@"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "test@some"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "test.some"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "t@t"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "T@T."
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = ".com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "@.com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@.com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@..com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@...com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@something..com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@something.-.com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@---.com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "a@a---.com"
if success, _ = IsValidEmail(email, false); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
// ========= VALID EMAILS ================
email = "test@test.com"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@dd.com"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@t2.com"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@2t.com"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@t.co"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@dekora.fashion"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@sierra.finance"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@money.cash.co"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "jp.power.co@money.we.cash.co"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@t.co.uk"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "t@test.com.uk"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "d-d-d.d.dt@test.com.uk"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "john_doe@test.com.uk"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
email = "john+doe@test.com.uk"
if success, _ = IsValidEmail(email, false); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
// ========= INVALID MX EMAILS ================
email = "d-d-d.d.dt@testthissite.com.uk"
if success, _ = IsValidEmail(email, true); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "tester@example.com"
if success, _ = IsValidEmail(email, true); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "yolanda@615.yt@gmail.com"
var err error
if success, err = IsValidEmail(email, true); success {
t.Fatal("Email value should be invalid! Value: "+email, success, err)
}
email = "d-d-d.d.dt@gmail.c"
if success, _ = IsValidEmail(email, true); success {
t.Fatal("Email value should be invalid! Value: " + email)
}
email = "someone@gmail.com"
if success, _ = IsValidEmail(email, true); !success {
t.Fatal("Email value should be valid! Value: " + email)
}
// ========= TEST ALL TLDS ================
// Test all blacklisted hosts and errors
for _, host := range blacklistedDomains {
if ok, err := IsValidEmail("someone@"+host, false); ok {
t.Fatal("This should have failed, host is blacklisted", host, err)
}
}
}
// TestIsValidEmailErrorResponses test the error responses
func TestIsValidEmailErrorResponses(t *testing.T) {
var err error
email := "test"
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email length is invalid" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
email = "test@"
if _, err = IsValidEmail(email, false); err != nil && err.Error() != emailInvalidFormatError {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
email = "test.some"
if _, err = IsValidEmail(email, false); err != nil && err.Error() != emailInvalidFormatError {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
email = "@this.com"
if _, err = IsValidEmail(email, false); err != nil && err.Error() != emailInvalidFormatError {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
email = "1234567890123456789012345678901234567890123456789012345678901234567890@this.com"
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email length is invalid" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
// Test all blacklisted hosts and errors
for _, host := range blacklistedDomains {
if _, err = IsValidEmail("someone@"+host, false); err != nil && err.Error() != "email domain is not accepted" {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
}
email = "someone@gmail.conn"
_, err = IsValidEmail(email, true)
// email domain invalid/cannot receive mail: lookup gmail.conn on 169.254.169.254:53: no such host
// if err.Error() != "email domain invalid/cannot receive mail: lookup gmail.conn: no such host" {
if err != nil && !strings.Contains(err.Error(), "email domain invalid/cannot receive mail:") {
t.Fatal("Error response was not as expected - Value: " + err.Error())
}
}
// ExampleIsValidEmail_invalid example of an invalid email address response
func ExampleIsValidEmail_invalid() {
ok, err := IsValidEmail("notvalid@domain", false) // Invalid
fmt.Println(ok, err)
// Output: false email is not a valid address format
}
// ExampleIsValidEmail_valid example of a valid email address response
func ExampleIsValidEmail_valid() {
ok, err := IsValidEmail("person@gmail.com", false) // Valid
fmt.Println(ok, err)
// Output: true <nil>
}
// BenchmarkIsValidEmail benchmarks the IsValidEmail (valid value)
func BenchmarkIsValidEmail(b *testing.B) {
testEmail := "person@gmail.com"
for i := 0; i < b.N; i++ {
_, _ = IsValidEmail(testEmail, false)
}
}
// TestIsValidEnum testing the enum value in an accepted list of values
func TestIsValidEnum(t *testing.T) {
var ok bool
var err error
// Invalid value
testEnumValue := "1"
testAcceptedValues := []string{"123"}
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); ok {
t.Fatal("This should have failed - value is not found", testEnumValue, testAcceptedValues, err)
} else if err != nil && err.Error() != "value "+testEnumValue+" is not allowed" {
t.Fatal("error message was not as expected", err.Error())
}
// Valid value
testEnumValue = "123"
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); !ok {
t.Fatal("This should have passed - value is valid", testEnumValue, testAcceptedValues, err)
}
// Empty valid not allowed
testEnumValue = ""
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); ok {
t.Fatal("This should have failed - can be empty flag", testEnumValue, testAcceptedValues, err)
}
// Empty value allowed
testEnumValue = ""
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, true); !ok {
t.Fatal("This should have passed - can be empty flag", testEnumValue, testAcceptedValues, err)
}
// Test case-insensitive
testEnumValue = "mystring"
testAcceptedValues = []string{"myString"}
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); !ok {
t.Fatal("This should have passed - can be empty flag", testEnumValue, testAcceptedValues, err)
}
}
// ExampleIsValidEnum_invalid example of an invalid enum
func ExampleIsValidEnum_invalid() {
testAcceptedValues := []string{"123"}
ok, err := IsValidEnum("1", &testAcceptedValues, false) // Invalid
fmt.Println(ok, err)
// Output: false value 1 is not allowed
}
// ExampleIsValidEnum_valid example of an valid enum
func ExampleIsValidEnum_valid() {
testAcceptedValues := []string{"123"}
ok, err := IsValidEnum("123", &testAcceptedValues, false) // Valid
fmt.Println(ok, err)
// Output: true <nil>
}
// BenchmarkIsValidEnum benchmarks the IsValidEnum (valid value)
func BenchmarkIsValidEnum(b *testing.B) {
testValue := "1"
testAcceptedValues := []string{"123"}
for i := 0; i < b.N; i++ {
_, _ = IsValidEnum(testValue, &testAcceptedValues, false)
}
}
// TestIsValidPhoneNumber testing the phone value
func TestIsValidPhoneNumber(t *testing.T) {
var ok bool
var err error
// Missing country code (too short)
phone := ""
countryCode := ""
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "country code length is invalid" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Invalid country code (too long)
countryCode = "6666"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "country code length is invalid" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Country code not accepted
countryCode = "+32"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "country code 32 is not accepted" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number missing
countryCode = "+1" // USA / CAN
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number length is invalid" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number not right length (USA)
countryCode = "+1" // USA / CAN
phone = "555-444-3"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number must be ten digits" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number not right length (MX)
phone = "555-444-3"
if ok, err = IsValidPhoneNumber(phone, testMxCountryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, testMxCountryCode, err)
} else if err != nil && err.Error() != "phone number must be either eight or ten digits" {
t.Fatal("error message was not as expected", phone, testMxCountryCode, err.Error())
}
// Phone number not right length (USA)
countryCode = "+1" // USA / CAN
phone = "234-444-3"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number must be ten digits" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number cannot start with 1
countryCode = "+1" // USA / CAN
phone = "123-123-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NPA cannot start with 1" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number cannot start with 0
countryCode = "+1" // USA / CAN
phone = "023-123-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NPA cannot start with 0" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number cannot start with 1
phone = "123-123-1234"
if ok, err = IsValidPhoneNumber(phone, testMxCountryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, testMxCountryCode, err)
} else if err != nil && err.Error() != "phone number NPA cannot start with 1" {
t.Fatal("error message was not as expected", phone, testMxCountryCode, err.Error())
}
// Phone number cannot start with 0
phone = "023-123-1234"
if ok, err = IsValidPhoneNumber(phone, testMxCountryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, testMxCountryCode, err)
} else if err != nil && err.Error() != "phone number NPA cannot start with 0" {
t.Fatal("error message was not as expected", phone, testMxCountryCode, err.Error())
}
// Phone number cannot start with 555
countryCode = "+1" // USA / CAN
phone = "555-123-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NPA cannot start with 555" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number NXX cannot start with 1
countryCode = "+1" // USA / CAN
phone = "234-123-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NXX cannot start with 1" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number NXX cannot start with 0
countryCode = "+1" // USA / CAN
phone = "234-023-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NXX cannot start with 0" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number NXX cannot be X11
countryCode = "+1" // USA / CAN
phone = "234-911-1234"
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
} else if err != nil && err.Error() != "phone number NXX cannot be X11" {
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
}
// Phone number valid
countryCode = "+1" // USA / CAN
if ok, err = IsValidPhoneNumber(testPhone, countryCode); !ok {
t.Fatal("This should have passed - phone is valid for USA", testPhone, countryCode, err)
}
}
// ExampleIsValidPhoneNumber_invalid example of an invalid phone number
func ExampleIsValidPhoneNumber_invalid() {
countryCode := "+1"
phone := "555-444-44"
ok, err := IsValidPhoneNumber(phone, countryCode)
fmt.Println(ok, err)
// Output: false phone number must be ten digits
}
// ExampleIsValidPhoneNumber_valid example of a valid phone number
func ExampleIsValidPhoneNumber_valid() {
countryCode := "+1"
ok, err := IsValidPhoneNumber(testPhone, countryCode)
fmt.Println(ok, err)
// Output: true <nil>
}
// BenchmarkIsValidPhoneNumber benchmarks the IsValidPhoneNumber (valid value)
func BenchmarkIsValidPhoneNumber(b *testing.B) {
countryCode := "+1"
for i := 0; i < b.N; i++ {
_, _ = IsValidPhoneNumber(testPhone, countryCode)
}
}
// TestIsValidDNSName testing the dns name
func TestIsValidDNSName(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected bool
}{
{"localhost", true},
{"a.bc", true},
{"a.b.", true},
{"a.b..", false},
{"localhost.local", true},
{"localhost.localdomain.intern", true},
{"l.local.intern", true},
{"ru.link.n.svpncloud.com", true},
{"-localhost", false},
{"localhost.-localdomain", false},
{"localhost.localdomain.-int", false},
{"_localhost", true},
{"localhost._localdomain", true},
{"localhost.localdomain._int", true},
{"lÖcalhost", false},
{"localhost.lÖcaldomain", false},
{"localhost.localdomain.üntern", false},
{"__", true},
{"localhost/", false},
{"127.0.0.1", false},
{"[::1]", false},
{"50.50.50.50", false},
{"localhost.localdomain.intern:65535", false},
{"漢字汉字", false},
{"www.jubfvq1v3p38i51622y0dvmdk1mymowjyeu26gbtw9andgynj1gg8z3msb1kl5z6906k846pj3sulm4kiyk82ln5teqj9nsht59opr0cs5ssltx78lfyvml19lfq1wp4usbl0o36cmiykch1vywbttcus1p9yu0669h8fj4ll7a6bmop505908s1m83q2ec2qr9nbvql2589adma3xsq2o38os2z3dmfh2tth4is4ixyfasasasefqwe4t2ub2fz1rme.de", false},
}
for _, test := range tests {
actual := IsValidDNSName(test.param)
if actual != test.expected {
t.Errorf("Expected IsValidDNSName(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
// ExampleIsValidDNSName_invalid example of an invalid dns name
func ExampleIsValidDNSName_invalid() {
ok := IsValidDNSName("localhost.-localdomain")
fmt.Println(ok)
// Output: false
}
// ExampleIsValidDNSName_valid example of a valid dns name
func ExampleIsValidDNSName_valid() {
ok := IsValidDNSName("localhost")
fmt.Println(ok)
// Output: true
}
// BenchmarkIsValidDNSName benchmarks the IsValidDNSName (valid value)
func BenchmarkIsValidDNSName(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IsValidDNSName("localhost")
}
}
// TestIsValidIP will test IPv4 and IPv6
func TestIsValidIP(t *testing.T) {
t.Parallel()
// Without version
var tests = []struct {
param string
expected bool
}{
{"", false},
{"127.0.0.1", true},
{"0.0.0.0", true},
{"255.255.255.255", true},
{"1.2.3.4", true},
{"::1", true},
{"2001:db8:0000:1:1:1:1:1", true},
{"300.0.0.0", false},
}
for _, test := range tests {
actual := IsValidIP(test.param)
if actual != test.expected {
t.Errorf("Expected IsValidIP(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
// IPv4
tests = []struct {
param string
expected bool
}{
{"", false},
{"127.0.0.1", true},
{"0.0.0.0", true},
{"255.255.255.255", true},
{"1.2.3.4", true},
{"::1", false},
{"2001:db8:0000:1:1:1:1:1", false},
{"300.0.0.0", false},
}
for _, test := range tests {
actual := IsValidIPv4(test.param)
if actual != test.expected {
t.Errorf("Expected IsValidIPv4(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
// IPv6
tests = []struct {
param string
expected bool
}{
{"", false},
{"127.0.0.1", false},
{"0.0.0.0", false},
{"255.255.255.255", false},
{"1.2.3.4", false},
{"::1", true},
{"2001:db8:0000:1:1:1:1:1", true},
{"300.0.0.0", false},
}
for _, test := range tests {
actual := IsValidIPv6(test.param)
if actual != test.expected {
t.Errorf("Expected IsValidIPv6(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
// ExampleIsValidIP_invalid example of an invalid ip address
func ExampleIsValidIP_invalid() {
ok := IsValidIP("300.0.0.0")
fmt.Println(ok)
// Output: false
}
// ExampleIsValidIP_valid example of a valid ip address
func ExampleIsValidIP_valid() {
ok := IsValidIP("127.0.0.1")
fmt.Println(ok)
// Output: true
}
// BenchmarkIsValidIP benchmarks the IsValidIP (valid value)
func BenchmarkIsValidIP(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IsValidIP("127.0.0.1")
}
}
// TestIsValidHost will test a hostname
func TestIsValidHost(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected bool
}{
{"localhost", true},
{"localhost.localdomain", true},
{"2001:db8:0000:1:1:1:1:1", true},
{"::1", true},
{"play.golang.org", true},
{"localhost.localdomain.intern:65535", false},
{"-[::1]", false},
{"-localhost", false},
{".localhost", false},
}
for _, test := range tests {
actual := IsValidHost(test.param)
if actual != test.expected {
t.Errorf("Expected IsValidHost(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
// ExampleIsValidHost_invalid example of an invalid host
func ExampleIsValidHost_invalid() {
ok := IsValidHost("-localhost")
fmt.Println(ok)
// Output: false
}
// ExampleIsValidHost_valid example of a valid host
func ExampleIsValidHost_valid() {
ok := IsValidHost("localhost")
fmt.Println(ok)
// Output: true
}
// BenchmarkIsValidHost benchmarks the IsValidHost (valid value)
func BenchmarkIsValidHost(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IsValidHost("localhost")
}
}