1
0
Fork 0
mirror of https://github.com/Luzifer/password.git synced 2024-12-30 01:31:18 +00:00
password/lib/xkcd_test.go

57 lines
1.3 KiB
Go
Raw Normal View History

package securepassword
import (
"fmt"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestXKCDWordList(t *testing.T) {
assert.GreaterOrEqual(t, len(xkcdWordList), 1000)
}
func TestXKCDGeneratePassword(t *testing.T) {
for i := 4; i < 20; i++ {
pwd, err := DefaultXKCD.GeneratePassword(i, false)
require.NoError(t, err)
assert.True(t, regexp.MustCompile(fmt.Sprintf("^([A-Z][a-z]+){%d}$", i)).MatchString(pwd))
}
}
func TestXKCDDatePrepend(t *testing.T) {
pwd, err := DefaultXKCD.GeneratePassword(4, true)
require.NoError(t, err)
assert.True(t, regexp.MustCompile(`^[0-9]{8}\.([A-Z][a-z]+){4}$`).MatchString(pwd))
}
func TestXKCDSeparator(t *testing.T) {
gen := NewXKCDGenerator()
gen.Separator = "-"
pwd, err := gen.GeneratePassword(4, false)
require.NoError(t, err)
assert.True(t, regexp.MustCompile(`^(?:[A-Z][a-z]+-){3}(?:[A-Z][a-z]+)$`).MatchString(pwd))
}
func BenchmarkGeneratePasswords4Words(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
_, err = DefaultXKCD.GeneratePassword(4, false)
require.NoError(b, err)
}
}
func BenchmarkGeneratePasswords20Words(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
_, err = DefaultXKCD.GeneratePassword(20, false)
require.NoError(b, err)
}
}