Files

167 lines
5.6 KiB
Go

// scope_test.go Day 51(R3 验收):请求级快照——SQL 计数断言与逐号旧路径语义回归。
package interceptor
import (
"context"
"sync"
"sync/atomic"
"testing"
"go-sms/internal/model/entity"
"gorm.io/gorm"
)
// batch100 生成 100 个连续测试号(避开造数命中号 137/138 段)。
func batch100() []string {
out := make([]string, 100)
for i := range out {
out[i] = "1590000" + threeDigits(i)
}
return out
}
func threeDigits(i int) string {
return string([]byte{byte('0' + i/100), byte('0' + (i/10)%10), byte('0' + i%10)})
}
// countQueries 注册查询计数 callback(作用于该 db 会话;name 需库内唯一)。
func countQueries(db *gorm.DB, name string) *int32 {
var n int32
_ = db.Callback().Query().Before("gorm:query").Register(name, func(tx *gorm.DB) {
atomic.AddInt32(&n, 1)
})
return &n
}
// TestScopeSQLCount 验收(Day51):批量 100 号经 scope SQL ≤3 次
// (白名单 IN/黑名单 IN/策略 Find 各 1);逐号旧路径 ≥200 次(每号 4 查)。
func TestScopeSQLCount(t *testing.T) {
db := newTestDB(t)
if err := db.Create(&entity.PolicyBlacklist{Mobile: "13700000000", AccountID: 1}).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&entity.PolicyFrequency{AccountID: 0, Dim: 1, WindowUnit: 1,
MaxCount: 1000, Status: 1}).Error; err != nil {
t.Fatal(err)
}
ctx := context.Background()
mobiles := batch100()
// ① scope 路径:3 次(白名单+黑名单+策略)。
chain := NewDefaultChain(db, nil, nil, Options{FreqStore: NewMemoryFreqStore()})
n := countQueries(db, "test:count_scope")
sc := chain.NewScope(ctx, 1, mobiles)
for _, m := range mobiles {
if b := chain.Check(ctx, &Req{AccountID: 1, Mobile: m,
Content: "【云】R3 计数验证", Scope: sc}); b != nil {
t.Fatalf("普通号 %s 不应拦截: %+v", m, b)
}
}
if *n > 3 {
t.Fatalf("scope 批量 100 号 SQL=%d,应 ≤3(白名单/黑名单/策略各 1)", *n)
}
t.Logf("scope 路径 100 号 SQL=%d", *n)
// ② 逐号旧路径(nil scope):黑名单 1 命中号短路,其余每号 4 查 ≥200。
n2 := countQueries(db, "test:count_legacy")
for _, m := range mobiles {
_ = chain.Check(ctx, &Req{AccountID: 1, Mobile: m, Content: "【云】R3 计数验证"})
}
if *n2 < 200 {
t.Fatalf("逐号旧路径 100 号 SQL=%d,应 ≥200(每号 4 查——Day50 定位的重复查询)", *n2)
}
t.Logf("逐号旧路径 100 号 SQL=%d", *n2)
}
// TestScopeSemantics 回归:scope 与逐号路径判定一致(白名单豁免/黑名单命中/频次超限/签名)。
func TestScopeSemantics(t *testing.T) {
db := newTestDB(t)
// 造数:1371=白名单豁免号(同时命中黑名单,验证豁免优先);1372=纯黑名单;策略 max=2。
seed := []interface{}{
&entity.PolicyBlacklist{Mobile: "13711111111", AccountID: 1},
&entity.PolicyBlacklist{Mobile: "13722222222", AccountID: 1},
&entity.PolicyWhitelist{Mobile: "13711111111", AccountID: 1},
&entity.PolicyFrequency{AccountID: 0, Dim: 1, WindowUnit: 1, MaxCount: 2, Status: 1},
}
for _, s := range seed {
if err := db.Create(s).Error; err != nil {
t.Fatal(err)
}
}
newChain := func() *Chain {
return NewDefaultChain(db, nil, nil, Options{FreqStore: NewMemoryFreqStore()})
}
ctx := context.Background()
cases := []struct {
name, mobile string
}{
{"白名单豁免(同号黑名单仍在库)", "13711111111"},
{"纯黑名单", "13722222222"},
{"频次超限(第 3 次)", "13733333333"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// 各自独立链+频次计数(同号 3 次触发超限)。
chScoped, chLegacy := newChain(), newChain()
sc := chScoped.NewScope(ctx, 1, []string{tc.mobile})
var gotScoped, gotLegacy *Block
for i := 0; i < 3; i++ {
gotScoped = chScoped.Check(ctx, &Req{AccountID: 1, Mobile: tc.mobile,
Content: "【云】语义回归", Scope: sc})
gotLegacy = chLegacy.Check(ctx, &Req{AccountID: 1, Mobile: tc.mobile,
Content: "【云】语义回归"})
}
if (gotScoped == nil) != (gotLegacy == nil) {
t.Fatalf("scope=%v legacy=%v 判定不一致", gotScoped, gotLegacy)
}
if gotScoped != nil {
if gotScoped.Interceptor != gotLegacy.Interceptor || gotScoped.Code != gotLegacy.Code {
t.Fatalf("命中拦截器不一致: scope=%+v legacy=%+v", gotScoped, gotLegacy)
}
}
})
}
}
// fakeSignAllow 计数调用的签名白名单(验证 scope 下账号级只查一次)。
type fakeSignAllow struct {
mu sync.Mutex
calls int
}
func (f *fakeSignAllow) AllowedSigns(_ context.Context, _ int64) ([]string, error) {
f.mu.Lock()
f.calls++
f.mu.Unlock()
return []string{"云"}, nil
}
// TestScopeSignCachedOnce 验收:签名白名单账号级数据经 scope 缓存,批量内只查一次。
func TestScopeSignCachedOnce(t *testing.T) {
db := newTestDB(t)
allow := &fakeSignAllow{}
chain := NewDefaultChain(db, nil, nil, Options{FreqStore: NewMemoryFreqStore(), SignAllow: allow})
ctx := context.Background()
mobiles := batch100()
sc := chain.NewScope(ctx, 1, mobiles)
for _, m := range mobiles {
if b := chain.Check(ctx, &Req{AccountID: 1, Mobile: m,
Content: "【云】签名缓存验证", Scope: sc}); b != nil {
t.Fatalf("已报备签名不应拦截: %+v", b)
}
}
if allow.calls != 1 {
t.Fatalf("签名白名单调用 %d 次,scope 下应恰 1 次(账号级跨号复用)", allow.calls)
}
// 对照:逐号路径每号一查。
for _, m := range mobiles {
if b := chain.Check(ctx, &Req{AccountID: 1, Mobile: m, Content: "【云】签名缓存验证"}); b != nil {
t.Fatalf("逐号路径不应拦截: %+v", b)
}
}
if allow.calls != 101 {
t.Fatalf("逐号路径签名白名单调用 %d 次,应 101(1+100)", allow.calls)
}
}