github.com/tommi2day/gomodules/pwlib@v0.0.0-20230217211148-82cdbcf0a79d/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 "Only-8-UpperAndDigits", 70 true, 71 true, 72 8, 73 1, 74 0, 75 1, 76 0, 77 false, 78 UpperChar + Digits, 79 }, 80 } 81 82 for _, c := range tests { 83 t.Run(c.name, func(t *testing.T) { 84 newPassword, err := GenPassword(c.length, c.uc, c.lc, c.num, c.sp, c.first) 85 t.Logf("generated Password: '%s'\n", newPassword) 86 if err == nil { 87 ok := DoPasswordCheck(newPassword, c.length, c.uc, c.lc, c.num, c.sp, c.first, c.chars) 88 if ok != c.valid { 89 t.Fatalf("invalid password '%s'", newPassword) 90 } 91 } else 92 // generation failed 93 if c.genValid { 94 t.Fatalf("Password Generation failed: %s", err.Error()) 95 } 96 }) 97 } 98 }