Files
iceking2nd 834882e044
sdk-ci / test (push) Successful in 1m54s
sdk-release / release (push) Failing after 39s
sdk-release / cleanup (push) Successful in 4s
fix(sdk): 离线 license 默认自动校验机器指纹
- 新增 sdk.MachineFingerprint:Linux machine-id+DMI、macOS IOPlatformUUID,容器内 fail-closed
- 文件模式在 bind.fp 存在时自动校验,无需显式 WithFingerprint;采集失败拒绝绑定 license
- 新增 WithFingerprintFunc 支持容器读取挂载宿主 machine-id;在线激活同步使用自动指纹
- license 包新增 NormalizeFingerprint(sha256:64hex 校验与小写归一化)
- 新增指纹绑定/采集失败/无绑定跳过等测试
2026-11-14 20:02:12 +08:00

119 lines
3.8 KiB
Go

package sdk
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"git.wxccs.org/EntitleForge/EntitleForge-SDK-go/license"
)
func writeLicense(t *testing.T, token string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "license.eflic")
if err := os.WriteFile(path, []byte(token), 0o600); err != nil {
t.Fatal(err)
}
return path
}
// TestFileMode_AutoFingerprintBinding 验证 bind.fp 在未显式传入指纹时也会自动校验。
func TestFileMode_AutoFingerprintBinding(t *testing.T) {
e := newEnv(t)
const bound = "sha256:bound-value"
token := e.sign(func(p *license.Payload) {
p.Bind = &license.Bindings{FP: bound}
})
path := writeLicense(t, token)
cases := []struct {
name string
opt Option
wantErr string
}{
{"自动采集匹配", WithFingerprintFunc(func() (string, error) { return bound, nil }), ""},
{"自动采集不符", WithFingerprintFunc(func() (string, error) { return "sha256:other", nil }), "指纹绑定不符"},
{"自动采集失败", WithFingerprintFunc(func() (string, error) { return "", errors.New("无稳定硬件标识") }), "指纹采集失败"},
{"显式指纹覆盖自动采集", WithFingerprint(bound), ""},
{"显式指纹不符", WithFingerprint("sha256:explicit-other"), "指纹绑定不符"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
client, err := New(WithLicenseFile(path), WithProductKey("prod_test"),
WithTrustChain(e.trust()), WithClock(&fakeClock{now: e.now}), tc.opt)
if tc.wantErr != "" {
if err == nil {
t.Fatalf("应拒绝,实际通过(状态 %s)", client.State())
}
if !strings.Contains(err.Error(), tc.wantErr) {
t.Fatalf("错误应包含 %q: %v", tc.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("应通过: %v", err)
}
defer client.Close()
if client.State() != StateActive {
t.Fatalf("状态: %s", client.State())
}
})
}
}
// TestFileMode_DefaultProviderInvoked 验证默认路径确实调用了机器指纹采集器。
func TestFileMode_DefaultProviderInvoked(t *testing.T) {
e := newEnv(t)
token := e.sign(func(p *license.Payload) {
p.Bind = &license.Bindings{FP: "sha256:auto-collected"}
})
path := writeLicense(t, token)
old := machineFingerprintProvider
machineFingerprintProvider = func() (string, error) { return "sha256:auto-collected", nil }
defer func() { machineFingerprintProvider = old }()
client, err := New(WithLicenseFile(path), WithProductKey("prod_test"),
WithTrustChain(e.trust()), WithClock(&fakeClock{now: e.now}))
if err != nil {
t.Fatalf("默认自动采集应通过: %v", err)
}
defer client.Close()
if client.State() != StateActive {
t.Fatalf("状态: %s", client.State())
}
}
// TestFileMode_UnboundLicenseSkipsCollector 无绑定 license 不应因采集失败而不可用。
func TestFileMode_UnboundLicenseSkipsCollector(t *testing.T) {
e := newEnv(t)
path := writeLicense(t, e.sign(nil))
old := machineFingerprintProvider
machineFingerprintProvider = func() (string, error) { return "", errors.New("采集失败") }
defer func() { machineFingerprintProvider = old }()
client, err := New(WithLicenseFile(path), WithProductKey("prod_test"),
WithTrustChain(e.trust()), WithClock(&fakeClock{now: e.now}))
if err != nil {
t.Fatalf("无绑定 license 不应触发指纹采集: %v", err)
}
defer client.Close()
if client.State() != StateActive {
t.Fatalf("状态: %s", client.State())
}
}
// TestMachineFingerprint_Shape 验证公开采集 API 的形态;容器内无法采集时跳过。
func TestMachineFingerprint_Shape(t *testing.T) {
fp, err := MachineFingerprint()
if err != nil {
t.Skipf("当前环境无法自动采集(预期于容器内): %v", err)
}
if !strings.HasPrefix(fp, "sha256:") || len(fp) != len("sha256:")+64 {
t.Fatalf("指纹形态非法: %q", fp)
}
}