feat(sdk): 支持编译截止授权模式与 OUTDATED 降级状态
- payload 新增 bc(编译截止 Unix 秒)/bcm(deny|degrade),结构校验拒绝负值与非法动作 - SDK 支持 WithBuildTime/WithBuildTimeUnix 与 ldflags BuildTime 注入;未提供构建时间时 fail-closed - degrade 模式进入 OUTDATED 状态,授权查询零值但进程继续运行;修复心跳/Proxy 续租覆盖该状态的隐患 - 缓存恢复路径同样执行截止判定;无截止的存量 license 行为不变 - 补充 license/sdk 单元测试与状态迁移回归用例
This commit is contained in:
9 files changed
+631
-30
No files matched your search
@@ -101,6 +101,99 @@ func TestPayload_Validate_Enums(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayload_BuildCutoffEnums(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
bc int64
|
||||
bcm string
|
||||
wantErr bool
|
||||
}{
|
||||
{"无截止", 0, "", false},
|
||||
{"截止+默认动作", 1762665600, "", false},
|
||||
{"截止+deny", 1762665600, BuildCutoffDeny, false},
|
||||
{"截止+degrade", 1762665600, BuildCutoffDegrade, false},
|
||||
{"负截止", -1, "", true},
|
||||
{"未知动作", 1762665600, "allow", true},
|
||||
{"有动作无截止", 0, BuildCutoffDegrade, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := testPayload()
|
||||
p.BC, p.BCM = tc.bc, tc.bcm
|
||||
err := p.Validate()
|
||||
if tc.wantErr && err == nil {
|
||||
t.Error("应报错, got nil")
|
||||
}
|
||||
if !tc.wantErr && err != nil {
|
||||
t.Errorf("不应报错: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayload_BuildCutoffExceeded(t *testing.T) {
|
||||
const cutoff = int64(1762665600)
|
||||
cases := []struct {
|
||||
name string
|
||||
bc int64
|
||||
buildTime int64
|
||||
want bool
|
||||
}{
|
||||
{"无截止不限制", 0, cutoff + 86400, false},
|
||||
{"截止前编译", cutoff, cutoff - 1, false},
|
||||
{"恰在截止时刻编译(含)", cutoff, cutoff, false},
|
||||
{"截止后编译", cutoff, cutoff + 1, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := testPayload()
|
||||
p.BC = tc.bc
|
||||
if got := p.BuildCutoffExceeded(tc.buildTime); got != tc.want {
|
||||
t.Errorf("BuildCutoffExceeded(%d) = %v, want %v", tc.buildTime, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayload_EffectiveBuildCutoffAction(t *testing.T) {
|
||||
p := testPayload()
|
||||
p.BC = 1762665600
|
||||
if got := p.EffectiveBuildCutoffAction(); got != BuildCutoffDeny {
|
||||
t.Errorf("缺省动作应为 deny: %q", got)
|
||||
}
|
||||
p.BCM = BuildCutoffDegrade
|
||||
if got := p.EffectiveBuildCutoffAction(); got != BuildCutoffDegrade {
|
||||
t.Errorf("动作应为 degrade: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayload_BuildCutoffJSON(t *testing.T) {
|
||||
p := testPayload()
|
||||
p.BC, p.BCM = 1762665600, BuildCutoffDegrade
|
||||
raw, err := json.Marshal(&p)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
var got Payload
|
||||
if err := json.Unmarshal(raw, &got); err != nil {
|
||||
t.Fatalf("roundtrip: %v", err)
|
||||
}
|
||||
if got.BC != p.BC || got.BCM != p.BCM {
|
||||
t.Errorf("roundtrip 不一致: bc=%d bcm=%q", got.BC, got.BCM)
|
||||
}
|
||||
// 无截止时省略字段(向前兼容)
|
||||
p.BC, p.BCM = 0, ""
|
||||
raw, _ = json.Marshal(&p)
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(raw, &m)
|
||||
if _, ok := m["bc"]; ok {
|
||||
t.Error("bc=0 时应省略字段")
|
||||
}
|
||||
if _, ok := m["bcm"]; ok {
|
||||
t.Error("bcm 为空时应省略字段")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayload_TimeValid(t *testing.T) {
|
||||
const skew = 600
|
||||
base := int64(1762665600)
|
||||
|
||||
Reference in new issue
Block a user