Skip to content

Commit

Permalink
fix distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
riadafridishibly authored and iwpnd committed Oct 24, 2022
1 parent 714084b commit 336f42c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pw.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const AllChars = UpperChars + LowerChars + SpecialChars + NumberChars
type Option func() string

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

for i := 0; i < length; i++ {
Expand Down
18 changes: 18 additions & 0 deletions pw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ func contains(chars string, password string) bool {
return false
}

// containsAll checks if password contains all chars in the charset
func containsAll(password, charset string) bool {
for _, char := range strings.Split(charset, "") {
if !strings.Contains(password, char) {
return false
}
}
return true
}

func TestDefaultNewPassword(t *testing.T) {
l := 100
p := NewPassword(l)
Expand Down Expand Up @@ -139,3 +149,11 @@ func TestSpecialCharOnlyPassword(t *testing.T) {
t.Errorf("should not contain upper characters")
}
}

func TestDistribution(t *testing.T) {
const charset = "12"
pass := randomFromChars(100, charset)
if !containsAll(pass, charset) {
t.Errorf("should contain all the chars from charset %q\n", charset)
}
}

0 comments on commit 336f42c

Please sign in to comment.