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 + 零依赖断言 + 三平台交叉编译冒烟)
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package license
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseLooseEntitlements(t *testing.T) {
|
|
raw := map[string]any{
|
|
"module.audit-log": true,
|
|
"seats": 50,
|
|
"tier": "enterprise",
|
|
"allowed-regions": []any{"cn-north", "cn-east"},
|
|
"explicit": map[string]any{"t": "q", "v": 100},
|
|
}
|
|
got, err := ParseLooseEntitlements(raw)
|
|
if err != nil {
|
|
t.Fatalf("ParseLooseEntitlements: %v", err)
|
|
}
|
|
if !got["module.audit-log"].Equal(Bool(true)) ||
|
|
!got["seats"].Equal(Quantity(50)) ||
|
|
!got["tier"].Equal(TierValue("enterprise")) ||
|
|
!got["allowed-regions"].Equal(Constraint([]string{"cn-north", "cn-east"})) ||
|
|
!got["explicit"].Equal(Quantity(100)) {
|
|
t.Errorf("解析结果错误: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestParseLooseEntitlements_Rejects(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
raw map[string]any
|
|
}{
|
|
{"浮点", map[string]any{"seats": 1.5}},
|
|
{"嵌套对象", map[string]any{"x": map[string]any{"y": 1}}},
|
|
{"混合列表", map[string]any{"regions": []any{"cn", 1}}},
|
|
{"显式类型错位", map[string]any{"seats": map[string]any{"t": "q", "v": "50"}}},
|
|
{"显式未知类型", map[string]any{"x": map[string]any{"t": "z", "v": 1}}},
|
|
{"显式缺 v", map[string]any{"x": map[string]any{"t": "q"}}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if _, err := ParseLooseEntitlements(tc.raw); err == nil {
|
|
t.Error("应报错")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGenerateLicenseKey(t *testing.T) {
|
|
pattern := regexp.MustCompile(`^ef-[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4}$`)
|
|
seen := make(map[string]struct{}, 1000)
|
|
for range 1000 {
|
|
key, err := GenerateLicenseKey()
|
|
if err != nil {
|
|
t.Fatalf("GenerateLicenseKey: %v", err)
|
|
}
|
|
if !pattern.MatchString(key) {
|
|
t.Fatalf("格式非法: %q", key)
|
|
}
|
|
if _, dup := seen[key]; dup {
|
|
t.Fatalf("重复 key: %q", key)
|
|
}
|
|
seen[key] = struct{}{}
|
|
}
|
|
}
|
|
|
|
func TestLicenseKeyHash(t *testing.T) {
|
|
key, _ := GenerateLicenseKey()
|
|
h1 := LicenseKeyHash(key)
|
|
h2 := LicenseKeyHash(key)
|
|
if h1 != h2 {
|
|
t.Error("同 key 哈希应一致")
|
|
}
|
|
other, _ := GenerateLicenseKey()
|
|
if LicenseKeyHash(other) == h1 {
|
|
t.Error("不同 key 哈希应不同")
|
|
}
|
|
if len(h1) != len("sha256:")+64 {
|
|
t.Errorf("哈希长度: %q", h1)
|
|
}
|
|
}
|