修复全量测试竞态并更新审查夹具,保存证书覆盖调整前基线
This commit is contained in:
1 parent
78dfcde5e1
commit
2aac6dd9be
15 files changed
+146
-158
No files matched your search
@@ -1,17 +1,9 @@
|
||||
package password
|
||||
|
||||
import "errors"
|
||||
import "git.pcux.cn/xuans/Smail/internal/passwordcapacity"
|
||||
|
||||
var ErrBusy = errors.New("password computation is busy")
|
||||
|
||||
// 所有协议和恢复流程共享一个计算名额;满载立即拒绝,不积压等待请求。
|
||||
var computationSlot = make(chan struct{}, 1)
|
||||
var ErrBusy = passwordcapacity.ErrBusy
|
||||
|
||||
func acquireComputation() (func(), error) {
|
||||
select {
|
||||
case computationSlot <- struct{}{}:
|
||||
return func() { <-computationSlot }, nil
|
||||
default:
|
||||
return nil, ErrBusy
|
||||
}
|
||||
return passwordcapacity.Acquire()
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
)
|
||||
|
||||
func TestCapacityRejectsBurstAndReleasesAfterPanic(t *testing.T) {
|
||||
func TestCapacityRejectsBurstAndAllowsRetry(t *testing.T) {
|
||||
encoded, err := password.Hash("StrongPass12")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -39,7 +39,7 @@ func TestCapacityRejectsBurstAndReleasesAfterPanic(t *testing.T) {
|
||||
wg.Wait()
|
||||
release()
|
||||
if matched, err := password.Check(encoded, "StrongPass12"); !matched || err != nil {
|
||||
t.Fatalf("异常释放后不能继续认证:%v %v", matched, err)
|
||||
t.Fatalf("名额释放后不能继续认证:%v %v", matched, err)
|
||||
}
|
||||
if matched, err := password.Check(encoded, "WrongPass12"); matched || err != nil {
|
||||
t.Fatalf("密码错误与服务繁忙未区分:%v %v", matched, err)
|
||||
|
||||
@@ -39,6 +39,10 @@ func ValidateUserPassword(plain string) error {
|
||||
|
||||
// Hash 生成 Argon2id 密码哈希。
|
||||
func Hash(plain string) (string, error) {
|
||||
return hashWithRandom(plain, rand.Read)
|
||||
}
|
||||
|
||||
func hashWithRandom(plain string, readRandom func([]byte) (int, error)) (string, error) {
|
||||
plainSize := len([]byte(plain))
|
||||
if plainSize < minSecretSize {
|
||||
return "", ErrInvalidLength
|
||||
@@ -49,7 +53,7 @@ func Hash(plain string) (string, error) {
|
||||
}
|
||||
defer release()
|
||||
salt := make([]byte, saltLength)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
if _, err := readRandom(salt); err != nil {
|
||||
return "", fmt.Errorf("generate password salt: %w", err)
|
||||
}
|
||||
hash := argon2.IDKey([]byte(plain), salt, iterations, memory, parallelism, keyLength)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashRandomFailureReleasesCapacity(t *testing.T) {
|
||||
expected := errors.New("random source unavailable")
|
||||
_, err := hashWithRandom("StrongPass12", func([]byte) (int, error) {
|
||||
return 0, expected
|
||||
})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatalf("随机源异常未返回:%v", err)
|
||||
}
|
||||
if _, err := Hash("StrongPass12"); err != nil {
|
||||
t.Fatalf("随机源异常后计算名额未释放:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashRandomPanicReleasesCapacity(t *testing.T) {
|
||||
expected := errors.New("random source panic")
|
||||
func() {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != expected {
|
||||
t.Errorf("未保留预期的随机源异常:%v", recovered)
|
||||
}
|
||||
}()
|
||||
_, _ = hashWithRandom("StrongPass12", func([]byte) (int, error) {
|
||||
panic(expected)
|
||||
})
|
||||
}()
|
||||
if _, err := Hash("StrongPass12"); err != nil {
|
||||
t.Fatalf("随机源异常退出后计算名额未释放:%v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user