48 lines
1.5 KiB
Go
48 lines
1.5 KiB
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")
|
|
}
|
|
long := strings.Repeat("密", 300)
|
|
encodedLong, err := Encode(long)
|
|
if err != nil || !Verify(encodedLong, long) || Verify(encodedLong, long+"x") {
|
|
t.Fatalf("long password round trip failed: %v", err)
|
|
}
|
|
maximumCharacters := strings.Repeat("密", 1024)
|
|
encodedMaximum, err := Encode(maximumCharacters)
|
|
if err != nil || !Verify(encodedMaximum, maximumCharacters) {
|
|
t.Fatalf("1024-character password round trip failed: %v", err)
|
|
}
|
|
if _, err := Encode(strings.Repeat("x", 1025)); err == nil {
|
|
t.Fatal("password longer than 1024 bytes accepted")
|
|
}
|
|
if _, err := Encode(strings.Repeat("密", 1025)); err == nil {
|
|
t.Fatal("password longer than 1024 characters accepted")
|
|
}
|
|
}
|