106 lines
2.2 KiB
Go
106 lines
2.2 KiB
Go
package crypto
|
||
|
||
import (
|
||
"testing"
|
||
)
|
||
|
||
func TestPasswordHashAndVerify(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
password string
|
||
cost []int
|
||
}{
|
||
{
|
||
name: "普通密码-默认复杂度",
|
||
password: "password123",
|
||
cost: []int{},
|
||
},
|
||
{
|
||
name: "普通密码-低复杂度",
|
||
password: "password123",
|
||
cost: []int{4},
|
||
},
|
||
{
|
||
name: "空密码",
|
||
password: "",
|
||
cost: []int{},
|
||
},
|
||
{
|
||
name: "中文密码",
|
||
password: "密码123",
|
||
cost: []int{},
|
||
},
|
||
{
|
||
name: "特殊字符密码",
|
||
password: "p@$$w0rd!#*&",
|
||
cost: []int{},
|
||
},
|
||
{
|
||
name: "我的密码",
|
||
password: "aA2021.12.31",
|
||
cost: []int{10},
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
// 1. 测试加密
|
||
hash, err := PasswordHash(tt.password, tt.cost...)
|
||
if err != nil {
|
||
t.Errorf("PasswordHash() error = %v", err)
|
||
return
|
||
}
|
||
if hash == "" {
|
||
t.Error("PasswordHash() 返回空哈希值")
|
||
}
|
||
if hash == tt.password {
|
||
t.Error("PasswordHash() 返回的哈希值与明文密码相同")
|
||
}
|
||
|
||
// 2. 测试验证 - 正确密码
|
||
if !PasswordVerify(tt.password, hash) {
|
||
t.Error("PasswordVerify() 无法验证正确的密码")
|
||
}
|
||
|
||
// 3. 测试验证 - 错误密码
|
||
if PasswordVerify(tt.password+"wrong", hash) {
|
||
t.Error("PasswordVerify() 错误地验证了错误的密码")
|
||
}
|
||
t.Logf("用户名: %s, 密码: %s, 哈希值: %s", tt.name, tt.password, hash)
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestPasswordHashWithInvalidCost(t *testing.T) {
|
||
// bcrypt库的cost范围是4-31,超出范围会返回错误
|
||
password := "testpassword"
|
||
|
||
// 测试过低的cost
|
||
_, err := PasswordHash(password, 2)
|
||
if err == nil {
|
||
t.Error("PasswordHash() 应该对过低的cost返回错误")
|
||
}
|
||
|
||
// 测试过高的cost
|
||
_, err = PasswordHash(password, 32)
|
||
if err == nil {
|
||
t.Error("PasswordHash() 应该对过高的cost返回错误")
|
||
}
|
||
}
|
||
|
||
func BenchmarkPasswordHash(b *testing.B) {
|
||
password := "benchmark-password"
|
||
for i := 0; i < b.N; i++ {
|
||
_, _ = PasswordHash(password)
|
||
}
|
||
}
|
||
|
||
func BenchmarkPasswordVerify(b *testing.B) {
|
||
password := "benchmark-password"
|
||
hash, _ := PasswordHash(password)
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
_ = PasswordVerify(password, hash)
|
||
}
|
||
}
|