1
0
mirror of https://github.com/Luzifer/password.git synced 2024-09-19 18:32:57 +00:00
password/lib/xkcd_test.go
Knut Ahlers aa39ad3413
[#6] Add optional separator to XKCD passwords
Squashed commit of the following:

commit ef74c84e5b4826c2c58012cd4b0ea975d1dd7260
Author: Knut Ahlers <knut@ahlers.me>
Date:   Sun Dec 1 13:09:53 2019 +0100

    Minor cleamup

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 0b57aa8c8abb2fd118c71fdfd8d51cb707861821
Author: Knut Ahlers <knut@ahlers.me>
Date:   Sun Dec 1 13:09:17 2019 +0100

    [#6] Add optional separator to XKCD passwords

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-12-01 13:10:20 +01:00

65 lines
1.5 KiB
Go

package securepassword
import (
"fmt"
"regexp"
"testing"
)
func TestXKCDWordList(t *testing.T) {
if w := len(xkcdWordList); w < 1000 {
t.Fatalf("Word list is expected to contain at least 1000 words, has %d", w)
}
}
func TestXKCDGeneratePassword(t *testing.T) {
for i := 4; i < 20; i++ {
pwd, err := DefaultXKCD.GeneratePassword(i, false)
if err != nil {
t.Fatalf("Generated had an error: %s", err)
}
if !regexp.MustCompile(fmt.Sprintf("^([A-Z][a-z]+){%d}$", i)).MatchString(pwd) {
t.Errorf("Password %q is expected to contain %d words, did not match expected RegEx", pwd, i)
}
}
}
func TestXKCDDatePrepend(t *testing.T) {
pwd, err := DefaultXKCD.GeneratePassword(4, true)
if err != nil {
t.Fatalf("Generated had an error: %s", err)
}
if !regexp.MustCompile(`^[0-9]{8}\.([A-Z][a-z]+){4}$`).MatchString(pwd) {
t.Errorf("Password %q did not match expected RegEx", pwd)
}
}
func TestXKCDSeparator(t *testing.T) {
gen := NewXKCDGenerator()
gen.Separator = "-"
pwd, err := gen.GeneratePassword(4, false)
if err != nil {
t.Fatalf("Generated had an error: %s", err)
}
if !regexp.MustCompile(`^(?:[A-Z][a-z]+-){3}(?:[A-Z][a-z]+)$`).MatchString(pwd) {
t.Errorf("Password %q did not match expected RegEx", pwd)
}
}
func BenchmarkGeneratePasswords4Words(b *testing.B) {
for i := 0; i < b.N; i++ {
DefaultXKCD.GeneratePassword(4, false)
}
}
func BenchmarkGeneratePasswords20Words(b *testing.B) {
for i := 0; i < b.N; i++ {
DefaultXKCD.GeneratePassword(20, false)
}
}