修复全量测试竞态并更新审查夹具,保存证书覆盖调整前基线
This commit is contained in:
1 parent
78dfcde5e1
commit
2aac6dd9be
15 files changed
+146
-158
No files matched your search
@@ -0,0 +1,18 @@
|
||||
// Package passwordcapacity 管理所有密码计算入口共享的并发名额。
|
||||
package passwordcapacity
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrBusy = errors.New("password computation is busy")
|
||||
|
||||
var computationSlot = make(chan struct{}, 1)
|
||||
|
||||
// Acquire 满载时立即拒绝,不积压等待请求;调用方必须延迟释放名额。
|
||||
func Acquire() (func(), error) {
|
||||
select {
|
||||
case computationSlot <- struct{}{}:
|
||||
return func() { <-computationSlot }, nil
|
||||
default:
|
||||
return nil, ErrBusy
|
||||
}
|
||||
}
|
||||
@@ -2,62 +2,23 @@
|
||||
package passwordtest
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
"git.pcux.cn/xuans/Smail/internal/passwordcapacity"
|
||||
)
|
||||
|
||||
type heldReader struct {
|
||||
original io.Reader
|
||||
entered chan struct{}
|
||||
resume chan struct{}
|
||||
enteredOnce atomic.Bool
|
||||
}
|
||||
|
||||
func (r *heldReader) Read(p []byte) (int, error) {
|
||||
if r.enteredOnce.CompareAndSwap(false, true) {
|
||||
close(r.entered)
|
||||
<-r.resume
|
||||
panic(r)
|
||||
}
|
||||
return r.original.Read(p)
|
||||
}
|
||||
|
||||
// Hold 仅供非并行测试使用,释放时同时验证异常路径没有泄漏计算名额。
|
||||
// Hold 仅供非并行测试使用,直接占用计算名额,不修改 TLS 等服务使用的随机源。
|
||||
func Hold(t *testing.T) func() {
|
||||
t.Helper()
|
||||
r := &heldReader{original: rand.Reader, entered: make(chan struct{}), resume: make(chan struct{})}
|
||||
rand.Reader = r
|
||||
done := make(chan any, 1)
|
||||
go func() {
|
||||
defer func() { done <- recover() }()
|
||||
_, _ = password.Hash("HeldPass123")
|
||||
}()
|
||||
unlock, err := passwordcapacity.Acquire()
|
||||
if err != nil {
|
||||
t.Fatalf("密码计算名额未被测试请求占用:%v", err)
|
||||
}
|
||||
var once sync.Once
|
||||
release := func() {
|
||||
once.Do(func() {
|
||||
close(r.resume)
|
||||
defer func() { rand.Reader = r.original }()
|
||||
select {
|
||||
case result := <-done:
|
||||
if result != r {
|
||||
t.Errorf("密码测试占位未经过预期的异常释放路径")
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("密码测试占位释放超时")
|
||||
}
|
||||
})
|
||||
once.Do(unlock)
|
||||
}
|
||||
t.Cleanup(release)
|
||||
select {
|
||||
case <-r.entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("密码计算名额未被测试请求占用")
|
||||
}
|
||||
return release
|
||||
}
|
||||
@@ -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