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