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

202 lines
5.2 KiB
Go
Raw Normal View History

2015-05-02 13:03:27 +00:00
package securepassword
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2015-05-02 13:03:27 +00:00
func TestInsecurePasswords(t *testing.T) {
passwords := map[string]string{
`8452028337962356`: "password with only numeric characters was accepted",
`adfgjadrgdagasdf`: "password with only lowercase characters was accepted",
`ASEFSTDHQAEGFADF`: "password with only uppercase characters was accepted",
`135fach74nc94bd6`: "password without uppercase characters was accepted",
`235JGOA0YTVKS46S`: "password without lowercase characters was accepted",
`sdgAFfgADTSgafoa`: "password without numeric characters was accepte",
`cKTn5mQXfasdS6qy`: "password with pattern asd was accepted",
`cKTn5mQXfdsaS6qy`: "password with pattern dsa was accepted",
`cKTn5mQXf345S6qy`: "password with pattern 345 was accepted",
`cKTn5mQXf987S6qy`: "password with pattern 987 was accepted",
`cKTn5mQXfabcS6qy`: "password with pattern abc was accepted",
`cKTn5mQXfcbaS6qy`: "password with pattern cba was accepted",
`cKTn5mQXfABCS6qy`: "password with pattern ABC was accepted",
`cKTn5mQXfONMS6qy`: "password with pattern ONM was accepted",
`Gncj5zzK29Dvx92h`: "password with character repetition was accepted",
`Gncj5%%K29Dvx92h`: "password with character repetition was accepted",
`Gncj55%K29Dvx92h`: "password with character repetition was accepted",
}
sp := NewSecurePassword()
2015-05-02 13:03:27 +00:00
for password, errorMessage := range passwords {
assert.False(t, sp.CheckPasswordSecurity(password, false), errorMessage)
2015-05-02 13:03:27 +00:00
}
}
func TestSecurePasswords(t *testing.T) {
passwords := []string{
`6e1GZ6V2empWAky5Z13a`,
`DLHZA2zfWor1XUoJYvFR`,
`sMf3uNf2E1pxPFMymah5`,
`prb4tX1vtyVL7R316dKU`,
`7bWc9C1ciL62h5u26Z9g`,
2015-05-02 13:03:27 +00:00
}
sp := NewSecurePassword()
2015-05-02 13:03:27 +00:00
for _, password := range passwords {
assert.True(t, sp.CheckPasswordSecurity(password, false), "password was rejected: %s", password)
2015-05-02 13:03:27 +00:00
}
}
func TestPasswordWithoutSpecialCharaterFail(t *testing.T) {
passwords := []string{
`6e1GZ6V2empWAky5Z13a`,
`DLHZA2zfWor1XUoJYvFR`,
`sMf3uNf2E1pxPFMymah5`,
`prb4tX1vtyVL7R316dKU`,
`7bWc9C1ciL62h5u26Z9g`,
2015-05-02 13:03:27 +00:00
}
sp := NewSecurePassword()
2015-05-02 13:03:27 +00:00
for _, password := range passwords {
assert.False(t, sp.CheckPasswordSecurity(password, true), "password was accepted: %s", password)
2015-05-02 13:03:27 +00:00
}
}
func TestSecurePasswordWithSpecialCharacter(t *testing.T) {
passwords := []string{
`a*5S(+zQ9=<J~bH}!F])`,
`9?)k1Ge?[F}5w{#$Ho(6`,
`LRrot)%qVH!>3%/1MiNr`,
`/K}.]C4-a/{,39r$"(D+`,
`9dk#:@xjPd_m$:F"}>Cj`,
}
sp := NewSecurePassword()
2015-05-02 13:03:27 +00:00
for _, password := range passwords {
assert.True(t, sp.CheckPasswordSecurity(password, true), "password was rejected: %s", password)
2015-05-02 13:03:27 +00:00
}
}
2015-05-03 08:38:40 +00:00
func TestPasswordGeneration(t *testing.T) {
sp := NewSecurePassword()
password, err := sp.GeneratePassword(20, false)
require.NoError(t, err)
2015-05-03 08:38:40 +00:00
assert.Len(t, password, 20)
assert.True(t, sp.CheckPasswordSecurity(password, false))
assert.False(t, sp.CheckPasswordSecurity(password, true))
2015-05-03 08:38:40 +00:00
password, err = NewSecurePassword().GeneratePassword(32, true)
require.NoError(t, err)
2015-05-03 08:38:40 +00:00
assert.Len(t, password, 32)
assert.True(t, sp.CheckPasswordSecurity(password, false))
assert.True(t, sp.CheckPasswordSecurity(password, true))
2015-05-03 08:38:40 +00:00
}
func TestImpossiblePasswords(t *testing.T) {
sp := NewSecurePassword()
for i := 0; i < 4; i++ {
_, err := sp.GeneratePassword(i, false)
assert.ErrorIs(t, err, ErrLengthTooLow)
}
}
func TestBadCharacters(t *testing.T) {
badCharacters := []string{"I", "l", "0", "O", "B", "8"}
sp := NewSecurePassword()
for i := 0; i < 500; i++ {
pwd, err := sp.GeneratePassword(20, false)
require.NoError(t, err)
for _, char := range badCharacters {
assert.NotContains(t, pwd, char)
}
}
}
2015-05-02 13:03:27 +00:00
func BenchmarkGeneratePasswords8Char(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(8, false)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords8CharSpecial(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(8, true)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords16Char(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(16, false)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords16CharSpecial(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(16, true)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords32Char(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(32, false)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords32CharSpecial(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(32, true)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords128Char(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(128, false)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}
func BenchmarkGeneratePasswords128CharSpecial(b *testing.B) {
pwd := NewSecurePassword()
var err error
2015-05-02 13:03:27 +00:00
for i := 0; i < b.N; i++ {
_, err = pwd.GeneratePassword(128, true)
require.NoError(b, err)
2015-05-02 13:03:27 +00:00
}
}