You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

166 lines
3.9 KiB

package utils
import (
"testing"
)
func TestHashPassword(t *testing.T) {
password := "testpassword123"
// 测试基本加密功能
hashedPassword, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
if hashedPassword == "" {
t.Fatal("HashedPassword should not be empty")
}
if hashedPassword == password {
t.Fatal("HashedPassword should not equal original password")
}
}
func TestHashPasswordWithCost(t *testing.T) {
password := "testpassword123"
cost := 12
// 测试指定成本的加密
hashedPassword, err := HashPasswordWithCost(password, cost)
if err != nil {
t.Fatalf("HashPasswordWithCost failed: %v", err)
}
// 验证成本
actualCost, err := GetCost(hashedPassword)
if err != nil {
t.Fatalf("GetCost failed: %v", err)
}
if actualCost != cost {
t.Fatalf("Expected cost %d, got %d", cost, actualCost)
}
}
func TestCheckPassword(t *testing.T) {
password := "testpassword123"
wrongPassword := "wrongpassword"
// 加密密码
hashedPassword, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
// 测试正确密码验证
if !CheckPassword(hashedPassword, password) {
t.Fatal("CheckPassword should return true for correct password")
}
// 测试错误密码验证
if CheckPassword(hashedPassword, wrongPassword) {
t.Fatal("CheckPassword should return false for wrong password")
}
}
func TestCheckPasswordWithError(t *testing.T) {
password := "testpassword123"
wrongPassword := "wrongpassword"
// 加密密码
hashedPassword, err := HashPassword(password)
if err != nil {
t.Fatalf("HashPassword failed: %v", err)
}
// 测试正确密码验证
err = CheckPasswordWithError(hashedPassword, password)
if err != nil {
t.Fatalf("CheckPasswordWithError should return nil for correct password, got: %v", err)
}
// 测试错误密码验证
err = CheckPasswordWithError(hashedPassword, wrongPassword)
if err == nil {
t.Fatal("CheckPasswordWithError should return error for wrong password")
}
}
func TestPasswordValidation(t *testing.T) {
// 测试空密码
_, err := HashPassword("")
if err != ErrPasswordEmpty {
t.Fatalf("Expected ErrPasswordEmpty, got: %v", err)
}
// 测试过长密码 (> 72 bytes)
longPassword := make([]byte, 73)
for i := range longPassword {
longPassword[i] = 'a'
}
_, err = HashPassword(string(longPassword))
if err != ErrPasswordTooLong {
t.Fatalf("Expected ErrPasswordTooLong, got: %v", err)
}
// 测试无效成本
_, err = HashPasswordWithCost("test", 3) // 低于 MinCost
if err != ErrInvalidCost {
t.Fatalf("Expected ErrInvalidCost for low cost, got: %v", err)
}
_, err = HashPasswordWithCost("test", 32) // 高于 MaxCost
if err != ErrInvalidCost {
t.Fatalf("Expected ErrInvalidCost for high cost, got: %v", err)
}
}
func TestNeedsRehash(t *testing.T) {
password := "testpassword123"
// 使用较低成本加密
lowCostPassword, err := HashPasswordWithCost(password, 8)
if err != nil {
t.Fatalf("HashPasswordWithCost failed: %v", err)
}
// 检查是否需要重新哈希
needsRehash, err := NeedsRehash(lowCostPassword, 12)
if err != nil {
t.Fatalf("NeedsRehash failed: %v", err)
}
if !needsRehash {
t.Fatal("Should need rehash when current cost is lower than preferred cost")
}
// 使用相同成本检查
needsRehash, err = NeedsRehash(lowCostPassword, 8)
if err != nil {
t.Fatalf("NeedsRehash failed: %v", err)
}
if needsRehash {
t.Fatal("Should not need rehash when current cost equals preferred cost")
}
}
func TestEmptyInputs(t *testing.T) {
// 测试空哈希密码
if CheckPassword("", "password") {
t.Fatal("CheckPassword should return false for empty hash")
}
// 测试空输入密码
if CheckPassword("hash", "") {
t.Fatal("CheckPassword should return false for empty password")
}
// 测试 GetCost 的空输入
_, err := GetCost("")
if err == nil {
t.Fatal("GetCost should return error for empty hash")
}
}