feat: 益铸 EntitleForge SDK 独立仓库首个版本
sdk-ci / test (push) Failing after 8s

- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属:
  license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓)
- sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc)
- license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书
- 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require
- CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
This commit is contained in:
iceking2nd committed 2026-11-09 15:42:22 +08:00
commit 81c7295893
32 files changed
+3940

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
package license
import (
"encoding/json"
"testing"
)
func testPayload() Payload {
return Payload{
JTI: "01JD8Z9W7S8XB5K2M3N4Q5R6TV",
ISS: "EntitleForge",
TID: "01JD8Y3N2M1K5J7H9G0F2E4D6R",
Prod: "prod_01JD8Y5P4Q3R2S1T0U9V8W7X6",
IAT: 1762665600,
NBF: 1762665600,
EXP: 1794201600,
LT: TypeSubscription,
Ent: map[string]Entitlement{
"module.audit-log": Bool(true),
"seats": Quantity(50),
"tier": TierValue("enterprise"),
allowedRegionsKey: Constraint([]string{"cn-north", "cn-east"}),
},
ST: StateIssued,
}
}
const allowedRegionsKey = "allowed-regions"
func TestPayload_Validate_OK(t *testing.T) {
p := testPayload()
if err := p.Validate(); err != nil {
t.Fatalf("合法 payload 不应报错: %v", err)
}
p.EXP = 0
p.LT = TypePerpetual
if err := p.Validate(); err != nil {
t.Fatalf("perpetual + exp=0 不应报错: %v", err)
}
p.Bind = &Bindings{FP: "sha256:9f86d0"}
p.Pol = &PolicySnapshot{Heartbeat: 900, LeaseTTL: 3600, Grace: 259200, ForcedCheck: 86400}
if err := p.Validate(); err != nil {
t.Fatalf("带 bind/pol 不应报错: %v", err)
}
}
func TestPayload_Validate_MissingFields(t *testing.T) {
cases := []struct {
name string
mut func(*Payload)
}{
{"缺 jti", func(p *Payload) { p.JTI = "" }},
{"缺 iss", func(p *Payload) { p.ISS = "" }},
{"缺 tid", func(p *Payload) { p.TID = "" }},
{"缺 prod", func(p *Payload) { p.Prod = "" }},
{"缺 iat", func(p *Payload) { p.IAT = 0 }},
{"缺 lt", func(p *Payload) { p.LT = "" }},
{"缺 st", func(p *Payload) { p.ST = "" }},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := testPayload()
tc.mut(&p)
if err := p.Validate(); err == nil {
t.Error("应报错, got nil")
}
})
}
}
func TestPayload_Validate_Enums(t *testing.T) {
cases := []struct {
name string
mut func(*Payload)
}{
{"未知 lt", func(p *Payload) { p.LT = "forever" }},
{"未知 st", func(p *Payload) { p.ST = "draft" }},
{"exp=0 但非 perpetual", func(p *Payload) { p.EXP = 0; p.LT = TypeSubscription }},
{"lease 类型缺 pol.lease_ttl", func(p *Payload) { p.LT = TypeLease }},
{"lease 类型带 lease_ttl", func(p *Payload) {
p.LT = TypeLease
p.Pol = &PolicySnapshot{LeaseTTL: 3600}
}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := testPayload()
tc.mut(&p)
err := p.Validate()
switch tc.name {
case "lease 类型带 lease_ttl":
if err != nil {
t.Errorf("不应报错: %v", err)
}
default:
if err == nil {
t.Error("应报错, got nil")
}
}
})
}
}
func TestPayload_TimeValid(t *testing.T) {
const skew = 600
base := int64(1762665600)
cases := []struct {
name string
mut func(*Payload)
now int64
want error
}{
{"时间窗内", func(p *Payload) {}, base + 100, nil},
{"nbf 未来超出容忍", func(p *Payload) { p.NBF = base + 3600 }, base, ErrNotYetValid},
{"nbf 未来但在容忍内", func(p *Payload) { p.NBF = base + 300 }, base, nil},
{"exp 已过超出容忍", func(p *Payload) { p.EXP = base - 3600 }, base, ErrExpired},
{"exp 已过但在容忍内", func(p *Payload) { p.EXP = base - 300 }, base, nil},
{"exp=0 永久有效", func(p *Payload) { p.EXP = 0; p.LT = TypePerpetual }, base, nil},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := testPayload()
tc.mut(&p)
got := p.TimeValid(tc.now, skew)
if got != tc.want {
t.Errorf("got %v want %v", got, tc.want)
}
})
}
}
func TestPayload_StrictDecode(t *testing.T) {
// 严格模式:ent 为 null / ent 值类型错位 → 报错
cases := []struct {
name string
payload string
}{
{"ent 为 null", `{"jti":"x","iss":"EntitleForge","tid":"t","prod":"p","iat":1,"nbf":1,"exp":2,"lt":"subscription","ent":null,"st":"issued"}`},
{"jti 类型错位", `{"jti":123,"iss":"EntitleForge","tid":"t","prod":"p","iat":1,"nbf":1,"exp":2,"lt":"subscription","ent":{},"st":"issued"}`},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var p Payload
if err := json.Unmarshal([]byte(tc.payload), &p); err == nil {
if err := p.Validate(); err == nil {
t.Error("应报错, got nil")
}
}
})
}
}
func TestPayload_IgnoresUnknownFields(t *testing.T) {
// 前向兼容:payload 出现未知字段应忽略
raw := `{"jti":"x","iss":"EntitleForge","tid":"t","prod":"p","iat":1,"nbf":1,"exp":2,"lt":"subscription","ent":{},"st":"issued","future_field":true}`
var p Payload
if err := json.Unmarshal([]byte(raw), &p); err != nil {
t.Fatalf("未知字段应忽略: %v", err)
}
if err := p.Validate(); err != nil {
t.Errorf("未知字段不影响校验: %v", err)
}
}