Skip to content

Commit 336f42c

Browse files
riadafridishiblyiwpnd
authored andcommitted
fix distribution
1 parent 714084b commit 336f42c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pw.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const AllChars = UpperChars + LowerChars + SpecialChars + NumberChars
2424
type Option func() string
2525

2626
func randomFromChars(length int, chars string) string {
27-
max := len(chars) - 1
27+
// rand.Int returns value in [0, max)
28+
max := len(chars)
2829
p := ""
2930

3031
for i := 0; i < length; i++ {

pw_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ func contains(chars string, password string) bool {
1515
return false
1616
}
1717

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+
1828
func TestDefaultNewPassword(t *testing.T) {
1929
l := 100
2030
p := NewPassword(l)
@@ -139,3 +149,11 @@ func TestSpecialCharOnlyPassword(t *testing.T) {
139149
t.Errorf("should not contain upper characters")
140150
}
141151
}
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+
}

0 commit comments

Comments
 (0)