2015-05-02 13:03:27 +00:00
|
|
|
package securepassword
|
|
|
|
|
2015-05-10 17:05:34 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2024-04-06 22:52:39 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-05-10 17:05:34 +00:00
|
|
|
)
|
2015-05-02 13:03:27 +00:00
|
|
|
|
|
|
|
func TestInsecurePasswords(t *testing.T) {
|
|
|
|
passwords := map[string]string{
|
2024-04-06 22:52:39 +00:00
|
|
|
`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 {
|
2024-04-06 22:52:39 +00:00
|
|
|
assert.False(t, sp.CheckPasswordSecurity(password, false), errorMessage)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecurePasswords(t *testing.T) {
|
|
|
|
passwords := []string{
|
2015-05-02 14:10:58 +00:00
|
|
|
`6e1GZ6V2empWAky5Z13a`,
|
|
|
|
`DLHZA2zfWor1XUoJYvFR`,
|
|
|
|
`sMf3uNf2E1pxPFMymah5`,
|
|
|
|
`prb4tX1vtyVL7R316dKU`,
|
|
|
|
`7bWc9C1ciL62h5u26Z9g`,
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
2015-05-02 13:03:27 +00:00
|
|
|
for _, password := range passwords {
|
2024-04-06 22:52:39 +00:00
|
|
|
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{
|
2015-05-02 14:10:58 +00:00
|
|
|
`6e1GZ6V2empWAky5Z13a`,
|
|
|
|
`DLHZA2zfWor1XUoJYvFR`,
|
|
|
|
`sMf3uNf2E1pxPFMymah5`,
|
|
|
|
`prb4tX1vtyVL7R316dKU`,
|
|
|
|
`7bWc9C1ciL62h5u26Z9g`,
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
2015-05-02 13:03:27 +00:00
|
|
|
for _, password := range passwords {
|
2024-04-06 22:52:39 +00:00
|
|
|
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`,
|
|
|
|
}
|
|
|
|
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
2015-05-02 13:03:27 +00:00
|
|
|
for _, password := range passwords {
|
2024-04-06 22:52:39 +00:00
|
|
|
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) {
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
|
|
|
password, err := sp.GeneratePassword(20, false)
|
|
|
|
require.NoError(t, err)
|
2015-05-03 08:38:40 +00:00
|
|
|
|
2024-04-06 22:52:39 +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
|
|
|
|
2024-04-06 22:52:39 +00:00
|
|
|
password, err = NewSecurePassword().GeneratePassword(32, true)
|
|
|
|
require.NoError(t, err)
|
2015-05-03 08:38:40 +00:00
|
|
|
|
2024-04-06 22:52:39 +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
|
|
|
}
|
|
|
|
|
2015-05-03 08:38:14 +00:00
|
|
|
func TestImpossiblePasswords(t *testing.T) {
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
2015-05-03 08:38:14 +00:00
|
|
|
for i := 0; i < 4; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err := sp.GeneratePassword(i, false)
|
|
|
|
assert.ErrorIs(t, err, ErrLengthTooLow)
|
2015-05-03 08:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 17:05:34 +00:00
|
|
|
func TestBadCharacters(t *testing.T) {
|
|
|
|
badCharacters := []string{"I", "l", "0", "O", "B", "8"}
|
2024-04-06 22:52:39 +00:00
|
|
|
sp := NewSecurePassword()
|
2015-05-10 17:05:34 +00:00
|
|
|
|
|
|
|
for i := 0; i < 500; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
pwd, err := sp.GeneratePassword(20, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-05-10 17:05:34 +00:00
|
|
|
for _, char := range badCharacters {
|
2024-04-06 22:52:39 +00:00
|
|
|
assert.NotContains(t, pwd, char)
|
2015-05-10 17:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
func BenchmarkGeneratePasswords8Char(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(8, false)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords8CharSpecial(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(8, true)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords16Char(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(16, false)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords16CharSpecial(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(16, true)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords32Char(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(32, false)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords32CharSpecial(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(32, true)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords128Char(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(128, false)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGeneratePasswords128CharSpecial(b *testing.B) {
|
|
|
|
pwd := NewSecurePassword()
|
2024-04-06 22:52:39 +00:00
|
|
|
var err error
|
|
|
|
|
2015-05-02 13:03:27 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-04-06 22:52:39 +00:00
|
|
|
_, err = pwd.GeneratePassword(128, true)
|
|
|
|
require.NoError(b, err)
|
2015-05-02 13:03:27 +00:00
|
|
|
}
|
|
|
|
}
|