Dragon-Knight/internal/password/password_test.go

61 lines
1.4 KiB
Go

package password
import (
"testing"
)
func TestHashAndVerify(t *testing.T) {
password := "Demo123!"
// Test hashing
hash, err := Hash(password)
if err != nil {
t.Fatalf("Failed to hash password: %v", err)
}
// Test that hash is not empty
if hash == "" {
t.Fatal("Hash should not be empty")
}
// Test that hash contains expected format
if hash[:9] != "$argon2id" {
t.Fatal("Hash should start with $argon2id")
}
// Test verification with correct password
valid, err := Verify(password, hash)
if err != nil {
t.Fatalf("Failed to verify password: %v", err)
}
if !valid {
t.Fatal("Password should be valid")
}
// Test verification with incorrect password
valid, err = Verify("wrongpassword", hash)
if err != nil {
t.Fatalf("Failed to verify wrong password: %v", err)
}
if valid {
t.Fatal("Wrong password should not be valid")
}
// Test that hashing same password twice produces different hashes (due to salt)
hash2, err := Hash(password)
if err != nil {
t.Fatalf("Failed to hash password second time: %v", err)
}
if hash == hash2 {
t.Fatal("Hashing same password twice should produce different hashes due to salt")
}
// But both hashes should verify correctly
valid, err = Verify(password, hash2)
if err != nil {
t.Fatalf("Failed to verify second hash: %v", err)
}
if !valid {
t.Fatal("Second hash should also be valid")
}
}