Files
iceking2nd fc4ef4e644
sdk-ci / test (push) Successful in 2m19s
sdk-release / release (push) Failing after 50s
sdk-release / cleanup (push) Successful in 5s
refactor(crypto): 拆分签发与验证,新增 sign 包
- crypto 仅保留验证:验签、信任链、CRL/verdict、指纹
- 新增 sign 包:SignToken/SignBundle、密钥生成、私钥加解密
- TrustChain 移除 SignBundle 方法,改由 Bundle() 导出快照,
  避免反射可达性把 SignToken 链入纯验证二进制
- Bundle 快照单次读锁,修复旧 SignBundle 递归读锁死锁窗口
- 新增 golden token 与 go tool nm 符号级回归测试
2026-11-15 13:26:50 +08:00

100 lines
2.9 KiB
Go

package crypto_test
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
const sdkModule = "git.wxccs.org/EntitleForge/EntitleForge-SDK-go"
func moduleRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("无法定位测试文件")
}
return filepath.Dir(filepath.Dir(file))
}
func runGo(t *testing.T, dir string, args ...string) string {
t.Helper()
goBin, err := exec.LookPath("go")
if err != nil {
t.Skip("go 工具链不可用")
}
cmd := exec.Command(goBin, args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOWORK=off", "GOFLAGS=-mod=mod", "GOPROXY=off")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go %s 失败: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// TestVerifierBinaryHasNoSigningSymbols 锁定核心不变量:纯验证二进制即使触发
// linker 的反射方法保留规则,也不得链入任何签名或私钥操作符号。
func TestVerifierBinaryHasNoSigningSymbols(t *testing.T) {
if testing.Short() {
t.Skip("短模式跳过链接符号检查")
}
root := moduleRoot(t)
dir := t.TempDir()
gomod := "module verifierfixture\n\ngo 1.27\n\nrequire " + sdkModule + " v0.0.0\n\nreplace " + sdkModule + " => " + filepath.ToSlash(root) + "\n"
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0o644); err != nil {
t.Fatal(err)
}
fixture := `package main
import (
"crypto/ed25519"
"fmt"
"reflect"
"` + sdkModule + `/crypto"
)
func main() {
chain := crypto.NewTrustChain(make(ed25519.PublicKey, ed25519.PublicKeySize))
// 触发 linker 的反射方法保留规则:修复前会把 (*TrustChain).SignBundle 与 SignToken 链入。
fmt.Println(reflect.TypeOf(chain).Method(0).Name)
fmt.Println(chain.KeyFor("none"))
}
`
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(fixture), 0o644); err != nil {
t.Fatal(err)
}
bin := filepath.Join(dir, "verifier")
runGo(t, dir, "build", "-o", bin, ".")
syms := runGo(t, dir, "tool", "nm", bin)
// 前提校验:验证符号确实被链入,否则测试没有意义。
if !strings.Contains(syms, ".KeyFor") {
t.Fatal("fixture 未链入 TrustChain 验证符号,测试前提不成立")
}
for _, banned := range []string{
"SignToken", "SignBundle", "GenerateSigningKey", "GenerateRootKey",
"EncryptPrivateKey", "DecryptPrivateKey",
} {
if strings.Contains(syms, banned) {
t.Errorf("纯验证二进制链入了签名符号 %q", banned)
}
}
}
// TestVerifierPackagesDoNotImportSign 在包依赖层面兜底:验证侧包不得 import sign。
func TestVerifierPackagesDoNotImportSign(t *testing.T) {
if testing.Short() {
t.Skip("短模式跳过依赖检查")
}
out := runGo(t, moduleRoot(t), "list", "-deps", "./crypto", "./sdk")
if strings.Contains(out, sdkModule+"/sign") {
t.Errorf("验证侧包依赖了 sign 包:\n%s", out)
}
}