基于 backend-code-audit 审计报告,修复剩余 P0/P1/P2 共 34 项问题: P0 生产 Bug: - 修复 time.Since(time.Now()) 计时逻辑错误 (P0-03) - generateRandomID 改用 crypto/rand 替代固定索引 (P0-04) - IncrementQuotaUsed 重写为 Ent 原子操作消除 TOCTOU 竞态 (P0-05) 安全加固: - gateway/openai handler 错误响应替换为泛化消息,防止内部信息泄露 (P1-14) - usage_log_repo dateFormat 参数改用白名单映射,防止 SQL 注入 (P1-16) - 默认配置安全加固:sslmode=prefer、response_headers=true、mode=release (P1-18/19, P2-15) 性能优化: - gateway handler 循环内 defer 替换为显式 releaseWait 闭包 (P1-02) - group_repo/promo_code_repo Count 前 Clone 查询避免状态污染 (P1-03) - usage_log_repo 四个查询添加 LIMIT 10000 防止 OOM (P1-07) - GetBatchUsageStats 添加时间范围参数,默认最近 30 天 (P1-10) - ip.go CIDR 预编译为包级变量 (P1-11) - BatchUpdateCredentials 重构为先验证后更新 (P1-13) 缓存一致性: - billing_cache 添加 jitteredTTL 防止缓存雪崩 (P2-10) - DeductUserBalance/UpdateSubscriptionUsage 错误传播修复 (P2-12) - UserService.UpdateBalance 成功后异步失效 billingCache (P2-13) 代码质量: - search 截断改为按 rune 处理,支持多字节字符 (P2-01) - TLS Handshake 改为 HandshakeContext 支持 context 取消 (P2-07) - CORS 预检添加 Access-Control-Max-Age: 86400 (P2-16) 测试覆盖: - 新增 user_service_test.go(UpdateBalance 缓存失效 6 个用例) - 新增 batch_update_credentials_test.go(fail-fast + 类型验证 7 个用例) - 新增 response_transformer_test.go、ip_test.go、usage_log_repo_unit_test.go、search_truncate_test.go - 集成测试:IncrementQuotaUsed 并发测试、billing_cache 错误传播测试 - config_test.go 补充 server.mode/sslmode 默认值断言 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
112 lines
2.3 KiB
Go
112 lines
2.3 KiB
Go
//go:build unit
|
|
|
|
package repository
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBillingBalanceKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
userID int64
|
|
expected string
|
|
}{
|
|
{
|
|
name: "normal_user_id",
|
|
userID: 123,
|
|
expected: "billing:balance:123",
|
|
},
|
|
{
|
|
name: "zero_user_id",
|
|
userID: 0,
|
|
expected: "billing:balance:0",
|
|
},
|
|
{
|
|
name: "negative_user_id",
|
|
userID: -1,
|
|
expected: "billing:balance:-1",
|
|
},
|
|
{
|
|
name: "max_int64",
|
|
userID: math.MaxInt64,
|
|
expected: "billing:balance:9223372036854775807",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := billingBalanceKey(tc.userID)
|
|
require.Equal(t, tc.expected, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBillingSubKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
userID int64
|
|
groupID int64
|
|
expected string
|
|
}{
|
|
{
|
|
name: "normal_ids",
|
|
userID: 123,
|
|
groupID: 456,
|
|
expected: "billing:sub:123:456",
|
|
},
|
|
{
|
|
name: "zero_ids",
|
|
userID: 0,
|
|
groupID: 0,
|
|
expected: "billing:sub:0:0",
|
|
},
|
|
{
|
|
name: "negative_ids",
|
|
userID: -1,
|
|
groupID: -2,
|
|
expected: "billing:sub:-1:-2",
|
|
},
|
|
{
|
|
name: "max_int64_ids",
|
|
userID: math.MaxInt64,
|
|
groupID: math.MaxInt64,
|
|
expected: "billing:sub:9223372036854775807:9223372036854775807",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := billingSubKey(tc.userID, tc.groupID)
|
|
require.Equal(t, tc.expected, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestJitteredTTL(t *testing.T) {
|
|
const (
|
|
minTTL = 4*time.Minute + 30*time.Second // 270s = 5min - 30s
|
|
maxTTL = 5*time.Minute + 30*time.Second // 330s = 5min + 30s
|
|
)
|
|
|
|
for i := 0; i < 200; i++ {
|
|
ttl := jitteredTTL()
|
|
require.GreaterOrEqual(t, ttl, minTTL, "jitteredTTL() 返回值低于下限: %v", ttl)
|
|
require.LessOrEqual(t, ttl, maxTTL, "jitteredTTL() 返回值超过上限: %v", ttl)
|
|
}
|
|
}
|
|
|
|
func TestJitteredTTL_HasVariation(t *testing.T) {
|
|
// 多次调用应该产生不同的值(验证抖动存在)
|
|
seen := make(map[time.Duration]struct{}, 50)
|
|
for i := 0; i < 50; i++ {
|
|
seen[jitteredTTL()] = struct{}{}
|
|
}
|
|
// 50 次调用中应该至少有 2 个不同的值
|
|
require.Greater(t, len(seen), 1, "jitteredTTL() 应产生不同的 TTL 值")
|
|
}
|