github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/pwlib/password_generate_test.go (about) 1 package pwlib 2 3 import ( 4 "testing" 5 ) 6 7 func TestGenPassword(t *testing.T) { 8 tests := []struct { 9 name string 10 genValid bool 11 valid bool 12 length int 13 uc int 14 lc int 15 num int 16 sp int 17 first bool 18 chars string 19 }{ 20 { 21 "Default Techuser", 22 true, 23 true, 24 TechProfile.Length, 25 TechProfile.Upper, 26 TechProfile.Lower, 27 TechProfile.Digits, 28 TechProfile.Special, 29 TechProfile.Firstchar, 30 AllChars, 31 }, 32 { 33 "InvalidGenZeroLength", 34 false, 35 false, 36 0, 37 0, 38 0, 39 0, 40 0, 41 false, 42 AllChars, 43 }, 44 { 45 "Personal User", 46 true, 47 true, 48 UserProfile.Length, 49 UserProfile.Upper, 50 UserProfile.Lower, 51 UserProfile.Digits, 52 UserProfile.Special, 53 UserProfile.Firstchar, 54 AllChars, 55 }, 56 { 57 "User-16-2-2-2-2-1", 58 true, 59 true, 60 16, 61 2, 62 2, 63 2, 64 2, 65 true, 66 AllChars, 67 }, 68 { 69 "User-16-2-2-2-2-1-no-charset", 70 true, 71 true, 72 16, 73 2, 74 2, 75 2, 76 2, 77 true, 78 "", 79 }, 80 { 81 "Only-8-UpperAndDigits", 82 true, 83 true, 84 8, 85 1, 86 0, 87 1, 88 0, 89 false, 90 UpperChar + Digits, 91 }, 92 } 93 SetSpecialChars(SpecialChar) 94 for _, c := range tests { 95 t.Run(c.name, func(t *testing.T) { 96 newPassword, err := GenPassword(c.length, c.uc, c.lc, c.num, c.sp, c.first) 97 t.Logf("generated Password: '%s'\n", newPassword) 98 if err == nil { 99 ok := DoPasswordCheck(newPassword, c.length, c.uc, c.lc, c.num, c.sp, c.first, c.chars) 100 if ok != c.valid { 101 t.Fatalf("invalid password '%s'", newPassword) 102 } 103 } else 104 // generation failed 105 if c.genValid { 106 t.Fatalf("Password Generation failed: %s", err.Error()) 107 } 108 }) 109 } 110 }