2019-04-25 04:52:08 +07:00
package authentication
import (
"testing"
"github.com/stretchr/testify/assert"
2019-12-27 23:55:00 +07:00
"github.com/stretchr/testify/require"
2019-04-25 04:52:08 +07:00
)
func TestShouldHashPassword ( t * testing . T ) {
2019-11-02 01:31:51 +07:00
hash := HashPassword ( "password" , "$6$rounds=50000$aFr56HjK3DrB8t3S" )
assert . Equal ( t , "$6$rounds=50000$aFr56HjK3DrB8t3S$zhPQiS85cgBlNhUKKE6n/AHMlpqrvYSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" , hash )
2019-04-25 04:52:08 +07:00
}
func TestShouldCheckPassword ( t * testing . T ) {
2019-11-02 01:31:51 +07:00
ok , err := CheckPassword ( "password" , "$6$rounds=50000$aFr56HjK3DrB8t3S$zhPQiS85cgBlNhUKKE6n/AHMlpqrvYSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
2019-04-25 04:52:08 +07:00
assert . NoError ( t , err )
assert . True ( t , ok )
}
2019-12-27 23:55:00 +07:00
func TestCannotParseHash ( t * testing . T ) {
ok , err := CheckPassword ( "password" , "$6$roSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
assert . EqualError ( t , err , "Cannot parse the hash $6$roSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
assert . False ( t , ok )
}
func TestOnlySupportSHA512 ( t * testing . T ) {
ok , err := CheckPassword ( "password" , "$8$rounds=50000$aFr56HjK3DrB8t3S$zhPQiS85cgBlNhUKKE6n/AHMlpqrvYSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
assert . EqualError ( t , err , "Authelia only supports salted SHA512 hashing ($6$), not $8$" )
assert . False ( t , ok )
}
func TestCannotFindNumberOfRounds ( t * testing . T ) {
ok , err := CheckPassword ( "password" , "$6$rounds50000$aFr56HjK3DrB8t3S$zhPQiS85cgBlNhUKKE6n/AHMlpqrvYSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
assert . EqualError ( t , err , "Cannot match pattern 'rounds=<int>' to find the number of rounds" )
assert . False ( t , ok )
}
func TestNumberOfRoundsNotInt ( t * testing . T ) {
ok , err := CheckPassword ( "password" , "$6$rounds=abc$aFr56HjK3DrB8t3S$zhPQiS85cgBlNhUKKE6n/AHMlpqrvYSnSL3fEVkK0yHFQ.oFFAd8D4OhPAy18K5U61Z2eBhxQXExGU/eknXlY1" )
assert . EqualError ( t , err , "Cannot find the number of rounds from abc using pattern 'rounds=<int>'. Cause: strconv.ParseInt: parsing \"abc\": invalid syntax" )
assert . False ( t , ok )
}
func TestShouldCheckPasswordHashedWithAuthelia ( t * testing . T ) {
password := "my;secure*password"
hash := HashPassword ( password , "" )
equal , err := CheckPassword ( password , hash )
require . NoError ( t , err )
assert . True ( t , equal )
}