44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package passwords_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "git.maronato.dev/maronato/goshort/internal/util/passwords"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGenerateSalt(t *testing.T) {
|
|
r1 := GenerateSalt(8)
|
|
|
|
assert.Len(t, r1, 8, "GenerateSalt should return a slice with the length passed as argument")
|
|
|
|
r2 := GenerateSalt(8)
|
|
|
|
assert.NotEqual(t, r1, r2, "GenerateSalt should return different results for different calls")
|
|
}
|
|
|
|
func TestAreEqual(t *testing.T) {
|
|
assert.True(t, AreEqual([]byte("abc"), []byte("abc")), "AreEqual should return true for equal byte slices")
|
|
assert.False(t, AreEqual([]byte("abc"), []byte("def")), "AreEqual should return false for different byte slices")
|
|
}
|
|
|
|
func TestEncodePasswordHash(t *testing.T) {
|
|
r1 := 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 := 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 = DecodePasswordHash("myalg$mykey=myvalue$mysalt") //nolint:dogsled // This is a test
|
|
|
|
assert.NotNil(t, err, "DecodePasswordHash should return an error for an invalid hash")
|
|
}
|