goshort/internal/util/passwords/util_test.go
Gustavo Maronato be39b22ace
Some checks failed
Check / checks (push) Failing after 3m48s
lint
2024-03-09 05:53:28 -05:00

44 lines
1.8 KiB
Go

package passwords_test
import (
"testing"
passwords "git.maronato.dev/maronato/goshort/internal/util/passwords"
"github.com/stretchr/testify/assert"
)
func TestGenerateSalt(t *testing.T) {
r1 := passwords.GenerateSalt(8)
assert.Len(t, r1, 8, "GenerateSalt should return a slice with the length passed as argument")
r2 := passwords.GenerateSalt(8)
assert.NotEqual(t, r1, r2, "GenerateSalt should return different results for different calls")
}
func TestAreEqual(t *testing.T) {
assert.True(t, passwords.AreEqual([]byte("abc"), []byte("abc")), "AreEqual should return true for equal byte slices")
assert.False(t, passwords.AreEqual([]byte("abc"), []byte("def")), "AreEqual should return false for different byte slices")
}
func TestEncodePasswordHash(t *testing.T) {
r1 := passwords.EncodePasswordHash("myalg", "mysalt", "myhash", map[string]string{"mykey": "myvalue"})
assert.Equal(t, "myalg$mykey=myvalue$mysalt$myhash", r1, "EncodePasswordHash should return a string with the correct format")
}
func TestDecodePasswordHash(t *testing.T) {
alg, salt, hash, params, err := passwords.DecodePasswordHash("myalg$mykey=myvalue$mysalt$myhash")
assert.Nil(t, err, "DecodePasswordHash should not return an error for a valid hash")
assert.Equal(t, "myalg", alg, "DecodePasswordHash should return the correct algorithm")
assert.Equal(t, "mysalt", salt, "DecodePasswordHash should return the correct salt")
assert.Equal(t, "myhash", hash, "DecodePasswordHash should return the correct hash")
assert.Equal(t, map[string]string{"mykey": "myvalue"}, params, "DecodePasswordHash should return the correct params")
_, _, _, _, err = passwords.DecodePasswordHash("myalg$mykey=myvalue$mysalt") //nolint:dogsled // This is a test
assert.NotNil(t, err, "DecodePasswordHash should return an error for an invalid hash")
}