32 lines
899 B
Go
32 lines
899 B
Go
package password
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestVersionedPasswordEncoding(t *testing.T) {
|
|
encoded, err := Encode("correct-horse-battery-staple")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(encoded, "bcrypt:v1:$2") || !Verify(encoded, "correct-horse-battery-staple") || Verify(encoded, "wrong-password") || Verify(strings.TrimPrefix(encoded, "bcrypt:v1:"), "correct-horse-battery-staple") {
|
|
t.Fatalf("versioned password verification failed")
|
|
}
|
|
}
|
|
func TestPasswordLength(t *testing.T) {
|
|
if _, err := Encode("12345"); err == nil {
|
|
t.Fatal("five-character password accepted")
|
|
}
|
|
encoded, err := Encode("123456")
|
|
if err != nil {
|
|
t.Fatalf("six-character password rejected: %v", err)
|
|
}
|
|
if !Verify(encoded, "123456") {
|
|
t.Fatal("six-character password could not be verified")
|
|
}
|
|
if Verify("bcrypt:v2:anything", "value") {
|
|
t.Fatal("unknown version accepted")
|
|
}
|
|
}
|