Files

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

// Package auth_test tests password hashing and verification.
package auth_test
import (
"testing"
"messenger/internal/auth"
)
func TestHashAndVerify(t *testing.T) {
pwd := "secret"
hash, err := auth.HashPassword(pwd)
if err != nil {
t.Fatalf("HashPassword error: %v", err)
}
ok, err := auth.VerifyPassword(hash, pwd)
if err != nil {
t.Fatalf("VerifyPassword error: %v", err)
}
if !ok {
t.Fatal("expected password to verify")
}
}
func TestWrongPassword(t *testing.T) {
hash, _ := auth.HashPassword("correct")
ok, err := auth.VerifyPassword(hash, "wrong")
if err != nil {
t.Fatalf("VerifyPassword error on wrong password: %v", err)
}
if ok {
t.Fatal("wrong password should not verify")
}
}
func TestInvalidHashFormat(t *testing.T) {
_, err := auth.VerifyPassword("invalidformat", "pwd")
if err == nil {
t.Fatal("expected error on invalid hash format")
}
}
func TestDifferentSalts(t *testing.T) {
h1, _ := auth.HashPassword("pass")
h2, _ := auth.HashPassword("pass")
if h1 == h2 {
t.Fatal("expected different hashes for same password due to salt")
}
}