CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package crypto
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHashPasswordAsBcrypt_RoundTrip(t *testing.T) {
|
|
password := "correct horse battery staple"
|
|
|
|
hash, err := HashPasswordAsBcrypt(password)
|
|
if err != nil {
|
|
t.Fatalf("HashPasswordAsBcrypt returned error: %v", err)
|
|
}
|
|
if hash == "" {
|
|
t.Fatal("expected non-empty hash")
|
|
}
|
|
if hash == password {
|
|
t.Fatal("hash must not equal the plaintext password")
|
|
}
|
|
if !strings.HasPrefix(hash, "$2") {
|
|
t.Fatalf("expected bcrypt prefix $2..., got %q", hash[:min(4, len(hash))])
|
|
}
|
|
|
|
if !CheckPasswordHash(hash, password) {
|
|
t.Fatal("CheckPasswordHash returned false for the matching password")
|
|
}
|
|
}
|
|
|
|
func TestCheckPasswordHash_WrongPassword(t *testing.T) {
|
|
hash, err := HashPasswordAsBcrypt("right-password")
|
|
if err != nil {
|
|
t.Fatalf("HashPasswordAsBcrypt returned error: %v", err)
|
|
}
|
|
|
|
if CheckPasswordHash(hash, "wrong-password") {
|
|
t.Fatal("CheckPasswordHash returned true for a wrong password")
|
|
}
|
|
if CheckPasswordHash(hash, "") {
|
|
t.Fatal("CheckPasswordHash returned true for an empty password")
|
|
}
|
|
}
|
|
|
|
func TestCheckPasswordHash_InvalidHash(t *testing.T) {
|
|
if CheckPasswordHash("", "anything") {
|
|
t.Fatal("empty hash must not validate")
|
|
}
|
|
if CheckPasswordHash("not-a-bcrypt-hash", "anything") {
|
|
t.Fatal("malformed hash must not validate")
|
|
}
|
|
}
|
|
|
|
func TestHashPasswordAsBcrypt_DifferentHashesForSamePassword(t *testing.T) {
|
|
password := "same-password"
|
|
h1, err := HashPasswordAsBcrypt(password)
|
|
if err != nil {
|
|
t.Fatalf("first hash failed: %v", err)
|
|
}
|
|
h2, err := HashPasswordAsBcrypt(password)
|
|
if err != nil {
|
|
t.Fatalf("second hash failed: %v", err)
|
|
}
|
|
if h1 == h2 {
|
|
t.Fatal("expected bcrypt to produce different hashes (random salt) for the same password")
|
|
}
|
|
if !CheckPasswordHash(h1, password) || !CheckPasswordHash(h2, password) {
|
|
t.Fatal("both hashes should still validate the original password")
|
|
}
|
|
}
|