File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,8 @@ const AllChars = UpperChars + LowerChars + SpecialChars + NumberChars
24
24
type Option func () string
25
25
26
26
func randomFromChars (length int , chars string ) string {
27
- max := len (chars ) - 1
27
+ // rand.Int returns value in [0, max)
28
+ max := len (chars )
28
29
p := ""
29
30
30
31
for i := 0 ; i < length ; i ++ {
Original file line number Diff line number Diff line change @@ -15,6 +15,16 @@ func contains(chars string, password string) bool {
15
15
return false
16
16
}
17
17
18
+ // containsAll checks if password contains all chars in the charset
19
+ func containsAll (password , charset string ) bool {
20
+ for _ , char := range strings .Split (charset , "" ) {
21
+ if ! strings .Contains (password , char ) {
22
+ return false
23
+ }
24
+ }
25
+ return true
26
+ }
27
+
18
28
func TestDefaultNewPassword (t * testing.T ) {
19
29
l := 100
20
30
p := NewPassword (l )
@@ -139,3 +149,11 @@ func TestSpecialCharOnlyPassword(t *testing.T) {
139
149
t .Errorf ("should not contain upper characters" )
140
150
}
141
151
}
152
+
153
+ func TestDistribution (t * testing.T ) {
154
+ const charset = "12"
155
+ pass := randomFromChars (100 , charset )
156
+ if ! containsAll (pass , charset ) {
157
+ t .Errorf ("should contain all the chars from charset %q\n " , charset )
158
+ }
159
+ }
You can’t perform that action at this time.
0 commit comments